Commit eadb88dff00c9914ecbdafceb51c2bffdaea011c
1 parent
85b72093
first commit from local
Showing
51 changed files
with
369 additions
and
45 deletions
Show diff stats
.gitignore
backend/components/FieldEditor/FieldEditor.php
100644 → 100755
backend/components/FieldEditor/views/phone_field.php
100644 → 100755
backend/components/FieldEditor/views/price_field.php
100644 → 100755
backend/components/FieldEditor/views/video_field.php
100644 → 100755
1 | +<?php | |
2 | +namespace backend\components; | |
3 | + | |
4 | +use yii\web\UploadedFile; | |
5 | +use backend\models\ImageSizerForm; | |
6 | +use yii\base\Widget; | |
7 | +use Imagine\Image\Box; | |
8 | +use yii\imagine\Image; | |
9 | +use Yii; | |
10 | +use Imagine\Gd\Imagine; | |
11 | + | |
12 | +class ImgResizer extends Widget | |
13 | +{ | |
14 | + | |
15 | + public $height; | |
16 | + public $width; | |
17 | + | |
18 | + public function init(){ | |
19 | + | |
20 | + parent::init(); | |
21 | + | |
22 | + } | |
23 | + | |
24 | + public function run(){ | |
25 | + | |
26 | + $model = new ImageSizerForm(); | |
27 | + | |
28 | + $request = Yii::$app->request->post(); | |
29 | + | |
30 | + if ($request) { | |
31 | + | |
32 | + if(isset($request['old_img'])){ | |
33 | + $this->deleteImages($request['old_img']); | |
34 | + } | |
35 | + | |
36 | + | |
37 | + $model->file = UploadedFile::getInstance($model, 'file'); | |
38 | + | |
39 | + $md5_file = md5_file($model->file->tempName); | |
40 | + | |
41 | + $imgDir = Yii::getAlias('@storage/'.$md5_file.'/'); | |
42 | + | |
43 | + $imageOrigAlias = Yii::getAlias($imgDir.'original'.'.'.$model->file->extension); | |
44 | + | |
45 | + if(!is_dir($imgDir)) { | |
46 | + mkdir($imgDir, 0755, true); | |
47 | + } | |
48 | + | |
49 | + $model->file->saveAs($imageOrigAlias); | |
50 | + | |
51 | + if($request['width'] && $request['height']){ | |
52 | + | |
53 | + $imageAlias = Yii::getAlias($imgDir.$request['width'].'x'.$request['height'].'.'.$model->file->extension); | |
54 | + | |
55 | + $imageLink = '/storage/'.$md5_file.'/'.$request['width'].'x'.$request['height'].'.'.$model->file->extension; | |
56 | + | |
57 | + $this->resizeImg($request['width'],$request['height'], $imageOrigAlias,$imageAlias); | |
58 | + | |
59 | + } else { | |
60 | + | |
61 | + $imageLink = '/storage/'.$md5_file.'/'.'original'.'.'.$model->file->extension; | |
62 | + | |
63 | + } | |
64 | + | |
65 | + | |
66 | + if($model->multi){ | |
67 | +// $view = $this->renderPartial('@app/components/views/_gallery_item', [ | |
68 | +// 'item' => ['image'=>$imageLink], | |
69 | +// ]); | |
70 | +// | |
71 | +// return json_encode(['link'=>$imageLink, 'view' =>$view]); | |
72 | + | |
73 | + | |
74 | + } else { | |
75 | + $p1[0] = "<img style='height:160px' src='$imageLink' class='file-preview-image'>"; | |
76 | + return json_encode(['success','initialPreview' => $p1, 'append' => false, 'name' =>$imageLink,]); | |
77 | + } | |
78 | + | |
79 | + | |
80 | + } else { | |
81 | + return json_encode(['error']); | |
82 | + } | |
83 | + | |
84 | + | |
85 | + | |
86 | + | |
87 | + } | |
88 | + | |
89 | + | |
90 | + public function isBigger($width,$height,$w,$h) | |
91 | + { | |
92 | + if($width>$w){ | |
93 | + return true; | |
94 | + }else if($height >$h) { | |
95 | + return true; | |
96 | + } | |
97 | + return false; | |
98 | + } | |
99 | + | |
100 | + | |
101 | + public function getImgSize($width,$height,$e){ | |
102 | + if($e>1){ | |
103 | + if($height>$width){ | |
104 | + $this->width = $width; | |
105 | + $this->height = $height/$e; | |
106 | + } else { | |
107 | + $this->height = $height; | |
108 | + $this->width= $width/$e; | |
109 | + } | |
110 | + | |
111 | + } else { | |
112 | + if($width >$height ){ | |
113 | + $this->height = $height; | |
114 | + $this->width = $width*$e; | |
115 | + } else { | |
116 | + $this->width = $width; | |
117 | + $this->height = $height*$e; | |
118 | + } | |
119 | + | |
120 | + } | |
121 | + } | |
122 | + | |
123 | + | |
124 | + | |
125 | + | |
126 | + public function resizeImg($w, $h, $imageAlias,$imageAliasSave) | |
127 | + { | |
128 | + $img = Image::getImagine()->open(Yii::getAlias($imageAlias)); | |
129 | + | |
130 | + $size = $img->getSize(); | |
131 | + | |
132 | + $width = $size->getWidth(); | |
133 | + $height = $size->getHeight(); | |
134 | + | |
135 | + $e = $w/$h; | |
136 | + | |
137 | + if($this->isBigger($width,$height,$w,$h)){ | |
138 | + if($height > $width) { | |
139 | + | |
140 | + $x = 0; | |
141 | + $y = ($height - $width) / 2; | |
142 | + $e1 = $width/$height; | |
143 | + | |
144 | + $height = $height*($e1/$e); | |
145 | + | |
146 | + }else if($height == $width){ | |
147 | + $x = 0; | |
148 | + $y = ($height - $width) / 2; | |
149 | + | |
150 | + | |
151 | + $height = $height/$e; | |
152 | + }else { | |
153 | + $y = 0; | |
154 | + $x = ($width - $height) / 2; | |
155 | + $e1 = $width/$height; | |
156 | + | |
157 | + $width = $width*($e/$e1); | |
158 | + } | |
159 | + } else { | |
160 | + $img->save($imageAliasSave, array('flatten' => false)); | |
161 | + return true; | |
162 | + } | |
163 | + | |
164 | + | |
165 | + | |
166 | + | |
167 | + | |
168 | + Image::crop($imageAlias, $width, $height,[$x,$y]) | |
169 | + ->save(Yii::getAlias($imageAliasSave), ['quality' => | |
170 | + 100]); | |
171 | + | |
172 | + | |
173 | + $imagine = new Imagine(); | |
174 | + $imagine->open($imageAliasSave) | |
175 | + ->resize(new Box($w, $h)) | |
176 | + ->save($imageAliasSave, array('flatten' => false)); | |
177 | + | |
178 | + | |
179 | + | |
180 | + } | |
181 | + | |
182 | + | |
183 | + private function deleteImages($old_img){ | |
184 | + | |
185 | + if(!empty($old_img) && file_exists($_SERVER['DOCUMENT_ROOT'].$old_img)){ | |
186 | + | |
187 | + $rootDir = explode("/", $old_img); | |
188 | + | |
189 | + $row = $_SERVER['DOCUMENT_ROOT'].'/'.$rootDir[1].'/'.$rootDir[2].'/'; | |
190 | + | |
191 | + $allFiles = scandir($row); | |
192 | + | |
193 | + $allFiles = array_slice($allFiles, 2); | |
194 | + | |
195 | + foreach($allFiles as $oldFile){ | |
196 | + | |
197 | + unlink($row.$oldFile); | |
198 | + | |
199 | + } | |
200 | + | |
201 | + } | |
202 | + } | |
203 | + | |
204 | + public function actionDeleteImage(){ | |
205 | + $old_img = Yii::$app->request->post('old_img'); | |
206 | + | |
207 | + if ($old_img) { | |
208 | + $this->deleteImages($old_img); | |
209 | + } | |
210 | + } | |
211 | + | |
212 | + | |
213 | + | |
214 | +} | |
0 | 215 | \ No newline at end of file | ... | ... |
backend/controllers/NewsController.php
... | ... | @@ -2,12 +2,16 @@ |
2 | 2 | |
3 | 3 | namespace backend\controllers; |
4 | 4 | |
5 | +use backend\models\ImageSizerForm; | |
6 | +use backend\models\UploadForm; | |
5 | 7 | use Yii; |
6 | 8 | use common\models\News; |
7 | 9 | use common\models\NewsSearch; |
8 | 10 | use yii\web\Controller; |
9 | 11 | use yii\web\NotFoundHttpException; |
10 | 12 | use yii\filters\VerbFilter; |
13 | +use yii\web\UploadedFile; | |
14 | +use backend\components\ImgResizer; | |
11 | 15 | |
12 | 16 | /** |
13 | 17 | * NewsController implements the CRUD actions for News model. |
... | ... | @@ -120,4 +124,11 @@ class NewsController extends Controller |
120 | 124 | throw new NotFoundHttpException('The requested page does not exist.'); |
121 | 125 | } |
122 | 126 | } |
127 | + | |
128 | + | |
129 | + public function actionFileUpload(){ | |
130 | + | |
131 | + echo ImgResizer::widget(); | |
132 | + | |
133 | + } | |
123 | 134 | } | ... | ... |
backend/models/ImageSizerForm.php
... | ... | @@ -20,6 +20,8 @@ class ImageSizerForm extends Model |
20 | 20 | public $form; |
21 | 21 | public $multi; |
22 | 22 | public $old_img; |
23 | + public $img; | |
24 | + public $price_list; | |
23 | 25 | |
24 | 26 | /** |
25 | 27 | * @return array the validation rules. |
... | ... | @@ -30,7 +32,7 @@ class ImageSizerForm extends Model |
30 | 32 | [['width', 'height'], 'integer'], |
31 | 33 | [['field', 'multi','old_img'], 'string', 'max' => 255], |
32 | 34 | [['model', 'form',], 'string'], |
33 | - [['file'], 'file'], | |
35 | + [['file','img','price_list'], 'file'], | |
34 | 36 | ]; |
35 | 37 | } |
36 | 38 | } |
37 | 39 | \ No newline at end of file | ... | ... |
1 | +<?php | |
2 | +namespace backend\models; | |
3 | + | |
4 | +use yii\base\Model; | |
5 | +use yii\web\UploadedFile; | |
6 | + | |
7 | +/** | |
8 | + * UploadForm is the model behind the upload form. | |
9 | + */ | |
10 | +class UploadForm extends Model | |
11 | +{ | |
12 | + /** | |
13 | + * @var UploadedFile file attribute | |
14 | + */ | |
15 | + public $file; | |
16 | + public $img; | |
17 | + | |
18 | + /** | |
19 | + * @return array the validation rules. | |
20 | + */ | |
21 | + public function rules() | |
22 | + { | |
23 | + return [ | |
24 | + [['file','img'], 'file'], | |
25 | + ]; | |
26 | + } | |
27 | +} | |
0 | 28 | \ No newline at end of file | ... | ... |
backend/views/accounts/_form.php
... | ... | @@ -75,33 +75,6 @@ use yii\helpers\ArrayHelper; |
75 | 75 | |
76 | 76 | |
77 | 77 | |
78 | - | |
79 | - | |
80 | - | |
81 | - | |
82 | - | |
83 | -<!-- --><?//= $form->field($model, 'country')->textInput() ?> | |
84 | -<!-- --> | |
85 | -<!-- --><?//= $form->field($model, 'rating')->textInput() ?> | |
86 | -<!-- --> | |
87 | -<!-- --><?//= $form->field($model, 'last_loginin')->textInput(['maxlength' => true]) ?> | |
88 | -<!-- --> | |
89 | -<!-- --><?//= $form->field($model, 'balance')->textInput() ?> | |
90 | -<!----> | |
91 | -<!-- --><?//= $form->field($model, 'office_id')->textInput() ?> | |
92 | -<!----> | |
93 | -<!-- --><?//= $form->field($model, 'car')->textInput() ?> | |
94 | -<!----> | |
95 | -<!-- --><?//= $form->field($model, 'mod')->textInput() ?> | |
96 | -<!----> | |
97 | -<!-- --><?//= $form->field($model, 'snumb')->textInput(['maxlength' => true]) ?> | |
98 | -<!----> | |
99 | -<!-- --><?//= $form->field($model, 'deliveries')->textInput() ?> | |
100 | - | |
101 | - | |
102 | - | |
103 | - | |
104 | - | |
105 | 78 | <div class="form-group"> |
106 | 79 | <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> |
107 | 80 | </div> | ... | ... |
backend/views/news/_form.php
... | ... | @@ -3,7 +3,8 @@ |
3 | 3 | use yii\helpers\Html; |
4 | 4 | use mihaildev\ckeditor\CKEditor; |
5 | 5 | use yii\widgets\ActiveForm; |
6 | - | |
6 | +use kartik\file\FileInput; | |
7 | +use \yii\helpers\Url; | |
7 | 8 | /* @var $this yii\web\View */ |
8 | 9 | /* @var $model common\models\News */ |
9 | 10 | /* @var $form yii\widgets\ActiveForm */ |
... | ... | @@ -49,9 +50,48 @@ use yii\widgets\ActiveForm; |
49 | 50 | |
50 | 51 | <?= $form->field($model, 'mails_count')->textInput() ?> |
51 | 52 | |
52 | - <?= $form->field($model, 'img')->textInput(['maxlength' => true]) ?> | |
53 | - <?= $form->field($model, 'img')->widget(FileInput::classname(), [ | |
54 | - 'options' => ['accept' => 'image/*'], | |
53 | + <?= Html::activeHiddenInput($model, 'img');?> | |
54 | + | |
55 | + <?= $form->field(new \backend\models\ImageSizerForm(), 'file')->widget(FileInput::classname(), [ | |
56 | + 'name' => 'file', | |
57 | + 'options' => [ | |
58 | + ['accept' => 'image/*'] | |
59 | + ], | |
60 | + 'pluginOptions' => [ | |
61 | + 'initialPreview'=>[ | |
62 | + Html::img($model->img, ['class'=>'file-preview-image']), | |
63 | + ], | |
64 | + 'uploadUrl' => Url::to(['/news/file-upload']), | |
65 | + 'overwriteInitial'=>true, | |
66 | + 'uploadExtraData'=> ['width' => 240, 'height' => 160] | |
67 | + | |
68 | + ], | |
69 | + 'pluginEvents' => [ | |
70 | + 'fileuploaded' => 'function(event, key) { document.getElementsByName("News[img]")[0].value = key.response.name }', | |
71 | + ] | |
72 | + | |
73 | + | |
74 | + ]);?> | |
75 | + | |
76 | + <?= $form->field(new \backend\models\ImageSizerForm(), 'price_list')->widget(FileInput::classname(), [ | |
77 | + 'name' => 'file', | |
78 | + 'options' => [ | |
79 | + ['accept' => 'image/*'] | |
80 | + ], | |
81 | + 'pluginOptions' => [ | |
82 | + 'initialPreview'=>[ | |
83 | + Html::img($model->price_list, ['class'=>'file-preview-image']), | |
84 | + ], | |
85 | + 'uploadUrl' => Url::to(['/news/file-upload']), | |
86 | + 'overwriteInitial'=>true, | |
87 | + 'uploadExtraData'=> ['width' => 240, 'height' => 160] | |
88 | + | |
89 | + ], | |
90 | + 'pluginEvents' => [ | |
91 | + 'fileuploaded' => 'function(event, key) { document.getElementsByName("News[img]")[0].value = key.response.name }', | |
92 | + ] | |
93 | + | |
94 | + | |
55 | 95 | ]); ?> |
56 | 96 | <div class="form-group"> |
57 | 97 | <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> | ... | ... |
common/models/News.php
... | ... | @@ -21,6 +21,7 @@ use Yii; |
21 | 21 | * @property integer $mail_send |
22 | 22 | * @property integer $mails_count |
23 | 23 | * @property string $img |
24 | + * @property string $price_list | |
24 | 25 | */ |
25 | 26 | class News extends \yii\db\ActiveRecord |
26 | 27 | { |
... | ... | @@ -42,8 +43,9 @@ class News extends \yii\db\ActiveRecord |
42 | 43 | return [ |
43 | 44 | [['name', 'code', 'brief', 'content', 'title', 'kwords', 'descr', 'dt', 'is_active', 'img'], 'required'], |
44 | 45 | [['brief', 'content'], 'string'], |
46 | + [['img'], 'file'], | |
45 | 47 | [['sort_delete', 'is_active', 'mail_send', 'mails_count'], 'integer'], |
46 | - [['name', 'code', 'title', 'kwords', 'descr', 'img'], 'string', 'max' => 254], | |
48 | + [['name', 'code', 'title', 'kwords', 'descr', 'img','price_list'], 'string', 'max' => 254], | |
47 | 49 | [['dt'], 'string', 'max' => 15], |
48 | 50 | [['code'], 'unique'] |
49 | 51 | ]; |
... | ... | @@ -54,6 +56,25 @@ class News extends \yii\db\ActiveRecord |
54 | 56 | |
55 | 57 | } |
56 | 58 | |
59 | + function minImg($dir, $width, $height=null){ | |
60 | + if($width=='original'){ | |
61 | + $preg = '/\/(.[^\/]*)$/'; | |
62 | + preg_match('/\.(.[^.]*)$/', $dir, $type); | |
63 | + $row = preg_replace( $preg, '/original.'.$type[1], $dir); | |
64 | + } else { | |
65 | + $preg = '/\/(.[^\/]*)$/'; | |
66 | + preg_match('/\.(.[^.]*)$/', $dir, $type); | |
67 | + $row = preg_replace( $preg, '/'.$width.'X'.$height.'.'.$type[1], $dir); | |
68 | + } | |
69 | + return $row; | |
70 | +// if(file_exists($_SERVER['DOCUMENT_ROOT'].$row)){ | |
71 | +// return $row; | |
72 | +// } else { | |
73 | +// return "/storage/no-image.png"; | |
74 | +// } | |
75 | + | |
76 | + } | |
77 | + | |
57 | 78 | /** |
58 | 79 | * @inheritdoc |
59 | 80 | */ |
... | ... | @@ -74,6 +95,7 @@ class News extends \yii\db\ActiveRecord |
74 | 95 | 'mail_send' => 'Mail Send', |
75 | 96 | 'mails_count' => 'Mails Count', |
76 | 97 | 'img' => 'Img', |
98 | + 'price_list' => 'price_list', | |
77 | 99 | ]; |
78 | 100 | } |
79 | 101 | } | ... | ... |
composer.json
... | ... | @@ -25,7 +25,8 @@ |
25 | 25 | "kartik-v/yii2-datecontrol": "dev-master", |
26 | 26 | "codeception/codeception": "*", |
27 | 27 | "2amigos/yii2-ckeditor-widget": "~1.0", |
28 | - "mihaildev/yii2-ckeditor": "*" | |
28 | + "mihaildev/yii2-ckeditor": "*", | |
29 | + "kartik-v/yii2-widget-fileinput": "@dev" | |
29 | 30 | }, |
30 | 31 | "require-dev": { |
31 | 32 | "yiisoft/yii2-codeception": "*", | ... | ... |
console/migrations/m151116_153942_add_field_to_news.php
0 → 100644
1 | +<?php | |
2 | + | |
3 | +use yii\db\Schema; | |
4 | +use yii\db\Migration; | |
5 | + | |
6 | +class m151116_153942_add_field_to_news extends Migration | |
7 | +{ | |
8 | + public function up() | |
9 | + { | |
10 | + | |
11 | + $view = <<< MySQL | |
12 | + ALTER TABLE `italautocomua`.`w_news` | |
13 | + ADD COLUMN `price_list` VARCHAR(255) NULL AFTER `img`; | |
14 | +MySQL; | |
15 | + $this->execute($view); | |
16 | + | |
17 | + } | |
18 | + | |
19 | + public function down() | |
20 | + { | |
21 | + echo "m151116_153942_add_field_to_news cannot be reverted.\n"; | |
22 | + | |
23 | + return false; | |
24 | + } | |
25 | + | |
26 | + /* | |
27 | + // Use safeUp/safeDown to run migration code within a transaction | |
28 | + public function safeUp() | |
29 | + { | |
30 | + } | |
31 | + | |
32 | + public function safeDown() | |
33 | + { | |
34 | + } | |
35 | + */ | |
36 | +} | ... | ... |
frontend/views/news/one_item.php
... | ... | @@ -4,7 +4,7 @@ use \yii\helpers\Html; |
4 | 4 | |
5 | 5 | |
6 | 6 | <div class="news"> |
7 | - <?= Html::a(Html::img('/storage/files/load/normal-'.$model->img),['news/view', 'translit' =>$model->code ]) ?> | |
7 | + <?= Html::a(Html::img($model->img),['news/view', 'translit' =>$model->code ]) ?> | |
8 | 8 | <span class='date'><?= $model->date?></span><br> |
9 | 9 | <?= Html::a("<p class='article'>".$model->name."</p>",['news/view', 'translit' =>$model->code ]) ?> |
10 | 10 | <?= Html::a("<p class='short_news'>".$model->brief."</p>",['news/view', 'translit' =>$model->code ]) ?> | ... | ... |
frontend/views/news/view.php
... | ... | @@ -10,7 +10,7 @@ $this->params['breadcrumbs'][] = $this->title; |
10 | 10 | <p class="vin_article"><?= $model->name ?></p> |
11 | 11 | </div> |
12 | 12 | <div class="choose_tovar"> |
13 | -<img src="/images/news_big.png" class="big_article"> | |
13 | +<img src="<?= $model->img ?>" class="big_article"> | |
14 | 14 | <div class="one_article"> |
15 | 15 | <?= $model->content ?> |
16 | 16 | <div class="download_catalog"> | ... | ... |
frontend/web/css/news_all.css
frontend/web/css/style/news_all.css
tests/_support/_generated/AcceptanceTesterActions.php
100644 → 100755
tests/_support/_generated/FunctionalTesterActions.php
100644 → 100755
tests/_support/_generated/UnitTesterActions.php
100644 → 100755