Exemplo n.º 1
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;
     }
 }
Exemplo n.º 2
0
 /**
  * Generates a user friendly attribute label based on the give attribute name.
  * This is done by replacing underscores, dashes and dots with blanks and
  * changing the first letter of each word to upper case.
  * For example, 'department_name' or 'DepartmentName' will generate 'Department Name'.
  * @param string $name the column name
  * @return string the attribute label
  */
 public function generateAttributeLabel($name)
 {
     return Inflector::camel2words($name, true);
 }
Exemplo n.º 3
0
 /**
  * @inheritdoc
  */
 protected function renderHeaderCellContent()
 {
     if ($this->header !== null || $this->label === null && $this->attribute === null) {
         return parent::renderHeaderCellContent();
     }
     $provider = $this->grid->dataProvider;
     if ($this->label === null) {
         if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
             /* @var $model Model */
             $model = new $provider->query->modelClass();
             $label = $model->getAttributeLabel($this->attribute);
         } else {
             $models = $provider->getModels();
             if (($model = reset($models)) instanceof Model) {
                 /* @var $model Model */
                 $label = $model->getAttributeLabel($this->attribute);
             } else {
                 $label = Inflector::camel2words($this->attribute);
             }
         }
     } else {
         $label = $this->label;
     }
     if ($this->attribute !== null && $this->enableSorting && ($sort = $provider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
         return $sort->link($this->attribute, array_merge($this->sortLinkOptions, ['label' => $this->encodeLabel ? Html::encode($label) : $label]));
     } else {
         return $this->encodeLabel ? Html::encode($label) : $label;
     }
 }
Exemplo n.º 4
0
 /**
  * Generates a hyperlink that links to the sort action to sort by the specified attribute.
  * Based on the sort direction, the CSS class of the generated hyperlink will be appended
  * with "asc" or "desc".
  * 
  * @param string $attribute the attribute name by which the data should be sorted by.
  * @param array $options additional HTML attributes for the hyperlink tag.
  *        There is one special attribute `label` which will be used as the label of the hyperlink.
  *        If this is not set, the label defined in [[attributes]] will be used.
  *        If no label is defined, [[\Leaps\Helper\Inflector::camel2words()]] will be called to get a label.
  *        Note that it will not be HTML-encoded.
  * @return string the generated hyperlink
  * @throws InvalidConfigException if the attribute is unknown
  */
 public function link($attribute, $options = [])
 {
     if (($direction = $this->getAttributeOrder($attribute)) !== null) {
         $class = $direction === SORT_DESC ? 'desc' : 'asc';
         if (isset($options['class'])) {
             $options['class'] .= ' ' . $class;
         } else {
             $options['class'] = $class;
         }
     }
     $url = $this->createUrl($attribute);
     $options['data-sort'] = $this->createSortParam($attribute);
     if (isset($options['label'])) {
         $label = $options['label'];
         unset($options['label']);
     } else {
         if (isset($this->attributes[$attribute]['label'])) {
             $label = $this->attributes[$attribute]['label'];
         } else {
             $label = Inflector::camel2words($attribute);
         }
     }
     return Html::a($label, $url, $options);
 }