How to remove unwanted columns from Express form CSV export?

Here’s what I came up with, something dropped in /application/controllerss/single_page/dashboard/reports/forms.php

It feels very hacky, but at least it only impacts one function.

(updated with Nour’s improvements)

<?php
namespace Application\Controller\SinglePage\Dashboard\Reports;

use Concrete\Core\Controller\Traits\DashboardExpressEntryDetailsTrait;
use Concrete\Core\Controller\Traits\DashboardSelectableExpressEntryListTrait;
use Concrete\Core\Csv\WriterFactory;
use Concrete\Core\Entity\Express\Entity;
use Concrete\Core\Express\Export\EntryList\CsvWriter;
use Concrete\Core\Localization\Service\Date;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;

class Forms extends \Concrete\Controller\SinglePage\Dashboard\Reports
{
    use DashboardSelectableExpressEntryListTrait;
    use DashboardExpressEntryDetailsTrait;

    protected function exportCsv(Entity $entity, $searchMethod = null)
    {
        set_time_limit(0);
        $permissions = new \Permissions($entity);
        if (!$permissions->canViewExpressEntries()) {
            throw new \Exception(t('Access Denied'));
        }

        $headers = [
            'Content-Type' => 'text/csv',
            'Content-Disposition' => 'attachment; filename=' . $entity->getHandle() . '.csv',
        ];

        $config = $this->app->make('config');
        $bom = $config->get('concrete.export.csv.include_bom') ? $config->get('concrete.charset_bom') : '';
        $datetime_format_constant = $config->get('concrete.export.csv.datetime_format');
        if (!defined($datetime_format_constant)) {
            $datetime_format_constant = sprintf('DATE_%s', $datetime_format_constant);
        }
        if (defined($datetime_format_constant)) {
            $datetime_format = constant($datetime_format_constant);
        } else {
            $datetime_format = DATE_ATOM;
        }
        if ($searchMethod == 'advanced_search') {
            $query = $this->getQueryFactory()->createFromAdvancedSearchRequest(
                $this->getSearchProvider($entity),
                $this->request,
                Request::METHOD_GET
            );
        } else {
            $query = $this->createDefaultQuery($entity);
        }
        $result = $this->createSearchResult($entity, $query);
        $entryList = $result->getItemListObject();

        return new StreamedResponse(function () use ($entity, $entryList, $bom, $datetime_format) {
            $writer = $this->app->make(CsvWriter::class, [
                'writer' => $this->app->make(WriterFactory::class)->createFromPath('php://output', 'w')
                    ->addFormatter(function ($record) {
                        array_shift($record);
                        array_shift($record);
                        array_shift($record);
                        array_shift($record);
                        array_shift($record);
                        return $record;
                    })
                ,
                'dateFormatter' => new Date(),
                'datetime_format' => $datetime_format
            ]);
            echo $bom;
            $writer->insertHeaders($entity);
            $writer->insertEntryList($entryList);
        }, 200, $headers);
    }
}
1 Like