1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18
19 Yii::import('yiiwheels.widgets.editable.WhEditableField');
20 Yii::import('zii.widgets.CDetailView');
21
22 class WhEditableDetailView extends CDetailView
23 {
24
25 26 27
28 private $_data = array();
29
30 31 32
33 private $_editableProperties;
34
35
36 37 38 39
40 public function init()
41 {
42 if (!$this->data instanceof CModel) {
43 throw new CException('Property "data" should be of CModel class.');
44 }
45
46
47 $this->htmlOptions = array('class' => 'table table-bordered table-striped table-hover');
48
49 $this->cssFile = false;
50
51
52 parent::init();
53 }
54
55 56 57 58 59
60 protected function renderItem($options, $templateData)
61 {
62
63 $apply = !empty($options['name']) && (!isset($options['editable']) || $options['editable'] !== false);
64
65 if ($apply) {
66
67 if (!isset($options['editable'])) $options['editable'] = array();
68
69
70 $options['editable'] = CMap::mergeArray($this->_data, $options['editable']);
71
72
73 $widgetOptions = array(
74 'model' => $this->data,
75 'attribute' => $options['name']
76 );
77
78
79 if (isset($options['value']) && $options['value'] !== null) {
80 $widgetOptions['text'] = $templateData['{value}'];
81 $widgetOptions['encode'] = false;
82 }
83
84 $widgetOptions = CMap::mergeArray($widgetOptions, $options['editable']);
85
86 $widget = $this->controller->createWidget('WhEditableField', $widgetOptions);
87
88
89 if ($widget->apply) {
90 ob_start();
91 $widget->run();
92 $templateData['{value}'] = ob_get_clean();
93 }
94 }
95
96 parent::renderItem($options, $templateData);
97 }
98
99 100 101 102
103 private function getEditableProperties()
104 {
105 if (!isset($this->_editableProperties)) {
106 $reflection = new ReflectionClass('WhEditableField');
107 $this->_editableProperties = array_map(function ($d) {
108 return $d->getName();
109 }, $reflection->getProperties());
110 }
111 return $this->_editableProperties;
112 }
113
114 115 116 117
118 public function __get($key)
119 {
120 return (array_key_exists($key, $this->_data) ? $this->_data[$key] : parent::__get($key));
121 }
122
123 124 125 126
127 public function __set($key, $value)
128 {
129 if (in_array($key, $this->getEditableProperties())) {
130 $this->_data[$key] = $value;
131 } else {
132 parent::__set($key, $value);
133 }
134 }
135
136 137 138 139
140 public function __isset($name)
141 {
142 return array_key_exists($name, $this->_data) || parent::__isset($name);
143 }
144 }
145