1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TbEditableDetailView class file. |
4
|
|
|
* |
5
|
|
|
* @author Vitaliy Potapov <[email protected]> |
6
|
|
|
* @link https://github.com/vitalets/x-editable-yii |
7
|
|
|
* @copyright Copyright © Vitaliy Potapov 2012 |
8
|
|
|
* @version 1.3.1 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
Yii::import('booster.widgets.TbEditableField'); |
12
|
|
|
Yii::import('zii.widgets.CDetailView'); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* TbEditableDetailView widget makes editable CDetailView (several attributes of single model shown as name-value table). |
16
|
|
|
* |
17
|
|
|
* @package widgets |
18
|
|
|
*/ |
19
|
|
|
class TbEditableDetailView extends CDetailView |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
public function init() |
23
|
|
|
{ |
24
|
|
|
if (!$this->data instanceof CModel) { |
|
|
|
|
25
|
|
|
throw new CException('Property "data" should be of CModel class.'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
//set bootstrap css |
29
|
|
|
/* TODO if(yii::app()->editable->form === 'bootstrap') { */ |
30
|
|
|
$this->htmlOptions = array('class'=> 'table table-bordered table-striped table-hover'); |
31
|
|
|
//disable loading Yii's css for bootstrap |
32
|
|
|
$this->cssFile = false; |
33
|
|
|
// } |
34
|
|
|
|
35
|
|
|
parent::init(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function renderItem($options, $templateData) |
39
|
|
|
{ |
40
|
|
|
//apply editable if not set 'editable' params or set and not false |
41
|
|
|
$apply = !empty($options['name']) && (!isset($options['editable']) || $options['editable'] !== false); |
42
|
|
|
|
43
|
|
|
if ($apply) { |
44
|
|
|
//ensure $options['editable'] is array |
45
|
|
|
if(!isset($options['editable'])) $options['editable'] = array(); |
46
|
|
|
|
47
|
|
|
//merge options with defaults: url, params, etc. |
48
|
|
|
$options['editable'] = CMap::mergeArray($this->_data, $options['editable']); |
49
|
|
|
|
50
|
|
|
//options to be passed into TbEditableField (constructed from $options['editable']) |
51
|
|
|
$widgetOptions = array( |
52
|
|
|
'model' => $this->data, |
53
|
|
|
'attribute' => $options['name'] |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
//if value in detailview options provided, set text directly (as value here means text) |
57
|
|
|
if(isset($options['value']) && $options['value'] !== null) { |
58
|
|
|
$widgetOptions['text'] = $templateData['{value}']; |
59
|
|
|
$widgetOptions['encode'] = false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$widgetOptions = CMap::mergeArray($widgetOptions, $options['editable']); |
63
|
|
|
|
64
|
|
|
$widget = $this->controller->createWidget('TbEditableField', $widgetOptions); |
65
|
|
|
|
66
|
|
|
//'apply' maybe changed during init of widget (e.g. if related model has unsafe attribute) |
67
|
|
|
if($widget->apply) { |
68
|
|
|
ob_start(); |
69
|
|
|
$widget->run(); |
70
|
|
|
$templateData['{value}'] = ob_get_clean(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
parent::renderItem($options, $templateData); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
//*************************************************************************************** |
78
|
|
|
// Generic getter/setter implementation to accept default configuration for TbEditableField |
79
|
|
|
//*************************************************************************************** |
80
|
|
|
|
81
|
|
|
/** Data for default fields of TbEditableField */ |
82
|
|
|
private $_data = array(); |
83
|
|
|
/** Valid attributes for TbEditableField (singleton) */ |
84
|
|
|
private $_editableProperties; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the properties available for {@link TbEditableField}. |
88
|
|
|
* |
89
|
|
|
* These properties can also be set for the {@link TbEditableDetailView} as default values. |
90
|
|
|
*/ |
91
|
|
|
private function getEditableProperties() |
92
|
|
|
{ |
93
|
|
|
if(!isset($this->_editableProperties)) |
94
|
|
|
{ |
95
|
|
|
$reflection = new ReflectionClass('TbEditableField'); |
96
|
|
|
$this->_editableProperties = array_map( |
97
|
|
|
function(ReflectionProperty $d) { return $d->getName(); }, |
98
|
|
|
$reflection->getProperties() |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
return $this->_editableProperties; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* (non-PHPdoc) |
106
|
|
|
* @see CComponent::__get() |
107
|
|
|
* |
108
|
|
|
* @param string $key |
109
|
|
|
* |
110
|
|
|
* @throws CException |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
public function __get($key) { |
114
|
|
|
return (array_key_exists($key,$this->_data) ? $this->_data[$key] : parent::__get($key)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* (non-PHPdoc) |
119
|
|
|
* @see CComponent::__set() |
120
|
|
|
* |
121
|
|
|
* @param string $key |
122
|
|
|
* @param mixed $value |
123
|
|
|
* |
124
|
|
|
* @throws CException |
125
|
|
|
* @return mixed|void |
126
|
|
|
*/ |
127
|
|
|
public function __set($key, $value) { |
128
|
|
|
if(in_array($key,$this->getEditableProperties())) { |
129
|
|
|
$this->_data[$key] = $value; |
130
|
|
|
} else { |
131
|
|
|
parent::__set($key,$value); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* (non-PHPdoc) |
137
|
|
|
* @see CComponent::__isset() |
138
|
|
|
* |
139
|
|
|
* @param string $name |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
public function __isset($name) { |
144
|
|
|
return array_key_exists($name,$this->_data)||parent::__isset($name); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.