| 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 |
* |
| 13 |
* @param array $data [element data] |
| 14 |
* @param \stdClass $form [Form Object] |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function compile($data, $form) |
| 19 |
{ |
| 20 |
$elementName = $data['element']; |
| 21 |
$data = apply_filters('fluentform_rendering_field_data_' . $elementName, $data, $form); |
| 22 |
|
| 23 |
wp_enqueue_script('flatpickr'); |
| 24 |
wp_enqueue_style('flatpickr'); |
| 25 |
|
| 26 |
$data['attributes']['class'] = trim( |
| 27 |
'ff-el-form-control ff-el-datepicker ' . $data['attributes']['class'] |
| 28 |
); |
| 29 |
$dateFormat = $data['settings']['date_format']; |
| 30 |
|
| 31 |
$data['attributes']['id'] = $this->makeElementId($data, $form); |
| 32 |
|
| 33 |
if ($tabIndex = Helper::getNextTabIndex()) { |
| 34 |
$data['attributes']['tabindex'] = $tabIndex; |
| 35 |
} |
| 36 |
|
| 37 |
$atts = $this->buildAttributes($data['attributes']); |
| 38 |
|
| 39 |
$elMarkup = "<input data-type-datepicker data-format='" . esc_attr($dateFormat) . "' " . $atts . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $atts is escaped before being passed in. |
| 40 |
|
| 41 |
$config = $this->getDateFormatConfigJSON($data['settings'], $form); |
| 42 |
$customConfig = $this->getCustomConfig($data['settings']); |
| 43 |
$this->loadToFooter($config, $customConfig, $form, $data['attributes']['id']); |
| 44 |
|
| 45 |
$html = $this->buildElementMarkup($elMarkup, $data, $form); |
| 46 |
|
| 47 |
$this->printContent('fluentform_rendering_field_html_' . $elementName, $html, $data, $form); |
| 48 |
} |
| 49 |
|
| 50 |
public function getAvailableDateFormats() |
| 51 |
{ |
| 52 |
$dateFormats = apply_filters('fluentform/available_date_formats', [ |
| 53 |
'm/d/Y' => 'm/d/Y - (Ex: 04/28/2018)', // USA |
| 54 |
'd/m/Y' => 'd/m/Y - (Ex: 28/04/2018)', // Canada, UK |
| 55 |
'd.m.Y' => 'd.m.Y - (Ex: 28.04.2019)', // Germany |
| 56 |
'n/j/y' => 'n/j/y - (Ex: 4/28/18)', |
| 57 |
'm/d/y' => 'm/d/y - (Ex: 04/28/18)', |
| 58 |
'M/d/Y' => 'M/d/Y - (Ex: Apr/28/2018)', |
| 59 |
'y/m/d' => 'y/m/d - (Ex: 18/04/28)', |
| 60 |
'Y-m-d' => 'Y-m-d - (Ex: 2018-04-28)', |
| 61 |
'd-M-y' => 'd-M-y - (Ex: 28-Apr-18)', |
| 62 |
'm/d/Y h:i K' => 'm/d/Y h:i K - (Ex: 04/28/2018 08:55 PM)', // USA |
| 63 |
'm/d/Y H:i' => 'm/d/Y H:i - (Ex: 04/28/2018 20:55)', // USA |
| 64 |
'd/m/Y h:i K' => 'd/m/Y h:i K - (Ex: 28/04/2018 08:55 PM)', // Canada, UK |
| 65 |
'd/m/Y H:i' => 'd/m/Y H:i - (Ex: 28/04/2018 20:55)', // Canada, UK |
| 66 |
'd.m.Y h:i K' => 'd.m.Y h:i K - (Ex: 28.04.2019 08:55 PM)', // Germany |
| 67 |
'd.m.Y H:i' => 'd.m.Y H:i - (Ex: 28.04.2019 20:55)', // Germany |
| 68 |
'h:i K' => 'h:i K (Only Time Ex: 08:55 PM)', |
| 69 |
'H:i' => 'H:i (Only Time Ex: 20:55)', |
| 70 |
]); |
| 71 |
|
| 72 |
$formatted = []; |
| 73 |
foreach ($dateFormats as $format => $label) { |
| 74 |
$formatted[] = [ |
| 75 |
'label' => $label, |
| 76 |
'value' => $format, |
| 77 |
]; |
| 78 |
} |
| 79 |
return $formatted; |
| 80 |
} |
| 81 |
|
| 82 |
public function getDateFormatConfigJSON($settings, $form) |
| 83 |
{ |
| 84 |
$dateFormat = ArrayHelper::get($settings, 'date_format'); |
| 85 |
|
| 86 |
if (! $dateFormat) { |
| 87 |
$dateFormat = 'm/d/Y'; |
| 88 |
} |
| 89 |
|
| 90 |
$hasTime = $this->hasTime($dateFormat); |
| 91 |
$time24 = false; |
| 92 |
|
| 93 |
if ($hasTime && false !== strpos($dateFormat, 'H')) { |
| 94 |
$time24 = true; |
| 95 |
} |
| 96 |
|
| 97 |
$config = apply_filters('fluentform/frontend_date_format', [ |
| 98 |
'dateFormat' => $dateFormat, |
| 99 |
'enableTime' => $hasTime, |
| 100 |
'noCalendar' => ! $this->hasDate($dateFormat), |
| 101 |
'disableMobile' => true, |
| 102 |
'time_24hr' => $time24, |
| 103 |
], $settings, $form); |
| 104 |
|
| 105 |
return json_encode($config, JSON_FORCE_OBJECT); |
| 106 |
} |
| 107 |
|
| 108 |
public function getCustomConfig($settings) |
| 109 |
{ |
| 110 |
$customConfigObject = trim(ArrayHelper::get($settings, 'date_config')); |
| 111 |
|
| 112 |
if (! $customConfigObject || '{' != substr($customConfigObject, 0, 1) || '}' != substr($customConfigObject, -1)) { |
| 113 |
$customConfigObject = '{}'; |
| 114 |
} |
| 115 |
|
| 116 |
return $customConfigObject; |
| 117 |
} |
| 118 |
|
| 119 |
private function loadToFooter($config, $customConfigObject, $form, $id) |
| 120 |
{ |
| 121 |
add_action('wp_footer', function () use ($config, $customConfigObject, $id, $form) { |
| 122 |
?> |
| 123 |
<script type="text/javascript"> |
| 124 |
jQuery(document).ready(function($) { |
| 125 |
function initPicker() { |
| 126 |
if (typeof flatpickr == 'undefined') { |
| 127 |
return; |
| 128 |
} |
| 129 |
flatpickr.localize(window.fluentFormVars.date_i18n); |
| 130 |
var config = <?php echo fluentform_sanitize_html($config, false); ?> ; |
| 131 |
try { |
| 132 |
var customConfig = <?php echo fluentform_sanitize_html($customConfigObject, false); ?> ; |
| 133 |
} catch (e) { |
| 134 |
var customConfig = {}; |
| 135 |
} |
| 136 |
|
| 137 |
var config = $.extend({}, config, customConfig); |
| 138 |
if (!config.locale) { |
| 139 |
config.locale = 'default'; |
| 140 |
} |
| 141 |
|
| 142 |
if (jQuery('#<?php echo esc_attr($id); ?>').length) { |
| 143 |
flatpickr('#<?php echo esc_attr($id); ?>', config); |
| 144 |
} |
| 145 |
} |
| 146 |
initPicker(); |
| 147 |
$(document).on( |
| 148 |
'reInitExtras', |
| 149 |
'.<?php echo esc_attr($form->instance_css_class); ?>', |
| 150 |
function() { |
| 151 |
initPicker(); |
| 152 |
} |
| 153 |
); |
| 154 |
}); |
| 155 |
</script> |
| 156 |
<?php |
| 157 |
}, 99999); |
| 158 |
} |
| 159 |
|
| 160 |
private function hasTime($string) |
| 161 |
{ |
| 162 |
$timeStrings = ['H', 'h', 'G', 'i', 'S', 's', 'K']; |
| 163 |
foreach ($timeStrings as $timeString) { |
| 164 |
if (false != strpos($string, $timeString)) { |
| 165 |
return true; |
| 166 |
} |
| 167 |
} |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
private function hasDate($string) |
| 172 |
{ |
| 173 |
$dateStrings = ['d', 'D', 'l', 'j', 'J', 'w', 'W', 'F', 'm', 'n', 'M', 'U', 'Y', 'y', 'Z']; |
| 174 |
foreach ($dateStrings as $dateString) { |
| 175 |
if (false != strpos($string, $dateString)) { |
| 176 |
return 'true'; |
| 177 |
} |
| 178 |
} |
| 179 |
return false; |
| 180 |
} |
| 181 |
} |
| 182 |
|