1 <?php
2 /**
3 * WhToggleAction action class
4 *
5 * Works in conjunction with WhToggleColumn widget in order to ease the task of the developer to update the the attribute
6 * displayed on the grid. Just attach to the controller you wish to make the calls to.
7 *
8 * @author Antonio Ramirez <amigo.cobos@gmail.com>
9 * @copyright Copyright © 2amigos.us 2013-
10 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
11 * @package YiiWheels.widgets.toggle
12 */
13
14 class WhToggleAction extends CAction
15 {
16 /**
17 * @var string the name of the model we are going to toggle values to
18 */
19 public $modelName;
20
21 /**
22 * @var bool whether to throw an exception if we cannot find a model requested by the id
23 */
24 public $exceptionOnNullModel = true;
25
26 /**
27 * @var array additional criteria to use to get the model
28 */
29 public $additionalCriteriaOnLoadModel = array();
30
31 /**
32 * @var mixed the route to redirect the call after updating attribute
33 */
34 public $redirectRoute;
35
36 /**
37 * @var int|string the value to update the model to [yes|no] standard toggle options, but you can toggle any value.
38 */
39 public $yesValue = 1;
40
41 /**
42 * @var int|string the value to update the model to [yes|no]
43 */
44 public $noValue = 0;
45
46 /**
47 * @var mixed the response to return to an AJAX call when the attribute was successfully saved.
48 */
49 public $ajaxResponseOnSuccess = 1;
50
51 /**
52 * @var mixed the response to return to an AJAX call when failed to update the attribute.
53 */
54 public $ajaxResponseOnFailed = 0;
55
56
57 /**
58 * Widgets run function
59 * @param integer $id
60 * @param string $attribute
61 * @throws CHttpException
62 */
63 public function run($id, $attribute)
64 {
65 if (Yii::app()->getRequest()->isPostRequest) {
66 $model = $this->loadModel($id);
67 $model->$attribute = ($model->$attribute == $this->noValue) ? $this->yesValue : $this->noValue;
68 $success = $model->save(false, array($attribute));
69
70 if (Yii::app()->getRequest()->isAjaxRequest) {
71 echo $success ? $this->ajaxResponseOnSuccess : $this->ajaxResponseOnFailed;
72 exit(0);
73 }
74 if ($this->redirectRoute !== null) {
75 $this->getController()->redirect($this->redirectRoute);
76 }
77 } else {
78 throw new CHttpException(Yii::t('zii', 'Invalid request'));
79 }
80 }
81
82 /**
83 * Loads the requested data model.
84 * @param integer $id the model ID
85 * @return CActiveRecord the model instance.
86 * @throws CHttpException if the model cannot be found
87 */
88 protected function loadModel($id)
89 {
90 if (empty($this->additionalCriteriaOnLoadModel)) {
91 $model = CActiveRecord::model($this->modelName)->findByPk($id);
92 } else {
93 $finder = CActiveRecord::model($this->modelName);
94 $c = new CDbCriteria($this->additionalCriteriaOnLoadModel);
95 $c->mergeWith(
96 array(
97 'condition' => $finder->tableSchema->primaryKey . '=:id',
98 'params' => array(':id' => $id),
99 )
100 );
101 $model = $finder->find($c);
102 }
103 if (isset($model)) {
104 return $model;
105 }
106 if ($this->additionalCriteriaOnLoadModel) {
107 throw new CHttpException(404, 'Unable to find the requested object.');
108 }
109 }
110 }