Exemple #1
0
 /**
  * Retrieves detail view attributes configuration using the modelClass.
  * @param Model $model
  * @param array $fields
  * @return array detail view attributes
  */
 public function getDetailAttributes($model, $fields)
 {
     if (!$model instanceof ActiveRecord) {
         return $model->attributes();
     }
     /** @var ActiveRecord $model */
     $formats = $model->attributeFormats();
     $keys = self::getModelKeys($model);
     list($behaviorAttributes, $blameableAttributes) = self::getModelBehaviorAttributes($model);
     $attributes = $model->attributes();
     $versionAttribute = $model->optimisticLock();
     $result = [];
     foreach ($fields as $key => $field) {
         if (is_array($field)) {
             $result[$key] = $field;
             continue;
         } elseif (!is_string($field) && is_callable($field)) {
             $result[$key] = call_user_func($field, $model);
             continue;
         }
         if (in_array($field, $attributes)) {
             if (in_array($field, $keys) || in_array($field, $blameableAttributes) || $field === $versionAttribute) {
                 continue;
             }
             $result[$field] = ['attribute' => $field, 'format' => $formats[$field]];
             continue;
         }
         $relation = $model->getRelation($field);
         if (!$relation->multiple) {
             // validate foreign keys only for hasOne relations
             foreach ($relation->link as $left => $right) {
                 if (in_array($left, $blameableAttributes)) {
                     continue;
                 }
             }
         }
         if (!Yii::$app->user->can($relation->modelClass . '.read')) {
             continue;
         }
         $result[$field] = ['attribute' => $field, 'format' => 'crudLink', 'visible' => true, 'label' => $model instanceof ActiveRecord ? $model->getRelationLabel($relation, $field) : null];
     }
     return $result;
 }