/** * Creates a data transformer chain by consuming data found a profile. * The $dataUserOptions is an object that will return info for the output file structure - tree or flat. * * @param \Shopware\Components\SwagImportExport\Profile\Profile $profile * @param array $dataUserOptions * @return \Shopware\Components\SwagImportExport\Transformers\DataTransformerChain */ public function createDataTransformerChain($profile, $dataUserOptions) { // this can be put in a separate hookable function $dataTransformerChain = new DataTransformerChain(); // for every config we create a transformer and add it to the chain $names = $profile->getConfigNames(); foreach ($names as $name) { $config = $profile->getConfig($name); $transformer = $this->createDataTransformer($name, $config); $dataTransformerChain->add($transformer); } // a little hack: if we are in csv, we flatten the tree by adding a flattener at the end if (!$dataUserOptions['isTree']) { $transformer = $this->createDataTransformer('flatten', $profile->getConfig('tree')); $dataTransformerChain->add($transformer); } return $dataTransformerChain; }
/** * @param $postData * @param $inputFile * @return mixed * @throws \Exception */ public function import($postData, $inputFile) { $tree = json_decode($this->profile->getConfig("tree"), true); if ($postData['format'] === 'xml') { $this->fileIO->setTree($tree); } if ($this->dataIO->getSessionState() == 'new') { $totalCount = $this->fileIO->getTotalCount($inputFile); $this->dataIO->setFileName($postData['importFile']); $this->dataIO->setFileSize(filesize($inputFile)); $this->dataIO->getDataSession()->setTotalCount($totalCount); $this->dataIO->startSession($this->profile); } else { // session has already loaded ids and some position, so we simply activate it $this->dataIO->resumeSession(); } $this->dataIO->usernameSession(); if ($this->dataIO->getSessionState() == 'active') { //get current session position $batchSize = (int) $postData['batchSize']; $position = $this->dataIO->getSessionPosition(); $records = $this->fileIO->readRecords($inputFile, $position, $batchSize); $data = $this->transformerChain->transformBackward($records); $defaultValues = $this->profile->getDefaultValues($tree); //inserts/update data into the database $this->dataIO->write($data, $defaultValues); //writes into database log table $profileName = $this->profile->getName(); $this->dataIO->writeLog($inputFile, $profileName); $this->dataIO->progressSession($batchSize); //gets unprocessed data from the adapter $postData['unprocessedData'] = $this->dataIO->getUnprocessedData(); } if ($this->dataIO->getSessionState() == 'finished') { $this->dataIO->closeSession(); } $postData['position'] = $this->dataIO->getSessionPosition(); if (!$postData['sessionId']) { $postData['sessionId'] = $this->dataIO->getDataSession()->getId(); } return $postData; }
/** * Check if the session contains ids. * If the session has no ids, then the db adapter must be used to retrieve them. * Then writes these ids to the session and sets the session state to "active". * For now we will write the ids as a serialized array. * * @param Profile $profile * @param array $data * @throws \Exception */ public function start(Profile $profile, array $data) { $sessionEntity = $this->getEntity(); if (isset($data['totalCountedIds']) && $data['totalCountedIds'] > 0) { //set count $sessionEntity->setTotalCount($data['totalCountedIds']); } //set ids $sessionEntity->setIds($data['serializedIds']); //set type $sessionEntity->setType($data['type']); //set position $sessionEntity->setPosition(0); $dateTime = new \DateTime('now'); //set date/time $sessionEntity->setCreatedAt($dateTime); if (!isset($data['fileName'])) { throw new \Exception('Invalid file name.'); } /** @var UploadPathProvider $uploadPathProvider */ $uploadPathProvider = Shopware()->Container()->get('swag_import_export.upload_path_provider'); //set fileName $sessionEntity->setFileName($uploadPathProvider->getFileNameFromPath($data['fileName'])); if (isset($data['fileSize'])) { $sessionEntity->setFileSize($data['fileSize']); } if (!isset($data['format'])) { throw new \Exception('Invalid format.'); } //set username $sessionEntity->setUserName($data['username']); //set format $sessionEntity->setFormat($data['format']); //change state $sessionEntity->setState('active'); //set profile $sessionEntity->setProfile($profile->getEntity()); $this->getManager()->persist($sessionEntity); $this->getManager()->flush(); $this->sessionId = $sessionEntity->getId(); }
/** * Returns the profile tree. * * @return array */ private function getProfileTree() { return json_decode($this->profile->getEntity()->getTree(), true); }
/** * @param Profile $profile * @param $format * @return FileReader */ private function createFileReader(Profile $profile, $format) { $fileReader = $this->fileIOFactory->createFileReader($format); if ($format === 'xml') { $tree = json_decode($profile->getConfig("tree"), true); $fileReader->setTree($tree); } return $fileReader; }
/** * Generates file name * * @param \Shopware\Components\SwagImportExport\Profile\Profile $profile * @return string */ public function generateFileName(Profile $profile) { $operationType = $this->getType(); $fileFormat = $this->getFormat(); $adapterType = $profile->getType(); $hash = $this->generateRandomHash(8); $dateTime = new \DateTime('now'); $fileName = $operationType . '.' . $adapterType . '.' . $dateTime->format('Y.m.d.h.i.s') . '-' . $hash . '.' . $fileFormat; $this->setFileName($fileName); return $fileName; }