<?php
 namespace Modules\Admin\ImportEntities\Model\Import; class SupplierProductsInsert extends \Modules\Admin\ImportEntities\Model\ImportAbstract { protected $fields = ['supplierProductName', 'category', 'destination', 'chainCode', 'address', 'telephone', 'email', 'website', 'serviceCode']; public function process() { $_LANG = \App\Loader_Lang::getInstance()->getGlobal(); $messagesFromImport = new \App\MessagesCollector(); foreach($this->fileData as $row) { $remarks = ''; foreach ($row as $name=>$value) { if (preg_match('/remark:/', $name) and strlen($value)) { $rem = str_replace('remark:', '', $name); $remarks .= sprintf($rem, $value) . "\n"; } } $countryCode = ''; $cityId = 0; if ($row['destination']) { $matchCity = new \App\EntityMatch\City('intersect'); $matchCity->setName($row['destination'], false); $matchCity->search(); if ($matchCity->getResultsCount()==1) { $cityDet = $matchCity->getFirstMatchDetails(); $cityId = $cityDet['id']; $countryCode = $cityDet['countryCode']; } } try { \DB::startTransaction(); $supplierProduct = \Entity\SupplierProduct::insert(); $supplierProduct->serviceCode = $row['serviceCode']; $supplierProduct->category = $row['category']; $supplierProduct->productName = $row['supplierProductName']; $supplierProduct->chainCode = ($row['chainCode'])?$row['chainCode']:''; $supplierProduct->destinationCountryCode = $countryCode; $supplierProduct->destinationCityId = $cityId; $supplierProduct->address = ($row['address'])?$row['address']:''; $supplierProduct->remarks = trim($remarks); $supplierProduct->telephone = ($row['telephone'])?$row['telephone']:''; $supplierProduct->email = ($row['email'])?$row['email']:''; $supplierProduct->website = ($row['website'])?$row['website']:''; $supplierProduct->save(); \DB::commitTransaction(); $idSupplierProduct = $supplierProduct->getId(); $messagesFromImport->addSuccess(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['supplierProduct']['saveOk'], $row['supplierProductName'], $idSupplierProduct)); } catch (\App\Entity\Exception $e) { \DB::rollbackTransaction(); $messagesFromImport->addWarning(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['supplierProduct']['saveError'], $row['supplierProductName'])); $messagesFromImport->appendMessages($e->getMessagesCollector()); } } return $messagesFromImport; } }