HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbToggleAction.php
Go to the documentation of this file.
1 <?php
16 class TbToggleAction extends CAction
17 {
21  public $modelName;
22 
26  public $exceptionOnNullModel = true;
27 
32 
37 
41  public $yesValue = 1;
42 
46  public $noValue = 0;
47 
52 
57 
58 
67  public function run() {
68 
69  $pk = Yii::app()->request->getParam('pk');
70  $attribute = Yii::app()->request->getParam('attribute');
71 
72  if (Yii::app()->getRequest()->isPostRequest) {
73  $model = $this->loadModel($pk);
74  $model->$attribute = ($model->$attribute == $this->noValue) ? $this->yesValue : $this->noValue;
75  $success = $model->save(false, array($attribute));
76 
77  if (Yii::app()->getRequest()->isAjaxRequest) {
78  echo $success ? $this->ajaxResponseOnSuccess : $this->ajaxResponseOnFailed;
79  exit(0);
80  }
81  if ($this->redirectRoute !== null) {
82  $this->getController()->redirect($this->redirectRoute);
83  }
84  } else {
85  throw new CHttpException(Yii::t('zii', 'Invalid request'));
86  }
87  }
88 
97  protected function loadModel($id)
98  {
99  $finder = CActiveRecord::model($this->modelName);
100  if ($this->additionalCriteriaOnLoadModel) {
101  $c = new CDbCriteria($this->additionalCriteriaOnLoadModel);
102  $c->mergeWith(
103  array(
104  'condition' => $finder->tableSchema->primaryKey . '=:id',
105  'params' => array(':id' => $id),
106  )
107  );
108  $model = $finder->find($c);
109  } else {
110  $model = $finder->findByPk($id);
111  }
112 
113  if (!$model)
114  throw new CHttpException(404, 'Unable to find the requested object.');
115 
116  return $model;
117  }
118 }