/**
  * Saves an element.
  *
  * @throws HttpException
  * @return null
  */
 public function actionSaveElement()
 {
     $elementId = craft()->request->getRequiredPost('elementId');
     $localeId = craft()->request->getRequiredPost('locale');
     $elementTypeClass = craft()->elements->getElementTypeById($elementId);
     $element = craft()->elements->getElementById($elementId, $elementTypeClass, $localeId);
     if (!$element || !ElementHelper::isElementEditable($element)) {
         throw new HttpException(403);
     }
     $namespace = craft()->request->getRequiredPost('namespace');
     $params = craft()->request->getPost($namespace);
     if (isset($params['title'])) {
         $element->getContent()->title = $params['title'];
         unset($params['title']);
     }
     if (isset($params['fields'])) {
         $fields = $params['fields'];
         $element->setContentFromPost($fields);
         unset($params['fields']);
     }
     // Either way, at least tell the element where its content comes from
     $element->setContentPostLocation($namespace . '.fields');
     // Now save it
     $elementType = craft()->elements->getElementType($element->elementType);
     if ($elementType->saveElement($element, $params)) {
         $this->returnJson(array('success' => true, 'newTitle' => (string) $element, 'cpEditUrl' => $element->getCpEditUrl()));
     } else {
         $this->_returnEditorHtml($element, false);
     }
 }
Beispiel #2
0
 /**
  * Returns the HTML for an element in the CP.
  *
  * @param array &$context
  *
  * @return string
  */
 private function _getCpElementHtml(&$context)
 {
     if (!isset($context['element'])) {
         return;
     }
     if (!isset($context['context'])) {
         $context['context'] = 'index';
     }
     if (isset($context['elementType'])) {
         $elementType = $context['elementType'];
     } else {
         $elementType = craft()->elements->getElementType($context['element']->getElementType());
     }
     // How big is the element going to be?
     if (isset($context['size']) && ($context['size'] == 'small' || $context['size'] == 'large')) {
         $elementSize = $context['size'];
     } else {
         if (isset($context['viewMode']) && $context['viewMode'] == 'thumbs') {
             $elementSize = 'large';
         } else {
             $elementSize = 'small';
         }
     }
     // Create the thumb/icon image, if there is one
     // ---------------------------------------------------------------------
     $thumbUrl = $context['element']->getThumbUrl(self::$_elementThumbSizes[0]);
     if ($thumbUrl) {
         $srcsets = array();
         foreach (self::$_elementThumbSizes as $i => $size) {
             if ($i == 0) {
                 $srcset = $thumbUrl;
             } else {
                 $srcset = $context['element']->getThumbUrl($size);
             }
             $srcsets[] = $srcset . ' ' . $size . 'w';
         }
         $imgHtml = '<div class="elementthumb">' . '<img ' . 'sizes="' . ($elementSize == 'small' ? self::$_elementThumbSizes[0] : self::$_elementThumbSizes[2]) . 'px" ' . 'srcset="' . implode(', ', $srcsets) . '" ' . 'alt="">' . '</div> ';
     } else {
         $imgHtml = '';
     }
     $html = '<div class="element ' . $elementSize;
     if ($context['context'] == 'field') {
         $html .= ' removable';
     }
     if ($elementType->hasStatuses()) {
         $html .= ' hasstatus';
     }
     if ($thumbUrl) {
         $html .= ' hasthumb';
     }
     $label = HtmlHelper::encode($context['element']);
     $html .= '" data-id="' . $context['element']->id . '" data-locale="' . $context['element']->locale . '" data-status="' . $context['element']->getStatus() . '" data-label="' . $label . '" data-url="' . $context['element']->getUrl() . '"';
     if ($context['element']->level) {
         $html .= ' data-level="' . $context['element']->level . '"';
     }
     $isEditable = ElementHelper::isElementEditable($context['element']);
     if ($isEditable) {
         $html .= ' data-editable';
     }
     $html .= '>';
     if ($context['context'] == 'field' && isset($context['name'])) {
         $html .= '<input type="hidden" name="' . $context['name'] . '[]" value="' . $context['element']->id . '">';
         $html .= '<a class="delete icon" title="' . Craft::t('Remove') . '"></a> ';
     }
     if ($elementType->hasStatuses()) {
         $html .= '<span class="status ' . $context['element']->getStatus() . '"></span>';
     }
     $html .= $imgHtml;
     $html .= '<div class="label">';
     $html .= '<span class="title">';
     if ($context['context'] == 'index' && ($cpEditUrl = $context['element']->getCpEditUrl())) {
         $cpEditUrl = HtmlHelper::encode($cpEditUrl);
         $html .= "<a href=\"{$cpEditUrl}\">{$label}</a>";
     } else {
         $html .= $label;
     }
     $html .= '</span></div></div>';
     return $html;
 }
 /**
  * Returns the HTML for an element in the CP.
  *
  * @param array &$context
  *
  * @return string
  */
 private function _getCpElementHtml(&$context)
 {
     if (!isset($context['element'])) {
         return;
     }
     if (!isset($context['context'])) {
         $context['context'] = 'index';
     }
     if (!isset($context['viewMode'])) {
         $context['viewMode'] = 'table';
     }
     $thumbClass = 'elementthumb' . $context['element']->id;
     $iconClass = 'elementicon' . $context['element']->id;
     if ($context['viewMode'] == 'thumbs') {
         $thumbSize = 100;
         $iconSize = 90;
         $thumbSelectorPrefix = '.thumbsview ';
     } else {
         $thumbSize = 30;
         $iconSize = 20;
         $thumbSelectorPrefix = '';
     }
     $thumbUrl = $context['element']->getThumbUrl($thumbSize);
     if ($thumbUrl) {
         $this->includeCss($thumbSelectorPrefix . '.' . $thumbClass . " { background-image: url('" . $thumbUrl . "'); }");
         $this->includeHiResCss($thumbSelectorPrefix . '.' . $thumbClass . " { background-image: url('" . $context['element']->getThumbUrl($thumbSize * 2) . "'); background-size: " . $thumbSize . 'px; }');
     } else {
         $iconUrl = $context['element']->getIconUrl($iconSize);
         if ($iconUrl) {
             $this->includeCss($thumbSelectorPrefix . '.' . $iconClass . " { background-image: url('" . $iconUrl . "'); }");
             $this->includeHiResCss($thumbSelectorPrefix . '.' . $iconClass . " { background-image: url('" . $context['element']->getIconUrl($iconSize * 2) . "); background-size: " . $iconSize . 'px; }');
         }
     }
     $html = '<div class="element';
     if ($context['context'] == 'field') {
         $html .= ' removable';
     }
     if ($thumbUrl) {
         $html .= ' hasthumb';
     } else {
         if ($iconUrl) {
             $html .= ' hasicon';
         }
     }
     $label = HtmlHelper::encode($context['element']);
     $html .= '" data-id="' . $context['element']->id . '" data-locale="' . $context['element']->locale . '" data-status="' . $context['element']->getStatus() . '" data-label="' . $label . '" data-url="' . $context['element']->getUrl() . '"';
     if ($context['element']->level) {
         $html .= ' data-level="' . $context['element']->level . '"';
     }
     $isEditable = ElementHelper::isElementEditable($context['element']);
     if ($isEditable) {
         $html .= ' data-editable';
     }
     $html .= '>';
     if ($context['context'] == 'field' && isset($context['name'])) {
         $html .= '<input type="hidden" name="' . $context['name'] . '[]" value="' . $context['element']->id . '">';
         $html .= '<a class="delete icon" title="' . Craft::t('Remove') . '"></a> ';
     }
     if ($thumbUrl) {
         $html .= '<div class="elementthumb ' . $thumbClass . '"></div> ';
     } else {
         if ($iconUrl) {
             $html .= '<div class="elementicon ' . $iconClass . '"></div> ';
         }
     }
     $html .= '<div class="label">';
     if (isset($context['elementType'])) {
         $elementType = $context['elementType'];
     } else {
         $elementType = craft()->elements->getElementType($context['element']->getElementType());
     }
     if ($elementType->hasStatuses()) {
         $html .= '<span class="status ' . $context['element']->getStatus() . '"></span>';
     }
     $html .= '<span class="title">';
     if ($context['context'] == 'index' && ($cpEditUrl = $context['element']->getCpEditUrl())) {
         $html .= '<a href="' . $cpEditUrl . '">' . $label . '</a>';
     } else {
         $html .= $label;
     }
     $html .= '</span></div></div>';
     return $html;
 }
 /**
  * Returns the element that is currently being edited.
  *
  * @throws HttpException
  * @return BaseElementModel
  */
 private function _getEditorElement()
 {
     $elementId = craft()->request->getPost('elementId');
     $localeId = craft()->request->getPost('locale');
     // Determine the element type
     $elementTypeClass = craft()->request->getPost('elementType');
     if ($elementTypeClass === null && $elementId !== null) {
         $elementTypeClass = craft()->elements->getElementTypeById($elementId);
     }
     if ($elementTypeClass === null) {
         throw new HttpException(400, Craft::t('POST param “{name}” doesn’t exist.', array('name' => 'elementType')));
     }
     // Make sure it's a valid element type
     $elementType = craft()->elements->getElementType($elementTypeClass);
     if (!$elementType) {
         throw new HttpException(404);
     }
     // Instantiate the element
     if ($elementId !== null) {
         $element = craft()->elements->getElementById($elementId, $elementTypeClass, $localeId);
     } else {
         $element = $elementType->populateElementModel(array());
     }
     if (!$element) {
         throw new HttpException(404);
     }
     // Populate it with any posted attributse
     $attributes = craft()->request->getPost('attributes', array());
     if ($localeId) {
         $attributes['locale'] = $localeId;
     }
     if ($attributes) {
         $element->setAttributes($attributes);
     }
     // Make sure it's editable
     if (!ElementHelper::isElementEditable($element)) {
         throw new HttpException(403);
     }
     return $element;
 }