From 5e673d8b90eebbf86554828d88368040e07c09a1 Mon Sep 17 00:00:00 2001 From: Administrator Date: Fri, 30 Oct 2015 17:22:54 +0200 Subject: [PATCH] Adding deletion capability --- backend/components/Helper.php | 9 ++++++--- backend/controllers/ImageController.php | 25 ++++++++++++++++++++++--- backend/models/UploadForm.php | 4 ++-- backend/views/image/gallery.php | 20 ++++++++++++++++++-- backend/web/js/main.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++------ 5 files changed, 96 insertions(+), 16 deletions(-) diff --git a/backend/components/Helper.php b/backend/components/Helper.php index 157891b..824ef7c 100644 --- a/backend/components/Helper.php +++ b/backend/components/Helper.php @@ -6,7 +6,8 @@ namespace backend\components; class Helper { - public static function transliterate($str) { + public static function transliterate($str) + { $converter = array( 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', @@ -35,7 +36,8 @@ class Helper return strtr($str, $converter); } - public static function findUrl($filePath, $countDir) { + public static function findUrl($filePath, $countDir) + { $pathArr = explode('/', $filePath); $n = count($pathArr); $url = ''; @@ -44,7 +46,8 @@ class Helper return $url; } - public static function isImage($file) { + public static function isImage($file) + { $a = getimagesize($file); $image_type = $a[2]; if(in_array($image_type , [IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP])) diff --git a/backend/controllers/ImageController.php b/backend/controllers/ImageController.php index e83f9b0..0dcf675 100644 --- a/backend/controllers/ImageController.php +++ b/backend/controllers/ImageController.php @@ -32,13 +32,13 @@ class ImageController extends Controller $cropContext = new CropContext($cropFactory->getCrop($crop_id)); $basePath = dirname(dirname(__DIR__)) . '/uploads/'. $ukrSeeds[$crop_id] . '/'; foreach($model->imageFiles as $file) { - $baseName = Helper::transliterate($file->baseName); - $path = $basePath . $baseName . '.' . $file->extension; + $baseName = Helper::transliterate($file->name); + $path = $basePath . $baseName; $image = Yii::$app->imagine->open($path); $image = $cropContext->cropImage($image, $path); $box = $image->getSize()->heighten($minHeight); $image->resize($box) - ->save($basePath . $baseName . '-resized' . '.' . $file->extension); + ->save($basePath . 'resized-' . $baseName); } } return $this->render('index', ['model' => $model, 'ukrSeeds' => $ukrSeeds]); @@ -89,4 +89,23 @@ class ImageController extends Controller $response = Yii::$app->response; $response->sendFile($filename); } + + public function actionDeleteImages() + { + if (Yii::$app->request->isAjax) { + + $data = Yii::$app->request->post(); + + $files = $data['images']; + $basePath = dirname(dirname(__DIR__)); + + foreach($files as $file) { + $path = $basePath . $file; + if(file_exists($path)) + unlink($path); + } + + return json_encode($data); + } + } } \ No newline at end of file diff --git a/backend/models/UploadForm.php b/backend/models/UploadForm.php index a3c5c37..2b36f1d 100644 --- a/backend/models/UploadForm.php +++ b/backend/models/UploadForm.php @@ -23,11 +23,11 @@ class UploadForm extends Model $basePath = dirname(dirname(__DIR__)) . '/uploads/'; if(!file_exists($basePath . $nameDir)) - FileHelper::createDirectory($basePath . $nameDir . '/'); + FileHelper::createDirectory($basePath . $nameDir . '/', 0777); if ($this->validate()) { foreach($this->imageFiles as $file) { - $path = $basePath . $nameDir . '/' . Helper::transliterate($file->baseName) . '.' . $file->extension; + $path = $basePath . $nameDir . '/' . Helper::transliterate($file->name); $file->saveAs($path); } return true; diff --git a/backend/views/image/gallery.php b/backend/views/image/gallery.php index 2414012..9f01f09 100644 --- a/backend/views/image/gallery.php +++ b/backend/views/image/gallery.php @@ -6,9 +6,25 @@ use yii\helpers\Html; /* @var $this yii\web\View */ $this->title = 'Image gallery'; ?> - +
+ + 'btn btn-primary']); ?> + + + 'btn btn-warning', 'id' => 'delete']); ?> + + + 'btn btn-info', 'id' => 'select-all']); ?> + +
+
- +
+ +
diff --git a/backend/web/js/main.js b/backend/web/js/main.js index f5725c3..726785d 100644 --- a/backend/web/js/main.js +++ b/backend/web/js/main.js @@ -1,5 +1,10 @@ -var canvas = $('canvas').attr({'width' : 1200, 'height' : 500}).css('border', '1px solid black'); -var context = canvas.get(0).getContext('2d'); +var canvas = $('canvas') + .attr({'width' : 1200, 'height' : 500}) + .css('border', '1px solid black') + .get(0); + +if(canvas) + var context = canvas.getContext('2d'); var model = { width : 0, @@ -11,11 +16,47 @@ var model = { }; var select = $('#ukr_seeds'); -select.change(getParams); +select.change(getModel); + +getModel(); + +$('#delete').click(function() { + var inputs = $('.single-checkbox:checked'); + var n = inputs.length; + if(n) { + var files = []; + $.each(inputs, function() { + files.push($(this).val()); + }); + deleteImages(files); + } +}); + +$('#select-all').click(function() { + $('.single-checkbox').prop('checked', true); +}); + +function deleteImages(images) { + var href = decodeURIComponent(location.href); + var url = href.substring(0, href.lastIndexOf('/')); + $.ajax({ + url: url + '/delete-images', + type: 'POST', + dataType: 'json', + data: { + images: images + }, + success: function(data) { + var images = data['images']; + for(var i = 0; i < images.length; i++) { + $('div[data-url="' + images[i] + '"]').remove(); + } + } + }); +} -getParams(); -function getParams() +function getModel() { $.ajax({ url: location.href + '/get-params', @@ -42,4 +83,5 @@ function draw() context.clearRect(0, 0, canvas.width, canvas.height); context.drawImage(image, model.point.x, model.point.y, model.width, model.height, 10, 10, model.width, model.height); console.log( image.width, image.height ); -} \ No newline at end of file +} + -- libgit2 0.21.4