forked from pollen8/fabrik
-
Notifications
You must be signed in to change notification settings - Fork 381
/
date.php
2499 lines (2145 loc) · 66.2 KB
/
date.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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin element to render date picker
*
* @package Joomla.Plugin
* @subpackage Fabrik.element.date
* @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');
/**
* Plugin element to render date picker
*
* @package Joomla.Plugin
* @subpackage Fabrik.element.date
* @since 3.0
*/
class PlgFabrik_ElementDate extends PlgFabrik_ElementList
{
/**
* States the element should be ignored from advanced search all queries.
*
* @var bool True, ignore in extended search all.
*/
protected $ignoreSearchAllDefault = true;
/**
* Toggle to determine if storeDatabaseFormat resets the date to GMT
*
* @var bool
*/
protected $resetToGMT = true;
/**
* Is the element a ranged filter (can depend on request data)
*
* @var bool
*/
protected $rangeFilterSet = false;
/**
* Date offset with TZ, for use in front end display
*
* @var string
*/
protected $offsetDate = null;
/**
* Does the element contain sub elements e.g checkboxes radiobuttons
*
* @var bool
*/
public $hasSubElements = false;
/**
* Shows the data formatted for the list view
*
* @param string $data Elements data
* @param object &$thisRow All the data in the lists current row
*
* @return string formatted value
*/
public function renderListData($data, &$thisRow)
{
if ($data == '')
{
return '';
}
// @TODO: deal with time options (currently can be defined in date_table_format param).
$timeZone = new DateTimeZone(JFactory::getConfig()->get('offset'));
$params = $this->getParams();
$store_as_local = (int) $params->get('date_store_as_local', 0);
$groupModel = $this->getGroup();
$data = FabrikWorker::JSONtoData($data, true);
$f = $params->get('date_table_format', '%Y-%m-%d');
if ($f == 'Y-m-d')
{
$f = '%Y-%m-%d';
}
$format = array();
foreach ($data as $d)
{
if (FabrikWorker::isDate($d))
{
$date = JFactory::getDate($d);
if ($this->shouldApplyTz('list'))
{
$date->setTimeZone($timeZone);
}
if ($f == '{age}')
{
$format[] = date('Y') - $date->format('Y', true);
}
else
{
$format[] = $date->toFormat($f, true);
}
}
else
{
$format[] = '';
}
}
$data = json_encode($format);
return parent::renderListData($data, $thisRow);
}
/**
* 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 value
*/
public function renderListData_csv($data, &$thisRow)
{
// @TODO: deal with time options (currently can be defined in date_table_format param).
$timeZone = new DateTimeZone(JFactory::getConfig()->get('offset'));
$db = FabrikWorker::getDbo();
$params = $this->getParams();
$element = $this->getElement();
$store_as_local = (int) $params->get('date_store_as_local', 0);
$groupModel = $this->getGroup();
$data = FabrikWorker::JSONtoData($data, true);
$f = $params->get('date_table_format', '%Y-%m-%d');
/* $$$ hugh - see http://fabrikar.com/forums/showthread.php?p=87507
* Really don't think we need to worry about $app->input 'incraw' here. The raw, GMT/MySQL data will get
* included in the _raw version of the element if incraw is selected. Here we just want to output
* the regular non-raw, formatted, TZ'ed version.
*/
$incRaw = false;
if ($f == 'Y-m-d')
{
$f = '%Y-%m-%d';
}
$format = array();
foreach ($data as $d)
{
if (FabrikWorker::isDate($d))
{
if ($incRaw)
{
$format[] = $d;
}
else
{
$date = JFactory::getDate($d);
/* $$$ hugh - added the showtime test so we don't get the day offset issue,
* as per regular table render.
*/
if ($params->get('date_showtime') && !$store_as_local)
{
$date->setTimeZone($timeZone);
}
if ($f == '{age}')
{
$format[] = date('Y') - $date->format('Y', true);
}
else
{
$format[] = $date->toFormat($f, true);
}
}
}
else
{
$format[] = '';
}
}
if (count($format) > 1)
{
return json_encode($format);
}
else
{
return implode('', $format);
}
}
/**
* Used in things like date when its id is suffixed with _cal
* called from getLabel();
*
* @param string &$id Initial id
*
* @return void
*/
protected function modHTMLId(&$id)
{
$id = $id . '_cal';
}
/**
* Called by form model to build an array of values to encrypt
*
* Dates should have tz applied if needed - their value should be the
* same as the displayed readonly date.
*
* @param array &$values Previously encrypted values
* @param array $data Form data
* @param int $c Repeat group counter
*
* @return void
*/
public function getValuesToEncrypt(&$values, $data, $c)
{
$name = $this->getFullName(false, true, false);
$group = $this->getGroup();
if ($group->canRepeat())
{
if (!array_key_exists($name, $values))
{
$values[$name]['data'] = array();
}
$gmt = $this->getValue($data, $c);
$localDate = $this->displayDate($gmt);
$values[$name]['data'][$c] = FabrikWorker::isDate($gmt) ? $localDate->toSql(true) : '';
}
else
{
$gmt = $this->getValue($data, $c);
$localDate = $this->displayDate($gmt);
$values[$name]['data'] = FabrikWorker::isDate($gmt) ? $localDate->toSql(true) : '';
}
}
/**
* 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)
{
$app = JFactory::getApplication();
$input = $app->input;
$j3 = FabrikWorker::j3();
$this->offsetDate = '';
FabrikHelperHTML::loadcalendar();
$name = $this->getHTMLName($repeatCounter);
$id = $this->getHTMLId($repeatCounter);
$params = $this->getParams();
$element = $this->getElement();
$format = $params->get('date_form_format', $params->get('date_table_format', '%Y-%m-%d'));
$timeformat = $params->get('date_time_format');
// Value is in mySQL format GMT
$gmt = $this->getValue($data, $repeatCounter);
if (!FabrikWorker::isDate($gmt))
{
$date = '';
$time = '';
}
else
{
// Set the date to display local time
$localDate = $this->displayDate($gmt);
// Get the formatted time
$time = ($params->get('date_showtime', 0)) ? ' ' . $localDate->toFormat($timeformat, true) : '';
// Formatted date
$date = $localDate->toFormat($format, true);
}
if (!$this->isEditable())
{
return $date . $time;
}
// Build HTML widget
if ($params->get('date_showtime', 0) && !$element->hidden)
{
// Can't have names as simply [] as json only picks up the last one
$timeElName = $name . '[time]';
$name .= '[date]';
}
$class = 'fabrikinput inputbox inout ' . $params->get('bootstrap_class', 'input-small');
$element->width = (int) $element->width < 0 ? 1 : (int) $element->width;
$calopts = array('class' => $class, 'size' => $element->width, 'maxlength' => '19');
$readonly = '';
if ($params->get('date_allow_typing_in_field', true) == false)
{
$readonly = ' readonly="readonly" ';
$calopts['readonly'] = 'readonly';
}
$str[] = '<div class="fabrikSubElementContainer" id="' . $id . '">';
$str[] = $this->calendar($date, $name, $id . '_cal', $format, $calopts, $repeatCounter);
if ($params->get('date_showtime', 0) && !$element->hidden)
{
$this->timeButton($timeElName, $time, $str);
}
$str[] = '</div>';
return implode("\n", $str);
}
/**
* Render time button
*
* @param string $timeElName Element name
* @param string $time Time value
* @param array &$str Markup to append time button to
*
* @since 3.1b2
*
* @return void
*/
protected function timeButton($timeElName, $time, &$str)
{
$j3 = FabrikWorker::j3();
$params = $this->getParams();
$timeformat = $params->get('date_time_format', 'H:i');
$class = 'inputbox fabrikinput timeField input ' . $params->get('bootstrap_time_class', 'input-mini');
$readonly = $params->get('date_allow_typing_in_field', true) == false ? ' readonly="readonly" ' : '';
if ($j3)
{
$str[] = '<div class="input-append">';
}
$timelength = JString::strlen($timeformat);
FabrikHelperHTML::addPath(COM_FABRIK_BASE . 'plugins/fabrik_element/date/images/', 'image', 'form', false);
$str[] = '<input type="text" class="' . $class . '" ' . $readonly . ' size="' . $timelength . '" value="' . $time . '" name="'
. $timeElName . '" />';
$opts = array('alt' => JText::_('PLG_ELEMENT_DATE_TIME'), 'class' => 'timeButton');
$file = FabrikWorker::j3() ? 'clock.png' : 'time.png';
$img = FabrikHelperHTML::image($file, 'form', @$this->tmpl, $opts);
if ($j3)
{
$img = '<button class="btn timeButton">' . $img . '</button>';
}
$str[] = $img;
if ($j3)
{
$str[] = '</div>';
}
}
/**
* Create the local datetime
*
* @param string $gmt Datetime
*
* @since 3.0.9
*
* @return date
*/
protected function displayDate($gmt)
{
$date = JFactory::getDate($gmt);
if ($this->shouldApplyTz())
{
$timeZone = new DateTimeZone(JFactory::getConfig()->get('offset'));
$date->setTimeZone($timeZone);
}
$this->offsetDate = $date->toSql(true);
return $date;
}
/**
* Should we apply the users timezone offset to the date for display purposes
*
* This is a bit long winded and could be reduced. BUT the logic is such that I think the
* verbosity maintains the readability of the logic
*
* @param string $view Component view [list/form]
*
* @since 3.0.9
*
* @return boolean
*/
protected function shouldApplyTz($view = 'form')
{
$formModel = $this->getFormModel();
$params = $this->getParams();
$store_as_local = (bool) $params->get('date_store_as_local', 0);
$newRecord = $formModel->isNewRecord();
$alwaysToday = $params->get('date_alwaystoday', false);
$defaultToday = $params->get('date_defaulttotoday');
if ($store_as_local)
{
if ($view === 'list')
{
return false;
}
if ($newRecord)
{
if (($alwaysToday || $defaultToday) || $formModel->hasErrors())
{
// User supplied defaults should be in GMT, they are only applied if no other default found.
return true;
}
}
if (!$newRecord)
{
return true;
}
}
else
{
if ($newRecord)
{
if (($alwaysToday || $defaultToday) || $formModel->hasErrors() || $view === 'list')
{
// User supplied defaults should be in GMT, they are only applied if no other default found.
return true;
}
}
if (!$newRecord)
{
return true;
}
}
return false;
}
/**
* Individual store database format
*
* @param string $val Value
*
* @return string MySQL formatted date
*/
private function _indStoreDBFormat($val)
{
$params = $this->getParams();
$timeZone = new DateTimeZone(JFactory::getConfig()->get('offset'));
$store_as_local = (bool) $params->get('date_store_as_local', false);
$alwaysToday = $params->get('date_alwaystoday', false);
if ($alwaysToday)
{
$val = JFactory::getDate('now', $timeZone)->toSql($store_as_local);
return $val;
}
// $$$ hugh - sometimes still getting $val as an array with date and time,
// like on AJAX submissions? Or maybe from getEmailData()? Or both?
if (is_array($val))
{
$val = JArrayHelper::getValue($val, 'date', '');
}
else
{
$val = urldecode($val);
}
if (!FabrikWorker::isDate($val))
{
return '';
}
jimport('joomla.utilities.date');
$listModel = $this->getListModel();
// $$$ hugh - offset_tz of 1 means 'in MySQL format, GMT'
// $$$ hugh - offset_tz of 2 means 'in MySQL format, Local TZ'
if ($listModel->importingCSV && $params->get('date_csv_offset_tz', '0') == '1')
{
return $val;
}
elseif ($listModel->importingCSV && $params->get('date_csv_offset_tz', '0') == '2')
{
return $this->toMySQLGMT(JFactory::getDate($val));
}
// $$$ rob - as the date js code formats to the db format - just return the value.
$timeZone = new DateTimeZone(JFactory::getConfig()->get('offset'));
$val = JFactory::getDate($val, $timeZone)->toSql($store_as_local);
return $val;
}
/**
* reset the date to GMT - reversing the offset
*
* @param object $date Date to convert
*
* @return string MySQL formatted GMT date
*/
protected function toMySQLGMT($date)
{
if ($this->resetToGMT)
{
// $$$ rob 3.0 offset is no longer an integer but a timezone string
$timeZone = new DateTimeZone(JFactory::getConfig()->get('offset'));
$hours = $timeZone->getOffset($date) / (60 * 60);
$invert = false;
if ($hours < 0)
{
$invert = true;
// Intervals can only be positive - set invert property
$hours = $hours * -1;
}
// 5.3 only
if (class_exists('DateInterval'))
{
$dateInterval = new DateInterval('PT' . $hours . 'H');
$dateInterval->invert = $invert;
$date->sub($dateInterval);
}
else
{
$date->modify('+' . $hours . ' hour');
}
return $date->toSql(true);
}
return $date->toSql();
}
/**
* 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))
{
/* $$$ hugh - we really need to work out why some AJAX data is not getting urldecoded.
* but for now ... a bandaid.
*/
$val = urldecode($val);
}
// @TODO: deal with failed validations
$groupModel = $this->getGroup();
if ($groupModel->isJoin() && is_array($val))
{
$val = JArrayHelper::getValue($val, 'date', '');
}
else
{
if ($groupModel->canRepeat())
{
if (is_array($val))
{
$res = array();
foreach ($val as $v)
{
$res[] = $this->_indStoreDBFormat($v);
}
return json_encode($res);
}
}
}
return $this->_indStoreDBFormat($val);
}
/**
* 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)
{
if ((is_array($value) && empty($value)) || (!is_array($value) && trim($value) == ''))
{
return '';
}
$groupModel = $this->getGroup();
if ($groupModel->isJoin() && $groupModel->canRepeat())
{
$value = $value[$repeatCounter];
}
if (is_array($value))
{
$date = JArrayHelper::getValue($value, 'date');
$d = JFactory::getDate($date);
$time = JArrayHelper::getValue($value, 'time', '');
if ($time !== '')
{
$bits = explode(':', $time);
$h = JArrayHelper::getValue($bits, 0, 0);
$m = JArrayHelper::getValue($bits, 1, 0);
$s = JArrayHelper::getValue($bits, 2, 0);
$d->setTime($h, $m, $s);
}
$value = $d->toSql();
}
// $$$ hugh - need to convert to database format so we GMT-ified date
return $this->renderListData($value, new stdClass);
/* $$$ rob - no need to covert to db format now as its posted as db format already.
*return $this->renderListData($this->storeDatabaseFormat($value, $data), new stdClass);
*/
}
/**
* Determines the label used for the browser title
* in the form/detail views
*
* @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
*
* @return string Text to add to the browser's title
*/
public function getTitlePart($data, $repeatCounter = 0, $opts = array())
{
$gmt_date = $this->getValue($data, $repeatCounter, $opts);
/* OK, now we've got the GMT date, convert it
* ripped the following off from renderListData ... SURELY we must have a func
* somewhere that does this?
*/
$params = $this->getParams();
$store_as_local = (int) $params->get('date_store_as_local', 0);
$timeZone = new DateTimeZone(JFactory::getConfig()->get('offset'));
$f = $params->get('date_table_format', '%Y-%m-%d');
if ($f == 'Y-m-d')
{
$f = '%Y-%m-%d';
}
$tz_date = '';
if (FabrikWorker::isDate($gmt_date))
{
$date = JFactory::getDate($gmt_date);
if (!$store_as_local)
{
$date->setTimeZone($timeZone);
}
if ($f == '{age}')
{
$tz_date = date('Y') - $date->format('Y', true);
}
else
{
$tz_date = $date->toFormat($f, true);
}
}
return $tz_date;
}
/**
* Converts a raw value into its label equivalent
*
* @param string &$v Raw value
*
* @return void
*/
protected function toLabel(&$v)
{
$params = $this->getParams();
$store_as_local = (int) $params->get('date_store_as_local', 0);
$f = $params->get('date_table_format', '%Y-%m-%d');
$timeZone = new DateTimeZone(JFactory::getConfig()->get('offset'));
$format = array();
if (FabrikWorker::isDate($v))
{
$date = JFactory::getDate($v);
/**
* $$$ rob - if not time selector then the date gets stored as 2009-11-13 00:00:00
* if we have a -1 timezone then date gets set to 2009-11-12 23:00:00
* then shown as 2009-11-12 which is wrong
*/
if ($params->get('date_showtime'))
{
$date->setTimeZone($timeZone);
}
if ($f == '{age}')
{
$v = date('Y') - $date->format('Y', true);
}
else
{
$v = $date->toFormat($f, true);
}
}
else
{
$v = '';
}
}
/**
* Displays a calendar control field
*
* hacked from behaviour as you need to check if the element exists
* it might not as you could be using a custom template
*
* @param string $value The date value (must be in the same format as supplied by $format)
* @param string $name The name of the text field
* @param string $id The id of the text field
* @param string $format The date format (not used)
* @param array $attribs Additional html attributes
* @param int $repeatCounter Repeat group counter (not used)
*
* @deprecated - don't think its used
*
* @return string
*/
public function calendar($value, $name, $id, $format = '%Y-%m-%d', $attribs = null, $repeatCounter = 0)
{
FabrikHelperHTML::loadcalendar();
$j3 = FabrikWorker::j3();
if (is_array($attribs))
{
$attribs = JArrayHelper::toString($attribs);
}
$paths = FabrikHelperHTML::addPath(COM_FABRIK_BASE . 'media/system/images/', 'image', 'form', false);
$opts = $j3 ? array('alt' => 'calendar') : array('alt' => 'calendar', 'class' => 'calendarbutton', 'id' => $id . '_cal_img');
$img = FabrikHelperHTML::image('calendar.png', 'form', @$this->tmpl, $opts);
$html = array();
if ($j3)
{
$img = '<button id ="' . $id . '_cal_img" class="btn calendarbutton">' . $img . '</button>';
$html[] = '<div class="input-append">';
}
$html[] = '<input type="text" name="' . $name . '" id="' . $id . '" value="' . $value . '" ' . $attribs . ' />' . $img;
if ($j3)
{
$html[] = '</div>';
}
return implode("\n", $html);
}
/**
* get the options used for the date elements calendar
*
* @param string $id HTML field id
*
* @return object ready for js encoding
*/
protected function _CalendarJSOpts($id)
{
$params = $this->getParams();
$opts = new stdClass;
$opts->inputField = $id;
$opts->button = $id . "_cal_img";
$opts->align = "Tl";
$opts->singleClick = true;
$opts->firstDay = intval($params->get('date_firstday'));
$validations = $this->getValidations();
$opts->ifFormat = $params->get('date_form_format', $params->get('date_table_format', '%Y-%m-%d'));
$opts->timeFormat = 24;
$opts->hasValidations = empty($validations) ? false : true;
$opts->dateAllowFunc = $params->get('date_allow_func');
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();
$element = $this->getElement();
$id = $this->getHTMLId($repeatCounter);
$opts = $this->getElementJSOptions($repeatCounter);
$opts->hidden = (bool) $this->getElement()->hidden;
$opts->defaultVal = $this->offsetDate;
$opts->showtime = (!$element->hidden && $params->get('date_showtime', 0)) ? true : false;
$opts->timelabel = JText::_('time');
$opts->typing = (bool) $params->get('date_allow_typing_in_field', true);
$opts->timedisplay = $params->get('date_timedisplay', 1);
$validations = $this->getValidations();
$opts->validations = empty($validations) ? false : true;
$opts->dateTimeFormat = $params->get('date_time_format', '');
// For reuse if element is duplicated in repeat group
$opts->calendarSetup = $this->_CalendarJSOpts($id);
$opts->advanced = $params->get('date_advanced', '0') == '1';
return array('FbDateTime', $id, $opts);
}
/**
* Get database field description
*
* @return string Db field type
*/
public function getFieldDescription()
{
if ($this->encryptMe())
{
return 'BLOB';
}
return "DATETIME";
}
/**
* Get an array of element html ids and their corresponding
* js events which trigger a validation.
* Examples of where this would be overwritten include timedate element with time field enabled
*
* @param int $repeatCounter Repeat group counter
*
* @return array html ids to watch for validation
*/
public function getValidationWatchElements($repeatCounter)
{
$params = $this->getParams();
$return = array();
$elName = $this->getHTMLName($repeatCounter);
$id = $this->getHTMLId($repeatCounter);
$return[] = array('id' => $id, 'triggerEvent' => 'blur');
return $return;
}
/**
* Element plugin specific method for setting unencrypted values back into post data
*
* @param array &$post Data passed by ref
* @param string $key Key
* @param string $data Elements unencrypted data
*
* @return void
*/
public function setValuesFromEncryt(&$post, $key, $data)
{
$date = $data[0];
if (FabrikWorker::isDate($date))
{
// Only apply date logic to actual date data, if blank for example we should leave blank
$date = JFactory::getDate($date);
$date = $date->toSql();
// Put in the correct format
list($dateOnly, $time) = explode(' ', $date);
}
else
{
$date = '';
$time = '';
}
$data = array('date' => $date, 'time' => $time);
parent::setValuesFromEncryt($post, $key, $data);
}
/**
* Get the GMT Date time - tz offset applied in render() if needed
*
* @param array $data Form data date will be GMT if store as local OFF, otherwise as local time
* @param int $repeatCounter When repeating joined groups we need to know what part of the array to access
* @param array $opts Options
*
* @return string value Date as GMT time
*/
public function getValue($data, $repeatCounter = 0, $opts = array())
{
$params = $this->getParams();
$alwaysToday = $params->get('date_alwaystoday', false);
$defaultToday = $params->get('date_defaulttotoday', false);
$formModel = $this->getFormModel();
$value = parent::getValue($data, $repeatCounter, $opts);
$db = FabrikWorker::getDbo();
$input = JFactory::getApplication()->input;
if (is_array($value))
{
// Submission posted as array but date & time in date key. Can be keyed to 0 if parent class casts string to array.
$value = JArrayHelper::getValue($value, 'date', JArrayHelper::getValue($value, 0));
}
if ($input->get('task') == 'form.process')
{
// Don't mess with posted value - can cause double offsets - instead do in _indStoareDBFormat();
return $value;
}
if (!($formModel->isNewRecord() && $defaultToday) && $value == '')
{
if (($value == '' || $db->getNullDate() == $value) && (!$alwaysToday))
{
return $value;
}
}
// Don't offset if null date.
if ($value === $db->getNullDate())
{
return $value;
}
if ($alwaysToday && $formModel->isEditable())
{
$value = '';
}
$timeZone = new DateTimeZone(JFactory::getConfig()->get('offset'));
$date = JFactory::getDate($value, $timeZone);
// If value = '' dont offset it (not sure what the logic is but testing seems to indicate this to be true)
$local = $formModel->hasErrors() || $value == '' || $params->get('date_store_as_local', 0) == 1 ? false : true;
$value = $date->toSQL($local);
return $value;
}
/**
* Does the format string contain time formatting options
*
* @param string $format Date format
*
* @since 2.1.1
*
* @return bool
*/
protected function formatContainsTime($format)
{
$times = array('%H', '%I', '%l', '%M', '%p', '%P', '%r', '%R', '%S', '%T', '%X', '%z', '%Z');