示例#1
0
 /**
  * Returns the fields for the given context.
  * If the item is an object the returned fields do have an additional field
  * "value" which represents the value for the given item. If the item has a
  * catid field, then additionally fields which belong to that category will
  * be returned.
  * Should the value being prepared to be shown in a HTML context
  * prepareValue must be set to true. Then no further escaping needs to be
  * don.
  *
  * @param string $context
  * @param stdClass $item
  * @param boolean $prepareValue
  * @return array
  */
 public static function getFields($context, $item = null, $prepareValue = false)
 {
     if (self::$fieldsCache === null) {
         // Load the model
         JLoader::import('joomla.application.component.model');
         JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_dpfields/models', 'DPFieldsModel');
         self::$fieldsCache = JModelLegacy::getInstance('Fields', 'DPFieldsModel', array('ignore_request' => true));
         self::$fieldsCache->setState('filter.published', 1);
         self::$fieldsCache->setState('filter.language', JFactory::getLanguage()->getTag());
         self::$fieldsCache->setState('list.limit', 0);
     }
     self::$fieldsCache->setState('filter.context', $context);
     if (is_array($item)) {
         $item = (object) $item;
     }
     // If item has catid parameter display only fields which belong to the
     // category
     if ($item && (isset($item->catid) || isset($item->dpfieldscatid))) {
         $catids = isset($item->catid) ? $item->catid : $item->dpfieldscatid;
         self::$fieldsCache->setState('filter.catid', is_array($catids) ? $catids : explode(',', $catids));
     }
     $fields = self::$fieldsCache->getItems();
     if ($item && isset($item->id)) {
         if (self::$fieldCache === null) {
             self::$fieldCache = JModelLegacy::getInstance('Field', 'DPFieldsModel', array('ignore_request' => true));
         }
         $new = array();
         foreach ($fields as $key => $original) {
             // Doing a clone, otherwise fields for different items will
             // always reference to the same object
             $field = clone $original;
             $field->value = self::$fieldCache->getFieldValue($field->id, $field->context, $item->id);
             if (!$field->value) {
                 $field->value = $field->default_value;
             }
             $field->rawvalue = $field->value;
             if ($prepareValue) {
                 $type = self::loadTypeObject($field->type, $field->context);
                 if ($type) {
                     $field->value = $type->prepareValueForDisplay($field->value, $field);
                 }
             }
             $new[$key] = $field;
         }
         $fields = $new;
     }
     return $fields;
 }