Skip to content

Commit 824aa02

Browse files
author
Igor Chepurnoy
committed
init
1 parent 7fbf2be commit 824aa02

File tree

8 files changed

+194
-2
lines changed

8 files changed

+194
-2
lines changed

Bootstrap.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,19 @@ public function bootstrap($app)
2626
if ($app->getModule('comment') === null) {
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Module extends \yii\base\Module
2626
'plugins' => ['video', 'fullscreen', 'table'],
2727
'options' => [
2828
'minHeight' => 200,
29+
'imageUpload' => '/cms/manage/file-upload',
30+
'fileUpload' => '/cms/manage/file-upload',
2931
],
3032
];
3133

Widget.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace yii2mod\cms;
4+
5+
use Yii;
6+
use yii\web\JsExpression;
7+
8+
/**
9+
* Class Widget
10+
*
11+
* @package yii2mod\cms
12+
*/
13+
class Widget extends \yii\imperavi\Widget
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->options['uploadImageFields'][$request->csrfParam] = $request->getCsrfToken();
26+
$this->options['uploadFileFields'][$request->csrfParam] = $request->getCsrfToken();
27+
}
28+
}
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function run()
34+
{
35+
parent::run();
36+
37+
$this->registerDefaultCallbacks();
38+
}
39+
40+
/**
41+
* Register default callbacks.
42+
*/
43+
protected function registerDefaultCallbacks()
44+
{
45+
if (isset($this->options['imageUpload']) && !isset($this->options['imageUploadErrorCallback'])) {
46+
$this->options['imageUploadErrorCallback'] = new JsExpression('function (response) { alert(response.error); }');
47+
}
48+
49+
if (isset($this->options['fileUpload']) && !isset($this->options['fileUploadErrorCallback'])) {
50+
$this->options['fileUploadErrorCallback'] = new JsExpression('function (response) { alert(response.error); }');
51+
}
52+
}
53+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"yii2mod/yii2-comments": "*",
2727
"yii2mod/yii2-markdown": "*",
2828
"asofter/yii2-imperavi-redactor": "*",
29-
"cebe/markdown": "~1.1.1"
29+
"cebe/markdown": "~1.1.1",
30+
"yii2tech/ar-file": "*"
3031
},
3132
"require-dev": {
3233
"friendsofphp/php-cs-fixer": "~2.0"

controllers/ManageController.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use yii\filters\VerbFilter;
77
use yii\web\Controller;
88
use yii\web\NotFoundHttpException;
9+
use yii\web\UploadedFile;
910
use yii2mod\cms\models\CmsModel;
1011
use yii2mod\editable\EditableAction;
1112
use yii2mod\rbac\filters\AccessControl;
@@ -42,6 +43,11 @@ class ManageController extends Controller
4243
*/
4344
public $modelClass = 'yii2mod\cms\models\CmsModel';
4445

46+
/**
47+
* @var string model class name for attachment model
48+
*/
49+
public $attachmentModelClass = 'yii2mod\cms\models\AttachmentModel';
50+
4551
/**
4652
* @inheritdoc
4753
*/
@@ -55,6 +61,7 @@ public function behaviors()
5561
'create' => ['get', 'post'],
5662
'update' => ['get', 'post'],
5763
'delete' => ['post'],
64+
'image-upload' => ['post'],
5865
],
5966
],
6067
'access' => [
@@ -161,6 +168,28 @@ public function actionDelete($id)
161168
return $this->redirect(['index']);
162169
}
163170

171+
/**
172+
* @return \yii\web\Response
173+
*/
174+
public function actionFileUpload()
175+
{
176+
$model = Yii::createObject($this->attachmentModelClass);
177+
$model->file = UploadedFile::getInstanceByName('file');
178+
179+
if ($model->save()) {
180+
$result = [
181+
'filelink' => $model->getFileUrl(),
182+
'filename' => $model->getFileSelfName(),
183+
];
184+
} else {
185+
$result = [
186+
'error' => $model->getFirstError('file'),
187+
];
188+
}
189+
190+
return $this->asJson($result);
191+
}
192+
164193
/**
165194
* Finds the CmsModel model based on its primary key value.
166195
* If the model is not found, a 404 HTTP exception will be thrown.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use yii\db\Migration;
4+
5+
/**
6+
* Handles the creation of table `attachment`.
7+
* Has foreign keys to the tables:
8+
*
9+
* - `cms`
10+
*/
11+
class m170419_224001_create_attachment_table extends Migration
12+
{
13+
/**
14+
* @inheritdoc
15+
*/
16+
public function up()
17+
{
18+
$this->createTable('{{%attachment}}', [
19+
'id' => $this->primaryKey(),
20+
'file_extension' => $this->string(),
21+
'file_version' => $this->integer(),
22+
]);
23+
}
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
public function down()
29+
{
30+
$this->dropTable('{{%attachment}}');
31+
}
32+
}

models/AttachmentModel.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace yii2mod\cms\models;
4+
5+
use Yii;
6+
use yii\db\ActiveRecord;
7+
use yii2tech\ar\file\FileBehavior;
8+
9+
/**
10+
* This is the model class for table "{{%attachment}}".
11+
*
12+
* @property int $id
13+
* @property string $file_extension
14+
* @property int $file_version
15+
*/
16+
class AttachmentModel extends ActiveRecord
17+
{
18+
/**
19+
* @inheritdoc
20+
*/
21+
public static function tableName()
22+
{
23+
return '{{%attachment}}';
24+
}
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
public function rules()
30+
{
31+
return [
32+
[['file_version'], 'integer'],
33+
[['file_extension'], 'string', 'max' => 255],
34+
['file', 'file', 'skipOnEmpty' => false],
35+
];
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function attributeLabels()
42+
{
43+
return [
44+
'id' => Yii::t('yii2mod.cms', 'ID'),
45+
'file_extension' => Yii::t('yii2mod.cms', 'File Extension'),
46+
'file_version' => Yii::t('yii2mod.cms', 'File Version'),
47+
];
48+
}
49+
50+
public function behaviors()
51+
{
52+
return [
53+
'file' => [
54+
'class' => FileBehavior::class,
55+
'fileStorageBucket' => 'attachment',
56+
'fileExtensionAttribute' => 'file_extension',
57+
'fileVersionAttribute' => 'file_version',
58+
],
59+
];
60+
}
61+
}

views/cms/_form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use yii\bootstrap\ActiveForm;
44
use yii\helpers\Html;
5-
use yii\imperavi\Widget;
65
use yii2mod\cms\models\enumerables\CmsStatus;
6+
use yii2mod\cms\Widget;
77
use yii2mod\markdown\MarkdownEditor;
88

99
/* @var $this yii\web\View */

0 commit comments

Comments
 (0)