diff --git a/backend/controllers/ArticlesController.php b/backend/controllers/ArticlesController.php index 8399cbf..15b6dd4 100755 --- a/backend/controllers/ArticlesController.php +++ b/backend/controllers/ArticlesController.php @@ -9,6 +9,8 @@ use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use developeruz\db_rbac\behaviors\AccessBehavior; +use yii\web\UploadedFile; + /** * ArticlesController implements the CRUD actions for Articles model. */ @@ -77,7 +79,16 @@ class ArticlesController extends Controller { $model = new Articles(); - if ($model->load(Yii::$app->request->post()) && $model->save()) { + if ($model->load(Yii::$app->request->post())) { + + if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { + $model->image = $image->name; + } + + if ($model->save() && $image) { + $image->saveAs(Yii::getAlias('@imagesDir/articles/' . $image->name)); + } + return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ @@ -96,7 +107,16 @@ class ArticlesController extends Controller { $model = $this->findModel($id); - if ($model->load(Yii::$app->request->post()) && $model->save()) { + if ($model->load(Yii::$app->request->post())) { + + if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { + $model->image = $image->name; + } + + if ($model->save() && $image) { + $image->saveAs(Yii::getAlias('@imagesDir/articles/' . $image->name)); + } + return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', [ diff --git a/backend/controllers/BlogController.php b/backend/controllers/BlogController.php index b92a161..4bf7891 100755 --- a/backend/controllers/BlogController.php +++ b/backend/controllers/BlogController.php @@ -79,10 +79,18 @@ class BlogController extends Controller { $model = new Blog(); - if ($model->load(Yii::$app->request->post()) && $model->save()) { + if ($model->load(Yii::$app->request->post())) { Fields::saveFieldData(Yii::$app->request->post('Fields'), $model->blog_id, Blog::className(), 'ru'); + if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { + $model->cover = $image->name; + } + + if ($model->save() && $image) { + $image->saveAs(Yii::getAlias('@imagesDir/articles/' . $image->name)); + } + return $this->redirect(['view', 'id' => $model->blog_id]); } else { return $this->render('create', [ @@ -101,8 +109,18 @@ class BlogController extends Controller { $model = $this->findModel($id); - if ($model->load(Yii::$app->request->post()) && $model->save()) { + if ($model->load(Yii::$app->request->post())) { + Fields::saveFieldData(Yii::$app->request->post('Fields'), $model->blog_id, Blog::className(), 'ru'); + + if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { + $model->cover = $image->name; + } + + if ($model->save() && $image) { + $image->saveAs(Yii::getAlias('@imagesDir/articles/' . $image->name)); + } + return $this->redirect(['view', 'id' => $model->blog_id]); } else { return $this->render('update', [ diff --git a/backend/controllers/BrandController.php b/backend/controllers/BrandController.php index 75b070b..058a1a0 100755 --- a/backend/controllers/BrandController.php +++ b/backend/controllers/BrandController.php @@ -9,6 +9,8 @@ use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use yii\filters\AccessControl; +use yii\web\UploadedFile; + /** * BrandController implements the CRUD actions for Brand model. */ @@ -78,8 +80,15 @@ class BrandController extends Controller public function actionCreate() { $model = new Brand(); - if ($model->load(Yii::$app->request->post()) && $model->save()) { - exit; + if ($model->load(Yii::$app->request->post())) { + if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { + $model->image = $image->name; + } + + if ($model->save() && $image) { + $image->saveAs(Yii::getAlias('@imagesDir/brands/' . $image->name)); + } + return is_null(Yii::$app->request->post('create_and_new')) ? $this->redirect(['view', 'id' => $model->brand_id]) : $this->redirect(array_merge(['create'], Yii::$app->request->queryParams)); } else { return $this->render('create', [ @@ -99,13 +108,15 @@ class BrandController extends Controller $model = $this->findModel($id); if ($model->load(Yii::$app->request->post())) { -// var_dump($_POST); -// print "
"; -// var_dump($_FILES); -// exit; - if ($model->save()) { - return $this->redirect(['view', 'id' => $model->brand_id]); + if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { + $model->image = $image->name; + } + + if ($model->save() && $image) { + $image->saveAs(Yii::getAlias('@imagesDir/brands/' . $image->name)); } + + return $this->redirect(['view', 'id' => $model->brand_id]); } else { return $this->render('update', [ 'model' => $model, diff --git a/backend/controllers/CategoryController.php b/backend/controllers/CategoryController.php index 7f17727..0556c9b 100755 --- a/backend/controllers/CategoryController.php +++ b/backend/controllers/CategoryController.php @@ -11,6 +11,8 @@ use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use yii\filters\AccessControl; +use yii\web\UploadedFile; + /** * CategoryController implements the CRUD actions for Category model. */ @@ -79,7 +81,15 @@ class CategoryController extends Controller { $model = new Category(); - if ($model->load(Yii::$app->request->post()) && $model->save()) { + if ($model->load(Yii::$app->request->post())) { + if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { + $model->image = $image->name; + } + + if ($model->save() && $image) { + $image->saveAs(Yii::getAlias('@imagesDir/categories/' . $image->name)); + } + return is_null(Yii::$app->request->post('create_and_new')) ? $this->redirect(['view', 'id' => $model->category_id]) : $this->redirect(array_merge(['create'], Yii::$app->request->queryParams)); } else { if (!empty(Yii::$app->request->queryParams['parent'])) { @@ -102,7 +112,15 @@ class CategoryController extends Controller { $model = $this->findModel($id); - if ($model->load(Yii::$app->request->post()) && $model->save()) { + if ($model->load(Yii::$app->request->post())) { + if ( ($image = UploadedFile::getInstance($model, 'imageUpload')) ) { + $model->image = $image->name; + } + + if ($model->save() && $image) { + $image->saveAs(Yii::getAlias('@imagesDir/categories/' . $image->name)); + } + return $this->redirect(['view', 'id' => $model->category_id]); } else { return $this->render('update', [ diff --git a/backend/views/articles/_form.php b/backend/views/articles/_form.php index c9f8ad4..f7f997a 100755 --- a/backend/views/articles/_form.php +++ b/backend/views/articles/_form.php @@ -13,7 +13,10 @@ use yii\jui\DatePicker;
- + false, + 'options' => ['enctype' => 'multipart/form-data'] + ]); ?> field($model, 'date') @@ -34,7 +37,21 @@ use yii\jui\DatePicker; ]) ?> - field($model, 'image')->textInput(['maxlength' => true]) ?> + field($model, 'imageUpload')->widget(\kartik\file\FileInput::classname(), [ + 'language' => 'ru', + 'options' => [ + 'accept' => 'image/*', + 'multiple' => false, + ], + 'pluginOptions' => [ + 'allowedFileExtensions' => ['jpg', 'gif', 'png'], + 'initialPreview' => !empty($model->imageUrl) ? \common\components\artboximage\ArtboxImageHelper::getImage($model->imageUrl, 'list') : '', + 'overwriteInitial' => true, + 'showRemove' => false, + 'showUpload' => false, + 'previewFileType' => 'image', + ], + ]); ?> field($model, 'translit')->textInput(['maxlength' => true]) ?> diff --git a/backend/views/blog/_form.php b/backend/views/blog/_form.php index 033b613..5f27928 100755 --- a/backend/views/blog/_form.php +++ b/backend/views/blog/_form.php @@ -15,7 +15,10 @@ use common\components\Request;
- + false, + 'options' => ['enctype' => 'multipart/form-data'] + ]); ?> field($model, 'name')->textInput(['maxlength' => true]) ?> @@ -31,20 +34,21 @@ use common\components\Request; ) ]) ?> - $model, - 'field'=>'cover', - 'size' => [ - [ - 'width'=>340, - 'height'=>260, - ] + field($model, 'imageUpload')->widget(\kartik\file\FileInput::classname(), [ + 'language' => 'ru', + 'options' => [ + 'accept' => 'image/*', + 'multiple' => false, ], - 'multi'=>true, - 'gallery' =>$model->cover, - 'name' => 'Загрузить миниатюру статьи' - ]); - ?> + 'pluginOptions' => [ + 'allowedFileExtensions' => ['jpg', 'gif', 'png'], + 'initialPreview' => !empty($model->imageUrl) ? \common\components\artboximage\ArtboxImageHelper::getImage($model->imageUrl, 'list') : '', + 'overwriteInitial' => true, + 'showRemove' => false, + 'showUpload' => false, + 'previewFileType' => 'image', + ], + ]); ?> 'education', diff --git a/backend/views/brand/_form.php b/backend/views/brand/_form.php index 0f2e78a..993a69b 100755 --- a/backend/views/brand/_form.php +++ b/backend/views/brand/_form.php @@ -19,7 +19,21 @@ use yii\widgets\ActiveForm; field($model, 'alias')->textInput(['maxlength' => true]) ?> - field($model, 'image')->fileInput(['accept' => 'image/*']) ?> + field($model, 'imageUpload')->widget(\kartik\file\FileInput::classname(), [ + 'language' => 'ru', + 'options' => [ + 'accept' => 'image/*', + 'multiple' => false, + ], + 'pluginOptions' => [ + 'allowedFileExtensions' => ['jpg', 'gif', 'png'], + 'initialPreview' => !empty($model->imageUrl) ? \common\components\artboximage\ArtboxImageHelper::getImage($model->imageUrl, 'list') : '', + 'overwriteInitial' => true, + 'showRemove' => false, + 'showUpload' => false, + 'previewFileType' => 'image', + ], + ]); ?> field($model, 'meta_title')->textInput(['maxlength' => true]) ?> diff --git a/backend/views/brand/view.php b/backend/views/brand/view.php index be314ae..ad1a066 100755 --- a/backend/views/brand/view.php +++ b/backend/views/brand/view.php @@ -31,7 +31,7 @@ $this->params['breadcrumbs'][] = $this->title; 'brand_id', 'brand_name_id', 'alias', - 'image', + 'imageUrl:image', 'meta_title', 'meta_desc:ntext', 'meta_robots', diff --git a/backend/views/category/_form.php b/backend/views/category/_form.php index 5aae4de..7d3264c 100755 --- a/backend/views/category/_form.php +++ b/backend/views/category/_form.php @@ -12,7 +12,10 @@ use kartik\select2\Select2;
- + false, + 'options' => ['enctype' => 'multipart/form-data'] + ]); ?> field($model, 'name')->textInput() ?> @@ -25,13 +28,6 @@ use kartik\select2\Select2; ] ])->label(Yii::t('product', 'Parent category')) ?> - field($model, 'group_to_category')->dropDownList( - \yii\helpers\ArrayHelper::map(\common\modules\rubrication\models\TaxGroup::find()->all(), 'tax_group_id', 'name'), [ - 'multiple' => true - ])->label(Yii::t('product', 'Linked options')) ?> - */?> - field($model, 'group_to_category')->widget(Select2::className(), [ 'data' => \yii\helpers\ArrayHelper::map(\common\modules\rubrication\models\TaxGroup::find()->all(), 'tax_group_id', 'name'), 'language' => 'ru', @@ -45,20 +41,21 @@ use kartik\select2\Select2; ] ) ?> - $model, - 'field'=>'image', - 'size' => [ - [ - 'width'=>340, - 'height'=>260, - ] + field($model, 'imageUpload')->widget(\kartik\file\FileInput::classname(), [ + 'language' => 'ru', + 'options' => [ + 'accept' => 'image/*', + 'multiple' => false, + ], + 'pluginOptions' => [ + 'allowedFileExtensions' => ['jpg', 'gif', 'png'], + 'initialPreview' => !empty($model->imageUrl) ? \common\components\artboximage\ArtboxImageHelper::getImage($model->imageUrl, 'list') : '', + 'overwriteInitial' => true, + 'showRemove' => false, + 'showUpload' => false, + 'previewFileType' => 'image', ], - 'multi'=>false, - 'gallery' => $model->image, - 'name' => 'Загрузить изображение' - ]); - ?> + ]); ?> field($model, 'meta_title')->textInput(['maxlength' => true]) ?> diff --git a/backend/views/category/view.php b/backend/views/category/view.php index 987a3d4..fe771c5 100755 --- a/backend/views/category/view.php +++ b/backend/views/category/view.php @@ -36,7 +36,7 @@ $this->params['breadcrumbs'][] = $this->title; 'parent_id', 'path', 'depth', - 'image', + 'imageUrl:image', 'meta_title', 'meta_desc:ntext', 'meta_robots', diff --git a/common/components/artboximage/ArtboxImageBehavior.php b/common/components/artboximage/ArtboxImageBehavior.php new file mode 100644 index 0000000..51a2c42 --- /dev/null +++ b/common/components/artboximage/ArtboxImageBehavior.php @@ -0,0 +1,10 @@ + 255], + [['imageUpload'], 'safe'], + [['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'], ]; } @@ -53,6 +57,7 @@ class Articles extends \yii\db\ActiveRecord 'title' => 'Title', 'body' => 'Body', 'image' => 'Image', + 'imageUrl' => Yii::t('app', 'Image'), 'translit' => 'Translit', 'meta_title' => 'Meta Title', 'meta_keywords' => 'Meta Keywords', @@ -61,4 +66,13 @@ class Articles extends \yii\db\ActiveRecord 'h1' => 'H1', ]; } + + public function getImageFile() { + return empty($this->image) ? null : Yii::getAlias('@imagesDir/articles/'. $this->image); + } + + public function getImageUrl() + { + return empty($this->image) ? null : Yii::getAlias('@imagesUrl/articles/' . $this->image); + } } diff --git a/common/models/Blog.php b/common/models/Blog.php index 5375804..1cf7239 100755 --- a/common/models/Blog.php +++ b/common/models/Blog.php @@ -21,6 +21,7 @@ */ class Blog extends \yii\db\ActiveRecord { + public $imageUpload; /** * @inheritdoc @@ -80,6 +81,8 @@ 'string', 'max' => 255, ], + [['imageUpload'], 'safe'], + [['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'], ]; } @@ -102,6 +105,17 @@ 'view_count' => Yii::t('app', 'view_count'), 'description' => Yii::t('app', 'description'), 'cover' => Yii::t('app', 'cover'), + 'image' => Yii::t('app', 'cover'), + 'imageUrl' => Yii::t('app', 'cover'), ]; } + + public function getImageFile() { + return empty($this->image) ? null : Yii::getAlias('@imagesDir/brands/'. $this->image); + } + + public function getImageUrl() + { + return empty($this->image) ? null : Yii::getAlias('@imagesUrl/brands/' . $this->image); + } } diff --git a/common/modules/product/models/Brand.php b/common/modules/product/models/Brand.php index 8f65f41..3b1ad37 100755 --- a/common/modules/product/models/Brand.php +++ b/common/modules/product/models/Brand.php @@ -4,8 +4,7 @@ namespace common\modules\product\models; use common\behaviors\Slug; use common\modules\rubrication\behaviors\ArtboxSynonymBehavior; -use mongosoft\file\UploadBehavior; -use mongosoft\file\UploadImageBehavior; +use yii\web\UploadedFile; use Yii; /** @@ -27,6 +26,8 @@ use Yii; */ class Brand extends \yii\db\ActiveRecord { + public $imageUpload; + public function behaviors() { return [ @@ -43,18 +44,6 @@ class Brand extends \yii\db\ActiveRecord 'slugKeyName' => 'alias', 'translit' => true ], - 'uploader' => [ - 'class' => UploadBehavior::className(), - 'attribute' => 'fileUpload', - 'scenarios' => ['insert', 'update'], -// 'placeholder' => '@app/modules/user/assets/images/userpic.jpg', - 'path' => '@storage/brands/{alias}', - 'url' => '/storage/brands/{alias}', -// 'thumbs' => [ -// 'thumb' => ['width' => 400, 'quality' => 90], -// 'preview' => ['width' => 200, 'height' => 200], -// ], - ], ], ]; } @@ -77,10 +66,10 @@ class Brand extends \yii\db\ActiveRecord [['brand_name_id'], 'integer'], [['meta_desc', 'seo_text'], 'string'], [['alias', 'name'], 'string', 'max' => 250], - [['meta_title'], 'string', 'max' => 255], + [['meta_title', 'image'], 'string', 'max' => 255], [['meta_robots'], 'string', 'max' => 50], - [['fileUpload'], 'file', 'extensions' => 'jpg, gif, png'], -// [['brand_name_id'], 'exist', 'skipOnError' => true, 'targetClass' => BrandName::className(), 'targetAttribute' => ['brand_name_id' => 'brand_name_id']], + [['imageUpload'], 'safe'], + [['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'], ]; } @@ -132,10 +121,11 @@ class Brand extends \yii\db\ActiveRecord } public function getImageFile() { - return empty($this->image) ? null : '/images/brand/'. $this->image; + return empty($this->image) ? null : Yii::getAlias('@imagesDir/brands/'. $this->image); } - public function getImageUrl() { - return empty($this->image) ? null : '/images/brand/'. $this->image; + public function getImageUrl() + { + return empty($this->image) ? null : Yii::getAlias('@imagesUrl/brands/' . $this->image); } } diff --git a/common/modules/product/models/Category.php b/common/modules/product/models/Category.php index 144fee4..8d70776 100755 --- a/common/modules/product/models/Category.php +++ b/common/modules/product/models/Category.php @@ -85,7 +85,7 @@ class Category extends \yii\db\ActiveRecord [['name'], 'string'], [['parent_id', 'depth', 'category_name_id', 'product_unit_id'], 'integer'], [['path', 'meta_desc', 'seo_text'], 'string'], - [['meta_title'], 'string', 'max' => 255], + [['meta_title', 'image'], 'string', 'max' => 255], [['meta_robots'], 'string', 'max' => 50], [['alias', 'name'], 'string', 'max' => 250], [['populary'], 'boolean'], @@ -93,7 +93,6 @@ class Category extends \yii\db\ActiveRecord [['category_name_id'], 'exist', 'skipOnError' => true, 'targetClass' => CategoryName::className(), 'targetAttribute' => ['category_name_id' => 'category_name_id']], [['imageUpload'], 'safe'], [['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'], -// [['product_unit_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProductUnit::className(), 'targetAttribute' => ['product_unit_id' => 'product_unit_id']], ]; } @@ -172,14 +171,17 @@ class Category extends \yii\db\ActiveRecord return $this->hasOne(CategoryName::className(), ['category_name_id' => 'category_name_id']); } - public function getName() - { + public function getName() { return empty($this->categoryName) ? null : $this->categoryName->value; } + public function getImageFile() { + return empty($this->image) ? null : Yii::getAlias('@imagesDir/categories/'. $this->image); + } + public function getImageUrl() { - return empty($this->image) ? null : '/images/category/' . $this->image; + return empty($this->image) ? null : Yii::getAlias('@imagesUrl/categories/' . $this->image); } public function beforeSave($insert) diff --git a/frontend/views/articles/index.php b/frontend/views/articles/index.php index fa23016..42e6fad 100755 --- a/frontend/views/articles/index.php +++ b/frontend/views/articles/index.php @@ -27,7 +27,9 @@ $this->registerMetaTag(['name' => 'keywords', 'content' => 'Блог']); diff --git a/frontend/views/articles/show.php b/frontend/views/articles/show.php index d49604f..90fb49a 100755 --- a/frontend/views/articles/show.php +++ b/frontend/views/articles/show.php @@ -22,8 +22,7 @@ $this->registerMetaTag(['name' => 'keywords', 'content' => $news->meta_keywords]

title?>

- - + imageUrl, 'product')?> body?>

date?>

diff --git a/frontend/views/catalog/product_item.php b/frontend/views/catalog/product_item.php index dfcc1c3..995273e 100755 --- a/frontend/views/catalog/product_item.php +++ b/frontend/views/catalog/product_item.php @@ -9,11 +9,7 @@ use yii\helpers\Url; 'catalog/product', 'product' => $product]) ?>"> - image)) :?> - - - image->imageUrl, 'list')?> - + imageUrl, 'list')?>