예제 #1
0
 /**
  * Renders the content of a menu item.
  * Note that the container and the sub-menus are not rendered here.
  * @param array $item the menu item to be rendered. Please refer to [[items]] to see what data might be in the item.
  * @return string the rendering result
  */
 protected function renderItem($item)
 {
     if (isset($item['url'])) {
         $template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
         return strtr($template, ['{url}' => Html::encode(Url::to($item['url'])), '{label}' => $item['label']]);
     } else {
         $template = ArrayHelper::getValue($item, 'template', $this->labelTemplate);
         return strtr($template, ['{label}' => $item['label']]);
     }
 }
예제 #2
0
 /**
  * Normalizes the attribute specifications.
  * @throws InvalidConfigException
  */
 protected function normalizeAttributes()
 {
     if ($this->attributes === null) {
         if ($this->model instanceof Model) {
             $this->attributes = $this->model->attributes();
         } elseif (is_object($this->model)) {
             $this->attributes = $this->model instanceof Arrayable ? $this->model->toArray() : array_keys(get_object_vars($this->model));
         } elseif (is_array($this->model)) {
             $this->attributes = array_keys($this->model);
         } else {
             throw new InvalidConfigException('The "model" property must be either an array or an object.');
         }
         sort($this->attributes);
     }
     foreach ($this->attributes as $i => $attribute) {
         if (is_string($attribute)) {
             if (!preg_match('/^([\\w\\.]+)(:(\\w*))?(:(.*))?$/', $attribute, $matches)) {
                 throw new InvalidConfigException('The attribute must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"');
             }
             $attribute = ['attribute' => $matches[1], 'format' => isset($matches[3]) ? $matches[3] : 'text', 'label' => isset($matches[5]) ? $matches[5] : null];
         }
         if (!is_array($attribute)) {
             throw new InvalidConfigException('The attribute configuration must be an array.');
         }
         if (isset($attribute['visible']) && !$attribute['visible']) {
             unset($this->attributes[$i]);
             continue;
         }
         if (!isset($attribute['format'])) {
             $attribute['format'] = 'text';
         }
         if (isset($attribute['attribute'])) {
             $attributeName = $attribute['attribute'];
             if (!isset($attribute['label'])) {
                 $attribute['label'] = $this->model instanceof Model ? $this->model->getAttributeLabel($attributeName) : Inflector::camel2words($attributeName, true);
             }
             if (!array_key_exists('value', $attribute)) {
                 $attribute['value'] = ArrayHelper::getValue($this->model, $attributeName);
             }
         } elseif (!isset($attribute['label']) || !array_key_exists('value', $attribute)) {
             throw new InvalidConfigException('The attribute configuration requires the "attribute" element to determine the value and display label.');
         }
         $this->attributes[$i] = $attribute;
     }
 }
예제 #3
0
 /**
  * Returns the data cell value.
  * @param mixed $model the data model
  * @param mixed $key the key associated with the data model
  * @param integer $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
  * @return string the data cell value
  */
 public function getDataCellValue($model, $key, $index)
 {
     if ($this->value !== null) {
         if (is_string($this->value)) {
             return ArrayHelper::getValue($model, $this->value);
         } else {
             return call_user_func($this->value, $model, $key, $index, $this);
         }
     } elseif ($this->attribute !== null) {
         return ArrayHelper::getValue($model, $this->attribute);
     }
     return null;
 }