|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
*## TbToggleAction class file |
|
4
|
|
|
* |
|
5
|
|
|
* @author antonio ramirez <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
*## TbToggleAction CAction Component |
|
10
|
|
|
* |
|
11
|
|
|
* It is a component that works in conjunction of TbToggleColumn widget. Just attach to the controller you wish to |
|
12
|
|
|
* make the calls to. |
|
13
|
|
|
* |
|
14
|
|
|
* @package booster.actions |
|
15
|
|
|
*/ |
|
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) |
|
94
|
|
|
{ |
|
95
|
|
|
$finder = CActiveRecord::model($this->modelName); |
|
96
|
|
|
if ($this->additionalCriteriaOnLoadModel) { |
|
|
|
|
|
|
97
|
|
|
$c = new CDbCriteria($this->additionalCriteriaOnLoadModel); |
|
98
|
|
|
$c->mergeWith( |
|
99
|
|
|
array( |
|
100
|
|
|
'condition' => $finder->tableSchema->primaryKey . '=:id', |
|
101
|
|
|
'params' => array(':id' => $id), |
|
102
|
|
|
) |
|
103
|
|
|
); |
|
104
|
|
|
$model = $finder->find($c); |
|
105
|
|
|
} else { |
|
106
|
|
|
$model = $finder->findByPk($id); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (!$model) |
|
110
|
|
|
throw new CHttpException(404, 'Unable to find the requested object.'); |
|
111
|
|
|
|
|
112
|
|
|
return $model; |
|
113
|
|
|
} |
|
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.