<?php
namespace App\Config; abstract class ExportData_Fields_GeneralAbstract extends DbMultiAssocVarsAbstract { protected $dbTableValueCol = 'value'; protected $entityCriteriaList = []; protected $cacheObj; protected function constructInit() { if (!$this->entityCriteriaList or !is_array($this->entityCriteriaList)) { throw new \Exception('You must set the `entityCriteriaList` attribute for `'.get_called_class().'`.'); } $this->dbTablePkCols = $this->entityCriteriaList; $this->dbTablePkCols[] = 'field'; return parent::constructInit(); } protected function getCacheObject() { if ($this->cacheObj) { return $this->cacheObj; } $cacheObj = new \Engine\Cache\DiskWithLRU(); $CFG = \Engine\Loader_Cfg::getInstance()->getGlobal(); $cacheObj->setPath(MODULE_REL_PATH.$CFG['paths']['exportDataCache']); $this->cacheObj = $cacheObj; return $cacheObj; } protected function getFieldValueByCriteriaList($field, Array $criteriaList) { $cacheKeyOrg = 'ORIGKEY_'.get_called_class().'_'.$field.'_'.serialize($criteriaList); $cachedValue = $this->getCacheObject()->get($cacheKeyOrg); if ($cachedValue!==null) { return $cachedValue; } if ($this->entityCriteriaList!=array_keys($criteriaList)) { throw new \Exception('Invalid criteria list for '.__METHOD__.' in class `'.get_called_class().'`.'); } $criteriaCount = count($criteriaList); $combinationsCount = 2**$criteriaCount-1; $returnValue = null; for ($idx=0; $idx<=$combinationsCount; $idx++) { $switches = str_pad(decbin($idx), $criteriaCount, '0', STR_PAD_LEFT); $criteriaFields = []; $switchesIndex = 0; foreach($criteriaList as $thisCriterionField=>$criterionValue) { $criteriaFields[$thisCriterionField] = ($switches[$switchesIndex]?'*':$criterionValue); $switchesIndex++; } $criteriaFields['field'] = $field; $foundValue = $this->searchAssoc( $criteriaFields ); if ($foundValue!==null) { $returnValue = $foundValue; break; } } $returnValue = (String)$returnValue; $this->getCacheObject()->set($cacheKeyOrg, $returnValue); return $returnValue; } public function save() { parent::save(); $this->getCacheObject()->flush(); } public function getFieldRulesCount($field) { if (!$field) { throw new \App\Config\MessageException("Undefined field to search by."); } $db=\DB::getInstance(); $searchSQL="SELECT COUNT(*) FROM `{$this->dbTableName}` WHERE field = ?"; $fieldRulesCount = $db->GetOne($searchSQL, $field); return $fieldRulesCount; } } 