|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\PerfectCmsImages\Forms; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\AssetAdmin\Forms\UploadField; |
|
6
|
|
|
use SilverStripe\Assets\Folder; |
|
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
8
|
|
|
use SilverStripe\Control\HTTPResponse; |
|
9
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
|
10
|
|
|
use SilverStripe\ORM\SS_List; |
|
11
|
|
|
use Sunnysideup\PerfectCmsImages\Api\PerfectCMSImages; |
|
12
|
|
|
use Sunnysideup\PerfectCmsImages\Filesystem\PerfectCmsImageValidator; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* image-friendly upload field. |
|
16
|
|
|
* |
|
17
|
|
|
* Usage: |
|
18
|
|
|
* $field = PerfectCmsImagesUploadFielde::create( |
|
19
|
|
|
* "ImageField", |
|
20
|
|
|
* "Add Image", |
|
21
|
|
|
* ); |
|
22
|
|
|
*/ |
|
23
|
|
|
class PerfectCmsImagesUploadField extends UploadField |
|
24
|
|
|
{ |
|
25
|
|
|
private static $max_size_in_kilobytes = 2048; |
|
26
|
|
|
|
|
27
|
|
|
private static $folder_prefix = ''; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @config |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private static $allowed_actions = [ |
|
34
|
|
|
'upload', |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
private $afterUpload = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $name The internal field name, passed to forms. |
|
41
|
|
|
* @param string $title The field label. |
|
42
|
|
|
* @param SS_List|null $items If no items are defined, the field will try to auto-detect an existing relation |
|
43
|
|
|
* @param string|null $alternativeName |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct( |
|
46
|
|
|
$name, |
|
47
|
|
|
$title = null, |
|
48
|
|
|
SS_List $items = null, |
|
49
|
|
|
$alternativeName = null |
|
50
|
|
|
) { |
|
51
|
|
|
parent::__construct( |
|
52
|
|
|
$name, |
|
53
|
|
|
$title, |
|
54
|
|
|
$items |
|
55
|
|
|
); |
|
56
|
|
|
$perfectCMSImageValidator = new PerfectCMSImageValidator(); |
|
57
|
|
|
$this->setValidator($perfectCMSImageValidator); |
|
58
|
|
|
if ($alternativeName === null) { |
|
59
|
|
|
$alternativeName = $name; |
|
60
|
|
|
} |
|
61
|
|
|
$this->selectFormattingStandard($alternativeName); |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function setDescription($string) |
|
67
|
|
|
{ |
|
68
|
|
|
parent::setDescription( |
|
69
|
|
|
DBField::create_field('HTMLText', $string . '<br />' . $this->RightTitle()) |
|
70
|
|
|
); |
|
71
|
|
|
//important! |
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $name Formatting Standard |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
|
|
public function selectFormattingStandard(string $name) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->setPerfectFolderName($name); |
|
82
|
|
|
|
|
83
|
|
|
$this->setDescription(PerfectCMSImages::get_description_for_cms($name)); |
|
84
|
|
|
|
|
85
|
|
|
$this->setAllowedFileCategories('image'); |
|
86
|
|
|
$alreadyAllowed = $this->getAllowedExtensions(); |
|
87
|
|
|
$this->setAllowedExtensions($alreadyAllowed + ['svg']); |
|
88
|
|
|
//keep the size reasonable |
|
89
|
|
|
$maxSizeInKilobytes = PerfectCMSImages::max_size_in_kilobytes($name); |
|
90
|
|
|
$this->getValidator()->setAllowedMaxFileSize(1 * 1024 * $maxSizeInKilobytes); |
|
91
|
|
|
$this->getValidator()->setFieldName($name); |
|
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Creates a single file based on a form-urlencoded upload. |
|
97
|
|
|
* Allows for hooking AfterUpload |
|
98
|
|
|
* |
|
99
|
|
|
* @param HTTPRequest $request |
|
100
|
|
|
* @return HTTPResponse |
|
101
|
|
|
*/ |
|
102
|
|
|
public function upload(HTTPRequest $request) |
|
103
|
|
|
{ |
|
104
|
|
|
$response = parent::upload($request); |
|
105
|
|
|
|
|
106
|
|
|
// If afterUpload is a function .. |
|
107
|
|
|
return is_callable($this->afterUpload) ? |
|
108
|
|
|
// .. then return the results from that .. |
|
109
|
|
|
($this->afterUpload)($response) : |
|
110
|
|
|
// .. else return the original $response |
|
111
|
|
|
$response; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Add an anonymous functions to run after upload completes |
|
116
|
|
|
* |
|
117
|
|
|
* @param callable $func |
|
118
|
|
|
* @return self |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setAfterUpload($func): self |
|
121
|
|
|
{ |
|
122
|
|
|
$this->afterUpload = $func; |
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
protected function setPerfectFolderName(string $name) |
|
127
|
|
|
{ |
|
128
|
|
|
$folderPrefix = $this->Config()->get('folder_prefix'); |
|
129
|
|
|
|
|
130
|
|
|
$folderName = $this->folderName; |
|
131
|
|
|
if (! $folderName) { |
|
132
|
|
|
//folder related stuff ... |
|
133
|
|
|
$folderName = PerfectCMSImages::get_folder($name); |
|
134
|
|
|
if (! $folderName) { |
|
135
|
|
|
$folderName = 'other-images'; |
|
136
|
|
|
} |
|
137
|
|
|
$folderName = implode( |
|
138
|
|
|
'/', |
|
139
|
|
|
array_filter([$folderPrefix, $folderName]) |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
//create folder |
|
143
|
|
|
Folder::find_or_make($folderName); |
|
144
|
|
|
//set folder |
|
145
|
|
|
$this->setFolderName($folderName); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|