PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.21
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.21
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / FormBuilder / Components / DateTime.php

DateTime.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 3.6.21, at app/Services/FormBuilder/Components/DateTime.php

163 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Services\FormBuilder\Components;
4
5 use FluentForm\App\Helpers\Helper;
6 use FluentForm\Framework\Helpers\ArrayHelper;
7
8 class DateTime extends BaseComponent
9 {
10 /**
11 * Compile and echo the html element
12 * @param array $data [element data]
13 * @param stdClass $form [Form Object]
14 * @return viod
15 */
16 public function compile($data, $form)
17 {
18 $elementName = $data['element'];
19 $data = apply_filters('fluenform_rendering_field_data_' . $elementName, $data, $form);
20
21 wp_enqueue_script('flatpickr');
22 wp_enqueue_style('flatpickr');
23
24 $data['attributes']['class'] = trim(
25 'ff-el-form-control ff-el-datepicker ' . $data['attributes']['class']
26 );
27 $dateFormat = $data['settings']['date_format'];
28
29 $data['attributes']['id'] = $this->makeElementId($data, $form);
30
31 if($tabIndex = \FluentForm\App\Helpers\Helper::getNextTabIndex()) {
32 $data['attributes']['tabindex'] = $tabIndex;
33 }
34
35 $elMarkup = "<input data-type-datepicker data-format='" . $dateFormat . "' " . $this->buildAttributes($data['attributes']) . ">";
36
37 $this->getDateFormatConfigJSON($data['settings'], $form, $data['attributes']['id']);
38
39 $html = $this->buildElementMarkup($elMarkup, $data, $form);
40 echo apply_filters('fluenform_rendering_field_html_' . $elementName, $html, $data, $form);
41 }
42
43
44 public function getAvailableDateFormats()
45 {
46 $dateFormats = apply_filters('fluentform/available_date_formats', array(
47 'm/d/Y' => 'm/d/Y - (Ex: 04/28/2018)', // USA
48 'd/m/Y' => 'd/m/Y - (Ex: 28/04/2018)', // Canada, UK
49 'd.m.Y' => 'd.m.Y - (Ex: 28.04.2019)', // Germany
50 'n/j/y' => 'n/j/y - (Ex: 4/28/18)',
51 'm/d/y' => 'm/d/y - (Ex: 04/28/18)',
52 'M/d/Y' => 'M/d/Y - (Ex: Apr/28/2018)',
53 'y/m/d' => 'y/m/d - (Ex: 18/04/28)',
54 'Y-m-d' => 'Y-m-d - (Ex: 2018-04-28)',
55 'd-M-y' => 'd-M-y - (Ex: 28-Apr-18)',
56 'm/d/Y h:i K' => 'm/d/Y h:i K - (Ex: 04/28/2018 08:55 PM)', // USA
57 'm/d/Y H:i' => 'm/d/Y H:i - (Ex: 04/28/2018 20:55)', // USA
58 'd/m/Y h:i K' => 'd/m/Y h:i K - (Ex: 28/04/2018 08:55 PM)', // Canada, UK
59 'd/m/Y H:i' => 'd/m/Y H:i - (Ex: 28/04/2018 20:55)', // Canada, UK
60 'd.m.Y h:i K' => 'd.m.Y h:i K - (Ex: 28.04.2019 08:55 PM)', // Germany
61 'd.m.Y H:i' => 'd.m.Y H:i - (Ex: 28.04.2019 20:55)', // Germany
62 'h:i K' => 'h:i K (Only Time Ex: 08:55 PM)',
63 'H:i' => 'H:i (Only Time Ex: 20:55)',
64 ));
65
66 $formatted = [];
67 foreach ($dateFormats as $format => $label) {
68 $formatted[] = [
69 'label' => $label,
70 'value' => $format
71 ];
72 }
73 return $formatted;
74 }
75
76 private function getDateFormatConfigJSON($settings, $form, $id)
77 {
78 $dateFormat = ArrayHelper::get($settings, 'date_format');
79 if (!$dateFormat) {
80 $dateFormat = 'm/d/Y';
81 }
82
83 $customConfigObject = trim(ArrayHelper::get($settings, 'date_config'));
84
85 if(!$customConfigObject || substr($customConfigObject, 0, 1) != '{' || substr($customConfigObject, -1) != '}') {
86 $customConfigObject = '{}';
87 }
88
89 $hasTime = $this->hasTime($dateFormat);
90 $time24 = false;
91
92 if($hasTime && strpos($dateFormat, 'H') !== false) {
93 $time24 = true;
94 }
95
96 $config = apply_filters('fluentform/frontend_date_format', array(
97 'dateFormat' => $dateFormat,
98 'enableTime' => $hasTime,
99 'noCalendar' => !$this->hasDate($dateFormat),
100 'disableMobile' => true,
101 'time_24hr' => $time24
102 ), $settings, $form);
103
104 $config = json_encode($config, JSON_FORCE_OBJECT);
105
106 add_action('wp_footer', function () use ($config, $customConfigObject, $id, $form) {
107 ?>
108 <script type="text/javascript">
109 jQuery(document).ready(function ($) {
110 function initPicker() {
111 if(typeof flatpickr == 'undefined') {
112 return;
113 }
114 flatpickr.localize(window.fluentFormVars.date_i18n);
115 var config = <?php echo $config; ?>;
116 try {
117 var customConfig = <?php echo $customConfigObject; ?>;
118 } catch (e) {
119 var customConfig = {};
120 }
121
122 var config = $.extend({}, config, customConfig);
123 if (!config.locale) {
124 config.locale = 'default';
125 }
126
127 if(jQuery('#<?php echo $id; ?>').length) {
128 flatpickr('#<?php echo $id; ?>', config);
129 }
130 }
131 initPicker();
132 $(document).on('reInitExtras', '.<?php echo $form->instance_css_class; ?>', function () {
133 initPicker();
134 });
135 });
136 </script>
137 <?php
138 }, 99999);
139 }
140
141 private function hasTime($string)
142 {
143 $timeStrings = ['H', 'h', 'G', 'i', 'S', 's', 'K'];
144 foreach ($timeStrings as $timeString) {
145 if (strpos($string, $timeString) != false) {
146 return true;
147 }
148 }
149 return false;
150 }
151
152 private function hasDate($string)
153 {
154 $dateStrings = ['d', 'D', 'l', 'j', 'J', 'w', 'W', 'F', 'm', 'n', 'M', 'U', 'Y', 'y', 'Z'];
155 foreach ($dateStrings as $dateString) {
156 if (strpos($string, $dateString) != false) {
157 return 'true';
158 }
159 }
160 return false;
161 }
162 }
163