forked from pollen8/fabrik
-
Notifications
You must be signed in to change notification settings - Fork 381
/
image.php
505 lines (430 loc) · 13.9 KB
/
image.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<?php
/**
* Plugin element to render an image already located on the server
*
* @package Joomla.Plugin
* @subpackage Fabrik.element.image
* @copyright Copyright (C) 2005-2013 fabrikar.com - All rights reserved.
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
*/
// No direct access
defined('_JEXEC') or die('Restricted access');
if (!defined('DS'))
{
define('DS', DIRECTORY_SEPARATOR);
}
/**
* Plugin element to render an image already located on the server
*
* @package Joomla.Plugin
* @subpackage Fabrik.element.image
* @since 3.0
*/
class PlgFabrik_ElementImage extends PlgFabrik_Element
{
/**
* Ignored folders
*
* @var array
*/
protected $ignoreFolders = array('cache', 'lib', 'install', 'modules', 'themes', 'upgrade', 'locks', 'smarty', 'tmp');
/**
* Db table field type
*
* @var string
*/
protected $fieldDesc = 'TEXT';
/**
* This really does get just the default value (as defined in the element's settings)
*
* @param array $data form data
*
* @return mixed
*/
public function getDefaultValue($data = array())
{
if (!isset($this->default))
{
$params = $this->getParams();
$element = $this->getElement();
$w = new FabrikWorker;
$this->default = $params->get('imagepath');
// $$$ hugh - this gets us the default image, with the root folder prepended.
// But ... if the root folder option is set, we need to strip it.
$rootFolder = $params->get('selectImage_root_folder', '/');
$rootFolder = JString::ltrim($rootFolder, '/');
$rootFolder = JString::rtrim($rootFolder, '/') . '/';
$this->default = preg_replace("#^$rootFolder#", '', $this->default);
$this->default = $w->parseMessageForPlaceHolder($this->default, $data);
if ($element->eval == "1")
{
$this->default = @eval((string) stripslashes($this->default));
FabrikWorker::logEval($this->default, 'Caught exception on eval in ' . $element->name . '::getDefaultValue() : %s');
}
}
return $this->default;
}
/**
* Helper method to get the default value used in getValue()
* For readonly elements:
* If the form is new we need to get the default value
* If the form is being edited we don't want to get the default value
* Otherwise use the 'use_default' value in $opts, defaulting to true
*
* Overrides element model as in edit/view details the image should be loaded regardless of $this->isEditable() #GH-527
*
* @param array $data Form data
* @param array $opts Options
*
* @since 3.0.7
*
* @return mixed value
*/
protected function getDefaultOnACL($data, $opts)
{
/**
* $$$rob - if no search form data submitted for the search element then the default
* selection was being applied instead
* otherwise get the default value so if we don't find the element's value in $data we fall back on this value
*/
return JArrayHelper::getValue($opts, 'use_default', true) == false ? '' : $this->getDefaultValue($data);
}
/**
* Shows the data formatted for the list view
*
* @param string $data elements data
* @param stdClass &$thisRow all the data in the lists current row
*
* @return string formatted value
*/
public function renderListData($data, stdClass &$thisRow)
{
$w = new FabrikWorker;
$data = FabrikWorker::JSONtoData($data, true);
$params = $this->getParams();
$pathset = false;
foreach ($data as $d)
{
if (strstr($d, '/'))
{
$pathset = true;
break;
}
}
if ($data === '' || empty($data) || !$pathset)
{
// No data so default to image (or simple image name stored).
$iPath = $params->get('imagepath');
if (!strstr($iPath, '/'))
{
// Single file specified so find it in tmpl folder
$data = (array) FabrikHelperHTML::image($iPath, 'list', @$this->tmpl, array(), true);
}
else
{
// 25/02/2014 Single image - we append any path later on - we certainly shouldn't set $data to $iPath.
//$data = (array) $iPath;
}
}
$selectImage_root_folder = $this->rootFolder();
// $$$ hugh - tidy up a bit so we don't have so many ///'s in the URL's
$selectImage_root_folder = JString::ltrim($selectImage_root_folder, '/');
$selectImage_root_folder = JString::rtrim($selectImage_root_folder, '/');
$selectImage_root_folder = $selectImage_root_folder === '' ? '' : $selectImage_root_folder . '/';
$showImage = $params->get('show_image_in_table', 0);
$linkURL = $params->get('link_url', '');
if (empty($data) || $data[0] == '')
{
$data[] = $params->get('imagepath');
}
for ($i = 0; $i < count($data); $i++)
{
if ($showImage)
{
// $$$ rob 30/06/2011 - say if we import via csv a url to the image check that and use that rather than the relative path
if (JString::substr($data[$i], 0, 4) == 'http')
{
$src = $data[$i];
}
else
{
$data[$i] = JString::ltrim($data[$i], '/');
$data[$i] = FabrikString::ltrimword($data[$i], $selectImage_root_folder);
$src = COM_FABRIK_LIVESITE . $selectImage_root_folder . $data[$i];
}
$data[$i] = '<img src="' . $src . '" alt="' . $data[$i] . '" />';
}
if ($linkURL)
{
$data[$i] = '<a href="' . $linkURL . '" target="_blank">' . $data[$i] . '</a>';
}
$data[$i] = $w->parseMessageForPlaceHolder($data[$i], $thisRow);
}
$data = json_encode($data);
return parent::renderListData($data, $thisRow);
}
/**
* Manipulates posted form data for insertion into database
*
* @param mixed $val this elements posted form data
* @param array $data posted form data
*
* @return mixed
*/
public function storeDatabaseFormat($val, $data)
{
$groupModel = $this->getGroup();
$params = $this->getParams();
$selectImage_root_folder = $params->get('selectImage_root_folder', '');
$key = $this->getFullName(false, true, false);
if (!array_key_exists($key, $data))
{
$element = $this->getElement();
$key = $element->name;
}
if ($groupModel->canRepeat() && !$groupModel->isJoin())
{
if ($groupModel->isJoin())
{
// @TODO - not tested with join group data
}
if (!array_key_exists($key . '_folder', $data))
{
$retval = json_encode($data[$key]);
}
else
{
$retvals = array();
foreach ($data[$key] as $k => $v)
{
$retvals[] = preg_replace("#^$selectImage_root_folder#", '', $data[$key . '_folder'][$k]) . $data[$key . '_image'][$k];
}
$retval = json_encode($retvals);
}
}
else
{
/* $$$ hugh - if we're using default image, no user selection,
* the _folder and _image won't exist,
* we'll just have the relative path in the element $key
*/
if (!array_key_exists($key . '_image', $data))
{
$retval = $data[$key];
}
else
{
$retval = preg_replace("#^$selectImage_root_folder#", '', $data[$key]);
}
}
return $retval;
}
/**
* Shows the data formatted for RSS export
*
* @param string $data Data
* @param object $thisRow All the data in the tables current row
*
* @return string formatted value
*/
public function renderListData_rss($data, $thisRow)
{
$params = $this->getParams();
$selectImage_root_folder = $params->get('selectImage_root_folder', '');
$selectImage_root_folder = JString::ltrim($selectImage_root_folder, '/');
$selectImage_root_folder = JString::rtrim($selectImage_root_folder, '/');
$selectImage_root_folder = $selectImage_root_folder === '' ? '' : $selectImage_root_folder . '/';
return '<img src="' . COM_FABRIK_LIVESITE . $selectImage_root_folder . $data . '" />';
}
/**
* Draws the html form element
*
* @param array $data to pre-populate element with
* @param int $repeatCounter repeat group counter
*
* @return string elements html
*/
public function render($data, $repeatCounter = 0)
{
$params = $this->getParams();
$name = $this->getHTMLName($repeatCounter);
$value = $this->getValue($data, $repeatCounter);
$id = $this->getHTMLId($repeatCounter);
$rootFolder = $this->rootFolder($value);
if ($rootFolder != '/')
{
$value = str_replace($rootFolder, '', $value);
}
// $$$ rob - 30/06/2011 can only select an image if its not a remote image
$canSelect = ($params->get('image_front_end_select', '0') && JString::substr($value, 0, 4) !== 'http');
// $$$ hugh - tidy up a bit so we don't have so many ///'s in the URL's
$rootFolder = JString::ltrim($rootFolder, '/');
$rootFolder = JString::rtrim($rootFolder, '/');
$rootFolder = $rootFolder === '' ? '' : $rootFolder . '/';
// $$$ rob - 30/062011 allow for full urls in the image. (e.g from csv import)
$defaultImage = JString::substr($value, 0, 4) == 'http' ? $value : COM_FABRIK_LIVESITE . $rootFolder . $value;
$float = $params->get('image_float');
$float = $float != '' ? "style='float:$float;'" : '';
$str = array();
$str[] = '<div class="fabrikSubElementContainer" id="' . $id . '">';
$rootFolder = str_replace('/', DS, $rootFolder);
if ($canSelect && $this->isEditable())
{
$str[] = '<img src="' . $defaultImage . '" alt="' . $value . '" ' . $float . ' class="imagedisplayor"/>';
if (array_key_exists($name, $data))
{
if (trim($value) == '' && $rootFolder === '')
{
$path = "/";
}
else
{
$bits = explode("/", $value);
if (count($bits) > 1)
{
$path = '/' . array_shift($bits) . '/';
$path = $rootFolder . $path;
$val = array_shift($bits);
}
else
{
$path = $rootFolder;
}
}
}
else
{
$path = $rootFolder;
}
$images = array();
$imagenames = (array) JFolder::files(JPATH_SITE . '/' . $path);
foreach ($imagenames as $n)
{
$images[] = JHTML::_('select.option', $n, $n);
}
// $$$rob not sure about his name since we are adding $repeatCounter to getHTMLName();
$imageName = $this->getGroupModel()->canRepeat() ? FabrikString::rtrimWord($name, "][$repeatCounter]") . "_image][$repeatCounter]"
: $id . '_image';
$bits = explode('/', $value);
$image = array_pop($bits);
// $$$ hugh - append $rootFolder to JPATH_SITE, otherwise we're showing folders
// they aren't supposed to be able to see.
$folders = JFolder::folders(JPATH_SITE . DS . $rootFolder);
// @TODO - if $folders is empty, hide the button/widget? All they can do is select
// from the initial image dropdown list, so no point having the widget for changing folder?
$str[] = '<br/>' . JHTML::_('select.genericlist', $images, $imageName, 'class="inputbox imageselector" ', 'value', 'text', $image);
$str[] = FabrikHelperHTML::folderAjaxSelect($folders);
$str[] = '<input type="hidden" name="' . $name . '" value="' . $value . '" class="fabrikinput hiddenimagepath folderpath" />';
}
else
{
$w = new FabrikWorker;
$value = $w->parseMessageForPlaceHolder($value, $data);
$linkURL = $params->get('link_url', '');
$imgstr = '<img src="' . $defaultImage . '" alt="' . $value . '" ' . $float . ' class="imagedisplayor"/>' . "\n";
if ($linkURL)
{
$imgstr = '<a href="' . $linkURL . '" target="_blank">' . $imgstr . '</a>';
}
$str[] = $imgstr;
$str[] = '<input type="hidden" name="' . $name . '" value="' . $value . '" class="fabrikinput hiddenimagepath folderpath" />';
}
$str[] = '</div>';
return implode("\n", $str);
}
/**
* On Ajax files
*
* @return void
*/
public function onAjax_files()
{
$this->loadMeForAjax();
$app = JFactory::getApplication();
$folder = $app->input->get('folder', '', 'string');
if (!strstr($folder, JPATH_SITE))
{
$folder = JPATH_SITE . '/' . $folder;
}
$pathA = JPath::clean($folder);
$folder = array();
$files = array();
$images = array();
FabrikWorker::readImages($pathA, "/", $folders, $images, $this->ignoreFolders);
if (!array_key_exists('/', $images))
{
$images['/'] = array();
}
echo json_encode($images['/']);
}
/**
* Returns javascript which creates an instance of the class defined in formJavascriptClass()
*
* @param int $repeatCounter Repeat group counter
*
* @return array
*/
public function elementJavascript($repeatCounter)
{
$app = JFactory::getApplication();
$params = $this->getParams();
$element = $this->getElement();
$id = $this->getHTMLId($repeatCounter);
$opts = $this->getElementJSOptions($repeatCounter);
$opts->rootPath = $this->rootFolder();
$opts->canSelect = (bool) $params->get('image_front_end_select', false);
$opts->id = $element->id;
$opts->ds = DS;
$opts->dir = JPATH_SITE . '/' . str_replace('/', DS, $opts->rootPath);
return array('FbImage', $id, $opts);
}
/**
* Get the root folder for images
*
* @param string $value Value
*
* @return string root folder
*/
protected function rootFolder($value = '')
{
$rootFolder = '';
$params = $this->getParams();
$canSelect = ($params->get('image_front_end_select', '0') && JString::substr($value, 0, 4) !== 'http');
$defaultImg = $params->get('imagepath');
// Changed first || from a && - http://fabrikar.com/forums/index.php?threads/3-1rc1-image-list-options-bug.36585/#post-184266
if ($canSelect || (JFolder::exists($defaultImg) || JFolder::exists(COM_FABRIK_BASE . $defaultImg)))
{
$rootFolder = $defaultImg;
}
return $rootFolder;
}
/**
* Used to format the data when shown in the form's email
*
* @param mixed $value element's data
* @param array $data form records data
* @param int $repeatCounter repeat group counter
*
* @return string formatted value
*/
public function getEmailValue($value, $data = array(), $repeatCounter = 0)
{
return $this->render($data);
}
/**
* Does the element consider the data to be empty
* Used in isempty validation rule
*
* $$$ hugh - right now this is the default code, here as a reminder we
* need to fix this so it makes sensible decisions about 'empty' image
*
* @param array $data data to test against
* @param int $repeatCounter repeat group #
*
* @return bool
*/
public function dataConsideredEmpty($data, $repeatCounter)
{
return ($data == '') ? true : false;
}
}