1 | <?php |
||
16 | class TbToggleAction extends CAction |
||
17 | { |
||
18 | /** |
||
19 | * @var string the name of the model we are going to toggle values to |
||
20 | */ |
||
21 | public $modelName; |
||
22 | |||
23 | /** |
||
24 | * @var bool whether to throw an exception if we cannot find a model requested by the id |
||
25 | */ |
||
26 | public $exceptionOnNullModel = true; |
||
27 | |||
28 | /** |
||
29 | * @var array additional criteria to use to get the model |
||
30 | */ |
||
31 | public $additionalCriteriaOnLoadModel = array(); |
||
32 | |||
33 | /** |
||
34 | * @var mixed the route to redirect the call after updating attribute |
||
35 | */ |
||
36 | public $redirectRoute; |
||
37 | |||
38 | /** |
||
39 | * @var int|string the value to update the model to [yes|no] standard toggle options, but you can toggle any value. |
||
40 | */ |
||
41 | public $yesValue = 1; |
||
42 | |||
43 | /** |
||
44 | * @var int|string the value to update the model to [yes|no] |
||
45 | */ |
||
46 | public $noValue = 0; |
||
47 | |||
48 | /** |
||
49 | * @var mixed the response to return to an AJAX call when the attribute was successfully saved. |
||
50 | */ |
||
51 | public $ajaxResponseOnSuccess = 1; |
||
52 | |||
53 | /** |
||
54 | * @var mixed the response to return to an AJAX call when failed to update the attribute. |
||
55 | */ |
||
56 | public $ajaxResponseOnFailed = 0; |
||
57 | |||
58 | |||
59 | /** |
||
60 | * Widgets run function |
||
61 | * |
||
62 | * @throws CHttpException |
||
63 | */ |
||
64 | public function run() { |
||
65 | |||
66 | $pk = Yii::app()->request->getParam('pk'); |
||
67 | $attribute = Yii::app()->request->getParam('attribute'); |
||
68 | |||
69 | if (!Yii::app()->getRequest()->isPostRequest) |
||
70 | throw new CHttpException(400); |
||
71 | |||
72 | $model = $this->loadModel($pk); |
||
73 | $model->$attribute = ($model->$attribute == $this->noValue) ? $this->yesValue : $this->noValue; |
||
74 | $success = $model->save(false, array($attribute)); |
||
75 | |||
76 | if (Yii::app()->getRequest()->isAjaxRequest) { |
||
77 | echo $success ? $this->ajaxResponseOnSuccess : $this->ajaxResponseOnFailed; |
||
78 | return; |
||
79 | } |
||
80 | if ($this->redirectRoute !== null) { |
||
81 | $this->getController()->redirect($this->redirectRoute); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Loads the requested data model. |
||
87 | * |
||
88 | * @param integer $id the model ID |
||
89 | * |
||
90 | * @return CActiveRecord the model instance. |
||
91 | * @throws CHttpException if the model cannot be found |
||
92 | */ |
||
93 | protected function loadModel($id) |
||
114 | } |
||
115 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.