|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Licensed under The GPL-3.0 License |
|
4
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
5
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
6
|
|
|
* |
|
7
|
|
|
* @since 2.0.0 |
|
8
|
|
|
* @author Christopher Castro <[email protected]> |
|
9
|
|
|
* @link http://www.quickappscms.org |
|
10
|
|
|
* @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace Field\Field; |
|
13
|
|
|
|
|
14
|
|
|
use Cake\Filesystem\File; |
|
15
|
|
|
use Cake\ORM\TableRegistry; |
|
16
|
|
|
use Cake\Utility\Hash; |
|
17
|
|
|
use Cake\Validation\Validator; |
|
18
|
|
|
use CMS\View\View; |
|
19
|
|
|
use Field\Handler; |
|
20
|
|
|
use Field\Model\Entity\Field; |
|
21
|
|
|
use Field\Model\Entity\FieldInstance; |
|
22
|
|
|
use Field\Utility\ImageToolbox; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Image Field Handler. |
|
26
|
|
|
* |
|
27
|
|
|
* This field allows attach images to entities. |
|
28
|
|
|
*/ |
|
29
|
|
|
class ImageField extends Handler |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritDoc} |
|
34
|
|
|
*/ |
|
35
|
|
View Code Duplication |
public function info() |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
|
|
'type' => 'text', |
|
39
|
|
|
'name' => __d('field', 'Image'), |
|
40
|
|
|
'description' => __d('field', 'Allows to attach image files to contents.'), |
|
41
|
|
|
'hidden' => false, |
|
42
|
|
|
'maxInstances' => 0, |
|
43
|
|
|
'searchable' => false, |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritDoc} |
|
49
|
|
|
*/ |
|
50
|
|
View Code Duplication |
public function render(Field $field, View $view) |
|
51
|
|
|
{ |
|
52
|
|
|
if ($field->metadata->settings['multi'] === 'custom') { |
|
53
|
|
|
$settings = $field->metadata->settings; |
|
54
|
|
|
$settings['multi'] = $field->metadata->settings['multi_custom']; |
|
55
|
|
|
$field->metadata->set('settings', $settings); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $view->element('Field.ImageField/display', compact('field')); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritDoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function edit(Field $field, View $view) |
|
65
|
|
|
{ |
|
66
|
|
|
return $view->element('Field.ImageField/edit', compact('field')); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritDoc} |
|
71
|
|
|
*/ |
|
72
|
|
View Code Duplication |
public function fieldAttached(Field $field) |
|
73
|
|
|
{ |
|
74
|
|
|
$extra = (array)$field->extra; |
|
75
|
|
|
if (!empty($extra)) { |
|
76
|
|
|
$newExtra = []; |
|
77
|
|
|
foreach ($extra as $file) { |
|
78
|
|
|
$newExtra[] = array_merge([ |
|
79
|
|
|
'mime_icon' => '', |
|
80
|
|
|
'file_name' => '', |
|
81
|
|
|
'file_size' => '', |
|
82
|
|
|
'title' => '', |
|
83
|
|
|
'alt' => '', |
|
84
|
|
|
], (array)$file); |
|
85
|
|
|
} |
|
86
|
|
|
$field->set('extra', $newExtra); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritDoc} |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
public function validate(Field $field, Validator $validator) |
|
94
|
|
|
{ |
|
95
|
|
|
if ($field->metadata->required) { |
|
96
|
|
|
$validator |
|
97
|
|
|
->add($field->name, 'isRequired', [ |
|
98
|
|
|
'rule' => function ($value, $context) use ($field) { |
|
99
|
|
|
if (isset($context['data'][$field->name])) { |
|
100
|
|
|
$count = 0; |
|
101
|
|
|
foreach ($context['data'][$field->name] as $k => $file) { |
|
102
|
|
|
if (is_integer($k)) { |
|
103
|
|
|
$count++; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $count > 0; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return false; |
|
111
|
|
|
}, |
|
112
|
|
|
'message' => __d('field', 'You must upload one image at least.') |
|
113
|
|
|
]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if ($field->metadata->settings['multi'] !== 'custom') { |
|
117
|
|
|
$maxFiles = intval($field->metadata->settings['multi']); |
|
118
|
|
|
} else { |
|
119
|
|
|
$maxFiles = intval($field->metadata->settings['multi_custom']); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$validator |
|
123
|
|
|
->add($field->name, 'numberOfFiles', [ |
|
124
|
|
|
'rule' => function ($value, $context) use ($field, $maxFiles) { |
|
125
|
|
|
if (isset($context['data'][$field->name])) { |
|
126
|
|
|
$count = 0; |
|
127
|
|
|
foreach ($context['data'][$field->name] as $k => $file) { |
|
128
|
|
|
if (is_integer($k)) { |
|
129
|
|
|
$count++; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $count <= $maxFiles; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return false; |
|
137
|
|
|
}, |
|
138
|
|
|
'message' => __d('field', 'You can upload {0} images as maximum.', $maxFiles) |
|
139
|
|
|
]); |
|
140
|
|
|
|
|
141
|
|
|
if (!empty($field->metadata->settings['extensions'])) { |
|
142
|
|
|
$extensions = $field->metadata->settings['extensions']; |
|
143
|
|
|
$extensions = array_map('strtolower', array_map('trim', explode(',', $extensions))); |
|
144
|
|
|
$validator |
|
145
|
|
|
->add($field->name, 'extensions', [ |
|
146
|
|
|
'rule' => function ($value, $context) use ($field, $extensions) { |
|
147
|
|
|
if (isset($context['data'][$field->name])) { |
|
148
|
|
|
foreach ($context['data'][$field->name] as $k => $file) { |
|
149
|
|
|
if (is_integer($k)) { |
|
150
|
|
|
$ext = strtolower(str_replace('.', '', strrchr($file['file_name'], '.'))); |
|
151
|
|
|
if (!in_array($ext, $extensions)) { |
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return true; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return false; |
|
161
|
|
|
}, |
|
162
|
|
|
'message' => __d('field', 'Invalid image extension. Allowed extension are: {0}', $field->metadata->settings['extensions']) |
|
163
|
|
|
]); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return true; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* {@inheritDoc} |
|
171
|
|
|
* |
|
172
|
|
|
* - extra: Holds a list (array) of files and their in formation (mime-icon, |
|
173
|
|
|
* file name, etc). |
|
174
|
|
|
* |
|
175
|
|
|
* - value: Holds a text containing all file names separated by space. |
|
176
|
|
|
*/ |
|
177
|
|
|
public function beforeSave(Field $field, $post) |
|
178
|
|
|
{ |
|
179
|
|
|
// FIX Removes the "dummy" input from extra if exists, the "dummy" input is |
|
180
|
|
|
// used to force Field Handler to work when empty POST information is sent |
|
181
|
|
|
$extra = []; |
|
182
|
|
|
foreach ((array)$field->extra as $k => $v) { |
|
183
|
|
|
if (is_integer($k)) { |
|
184
|
|
|
$extra[] = $v; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
$field->set('extra', $extra); |
|
188
|
|
|
$files = (array)$post; |
|
189
|
|
|
|
|
190
|
|
|
if (!empty($files)) { |
|
191
|
|
|
$value = []; |
|
192
|
|
|
foreach ($files as $k => $file) { |
|
193
|
|
View Code Duplication |
if (!is_integer($k)) { |
|
194
|
|
|
unset($files[$k]); |
|
195
|
|
|
continue; |
|
196
|
|
|
} else { |
|
197
|
|
|
$file = array_merge([ |
|
198
|
|
|
'mime_icon' => '', |
|
199
|
|
|
'file_name' => '', |
|
200
|
|
|
'file_size' => '', |
|
201
|
|
|
'title' => '', |
|
202
|
|
|
'alt' => '', |
|
203
|
|
|
], (array)$file); |
|
204
|
|
|
} |
|
205
|
|
|
$value[] = trim("{$file['file_name']} {$file['title']} {$file['alt']}"); |
|
206
|
|
|
} |
|
207
|
|
|
$field->set('value', implode(' ', $value)); |
|
208
|
|
|
$field->set('extra', $files); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
View Code Duplication |
if ($field->metadata->value_id) { |
|
212
|
|
|
$newFileNames = Hash::extract($files, '{n}.file_name'); |
|
213
|
|
|
|
|
214
|
|
|
try { |
|
215
|
|
|
$prevFiles = (array)TableRegistry::get('Eav.EavValues') |
|
216
|
|
|
->get($field->metadata->value_id) |
|
217
|
|
|
->extra; |
|
218
|
|
|
} catch (\Exception $ex) { |
|
219
|
|
|
$prevFiles = []; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
foreach ($prevFiles as $f) { |
|
223
|
|
|
if (!in_array($f['file_name'], $newFileNames)) { |
|
224
|
|
|
$file = normalizePath(WWW_ROOT . "/files/{$field->settings['upload_folder']}/{$f['file_name']}", DS); |
|
225
|
|
|
$file = new File($file); |
|
226
|
|
|
$file->delete(); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return true; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* {@inheritDoc} |
|
236
|
|
|
*/ |
|
237
|
|
|
public function beforeDelete(Field $field) |
|
238
|
|
|
{ |
|
239
|
|
|
foreach ((array)$field->extra as $image) { |
|
240
|
|
|
if (!empty($image['file_name'])) { |
|
241
|
|
|
ImageToolbox::delete(WWW_ROOT . "/files/{$field->settings['upload_folder']}/{$image['file_name']}"); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* {@inheritDoc} |
|
248
|
|
|
*/ |
|
249
|
|
|
public function settings(FieldInstance $instance, View $view) |
|
250
|
|
|
{ |
|
251
|
|
|
return $view->element('Field.ImageField/settings_form', compact('instance')); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* {@inheritDoc} |
|
256
|
|
|
*/ |
|
257
|
|
|
public function defaultSettings(FieldInstance $instance) |
|
258
|
|
|
{ |
|
259
|
|
|
return [ |
|
260
|
|
|
'extensions' => 'jpg,jpeg,png,bmp,gif,tif,tiff', |
|
261
|
|
|
'multi' => 1, |
|
262
|
|
|
'multi_custom' => 1, |
|
263
|
|
|
'upload_folder' => '', |
|
264
|
|
|
'description' => '', |
|
265
|
|
|
'preview' => '', |
|
266
|
|
|
'min_width' => '', |
|
267
|
|
|
'min_height' => '', |
|
268
|
|
|
'max_width' => '', |
|
269
|
|
|
'max_height' => '', |
|
270
|
|
|
'min_ratio' => '', |
|
271
|
|
|
'max_ratio' => '', |
|
272
|
|
|
'min_pixels' => '', |
|
273
|
|
|
'max_pixels' => '', |
|
274
|
|
|
]; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* {@inheritDoc} |
|
279
|
|
|
*/ |
|
280
|
|
|
public function viewModeSettings(FieldInstance $instance, View $view, $viewMode) |
|
281
|
|
|
{ |
|
282
|
|
|
return $view->element('Field.ImageField/view_mode_form', compact('instance', 'viewMode')); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* {@inheritDoc} |
|
287
|
|
|
*/ |
|
288
|
|
|
public function defaultViewModeSettings(FieldInstance $instance, $viewMode) |
|
289
|
|
|
{ |
|
290
|
|
|
return [ |
|
291
|
|
|
'label_visibility' => 'above', |
|
292
|
|
|
'shortcodes' => true, |
|
293
|
|
|
'hidden' => false, |
|
294
|
|
|
'size' => 'thumbnail', |
|
295
|
|
|
'link_type' => '', |
|
296
|
|
|
]; |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|