Commit 5e673d8b90eebbf86554828d88368040e07c09a1

Authored by Administrator
1 parent b5f053d9

Adding deletion capability

backend/components/Helper.php
@@ -6,7 +6,8 @@ namespace backend\components; @@ -6,7 +6,8 @@ namespace backend\components;
6 class Helper 6 class Helper
7 { 7 {
8 8
9 - public static function transliterate($str) { 9 + public static function transliterate($str)
  10 + {
10 $converter = array( 11 $converter = array(
11 'а' => 'a', 'б' => 'b', 'в' => 'v', 12 'а' => 'a', 'б' => 'b', 'в' => 'v',
12 'г' => 'g', 'д' => 'd', 'е' => 'e', 13 'г' => 'g', 'д' => 'd', 'е' => 'e',
@@ -35,7 +36,8 @@ class Helper @@ -35,7 +36,8 @@ class Helper
35 return strtr($str, $converter); 36 return strtr($str, $converter);
36 } 37 }
37 38
38 - public static function findUrl($filePath, $countDir) { 39 + public static function findUrl($filePath, $countDir)
  40 + {
39 $pathArr = explode('/', $filePath); 41 $pathArr = explode('/', $filePath);
40 $n = count($pathArr); 42 $n = count($pathArr);
41 $url = ''; 43 $url = '';
@@ -44,7 +46,8 @@ class Helper @@ -44,7 +46,8 @@ class Helper
44 return $url; 46 return $url;
45 } 47 }
46 48
47 - public static function isImage($file) { 49 + public static function isImage($file)
  50 + {
48 $a = getimagesize($file); 51 $a = getimagesize($file);
49 $image_type = $a[2]; 52 $image_type = $a[2];
50 if(in_array($image_type , [IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP])) 53 if(in_array($image_type , [IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP]))
backend/controllers/ImageController.php
@@ -32,13 +32,13 @@ class ImageController extends Controller @@ -32,13 +32,13 @@ class ImageController extends Controller
32 $cropContext = new CropContext($cropFactory->getCrop($crop_id)); 32 $cropContext = new CropContext($cropFactory->getCrop($crop_id));
33 $basePath = dirname(dirname(__DIR__)) . '/uploads/'. $ukrSeeds[$crop_id] . '/'; 33 $basePath = dirname(dirname(__DIR__)) . '/uploads/'. $ukrSeeds[$crop_id] . '/';
34 foreach($model->imageFiles as $file) { 34 foreach($model->imageFiles as $file) {
35 - $baseName = Helper::transliterate($file->baseName);  
36 - $path = $basePath . $baseName . '.' . $file->extension; 35 + $baseName = Helper::transliterate($file->name);
  36 + $path = $basePath . $baseName;
37 $image = Yii::$app->imagine->open($path); 37 $image = Yii::$app->imagine->open($path);
38 $image = $cropContext->cropImage($image, $path); 38 $image = $cropContext->cropImage($image, $path);
39 $box = $image->getSize()->heighten($minHeight); 39 $box = $image->getSize()->heighten($minHeight);
40 $image->resize($box) 40 $image->resize($box)
41 - ->save($basePath . $baseName . '-resized' . '.' . $file->extension); 41 + ->save($basePath . 'resized-' . $baseName);
42 } 42 }
43 } 43 }
44 return $this->render('index', ['model' => $model, 'ukrSeeds' => $ukrSeeds]); 44 return $this->render('index', ['model' => $model, 'ukrSeeds' => $ukrSeeds]);
@@ -89,4 +89,23 @@ class ImageController extends Controller @@ -89,4 +89,23 @@ class ImageController extends Controller
89 $response = Yii::$app->response; 89 $response = Yii::$app->response;
90 $response->sendFile($filename); 90 $response->sendFile($filename);
91 } 91 }
  92 +
  93 + public function actionDeleteImages()
  94 + {
  95 + if (Yii::$app->request->isAjax) {
  96 +
  97 + $data = Yii::$app->request->post();
  98 +
  99 + $files = $data['images'];
  100 + $basePath = dirname(dirname(__DIR__));
  101 +
  102 + foreach($files as $file) {
  103 + $path = $basePath . $file;
  104 + if(file_exists($path))
  105 + unlink($path);
  106 + }
  107 +
  108 + return json_encode($data);
  109 + }
  110 + }
92 } 111 }
93 \ No newline at end of file 112 \ No newline at end of file
backend/models/UploadForm.php
@@ -23,11 +23,11 @@ class UploadForm extends Model @@ -23,11 +23,11 @@ class UploadForm extends Model
23 $basePath = dirname(dirname(__DIR__)) . '/uploads/'; 23 $basePath = dirname(dirname(__DIR__)) . '/uploads/';
24 24
25 if(!file_exists($basePath . $nameDir)) 25 if(!file_exists($basePath . $nameDir))
26 - FileHelper::createDirectory($basePath . $nameDir . '/'); 26 + FileHelper::createDirectory($basePath . $nameDir . '/', 0777);
27 27
28 if ($this->validate()) { 28 if ($this->validate()) {
29 foreach($this->imageFiles as $file) { 29 foreach($this->imageFiles as $file) {
30 - $path = $basePath . $nameDir . '/' . Helper::transliterate($file->baseName) . '.' . $file->extension; 30 + $path = $basePath . $nameDir . '/' . Helper::transliterate($file->name);
31 $file->saveAs($path); 31 $file->saveAs($path);
32 } 32 }
33 return true; 33 return true;
backend/views/image/gallery.php
@@ -6,9 +6,25 @@ use yii\helpers\Html; @@ -6,9 +6,25 @@ use yii\helpers\Html;
6 /* @var $this yii\web\View */ 6 /* @var $this yii\web\View */
7 $this->title = 'Image gallery'; 7 $this->title = 'Image gallery';
8 ?> 8 ?>
9 -<?= Html::a('Download all', ['download-all']) ?> 9 +<?= Html::a('Loader', ['index']); ?>
10 <div> 10 <div>
  11 + <span>
  12 + <?= Html::a('Download all', ['download-all'], ['class' => 'btn btn-primary']); ?>
  13 + </span>
  14 + <span>
  15 + <?= Html::buttonInput('Delete', ['class' => 'btn btn-warning', 'id' => 'delete']); ?>
  16 + </span>
  17 + <span>
  18 + <?= Html::buttonInput('Select All', ['class' => 'btn btn-info', 'id' => 'select-all']); ?>
  19 + </span>
  20 +</div>
  21 +<div class="content">
11 <?php foreach ($files as $file): ?> 22 <?php foreach ($files as $file): ?>
12 -<img height="300px" src="<?= Html::encode($file) ?>"> 23 + <div data-url="<?= Html::encode($file) ?>" style="display: inline-block; border: 1px solid black">
  24 + <label>
  25 + <img height="300px" src="<?= Html::encode($file) ?>">
  26 + <input style="display: block; margin: auto" type="checkbox" value="<?= Html::encode($file) ?>" class="single-checkbox">
  27 + </label>
  28 + </div>
13 <?php endforeach ?> 29 <?php endforeach ?>
14 </div> 30 </div>
backend/web/js/main.js
1 -var canvas = $('canvas').attr({'width' : 1200, 'height' : 500}).css('border', '1px solid black');  
2 -var context = canvas.get(0).getContext('2d'); 1 +var canvas = $('canvas')
  2 + .attr({'width' : 1200, 'height' : 500})
  3 + .css('border', '1px solid black')
  4 + .get(0);
  5 +
  6 +if(canvas)
  7 + var context = canvas.getContext('2d');
3 8
4 var model = { 9 var model = {
5 width : 0, 10 width : 0,
@@ -11,11 +16,47 @@ var model = { @@ -11,11 +16,47 @@ var model = {
11 }; 16 };
12 17
13 var select = $('#ukr_seeds'); 18 var select = $('#ukr_seeds');
14 -select.change(getParams); 19 +select.change(getModel);
  20 +
  21 +getModel();
  22 +
  23 +$('#delete').click(function() {
  24 + var inputs = $('.single-checkbox:checked');
  25 + var n = inputs.length;
  26 + if(n) {
  27 + var files = [];
  28 + $.each(inputs, function() {
  29 + files.push($(this).val());
  30 + });
  31 + deleteImages(files);
  32 + }
  33 +});
  34 +
  35 +$('#select-all').click(function() {
  36 + $('.single-checkbox').prop('checked', true);
  37 +});
  38 +
  39 +function deleteImages(images) {
  40 + var href = decodeURIComponent(location.href);
  41 + var url = href.substring(0, href.lastIndexOf('/'));
  42 + $.ajax({
  43 + url: url + '/delete-images',
  44 + type: 'POST',
  45 + dataType: 'json',
  46 + data: {
  47 + images: images
  48 + },
  49 + success: function(data) {
  50 + var images = data['images'];
  51 + for(var i = 0; i < images.length; i++) {
  52 + $('div[data-url="' + images[i] + '"]').remove();
  53 + }
  54 + }
  55 + });
  56 +}
15 57
16 -getParams();  
17 58
18 -function getParams() 59 +function getModel()
19 { 60 {
20 $.ajax({ 61 $.ajax({
21 url: location.href + '/get-params', 62 url: location.href + '/get-params',
@@ -42,4 +83,5 @@ function draw() @@ -42,4 +83,5 @@ function draw()
42 context.clearRect(0, 0, canvas.width, canvas.height); 83 context.clearRect(0, 0, canvas.width, canvas.height);
43 context.drawImage(image, model.point.x, model.point.y, model.width, model.height, 10, 10, model.width, model.height); 84 context.drawImage(image, model.point.x, model.point.y, model.width, model.height, 10, 10, model.width, model.height);
44 console.log( image.width, image.height ); 85 console.log( image.width, image.height );
45 -}  
46 \ No newline at end of file 86 \ No newline at end of file
  87 +}
  88 +