|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
-
-
- namespace yii\widgets;
-
- use Yii;
- use yii\base\Arrayable;
- use yii\i18n\Formatter;
- use yii\base\InvalidConfigException;
- use yii\base\Model;
- use yii\base\Widget;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Html;
- use yii\helpers\Inflector;
-
-
- class DetailView extends Widget
- {
-
-
- public $model;
-
-
- public $attributes;
-
-
- public $template = '<tr><th{captionOptions}>{label}</th><td{contentOptions}>{value}</td></tr>';
-
-
- public $options = ['class' => 'table table-striped table-bordered detail-view'];
-
-
- public $formatter;
-
-
-
-
- public function init()
- {
- if ($this->model === null) {
- throw new InvalidConfigException('Please specify the "model" property.');
- }
- if ($this->formatter === null) {
- $this->formatter = Yii::$app->getFormatter();
- } elseif (is_array($this->formatter)) {
- $this->formatter = Yii::createObject($this->formatter);
- }
- if (!$this->formatter instanceof Formatter) {
- throw new InvalidConfigException('The "formatter" property must be either a Format object or a configuration array.');
- }
- $this->normalizeAttributes();
-
- if (!isset($this->options['id'])) {
- $this->options['id'] = $this->getId();
- }
- }
-
-
-
- public function run()
- {
- $rows = [];
- $i = 0;
- foreach ($this->attributes as $attribute) {
- $rows[] = $this->renderAttribute($attribute, $i++);
- }
-
- $options = $this->options;
- $tag = ArrayHelper::remove($options, 'tag', 'table');
- echo Html::tag($tag, implode("\n", $rows), $options);
- }
-
-
-
- protected function renderAttribute($attribute, $index)
- {
- if (is_string($this->template)) {
- $captionOptions = Html::renderTagAttributes(ArrayHelper::getValue($attribute, 'captionOptions', []));
- $contentOptions = Html::renderTagAttributes(ArrayHelper::getValue($attribute, 'contentOptions', []));
- return strtr($this->template, [
- '{label}' => $attribute['label'],
- '{value}' => $this->formatter->format($attribute['value'], $attribute['format']),
- '{captionOptions}' => $captionOptions,
- '{contentOptions}' => $contentOptions,
- ]);
- } else {
- return call_user_func($this->template, $attribute, $index, $this);
- }
- }
-
-
-
- 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 ? array_keys($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;
- }
- }
- }
|