Ejemplo n.º 1
0
 /**
  * Prepares an element's content for being saved to the database.
  *
  * @param BaseElementModel $element
  * @param FieldLayoutModel $fieldLayout
  * @param bool             $validate
  * @return ContentModel
  */
 public function prepElementContentForSave(BaseElementModel $element, FieldLayoutModel $fieldLayout, $validate = true)
 {
     $elementTypeClass = $element->getElementType();
     $elementType = craft()->elements->getElementType($elementTypeClass);
     $content = $element->getContent();
     if ($validate) {
         // Set the required fields from the layout
         $requiredFields = array();
         if ($elementType->hasTitles()) {
             $requiredFields[] = 'title';
         }
         foreach ($fieldLayout->getFields() as $field) {
             if ($field->required) {
                 $requiredFields[] = $field->fieldId;
             }
         }
         if ($requiredFields) {
             $content->setRequiredFields($requiredFields);
         }
     }
     // Give the fieldtypes a chance to clean up the post data
     foreach (craft()->fields->getAllFields() as $field) {
         $fieldType = craft()->fields->populateFieldType($field);
         if ($fieldType) {
             $fieldType->element = $element;
             $handle = $field->handle;
             $content->{$handle} = $fieldType->prepValueFromPost($content->{$handle});
         }
     }
     return $content;
 }
Ejemplo n.º 2
0
 /**
  * Returns whether this is the first time the element's content has been edited.
  *
  * @return bool
  */
 protected function isFresh()
 {
     // If this is for a Matrix block, we're more interested in its owner
     if (isset($this->element) && $this->element->getElementType() == ElementType::MatrixBlock) {
         $element = $this->element->getOwner();
     } else {
         $element = $this->element;
     }
     return !$element || empty($element->getContent()->id) && !$element->hasErrors();
 }
Ejemplo n.º 3
0
 /**
  * Indexes the attributes of a given element defined by its element type.
  *
  * @param BaseElementModel $element
  * @param string|null      $localeId
  * @return bool Whether the indexing was a success.
  */
 public function indexElementAttributes(BaseElementModel $element, $localeId = null)
 {
     // Get the element type
     $elementTypeClass = $element->getElementType();
     $elementType = craft()->elements->getElementType($elementTypeClass);
     // Does it have any searchable attributes?
     $searchableAttributes = $elementType->defineSearchableAttributes();
     if ($elementType->hasTitles()) {
         $searchableAttributes[] = 'title';
     }
     foreach ($searchableAttributes as $attribute) {
         $value = $element->{$attribute};
         $value = StringHelper::arrayToString($value);
         $this->_indexElementKeywords($element->id, $attribute, '0', $localeId, $value);
     }
     return true;
 }
Ejemplo n.º 4
0
 /**
  * Updates an element’s descendants’ slugs and URIs.
  *
  * @param BaseElementModel $element            The element whose descendants should be updated.
  * @param bool             $updateOtherLocales Whether the element’s other locales should also be updated.
  * @param bool             $asTask             Whether the descendants’ slugs and URIs should be updated via a background task.
  *
  * @return null
  */
 public function updateDescendantSlugsAndUris(BaseElementModel $element, $updateOtherLocales = true, $asTask = false)
 {
     $criteria = $this->getCriteria($element->getElementType());
     $criteria->descendantOf = $element;
     $criteria->descendantDist = 1;
     $criteria->status = null;
     $criteria->localeEnabled = null;
     $criteria->locale = $element->locale;
     if ($asTask) {
         $childIds = $criteria->ids();
         if ($childIds) {
             craft()->tasks->createTask('UpdateElementSlugsAndUris', null, array('elementId' => $childIds, 'elementType' => $element->getElementType(), 'locale' => $element->locale, 'updateOtherLocales' => $updateOtherLocales, 'updateDescendants' => true));
         }
     } else {
         $children = $criteria->find();
         foreach ($children as $child) {
             $this->updateElementSlugAndUri($child, $updateOtherLocales, true, false);
         }
     }
 }
 /**
  * Save Element
  *
  * @param array $params Parameters
  *
  * @return BaseElementModel $model
  */
 public function saveElement(BaseElementModel $element, Request $request)
 {
     $element_type = \Craft\craft()->elements->getElementType($element->getElementType());
     $result = $element_type->saveElement($element, null);
     if (!$result) {
         $exception = new RestException();
         $exception->setStatus(400)->setMessage('Element could not be stored.');
         throw $exception;
     }
     \Craft\craft()->content->saveContent($element);
     return $element;
 }
Ejemplo n.º 6
0
 /**
  * Validates some content with a given field layout.
  *
  * @param BaseElementModel $element The element whose content should be validated.
  *
  * @return bool Whether the element's content validates.
  */
 public function validateContent(BaseElementModel $element)
 {
     $elementType = craft()->elements->getElementType($element->getElementType());
     $fieldLayout = $element->getFieldLayout();
     $content = $element->getContent();
     // Set the required fields from the layout
     $attributesToValidate = array('id', 'elementId', 'locale');
     $requiredFields = array();
     if ($elementType->hasTitles()) {
         $requiredFields[] = 'title';
         $attributesToValidate[] = 'title';
     }
     if ($fieldLayout) {
         foreach ($fieldLayout->getFields() as $fieldLayoutField) {
             $field = $fieldLayoutField->getField();
             if ($field) {
                 $attributesToValidate[] = $field->handle;
                 if ($fieldLayoutField->required) {
                     $requiredFields[] = $field->id;
                 }
             }
         }
     }
     if ($requiredFields) {
         $content->setRequiredFields($requiredFields);
     }
     return $content->validate($attributesToValidate);
 }
Ejemplo n.º 7
0
 /**
  * Updates an element’s descendants’ slugs and URIs.
  *
  * @param BaseElementModel $element The element whose descendants should be updated.
  *
  * @return null
  */
 public function updateDescendantSlugsAndUris(BaseElementModel $element)
 {
     $criteria = $this->getCriteria($element->getElementType());
     $criteria->descendantOf = $element;
     $criteria->descendantDist = 1;
     $criteria->status = null;
     $criteria->localeEnabled = null;
     $children = $criteria->find();
     foreach ($children as $child) {
         $this->updateElementSlugAndUri($child);
     }
 }