33namespace yii2mod \cms \controllers ;
44
55use Yii ;
6- use yii \filters \ VerbFilter ;
6+ use yii \db \ ActiveRecord ;
77use yii \web \Controller ;
88use yii \web \NotFoundHttpException ;
9+ use yii \web \UnprocessableEntityHttpException ;
10+ use yii \web \UploadedFile ;
11+ use yii2mod \cms \models \AttachmentModel ;
912use yii2mod \cms \models \CmsModel ;
1013use yii2mod \editable \EditableAction ;
1114
@@ -41,6 +44,27 @@ class ManageController extends Controller
4144 */
4245 public $ modelClass = 'yii2mod\cms\models\CmsModel ' ;
4346
47+ /**
48+ * @var string class name for attachment model
49+ */
50+ public $ attachmentModelClass = 'yii2mod\cms\models\AttachmentModel ' ;
51+
52+ /**
53+ * @var array verb filter config
54+ */
55+ public $ verbFilterConfig = [
56+ 'class ' => 'yii\filters\VerbFilter ' ,
57+ 'actions ' => [
58+ 'index ' => ['get ' ],
59+ 'create ' => ['get ' , 'post ' ],
60+ 'update ' => ['get ' , 'post ' ],
61+ 'delete ' => ['post ' ],
62+ 'upload-image ' => ['post ' ],
63+ 'delete-image ' => ['post ' ],
64+ 'images ' => ['get ' ],
65+ ],
66+ ];
67+
4468 /**
4569 * @var array access control config
4670 */
@@ -60,15 +84,7 @@ class ManageController extends Controller
6084 public function behaviors ()
6185 {
6286 return [
63- 'verbs ' => [
64- 'class ' => VerbFilter::class,
65- 'actions ' => [
66- 'index ' => ['get ' ],
67- 'create ' => ['get ' , 'post ' ],
68- 'update ' => ['get ' , 'post ' ],
69- 'delete ' => ['post ' ],
70- ],
71- ],
87+ 'verbs ' => $ this ->verbFilterConfig ,
7288 'access ' => $ this ->accessControlConfig ,
7389 ];
7490 }
@@ -87,7 +103,7 @@ public function actions()
87103 }
88104
89105 /**
90- * Lists all CmsModel models.
106+ * List of all cms models.
91107 *
92108 * @return mixed
93109 */
@@ -103,7 +119,7 @@ public function actionIndex()
103119 }
104120
105121 /**
106- * Creates a new CmsModel model .
122+ * Creates a new CmsModel.
107123 *
108124 * If creation is successful, the browser will be redirected to the 'index' page.
109125 *
@@ -125,7 +141,7 @@ public function actionCreate()
125141 }
126142
127143 /**
128- * Updates an existing CmsModel model .
144+ * Updates an existing CmsModel.
129145 *
130146 * If update is successful, the browser will be redirected to the 'index' page.
131147 *
@@ -135,7 +151,7 @@ public function actionCreate()
135151 */
136152 public function actionUpdate ($ id )
137153 {
138- $ model = $ this ->findModel ($ id );
154+ $ model = $ this ->findModel ($ this -> modelClass , $ id );
139155
140156 if ($ model ->load (Yii::$ app ->request ->post ()) && $ model ->save ()) {
141157 Yii::$ app ->session ->setFlash ('success ' , Yii::t ('yii2mod.cms ' , 'Page has been updated. ' ));
@@ -149,7 +165,7 @@ public function actionUpdate($id)
149165 }
150166
151167 /**
152- * Deletes an existing CmsModel model .
168+ * Deletes an existing CmsModel.
153169 *
154170 * If deletion is successful, the browser will be redirected to the 'index' page.
155171 *
@@ -159,27 +175,85 @@ public function actionUpdate($id)
159175 */
160176 public function actionDelete ($ id )
161177 {
162- $ this ->findModel ($ id )->delete ();
178+ $ this ->findModel ($ this -> modelClass , $ id )->delete ();
163179 Yii::$ app ->session ->setFlash ('success ' , Yii::t ('yii2mod.cms ' , 'Page has been deleted. ' ));
164180
165181 return $ this ->redirect (['index ' ]);
166182 }
167183
168184 /**
169- * Finds the CmsModel model based on its primary key value.
185+ * Upload an image
186+ *
187+ * @return \yii\web\Response
188+ *
189+ * @throws UnprocessableEntityHttpException
190+ */
191+ public function actionUploadImage ()
192+ {
193+ $ model = Yii::createObject ($ this ->attachmentModelClass );
194+ $ model ->file = UploadedFile::getInstanceByName ('file ' );
195+
196+ if (!$ model ->save ()) {
197+ throw new UnprocessableEntityHttpException ($ model ->getFirstError ('file ' ));
198+ }
199+
200+ return $ this ->asJson ([
201+ 'link ' => $ model ->getFileUrl ('origin ' ),
202+ ]);
203+ }
204+
205+ /**
206+ * Delete the image
207+ *
208+ * @return \yii\web\Response
209+ *
210+ * @throws UnprocessableEntityHttpException
211+ */
212+ public function actionDeleteImage ()
213+ {
214+ $ model = $ this ->findModel ($ this ->attachmentModelClass , Yii::$ app ->request ->post ('id ' ));
215+
216+ $ model ->delete ();
217+
218+ return $ this ->asJson ([
219+ 'status ' => 'success ' ,
220+ ]);
221+ }
222+
223+ /**
224+ * Return list of all images
225+ *
226+ * @return \yii\web\Response
227+ */
228+ public function actionImages ()
229+ {
230+ $ result = [];
231+
232+ foreach (AttachmentModel::find ()->each () as $ attachment ) {
233+ $ result [] = [
234+ 'id ' => $ attachment ->id ,
235+ 'url ' => $ attachment ->getFileUrl ('origin ' ),
236+ 'thumb ' => $ attachment ->getFileUrl ('thumbnail ' ),
237+ ];
238+ }
239+
240+ return $ this ->asJson ($ result );
241+ }
242+
243+ /**
244+ * Finds the model based on its primary key value.
170245 * If the model is not found, a 404 HTTP exception will be thrown.
171246 *
172- * @param int $id
247+ * @param string|ActiveRecord $modelClass
248+ * @param $condition
173249 *
174- * @return CmsModel the loaded model
250+ * @return ActiveRecord the loaded model
175251 *
176252 * @throws NotFoundHttpException if the model cannot be found
177253 */
178- protected function findModel ($ id )
254+ protected function findModel ($ modelClass , $ condition )
179255 {
180- $ cmsModel = $ this ->modelClass ;
181-
182- if (($ model = $ cmsModel ::findOne ($ id )) !== null ) {
256+ if (($ model = $ modelClass ::findOne ($ condition )) !== null ) {
183257 return $ model ;
184258 } else {
185259 throw new NotFoundHttpException (Yii::t ('yii2mod.cms ' , 'The requested page does not exist. ' ));
0 commit comments