1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibraryModule\Application\Modules\AbstractModule\Controllers\Traits; |
4
|
|
|
|
5
|
|
|
use ByTIC\MediaLibrary\Exceptions\FileCannotBeAdded; |
6
|
|
|
use ByTIC\MediaLibrary\Exceptions\FileCannotBeAdded\FileUnacceptableForCollection; |
7
|
|
|
use ByTIC\MediaLibrary\FileAdder\FileAdderFactory; |
8
|
|
|
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait; |
9
|
|
|
use ByTIC\MediaLibraryModule\Controllers\Traits\Async\Covers; |
10
|
|
|
use ByTIC\MediaLibraryModule\Controllers\Traits\Async\Files; |
11
|
|
|
use ByTIC\MediaLibraryModule\Controllers\Traits\Async\Gallery; |
12
|
|
|
use ByTIC\MediaLibraryModule\Controllers\Traits\Async\Images; |
13
|
|
|
use ByTIC\MediaLibraryModule\Controllers\Traits\Async\Logos; |
14
|
|
|
use Nip\Http\Request; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class HasMediaAsyncTrait |
18
|
|
|
* @package ByTIC\MediaLibraryModule\Application\Modules\AbstractModule\Controllers\Traits |
19
|
|
|
* |
20
|
|
|
* @method HasMediaTrait getModelFromRequest |
21
|
|
|
* @method Request getRequest |
22
|
|
|
*/ |
23
|
|
|
trait HasMediaAsyncTrait |
24
|
|
|
{ |
25
|
|
|
use Covers; |
26
|
|
|
use Files; |
27
|
|
|
use Gallery; |
28
|
|
|
use Images; |
29
|
|
|
use Logos; |
30
|
|
|
|
31
|
|
|
public function uploadGallery() |
32
|
|
|
{ |
33
|
|
|
if (!$this->getRequest()->files->has('file') || $this->getRequest()->request->has('action')) { |
34
|
|
|
$this->uploadGalleryImgPicker(); |
35
|
|
|
|
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** @var HasMediaTrait $item */ |
40
|
|
|
$item = $this->getModelFromRequest(); |
41
|
|
|
$mediaType = $this->getRequest()->get('media_type', 'images'); |
42
|
|
|
|
43
|
|
|
/** @var \ByTIC\MediaLibrary\Validation\Constraints\ImageConstraint $constraint */ |
44
|
|
|
// $constraint = $item->getMediaRepository()->getCollection('images')->getConstraint(); |
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
$adder = FileAdderFactory::create($item, $this->getRequest()->files->get('file')); |
48
|
|
|
|
49
|
|
|
if ($this->getRequest()->request->has('cropper')) { |
50
|
|
|
parse_str($this->getRequest()->request->get('cropper'), $copperData); |
51
|
|
|
$adder->getMedia()->cropperData = $copperData; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
$adder->toMediaCollection($mediaType); |
54
|
|
|
} catch (FileCannotBeAdded $exception) { |
55
|
|
|
http_response_code(415); |
56
|
|
|
header('Content-Type: application/json; charset=utf-8'); |
57
|
|
|
$output = ['error' => $exception->getMessage()]; |
58
|
|
|
if ($exception instanceof FileUnacceptableForCollection) { |
59
|
|
|
$output['error'] .= ': ' . $exception->violations->getMessageString(); |
60
|
|
|
} |
61
|
|
|
echo json_encode($output); |
62
|
|
|
} |
63
|
|
|
die(); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function setDefaultMediaItem() |
67
|
|
|
{ |
68
|
|
|
$item = $this->getModelFromRequest(); |
69
|
|
|
$type = $this->getRequest()->get('media_type'); |
70
|
|
|
switch ($type) { |
71
|
|
|
case 'images': |
72
|
|
|
case 'covers': |
73
|
|
|
case 'logos': |
74
|
|
|
$item->getMedia($type)->persistDefaultMediaFromName( |
75
|
|
|
$this->getRequest()->get('media_filename') |
76
|
|
|
); |
77
|
|
|
$this->sendSuccess("Imaginea a fost stabilita ca principala"); |
|
|
|
|
78
|
|
|
break; |
79
|
|
|
|
80
|
|
|
default: |
81
|
|
|
$this->sendError("Imaginea nu a putut fi stabilita ca principala"); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function removeMediaItem() |
86
|
|
|
{ |
87
|
|
|
$item = $this->getModelFromRequest(); |
88
|
|
|
$type = $this->getRequest()->get('media_type'); |
89
|
|
|
|
90
|
|
|
switch ($this->getRequest()->get('media_type')) { |
91
|
|
|
case 'images': |
92
|
|
|
case 'covers': |
93
|
|
|
case 'logos': |
94
|
|
|
$item->getMedia($type)->deleteMediaByKey( |
|
|
|
|
95
|
|
|
$this->getRequest()->get('media_filename') |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$this->sendSuccess("Imaginea a fost stearsa"); |
99
|
|
|
break; |
100
|
|
|
default: |
101
|
|
|
$this->sendError("Imaginea nu a putut fi stearsa"); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|