This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * CToggleColumn class file. |
||
4 | * @author Nikola Trifunovic <[email protected]> |
||
5 | * @link http://www.trifunovic.me/ |
||
6 | * @copyright Copyright © 2012 Nikola Trifunovic |
||
7 | * @license http://www.yiiframework.com/license/ |
||
8 | */ |
||
9 | class JToggleColumn extends BDataColumn |
||
10 | { |
||
11 | /** |
||
12 | * @var string the attribute name of the data model. Used for column sorting, filtering and to render the corresponding |
||
13 | * attribute value in each data cell. If {@link value} is specified it will be used to rendered the data cell instead of the attribute value. |
||
14 | * @see value |
||
15 | * @see sortable |
||
16 | */ |
||
17 | public $name; |
||
18 | |||
19 | /** |
||
20 | * @var array the HTML options for the data cell tags. |
||
21 | */ |
||
22 | public $htmlOptions = array('class' => 'toggle-column'); |
||
23 | |||
24 | /** |
||
25 | * @var array the HTML options for the header cell tag. |
||
26 | */ |
||
27 | public $headerHtmlOptions = array('class' => 'toggle-column'); |
||
28 | |||
29 | /** |
||
30 | * @var array the HTML options for the footer cell tag. |
||
31 | */ |
||
32 | public $footerHtmlOptions = array('class' => 'toggle-column'); |
||
33 | |||
34 | /** |
||
35 | * @var string the label for the toggle button. Defaults to "toggle". |
||
36 | * Note that the label will not be HTML-encoded when rendering. |
||
37 | */ |
||
38 | public $checkedButtonLabel; |
||
39 | |||
40 | /** |
||
41 | * @var string the label for the toggle button. Defaults to "toggle". |
||
42 | * Note that the label will not be HTML-encoded when rendering. |
||
43 | */ |
||
44 | public $uncheckedButtonLabel; |
||
45 | |||
46 | /** |
||
47 | * @var string the image URL for the toggle button. If not set, an integrated image will be used. |
||
48 | * You may set this property to be false to render a text link instead. |
||
49 | */ |
||
50 | public $checkedButtonImageUrl; |
||
51 | |||
52 | /** |
||
53 | * @var string the image URL for the toggle button. If not set, an integrated image will be used. |
||
54 | * You may set this property to be false to render a text link instead. |
||
55 | */ |
||
56 | public $uncheckedButtonImageUrl; |
||
57 | |||
58 | /** |
||
59 | * @var array the configuration for toggle button. |
||
60 | */ |
||
61 | public $toggle_button = array(); |
||
62 | |||
63 | /** |
||
64 | * @var boolean whether the column is sortable. If so, the header cell will contain a link that may trigger the sorting. |
||
65 | * Defaults to true. Note that if {@link name} is not set, or if {@link name} is not allowed by {@link CSort}, |
||
66 | * this property will be treated as false. |
||
67 | * @see name |
||
68 | */ |
||
69 | public $sortable = true; |
||
70 | |||
71 | /** |
||
72 | * @var mixed the HTML code representing a filter input (eg a text field, a dropdown list) |
||
73 | * that is used for this data column. This property is effective only when |
||
74 | * {@link CGridView::filter} is set. |
||
75 | * If this property is not set, a text field will be generated as the filter input; |
||
76 | * If this property is an array, a dropdown list will be generated that uses this property value as |
||
77 | * the list options. |
||
78 | * If you don't want a filter for this data column, set this value to false. |
||
79 | * @since 1.1.1 |
||
80 | */ |
||
81 | public $filter; |
||
82 | |||
83 | /** |
||
84 | * @var string Name of the action |
||
85 | */ |
||
86 | public $action; |
||
87 | |||
88 | /** |
||
89 | * @var string |
||
90 | */ |
||
91 | public $ajaxUrl; |
||
92 | |||
93 | /** |
||
94 | * @var string Assets url |
||
95 | */ |
||
96 | private $_assetsUrl; |
||
97 | |||
98 | /** |
||
99 | * Returns assets url, where check and uncheck images are located |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getAssetsUrl() |
||
103 | { |
||
104 | if( $this->_assetsUrl === null ) |
||
105 | $this->_assetsUrl = Yii::app()->getAssetManager()->publish(dirname(__FILE__).'/images'); |
||
106 | return $this->_assetsUrl; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Initializes the column. |
||
111 | * This method registers necessary client script for the button column. |
||
112 | */ |
||
113 | public function init() |
||
114 | { |
||
115 | if( $this->name === null ) |
||
116 | $this->sortable = false; |
||
117 | if( $this->name === null ) |
||
118 | throw new CException(Yii::t('toggle_column', 'Model attribute ("name") must be specified for CToggleColumn.')); |
||
119 | |||
120 | $this->initDefaultButtons(); |
||
121 | |||
122 | $this->toggle_button['click'] = 'js:'.$this->toggle_button['click']; |
||
123 | |||
124 | $this->registerClientScript(); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Initializes the default buttons (toggle). |
||
129 | */ |
||
130 | protected function initDefaultButtons() |
||
131 | { |
||
132 | if( $this->checkedButtonLabel === null ) |
||
133 | $this->checkedButtonLabel = Yii::t('toggle_column', 'Выключить'); |
||
134 | if( $this->uncheckedButtonLabel === null ) |
||
135 | $this->uncheckedButtonLabel = Yii::t('toggle_column', 'Включить'); |
||
136 | if( $this->checkedButtonImageUrl === null ) |
||
137 | $this->checkedButtonImageUrl = $this->getAssetsUrl().'/toggle-on.png'; |
||
138 | if( $this->uncheckedButtonImageUrl === null ) |
||
139 | $this->uncheckedButtonImageUrl = $this->getAssetsUrl().'/toggle-off.png'; |
||
140 | |||
141 | if( $this->action === null ) |
||
142 | $this->action = 'toggle'; |
||
143 | |||
144 | if( !isset($this->toggle_button['htmlOptions']) ) |
||
145 | $this->toggle_button['htmlOptions'] = array('rel' => 'tooltip'); |
||
146 | |||
147 | $this->toggle_button = array( |
||
148 | 'url' => $this->ajaxUrl ? $this->ajaxUrl : 'Yii::app()->controller->createUrl("'.$this->action.'", array("id" => $data["primaryKey"], "attribute" => "'.$this->name.'"))', |
||
149 | 'options' => CMap::mergeArray(array('class' => $this->name.'_toggle'), $this->toggle_button['htmlOptions']), |
||
150 | ); |
||
151 | |||
152 | if( Yii::app()->request->enableCsrfValidation ) |
||
153 | { |
||
154 | $csrfTokenName = Yii::app()->request->csrfTokenName; |
||
155 | $csrfToken = Yii::app()->request->csrfToken; |
||
156 | $csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },"; |
||
157 | } |
||
158 | else |
||
159 | $csrf = ''; |
||
160 | |||
161 | $this->toggle_button['click'] = <<<EOD |
||
162 | function() { |
||
163 | var th=this; |
||
164 | $.fn.yiiGridView.update('{$this->grid->id}', { |
||
165 | type:'POST', |
||
166 | url:$(this).attr('href'),$csrf |
||
167 | success:function(data) { |
||
168 | $.fn.yiiGridView.update('{$this->grid->id}'); |
||
169 | }, |
||
170 | ajaxUpdate:true |
||
171 | }); |
||
172 | return false; |
||
173 | } |
||
174 | EOD; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Registers the client scripts for the button column. |
||
179 | */ |
||
180 | protected function registerClientScript() |
||
181 | { |
||
182 | $js = array(); |
||
183 | |||
184 | $function = CJavaScript::encode($this->toggle_button['click']); |
||
185 | $class = preg_replace('/\s+/', '.', $this->toggle_button['options']['class']); |
||
186 | $js[] = "jQuery('body').on('click', '#{$this->grid->id} a.{$class}', $function);"; |
||
187 | |||
188 | if( $js !== array() ) |
||
189 | Yii::app()->clientScript->registerScript(__CLASS__.'#'.$this->id, implode("\n", $js)); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Renders the data cell content. |
||
194 | * This method renders the view, update and toggle buttons in the data cell. |
||
195 | * |
||
196 | * @param integer $row the row number (zero-based) |
||
197 | * @param mixed $data the data associated with the row |
||
198 | */ |
||
199 | protected function renderDataCellContent($row, $data) |
||
200 | { |
||
201 | ob_start(); |
||
202 | $this->renderButton($this->toggle_button, $row, $data); |
||
203 | $toggle_button = ob_get_contents(); |
||
204 | ob_clean(); |
||
205 | ob_end_clean(); |
||
206 | echo $toggle_button; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Renders the header cell content. |
||
211 | * This method will render a link that can trigger the sorting if the column is sortable. |
||
212 | */ |
||
213 | protected function renderHeaderCellContent() |
||
214 | { |
||
215 | if( $this->grid->enableSorting && $this->sortable && $this->name !== null ) |
||
216 | echo $this->grid->dataProvider->getSort()->link($this->name, $this->header); |
||
217 | View Code Duplication | else if( $this->name !== null && $this->header === null ) |
|
0 ignored issues
–
show
|
|||
218 | { |
||
219 | if( $this->grid->dataProvider instanceof CActiveDataProvider ) |
||
0 ignored issues
–
show
The class
CActiveDataProvider does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. ![]() |
|||
220 | echo CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name)); |
||
221 | else |
||
222 | echo CHtml::encode($this->name); |
||
223 | } |
||
224 | else |
||
225 | parent::renderHeaderCellContent(); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Renders a toggle button. |
||
230 | * |
||
231 | * @param array $button the button configuration which may contain 'label', 'url', 'imageUrl' and 'options' elements. |
||
232 | * @param integer $row the row number (zero-based) |
||
233 | * @param mixed $data the data object associated with the row |
||
234 | * |
||
235 | * @internal param string $id the ID of the button |
||
236 | */ |
||
237 | protected function renderButton($button, $row, $data) |
||
238 | { |
||
239 | if( $this->name !== null ) |
||
240 | $checked = CHtml::value($data, $this->name); |
||
241 | |||
242 | $button['imageUrl'] = $checked ? $this->checkedButtonImageUrl : $this->uncheckedButtonImageUrl; |
||
0 ignored issues
–
show
The variable
$checked does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
![]() |
|||
243 | $button['label'] = $checked ? $this->checkedButtonLabel : $this->uncheckedButtonLabel; |
||
244 | |||
245 | $label = $button['label']; |
||
246 | $url = isset($button['url']) ? $this->evaluateExpression($button['url'], array('data' => $data, 'row' => $row)) : '#'; |
||
247 | $options = isset($button['options']) ? $button['options'] : array(); |
||
248 | if( !isset($options['title']) ) |
||
249 | $options['title'] = $label; |
||
250 | View Code Duplication | if( isset($button['imageUrl']) && is_string($button['imageUrl']) ) |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
251 | echo CHtml::link(CHtml::image($button['imageUrl'], $label), $url, $options); |
||
252 | else |
||
253 | echo CHtml::link($label, $url, $options); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Renders the filter cell content. |
||
258 | * This method will render the {@link filter} as is if it is a string. |
||
259 | * If {@link filter} is an array, it is assumed to be a list of options, and a dropdown selector will be rendered. |
||
260 | * Otherwise if {@link filter} is not false, a text field is rendered. |
||
261 | * @since 1.1.1 |
||
262 | */ |
||
263 | protected function renderFilterCellContent() |
||
264 | { |
||
265 | if( $this->filter !== null ) |
||
266 | { |
||
267 | if( is_string($this->filter) ) |
||
268 | echo $this->filter; |
||
269 | else if( $this->filter !== false && $this->grid->filter !== null && $this->name !== null && strpos($this->name, '.') === false ) |
||
270 | { |
||
271 | echo CHtml::activeLabelEx($this->grid->filter, $this->name, array('id' => false)); |
||
272 | |||
273 | if( is_array($this->filter) ) |
||
274 | echo CHtml::activeDropDownList($this->grid->filter, $this->name, $this->filter, array('id' => false, 'prompt' => '')); |
||
275 | else if( $this->filter === null ) |
||
276 | echo CHtml::activeTextField($this->grid->filter, $this->name, array('id' => false)); |
||
277 | } |
||
278 | else |
||
279 | parent::renderFilterCellContent(); |
||
280 | } |
||
281 | } |
||
282 | |||
283 | protected function renderFilterDivContent() |
||
284 | { |
||
285 | if( is_string($this->filter) ) |
||
286 | echo $this->filter; |
||
287 | else if( $this->filter !== false && $this->grid->filter !== null && $this->name !== null && strpos($this->name, '.') === false ) |
||
288 | { |
||
289 | echo CHtml::activeLabel($this->grid->filter, $this->name, array('id' => false)); |
||
290 | echo CHtml::activeDropDownList($this->grid->filter, $this->name, CHtml::listData($this->grid->filter->yesNoList(), 'id', 'name'), array('id' => false, 'prompt' => '')); |
||
291 | } |
||
292 | } |
||
293 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.