Skip to content

Commit a89753f

Browse files
author
Igor Chepurnoy
authored
Merge pull request #16 from yii2mod/add_file_upload
Add files uploading
2 parents 3d1526a + 68cd177 commit a89753f

File tree

11 files changed

+267
-35
lines changed

11 files changed

+267
-35
lines changed

Bootstrap.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,22 @@ public function bootstrap($app)
2323
];
2424
}
2525

26-
if ($app->getModule('comment') === null) {
26+
if (!$app->hasModule('comment')) {
2727
$app->setModule('comment', ['class' => 'yii2mod\comments\Module']);
2828
}
29+
30+
if (!$app->has('fileStorage')) {
31+
$app->set('fileStorage', [
32+
'class' => 'yii2tech\filestorage\local\Storage',
33+
'basePath' => '@webroot/files',
34+
'baseUrl' => '@web/files',
35+
'filePermission' => 0777,
36+
'buckets' => [
37+
'item' => [
38+
'baseSubPath' => 'attachment',
39+
],
40+
],
41+
]);
42+
}
2943
}
3044
}

Module.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,22 @@ class Module extends \yii\base\Module
2020
public $defaultRoute = 'manage';
2121

2222
/**
23-
* @var array imperavi editor options
23+
* @var array froala editor options
24+
*
25+
* @see https://www.froala.com/wysiwyg-editor/docs
2426
*/
25-
public $imperaviEditorOptions = [
26-
'plugins' => ['video', 'fullscreen', 'table'],
27-
'options' => [
28-
'minHeight' => 200,
27+
public $froalaEditorOptions = [
28+
'clientOptions' => [
29+
'heightMin' => 300,
30+
'theme' => 'gray',
31+
'imageUploadURL' => '/cms/manage/upload-image',
32+
'imageManagerDeleteURL' => '/cms/manage/delete-image',
33+
'imageManagerDeleteMethod' => 'POST',
34+
'imageManagerLoadURL' => '/cms/manage/images',
35+
],
36+
'excludedPlugins' => [
37+
'file',
38+
'emoticons',
2939
],
3040
];
3141

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ This module provides a web interface for content management system and includes
44

55
- Allows CRUD operations for pages
66
- [Support Markdown Editor](https://github.com/yii2mod/yii2-markdown)
7+
- [Support Froala Editor](https://www.froala.com/wysiwyg-editor)
78
- [Support Comments Management System](https://github.com/yii2mod/yii2-comments)
89
- Integrated with [yii2mod/base](https://github.com/yii2mod/base)
910

Widget.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace yii2mod\cms;
4+
5+
use froala\froalaeditor\FroalaEditorWidget;
6+
use Yii;
7+
8+
/**
9+
* Class Widget
10+
*
11+
* @package yii2mod\cms
12+
*/
13+
class Widget extends FroalaEditorWidget
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function init()
19+
{
20+
parent::init();
21+
22+
$request = Yii::$app->getRequest();
23+
24+
if ($request->enableCsrfValidation) {
25+
$this->clientOptions['imageUploadParams'][$request->csrfParam] = $request->getCsrfToken();
26+
$this->clientOptions['imageManagerDeleteParams'][$request->csrfParam] = $request->getCsrfToken();
27+
}
28+
}
29+
}

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@
1515
},
1616
{
1717
"name": "Igor Chepurnoy",
18-
"email": "igorzfort@gmail.com"
18+
"email": "chepurnoi.igor@gmail.com"
1919
}
2020
],
2121
"require": {
2222
"php": ">=5.6",
2323
"yiisoft/yii2": "~2.0.11",
24+
"yiisoft/yii2-bootstrap": "*",
2425
"yii2mod/yii2-enum": "*",
2526
"yii2mod/yii2-editable": "*",
2627
"yii2mod/yii2-comments": "*",
2728
"yii2mod/yii2-markdown": "*",
28-
"asofter/yii2-imperavi-redactor": "*",
29-
"cebe/markdown": "~1.1.1"
29+
"froala/yii2-froala-editor": "dev-master",
30+
"cebe/markdown": "~1.1.1",
31+
"yii2tech/ar-file": "*",
32+
"yiisoft/yii2-imagine": "^2.1"
3033
},
3134
"require-dev": {
3235
"friendsofphp/php-cs-fixer": "~2.0"

controllers/ManageController.php

Lines changed: 97 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace yii2mod\cms\controllers;
44

55
use Yii;
6-
use yii\filters\VerbFilter;
6+
use yii\db\ActiveRecord;
77
use yii\web\Controller;
88
use yii\web\NotFoundHttpException;
9+
use yii\web\UnprocessableEntityHttpException;
10+
use yii\web\UploadedFile;
11+
use yii2mod\cms\models\AttachmentModel;
912
use yii2mod\cms\models\CmsModel;
1013
use 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.'));

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
];
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use yii\db\Migration;
4+
5+
/**
6+
* Handles the creation of table `attachment`.
7+
*/
8+
class m170419_224001_create_attachment_table extends Migration
9+
{
10+
/**
11+
* @inheritdoc
12+
*/
13+
public function up()
14+
{
15+
$this->createTable('{{%attachment}}', [
16+
'id' => $this->primaryKey(),
17+
'file_extension' => $this->string(),
18+
'file_version' => $this->integer(),
19+
]);
20+
}
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function down()
26+
{
27+
$this->dropTable('{{%attachment}}');
28+
}
29+
}

0 commit comments

Comments
 (0)