33namespace yii2mod \cms \controllers ;
44
55use Yii ;
6+ use yii \db \ActiveRecord ;
67use yii \web \Controller ;
78use yii \web \NotFoundHttpException ;
89use yii \web \UnprocessableEntityHttpException ;
@@ -58,7 +59,8 @@ class ManageController extends Controller
5859 'create ' => ['get ' , 'post ' ],
5960 'update ' => ['get ' , 'post ' ],
6061 'delete ' => ['post ' ],
61- 'image-upload ' => ['post ' ],
62+ 'upload-image ' => ['post ' ],
63+ 'delete-image ' => ['post ' ],
6264 'images ' => ['get ' ],
6365 ],
6466 ];
@@ -149,7 +151,7 @@ public function actionCreate()
149151 */
150152 public function actionUpdate ($ id )
151153 {
152- $ model = $ this ->findModel ($ id );
154+ $ model = $ this ->findModel ($ this -> modelClass , $ id );
153155
154156 if ($ model ->load (Yii::$ app ->request ->post ()) && $ model ->save ()) {
155157 Yii::$ app ->session ->setFlash ('success ' , Yii::t ('yii2mod.cms ' , 'Page has been updated. ' ));
@@ -173,7 +175,7 @@ public function actionUpdate($id)
173175 */
174176 public function actionDelete ($ id )
175177 {
176- $ this ->findModel ($ id )->delete ();
178+ $ this ->findModel ($ this -> modelClass , $ id )->delete ();
177179 Yii::$ app ->session ->setFlash ('success ' , Yii::t ('yii2mod.cms ' , 'Page has been deleted. ' ));
178180
179181 return $ this ->redirect (['index ' ]);
@@ -186,7 +188,7 @@ public function actionDelete($id)
186188 *
187189 * @throws UnprocessableEntityHttpException
188190 */
189- public function actionImageUpload ()
191+ public function actionUploadImage ()
190192 {
191193 $ model = Yii::createObject ($ this ->attachmentModelClass );
192194 $ model ->file = UploadedFile::getInstanceByName ('file ' );
@@ -200,6 +202,24 @@ public function actionImageUpload()
200202 ]);
201203 }
202204
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+
203223 /**
204224 * Return list of all images
205225 *
@@ -211,6 +231,7 @@ public function actionImages()
211231
212232 foreach (AttachmentModel::find ()->each () as $ attachment ) {
213233 $ result [] = [
234+ 'id ' => $ attachment ->id ,
214235 'url ' => $ attachment ->getFileUrl ('origin ' ),
215236 'thumb ' => $ attachment ->getFileUrl ('thumbnail ' ),
216237 ];
@@ -220,20 +241,19 @@ public function actionImages()
220241 }
221242
222243 /**
223- * Finds the CmsModel based on its primary key value.
244+ * Finds the model based on its primary key value.
224245 * If the model is not found, a 404 HTTP exception will be thrown.
225246 *
226- * @param int $id
247+ * @param string|ActiveRecord $modelClass
248+ * @param $condition
227249 *
228- * @return CmsModel the loaded model
250+ * @return ActiveRecord the loaded model
229251 *
230252 * @throws NotFoundHttpException if the model cannot be found
231253 */
232- protected function findModel ($ id )
254+ protected function findModel ($ modelClass , $ condition )
233255 {
234- $ cmsModel = $ this ->modelClass ;
235-
236- if (($ model = $ cmsModel ::findOne ($ id )) !== null ) {
256+ if (($ model = $ modelClass ::findOne ($ condition )) !== null ) {
237257 return $ model ;
238258 } else {
239259 throw new NotFoundHttpException (Yii::t ('yii2mod.cms ' , 'The requested page does not exist. ' ));
0 commit comments