forked from Fabrik/fabrik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.php
693 lines (588 loc) · 16.2 KB
/
field.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
<?php
/**
* Plugin element to render fields
*
* @package Joomla.Plugin
* @subpackage Fabrik.element.field
* @copyright Copyright (C) 2005-2016 Media A-Team, Inc. - All rights reserved.
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
*/
// No direct access
defined('_JEXEC') or die('Restricted access');
use Joomla\Utilities\ArrayHelper;
jimport('joomla.application.component.model');
/**
* Plugin element to render fields
*
* @package Joomla.Plugin
* @subpackage Fabrik.element.field
* @since 3.0
*/
class PlgFabrik_ElementField extends PlgFabrik_Element
{
/**
* Shows the data formatted for the list view
*
* @param string $data Elements data
* @param stdClass &$thisRow All the data in the lists current row
* @param array $opts Rendering options
*
* @return string formatted value
*/
public function renderListData($data, stdClass &$thisRow, $opts = array())
{
$profiler = JProfiler::getInstance('Application');
JDEBUG ? $profiler->mark("renderListData: {$this->element->plugin}: start: {$this->element->name}") : null;
$data = FabrikWorker::JSONtoData($data, true);
$params = $this->getParams();
foreach ($data as &$d)
{
$d = $this->format($d);
$this->_guessLinkType($d, $thisRow);
if ($params->get('render_as_qrcode', '0') === '1')
{
if (!empty($d))
{
$d = $this->qrCodeLink($thisRow);
}
}
}
return parent::renderListData($data, $thisRow, $opts);
}
/**
* Format the string for use in list view, email data
*
* @param mixed $d data
* @param bool $doNumberFormat run numberFormat()
*
* @return string
*/
protected function format(&$d, $doNumberFormat = true)
{
$params = $this->getParams();
$format = $params->get('text_format_string');
$formatBlank = $params->get('field_format_string_blank', true);
if ($doNumberFormat)
{
$d = $this->numberFormat($d);
}
if ($format != '' && ($formatBlank || $d != ''))
{
$d = sprintf($format, $d);
}
if ($params->get('password') == '1')
{
$d = str_pad('', JString::strlen($d), '*');
}
return $d;
}
/**
* Prepares the element data for CSV export
*
* @param string $data Element data
* @param object &$thisRow All the data in the lists current row
*
* @return string Formatted CSV export value
*/
public function renderListData_csv($data, &$thisRow)
{
$data = $this->format($data);
return $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();
$element = $this->getElement();
$bits = $this->inputProperties($repeatCounter);
/* $$$ rob - not sure why we are setting $data to the form's data
* but in table view when getting read only filter value from url filter this
* _form_data was not set to no readonly value was returned
* added little test to see if the data was actually an array before using it
*/
if (is_array($this->getFormModel()->data))
{
$data = $this->getFormModel()->data;
}
$value = $this->getValue($data, $repeatCounter);
if (!$this->getFormModel()->failedValidation())
{
$value = $this->numberFormat($value);
}
if (!$this->isEditable())
{
if ($params->get('render_as_qrcode', '0') === '1')
{
// @TODO - skip this is new form
if (!empty($value))
{
$value = $this->qrCodeLink($data);
}
}
else
{
$this->_guessLinkType($value, $data);
$value = $this->format($value, false);
$value = $this->getReadOnlyOutput($value, $value);
}
return ($element->hidden == '1') ? "<!-- " . $value . " -->" : $value;
}
else
{
if ($params->get('autocomplete', '0') === '3')
{
$bits['class'] .= ' fabrikGeocomplete';
}
}
/* stop "'s from breaking the content out of the field.
* $$$ rob below now seemed to set text in field from "test's" to "test's" when failed validation
* so add false flag to ensure its encoded once only
* $$$ hugh - the 'double encode' arg was only added in 5.2.3, so this is blowing some sites up
*/
if (version_compare(phpversion(), '5.2.3', '<'))
{
$bits['value'] = htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
}
else
{
$bits['value'] = htmlspecialchars($value, ENT_COMPAT, 'UTF-8', false);
}
$bits['class'] .= ' ' . $params->get('text_format');
if ($params->get('speech', 0))
{
$bits['x-webkit-speech'] = 'x-webkit-speech';
}
$layout = $this->getLayout('form');
$layoutData = new stdClass;
$layoutData->attributes = $bits;
$layoutData->sizeClass = $params->get('bootstrap_class', '');
return $layout->render($layoutData);
}
/**
* Determines the value for the element in the form view
*
* @param array $data Form data
* @param int $repeatCounter When repeating joined groups we need to know what part of the array to access
* @param array $opts Options, 'raw' = 1/0 use raw value
*
* @return string value
*/
public function getValue($data, $repeatCounter = 0, $opts = array())
{
$value = parent::getValue($data, $repeatCounter, $opts);
if (is_array($value))
{
return array_pop($value);
}
return $value;
}
/**
* Format guess link type
*
* @param string &$value Original field value
* @param array $data Record data
*
* @return void
*/
protected function _guessLinkType(&$value, $data)
{
$params = $this->getParams();
if ($params->get('guess_linktype') == '1')
{
$w = new FabrikWorker;
$opts = $this->linkOpts();
$title = $params->get('link_title', '');
if (FabrikWorker::isEmail($value) || JString::stristr($value, 'http'))
{
}
elseif (JString::stristr($value, 'www.'))
{
$value = 'http://' . $value;
}
if ($title !== '')
{
$opts['title'] = strip_tags($w->parseMessageForPlaceHolder($title, $data));
}
$label = FArrayHelper::getValue($opts, 'title', '') !== '' ? $opts['title'] : $value;
$value = FabrikHelperHTML::a($value, $label, $opts);
}
}
/**
* Get the link options
*
* @return array
*/
protected function linkOpts()
{
$fbConfig = JComponentHelper::getParams('com_fabrik');
$params = $this->getParams();
$target = $params->get('link_target_options', 'default');
$opts = array();
$opts['rel'] = $params->get('rel', '');
switch ($target)
{
default:
$opts['target'] = $target;
break;
case 'default':
break;
case 'lightbox':
FabrikHelperHTML::slimbox();
$opts['rel'] = 'lightbox[]';
if ($fbConfig->get('use_mediabox', false))
{
$opts['target'] = 'mediabox';
}
break;
}
return $opts;
}
/**
* Returns javascript which creates an instance of the class defined in formJavascriptClass()
*
* @param int $repeatCounter Repeat group counter
*
* @return array
*/
public function elementJavascript($repeatCounter)
{
$params = $this->getParams();
$id = $this->getHTMLId($repeatCounter);
$opts = $this->getElementJSOptions($repeatCounter);
$inputMask = trim($params->get('text_input_mask', ''));
if (!empty($inputMask))
{
$opts->use_input_mask = true;
$opts->input_mask = $inputMask;
$opts->input_mask_definitions = $params->get('text_input_mask_definitions', '{}');
$opts->input_mask_autoclear = $params->get('text_input_mask_autoclear', '0') === '1';
}
else
{
$opts->use_input_mask = false;
$opts->input_mask = '';
}
$opts->geocomplete = $params->get('autocomplete', '0') === '3';
$config = JComponentHelper::getParams('com_fabrik');
$apiKey = $config->get('google_api_key', '');
$opts->mapKey = empty($apiKey) ? false : $apiKey;
if ($this->getParams()->get('autocomplete', '0') == '2')
{
$autoOpts = array();
$autoOpts['max'] = $this->getParams()->get('autocomplete_rows', '10');
$autoOpts['storeMatchedResultsOnly'] = false;
FabrikHelperHTML::autoComplete($id, $this->getElement()->id, $this->getFormModel()->getId(), 'field', $autoOpts);
}
return array('FbField', $id, $opts);
}
/**
* Get the class to manage the form element
* to ensure that the file is loaded only once
*
* @param array &$srcs Scripts previously loaded
* @param string $script Script to load once class has loaded
* @param array &$shim Dependant class names to load before loading the class - put in requirejs.config shim
*
* @return void|boolean
*/
public function formJavascriptClass(&$srcs, $script = '', &$shim = array())
{
$key = FabrikHelperHTML::isDebug() ? 'element/field/field' : 'element/field/field-min';
$params = $this->getParams();
$inputMask = trim($params->get('text_input_mask', ''));
$geoComplete = $params->get('autocomplete', '0') === '3';
$s = new stdClass;
// Even though fab/element is now an AMD defined module we should still keep it in here
// otherwise (not sure of the reason) jQuery.mask is not defined in field.js
// Seems OK now - reverting to empty array
$s->deps = array();
if (!empty($inputMask))
{
$folder = 'components/com_fabrik/libs/masked_input/';
$s->deps[] = $folder . 'jquery.maskedinput';
}
if ($geoComplete)
{
$folder = 'components/com_fabrik/libs/googlemaps/geocomplete/';
$s->deps[] = $folder . 'jquery.geocomplete';
}
if (array_key_exists($key, $shim))
{
$shim[$key]->deps = array_merge($shim[$key]->deps, $s->deps);
}
else
{
$shim[$key] = $s;
}
parent::formJavascriptClass($srcs, $script, $shim);
// $$$ hugh - added this, and some logic in the view, so we will get called on a per-element basis
return false;
}
/**
* Get database field description
*
* @return string db field type
*/
public function getFieldDescription()
{
$p = $this->getParams();
if ($this->encryptMe())
{
return 'BLOB';
}
switch ($p->get('text_format'))
{
case 'text':
default:
$objType = "VARCHAR(" . $p->get('maxlength', 255) . ")";
break;
case 'integer':
$objType = "INT(" . $p->get('integer_length', 11) . ")";
break;
case 'decimal':
$total = (int) $p->get('integer_length', 11) + (int) $p->get('decimal_length', 2);
$objType = "DECIMAL(" . $total . "," . $p->get('decimal_length', 2) . ")";
break;
}
return $objType;
}
/**
* Get Joomfish options
*
* @deprecated - not supporting joomfish
*
* @return array key=>value options
*/
public function getJoomfishOptions()
{
$params = $this->getParams();
$return = array();
$size = (int) $this->getElement()->width;
$maxLength = (int) $params->get('maxlength');
if ($size !== 0)
{
$return['length'] = $size;
}
if ($maxLength === 0)
{
$maxLength = $size;
}
if ($params->get('textarea-showmax') && $maxLength !== 0)
{
$return['maxlength'] = $maxLength;
}
return $return;
}
/**
* Can the element plugin encrypt data
*
* @return bool
*/
public function canEncrypt()
{
return true;
}
/**
* 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)
{
if (is_array($val))
{
foreach ($val as $k => $v)
{
$val[$k] = $this->_indStoreDatabaseFormat($v);
}
$val = implode(GROUPSPLITTER, $val);
}
else
{
$val = $this->_indStoreDatabaseFormat($val);
}
return $val;
}
/**
* Manipulates individual values posted form data for insertion into database
*
* @param string $val This elements posted form data
*
* @return string
*/
protected function _indStoreDatabaseFormat($val)
{
return $this->unNumberFormat($val);
}
/**
* Get the element's cell class
*
* @since 3.0.4
*
* @return string css classes
*/
public function getCellClass()
{
$params = $this->getParams();
$classes = parent::getCellClass();
$format = $params->get('text_format');
if ($format == 'decimal' || $format == 'integer')
{
$classes .= ' ' . $format;
}
return $classes;
}
/**
* Output a QR Code image
*
* @since 3.1
*/
public function onAjax_renderQRCode()
{
$input = $this->app->input;
$this->setId($input->getInt('element_id'));
$this->loadMeForAjax();
$this->getElement();
$url = 'index.php';
$this->lang->load('com_fabrik.plg.element.field', JPATH_ADMINISTRATOR);
if (!$this->canView())
{
$this->app->enqueueMessage(FText::_('PLG_ELEMENT_FIELD_NO_PERMISSION'));
$this->app->redirect($url);
exit;
}
$rowId = $input->get('rowid', '', 'string');
if (empty($rowId))
{
$this->app->redirect($url);
exit;
}
$listModel = $this->getListModel();
$row = $listModel->getRow($rowId, false);
if (empty($row))
{
$this->app->redirect($url);
exit;
}
$elName = $this->getFullName(true, false);
$value = $row->$elName;
/*
require JPATH_SITE . '/components/com_fabrik/libs/qrcode/qrcode.php';
// Usage: $a=new QR('234DSKJFH23YDFKJHaS');$a->image(4);
$qr = new QR($value);
$img = $qr->image(4);
*/
if (!empty($value))
{
require JPATH_SITE . '/components/com_fabrik/libs/phpqrcode/phpqrcode.php';
ob_start();
QRCode::png($value);
$img = ob_get_contents();
ob_end_clean();
}
if (empty($img))
{
$img = file_get_contents(JPATH_SITE . '/media/system/images/notice-note.png');
}
// Some time in the past
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Accept-Ranges: bytes');
header('Content-Length: ' . strlen($img));
//header('Content-Type: ' . 'image/gif');
// Serve up the file
echo $img;
// And we're done.
exit();
}
/**
* Get a link to this element which will call onAjax_renderQRCode().
*
* @param array|object $thisRow Row data
*
* @since 3.1
*
* @return string QR code link
*/
protected function qrCodeLink($thisRow)
{
if (is_object($thisRow))
{
$thisRow = ArrayHelper::fromObject($thisRow);
}
$formModel = $this->getFormModel();
$formId = $formModel->getId();
$rowId = $formModel->getRowId();
if (empty($rowId))
{
/**
* Meh. See commentary at the start of $formModel->getEmailData() about rowid. For now, if this is a new row,
* the only place we're going to find it is in the list model's lastInsertId. Bah humbug.
* But check __pk_val first anyway, what the heck.
*/
$rowId = FArrayHelper::getValue($thisRow, '__pk_val', '');
if (empty($rowId))
{
/**
* Nope. Try lastInsertId. Or maybe on top of the fridge? Or in the microwave? Down the back
* of the couch cushions?
*/
$rowId = $formModel->getListModel()->lastInsertId;
/**
* OK, give up. If *still* no rowid, we're probably being called from something like getEmailData() on onBeforeProcess or
* onBeforeStore, and it's a new form, so no rowid yet. So no point returning anything yet.
*/
if (empty($rowId))
{
return '';
}
}
}
/*
* YAY!!! w00t!! We have a rowid. Whoop de freakin' doo!!
*/
$elementId = $this->getId();
$src = COM_FABRIK_LIVESITE
. 'index.php?option=com_' . $this->package . '&task=plugin.pluginAjax&plugin=field&method=ajax_renderQRCode&'
. 'format=raw&element_id=' . $elementId . '&formid=' . $formId . '&rowid=' . $rowId . '&repeatcount=0';
$layout = $this->getLayout('qr');
$displayData = new stdClass;
$displayData->src = $src;
return $layout->render($displayData);
}
/**
* Turn form value into email formatted value
*
* @param mixed $value Element value
* @param array $data Form data
* @param int $repeatCounter Group repeat counter
*
* @return string email formatted value
*/
protected function getIndEmailValue($value, $data = array(), $repeatCounter = 0)
{
$params = $this->getParams();
if ($params->get('render_as_qrcode', '0') === '1')
{
return html_entity_decode($this->qrCodeLink($data));
}
else
{
$value = $this->format($value);
return parent::getIndEmailValue($value, $data, $repeatCounter);
}
}
}