1 <?php
2
3 class P3PageController 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', 'createChild','editableSaver','update','delete','admin','view'),
19 'roles'=>array('P3pages.P3Page.*'),
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=P3Page::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 actionCreateChild() {
52 $model = new P3Page;
53 $model->save();
54 $model->p3PageMeta->treeParent_id = $_GET['P3PageMeta']['treeParent_id'];
55 $model->p3PageMeta->save();
56
57 $this->redirect(array('update','id'=>$model->id,'returnUrl'=>$_GET['returnUrl']));
58
59 }
60
61
62 public function actionCreate()
63 {
64 $model = new P3Page;
65 $model->scenario = $this->scenario;
66
67 $this->performAjaxValidation($model, 'p3-page-form');
68
69 if(isset($_POST['P3Page'])) {
70 $model->attributes = $_POST['P3Page'];
71
72 try {
73 if($model->save()) {
74 if (isset($_GET['returnUrl'])) {
75 $this->redirect($_GET['returnUrl']);
76 } else {
77 $this->redirect(array('view','id'=>$model->id));
78 }
79 }
80 } catch (Exception $e) {
81 $model->addError('id', $e->getMessage());
82 }
83 } elseif(isset($_GET['P3Page'])) {
84 $model->attributes = $_GET['P3Page'];
85 }
86
87 $this->render('create',array( 'model'=>$model));
88 }
89
90
91 public function actionUpdate($id)
92 {
93 $model = $this->loadModel($id);
94 $model->scenario = $this->scenario;
95
96 $this->performAjaxValidation($model, 'p3-page-form');
97
98 if(isset($_POST['P3Page']))
99 {
100 $model->attributes = $_POST['P3Page'];
101
102
103 try {
104 if($model->save()) {
105 if (isset($_GET['returnUrl'])) {
106 $this->redirect($_GET['returnUrl']);
107 } else {
108 $this->redirect(array('view','id'=>$model->id));
109 }
110 }
111 } catch (Exception $e) {
112 $model->addError('id', $e->getMessage());
113 }
114 }
115
116 $this->render('update',array('model'=>$model,));
117 }
118
119 public function actionEditableSaver()
120 {
121 Yii::import('EditableSaver');
122 $es = new EditableSaver('P3Page');
123 $es->update();
124 }
125
126 public function actionDelete($id)
127 {
128 if(Yii::app()->request->isPostRequest)
129 {
130 try {
131 $this->loadModel($id)->delete();
132 } catch (Exception $e) {
133 throw new CHttpException(500,$e->getMessage());
134 }
135
136 if(!isset($_GET['ajax']))
137 {
138 if (isset($_GET['returnUrl'])) {
139 $this->redirect($_GET['returnUrl']);
140 } else {
141 $this->redirect(array('admin'));
142 }
143 }
144 }
145 else
146 throw new CHttpException(400,Yii::t('P3PagesModule.crud', 'Invalid request. Please do not repeat this request again.'));
147 }
148
149 public function actionIndex()
150 {
151 $dataProvider=new CActiveDataProvider('P3Page');
152 $this->render('index',array('dataProvider'=>$dataProvider,));
153 }
154
155 public function actionAdmin()
156 {
157 $model=new P3Page('search');
158 $model->unsetAttributes();
159
160 if(isset($_GET['P3Page'])) {
161 $model->attributes = $_GET['P3Page'];
162 }
163
164 $this->render('admin',array('model'=>$model,));
165 }
166
167 public function loadModel($id)
168 {
169 $model=P3Page::model()->findByPk($id);
170 if($model===null)
171 throw new CHttpException(404,Yii::t('P3PagesModule.crud', 'The requested page does not exist.'));
172 return $model;
173 }
174
175 protected function performAjaxValidation($model)
176 {
177 if(isset($_POST['ajax']) && $_POST['ajax']==='p3-page-form')
178 {
179 echo CActiveForm::validate($model);
180 Yii::app()->end();
181 }
182 }
183 }
184