CoreCommunity ExtensionsIncubatorDistributionsTYPO3 4.5 ProjectsTYPO3 4.6 ProjectsTYPO3 4.7 ProjectsTYPO3 6.0 ProjectsTYPO3 6.1 ProjectsTYPO3 6.2 Projects (+)

Bug #9342

Problems with quotes

Added by Felix Nagel almost 3 years ago. Updated over 2 years ago.

Status:Resolved Start date:2010-08-17
Priority:Must have Due date:
Assignee:Reinhard Führicht % Done:

100%

Category:-
Target version:Stable v1.0
Votes: 0

Description

When exporting entries as csv the fields are splitted with commas and surrounded by double quotes. When an entry contains quotes ("normal" double quotes or german quotes) this messes up the whole csv when try to import in openoffice or MS office).

9342.patch (376 Bytes) Reinhard Führicht, 2010-08-18 13:28

parsecsv.lib.php (23.3 kB) Felix Nagel, 2010-08-24 16:49

newCsvLib.patch (3 kB) Felix Nagel, 2010-08-24 16:49

formhandler_generator_csv_newCsvLib.patch (3.1 kB) Felix Nagel, 2010-09-03 11:49

History

Updated by Reinhard Führicht almost 3 years ago

See attached patch for a proposal to fix this. It justs replaces " with "". As far as I understand the CSV "standard", this is the way to go. Can you test this please?

Updated by Felix Nagel almost 3 years ago

This works so far, but there a still problems with the csv parser.

Office cant import the generated csv files correctly, but OpenOffice is useable. I think this is a problem of newlines and some other UTF.8 codes.

Perhaps we should use another PHP class for doing that. As this is hard to test without real life data, I will do some tests with the following classes:

This TYPO3 Extension would enable formhandler to write even xls directly:
http://typo3.org/documentation/document-library/extension-manuals/phpexcel_library/1.7.0/view/toc/0/

http://code.google.com/p/parsecsv-for-php/

A PEAR CLASS (has dependencies, not my first choice)
http://pear.php.net/package/File_CSV

Any more ideas?

Updated by Felix Nagel almost 3 years ago

Is it possible that there is already a similar patch (like the one you provided above) in the function generateFrontendCSV in the Tx_Formhandler_Generator_CSV.php file?

public function generateFrontendCSV($params, $exportParams = array()) {
        //require class for $this->csv
        require_once('typo3conf/ext/formhandler/Resources/PHP/csv.lib.php');

        //build data
        foreach($params as $key => &$value) {
            if(is_array($value)) {
                $value = implode(',', $value);
            }
            if(count($exportParams) > 0 && !in_array($key, $exportParams)) {
                unset($params[$key]);
            }
            $value = str_replace('"', '""', $value);
        }

        //init csv object
        $this->csv = new export2CSV(',', "\n");
        $data[0] = $params;

        //generate file
        $this->csv = $this->csv->create_csv_file($data);
        header('Content-type: application/eml');
        header('Content-Disposition: attachment; filename=formhandler.csv');
        echo $this->csv;
        die();
    }    

but the

            if(is_array($value)) {
                $value = implode(',', $value);
            }

and


            $value = str_replace('"', '""', $value);

part are not in use, as the $value var is never ever used?

Updated by Reinhard Führicht almost 3 years ago

Yeah, it seems as if I patched it some time, but only for the frontend CSV. Nice work by me! :-)
Anyway, it would be great if you could test it with real life data. If you ask me, generating xls would be too much, as CSV can be opened easily in in excel and others (if the format is correct :-)).

I think, the best will be to fix the CSV bug first to make the file readable in Excel and OpenOffice. And afterwards we can discuss about generateing xls or not.

Updated by Felix Nagel almost 3 years ago

Our problem is based on something i would call a "word-copy-and-paste-char-problem". Some users paste word text in or textareas and some of these chars are written to the DB and later cause problems when opening in excel (not in openoffice).

Some example what data we get (pass is formhandler):
http://miranda.pastebin.ca/EXCIV7BJ

Did I forget to use an option in my form TS which activates "RTE-Cleaning" before saving?

Aside from the above mentioned problem I would recommend to use the parsecsv class as its stable, under active development, is well configurable and parses the values for quotes, newline, etc:
http://code.google.com/p/parsecsv-for-php/

Its very easy to use (im using the stable version):

    public function generateModuleCSV($records, $exportParams = array()) {

        //require class for $this->csv
        require_once('../../../Resources/PHP/parsecsv.lib.php');
        $data = array();        
        $dataSorted = array();

        //build data array
        foreach($records as $record) {
            if(!is_array($record['params'])) {
                $record['params'] = array();
            }
            foreach($record['params'] as &$param) {
                if(is_array($param)) {
                    $param = implode(';', $param);
                }
            }
            $data[] = $record['params'];
        }
        if(count($exportParams) > 0) {
            foreach($data as &$params) {
                foreach($params as $key => $value) {
                    if(!in_array($key, $exportParams)) {
                        unset($params[$key]);
                    }
                }
            }
        }

        // sort data
        foreach($data as $array) {
            debug($array);
            $dataSorted[] = $this->sortArrayByArray($array, $exportParams);
        }            

        $csv = new parseCSV();
        $csv->output (true, 'formhandler.csv', $dataSorted);
        die();
    }

as an example for the BE download option. Should be easily adopted for FE.

Im on vacation the next few days, so Im not able to test -- please consider this when waiting for an answer.

Updated by Reinhard Führicht over 2 years ago

Regarding the "RTE-Cleaning" problem: AFAIK, there is no built-in solution for that. But if you ask me, this could easily be done with a saveInterceptor.

parsecsv looks promising. Let's talk about that after your vacation.

Updated by Felix Nagel over 2 years ago

I'm back and allocated some time to test the current svn version. I hopefully will answer some of the open issues the next few hours.

Updated by Felix Nagel over 2 years ago

Here is a patch for using the new csv lib.

FE should be tested.

$value = str_replace('"', '""', $value);

seems to be unnecessary.

Updated by Reinhard Führicht over 2 years ago

I just started testing. Looks promising.

Updated by Felix Nagel over 2 years ago

Please use the newly attached patch instead as I fixed a problem and generates this against the latest trunk. Please note: before you apply this patch, the latest patch in http://forge.typo3.org/issues/9328 MUST be used! This is needed because of the remove empty fields loop.

Updated by Reinhard Führicht over 2 years ago

  • Status changed from New to Needs Feedback

Committed to trunk. Please test yourself. For me it looks OK.

Updated by Felix Nagel over 2 years ago

Nice! Works great so far!
Please note Im only testing BE not FE.

Updated by Reinhard Führicht over 2 years ago

  • Status changed from Needs Feedback to Resolved
  • Assignee set to Reinhard Führicht
  • Target version set to Stable v1.0
  • % Done changed from 0 to 100

After recent changes to make CSV and PDF generation in FE work again, I had a look at the generated CSV. Works fine for me.
Issue closed. For any further problem, please open a new issue.

Also available in: Atom PDF