From 0b491165a12251d4574d108a07cc586350c6da37 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 8 Oct 2015 13:13:39 +0300 Subject: [PATCH] Importers CRUD --- backend/components/ImageSizer.php | 17 +++++++++++++---- backend/components/base/BaseController.php | 47 ++++++++++++++++++++++++++++++++++++++--------- backend/components/views/image_sizer.php | 21 ++++++++++++--------- backend/controllers/SiteController.php | 6 +++++- backend/models/ImageSizerForm.php | 4 +++- backend/views/layouts/column.php | 8 ++++++++ backend/views/user/_form.php | 2 +- backend/web/css/AdminLTE.css | 4 ++-- 8 files changed, 82 insertions(+), 27 deletions(-) diff --git a/backend/components/ImageSizer.php b/backend/components/ImageSizer.php index bccb202..3d3161d 100644 --- a/backend/components/ImageSizer.php +++ b/backend/components/ImageSizer.php @@ -12,8 +12,8 @@ use yii\base\Widget; class ImageSizer extends Widget { - public $height; - public $width; + public $height = 0; + public $width = 0; public $field; public $file; public $model; @@ -43,8 +43,17 @@ class ImageSizer extends Widget } - public function getGalley(){ - return $this->gallery; + public function getGallery(){ + if($this->gallery){ + $array = explode(",", $this->gallery); + if(count($array) > 1){ + array_pop($array); + } + return $array; + } else { + return array(); + } + } } \ No newline at end of file diff --git a/backend/components/base/BaseController.php b/backend/components/base/BaseController.php index 00b6654..87d48b2 100644 --- a/backend/components/base/BaseController.php +++ b/backend/components/base/BaseController.php @@ -17,10 +17,7 @@ use yii\imagine\Image; class BaseController extends Controller { - public function beforeAction($action) { - $this->enableCsrfValidation = false; - return parent::beforeAction($action); - } + private function resizeImg($w, $h, $imageAlias,$imageAliasSave){ $img = Image::getImagine()->open(Yii::getAlias($imageAlias)); @@ -56,6 +53,35 @@ class BaseController extends Controller { } + private function deleteImages($old_img){ + + if(!empty($old_img) && file_exists($_SERVER['DOCUMENT_ROOT'].$old_img)){ + + $rootDir = explode("/", $old_img); + + $row = $_SERVER['DOCUMENT_ROOT'].'/'.$rootDir[1].'/'.$rootDir[2].'/'; + + $allFiles = scandir($row); + + $allFiles = array_slice($allFiles, 2); + + foreach($allFiles as $oldFile){ + + unlink($row.$oldFile); + + } + + } + } + + public function actionDeleteImage(){ + $old_img = Yii::$app->request->post('old_img'); + + if ($old_img) { + $this->deleteImages($old_img); + } + } + public function actionDownloadPhoto() { @@ -64,6 +90,8 @@ class BaseController extends Controller { if ($model->load(Yii::$app->request->post())) { + $this->deleteImages($model->old_img); + $model->file = UploadedFile::getInstance($model, 'file'); $md5_file = md5_file($model->file->tempName); @@ -88,20 +116,21 @@ class BaseController extends Controller { } else { - $imageLink = $imageOrigAlias; + $imageLink = '/storage/'.$md5_file.'/'.'original'.'.'.$model->file->extension; } - if($model->multi){ - - return json_encode(['link'=>$imageLink]); - } else { + if($model->multi){ $view = $this->renderPartial('@app/components/views/_gallery_item', [ 'item' => ['image'=>$imageLink], ]); return json_encode(['link'=>$imageLink, 'view' =>$view]); + + + } else { + return json_encode(['link'=>$imageLink]); } diff --git a/backend/components/views/image_sizer.php b/backend/components/views/image_sizer.php index a66cca0..20fd8ca 100644 --- a/backend/components/views/image_sizer.php +++ b/backend/components/views/image_sizer.php @@ -41,12 +41,11 @@ $this->registerJsFile('@web/js/vendor/bower/jquery-file-upload/js/jquery.fileupl field( new \backend\models\ImageSizerForm(), 'file')->fileInput(['id'=>$field, 'data-url'=>"/admin/site/download-photo" ]); ?> field($model,$field)->hiddenInput(['id' => "{$field}_picture_link"]) ?> -field(new \backend\models\ImageSizerForm(),'height')->hiddenInput(['value' => $height])->label(false) ?> -field(new \backend\models\ImageSizerForm(), 'width')->hiddenInput(['value' => $width])->label(false) ?> - - + + +
- $field ? Html::img('/'.$model->$field): '' ?> + $field ? Html::img($model->$field): '' ?>