| @@ -6,8 +6,15 @@ | ||
| 6 | 6 | |
| 7 | 7 | class CSVFile extends File |
| 8 | 8 | { |
| 9 | 9 | private $columns; |
| 10 | + private $setup = false; | |
| 11 | + | |
| 12 | + protected $default_delimiter = ","; | |
| 13 | + protected $default_enclosure = "\""; | |
| 14 | + protected $default_escape = "\\"; | |
| 15 | + protected $default_utf8_bom = false; | |
| 16 | + | |
| 10 | 17 | public function start() |
| 11 | 18 | { |
| 12 | 19 | $fields = $this->exporter->getFields(); |
| 13 | 20 | |
| @@ -14,12 +21,33 @@ | ||
| 14 | 21 | $this->columns = array_reduce($fields, function ($carry, $item) { |
| 15 | 22 | $carry[$this->getFieldLabel($item)] = isset($item['selection']) ? $item['selection'] : ''; |
| 16 | 23 | return $carry; |
| 17 | 24 | }, []); |
| 25 | + | |
| 26 | + list($delimiter, $enclosure, $escape, $utf8_bom) = $this->get_csv_settings(); | |
| 27 | + | |
| 28 | + // output uft8 BOM | |
| 29 | + if($utf8_bom){ | |
| 30 | + fwrite($this->fh, "\xEF\xBB\xBF"); | |
| 31 | + } | |
| 32 | + | |
| 18 | 33 | // write headers |
| 19 | - fputcsv($this->fh, array_keys($this->columns)); | |
| 34 | + fputcsv($this->fh, array_keys($this->columns), $delimiter, $enclosure, $escape); | |
| 35 | + | |
| 36 | + update_option('iwp_exporter_csv_config', [ | |
| 37 | + 'columns' => $this->columns | |
| 38 | + ]); | |
| 20 | 39 | } |
| 21 | 40 | |
| 41 | + public function loadConfig() | |
| 42 | + { | |
| 43 | + if (!$this->setup) { | |
| 44 | + $config = get_option('iwp_exporter_csv_config', []); | |
| 45 | + $this->columns = $config['columns']; | |
| 46 | + $this->setup = true; | |
| 47 | + } | |
| 48 | + } | |
| 49 | + | |
| 22 | 50 | /** |
| 23 | 51 | * @param MapperData $mapper |
| 24 | 52 | * @return void |
| 25 | 53 | */ |
| @@ -24,10 +52,13 @@ | ||
| 24 | 52 | * @return void |
| 25 | 53 | */ |
| 26 | 54 | public function add($mapper) |
| 27 | 55 | { |
| 56 | + $this->loadConfig(); | |
| 28 | 57 | $data = []; |
| 29 | 58 | |
| 59 | + list($delimiter, $enclosure, $escape) = $this->get_csv_settings(); | |
| 60 | + | |
| 30 | 61 | $max = $mapper->get_total_records(); |
| 31 | 62 | if ($max <= 0) { |
| 32 | 63 | $max = 1; |
| 33 | 64 | } |
| @@ -38,10 +69,24 @@ | ||
| 38 | 69 | $tmp = $mapper->get_value($item, $record[0]); |
| 39 | 70 | return is_array($tmp) ? implode(',', $tmp) : $tmp; |
| 40 | 71 | }, $this->columns); |
| 41 | 72 | |
| 42 | - fputcsv($this->fh, $data); | |
| 73 | + fputcsv($this->fh, $data, $delimiter, $enclosure, $escape); | |
| 43 | 74 | } |
| 75 | + } | |
| 76 | + | |
| 77 | + public function get_csv_settings() | |
| 78 | + { | |
| 79 | + $delimiter = $this->exporter->getFileSetting('delimiter', $this->default_delimiter); | |
| 80 | + $enclosure = $this->exporter->getFileSetting('enclosure', $this->default_enclosure); | |
| 81 | + $escape = $this->exporter->getFileSetting('escape', $this->default_escape); | |
| 82 | + $utf8_bom = $this->exporter->getFileSetting('utf8_bom', $this->default_utf8_bom); | |
| 83 | + | |
| 84 | + if ($enclosure === $escape) { | |
| 85 | + $escape = ''; | |
| 86 | + } | |
| 87 | + | |
| 88 | + return [$delimiter, $enclosure, $escape, $utf8_bom]; | |
| 44 | 89 | } |
| 45 | 90 | |
| 46 | 91 | public function end() |
| 47 | 92 | { |