<commit_before><?php
namespace Craft;

class ExportService extends BaseApplicationComponent 
{

    protected $delimiter = ExportModel::DelimiterComma;

    public function download($settings) 
    {
    
        // Get max power
        craft()->config->maxPowerCaptain();
        
        // Create the export template
        $export = "";
        
        // Get data
        $data = $this->getData($settings);
        
        // If there is data, process
        if(count($data)) {
        
            // Count rows
            $rows = 0;
            
            // Loop trough data
            foreach($data as $element) {
            
                $row = "";
                
                // Get fields
                $fields = $this->parseFields($settings['map'], $element);
                
                // Put down columns
                if(!$rows) {
                    $row .= $this->parseColumns($settings['entrytype'], $fields);
                }
            
                // Loop trough the fields
                foreach($fields as $handle => $data) {
                    
                    // Parse element data
                    $data = $this->parseElementData($handle, $data);
                    
                    // Parse field data
                    $data = $this->parseFieldData($handle, $data);
                
                    // Put in quotes and escape
                    $row .= '"'.addslashes($data).'"'.$this->delimiter;
                
                }
                
                // Remove last comma
                $row = substr($row, 0, -1);
                
                // Encode row
                $row = mb_convert_encoding(utf8_decode($row), "Windows-1252", "UTF-8");
                
                // And start a new line
                $row = $row . "\r\n";
                
                // Append to data
                $export .= $row;
                
                // Count rows
                $rows++;
            
            }
                        
            // Download the csv
            craft()->request->sendFile('export.csv', $export, array('forceDownload' => true, 'mimeType' => 'text/csv'));
            //echo $export;
                
        }
        
        // There's nothing beyond the export
        craft()->end();
    
    }
    
    protected function getData($settings) 
    {
    
        // Get other sources
        $sources = craft()->plugins->call('registerExportSource', array($settings));
        
        // Loop through sources, see if we can get any data
        $data = array();
        foreach($sources as $plugin) {
            if(is_array($plugin)) {
                foreach($plugin as $source) {
                    $data[] = $source;
                }
            }
        }
                        
        // If no data from source, get data by ourselves
        if(!count($data)) {
        
            // Find data
            $criteria = craft()->elements->getCriteria($settings['type']);
            $criteria->limit = null;
            $criteria->status = isset($settings['map']['status']) ? $settings['map']['status'] : null;
            
            // Entry specific data
            if($settings['type'] == ElementType::Entry) {
                $criteria->sectionId = $settings['section'];
                $criteria->type = $settings['entrytype'];
            }
            
            // User specific data
            if($settings['type'] == ElementType::User) {
                $criteria->groupId = $settings['groups'];
            }
            
            // Gather data
            $data = $criteria->find();
        
        }
        
        return $data;
    
    }
    
    // Parse fields
    protected function parseFields($map, $element) 
    {
    
        $fields = array();
    
        // Only get element attributes and content attributes
        if($element instanceof BaseElementModel) {
            $attributes = array_merge($element->getAttributes(), $element->getContent()->getAttributes());
        } else {
            $attributes = $element;
        }
        
        // Loop through the map
        foreach($map as $handle => $checked) {
        
            // Only get checked fields
            if($checked == '1') {
            
                // Fill them with data
                $fields[$handle] = $attributes[$handle];
            
            }
        
        }
        
        return $fields;
    
    }
    <commit_msg>Extra check if an attribute exists<commit_after><?php
namespace Craft;

class ExportService extends BaseApplicationComponent 
{

    protected $delimiter = ExportModel::DelimiterComma;

    public function download($settings) 
    {
    
        // Get max power
        craft()->config->maxPowerCaptain();
        
        // Create the export template
        $export = "";
        
        // Get data
        $data = $this->getData($settings);
        
        // If there is data, process
        if(count($data)) {
        
            // Count rows
            $rows = 0;
            
            // Loop trough data
            foreach($data as $element) {
            
                $row = "";
                
                // Get fields
                $fields = $this->parseFields($settings['map'], $element);
                
                // Put down columns
                if(!$rows) {
                    $row .= $this->parseColumns($settings['entrytype'], $fields);
                }
            
                // Loop trough the fields
                foreach($fields as $handle => $data) {
                    
                    // Parse element data
                    $data = $this->parseElementData($handle, $data);
                    
                    // Parse field data
                    $data = $this->parseFieldData($handle, $data);
                
                    // Put in quotes and escape
                    $row .= '"'.addslashes($data).'"'.$this->delimiter;
                
                }
                
                // Remove last comma
                $row = substr($row, 0, -1);
                
                // Encode row
                $row = mb_convert_encoding(utf8_decode($row), "Windows-1252", "UTF-8");
                
                // And start a new line
                $row = $row . "\r\n";
                
                // Append to data
                $export .= $row;
                
                // Count rows
                $rows++;
            
            }
                        
            // Download the csv
            craft()->request->sendFile('export.csv', $export, array('forceDownload' => true, 'mimeType' => 'text/csv'));
            //echo $export;
                
        }
        
        // There's nothing beyond the export
        craft()->end();
    
    }
    
    protected function getData($settings) 
    {
    
        // Get other sources
        $sources = craft()->plugins->call('registerExportSource', array($settings));
        
        // Loop through sources, see if we can get any data
        $data = array();
        foreach($sources as $plugin) {
            if(is_array($plugin)) {
                foreach($plugin as $source) {
                    $data[] = $source;
                }
            }
        }
                        
        // If no data from source, get data by ourselves
        if(!count($data)) {
        
            // Find data
            $criteria = craft()->elements->getCriteria($settings['type']);
            $criteria->limit = null;
            $criteria->status = isset($settings['map']['status']) ? $settings['map']['status'] : null;
            
            // Entry specific data
            if($settings['type'] == ElementType::Entry) {
                $criteria->sectionId = $settings['section'];
                $criteria->type = $settings['entrytype'];
            }
            
            // User specific data
            if($settings['type'] == ElementType::User) {
                $criteria->groupId = $settings['groups'];
            }
            
            // Gather data
            $data = $criteria->find();
        
        }
        
        return $data;
    
    }
    
    // Parse fields
    protected function parseFields($map, $element) 
    {
    
        $fields = array();
    
        // Only get element attributes and content attributes
        if($element instanceof BaseElementModel) {
            $attributes = array_merge($element->getAttributes(), $element->getContent()->getAttributes());
        } else {
            $attributes = $element;
        }
        
        // Loop through the map
        foreach($map as $handle => $checked) {
        
            // Only get checked fields
            if($checked == '1' && isset($attributes[$handle])) {
            
                // Fill them with data
                $fields[$handle] = $attributes[$handle];
            
            }
        
        }
        
        return $fields;
    
    }
    
{'fraction_non_alphanumeric': 0.12051902353199911, 'fraction_numerical': 0.0021992522542335605, 'mean_word_length': 1.0726982680036463, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '328752', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2093, 'n_words': 498}