Skip to content

Commit 154018b

Browse files
author
Igor Chepurnoy
committed
added delete image action
1 parent 4971983 commit 154018b

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

Module.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ class Module extends \yii\base\Module
2828
'clientOptions' => [
2929
'heightMin' => 300,
3030
'theme' => 'gray',
31-
'imageUploadURL' => '/cms/manage/image-upload',
31+
'imageUploadURL' => '/cms/manage/upload-image',
32+
'imageManagerDeleteURL' => '/cms/manage/delete-image',
33+
'imageManagerDeleteMethod' => 'POST',
3234
'imageManagerLoadURL' => '/cms/manage/images',
3335
],
3436
'excludedPlugins' => [

Widget.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function init()
2323

2424
if ($request->enableCsrfValidation) {
2525
$this->clientOptions['imageUploadParams'][$request->csrfParam] = $request->getCsrfToken();
26+
$this->clientOptions['imageManagerDeleteParams'][$request->csrfParam] = $request->getCsrfToken();
2627
}
2728
}
2829
}

controllers/ManageController.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace yii2mod\cms\controllers;
44

55
use Yii;
6+
use yii\db\ActiveRecord;
67
use yii\web\Controller;
78
use yii\web\NotFoundHttpException;
89
use 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.'));

messages/en/yii2mod.cms.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@
4848
'Page has been updated.' => 'Page has been updated.',
4949
'Page has been deleted.' => 'Page has been deleted.',
5050
'The requested page does not exist.' => 'The requested page does not exist.',
51+
'File Extension' => 'File Extension',
52+
'File Version' => 'File Version',
5153
];

messages/ru/yii2mod.cms.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@
4848
'Page has been updated.' => 'Страница был обновлена.',
4949
'Page has been deleted.' => 'Страница был удалена.',
5050
'The requested page does not exist.' => 'Ошибка 404 - страница не найдена!',
51+
'File Extension' => 'Расширение Файла',
52+
'File Version' => 'Версия Файла',
5153
];

0 commit comments

Comments
 (0)