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