Exemplo n.º 1
0
 function load()
 {
     parent::load();
     $lines = file($this->file_name);
     $this->updatedParts = array();
     $this->rel = array();
     $this->part = array();
     $this->count_parts = 0;
     array_shift($lines);
     // начало цикла по строкам
     foreach ($lines as $l) {
         \helpers\Debug::prePrintR($l);
         $row = explode("\t", trim($l));
         $this->processRow($row);
         if (count($this->rel) > $this->row_count) {
             $this->log .= 'Сохраняем кусок...<br>';
             $this->savePart();
         }
     }
     $this->log .= 'Сохраняем последний кусок...<br>';
     $this->savePart();
     $this->log .= 'Закончено! Загрузили записей ' . ($ws['max_row'] - $this->begin_row) . '<br>';
     $this->log .= 'Уникальных запчастей: ' . $this->count_parts . '<br>';
     \helpers\Debug::prePrintR($this->log);
     /* if (!$done_query)
     	  $db->exec($this->query); */
 }
Exemplo n.º 2
0
 function load()
 {
     parent::load();
     $exc = new \excel\ExcelFileParser("log.txt", ABC_NO_LOG);
     $res = $exc->ParseFromFile($this->file_name);
     // TODO: Вынести в конфиг сообщения
     switch ($res) {
         case 0:
             $this->log .= "Файл успешно прочитан <br>";
             break;
         case 1:
             die("Невозможно открыть файл");
         case 2:
             die("Файл, слишком маленький чтобы быть файлом Excel");
         case 3:
             die("Ошибка чтения заголовка файла");
         case 4:
             die("Ошибка чтения файла");
         case 5:
             die("Это - не файл Excel или файл, сохраненный в Excel < 5.0");
         case 6:
             die("Битый файл");
         case 7:
             die("В файле не найдены данные  Excel");
         case 8:
             die("Неподдерживаемая версия файла");
         default:
             die("Неизвестная ошибка");
     }
     $ws = $exc->worksheet['data'][0];
     $this->updatedParts = array();
     $this->rel = array();
     $this->part = array();
     $this->count_parts = 0;
     $this->exc = $exc;
     // если рабочий лист не пустой
     if (is_array($ws) && isset($ws['max_row']) && isset($ws['max_col'])) {
         // начало цикла по строкам
         for ($i = $this->begin_row; $i <= $ws['max_row']; $i++) {
             $this->processRow($ws['cell'][$i]);
             if (count($this->rel) > $this->row_count) {
                 $this->log .= 'Сохраняем кусок...<br>';
                 $this->savePart();
             }
         }
         $this->log .= 'Сохраняем последний кусок...<br>';
         $this->savePart();
         $this->log .= 'Закончено! Загрузили записей ' . ($ws['max_row'] - $this->begin_row) . '<br>';
         $this->log .= 'Уникальных запчастей: ' . $this->count_parts . '<br>';
         /*if (!$done_query)
           $db->exec($this->query);*/
     }
 }
 /**
  * This loads a CSV file and processes it line by line. Only the current row is stored in $this->data at any given time.
  * 
  * @see importer/BaseImporter::loadData()
  */
 public function loadData($path)
 {
     $this->properties->lines = 1;
     $sql = 'INSERT INTO ' . $this->properties->table . ' (' . implode(", ", $this->properties->columns) . ') VALUES (:' . implode(", :", $this->properties->columns) . ')';
     $statement = $this->properties->con->prepare($sql);
     try {
         $finfo = finfo_open(FILEINFO_MIME);
         if (substr(finfo_file($finfo, $path), 0, 4) != "text") {
             throw new ImporterException("Not a CSV document!");
         }
         $fp = fopen($path, "rt");
         if (!$fp) {
             throw new ImporterException("Couldn't read file!");
         }
         $first_row = 1;
         while (($row = fgetcsv($fp, 0, $this->properties->delimeter, $this->properties->enclosure)) !== false) {
             if (count($row) == 1 && !$row[0]) {
                 continue;
             }
             foreach ($row as &$value) {
                 $value = trim($value);
             }
             $this->data[] = $row;
             if ($first_row && $this->properties->has_header) {
                 $first_row = 0;
                 continue;
             }
             parent::insertData($statement);
             $this->properties->lines++;
             $this->properties->has_header = false;
             $this->properties->append = true;
             $this->data = array();
         }
         fclose($fp);
     } catch (Exception $e) {
         /*if($this->properties->lines > 1) {
         			parent::deleteData();
         		}*/
         throw new ImporterException("<b>File</b>: " . $path . "<br /><br /><b>Error</b>: " . $e->getMessage() . "<br/>");
     }
 }
 /**
  * Overrides the base method, to process multiple worksheets.
  * @see importer/BaseImporter::insertData()
  */
 public function insertData()
 {
     if (!$this->properties->worksheets_as_models) {
         $data = array();
         foreach ($this->data as $sheet) {
             foreach ($sheet as $row) {
                 $data[] = $row;
             }
         }
         $this->data = $data;
         parent::insertData();
     } else {
         $data = $this->data;
         foreach ($data as $sheetname => $sheetrows) {
             $this->properties->model = $sheetname;
             $this->data = $sheetrows;
             $this->properties->columns = null;
             parent::insertData();
         }
     }
 }