public function importCsvFile($filePath, $database) { // get dir for uploaded csv $targetDir = $this->moduleOptions->getContactUploadedCsvDir(); // transfer uploded file $now = new \DateTime(); $fileInfo = pathinfo($filePath); $receiver = new \Zend\File\Transfer\Adapter\Http(); $receiver->setDestination($targetDir)->setFilters([new \Zend\Filter\File\Rename(["target" => $targetDir . '/uploaded_csv_' . $now->format('Y_m_d-H:i') . '_' . '.' . $fileInfo['extension'], "randomize" => true])]); // file upload element in form class should have name 'csv-file' !! TODO: fix this dependency $receiver->receive('csv-file'); $uploadedCsvFilePath = $receiver->getFileName('csv-file'); // detect delimiter for csv $delimiter = self::detectCsvFileDelimiter($uploadedCsvFilePath); // convert file to array $contactsArray = file($uploadedCsvFilePath); // get titles of columns and transform $columnNames = str_getcsv($contactsArray[0], $delimiter); array_walk($columnNames, function (&$item) { $item = str_replace(" ", "-", $item); $item = strtolower($item); }); foreach ($contactsArray as $key => $contact) { // get CSV line by line $contact = str_getcsv($contact, $delimiter); // change keys in array to column names $contact = array_combine($columnNames, $contact); // detect Windows-1251 ecoding and change to UTF-8 array_walk($contact, function (&$item) { $encoding = mb_detect_encoding($item, array('UTF-8', 'Windows-1251', 'KOI8-R')); // if(mb_check_encoding($item, 'CP1251')){ // $item = iconv('CP1251', 'UTF-8', $item); // } if ($encoding !== 'UTF-8') { $item = iconv('CP1251', 'UTF-8', $item); } $item = \VisoftBaseModule\Service\ForceUTF8\Encoding::toUTF8($item); // if(mb_check_encoding($item, 'CP1251')){ // $item = iconv('CP1251', 'UTF-8', $item); // } }); // rewrite current element to new one $contactsArray[$key] = $contact; } // remove column header array_shift($contactsArray); // debug array // var_dump($contactsArray); // die('11122'); // save contacts to database $this->contactService->enter($database, $contactsArray); }
protected function csvFile2Array($csvFilePath) { // detect delimiter for csv $delimiter = self::detectCsvFileDelimiter($csvFilePath); // convert file to array $contactsArray = file($csvFilePath); // get titles of columns and transform $columnNames = str_getcsv($contactsArray[0], $delimiter); array_walk($columnNames, function (&$item) { $item = str_replace(" ", "-", $item); $item = strtolower($item); }); foreach ($contactsArray as $key => $contact) { // get CSV line by line $contact = str_getcsv($contact, $delimiter); // change keys in array to column names $contact = array_combine($columnNames, $contact); // detect Windows-1251 ecoding and change to UTF-8 array_walk($contact, function (&$item) { $encoding = mb_detect_encoding($item, array('UTF-8', 'Windows-1251', 'KOI8-R')); // if(mb_check_encoding($item, 'CP1251')){ // $item = iconv('CP1251', 'UTF-8', $item); // } if ($encoding !== 'UTF-8') { $item = iconv('CP1251', 'UTF-8', $item); } $item = \VisoftBaseModule\Service\ForceUTF8\Encoding::toUTF8($item); // if(mb_check_encoding($item, 'CP1251')){ // $item = iconv('CP1251', 'UTF-8', $item); // } }); // rewrite current element to new one $contactsArray[$key] = $contact; } // remove column header array_shift($contactsArray); return $contactsArray; }