1: <?php
2:
3: 4: 5:
6: class Quform_Element_Date extends Quform_Element_Field implements Quform_Element_Editable
7: {
8: 9: 10: 11: 12:
13: public function getValueHtml()
14: {
15: $value = Quform::escape($this->getValueText());
16:
17: $value = apply_filters('quform_get_value_html_' . $this->getIdentifier(), $value, $this, $this->getForm());
18:
19: return $value;
20: }
21:
22: 23: 24: 25: 26: 27:
28: public function getValueText($separator = ', ')
29: {
30: $value = '';
31:
32: if ( ! $this->isEmpty()) {
33: try {
34: $value = Quform::date(
35: $this->config('dateFormat'),
36: new DateTime($this->getValue(), new DateTimeZone('UTC')),
37: new DateTimeZone('UTC')
38: );
39: } catch (Exception $e) {
40:
41: }
42: }
43:
44: $value = apply_filters('quform_get_value_text_' . $this->getIdentifier(), $value, $this, $this->getForm());
45:
46: return $value;
47: }
48:
49: 50: 51: 52: 53: 54:
55: protected function isValidValue($value)
56: {
57: if ( ! is_string($value)) {
58: return false;
59: }
60:
61: if ($value == '9999-99-99') {
62: return false;
63: }
64:
65: return preg_match('/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/', $value);
66: }
67:
68: 69: 70: 71: 72: 73: 74: 75:
76: public function prepareDynamicValue($value)
77: {
78: if (Quform::isNonEmptyString($value)) {
79: $parts = explode('-', $value);
80:
81: if (isset($parts[0], $parts[1], $parts[2])) {
82: $year = (int) $parts[0];
83: $month = (int) $parts[1];
84: $day = (int) $parts[2];
85:
86: if (checkdate($month, $day, $year)) {
87: try {
88: return Quform::date(
89: 'Y-m-d',
90: new DateTime("$year-$month-$day", new DateTimeZone('UTC')),
91: new DateTimeZone('UTC')
92: );
93: } catch (Exception $e) {
94:
95: }
96: }
97: }
98: }
99:
100: return $this->getEmptyValue();
101: }
102:
103: 104: 105: 106: 107: 108:
109: protected function getFieldAttributes(array $context = array())
110: {
111: $attributes = array(
112: 'type' => 'text',
113: 'id' => $this->getUniqueId(),
114: 'name' => $this->getFullyQualifiedName(),
115: 'class' => Quform::sanitizeClass($this->getFieldClasses($context)),
116: 'placeholder' => 'YYYY-MM-DD',
117: 'data-options' => wp_json_encode($this->getDatepickerOptions())
118: );
119:
120: if ( ! $this->isEmpty()) {
121: $attributes['value'] = $this->getValue();
122: }
123:
124: if ($this->config('readOnly')) {
125: $attributes['readonly'] = true;
126: }
127:
128: $attributes = apply_filters('quform_field_attributes', $attributes, $this, $this->form, $context);
129: $attributes = apply_filters('quform_field_attributes_' . $this->getIdentifier(), $attributes, $this, $this->form, $context);
130:
131: return $attributes;
132: }
133:
134: 135: 136: 137: 138:
139: protected function getDatepickerOptions()
140: {
141: $options = array(
142: 'format' => $this->config('dateFormatJs'),
143: 'min' => $this->config('dateMin'),
144: 'max' => $this->config('dateMax'),
145: 'start' => $this->config('dateViewStart'),
146: 'depth' => $this->config('dateViewDepth'),
147: 'showFooter' => $this->config('dateShowFooter'),
148: 'locale' => $this->config('dateLocale'),
149: 'placeholder' => $this->config('placeholder'),
150: 'autoOpen' => $this->config('dateAutoOpen'),
151: 'identifier' => $this->getIdentifier()
152: );
153:
154: return $options;
155: }
156:
157: 158: 159: 160: 161: 162:
163: protected function getFieldClasses(array $context = array())
164: {
165: $classes = array(
166: 'quform-field',
167: 'quform-field-date',
168: sprintf('quform-field-%s', $this->getIdentifier())
169: );
170:
171: if ($this->form->config('tooltipsEnabled') && Quform::isNonEmptyString($this->config('tooltip')) && Quform::get($context, 'tooltipType') == 'field') {
172: $classes[] = sprintf('quform-tooltip-%s', Quform::get($context, 'tooltipEvent'));
173: }
174:
175: if (Quform::isNonEmptyString($this->config('customClass'))) {
176: $classes[] = $this->config('customClass');
177: }
178:
179: $classes = apply_filters('quform_field_classes', $classes, $this, $this->form, $context);
180: $classes = apply_filters('quform_field_classes_' . $this->getIdentifier(), $classes, $this, $this->form, $context);
181:
182: return $classes;
183: }
184:
185: 186: 187: 188: 189: 190:
191: protected function getFieldHtml(array $context = array())
192: {
193: return Quform::getHtmlTag('input', $this->getFieldAttributes($context));
194: }
195:
196: 197: 198: 199: 200:
201: public function getEditFieldHtml()
202: {
203: return $this->getFieldHtml();
204: }
205:
206: 207: 208: 209: 210: 211:
212: protected function renderCss(array $context = array())
213: {
214: $css = parent::renderCss($context);
215:
216: if ($context['fieldWidth'] == 'custom' && Quform::isNonEmptyString($context['fieldWidthCustom'])) {
217: $css .= sprintf('.quform-input-date.quform-input-%s { width: %s; }', $this->getIdentifier(), Quform::addCssUnit($context['fieldWidthCustom']));
218: $css .= sprintf('.quform-inner-%s > .quform-error > .quform-error-inner { float: left; min-width: %s; }', $this->getIdentifier(), Quform::addCssUnit($context['fieldWidthCustom']));
219: }
220:
221: return $css;
222: }
223:
224: 225: 226: 227: 228:
229: protected function getCssSelectors()
230: {
231: return parent::getCssSelectors() + array(
232: 'datepickerHeader' => '%1$s-datepicker.quform-%2$s-datepicker.quform-datepicker .k-calendar .k-header, %1$s-datepicker.quform-%2$s-datepicker .k-calendar .k-header .k-state-hover',
233: 'datepickerHeaderText' => '%s-datepicker.quform-%s-datepicker.quform-datepicker .k-calendar .k-header .k-link',
234: 'datepickerHeaderTextHover' => '%s-datepicker.quform-%s-datepicker.quform-datepicker .k-calendar .k-header .k-link:hover',
235: 'datepickerFooter' => '%s-datepicker.quform-%s-datepicker.quform-datepicker .k-calendar .k-footer',
236: 'datepickerFooterText' => '%s-datepicker.quform-%s-datepicker.quform-datepicker .k-calendar .k-footer .k-link',
237: 'datepickerFooterTextHover' => '%s-datepicker.quform-%s-datepicker.quform-datepicker .k-calendar .k-footer .k-link:hover',
238: 'datepickerSelectionText' => '%s-datepicker.quform-%s-datepicker.quform-datepicker .k-calendar td.k-state-focused .k-link',
239: 'datepickerSelectionTextHover' => '%s-datepicker.quform-%s-datepicker.quform-datepicker .k-calendar td.k-state-focused .k-link:hover',
240: 'datepickerSelectionActiveText' => '%s-datepicker.quform-%s-datepicker.quform-datepicker .k-calendar td.k-state-selected.k-state-focused .k-link',
241: 'datepickerSelectionActiveTextHover' => '%s-datepicker.quform-%s-datepicker.quform-datepicker .k-calendar td.k-state-selected.k-state-focused .k-link:hover',
242: 'datepickerSelection' => '%s-datepicker.quform-%s-datepicker.quform-datepicker .k-calendar td.k-state-focused',
243: 'datepickerSelectionActive' => '%s-datepicker.quform-%s-datepicker.quform-datepicker .k-calendar td.k-state-selected.k-state-focused'
244: );
245: }
246:
247: 248: 249: 250: 251: 252: 253:
254: protected function isLogicValueMatch($value, array $rule)
255: {
256: if ($rule['operator'] == 'gt' || $rule['operator'] == 'lt') {
257: if ( ! $this->isEmpty()) {
258: $result = Quform::compareDates($value, $rule['value']);
259:
260: if ($result !== false) {
261: return $rule['operator'] == 'gt' ? $result === 1 : $result === -1;
262: }
263: }
264:
265: return false;
266: }
267:
268: return parent::isLogicValueMatch($value, $rule);
269: }
270:
271: 272: 273: 274: 275: 276:
277: public static function getDefaultConfig($key = null)
278: {
279: $config = apply_filters('quform_default_config_date', array(
280:
281: 'label' => __('Date', 'quform'),
282: 'description' => '',
283: 'descriptionAbove' => '',
284: 'required' => false,
285:
286:
287: 'labelIcon' => '',
288: 'fieldIconLeft' => '',
289: 'fieldIconRight' => 'qicon-calendar',
290: 'fieldSize' => 'inherit',
291: 'fieldWidth' => 'inherit',
292: 'fieldWidthCustom' => '',
293: 'customClass' => '',
294: 'customElementClass' => '',
295: 'styles' => array(),
296:
297:
298: 'placeholder' => '',
299: 'subLabel' => '',
300: 'subLabelAbove' => '',
301: 'adminLabel' => '',
302: 'tooltip' => '',
303: 'tooltipType' => 'inherit',
304: 'tooltipEvent' => 'inherit',
305: 'labelPosition' => 'inherit',
306: 'labelWidth' => '',
307:
308:
309: 'logicEnabled' => false,
310: 'logicAction' => true,
311: 'logicMatch' => 'all',
312: 'logicRules' => array(),
313:
314:
315: 'defaultValue' => '',
316: 'dynamicDefaultValue' => false,
317: 'dynamicKey' => '',
318: 'dateMin' => '',
319: 'dateMax' => '',
320: 'dateViewStart' => 'month',
321: 'dateViewDepth' => 'month',
322: 'dateShowFooter' => false,
323: 'dateLocale' => '',
324: 'dateFormatJs' => '',
325: 'dateFormat' => '',
326: 'dateAutoOpen' => true,
327: 'readOnly' => false,
328: 'showInEmail' => true,
329: 'saveToDatabase' => true,
330:
331:
332: 'visibility' => '',
333: 'validators' => array(),
334:
335:
336: 'messageRequired' => '',
337: 'messageDateInvalidDate' => '',
338: 'messageDateTooEarly' => '',
339: 'messageDateTooLate' => ''
340: ));
341:
342: $config['type'] = 'date';
343:
344: if (Quform::isNonEmptyString($key)) {
345: return Quform::get($config, $key);
346: }
347:
348: return $config;
349: }
350: }
351: