Пример #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;
     }
 }
Пример #2
0
 /**
  * Returns available commands of a specified module.
  * @param \yii\base\Module $module the module instance
  * @return array the available command names
  */
 protected function getModuleCommands($module)
 {
     $prefix = $module instanceof Application ? '' : $module->getUniqueID() . '/';
     $commands = [];
     foreach (array_keys($module->controllerMap) as $id) {
         $commands[] = $prefix . $id;
     }
     foreach ($module->getModules() as $id => $child) {
         if (($child = $module->getModule($id)) === null) {
             continue;
         }
         foreach ($this->getModuleCommands($child) as $command) {
             $commands[] = $command;
         }
     }
     $controllerPath = $module->getControllerPath();
     if (is_dir($controllerPath)) {
         $files = scandir($controllerPath);
         foreach ($files as $file) {
             if (!empty($file) && substr_compare($file, 'Controller.php', -14, 14) === 0) {
                 $controllerClass = $module->controllerNamespace . '\\' . substr(basename($file), 0, -4);
                 if ($this->validateControllerClass($controllerClass)) {
                     $commands[] = $prefix . Inflector::camel2id(substr(basename($file), 0, -14));
                 }
             }
         }
     }
     return $commands;
 }
Пример #3
0
 /**
  * Declares the name of the database table associated with this AR class.
  * By default this method returns the class name as the table name by calling [[Inflector::camel2id()]]
  * with prefix [[Connection::tablePrefix]]. For example if [[Connection::tablePrefix]] is 'tbl_',
  * 'Customer' becomes 'tbl_customer', and 'OrderItem' becomes 'tbl_order_item'. You may override this method
  * if the table is not named after this convention.
  * @return string the table name
  */
 public static function tableName()
 {
     return '{{%' . Inflector::camel2id(StringHelper::basename(get_called_class()), '_') . '}}';
 }
Пример #4
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;
     }
 }
Пример #5
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);
 }
Пример #6
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     if (empty($this->controller)) {
         throw new InvalidConfigException('"controller" must be set.');
     }
     $controllers = [];
     foreach ((array) $this->controller as $urlName => $controller) {
         if (is_int($urlName)) {
             $urlName = $this->pluralize ? Inflector::pluralize($controller) : $controller;
         }
         $controllers[$urlName] = $controller;
     }
     $this->controller = $controllers;
     $this->prefix = trim($this->prefix, '/');
     parent::init();
 }
Пример #7
0
 /**
  * @inheritdoc
  */
 protected function getValue($event)
 {
     $isNewSlug = true;
     if ($this->attribute !== null) {
         $attributes = (array) $this->attribute;
         /* @var $owner BaseActiveRecord */
         $owner = $this->owner;
         if (!empty($owner->{$this->slugAttribute})) {
             $isNewSlug = false;
             if (!$this->immutable) {
                 foreach ($attributes as $attribute) {
                     if ($owner->isAttributeChanged($attribute)) {
                         $isNewSlug = true;
                         break;
                     }
                 }
             }
         }
         if ($isNewSlug) {
             $slugParts = [];
             foreach ($attributes as $attribute) {
                 $slugParts[] = $owner->{$attribute};
             }
             $slug = Inflector::slug(implode('-', $slugParts));
         } else {
             $slug = $owner->{$this->slugAttribute};
         }
     } else {
         $slug = parent::getValue($event);
     }
     if ($this->ensureUnique && $isNewSlug) {
         $baseSlug = $slug;
         $iteration = 0;
         while (!$this->validateSlug($slug)) {
             $iteration++;
             $slug = $this->generateUniqueSlug($baseSlug, $iteration);
         }
     }
     return $slug;
 }
Пример #8
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);
 }