CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.7 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.7
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • BasicWidget
  • ButtonWidget
  • CheckboxWidget
  • DateTimeWidget
  • FileWidget
  • LabelWidget
  • MultiCheckboxWidget
  • NestingLabelWidget
  • RadioWidget
  • SelectBoxWidget
  • TextareaWidget
  • WidgetLocator

Interfaces

  • WidgetInterface
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         3.0.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\View\Widget;
 16: 
 17: use Cake\View\Form\ContextInterface;
 18: use Cake\View\StringTemplate;
 19: use DateTime;
 20: use Exception;
 21: use RuntimeException;
 22: 
 23: /**
 24:  * Input widget class for generating a date time input widget.
 25:  *
 26:  * This class is intended as an internal implementation detail
 27:  * of Cake\View\Helper\FormHelper and is not intended for direct use.
 28:  */
 29: class DateTimeWidget implements WidgetInterface
 30: {
 31: 
 32:     /**
 33:      * Select box widget.
 34:      *
 35:      * @var \Cake\View\Widget\SelectBoxWidget
 36:      */
 37:     protected $_select;
 38: 
 39:     /**
 40:      * List of inputs that can be rendered
 41:      *
 42:      * @var array
 43:      */
 44:     protected $_selects = [
 45:         'year',
 46:         'month',
 47:         'day',
 48:         'hour',
 49:         'minute',
 50:         'second',
 51:         'meridian',
 52:     ];
 53: 
 54:     /**
 55:      * Template instance.
 56:      *
 57:      * @var \Cake\View\StringTemplate
 58:      */
 59:     protected $_templates;
 60: 
 61:     /**
 62:      * Constructor
 63:      *
 64:      * @param \Cake\View\StringTemplate $templates Templates list.
 65:      * @param \Cake\View\Widget\SelectBoxWidget $selectBox Selectbox widget instance.
 66:      */
 67:     public function __construct(StringTemplate $templates, SelectBoxWidget $selectBox)
 68:     {
 69:         $this->_select = $selectBox;
 70:         $this->_templates = $templates;
 71:     }
 72: 
 73:     /**
 74:      * Renders a date time widget
 75:      *
 76:      * - `name` - Set the input name.
 77:      * - `disabled` - Either true or an array of options to disable.
 78:      * - `val` - A date time string, integer or DateTime object
 79:      * - `empty` - Set to true to add an empty option at the top of the
 80:      *   option elements. Set to a string to define the display value of the
 81:      *   empty option.
 82:      *
 83:      * In addition to the above options, the following options allow you to control
 84:      * which input elements are generated. By setting any option to false you can disable
 85:      * that input picker. In addition each picker allows you to set additional options
 86:      * that are set as HTML properties on the picker.
 87:      *
 88:      * - `year` - Array of options for the year select box.
 89:      * - `month` - Array of options for the month select box.
 90:      * - `day` - Array of options for the day select box.
 91:      * - `hour` - Array of options for the hour select box.
 92:      * - `minute` - Array of options for the minute select box.
 93:      * - `second` - Set to true to enable the seconds input. Defaults to false.
 94:      * - `meridian` - Set to true to enable the meridian input. Defaults to false.
 95:      *   The meridian will be enabled automatically if you choose a 12 hour format.
 96:      *
 97:      * The `year` option accepts the `start` and `end` options. These let you control
 98:      * the year range that is generated. It defaults to +-5 years from today.
 99:      *
100:      * The `month` option accepts the `name` option which allows you to get month
101:      * names instead of month numbers.
102:      *
103:      * The `hour` option allows you to set the following options:
104:      *
105:      * - `format` option which accepts 12 or 24, allowing
106:      *   you to indicate which hour format you want.
107:      * - `start` The hour to start the options at.
108:      * - `end` The hour to stop the options at.
109:      *
110:      * The start and end options are dependent on the format used. If the
111:      * value is out of the start/end range it will not be included.
112:      *
113:      * The `minute` option allows you to define the following options:
114:      *
115:      * - `interval` The interval to round options to.
116:      * - `round` Accepts `up` or `down`. Defines which direction the current value
117:      *   should be rounded to match the select options.
118:      *
119:      * @param array $data Data to render with.
120:      * @param \Cake\View\Form\ContextInterface $context The current form context.
121:      * @return string A generated select box.
122:      * @throws \RuntimeException When option data is invalid.
123:      */
124:     public function render(array $data, ContextInterface $context)
125:     {
126:         $data = $this->_normalizeData($data);
127: 
128:         $selected = $this->_deconstructDate($data['val'], $data);
129: 
130:         $templateOptions = ['templateVars' => $data['templateVars']];
131:         foreach ($this->_selects as $select) {
132:             if ($data[$select] === false || $data[$select] === null) {
133:                 $templateOptions[$select] = '';
134:                 unset($data[$select]);
135:                 continue;
136:             }
137:             if (!is_array($data[$select])) {
138:                 throw new RuntimeException(sprintf(
139:                     'Options for "%s" must be an array|false|null',
140:                     $select
141:                 ));
142:             }
143:             $method = "_{$select}Select";
144:             $data[$select]['name'] = $data['name'] . '[' . $select . ']';
145:             $data[$select]['val'] = $selected[$select];
146: 
147:             if (!isset($data[$select]['empty'])) {
148:                 $data[$select]['empty'] = $data['empty'];
149:             }
150:             if (!isset($data[$select]['disabled'])) {
151:                 $data[$select]['disabled'] = $data['disabled'];
152:             }
153:             if (isset($data[$select]['templateVars']) && $templateOptions['templateVars']) {
154:                 $data[$select]['templateVars'] = array_merge(
155:                     $templateOptions['templateVars'],
156:                     $data[$select]['templateVars']
157:                 );
158:             }
159:             if (!isset($data[$select]['templateVars'])) {
160:                 $data[$select]['templateVars'] = $templateOptions['templateVars'];
161:             }
162:             $templateOptions[$select] = $this->{$method}($data[$select], $context);
163:             unset($data[$select]);
164:         }
165:         unset($data['name'], $data['empty'], $data['disabled'], $data['val']);
166:         $templateOptions['attrs'] = $this->_templates->formatAttributes($data);
167: 
168:         return $this->_templates->format('dateWidget', $templateOptions);
169:     }
170: 
171:     /**
172:      * Normalize data.
173:      *
174:      * @param array $data Data to normalize.
175:      * @return array Normalized data.
176:      */
177:     protected function _normalizeData($data)
178:     {
179:         $data += [
180:             'name' => '',
181:             'empty' => false,
182:             'disabled' => null,
183:             'val' => null,
184:             'year' => [],
185:             'month' => [],
186:             'day' => [],
187:             'hour' => [],
188:             'minute' => [],
189:             'second' => [],
190:             'meridian' => null,
191:             'templateVars' => [],
192:         ];
193: 
194:         $timeFormat = isset($data['hour']['format']) ? $data['hour']['format'] : null;
195:         if ($timeFormat === 12 && !isset($data['meridian'])) {
196:             $data['meridian'] = [];
197:         }
198:         if ($timeFormat === 24) {
199:             $data['meridian'] = false;
200:         }
201: 
202:         return $data;
203:     }
204: 
205:     /**
206:      * Deconstructs the passed date value into all time units
207:      *
208:      * @param string|int|array|\DateTime|null $value Value to deconstruct.
209:      * @param array $options Options for conversion.
210:      * @return array
211:      */
212:     protected function _deconstructDate($value, $options)
213:     {
214:         if ($value === '' || $value === null) {
215:             return [
216:                 'year' => '', 'month' => '', 'day' => '',
217:                 'hour' => '', 'minute' => '', 'second' => '',
218:                 'meridian' => '',
219:             ];
220:         }
221:         try {
222:             if (is_string($value) && !is_numeric($value)) {
223:                 $date = new DateTime($value);
224:             } elseif (is_bool($value)) {
225:                 $date = new DateTime();
226:             } elseif (is_int($value) || is_numeric($value)) {
227:                 $date = new DateTime('@' . $value);
228:             } elseif (is_array($value)) {
229:                 $dateArray = [
230:                     'year' => '', 'month' => '', 'day' => '',
231:                     'hour' => '', 'minute' => '', 'second' => '',
232:                     'meridian' => '',
233:                 ];
234:                 $validDate = false;
235:                 foreach ($dateArray as $key => $dateValue) {
236:                     $exists = isset($value[$key]);
237:                     if ($exists) {
238:                         $validDate = true;
239:                     }
240:                     if ($exists && $value[$key] !== '') {
241:                         $dateArray[$key] = str_pad($value[$key], 2, '0', STR_PAD_LEFT);
242:                     }
243:                 }
244:                 if ($validDate) {
245:                     if (!isset($dateArray['second'])) {
246:                         $dateArray['second'] = 0;
247:                     }
248:                     if (!empty($value['meridian'])) {
249:                         $isAm = strtolower($dateArray['meridian']) === 'am';
250:                         $dateArray['hour'] = $isAm ? $dateArray['hour'] : $dateArray['hour'] + 12;
251:                     }
252:                     if (!empty($dateArray['minute']) && isset($options['minute']['interval'])) {
253:                         $dateArray['minute'] += $this->_adjustValue($dateArray['minute'], $options['minute']);
254:                         $dateArray['minute'] = str_pad((string)$dateArray['minute'], 2, '0', STR_PAD_LEFT);
255:                     }
256: 
257:                     return $dateArray;
258:                 }
259: 
260:                 $date = new DateTime();
261:             } else {
262:                 /* @var \DateTime $value */
263:                 $date = clone $value;
264:             }
265:         } catch (Exception $e) {
266:             $date = new DateTime();
267:         }
268: 
269:         if (isset($options['minute']['interval'])) {
270:             $change = $this->_adjustValue((int)$date->format('i'), $options['minute']);
271:             $date->modify($change > 0 ? "+$change minutes" : "$change minutes");
272:         }
273: 
274:         return [
275:             'year' => $date->format('Y'),
276:             'month' => $date->format('m'),
277:             'day' => $date->format('d'),
278:             'hour' => $date->format('H'),
279:             'minute' => $date->format('i'),
280:             'second' => $date->format('s'),
281:             'meridian' => $date->format('a'),
282:         ];
283:     }
284: 
285:     /**
286:      * Adjust $value based on rounding settings.
287:      *
288:      * @param int $value The value to adjust.
289:      * @param array $options The options containing interval and possibly round.
290:      * @return int The amount to adjust $value by.
291:      */
292:     protected function _adjustValue($value, $options)
293:     {
294:         $options += ['interval' => 1, 'round' => null];
295:         $changeValue = $value * (1 / $options['interval']);
296:         switch ($options['round']) {
297:             case 'up':
298:                 $changeValue = ceil($changeValue);
299:                 break;
300:             case 'down':
301:                 $changeValue = floor($changeValue);
302:                 break;
303:             default:
304:                 $changeValue = round($changeValue);
305:         }
306: 
307:         return ($changeValue * $options['interval']) - $value;
308:     }
309: 
310:     /**
311:      * Generates a year select
312:      *
313:      * @param array $options Options list.
314:      * @param \Cake\View\Form\ContextInterface $context The current form context.
315:      * @return string
316:      */
317:     protected function _yearSelect($options, $context)
318:     {
319:         $options += [
320:             'name' => '',
321:             'val' => null,
322:             'start' => date('Y', strtotime('-5 years')),
323:             'end' => date('Y', strtotime('+5 years')),
324:             'order' => 'desc',
325:             'templateVars' => [],
326:             'options' => []
327:         ];
328: 
329:         if (!empty($options['val'])) {
330:             $options['start'] = min($options['val'], $options['start']);
331:             $options['end'] = max($options['val'], $options['end']);
332:         }
333:         if (empty($options['options'])) {
334:             $options['options'] = $this->_generateNumbers($options['start'], $options['end']);
335:         }
336:         if ($options['order'] === 'desc') {
337:             $options['options'] = array_reverse($options['options'], true);
338:         }
339:         unset($options['start'], $options['end'], $options['order']);
340: 
341:         return $this->_select->render($options, $context);
342:     }
343: 
344:     /**
345:      * Generates a month select
346:      *
347:      * @param array $options The options to build the month select with
348:      * @param \Cake\View\Form\ContextInterface $context The current form context.
349:      * @return string
350:      */
351:     protected function _monthSelect($options, $context)
352:     {
353:         $options += [
354:             'name' => '',
355:             'names' => false,
356:             'val' => null,
357:             'leadingZeroKey' => true,
358:             'leadingZeroValue' => false,
359:             'templateVars' => [],
360:         ];
361: 
362:         if (empty($options['options'])) {
363:             if ($options['names'] === true) {
364:                 $options['options'] = $this->_getMonthNames($options['leadingZeroKey']);
365:             } elseif (is_array($options['names'])) {
366:                 $options['options'] = $options['names'];
367:             } else {
368:                 $options['options'] = $this->_generateNumbers(1, 12, $options);
369:             }
370:         }
371: 
372:         unset($options['leadingZeroKey'], $options['leadingZeroValue'], $options['names']);
373: 
374:         return $this->_select->render($options, $context);
375:     }
376: 
377:     /**
378:      * Generates a day select
379:      *
380:      * @param array $options The options to generate a day select with.
381:      * @param \Cake\View\Form\ContextInterface $context The current form context.
382:      * @return string
383:      */
384:     protected function _daySelect($options, $context)
385:     {
386:         $options += [
387:             'name' => '',
388:             'val' => null,
389:             'leadingZeroKey' => true,
390:             'leadingZeroValue' => false,
391:             'templateVars' => [],
392:         ];
393:         $options['options'] = $this->_generateNumbers(1, 31, $options);
394: 
395:         unset($options['names'], $options['leadingZeroKey'], $options['leadingZeroValue']);
396: 
397:         return $this->_select->render($options, $context);
398:     }
399: 
400:     /**
401:      * Generates a hour select
402:      *
403:      * @param array $options The options to generate an hour select with
404:      * @param \Cake\View\Form\ContextInterface $context The current form context.
405:      * @return string
406:      */
407:     protected function _hourSelect($options, $context)
408:     {
409:         $options += [
410:             'name' => '',
411:             'val' => null,
412:             'format' => 24,
413:             'start' => null,
414:             'end' => null,
415:             'leadingZeroKey' => true,
416:             'leadingZeroValue' => false,
417:             'templateVars' => [],
418:         ];
419:         $is24 = $options['format'] == 24;
420: 
421:         $defaultStart = $is24 ? 0 : 1;
422:         $defaultEnd = $is24 ? 23 : 12;
423:         $options['start'] = max($defaultStart, $options['start']);
424: 
425:         $options['end'] = min($defaultEnd, $options['end']);
426:         if ($options['end'] === null) {
427:             $options['end'] = $defaultEnd;
428:         }
429: 
430:         if (!$is24 && $options['val'] > 12) {
431:             $options['val'] = sprintf('%02d', $options['val'] - 12);
432:         }
433:         if (!$is24 && in_array($options['val'], ['00', '0', 0], true)) {
434:             $options['val'] = 12;
435:         }
436: 
437:         if (empty($options['options'])) {
438:             $options['options'] = $this->_generateNumbers(
439:                 $options['start'],
440:                 $options['end'],
441:                 $options
442:             );
443:         }
444: 
445:         unset(
446:             $options['end'],
447:             $options['start'],
448:             $options['format'],
449:             $options['leadingZeroKey'],
450:             $options['leadingZeroValue']
451:         );
452: 
453:         return $this->_select->render($options, $context);
454:     }
455: 
456:     /**
457:      * Generates a minute select
458:      *
459:      * @param array $options The options to generate a minute select with.
460:      * @param \Cake\View\Form\ContextInterface $context The current form context.
461:      * @return string
462:      */
463:     protected function _minuteSelect($options, $context)
464:     {
465:         $options += [
466:             'name' => '',
467:             'val' => null,
468:             'interval' => 1,
469:             'round' => 'up',
470:             'leadingZeroKey' => true,
471:             'leadingZeroValue' => true,
472:             'templateVars' => [],
473:         ];
474:         $options['interval'] = max($options['interval'], 1);
475:         if (empty($options['options'])) {
476:             $options['options'] = $this->_generateNumbers(0, 59, $options);
477:         }
478: 
479:         unset(
480:             $options['leadingZeroKey'],
481:             $options['leadingZeroValue'],
482:             $options['interval'],
483:             $options['round']
484:         );
485: 
486:         return $this->_select->render($options, $context);
487:     }
488: 
489:     /**
490:      * Generates a second select
491:      *
492:      * @param array $options The options to generate a second select with
493:      * @param \Cake\View\Form\ContextInterface $context The current form context.
494:      * @return string
495:      */
496:     protected function _secondSelect($options, $context)
497:     {
498:         $options += [
499:             'name' => '',
500:             'val' => null,
501:             'leadingZeroKey' => true,
502:             'leadingZeroValue' => true,
503:             'options' => $this->_generateNumbers(0, 59),
504:             'templateVars' => [],
505:         ];
506: 
507:         unset($options['leadingZeroKey'], $options['leadingZeroValue']);
508: 
509:         return $this->_select->render($options, $context);
510:     }
511: 
512:     /**
513:      * Generates a meridian select
514:      *
515:      * @param array $options The options to generate a meridian select with.
516:      * @param \Cake\View\Form\ContextInterface $context The current form context.
517:      * @return string
518:      */
519:     protected function _meridianSelect($options, $context)
520:     {
521:         $options += [
522:             'name' => '',
523:             'val' => null,
524:             'options' => ['am' => 'am', 'pm' => 'pm'],
525:             'templateVars' => [],
526:         ];
527: 
528:         return $this->_select->render($options, $context);
529:     }
530: 
531:     /**
532:      * Returns a translated list of month names
533:      *
534:      * @param bool $leadingZero Whether to generate month keys with leading zero.
535:      * @return array
536:      */
537:     protected function _getMonthNames($leadingZero = false)
538:     {
539:         $months = [
540:             '01' => __d('cake', 'January'),
541:             '02' => __d('cake', 'February'),
542:             '03' => __d('cake', 'March'),
543:             '04' => __d('cake', 'April'),
544:             '05' => __d('cake', 'May'),
545:             '06' => __d('cake', 'June'),
546:             '07' => __d('cake', 'July'),
547:             '08' => __d('cake', 'August'),
548:             '09' => __d('cake', 'September'),
549:             '10' => __d('cake', 'October'),
550:             '11' => __d('cake', 'November'),
551:             '12' => __d('cake', 'December'),
552:         ];
553: 
554:         if ($leadingZero === false) {
555:             $i = 1;
556:             foreach ($months as $key => $name) {
557:                 unset($months[$key]);
558:                 $months[$i++] = $name;
559:             }
560:         }
561: 
562:         return $months;
563:     }
564: 
565:     /**
566:      * Generates a range of numbers
567:      *
568:      * ### Options
569:      *
570:      * - leadingZeroKey - Set to true to add a leading 0 to single digit keys.
571:      * - leadingZeroValue - Set to true to add a leading 0 to single digit values.
572:      * - interval - The interval to generate numbers for. Defaults to 1.
573:      *
574:      * @param int $start Start of the range of numbers to generate
575:      * @param int $end End of the range of numbers to generate
576:      * @param array $options Options list.
577:      * @return array
578:      */
579:     protected function _generateNumbers($start, $end, $options = [])
580:     {
581:         $options += [
582:             'leadingZeroKey' => true,
583:             'leadingZeroValue' => true,
584:             'interval' => 1
585:         ];
586: 
587:         $numbers = [];
588:         $i = $start;
589:         while ($i <= $end) {
590:             $key = (string)$i;
591:             $value = (string)$i;
592:             if ($options['leadingZeroKey'] === true) {
593:                 $key = sprintf('%02d', $key);
594:             }
595:             if ($options['leadingZeroValue'] === true) {
596:                 $value = sprintf('%02d', $value);
597:             }
598:             $numbers[$key] = $value;
599:             $i += $options['interval'];
600:         }
601: 
602:         return $numbers;
603:     }
604: 
605:     /**
606:      * Returns a list of fields that need to be secured for this widget.
607:      *
608:      * When the hour picker is in 24hr mode (null or format=24) the meridian
609:      * picker will be omitted.
610:      *
611:      * @param array $data The data to render.
612:      * @return array Array of fields to secure.
613:      */
614:     public function secureFields(array $data)
615:     {
616:         $data = $this->_normalizeData($data);
617: 
618:         $fields = [];
619:         foreach ($this->_selects as $select) {
620:             if ($data[$select] === false || $data[$select] === null) {
621:                 continue;
622:             }
623: 
624:             $fields[] = $data['name'] . '[' . $select . ']';
625:         }
626: 
627:         return $fields;
628:     }
629: }
630: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs