예제 #1
0
 /**
  * Displays a category.
  *
  * @param CategoryModel $category
  *
  * @throws HttpException
  * @return null
  */
 private function _showCategory(CategoryModel $category)
 {
     $group = $category->getGroup();
     if (!$group) {
         Craft::log('Attempting to preview a category that doesn’t have a group', LogLevel::Error);
         throw new HttpException(404);
     }
     craft()->setLanguage($category->locale);
     // Have this category override any freshly queried categories with the same ID/locale
     craft()->elements->setPlaceholderElement($category);
     craft()->templates->getTwig()->disableStrictVariables();
     $this->renderTemplate($group->template, array('category' => $category));
 }
 /**
  * Parse category fields.
  *
  * @param CategoryModel $category
  * @param bool          $empty
  *
  * @return array
  */
 public function fields(CategoryModel $category, $empty = false)
 {
     // Always save id
     $fields = array('id' => array('label' => Craft::t('ID'), 'value' => $category->id), 'title' => array('label' => Craft::t('Title'), 'value' => (string) $category->getTitle()), 'group' => array('label' => Craft::t('Group'), 'value' => (string) $category->getGroup()));
     // Get element type
     $elementType = craft()->elements->getElementType(ElementType::Category);
     // Get nice attributes
     $availableAttributes = $elementType->defineAvailableTableAttributes();
     // Make 'em fit
     $attributes = array();
     foreach ($availableAttributes as $key => $result) {
         $attributes[$key] = $result['label'];
     }
     // Get static "fields"
     foreach ($category->getAttributes() as $handle => $value) {
         // Only show nice attributes
         if (array_key_exists($handle, $attributes)) {
             $fields[$handle] = array('label' => $attributes[$handle], 'value' => StringHelper::arrayToString(is_array($value) ? array_filter(ArrayHelper::flattenArray($value), 'strlen') : $value, ', '));
         }
     }
     // Get fieldlayout
     foreach (craft()->fields->getLayoutByType(ElementType::Category)->getFields() as $field) {
         // Get field values
         $field = $field->getField();
         $handle = $field->handle;
         $label = $field->name;
         $value = $empty ? '' : craft()->auditLog->parseFieldData($handle, $category->{$handle});
         // Set on fields
         $fields[$handle] = array('label' => $label, 'value' => $value);
     }
     // Return
     return $fields;
 }