protected function modifyCondition(&$condition) { if (!$condition) { $condition = new Condition(null, $this->jarves); } $languages = $this->jarves->getSystemConfig()->getLanguages(); $languages = preg_replace('/\\W+/', ',', $languages); $languages = explode(',', $languages); foreach ($languages as $lang) { $condition->addOr(['code', '=', $lang]); } }
public function testConditionToSql() { $condition = new Condition(); $condition2 = new Condition(); $condition2->addAnd(['title', '=', 'TestNode tree']); $condition->addAnd($condition2); $condition->addOr(['1', '=', '1']); $params = []; $sql = $this->getConditionOperator()->standardConditionToSql($condition, $params, 'jarves/node'); $expectedArray = [[['title', '=', 'TestNode tree']], 'OR', ['1', '=', '1']]; $this->assertEquals($expectedArray, $condition->toArray()); $this->assertEquals([':p1' => 'TestNode tree'], $params); $this->assertEquals(' system_node.title = :p1 OR 1= 1', $sql); }
/** * Get a condition object for item listings. * * @param string $objectKey * * @return Condition */ public function getListingCondition($objectKey) { $objectKey = Objects::normalizeObjectKey($objectKey); $obj = $this->objects->getStorageController($objectKey); $rules = self::getRules($objectKey, static::MODE_LISTING); if (count($rules) === 0) { return null; } if ($this->getCaching()) { $cacheKey = md5($objectKey); $cached = $this->cacher->getDistributedCache('core/acl/listing/' . $cacheKey); if (null !== $cached) { return $cached; } } $condition = ''; $primaryList = $this->objects->getPrimaryList($objectKey); $primaryKey = current($primaryList); $denyList = array(); $conditionObject = new Condition(null, $this->jarves); foreach ($rules as $rule) { if ($rule['constraint_type'] === ACL::CONSTRAINT_EXACT) { //todo $rule['constraint_code'] can be a (urlencoded) composite pk //todo constraint_code is always urlencoded; $condition = Condition::create(array($primaryKey, '=', Tools::urlDecode($rule['constraint_code'])), $this->jarves); } if ($rule['constraint_type'] === ACL::CONSTRAINT_CONDITION) { $condition = Condition::create($rule['constraint_code'], $this->jarves); } if ($rule['constraint_type'] === ACL::CONSTRAINT_ALL) { $condition = array('1', '=', '1'); } elseif ($rule['sub']) { $subCondition = $obj->getNestedSubCondition($condition); if ($subCondition) { $condition = array($condition, 'OR', $subCondition); } } if ($rule['access'] === 1) { if ($denyList) { $condition = array($condition, 'AND NOT', $denyList); $conditionObject->addOr($condition); // $conditionObject->add('AND NOT', $denyList); } else { $conditionObject->addOr($condition); } } if ($rule['access'] !== 1) { if ($denyList) { $denyList[] = 'AND NOT'; } $denyList[] = $condition; } } if (!$conditionObject->hasRules()) { $conditionObject->addAnd(array('1', '!=', '1')); } if ($this->getCaching()) { $cacheKey = md5($objectKey); $this->cacher->setDistributedCache('core/acl/listing/' . $cacheKey, $conditionObject); } return $conditionObject; }
/** * * array( * 'items' => $items, * 'count' => $maxItems, * 'pages' => $maxPages * ); * * @param array $filter * @param integer $limit * @param integer $offset * @param string $query * @param string $fields * @param array $orderBy * * @param bool $withAcl * @param array $primaryKeys * * @return array * @throws ObjectNotFoundException * @throws \Exception */ public function getItems($filter = null, $limit = null, $offset = null, $query = '', $fields = null, $orderBy = [], $withAcl = false, array $primaryKeys = []) { $options = array(); $storageController = $this->objects->getStorageController($this->getObject()); $options['offset'] = $offset; $options['limit'] = $limit ? $limit : $this->defaultLimit; $condition = $this->getCondition(); if ($extraCondition = $this->getCustomListingCondition()) { $condition->mergeAnd($extraCondition); } $options['order'] = $orderBy ?: $this->getOrder(); $options['fields'] = $this->getItemsSelection($fields); $options['permissionCheck'] = $this->getPermissionCheck(); $aclRequest = ACLRequest::create($this->getObject())->onlyListingMode(); if ($this->getPermissionCheck() && !$this->acl->check($aclRequest)) { return null; } if ($limit = $this->getObjectDefinition()->getLimitDataSets()) { $condition->mergeAnd($limit); } if ($this->getMultiLanguage() && $this->getLanguage()) { if ($this->getObjectDefinition()->getNestedRootAsObject() && ($rootObjectKey = $this->getObjectDefinition()->getNestedRootObject())) { $rootObjects = $this->objects->getList($rootObjectKey, null, ['lang' => $this->getLanguage(), 'fields' => 'id']); $langConditions = new Condition(); foreach ($rootObjects as $item) { $langConditions->addOr(['domain', '=', $item['id']]); } $condition->addAnd($langConditions); } else { //does the object have a lang field? if ($this->objectDefinition->hasField('lang') && !isset($filter['lang'])) { $filter['lang'] = $this->getLanguage(); } } } if ($query) { if ($queryCondition = $this->getQueryCondition($query, $options['fields'])) { $condition->mergeAnd($queryCondition); } } if ($primaryKeys) { $primaryKeyCondition = Condition::create(); foreach ($primaryKeys as $pk) { $primaryKeyConditionItem = Condition::create(); foreach ($this->getObjectDefinition()->getPrimaryKeyNames() as $primaryKeyName) { if (!isset($pk[$primaryKeyName])) { throw new \LogicException(sprintf('Field %s not found in primaryKey parameter (%s)', $primaryKeyName, json_encode($pk))); } $primaryKeyConditionItem->addAnd([$primaryKeyName, '=', $pk[$primaryKeyName]]); } $primaryKeyCondition->mergeOr($primaryKeyConditionItem); } $condition->mergeAndBegin($primaryKeyCondition); } if ($this->getPermissionCheck() && ($aclCondition = $this->acl->getListingCondition($this->getObject()))) { $condition->mergeAndBegin($aclCondition); } $items = $storageController->getItems($filter, $condition, $options); if ($withAcl && is_array($items)) { foreach ($items as &$item) { if ($item) { $this->prepareRow($item); } } } return $items ?: null; }