1: <?php
  2: 
  3:   4:   5: 
  6: class Quform_Element_Time 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('timeFormat'),
 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 == '99:99') {
 62:             return false;
 63:         }
 64: 
 65:         return preg_match('/^([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])) {
 82:                 $hour = (int) $parts[0];
 83:                 $minute = (int) $parts[1];
 84: 
 85:                 if ($hour >= 0 && $hour <= 23 && $minute >= 0 && $minute <= 59) {
 86:                     try {
 87:                         return Quform::date(
 88:                             'H:i',
 89:                             new DateTime("$hour:$minute", new DateTimeZone('UTC')),
 90:                             new DateTimeZone('UTC')
 91:                         );
 92:                     } catch (Exception $e) {
 93:                         
 94:                     }
 95:                 }
 96:             }
 97:         }
 98: 
 99:         return $this->getEmptyValue();
100:     }
101: 
102:     103: 104: 105: 106: 107: 
108:     protected function getFieldAttributes(array $context = array())
109:     {
110:         $attributes = array(
111:             'type' => 'text',
112:             'id' => $this->getUniqueId(),
113:             'name' => $this->getFullyQualifiedName(),
114:             'class' => Quform::sanitizeClass($this->getFieldClasses($context)),
115:             'placeholder' => 'HH:MM',
116:             'data-options' => wp_json_encode($this->getTimepickerOptions())
117:         );
118: 
119:         if ( ! $this->isEmpty()) {
120:             $attributes['value'] = $this->getValue();
121:         }
122: 
123:         if ($this->config('readOnly')) {
124:             $attributes['readonly'] = true;
125:         }
126: 
127:         $attributes = apply_filters('quform_field_attributes', $attributes, $this, $this->form, $context);
128:         $attributes = apply_filters('quform_field_attributes_' . $this->getIdentifier(), $attributes, $this, $this->form, $context);
129: 
130:         return $attributes;
131:     }
132: 
133:     134: 135: 136: 137: 
138:     protected function getTimepickerOptions()
139:     {
140:         $options = array(
141:             'min' => $this->config('timeMin'),
142:             'max' => $this->config('timeMax'),
143:             'interval' => $this->config('timeInterval'),
144:             'locale' => $this->config('timeLocale'),
145:             'format' => $this->config('timeFormatJs'),
146:             'placeholder' => $this->config('placeholder'),
147:             'autoOpen' => $this->config('timeAutoOpen'),
148:             'identifier' => $this->getIdentifier()
149:         );
150: 
151:         return $options;
152:     }
153: 
154:     155: 156: 157: 158: 159: 
160:     protected function getFieldClasses(array $context = array())
161:     {
162:         $classes = array(
163:             'quform-field',
164:             'quform-field-time',
165:             sprintf('quform-field-%s', $this->getIdentifier())
166:         );
167: 
168:         if ($this->form->config('tooltipsEnabled') && Quform::isNonEmptyString($this->config('tooltip')) && Quform::get($context, 'tooltipType') == 'field') {
169:             $classes[] = sprintf('quform-tooltip-%s', Quform::get($context, 'tooltipEvent'));
170:         }
171: 
172:         if (Quform::isNonEmptyString($this->config('customClass'))) {
173:             $classes[] = $this->config('customClass');
174:         }
175: 
176:         $classes = apply_filters('quform_field_classes', $classes, $this, $this->form, $context);
177:         $classes = apply_filters('quform_field_classes_' . $this->getIdentifier(), $classes, $this, $this->form, $context);
178: 
179:         return $classes;
180:     }
181: 
182:     183: 184: 185: 186: 187: 
188:     protected function getFieldHtml(array $context = array())
189:     {
190:         return Quform::getHtmlTag('input', $this->getFieldAttributes($context));
191:     }
192: 
193:     194: 195: 196: 197: 
198:     public function getEditFieldHtml()
199:     {
200:         return $this->getFieldHtml();
201:     }
202: 
203:     204: 205: 206: 207: 208: 
209:     protected function renderCss(array $context = array())
210:     {
211:         $css = parent::renderCss($context);
212: 
213:         if ($context['fieldWidth'] == 'custom' && Quform::isNonEmptyString($context['fieldWidthCustom'])) {
214:             $css .= sprintf('.quform-input-time.quform-input-%s { width: %s; }', $this->getIdentifier(), Quform::addCssUnit($context['fieldWidthCustom']));
215:             $css .= sprintf('.quform-inner-%s > .quform-error > .quform-error-inner { float: left; min-width: %s; }', $this->getIdentifier(), Quform::addCssUnit($context['fieldWidthCustom']));
216:         }
217: 
218:         return $css;
219:     }
220: 
221:     222: 223: 224: 225: 226: 227: 
228:     protected function isLogicValueMatch($value, array $rule)
229:     {
230:         if ($rule['operator'] == 'gt' || $rule['operator'] == 'lt') {
231:             if ( ! $this->isEmpty()) {
232:                 $result = Quform::compareDates($value, $rule['value']);
233: 
234:                 if ($result !== false) {
235:                     return $rule['operator'] == 'gt' ? $result === 1 : $result === -1;
236:                 }
237:             }
238: 
239:             return false;
240:         }
241: 
242:         return parent::isLogicValueMatch($value, $rule);
243:     }
244: 
245:     246: 247: 248: 249: 250: 
251:     public static function getDefaultConfig($key = null)
252:     {
253:         $config = apply_filters('quform_default_config_time', array(
254:             
255:             'label' => __('Time', 'quform'),
256:             'description' => '',
257:             'descriptionAbove' => '',
258:             'required' => false,
259: 
260:             
261:             'labelIcon' => '',
262:             'fieldIconLeft' => '',
263:             'fieldIconRight' => 'qicon-schedule',
264:             'fieldSize' => 'inherit',
265:             'fieldWidth' => 'inherit',
266:             'fieldWidthCustom' => '',
267:             'customClass' => '',
268:             'customElementClass' => '',
269:             'styles' => array(),
270: 
271:             
272:             'placeholder' => '',
273:             'subLabel' => '',
274:             'subLabelAbove' => '',
275:             'adminLabel' => '',
276:             'tooltip' => '',
277:             'tooltipType' => 'inherit',
278:             'tooltipEvent' => 'inherit',
279:             'labelPosition' => 'inherit',
280:             'labelWidth' => '',
281: 
282:             
283:             'logicEnabled' => false,
284:             'logicAction' => true,
285:             'logicMatch' => 'all',
286:             'logicRules' => array(),
287: 
288:             
289:             'defaultValue' => '',
290:             'dynamicDefaultValue' => false,
291:             'dynamicKey' => '',
292:             'timeMin' => '',
293:             'timeMax' => '',
294:             'timeInterval' => '',
295:             'timeLocale' => '',
296:             'timeFormatJs' => '',
297:             'timeFormat' => '',
298:             'timeAutoOpen' => true,
299:             'readOnly' => false,
300:             'showInEmail' => true,
301:             'saveToDatabase' => true,
302: 
303:             
304:             'visibility' => '',
305:             'validators' => array(),
306: 
307:             
308:             'messageRequired' => '',
309:             'messageTimeInvalidTime' => '',
310:             'messageTimeTooEarly' => '',
311:             'messageTimeTooLate' => ''
312:         ));
313: 
314:         $config['type'] = 'time';
315: 
316:         if (Quform::isNonEmptyString($key)) {
317:             return Quform::get($config, $key);
318:         }
319: 
320:         return $config;
321:     }
322: }
323: