/**
  * Gets the content objects for the provided entries
  * @param \ride\library\orm\model\Model $model
  * @param array $entries
  * @param string $siteId
  * @param string $locale
  * @param string $idContentMapper
  * @param string $titleFormat
  * @param string $teaserFormat
  * @param string $imageFormat
  * @param string $dateFormat
  * @return array Array with Content objects for the provided entries
  * @see \ride\library\cms\content\Content
  */
 public function getContentForEntries(Model $model, array $result, $siteId, $locale, $idContentMapper = null, $titleFormat = null, $teaserFormat = null, $imageFormat = null, $dateFormat = null)
 {
     $modelName = $model->getName();
     $modelTable = $model->getMeta()->getModelTable();
     $entryFormatter = $this->orm->getEntryFormatter();
     if (!$titleFormat) {
         $titleFormat = $modelTable->getFormat(EntryFormatter::FORMAT_TITLE, false);
         if ($titleFormat == null) {
             $titleFormat = $model->getName() . ' #{id}';
         }
     }
     if (!$teaserFormat && $modelTable->hasFormat(EntryFormatter::FORMAT_TEASER)) {
         $teaserFormat = $modelTable->getFormat(EntryFormatter::FORMAT_TEASER);
     }
     if (!$imageFormat && $modelTable->hasFormat(EntryFormatter::FORMAT_IMAGE)) {
         $imageFormat = $modelTable->getFormat(EntryFormatter::FORMAT_IMAGE);
     }
     if (!$dateFormat && $modelTable->hasFormat(EntryFormatter::FORMAT_DATE)) {
         $dateFormat = $modelTable->getFormat(EntryFormatter::FORMAT_DATE);
     }
     try {
         $mapper = $this->getContentMapper($modelName, $idContentMapper);
     } catch (Exception $e) {
         $mapper = new OrmContentMapper($this->nodeModel, $model, $entryFormatter);
     }
     foreach ($result as $index => $entry) {
         $title = $entryFormatter->formatEntry($entry, $titleFormat);
         $url = $mapper->getUrl($siteId, $locale, $entry);
         $teaser = null;
         if ($teaserFormat) {
             $teaser = $entryFormatter->formatEntry($entry, $teaserFormat);
         }
         $image = null;
         if ($imageFormat) {
             $image = $entryFormatter->formatEntry($entry, $imageFormat);
         }
         $date = null;
         if ($dateFormat) {
             $date = $entryFormatter->formatEntry($entry, $dateFormat);
         }
         $content = new Content($modelName, $title, $url, $teaser, $image, $date, $entry);
         $result[$index] = $content;
     }
     return $result;
 }
예제 #2
0
 /**
  * Gets the file name for a newly uploaded file
  * @param \ride\library\orm\model\Model $model
  * @param \ride\library\orm\definition\field\ModelField $field
  * @param \ride\library\orm\entry\Entry $entry
  * @return string Name for the file without path and extension
  */
 protected function getFileName(Model $model, ModelField $field, Entry $entry)
 {
     $entryFormatter = $model->getOrmManager()->getEntryFormatter();
     $format = $model->getMeta()->getFormat(EntryFormatter::FORMAT_TITLE);
     $name = $entryFormatter->formatEntry($entry, $format);
     if ($name) {
         $name = StringHelper::safeString($name);
     } else {
         $name = StringHelper::generate();
     }
     return $name;
 }
예제 #3
0
 /**
  * Resolves the provided relationship field
  * @param \ride\library\orm\model\Model $model
  * @param string $relationship
  * @param string $type
  * @return \ride\library\orm\definition\field\Field
  */
 private function getField(Model $model, $relationship, $type)
 {
     $field = $model->getMeta()->getField($relationship);
     if (!$field instanceof RelationField) {
         // invalid relationship
         $error = $this->api->createError(Response::STATUS_CODE_BAD_REQUEST, 'input.relationship', 'Could not set relationship');
         $error->setDetail('Relationship \'' . $relationship . '\' does not exist for type \'' . $type . '\'');
         $error->setSourcePointer('/data/relationships/' . $relationship);
         $this->document->addError($error);
     }
     return $field;
 }
 /**
  * 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;
 }
 /**
  * Creates the model query from the provided properties
  * @param \ride\library\orm\model\Model $model
  * @param \ride\library\orm\model\ContentProperties $contentProperties
  * @param string $locale Code of the locale
  * @param integer $page Page number
  * @param array $arguments Arguments for the condition
  * @return \ride\library\orm\query\ModelQuery
  */
 public function getModelQuery(ContentProperties $contentProperties, $locale, $page = 1, array $arguments, &$isFiltered = null)
 {
     $isFiltered = false;
     // create query
     $query = $this->model->createQuery($locale);
     $query->setRecursiveDepth($contentProperties->getRecursiveDepth());
     $query->setFetchUnlocalized($contentProperties->getIncludeUnlocalized());
     // select fields
     $modelFields = $contentProperties->getModelFields();
     if ($modelFields) {
         foreach ($modelFields as $fieldName) {
             $query->addFields('{' . $fieldName . '}');
         }
     }
     // apply condition
     $condition = $contentProperties->getCondition();
     if ($condition) {
         $arguments = $this->parseContextVariables($condition, $arguments);
         if ($arguments) {
             $isFiltered = true;
             $query->addConditionWithVariables($condition, $arguments);
         } else {
             $query->addCondition($condition);
         }
     }
     // apply search
     if ($contentProperties->hasSearch()) {
         $searchQuery = $this->request->getQueryParameter('query');
         if ($searchQuery) {
             $conditions = array();
             $fields = $this->model->getMeta()->getProperties();
             foreach ($fields as $fieldName => $field) {
                 if ($field->getOption('scaffold.search')) {
                     $conditions[] = '{' . $fieldName . '} LIKE %1%';
                 }
             }
             if ($conditions) {
                 $condition = implode(' OR ', $conditions);
                 $searchQuery = '%' . $searchQuery . '%';
                 $query->addCondition($condition, $searchQuery);
             }
         }
     }
     // apply filters
     $this->filters = array();
     $filters = $contentProperties->getFilters();
     foreach ($filters as $filter) {
         $filterValue = $this->request->getQueryParameter($filter['name']);
         if ($filterValue) {
             $isFiltered = true;
         }
         $this->filters[$filter['name']] = array('type' => $filter['type'], 'field' => $filter['field'], 'filter' => $this->dependencyInjector->get('ride\\web\\cms\\orm\\filter\\ContentOverviewFilter', $filter['type']));
         $this->filters[$filter['name']]['value'] = $this->filters[$filter['name']]['filter']->applyQuery($this->model, $query, $filter['field'], $filterValue);
     }
     // apply order
     $order = $contentProperties->getOrder();
     if ($order) {
         $query->addOrderBy($order);
     }
     // apply pagination
     if ($contentProperties->isPaginationEnabled()) {
         $paginationOffset = $contentProperties->getPaginationOffset();
         $rows = $contentProperties->getPaginationRows();
         $offset = ($page - 1) * $rows;
         if ($paginationOffset) {
             $offset += $paginationOffset;
         }
         $query->setLimit((int) $rows, (int) $offset);
     }
     return $query;
 }