1 <?php
2
3 class P3WidgetController extends Controller
4 {
5
6 public $defaultAction = "admin";
7 public $scenario = "crud";
8
9 public function filters() {
10 return array(
11 'accessControl',
12 );
13 }
14
15 public function accessRules() {
16 return array(
17 array('allow',
18 'actions'=>array('create','ajaxUpdate','update','delete','admin','view','classVars','updateOrder'),
19 'roles'=>array('P3widgets.P3Widget.*'),
20 ),
21 array('deny',
22 'users'=>array('*'),
23 ),
24 );
25 }
26
27 public function beforeAction($action){
28 parent::beforeAction($action);
29
30 if (!isset($_GET['id']) && isset($_GET['id'])) {
31 $model=P3Widget::model()->find('id = :id', array(
32 ':id' => $_GET['id']));
33 if ($model !== null) {
34 $_GET['id'] = $model->id;
35 } else {
36 throw new CHttpException(400);
37 }
38 }
39 if ($this->module !== null) {
40 $this->breadcrumbs[$this->module->Id] = array('/'.$this->module->Id);
41 }
42 return true;
43 }
44
45 public function actionView($id)
46 {
47 $model = $this->loadModel($id);
48 $this->render('view',array('model' => $model,));
49 }
50
51 public function actionCreate()
52 {
53 $model = new P3Widget;
54 $model->scenario = $this->scenario;
55
56 $this->performAjaxValidation($model, 'p3-widget-form');
57
58 if (isset($_GET['autoCreate'])) {
59 $_POST['P3Widget'] = $_GET['P3Widget'];
60 }
61
62 if(isset($_POST['P3Widget'])) {
63 $model->attributes = $_POST['P3Widget'];
64
65 try {
66 if($model->save()) {
67 if (isset($_GET['returnUrl'])) {
68 $this->redirect($_GET['returnUrl']);
69 } else {
70 $this->redirect(array('view','id'=>$model->id));
71 }
72 }
73 } catch (Exception $e) {
74 $model->addError('id', $e->getMessage());
75 }
76 } elseif(isset($_GET['P3Widget'])) {
77 $model->attributes = $_GET['P3Widget'];
78 }
79
80 $this->render('create',array( 'model'=>$model));
81 }
82
83
84 public function actionUpdate($id)
85 {
86 $model = $this->loadModel($id);
87 $model->scenario = $this->scenario;
88
89 $this->performAjaxValidation($model, 'p3-widget-form');
90
91 if(isset($_POST['P3Widget']))
92 {
93 $model->attributes = $_POST['P3Widget'];
94
95
96 try {
97 if($model->save()) {
98 if (Yii::app()->request->isAjaxRequest) {
99 return;
100 }
101 if (isset($_GET['returnUrl'])) {
102 $this->redirect($_GET['returnUrl']);
103 } else {
104 $this->redirect(array('view','id'=>$model->id));
105 }
106 }
107 } catch (Exception $e) {
108 $model->addError('id', $e->getMessage());
109 }
110 }
111
112 $this->render('update',array('model'=>$model,));
113 }
114
115 public function actionEditableSaver()
116 {
117 Yii::import('EditableSaver');
118 $es = new EditableSaver('P3Widget');
119 $es->update();
120 }
121
122 public function actionDelete($id)
123 {
124 if(Yii::app()->request->isPostRequest)
125 {
126 try {
127 $this->loadModel($id)->delete();
128 } catch (Exception $e) {
129 throw new CHttpException(500,$e->getMessage());
130 }
131
132 if(!isset($_GET['ajax']))
133 {
134 if (isset($_GET['returnUrl'])) {
135 $this->redirect($_GET['returnUrl']);
136 } else {
137 $this->redirect(array('admin'));
138 }
139 }
140 }
141 else
142 throw new CHttpException(400,Yii::t('P3WidgetsModule.crud', 'Invalid request. Please do not repeat this request again.'));
143 }
144
145 public function actionIndex()
146 {
147 $dataProvider=new CActiveDataProvider('P3Widget');
148 $this->render('index',array('dataProvider'=>$dataProvider,));
149 }
150
151 public function actionAdmin()
152 {
153 $model=new P3Widget('search');
154 $model->unsetAttributes();
155
156 if(isset($_GET['P3Widget'])) {
157 $model->attributes = $_GET['P3Widget'];
158 }
159
160 $this->render('admin',array('model'=>$model,));
161 }
162
163 public function loadModel($id)
164 {
165 $model=P3Widget::model()->findByPk($id);
166 if($model===null)
167 throw new CHttpException(404,Yii::t('P3WidgetsModule.crud', 'The requested page does not exist.'));
168 return $model;
169 }
170
171 protected function performAjaxValidation($model)
172 {
173 if(isset($_POST['ajax']) && $_POST['ajax']==='p3-widget-form')
174 {
175 echo CActiveForm::validate($model);
176 Yii::app()->end();
177 }
178 }
179
180 181 182 183 184
185 public function actionClassVars($alias) {
186 $class = Yii::createComponent($alias);
187 $array = get_class_vars(get_class($class));
188 $return = array_map(array($this, '_replaceJSON'), $array);
189 echo CJSON::encode($return);
190 }
191
192 193 194 195 196 197
198 private function _replaceJSON($input) {
199 if (is_array($input)) {
200 return array_map(array($this, '_replaceJSON'), $input);
201 } elseif ($input === null)
202 return "NULL";
203 elseif ($input === false) {
204 return "0";
205 } elseif ($input === true) {
206 return "1";
207 } else {
208 return $input;
209 }
210 }
211
212 213 214 215 216
217 public function actionUpdateOrder() {
218 if (!isset($_POST['widget'])) {
219 echo "No data.";
220 return;
221 }
222 $updateRecordsArray = $_POST['widget'];
223 $listingCounter = 10;
224 foreach ($updateRecordsArray as $id) {
225 $model = $this->loadModel($id);
226 $model->rank = $listingCounter;
227 $model->save(false);
228 $listingCounter = $listingCounter + 10;
229 }
230 echo "Updated widget order successfully.";
231 }
232 }
233