Commit e8e3ed482afcfbbae6a04d9060966f11692999eb
Merge remote-tracking branch 'origin/master'
Showing
6 changed files
with
65 additions
and
24 deletions
Show diff stats
backend/controllers/EventController.php
... | ... | @@ -9,6 +9,8 @@ use yii\web\Controller; |
9 | 9 | use yii\web\NotFoundHttpException; |
10 | 10 | use yii\filters\VerbFilter; |
11 | 11 | use developeruz\db_rbac\behaviors\AccessBehavior; |
12 | +use yii\web\UploadedFile; | |
13 | + | |
12 | 14 | /** |
13 | 15 | * EventController implements the CRUD actions for Event model. |
14 | 16 | */ |
... | ... | @@ -77,7 +79,16 @@ class EventController extends Controller |
77 | 79 | { |
78 | 80 | $model = new Event(); |
79 | 81 | |
80 | - if ($model->load(Yii::$app->request->post()) && $model->save()) { | |
82 | + if ($model->load(Yii::$app->request->post())) { | |
83 | + | |
84 | + if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { | |
85 | + $model->image = $image->name; | |
86 | + } | |
87 | + | |
88 | + if ($model->save() && $image) { | |
89 | + $image->saveAs(Yii::getAlias('@imagesDir/articles/' . $image->name)); | |
90 | + } | |
91 | + | |
81 | 92 | return $this->redirect(['view', 'id' => $model->event_id]); |
82 | 93 | } else { |
83 | 94 | return $this->render('create', [ |
... | ... | @@ -96,7 +107,16 @@ class EventController extends Controller |
96 | 107 | { |
97 | 108 | $model = $this->findModel($id); |
98 | 109 | |
99 | - if ($model->load(Yii::$app->request->post()) && $model->save()) { | |
110 | + if ($model->load(Yii::$app->request->post())) { | |
111 | + | |
112 | + if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { | |
113 | + $model->image = $image->name; | |
114 | + } | |
115 | + | |
116 | + if ($model->save() && $image) { | |
117 | + $image->saveAs(Yii::getAlias('@imagesDir/articles/' . $image->name)); | |
118 | + } | |
119 | + | |
100 | 120 | return $this->redirect(['view', 'id' => $model->event_id]); |
101 | 121 | } else { |
102 | 122 | return $this->render('update', [ | ... | ... |
backend/views/event/_form.php
... | ... | @@ -12,7 +12,10 @@ use mihaildev\elfinder\ElFinder; |
12 | 12 | |
13 | 13 | <div class="event-form"> |
14 | 14 | |
15 | - <?php $form = ActiveForm::begin(); ?> | |
15 | + <?php $form = ActiveForm::begin([ | |
16 | + 'enableClientValidation' => false, | |
17 | + 'options' => ['enctype' => 'multipart/form-data'] | |
18 | + ]); ?> | |
16 | 19 | |
17 | 20 | <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?> |
18 | 21 | |
... | ... | @@ -37,24 +40,21 @@ use mihaildev\elfinder\ElFinder; |
37 | 40 | ]]) ?> |
38 | 41 | |
39 | 42 | |
40 | - <?= \common\modules\file\widgets\ImageUploader::widget([ | |
41 | - 'model'=> $model, | |
42 | - 'field'=>'image', | |
43 | - 'size' => [ | |
44 | - [ | |
45 | - 'width'=>200, | |
46 | - 'height'=>200, | |
47 | - ], | |
48 | - [ | |
49 | - 'width'=>940, | |
50 | - 'height'=>480, | |
51 | - ] | |
43 | + <?= $form->field($model, 'imageUpload')->widget(\kartik\file\FileInput::classname(), [ | |
44 | + 'language' => 'ru', | |
45 | + 'options' => [ | |
46 | + 'accept' => 'image/*', | |
47 | + 'multiple' => false, | |
52 | 48 | ], |
53 | - 'multi'=>false, | |
54 | - 'gallery' => $model->image, | |
55 | - 'name' => 'ะะฐะณััะทะธัั ะธะทะพะฑัะฐะถะตะฝะธะต' | |
56 | - ]); | |
57 | - ?> | |
49 | + 'pluginOptions' => [ | |
50 | + 'allowedFileExtensions' => ['jpg', 'gif', 'png'], | |
51 | + 'initialPreview' => !empty($model->imageUrl) ? \common\components\artboximage\ArtboxImageHelper::getImage($model->imageUrl, 'list') : '', | |
52 | + 'overwriteInitial' => true, | |
53 | + 'showRemove' => false, | |
54 | + 'showUpload' => false, | |
55 | + 'previewFileType' => 'image', | |
56 | + ], | |
57 | + ]); ?> | |
58 | 58 | |
59 | 59 | <?= $form->field($model, 'meta_title')->textInput(['maxlength' => true]) ?> |
60 | 60 | ... | ... |
backend/views/event/view.php
... | ... | @@ -33,8 +33,11 @@ $this->params['breadcrumbs'][] = $this->title; |
33 | 33 | 'alias', |
34 | 34 | 'body:ntext', |
35 | 35 | [ |
36 | - 'format' => 'image', | |
37 | - 'attribute'=>'image', | |
36 | + 'format' => 'html', | |
37 | + 'attribute' => 'imageUrl', | |
38 | + 'value' => function($data) { | |
39 | + return \common\components\artboximage\ArtboxImageHelper::getImage($data->imageUrl, 'list'); | |
40 | + }, | |
38 | 41 | ], |
39 | 42 | 'meta_title', |
40 | 43 | 'description', | ... | ... |
common/components/artboximage/ArtboxImageHelper.php
... | ... | @@ -86,7 +86,10 @@ class ArtboxImageHelper extends Object { |
86 | 86 | foreach ($preset as $action => $data) { |
87 | 87 | switch($action) { |
88 | 88 | case 'resize': |
89 | - $image->resize($data['width'], $data['height'], @$data['master']); | |
89 | + $width = empty($data['width']) ? null : $data['width']; | |
90 | + $height = empty($data['height']) ? null : $data['height']; | |
91 | + $master = empty($data['master']) ? null : $data['master']; | |
92 | + $image->resize($width, $height, $master); | |
90 | 93 | break; |
91 | 94 | case 'flip': |
92 | 95 | $image->flip(@$data['direction']); | ... | ... |
common/models/Event.php
... | ... | @@ -22,6 +22,7 @@ use yii\behaviors\TimestampBehavior; |
22 | 22 | */ |
23 | 23 | class Event extends \yii\db\ActiveRecord |
24 | 24 | { |
25 | + public $imageUpload; | |
25 | 26 | |
26 | 27 | /** |
27 | 28 | * @inheritdoc |
... | ... | @@ -76,6 +77,8 @@ class Event extends \yii\db\ActiveRecord |
76 | 77 | [['created_at', 'updated_at' ], 'integer'], |
77 | 78 | [['name', 'alias', 'image', 'meta_title', 'description', 'h1','end_at'], 'string', 'max' => 255], |
78 | 79 | [['name','body'], 'required'], |
80 | + [['imageUpload'], 'safe'], | |
81 | + [['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'], | |
79 | 82 | ]; |
80 | 83 | } |
81 | 84 | |
... | ... | @@ -99,4 +102,14 @@ class Event extends \yii\db\ActiveRecord |
99 | 102 | 'end_at' => Yii::t('app', 'end_at'), |
100 | 103 | ]; |
101 | 104 | } |
105 | + | |
106 | + | |
107 | + public function getImageFile() { | |
108 | + return empty($this->image) ? null : Yii::getAlias('@imagesDir/articles/'. $this->image); | |
109 | + } | |
110 | + | |
111 | + public function getImageUrl() | |
112 | + { | |
113 | + return empty($this->image) ? null : Yii::getAlias('@imagesUrl/articles/' . $this->image); | |
114 | + } | |
102 | 115 | } | ... | ... |
frontend/views/event/_objects.php
... | ... | @@ -5,7 +5,9 @@ use yii\helpers\Url; |
5 | 5 | use frontend\components\Text; |
6 | 6 | ?> |
7 | 7 | <div class="news_item"> |
8 | - <a href="<?=Url::to(['event/show','alias'=>$model->alias,'id'=>$model->primaryKey])?>"><img src="<?=Yii::$app->request->baseUrl.'/storage/articles/ico/'.$model->image?>" width="180" height="125" border="0" align="left" /></a> | |
8 | + <a href="<?=Url::to(['event/show','alias'=>$model->alias,'id'=>$model->primaryKey])?>"> | |
9 | + <?= \common\components\artboximage\ArtboxImageHelper::getImage($model->imageUrl, 'eventlist', ['align' => 'left'])?> | |
10 | + </a> | |
9 | 11 | <a href="<?=Url::to(['event/show','alias'=>$model->alias,'id'=>$model->primaryKey])?>" class="name"><?=$model->name?></a><br /> |
10 | 12 | <?=Text::getShort($model->body,600);?><div class="both"></div> |
11 | 13 | </div> |
12 | 14 | \ No newline at end of file | ... | ... |