Пример #1
0
    /**
    * Starts data import
    * @throws Exception
    */
    public function import() {
        $this->setuUpCsv();
        $row = '';
        $mastroProduct = new MastroProduct($this);
        $rowCount = 0;
        $byteCount = 0;
        while (($buffer = fgets($this->mastroCsvHandle, 4096)) !== false) {
            $byteCount += (strlen($buffer) + 2);
            $lastchar = ord(substr($buffer, strlen($buffer) - 1));

            while (
            $lastchar == 10 ||
            $lastchar == 13
            ) {
                $buffer = substr($buffer, 0, strlen($buffer) - 1);
                $lastchar = ord(substr($buffer, strlen($buffer) - 1));
            }
            if (is_resource($this->mastroUtf8CsvHandle))
                fwrite($this->mastroUtf8CsvHandle , iconv('WINDOWS-1252', 'UTF-8', $buffer ). PHP_EOL);
            if (strlen($row) > 0)
                $row .= '<br/>';
            $row .= $buffer;
            if (preg_match('/\*\*$/', $buffer)) {

                $row = iconv('WINDOWS-1252', 'UTF-8', $row);
                $mastroProduct->importFromCsvRow($row);
                $magentoProductArray = $mastroProduct->createMagentoProduct();
                foreach($magentoProductArray as  $magentoProduct) {
                  if ($magentoProduct instanceof MagentoProduct) {
                      if (ftell($this->magentoCsvHandler) == 0)
                          fwrite($this->magentoCsvHandler, "\xEF\xBB\xBF".$magentoProduct->getCsvHeaders() . PHP_EOL);
                      fwrite($this->magentoCsvHandler, $magentoProduct->getCsvRow() . PHP_EOL);
                  }
                }
                $rowCount++;
                $row = '';
                if ($rowCount / 100 == (int) ($rowCount / 100)) {
                    $microtime = microtime(true) - $this->startTime;
                    $rimaningTime = $microtime * $this->mastroCsvLastpos / $byteCount - $microtime;
                    $progress = 'Progress:' . intval($byteCount / $this->mastroCsvLastpos * 100 - 1) . " %\t";
                    $progress .= 'Products: ' . $rowCount . "\t";
                    $progress .= 'Remaning time: ' . intval($rimaningTime / 60 + 1) . "m\t";
                    $progress .= 'ETA:' . date('G:i:s', $this->startTime + $microtime + $rimaningTime) . PHP_EOL;
                    echo $progress;
                    file_put_contents($this->lock, $progress);
                    $this->setUpFtp();
                    if (is_resource($this->ftp)) {
                        ftp_chdir($this->ftp, $this->config['FTP_BASE_DIR']);
                        foreach (array('media','import') as $dir) {
                            $fileList = ftp_nlist($this->ftp,'.');
                            if (is_array($fileList) && !in_array($dir, $fileList)) {
                                ftp_mkdir($this->ftp,$dir);
                            }
                            ftp_chdir($this->ftp,$dir);
                            ftp_put($this->ftp, 'progress.txt', $this->lock, FTP_ASCII);
                        }
                    }
                }
            }
        }
        $this->uploadCsv();
        $this->downloadBackup();
        $this->execOnShutdown();
    }