Пример #1
0
 public function getCsvFile()
 {
     \DB::connection()->disableQueryLog();
     $tmpName = tempnam(storage_path() . 'csv', 'csv');
     $csvFile = new \Keboola\Csv\CsvFile($tmpName);
     $csvFile->writeRow(array_values($this->csv_columns));
     // Determine how many chunks to get
     $limit = 1000;
     $count = $this->query->count();
     $chunks = ceil($count / $limit);
     for ($i = 0; $i < $chunks; $i++) {
         $this->query->take($limit)->skip($i * $limit);
         $data = $this->query->get();
         foreach ($data as $row) {
             $csvRow = array();
             foreach (array_keys($this->csv_columns) as $field) {
                 $csvRow[] = $this->getColumnData($row, $field);
             }
             $csvFile->writeRow($csvRow);
         }
     }
     return $tmpName;
 }
Пример #2
0
 public function queue_import_csv($filename)
 {
     only_admin_access();
     if (!is_file($filename)) {
         return array('error' => "You have not provided a existing backup to restore.");
     }
     $csv = new \Keboola\Csv\CsvFile($filename);
     $head = $csv->getHeader();
     if (!isset($head[2])) {
         $csv = new \Keboola\Csv\CsvFile($filename, ';');
         $head = $csv->getHeader();
     } else {
         if (isset($head[0]) and stristr($head[0], ';')) {
             $csv = new \Keboola\Csv\CsvFile($filename, ';');
             $head = $csv->getHeader();
         }
     }
     if (empty($head) or empty($csv)) {
         return array('error' => "CSV file cannot be parsed properly.");
     }
     $rows = array();
     $i = 0;
     foreach ($csv as $row) {
         if ($i > 0) {
             $r = array();
             if (is_array($row)) {
                 foreach ($row as $k => $v) {
                     if (isset($head[$k])) {
                         $row[$head[$k]] = $v;
                         $new_k = strtolower($head[$k]);
                         $new_k = str_replace(' ', '_', $new_k);
                         $new_k = str_replace('__', '_', $new_k);
                         // $new_k = preg_replace("/[^a-zA-Z0-9_]+/", "", $new_k);
                         $new_k = rtrim($new_k, '_');
                         $r[$new_k] = $v;
                     }
                 }
             }
             $rows[] = $r;
         }
         $i++;
     }
     $content_items = $rows;
     $content_items = $this->map_array($rows);
     return $this->batch_save($content_items);
 }