/** * Applies the API query on the model query through this strategy * @param \ride\library\http\jsonapi\JsonApiQuery $jsonApiQuery * @param \ride\library\orm\query\ModelQuery $modelQuery * @return null */ public function applyFilter(JsonApiQuery $jsonApiQuery, ModelQuery $modelQuery) { $expression = $jsonApiQuery->getFilter('expression', null); if ($expression) { $modelQuery->addCondition($expression); } }
/** * Applies the filter to the provided query * @param \ride\library\orm\model\Model $model * @param \ride\library\orm\query\ModelQuery $query * @param string $fieldName Name of the filter field * @param string|array $value Submitted value * @return string|array Value of the filter */ public function applyQuery(Model $model, ModelQuery $query, $fieldName, $value = null) { if (!$value) { return null; } $query->addCondition('{' . $fieldName . '} = %1%', $value); return $value; }
/** * Applies the filter to the provided query * @param \ride\library\orm\model\Model $model * @param \ride\library\orm\query\ModelQuery $query * @param string $fieldName Name of the filter field * @param string|array $value Submitted value * @return string|array Value of the filter */ public function applyQuery(Model $model, ModelQuery $query, $fieldName, $value = null) { if ($value === null || $value === '' || is_array($value) && !$value) { return null; } $from = null; $until = null; $this->getPeriodFromValue($value, $from, $until); if ($from && $until) { $query->addCondition('({dateStop} IS NULL AND %1% <= {dateStart} AND {dateStart} <= %2%) OR ({dateStop} IS NOT NULL AND ((%1% <= {dateStart} AND {dateStart} <= %2%) OR (%1% <= {dateStop} AND {dateStop} <= %2%) OR ({dateStart} <= %1% AND %2% <= {dateStop})))', $from, $until); } return $value; }
/** * Applies the API query on the model query through this strategy * @param \ride\library\http\jsonapi\JsonApiQuery $jsonApiQuery * @param \ride\library\orm\query\ModelQuery $modelQuery * @return null */ public function applyFilter(JsonApiQuery $jsonApiQuery, ModelQuery $modelQuery) { $query = $jsonApiQuery->getFilter('elastic', null); if (!$query) { return; } $model = $modelQuery->getModel(); if ($model->getMeta()->isLocalized()) { $query = str_replace(array('%locale%', '%25locale%25'), $modelQuery->getLocale(), $query); } $parameters = array('query' => $query, 'limit' => $jsonApiQuery->getLimit(50), 'offset' => $jsonApiQuery->getOffset()); $result = $this->search->searchByQueryString($model, $parameters); $this->search->applyResultToModelQuery($result, $modelQuery); }
/** * Applies the filter to the provided query * @param \ride\library\orm\model\Model $model * @param \ride\library\orm\query\ModelQuery $query * @param string $fieldName Name of the filter field * @param string|array $value Submitted value * @return string|array Value of the filter */ public function applyQuery(Model $model, ModelQuery $query, $fieldName, $value = null) { if ($value === null || $value === '' || is_array($value) && !$value) { return null; } $from = null; $until = null; $this->getPeriodFromValue($value, $from, $until); if ($from) { $query->addCondition('{' . $fieldName . '} >= %1%', $from); } if ($until) { $query->addCondition('{' . $fieldName . '} <= %1%', $until); } return $value; }
/** * Gets the result from the query * @param \ride\web\cms\orm\ContentProperties $properties * @param \ride\library\orm\query\ModelQuery $query * @return array Array with Content objects */ protected function getResult(ContentProperties $contentProperties, ContentService $contentService, ModelQuery $query) { $entry = $query->queryFirst(); if (!$entry) { return $entry; } $node = $this->properties->getNode(); $titleFormat = $contentProperties->getContentTitleFormat(); $teaserFormat = $contentProperties->getContentTeaserFormat(); $imageFormat = $contentProperties->getContentImageFormat(); $dateFormat = $contentProperties->getContentDateFormat(); return $contentService->getContentForEntry($this->model, $entry, $node->getRootNodeId(), $this->locale, null, $titleFormat, $teaserFormat, $imageFormat, $dateFormat); }
/** * Prepares the query for the condition * @param \ride\library\orm\model\Model $model * @param \ride\library\orm\query\ModelQuery $query * @param string $fieldName Name of the filter field * @param string $conditionField Name of the model field to query on * @return boolean True when conditionField is set, false otherwise */ protected function prepareQuery(Model $model, ModelQuery $query, $fieldName, &$conditionField) { $orm = $model->getOrmManager(); $meta = $model->getMeta(); $fieldTokens = explode('.', $fieldName); $fieldTokenName = array_shift($fieldTokens); $field = $meta->getField($fieldTokenName); if ($field instanceof PropertyField) { return false; } elseif ($field instanceof BelongsToField) { $conditionField = $fieldTokenName; } else { $conditionField = $fieldTokenName . '.' . ModelTable::PRIMARY_KEY; } $numFields = count($fieldTokens); $numField = 0; $oldFieldTokenName = null; while ($numField <= $numFields) { if ($numField > 0) { $foreignKey = $meta->getRelationForeignKey($fieldTokenName); if ($field instanceof BelongsToField) { // $query->addJoin('LEFT', $relationModelName, $relationModelName, '{self.' . $oldFieldTokenName . '} = {' . $oldFieldTokenName . '.id}'); $conditionField = $oldFieldTokenName . '.' . $fieldTokenName; } elseif ($field instanceof HasManyField) { $relation = $meta->getRelationMeta($fieldTokenName); $foreignKey = $relation->getForeignKey(); $linkModelName = $relation->getLinkModelName(); // $relation->isRelationWithSelf(); // $relation->isHasManyAndBelongsToMany(); if ($linkModelName) { $foreignKeyToSelf = $relation->getForeignKeyToSelf($fieldTokenName); $query->addJoin('LEFT', $linkModelName, $linkModelName, '{' . $linkModelName . '.' . $foreignKeyToSelf . '} = {' . $oldFieldTokenName . '.id}'); $conditionField = $linkModelName . '.' . $foreignKey; } else { $linkRelationModelName = $meta->getRelationModelName($fieldTokenName); $foreignKeyToSelf = $meta->getRelationForeignKeyToSelf($fieldTokenName); $linkModel = $orm->getModel($linkRelationModelName); $linkMeta = $linkModel->getMeta(); $query->addJoin('LEFT', $linkRelationModelName, $linkRelationModelName, '{' . $linkRelationModelName . '.' . $foreignKey . '} = {' . $oldFieldTokenName . '.id}'); $conditionField = $linkRelationModelName . '.' . ModelTable::PRIMARY_KEY; } } } $oldMeta = $meta; $oldFieldTokenName = $fieldTokenName; $relationModelName = $meta->getRelationModelName($fieldTokenName); $relationModel = $orm->getModel($relationModelName); $meta = $relationModel->getMeta(); if ($fieldTokens) { $fieldTokenName = array_shift($fieldTokens); if ($fieldTokenName) { $field = $meta->getField($fieldTokenName); } } else { $fieldTokenName = null; } $numField++; } return true; }
/** * Gets the result from the query * @param \ride\web\cms\orm\ContentProperties $contentProperties * @param \ride\web\cms\orm\ContentService $contentService * @param \ride\library\orm\query\ModelQuery $query * @return array Array with Content objects */ private function getResult(ContentProperties $contentProperties, ContentService $contentService, ModelQuery $query) { $result = $query->query(); if (!$result) { return $result; } $node = $this->properties->getNode(); $contentMapper = $contentProperties->getContentMapper(); $titleFormat = $contentProperties->getContentTitleFormat(); $teaserFormat = $contentProperties->getContentTeaserFormat(); $imageFormat = $contentProperties->getContentImageFormat(); $dateFormat = $contentProperties->getContentDateFormat(); return $contentService->getContentForEntries($this->model, $result, $node->getRootNodeId(), $this->locale, $contentMapper, $titleFormat, $teaserFormat, $imageFormat, $dateFormat); }