1 <?php
2
3 class P3MediaController extends Controller
4 {
5
6 public $defaultAction = "admin";
7 public $scenario = "crud";
8
9 public function filters()
10 {
11 return array(
12 'accessControl',
13 );
14 }
15
16 public function accessRules()
17 {
18 return array(
19 array('allow',
20 'actions' => array('create', 'update', 'delete', 'admin', 'view', 'ajaxUpdate', 'ajaxSearch'),
21 'roles' => array('P3media.P3Media.*'),
22 ),
23 array('deny',
24 'users' => array('*'),
25 ),
26 );
27 }
28
29 public function beforeAction($action)
30 {
31 parent::beforeAction($action);
32
33 if (!isset($_GET['id']) && isset($_GET['path'])) {
34 $model = P3Media::model()->find('path = :path', array(
35 ':path' => $_GET['path']));
36 if ($model !== null) {
37 $_GET['id'] = $model->id;
38 }
39 else {
40 throw new CHttpException(400);
41 }
42 }
43 if ($this->module !== null) {
44 $this->breadcrumbs[$this->module->Id] = array('/' . $this->module->Id);
45 }
46
47 return true;
48 }
49
50 public function actionView($id)
51 {
52 $model = $this->loadModel($id);
53 $this->render('view', array('model' => $model,));
54 }
55
56 public function actionCreate()
57 {
58 $model = new P3Media;
59 $model->scenario = $this->scenario;
60
61 $this->performAjaxValidation($model, 'p3-media-form');
62
63 if (isset($_POST['P3Media'])) {
64 $model->attributes = $_POST['P3Media'];
65
66 try {
67 if ($model->save()) {
68 if (isset($_GET['returnUrl'])) {
69 $this->redirect($_GET['returnUrl']);
70 }
71 else {
72 $this->redirect(array('view', 'id' => $model->id));
73 }
74 }
75 } catch (Exception $e) {
76 $model->addError('path', $e->getMessage());
77 }
78 }
79 elseif (isset($_GET['P3Media'])) {
80 $model->attributes = $_GET['P3Media'];
81 }
82
83 $this->render('create', array('model' => $model));
84 }
85
86
87 public function actionUpdate($id)
88 {
89 $model = $this->loadModel($id);
90 $model->scenario = $this->scenario;
91
92 $this->performAjaxValidation($model, 'p3-media-form');
93
94 if (isset($_POST['P3Media'])) {
95 $model->attributes = $_POST['P3Media'];
96
97
98 try {
99 if ($model->save()) {
100 if (isset($_GET['returnUrl'])) {
101 $this->redirect($_GET['returnUrl']);
102 }
103 else {
104 $this->redirect(array('view', 'id' => $model->id));
105 }
106 }
107 } catch (Exception $e) {
108 $model->addError('path', $e->getMessage());
109 }
110 }
111
112 $this->render('update', array('model' => $model,));
113 }
114
115 public function actionAjaxUpdate()
116 {
117 Yii::import('EditableSaver');
118 $es = new EditableSaver('P3Media');
119 $es->update();
120 }
121
122 public function actionDelete($id)
123 {
124 if (Yii::app()->request->isPostRequest) {
125 try {
126 $this->loadModel($id)->delete();
127 } catch (Exception $e) {
128 throw new CHttpException(500, $e->getMessage());
129 }
130
131 if (!isset($_GET['ajax'])) {
132 if (isset($_GET['returnUrl'])) {
133 $this->redirect($_GET['returnUrl']);
134 }
135 else {
136 $this->redirect(array('admin'));
137 }
138 }
139 }
140 else {
141 throw new CHttpException(400, Yii::t('app', 'Invalid request. Please do not repeat this request again.'));
142 }
143 }
144
145 public function actionIndex()
146 {
147 $dataProvider = new CActiveDataProvider('P3Media');
148 $this->render('index', array('dataProvider' => $dataProvider,));
149 }
150
151 public function actionAdmin()
152 {
153 $model = new P3Media('search');
154 $model->unsetAttributes();
155
156 if (isset($_GET['P3Media'])) {
157 $model->attributes = $_GET['P3Media'];
158 }
159
160 $this->render('admin', array('model' => $model,));
161 }
162
163 public function actionAjaxSearch()
164 {
165 $model = new P3Media('search');
166 $model->title = $_GET['q'];
167 $result = array();
168 foreach ($model->search()->getData() AS $data) {
169 $result[] = array(
170 'id' => $data->id,
171 'title' => $data->title,
172 'image' => $data->image('p3media-upload')
173 );
174 }
175 echo $_GET['callback'] . "(";
176 echo CJSON::encode($result);
177 echo ")";
178 }
179
180 public function loadModel($id)
181 {
182 $model = P3Media::model()->findByPk($id);
183 if ($model === null) {
184 throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.'));
185 }
186
187 return $model;
188 }
189
190 protected function performAjaxValidation($model)
191 {
192 if (isset($_POST['ajax']) && $_POST['ajax'] === 'p3-media-form') {
193 echo CActiveForm::validate($model);
194 Yii::app()->end();
195 }
196 }
197 }
198