<commit_before><?php
/**
 * @version		$Id$
 * @category	Koowa
 * @package     Koowa_View
 * @copyright	Copyright (C) 2007 - 2010 <NAME>. All rights reserved.
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link     	http://www.nooku.org
 */

/**
 * Export a view as a CSV file
 * 
 * @author		<NAME> <<EMAIL>>
 * @category	Koowa
 * @package     Koowa_View 
 */
class KViewCsv extends KViewFile
{
	/**
	 * Character used for quoting
	 * 
	 * @var string
	 */
	public $quote = '"';
    
	/**
	 * Character used for separating fields
	 * 
	 * @var string
	 */
	public $separator = ',';
	
	/**
	 * End of line
	 * 
	 * @var string
	 */
	public $eol = "\n";
	
	/**
	 * Initializes the options for the object
	 *
	 * Called from {@link __construct()} as a first step of object instantiation.
	 *
	 * @param 	object 	An optional KConfig object with configuration options
	 * @return  void
	 */
	protected function _initialize(KConfig $config)
	{
		$config->append(array(
			'mimetype'	  => 'text/csv',
			'disposition' => 'inline',
			'quote'		  => '"',
			'separator'	  => ',',
			'eol'		  => "\n"
       	));
       	
       	parent::_initialize($config);
    }
	
	/**
	 * Return the views output
 	 *
	 *  @return string 	The output of the view
	 */
	public function display()
	{
		//Get the rowset
		$rowset = $this->getModel()->getList();
		
		// Header
		$this->output .= $this->_arrayToString($rowset->getColumns()).$this->eol;
		
		// Data
		foreach($rowset as $row) {
			$this->output .= $this->_arrayToString($row->toArray()).$this->eol;
		}
	 	
		return parent::display();
	}
		
	/**
     * Render 
     * 
     * @param	string	Value
     * return 	boolean
     */
	protected function _arrayToString($data)
    {
    	$fields = array();
        foreach($data as $value)
        {
            if ($this->_quoteValue($value)) 
            {
                // Escape the quote character within the field (e.g. " becomes "")
                $quoted_value = str_replace($this->quote, $this->quote.$this->quote, $value);
                $fields[] 	  = $this->quote . $quoted_value . $this->quote;
            } 
            else $fields[] = $value;
        }

        return  implode($this->separator, $fields);
    }
	
    /**
     * Check if the value should be quoted
     * 
     * @param	string	Value
     * return 	boolean
     */
    protected function _quoteValue($value)
    {
    	if(is_numeric($value)) {
    		return false;
    	}
    	
        if(strpos($value, $this->separator) !== false) { // Separator is present in field
        	return true;
        }
        
        if(strpos($value, $this->quote) !== false) { // Quote character is present in field
        	return true;
        }
        <commit_msg>KViewCvs now builds the header based on all the columns in the rowset. If a row value is an array it's imploded using a ',' to a string. This change allows you to easily limit the data that is being displayed using the KDatabaseRowAbstract::toArray() function. <commit_after><?php
/**
 * @version		$Id$
 * @category	Koowa
 * @package     Koowa_View
 * @copyright	Copyright (C) 2007 - 2010 <NAME>. All rights reserved.
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link     	http://www.nooku.org
 */

/**
 * Export a view as a CSV file
 * 
 * @author		<NAME> <<EMAIL>>
 * @category	Koowa
 * @package     Koowa_View 
 */
class KViewCsv extends KViewFile
{
	/**
	 * Character used for quoting
	 * 
	 * @var string
	 */
	public $quote = '"';
    
	/**
	 * Character used for separating fields
	 * 
	 * @var string
	 */
	public $separator = ',';
	
	/**
	 * End of line
	 * 
	 * @var string
	 */
	public $eol = "\n";
	
	/**
	 * Initializes the options for the object
	 *
	 * Called from {@link __construct()} as a first step of object instantiation.
	 *
	 * @param 	object 	An optional KConfig object with configuration options
	 * @return  void
	 */
	protected function _initialize(KConfig $config)
	{
		$config->append(array(
			'mimetype'	  => 'text/csv',
			'disposition' => 'inline',
			'quote'		  => '"',
			'separator'	  => ',',
			'eol'		  => "\n"
       	));
       	
       	parent::_initialize($config);
    }
	
	/**
	 * Return the views output
 	 *
	 *  @return string 	The output of the view
	 */
	public function display()
	{
		$columns = array();
	    
	    //Get the rowset
		$rowset = $this->getModel()->getList();
		
		// Rows
		$rows = '';
		foreach($rowset as $row) 
		{
			$data    = $row->toArray();
		    $columns = array_merge($columns, array_keys($data));
		    
		    $rows .= $this->_arrayToString(array_values($data)).$this->eol;
		}
		
		// Header
		$header = $this->_arrayToString($columns).$this->eol;
		
		// Set the output
		$this->output = $header.$rows;
	 	
		return parent::display();
	}
		
	/**
     * Render 
     * 
     * @param	string	Value
     * return 	boolean
     */
	protected function _arrayToString($data)
    {
    	$fields = array();
        foreach($data as $value)
        {
            //Implode array's
            if(is_array($value)) {
    	        $value = implode(',', $value);
    	    }
            
    	     // Escape the quote character within the field (e.g. " becomes "")
            if ($this->_quoteValue($value)) 
            { 
                $quoted_value = str_replace($this->quote, $this->quote.$this->quote, $value);
                $fields[] 	  = $this->quote . $quoted_value . $this->quote;
            } 
            else $fields[] = $value;
        }

        return  implode($this->separator, $fields);
    }
	
    /**
     * Check if the value should be quoted
     * 
     * @param	string	Value
     * return 	boolean
     */
    protected function _quoteValue($value)
    {
        if(is_numeric($value)) {
    		return false;
    	}
    	
        if(strpos($value, $this->separator) !== false) { // Separator is present in field
        	return true;
        }
        
        if(strpos($value, $this->quote) !== false) { // Quote character is present in field
        	return true;
        }
        
{'fraction_non_alphanumeric': 0.14732660155603378, 'fraction_numerical': 0.0029796391325939415, 'mean_word_length': 2.0300902708124373, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4419378', 'n_tokens_mistral': 2111, 'n_tokens_neox': 1905, 'n_words': 493}