<commit_before><?php

namespace Admingenerated\{{ namespace_prefix }}{{ bundle_name }}\{{ builder.generator.GeneratedControllerFolder }};

{{- block('security_use') }}

{{- block('csrf_protection_use') }}

use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

/**
 * @author <NAME>
 */
class ExcelController extends ListController
{
    /**
     * Generates the Excel object and send a streamed response
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
     */
    public function excelAction()
    {
        // Create the PHPExcel object with some standard values
        try {
          $phpexcel = $this->get('phpexcel');
        } catch (ServiceNotFoundException $e){
          throw new \Exception('You will need to enable the PHPExcel bundle for this function to work.', null, $e);
        }

        $phpExcelObject = $phpexcel->createPHPExcelObject();
        $this->createExcelObject($phpExcelObject);
        $sheet = $phpExcelObject->setActiveSheetIndex(0);

        // Create the first bold row in the Excel spreadsheet
        $this->createExcelHeader($sheet);

        // Print the data
        $this->createExcelData($sheet);

        // Create the Writer, Response and add header
        $writer = $phpexcel->createWriter($phpExcelObject, '{{ builder.filetype }}');
        $response = $phpexcel->createStreamedResponse($writer);
        $response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8');
        $response->headers->set('Content-Disposition', 'attachment;filename={{ builder.filename }}.xlsx');

        return $response;
    }

    /**
     * Override this method to add your own creator, title and more to the Excel spreadsheet
     */
    protected function createExcelObject(\PHPExcel $phpExcelObject)
    {
        $phpExcelObject->getProperties()->setCreator("AdminGeneratorBundle")
          ->setTitle('AdminGenerator Excel Export')
          ->setSubject("AdminGenerator Excel Export")
          ->setDescription("AdminGenerator Excel export");
    }

    /**
     * Fill the Excel speadsheet with the headers
     */
    protected function createExcelheader(\PhpExcel_Worksheet $sheet)
    {
        $translator = $this->get('translator');
        
        {% set colNum = 0 %}
        {% for column in builder.columns %}
        $sheet->setCellValueByColumnAndRow({{ colNum }}, 1, $translator->trans("{{ column.label }}", array(), '{{ i18n_catalog|default("Admin") }}'), true);
        $columnLetter = \PHPExcel_Cell::stringFromColumnIndex({{ colNum }});
        $sheet->getStyle($columnLetter . '1')->getFont()->setBold(true);
        $sheet->getColumnDimension($columnLetter)->setAutoSize(true);

        {% set colNum = colNum + 1 %}
        {% endfor %}
    }

    /**
     * Fills the Excel spreadsheet with data
     */
    protected function createExcelData(\PhpExcel_Worksheet $sheet)
    {
        $row = 2;
        $results = $this->getResults();

        {% set column = 0 %}
        foreach($results as $rowData) {
            {% for builder_column in builder.columns %}
            // Retrieve relations
            $getter = '{{ builder_column.getter }}';
            $data = $rowData;

            while (($pos = strpos($getter, '.')) > 0){
                $tempGetter = 'get' . ucfirst(substr($getter, 0, $pos));
                $getter = substr($getter, $pos + 1);
                $data = $data->$tempGetter();
            }

            $getter = 'get' . ucfirst($getter);
            $data = $data->$getter();

            // Convert DateTime object to given format
            if ($data instanceof \DateTime){
                $data = $data->format('{{ builder.dateTimeFormat }}');
            }

            $sheet->setCellValueByColumnAndRow({{ column }}, $row, $data);
            {% set column = column + 1 %}
            {% endfor %}
<commit_msg>Check if method exists, default to null<commit_after><?php

namespace Admingenerated\{{ namespace_prefix }}{{ bundle_name }}\{{ builder.generator.GeneratedControllerFolder }};

{{- block('security_use') }}

{{- block('csrf_protection_use') }}

use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

/**
 * @author <NAME>
 */
class ExcelController extends ListController
{
    /**
     * Generates the Excel object and send a streamed response
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
     */
    public function excelAction()
    {
        // Create the PHPExcel object with some standard values
        try {
          $phpexcel = $this->get('phpexcel');
        } catch (ServiceNotFoundException $e){
          throw new \Exception('You will need to enable the PHPExcel bundle for this function to work.', null, $e);
        }

        $phpExcelObject = $phpexcel->createPHPExcelObject();
        $this->createExcelObject($phpExcelObject);
        $sheet = $phpExcelObject->setActiveSheetIndex(0);

        // Create the first bold row in the Excel spreadsheet
        $this->createExcelHeader($sheet);

        // Print the data
        $this->createExcelData($sheet);

        // Create the Writer, Response and add header
        $writer = $phpexcel->createWriter($phpExcelObject, '{{ builder.filetype }}');
        $response = $phpexcel->createStreamedResponse($writer);
        $response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8');
        $response->headers->set('Content-Disposition', 'attachment;filename={{ builder.filename }}.xlsx');

        return $response;
    }

    /**
     * Override this method to add your own creator, title and more to the Excel spreadsheet
     */
    protected function createExcelObject(\PHPExcel $phpExcelObject)
    {
        $phpExcelObject->getProperties()->setCreator("AdminGeneratorBundle")
          ->setTitle('AdminGenerator Excel Export')
          ->setSubject("AdminGenerator Excel Export")
          ->setDescription("AdminGenerator Excel export");
    }

    /**
     * Fill the Excel speadsheet with the headers
     */
    protected function createExcelheader(\PhpExcel_Worksheet $sheet)
    {
        $translator = $this->get('translator');
        
        {% set colNum = 0 %}
        {% for column in builder.columns %}
        $sheet->setCellValueByColumnAndRow({{ colNum }}, 1, $translator->trans("{{ column.label }}", array(), '{{ i18n_catalog|default("Admin") }}'), true);
        $columnLetter = \PHPExcel_Cell::stringFromColumnIndex({{ colNum }});
        $sheet->getStyle($columnLetter . '1')->getFont()->setBold(true);
        $sheet->getColumnDimension($columnLetter)->setAutoSize(true);

        {% set colNum = colNum + 1 %}
        {% endfor %}
    }

    /**
     * Fills the Excel spreadsheet with data
     */
    protected function createExcelData(\PhpExcel_Worksheet $sheet)
    {
        $row = 2;
        $results = $this->getResults();

        {% set colNum = 0 %}
        foreach($results as $rowData) {
            {% for column in builder.columns %}
            // Retrieve relations
            $getter = '{{ column.getter }}';
            $data = $rowData;

            while (($pos = strpos($getter, '.')) > 0){
                $tempGetter = 'get'.ucfirst(substr($getter, 0, $pos));
                $getter = substr($getter, $pos + 1);
                $data = method_exists($data, $tempGetter) ? $data->$tempGetter() : null;
            }

            $getter = 'get'.ucfirst($getter);
            $data = method_exists($data, $getter) ? $data->$getter() : null;

            // Convert DateTime object to given format
            if ($data instanceof \DateTime){
                $data = $data->format('{{ builder.dateTimeFormat }}');
            }

            $sheet->setCellValueByColumnAndRow({{ colNum }}, $row, $data);
            {% set colNum = colNum + 1 %}
            {% endfor %}

{'fraction_non_alphanumeric': 0.1403486609398686, 'fraction_numerical': 0.0035371399696816574, 'mean_word_length': 2.5807327001356852, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '6203983', 'n_tokens_mistral': 2296, 'n_tokens_neox': 2124, 'n_words': 492}