/**
  * Processes the incoming value before setting it to the entry
  * @param \ride\library\orm\model\Model $model
  * @param \ride\library\orm\definition\field\ModelField $field
  * @param \ride\library\orm\entry\Entry $entry
  * @param mixed $value Incoming value
  * @return mixed Value to set on the entry
  */
 public function processInputValue(Model $model, ModelField $field, Entry $entry, $value)
 {
     if ($value === null || $model->getName() != 'Asset' || $field->getName() != 'value' || $this->isUrl($value)) {
         // no value or not a asset file value
         return $value;
     }
     return $this->handleValue($model, $field, $entry, $value);
 }
 /**
  * Gets the options for the date field
  * @param \ride\library\orm\model\Model $model
  * @param string $fieldName Name of the field
  * @param string $locale Code of the current locale
  * @return array
  */
 protected function getMonthOptions(Model $model, $fieldName, $locale)
 {
     $query = $model->createQuery($locale);
     $query->setDistinct(true);
     $query->setFields('DATE_FORMAT(FROM_UNIXTIME({dateStart}), \'%Y-%m\') AS monthYear');
     $query->addOrderBy('monthYear DESC');
     $months = array_keys($query->query('monthYear'));
     return array_combine($months, $months);
 }
 /**
  * Constructs a new model resource adapter
  * @param \ride\web\WebApplication $web Instance of the web application
  * @param \ride\library\orm\model\Model $model Instance of the model
  * @param string $type Resource type for this model, defaults to model name
  * @return null
  */
 public function __construct(WebApplication $web, Model $model, $type = null)
 {
     if ($type === null) {
         $type = $model->getName();
     }
     $this->web = $web;
     $this->model = $model;
     $this->reflectionHelper = $model->getReflectionHelper();
     $this->type = $type;
     $this->filterStrategies = array();
 }
 /**
  * Parses the context into the arguments
  * @param string $condition Condition string
  * @param array $arguments Already set arguments
  * @return array Provided arguments with the resolved context arguments
  * @throws Exception when the context arguments could not be parsed
  */
 public function parseContextVariables($condition, array $arguments)
 {
     if (strpos($condition, '%context') === false) {
         return $arguments;
     }
     $matches = array();
     preg_match_all('(%context([A-Za-z0-9]|\\.)*%)', $condition, $matches);
     if (!$matches[0]) {
         throw new Exception('Could not parse context argument: no arguments matched');
     }
     $reflectionHelper = $this->model->getReflectionHelper();
     foreach ($matches[0] as $variable) {
         $tokens = explode('.', trim($variable, '%'));
         array_shift($tokens);
         $value = null;
         do {
             $token = array_shift($tokens);
             if ($value === null) {
                 $value = $this->getContext($token);
             } else {
                 $value = $reflectionHelper->getProperty($value, $token);
             }
             if ($value === null) {
                 throw new Exception('Could not parse context arguments: ' . $variable . ' could not be resolved');
             }
         } while ($tokens);
         $arguments[substr($variable, 1, -1)] = $value;
     }
     return $arguments;
 }
Exemple #5
0
 /**
  * Loads the value of a relation field of this User entry
  * @param string $fieldName Name of the relation field
  * @return null
  */
 private function loadRelation($fieldName)
 {
     $id = $this->getId();
     if (!$id) {
         return;
     }
     $query = $this->_model->createQuery();
     $entry = $query->queryRelation($this->getId(), $fieldName);
     $getterMethodName = 'get' . ucfirst($fieldName);
     $this->{$fieldName} = $entry->{$getterMethodName}();
     $this->loadedValues[$fieldName] = $entry->loadedValues[$fieldName];
     $this->loadedFields[$fieldName] = true;
 }
 /**
  * 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;
 }
 /**
  * Gets the current field value of the entry
  * @param \ride\library\orm\model\Model $model
  * @param \ride\library\orm\definition\field\ModelField $field
  * @param \ride\library\orm\entry\Entry $entry
  * @return mixed Current field value of the entry
  */
 protected function getCurrentValue(Model $model, ModelField $field, Entry $entry)
 {
     return $model->getReflectionHelper()->getProperty($entry, $field->getName());
 }
 /**
  * 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;
 }
 /**
  * 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;
 }