2 namespace Utility\Import;
4 class CsvSemicolon implements ImportInterface
6 private $_isExtractedFile = false;
9 private $_delimiter = ';';
10 private $_enclosure = '"';
11 private $_escape = '\\';
12 private $_recordLength = 0;
13 private $_specialFields = array();
14 protected $_headers = array();
17 const FETCH_ASSOC = 2;
20 public function __construct($file, $firstLineIsHeaders = true)
22 $this->_fileName = $file;
23 if (!file_exists($this->_fileName))
25 throw new \Exception('File ' . $this->_fileName . ' not found');
27 $this->handleZipArchive();
28 $this->_fileHandle = fopen($this->_fileName, 'r');
29 if ($firstLineIsHeaders)
31 $this->_headers = $this->getRecord();
38 protected function handleZipArchive()
40 if (strpos($this->_fileName, '.zip'))
42 #-> Extract csv from zip archive.
43 $this->processZipFile();
50 protected function processZipFile()
52 $zip = new \ZipArchive();
53 if ($this->zipArchiveSuccessfullyOpened($zip))
55 $this->extractZipFile($zip);
57 #-> Find the csv file.
58 $this->establishFilePath();
62 throw new \Exception('CSV Importer Error: Could not open zip archive!');
70 protected function zipArchiveSuccessfullyOpened(\ZipArchive $zip)
72 return true === $zip->open($this->_fileName);
79 protected function extractZipFile(\ZipArchive $zip)
81 if (false === $zip->extractTo(getcwd() . '/data/extract'))
83 throw new \Exception('CSV Import Utility: Could open but not extract zip archive!');
88 protected function establishFilePath()
90 $fileList = scandir(getcwd() . '/data/extract/');
97 foreach ($fileList as $fileEntry)
99 $numFiles = $this->processFileEntry($ignoreFiles, $fileEntry, $numFiles);
101 $this->ensureSingleCsvFileInZip($numFiles);
102 $this->_isExtractedFile = true;
106 * @param $ignoreFiles
111 protected function processFileEntry($ignoreFiles, $fileEntry, $numFiles)
113 if (isset($ignoreFiles[$fileEntry]))
117 if (strpos($fileEntry, '.csv'))
119 return $this->getFilePath($fileEntry, $numFiles);
123 unlink(getcwd() . '/data/extract/' . $fileEntry);
133 protected function getFilePath($fileEntry, $numFiles)
138 unlink($this->_fileName);
140 $this->_fileName = getcwd() . '/data/extract/' . $fileEntry;
148 protected function ensureSingleCsvFileInZip($numFiles)
152 unlink($this->_fileName);
153 throw new \Exception('CSV Importer Error: More than 1 csv file in zip archive!');
157 unlink($this->_fileName);
158 throw new \Exception('CSV Importer Error: 0 csv files in zip archive!');
162 public function getHeaders()
164 return $this->_headers;
167 public function setDelimiter($delimiter)
169 $this->_delimiter = $delimiter;
170 if (!empty($this->_headers))
173 $this->_headers = $this->getRecord(self::FETCH_LAZY);
177 public function setEnclosure($enclosure)
179 $this->_enclosure = $enclosure;
182 public function setEscape($escape)
184 $this->_escape = $escape;
187 public function setRecordLength($length)
189 $this->_recordLength = $length;
193 * Gets the next record in the file
196 public function getRecord($method = self::FETCH_BOTH)
198 $record = fgetcsv($this->_fileHandle, $this->_recordLength, $this->_delimiter, $this->_enclosure, $this->_escape);
200 \Utility\Debug::errorLog('$record', $record);
202 if($record === false)
207 $fieldsCount = count($record);
208 foreach ($this->_specialFields as $key => $value)
210 $record[$key] = $value->parse($record[$key]);
212 if (($method & self::FETCH_ASSOC) && !empty($this->_headers))
214 for($i = 0; $i < count($this->_headers); $i++)
215 $record[$this->_headers[$i]] = $record[$i];
217 if (!($method & self::FETCH_LAZY))
219 for ($i = 0; $i < $fieldsCount; $i++)
228 public function reset()
230 fseek($this->_fileHandle, 0);
233 public function setField($fieldNr, ImportInterface $class)
235 $this->_specialFields[$fieldNr - 1] = $class;
238 public function __destruct()
240 fclose($this->_fileHandle);
241 if ($this->_isExtractedFile)
243 unlink($this->_fileName);