Skip to content

Feed CSV export API writes "null" for zero values as well as for missing values #1895

@richardloxley

Description

@richardloxley

When using the feed API to export a CSV file (.../feed/data.json?id=...&csv=1), the downloaded CSV file contains null for values that should be 0.

The bug seems to be in Modules/feed/engine/shared_helper.php in the functions csv_write and csv_write_multi:

    public function csv_write($time,$value) {
        $time = $this->format_time($time);
        if ($value!=null) {
            $value = number_format($value,$this->csv_dp,$this->csv_dp_separator,'');
        } else {
            $value = 'null';
        }
        fwrite($this->export_fh,$time.$this->csv_field_separator.$value."\n");
    }

    public function csv_write_multi($values) {
        // $values[0] = $this->format_time($values[0]);

        for ($z=1; $z<count($values); $z++) {
            if ($values[$z]==null) {
                $values[$z] = 'null';
            } else {
                $values[$z] = number_format($values[$z],$this->csv_dp,$this->csv_dp_separator,'');
            }
        }
        fwrite($this->export_fh,implode($this->csv_field_separator,$values)."\n");
    }

The lines if ($value!=null) { and if ($values[$z]==null) { should use the PHP identity operator === rather than equality == to distinguish between null values and zero values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions