1 | <?php |
||
22 | class ToggleAction extends Action |
||
23 | { |
||
24 | const TOGGLE_ANY = 'toggleany'; |
||
25 | const TOGGLE_UNIQ = 'toggleuniq'; |
||
26 | const TOGGLE_COND = 'togglecond'; |
||
27 | /** |
||
28 | * @var string the class name of the model. |
||
29 | */ |
||
30 | public $modelClass; |
||
31 | /** |
||
32 | * @var mixed the on value to set the attribute with. |
||
33 | */ |
||
34 | public $onValue = 1; |
||
35 | /** |
||
36 | * @var mixed the off value to set the attribute with. |
||
37 | */ |
||
38 | public $offValue = 0; |
||
39 | |||
40 | /** |
||
41 | * one of constants |
||
42 | * ToggleAction::TOGGLE_ANY - change state of any element (by default) |
||
43 | * ToggleAction::TOGGLE_UNIQ - only one element must be equal $onValue |
||
44 | * ToggleAction::TOGGLE_COND - toggle one element to $onValue per condition (for example only one foto can be set as cover per album) |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | public $toggleType = self::TOGGLE_ANY; |
||
49 | |||
50 | /** |
||
51 | * property for TOGGLE_UNIQ and TOGGLE_COND types - allow set all in offValue |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | public $allowAllOff = false; |
||
56 | |||
57 | /** |
||
58 | * @var mixed (array|string|callable) $condition the conditions that will be put in the WHERE part of the UPDATE SQL |
||
59 | * (used where $toggleType=ToggleAction::TOGGLE_COND) |
||
60 | * Please refer to [[Query::where()]] on how to specify this parameter. |
||
61 | * |
||
62 | * @example |
||
63 | * ['another_attribute'=>20] |
||
64 | * or |
||
65 | * function($model){ |
||
66 | * return ['attr'=>$model->attr]; |
||
67 | * } |
||
68 | * where $model - is current toggled model object |
||
69 | **/ |
||
70 | public $condition = []; |
||
71 | |||
72 | /** |
||
73 | * @var string scenario for this action |
||
74 | **/ |
||
75 | public $scenario = 'default'; |
||
76 | |||
77 | /** |
||
78 | * @inheritdoc |
||
79 | * @throws \yii\base\InvalidConfigException |
||
80 | */ |
||
81 | public function init() |
||
88 | |||
89 | /** |
||
90 | * @inheritdoc |
||
91 | * @throws \yii\web\BadRequestHttpException |
||
92 | */ |
||
93 | public function run($id, $attribute) |
||
138 | |||
139 | /** |
||
140 | * Finds the model based on its primary key value. |
||
141 | * If the model is not found, a 404 HTTP exception will be thrown. |
||
142 | * @param integer $id |
||
143 | * @throws NotFoundHttpException if the model cannot be found |
||
144 | * @return \yii\db\ActiveRecord the loaded model |
||
145 | */ |
||
146 | protected function findModel($id) |
||
154 | } |
||
155 |