<?php
namespace Entity; class SabreFile extends \App\Entity\GeneralAbstract { protected $saveInsideTransaction = true; const cacheTtl=604800; protected $tablePrimaryKey = 'idFile'; protected $primaryKeyType = 'string'; protected function setTableName(): void { global $DBTSabreParsedFiles; $this->tableName = $DBTSabreParsedFiles; } protected function getAttribute($var) { switch ($var) { case 'filePath': { $filePath = $this->fields['filePath']; return $filePath; } case 'relativeFilePath': { return $this->fields['filePath']; } case 'parsedFile': { $cacheKey = sprintf("%s-%s-%s", __METHOD__, $var, $this->getId()); $data=\Engine\Cache\General::getInstance('disk')->get($cacheKey); if (!$data) { $data = $this->getAttribute('parsedFileParser')->getExtractedData(); \Engine\Cache\General::getInstance('disk')->set($cacheKey, $data, self::cacheTtl); } return $data; } case 'parsedFileParser': { $fileParser = new \App\Sabre\FileParser\IUR(); global $CFG; if ($this->getAttribute('filePath')[0]=='/') { $filePath = realpath($this->getAttribute('filePath')); } else { $filePath = realpath(MODULE_REL_PATH.$CFG['paths']['sabre'].$this->getAttribute('filePath')); } $fileParser->setRawData(file_get_contents($filePath)); $fileParser->setStoredData('fileDate', min($fileParser->collectFileDatesFromPath($filePath))); $fileParser->extract(); return $fileParser; } case 'parsedFileProcessor': { $processor = new \App\Sabre\StructureParser\Processor($this->getAttribute('parsedFileParser')); $processor->setFile($this->getAttribute('filePath')); $processor->initialize(); return $processor; } case 'idImportedOrders' : return explodeClean("|", $this->fields[$var]); break; case 'addUser': { return \Entity\User::select($this->fields['idAddUser'], $this->selectForUpdate); } case 'updateUser': { return \Entity\User::select($this->fields['idUpdateUser'], $this->selectForUpdate); } default: { return parent::getAttribute($var); } } } protected function setAttribute($var, $value) { if ($this->operation!='insert' and $this->operation!='update') { throw new \Exception('You can call '.__METHOD__.' only for insert/update operations.'); } switch ($var) { case 'idFile': case 'source': case 'idImportedOrders': case 'IP':{ $this->fields[$var]=$value; break; } case 'filePath': { global $CFG; $filesPath = realpath(MODULE_REL_PATH.$CFG['paths']['sabre']).'/'; if (!\Engine\Dir::check(MODULE_REL_PATH.$CFG['paths']['sabre'])) { throw new \Exception(sprintf("Dir::check(%s) returned false", MODULE_REL_PATH.$CFG['paths']['sabre'])); } $filePath = realpath($value); $this->fields[$var] = str_replace($filesPath, '', $filePath); break; } default: { throw new \Exception("The field '$var' is not valid for the `".__CLASS__."` entity."); } } } protected function save_finalSaveData(Array $saveData) { if ($this->operation == 'insert') { $saveData['addTime'] = DATETIME; $saveData['idAddUser']= CURRENT_ID_USER; } return $saveData; } protected function save_filterSaveData(Array $saveData) { if ($saveData['idImportedOrders']) { $saveData['idImportedOrders'] = '|'.implode('|', array_filter($saveData['idImportedOrders'])).'|'; } return $saveData; } } 