1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: App::uses('ClassRegistry', 'Utility');
18: App::uses('AppHelper', 'View/Helper');
19: App::uses('Hash', 'Utility');
20: App::uses('Inflector', 'Utility');
21:
22: 23: 24: 25: 26: 27: 28: 29: 30:
31: class FormHelper extends AppHelper {
32:
33: 34: 35: 36: 37:
38: public $helpers = array('Html');
39:
40: 41: 42: 43: 44:
45: protected $_options = array(
46: 'day' => array(), 'minute' => array(), 'hour' => array(),
47: 'month' => array(), 'year' => array(), 'meridian' => array()
48: );
49:
50: 51: 52: 53: 54:
55: public $fields = array();
56:
57: 58: 59: 60: 61: 62:
63: const SECURE_SKIP = 'skip';
64:
65: 66: 67: 68: 69:
70: public $requestType = null;
71:
72: 73: 74: 75: 76:
77: public $defaultModel = null;
78:
79: 80: 81: 82: 83:
84: protected $_inputDefaults = array();
85:
86: 87: 88: 89: 90: 91: 92: 93:
94: protected $_unlockedFields = array();
95:
96: 97: 98: 99: 100: 101:
102: protected $_models = array();
103:
104: 105: 106: 107: 108: 109: 110:
111: public $validationErrors = array();
112:
113: 114: 115: 116: 117:
118: protected $_domIdSuffixes = array();
119:
120: 121: 122: 123: 124: 125:
126: protected $_lastAction = '';
127:
128: 129: 130: 131: 132: 133:
134: public function __construct(View $View, $settings = array()) {
135: parent::__construct($View, $settings);
136: $this->validationErrors =& $View->validationErrors;
137: }
138:
139: 140: 141: 142: 143: 144: 145:
146: protected function _getModel($model) {
147: $object = null;
148: if (!$model || $model === 'Model') {
149: return $object;
150: }
151:
152: if (array_key_exists($model, $this->_models)) {
153: return $this->_models[$model];
154: }
155:
156: if (ClassRegistry::isKeySet($model)) {
157: $object = ClassRegistry::getObject($model);
158: } elseif (isset($this->request->params['models'][$model])) {
159: $plugin = $this->request->params['models'][$model]['plugin'];
160: $plugin .= ($plugin) ? '.' : null;
161: $object = ClassRegistry::init(array(
162: 'class' => $plugin . $this->request->params['models'][$model]['className'],
163: 'alias' => $model
164: ));
165: } elseif (ClassRegistry::isKeySet($this->defaultModel)) {
166: $defaultObject = ClassRegistry::getObject($this->defaultModel);
167: if ($defaultObject && in_array($model, array_keys($defaultObject->getAssociated()), true) && isset($defaultObject->{$model})) {
168: $object = $defaultObject->{$model};
169: }
170: } else {
171: $object = ClassRegistry::init($model, true);
172: }
173:
174: $this->_models[$model] = $object;
175: if (!$object) {
176: return null;
177: }
178:
179: $this->fieldset[$model] = array('fields' => null, 'key' => $object->primaryKey, 'validates' => null);
180: return $object;
181: }
182:
183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202:
203: protected function _introspectModel($model, $key, $field = null) {
204: $object = $this->_getModel($model);
205: if (!$object) {
206: return;
207: }
208:
209: if ($key === 'key') {
210: return $this->fieldset[$model]['key'] = $object->primaryKey;
211: }
212:
213: if ($key === 'fields') {
214: if (!isset($this->fieldset[$model]['fields'])) {
215: $this->fieldset[$model]['fields'] = $object->schema();
216: foreach ($object->hasAndBelongsToMany as $alias => $assocData) {
217: $this->fieldset[$object->alias]['fields'][$alias] = array('type' => 'multiple');
218: }
219: }
220: if ($field === null || $field === false) {
221: return $this->fieldset[$model]['fields'];
222: } elseif (isset($this->fieldset[$model]['fields'][$field])) {
223: return $this->fieldset[$model]['fields'][$field];
224: }
225: return isset($object->hasAndBelongsToMany[$field]) ? array('type' => 'multiple') : null;
226: }
227:
228: if ($key === 'errors' && !isset($this->validationErrors[$model])) {
229: $this->validationErrors[$model] =& $object->validationErrors;
230: return $this->validationErrors[$model];
231: } elseif ($key === 'errors' && isset($this->validationErrors[$model])) {
232: return $this->validationErrors[$model];
233: }
234:
235: if ($key === 'validates' && !isset($this->fieldset[$model]['validates'])) {
236: $validates = array();
237: foreach (iterator_to_array($object->validator(), true) as $validateField => $validateProperties) {
238: if ($this->_isRequiredField($validateProperties)) {
239: $validates[$validateField] = true;
240: }
241: }
242: $this->fieldset[$model]['validates'] = $validates;
243: }
244:
245: if ($key === 'validates') {
246: if (empty($field)) {
247: return $this->fieldset[$model]['validates'];
248: }
249: return isset($this->fieldset[$model]['validates'][$field]) ?
250: $this->fieldset[$model]['validates'] : null;
251: }
252: }
253:
254: 255: 256: 257: 258: 259:
260: protected function _isRequiredField($validationRules) {
261: if (empty($validationRules) || count($validationRules) === 0) {
262: return false;
263: }
264:
265: $isUpdate = $this->requestType === 'put';
266: foreach ($validationRules as $rule) {
267: $rule->isUpdate($isUpdate);
268: if ($rule->skip()) {
269: continue;
270: }
271:
272: return !$rule->allowEmpty;
273: }
274: return false;
275: }
276:
277: 278: 279: 280: 281: 282: 283: 284:
285: public function tagIsInvalid() {
286: $entity = $this->entity();
287: $model = array_shift($entity);
288:
289:
290: if (empty($model) || is_numeric($model)) {
291: array_splice($entity, 1, 0, $model);
292: $model = array_shift($entity);
293: }
294:
295: $errors = array();
296: if (!empty($entity) && isset($this->validationErrors[$model])) {
297: $errors = $this->validationErrors[$model];
298: }
299: if (!empty($entity) && empty($errors)) {
300: $errors = $this->_introspectModel($model, 'errors');
301: }
302: if (empty($errors)) {
303: return false;
304: }
305: $errors = Hash::get($errors, implode('.', $entity));
306: return $errors === null ? false : $errors;
307: }
308:
309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334:
335: public function create($model = null, $options = array()) {
336: $created = $id = false;
337: $append = '';
338:
339: if (is_array($model) && empty($options)) {
340: $options = $model;
341: $model = null;
342: }
343:
344: if (empty($model) && $model !== false && !empty($this->request->params['models'])) {
345: $model = key($this->request->params['models']);
346: } elseif (empty($model) && empty($this->request->params['models'])) {
347: $model = false;
348: }
349: $this->defaultModel = $model;
350:
351: $key = null;
352: if ($model !== false) {
353: list($plugin, $model) = pluginSplit($model, true);
354: $key = $this->_introspectModel($plugin . $model, 'key');
355: $this->setEntity($model, true);
356: }
357:
358: if ($model !== false && $key) {
359: $recordExists = (
360: isset($this->request->data[$model]) &&
361: !empty($this->request->data[$model][$key]) &&
362: !is_array($this->request->data[$model][$key])
363: );
364:
365: if ($recordExists) {
366: $created = true;
367: $id = $this->request->data[$model][$key];
368: }
369: }
370:
371: $options += array(
372: 'type' => ($created && empty($options['action'])) ? 'put' : 'post',
373: 'action' => null,
374: 'url' => null,
375: 'default' => true,
376: 'encoding' => strtolower(Configure::read('App.encoding')),
377: 'inputDefaults' => array()
378: );
379: $this->inputDefaults($options['inputDefaults']);
380: unset($options['inputDefaults']);
381:
382: if (!isset($options['id'])) {
383: $domId = isset($options['action']) ? $options['action'] : $this->request['action'];
384: $options['id'] = $this->domId($domId . 'Form');
385: }
386:
387: if ($options['action'] === null && $options['url'] === null) {
388: $options['action'] = $this->request->here(false);
389: } elseif (empty($options['url']) || is_array($options['url'])) {
390: if (empty($options['url']['controller'])) {
391: if (!empty($model)) {
392: $options['url']['controller'] = Inflector::underscore(Inflector::pluralize($model));
393: } elseif (!empty($this->request->params['controller'])) {
394: $options['url']['controller'] = Inflector::underscore($this->request->params['controller']);
395: }
396: }
397: if (empty($options['action'])) {
398: $options['action'] = $this->request->params['action'];
399: }
400:
401: $plugin = null;
402: if ($this->plugin) {
403: $plugin = Inflector::underscore($this->plugin);
404: }
405: $actionDefaults = array(
406: 'plugin' => $plugin,
407: 'controller' => $this->_View->viewPath,
408: 'action' => $options['action'],
409: );
410: $options['action'] = array_merge($actionDefaults, (array)$options['url']);
411: if (!isset($options['action'][0]) && !empty($id)) {
412: $options['action'][0] = $id;
413: }
414: } elseif (is_string($options['url'])) {
415: $options['action'] = $options['url'];
416: }
417: unset($options['url']);
418:
419: switch (strtolower($options['type'])) {
420: case 'get':
421: $htmlAttributes['method'] = 'get';
422: break;
423: case 'file':
424: $htmlAttributes['enctype'] = 'multipart/form-data';
425: $options['type'] = ($created) ? 'put' : 'post';
426: case 'post':
427: case 'put':
428: case 'delete':
429: $append .= $this->hidden('_method', array(
430: 'name' => '_method', 'value' => strtoupper($options['type']), 'id' => null,
431: 'secure' => self::SECURE_SKIP
432: ));
433: default:
434: $htmlAttributes['method'] = 'post';
435: }
436: $this->requestType = strtolower($options['type']);
437:
438: $action = $this->url($options['action']);
439: $this->_lastAction($options['action']);
440: unset($options['type'], $options['action']);
441:
442: if (!$options['default']) {
443: if (!isset($options['onsubmit'])) {
444: $options['onsubmit'] = '';
445: }
446: $htmlAttributes['onsubmit'] = $options['onsubmit'] . 'event.returnValue = false; return false;';
447: }
448: unset($options['default']);
449:
450: if (!empty($options['encoding'])) {
451: $htmlAttributes['accept-charset'] = $options['encoding'];
452: unset($options['encoding']);
453: }
454:
455: $htmlAttributes = array_merge($options, $htmlAttributes);
456:
457: $this->fields = array();
458: if ($this->requestType !== 'get') {
459: $append .= $this->_csrfField();
460: }
461:
462: if (!empty($append)) {
463: $append = $this->Html->useTag('hiddenblock', $append);
464: }
465:
466: if ($model !== false) {
467: $this->setEntity($model, true);
468: $this->_introspectModel($model, 'fields');
469: }
470:
471: return $this->Html->useTag('form', $action, $htmlAttributes) . $append;
472: }
473:
474: 475: 476: 477: 478: 479:
480: protected function _csrfField() {
481: if (empty($this->request->params['_Token'])) {
482: return '';
483: }
484: if (!empty($this->request['_Token']['unlockedFields'])) {
485: foreach ((array)$this->request['_Token']['unlockedFields'] as $unlocked) {
486: $this->_unlockedFields[] = $unlocked;
487: }
488: }
489: return $this->hidden('_Token.key', array(
490: 'value' => $this->request->params['_Token']['key'], 'id' => 'Token' . mt_rand(),
491: 'secure' => self::SECURE_SKIP
492: ));
493: }
494:
495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519:
520: public function end($options = null, $secureAttributes = array()) {
521: $out = null;
522: $submit = null;
523:
524: if ($options !== null) {
525: $submitOptions = array();
526: if (is_string($options)) {
527: $submit = $options;
528: } else {
529: if (isset($options['label'])) {
530: $submit = $options['label'];
531: unset($options['label']);
532: }
533: $submitOptions = $options;
534: }
535: $out .= $this->submit($submit, $submitOptions);
536: }
537: if ($this->requestType !== 'get' &&
538: isset($this->request['_Token']) &&
539: !empty($this->request['_Token'])
540: ) {
541: $out .= $this->secure($this->fields, $secureAttributes);
542: $this->fields = array();
543: }
544: $this->setEntity(null);
545: $out .= $this->Html->useTag('formend');
546:
547: $this->_View->modelScope = false;
548: $this->requestType = null;
549: return $out;
550: }
551:
552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566:
567: public function secure($fields = array(), $secureAttributes = array()) {
568: if (!isset($this->request['_Token']) || empty($this->request['_Token'])) {
569: return;
570: }
571: $locked = array();
572: $unlockedFields = $this->_unlockedFields;
573:
574: foreach ($fields as $key => $value) {
575: if (!is_int($key)) {
576: $locked[$key] = $value;
577: unset($fields[$key]);
578: }
579: }
580:
581: sort($unlockedFields, SORT_STRING);
582: sort($fields, SORT_STRING);
583: ksort($locked, SORT_STRING);
584: $fields += $locked;
585:
586: $locked = implode(array_keys($locked), '|');
587: $unlocked = implode($unlockedFields, '|');
588: $hashParts = array(
589: $this->_lastAction,
590: serialize($fields),
591: $unlocked,
592: Configure::read('Security.salt')
593: );
594: $fields = Security::hash(implode('', $hashParts), 'sha1');
595:
596: $tokenFields = array_merge($secureAttributes, array(
597: 'value' => urlencode($fields . ':' . $locked),
598: 'id' => 'TokenFields' . mt_rand(),
599: ));
600: $out = $this->hidden('_Token.fields', $tokenFields);
601: $tokenUnlocked = array_merge($secureAttributes, array(
602: 'value' => urlencode($unlocked),
603: 'id' => 'TokenUnlocked' . mt_rand(),
604: ));
605: $out .= $this->hidden('_Token.unlocked', $tokenUnlocked);
606: return $this->Html->useTag('hiddenblock', $out);
607: }
608:
609: 610: 611: 612: 613: 614: 615: 616: 617: 618:
619: public function unlockField($name = null) {
620: if ($name === null) {
621: return $this->_unlockedFields;
622: }
623: if (!in_array($name, $this->_unlockedFields)) {
624: $this->_unlockedFields[] = $name;
625: }
626: $index = array_search($name, $this->fields);
627: if ($index !== false) {
628: unset($this->fields[$index]);
629: }
630: unset($this->fields[$name]);
631: }
632:
633: 634: 635: 636: 637: 638: 639: 640: 641: 642:
643: protected function _secure($lock, $field = null, $value = null) {
644: if (!$field) {
645: $field = $this->entity();
646: } elseif (is_string($field)) {
647: $field = Hash::filter(explode('.', $field));
648: }
649:
650: foreach ($this->_unlockedFields as $unlockField) {
651: $unlockParts = explode('.', $unlockField);
652: if (array_values(array_intersect($field, $unlockParts)) === $unlockParts) {
653: return;
654: }
655: }
656:
657: $field = implode('.', $field);
658: $field = preg_replace('/(\.\d+)+$/', '', $field);
659:
660: if ($lock) {
661: if (!in_array($field, $this->fields)) {
662: if ($value !== null) {
663: return $this->fields[$field] = $value;
664: }
665: $this->fields[] = $field;
666: }
667: } else {
668: $this->unlockField($field);
669: }
670: }
671:
672: 673: 674: 675: 676: 677: 678:
679: public function isFieldError($field) {
680: $this->setEntity($field);
681: return (bool)$this->tagIsInvalid();
682: }
683:
684: 685: 686: 687: 688: 689: 690: 691: 692: 693: 694: 695: 696: 697: 698: 699: 700:
701: public function error($field, $text = null, $options = array()) {
702: $defaults = array('wrap' => true, 'class' => 'error-message', 'escape' => true);
703: $options += $defaults;
704: $this->setEntity($field);
705:
706: $error = $this->tagIsInvalid();
707: if ($error === false) {
708: return null;
709: }
710: if (is_array($text)) {
711: if (isset($text['attributes']) && is_array($text['attributes'])) {
712: $options = array_merge($options, $text['attributes']);
713: unset($text['attributes']);
714: }
715: $tmp = array();
716: foreach ($error as &$e) {
717: if (isset($text[$e])) {
718: $tmp[] = $text[$e];
719: } else {
720: $tmp[] = $e;
721: }
722: }
723: $text = $tmp;
724: }
725:
726: if ($text !== null) {
727: $error = $text;
728: }
729: if (is_array($error)) {
730: foreach ($error as &$e) {
731: if (is_numeric($e)) {
732: $e = __d('cake', 'Error in field %s', Inflector::humanize($this->field()));
733: }
734: }
735: }
736: if ($options['escape']) {
737: $error = h($error);
738: unset($options['escape']);
739: }
740: if (is_array($error)) {
741: if (count($error) > 1) {
742: $listParams = array();
743: if (isset($options['listOptions'])) {
744: if (is_string($options['listOptions'])) {
745: $listParams[] = $options['listOptions'];
746: } else {
747: if (isset($options['listOptions']['itemOptions'])) {
748: $listParams[] = $options['listOptions']['itemOptions'];
749: unset($options['listOptions']['itemOptions']);
750: } else {
751: $listParams[] = array();
752: }
753: if (isset($options['listOptions']['tag'])) {
754: $listParams[] = $options['listOptions']['tag'];
755: unset($options['listOptions']['tag']);
756: }
757: array_unshift($listParams, $options['listOptions']);
758: }
759: unset($options['listOptions']);
760: }
761: array_unshift($listParams, $error);
762: $error = call_user_func_array(array($this->Html, 'nestedList'), $listParams);
763: } else {
764: $error = array_pop($error);
765: }
766: }
767: if ($options['wrap']) {
768: $tag = is_string($options['wrap']) ? $options['wrap'] : 'div';
769: unset($options['wrap']);
770: return $this->Html->tag($tag, $error, $options);
771: }
772: return $error;
773: }
774:
775: 776: 777: 778: 779: 780: 781: 782: 783: 784: 785: 786: 787: 788: 789: 790: 791: 792: 793: 794: 795: 796: 797: 798: 799: 800: 801: 802: 803: 804: 805: 806: 807: 808: 809: 810: 811: 812: 813: 814: 815: 816: 817: 818: 819: 820: 821: 822: 823: 824:
825: public function label($fieldName = null, $text = null, $options = array()) {
826: if ($fieldName === null) {
827: $fieldName = implode('.', $this->entity());
828: }
829:
830: if ($text === null) {
831: if (strpos($fieldName, '.') !== false) {
832: $fieldElements = explode('.', $fieldName);
833: $text = array_pop($fieldElements);
834: } else {
835: $text = $fieldName;
836: }
837: if (substr($text, -3) === '_id') {
838: $text = substr($text, 0, -3);
839: }
840: $text = __(Inflector::humanize(Inflector::underscore($text)));
841: }
842:
843: if (is_string($options)) {
844: $options = array('class' => $options);
845: }
846:
847: if (isset($options['for'])) {
848: $labelFor = $options['for'];
849: unset($options['for']);
850: } else {
851: $labelFor = $this->domId($fieldName);
852: }
853:
854: return $this->Html->useTag('label', $labelFor, $options, $text);
855: }
856:
857: 858: 859: 860: 861: 862: 863: 864: 865: 866: 867: 868: 869: 870: 871: 872: 873: 874: 875: 876: 877: 878: 879: 880: 881: 882: 883:
884: public function inputs($fields = null, $blacklist = null, $options = array()) {
885: $fieldset = $legend = true;
886: $modelFields = array();
887: $model = $this->model();
888: if ($model) {
889: $modelFields = array_keys((array)$this->_introspectModel($model, 'fields'));
890: }
891: if (is_array($fields)) {
892: if (array_key_exists('legend', $fields) && !in_array('legend', $modelFields)) {
893: $legend = $fields['legend'];
894: unset($fields['legend']);
895: }
896:
897: if (isset($fields['fieldset']) && !in_array('fieldset', $modelFields)) {
898: $fieldset = $fields['fieldset'];
899: unset($fields['fieldset']);
900: }
901: } elseif ($fields !== null) {
902: $fieldset = $legend = $fields;
903: if (!is_bool($fieldset)) {
904: $fieldset = true;
905: }
906: $fields = array();
907: }
908:
909: if (isset($options['legend'])) {
910: $legend = $options['legend'];
911: }
912: if (isset($options['fieldset'])) {
913: $fieldset = $options['fieldset'];
914: }
915:
916: if (empty($fields)) {
917: $fields = $modelFields;
918: }
919:
920: if ($legend === true) {
921: $actionName = __d('cake', 'New %s');
922: $isEdit = (
923: strpos($this->request->params['action'], 'update') !== false ||
924: strpos($this->request->params['action'], 'edit') !== false
925: );
926: if ($isEdit) {
927: $actionName = __d('cake', 'Edit %s');
928: }
929: $modelName = Inflector::humanize(Inflector::underscore($model));
930: $legend = sprintf($actionName, __($modelName));
931: }
932:
933: $out = null;
934: foreach ($fields as $name => $options) {
935: if (is_numeric($name) && !is_array($options)) {
936: $name = $options;
937: $options = array();
938: }
939: $entity = explode('.', $name);
940: $blacklisted = (
941: is_array($blacklist) &&
942: (in_array($name, $blacklist) || in_array(end($entity), $blacklist))
943: );
944: if ($blacklisted) {
945: continue;
946: }
947: $out .= $this->input($name, $options);
948: }
949:
950: if (is_string($fieldset)) {
951: $fieldsetClass = sprintf(' class="%s"', $fieldset);
952: } else {
953: $fieldsetClass = '';
954: }
955:
956: if ($fieldset) {
957: if ($legend) {
958: $out = $this->Html->useTag('legend', $legend) . $out;
959: }
960: $out = $this->Html->useTag('fieldset', $fieldsetClass, $out);
961: }
962: return $out;
963: }
964:
965: 966: 967: 968: 969: 970: 971: 972: 973: 974: 975: 976: 977: 978: 979: 980: 981: 982: 983: 984: 985: 986: 987: 988: 989: 990: 991: 992: 993: 994: 995: 996: 997:
998: public function input($fieldName, $options = array()) {
999: $this->setEntity($fieldName);
1000: $options = $this->_parseOptions($options);
1001:
1002: $divOptions = $this->_divOptions($options);
1003: unset($options['div']);
1004:
1005: if ($options['type'] === 'radio' && isset($options['options'])) {
1006: $radioOptions = (array)$options['options'];
1007: unset($options['options']);
1008: }
1009:
1010: $label = $this->_getLabel($fieldName, $options);
1011: if ($options['type'] !== 'radio') {
1012: unset($options['label']);
1013: }
1014:
1015: $error = $this->_extractOption('error', $options, null);
1016: unset($options['error']);
1017:
1018: $errorMessage = $this->_extractOption('errorMessage', $options, true);
1019: unset($options['errorMessage']);
1020:
1021: $selected = $this->_extractOption('selected', $options, null);
1022: unset($options['selected']);
1023:
1024: if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time') {
1025: $dateFormat = $this->_extractOption('dateFormat', $options, 'MDY');
1026: $timeFormat = $this->_extractOption('timeFormat', $options, 12);
1027: unset($options['dateFormat'], $options['timeFormat']);
1028: }
1029:
1030: $type = $options['type'];
1031: $out = array('before' => $options['before'], 'label' => $label, 'between' => $options['between'], 'after' => $options['after']);
1032: $format = $this->_getFormat($options);
1033:
1034: unset($options['type'], $options['before'], $options['between'], $options['after'], $options['format']);
1035:
1036: $out['error'] = null;
1037: if ($type !== 'hidden' && $error !== false) {
1038: $errMsg = $this->error($fieldName, $error);
1039: if ($errMsg) {
1040: $divOptions = $this->addClass($divOptions, 'error');
1041: if ($errorMessage) {
1042: $out['error'] = $errMsg;
1043: }
1044: }
1045: }
1046:
1047: if ($type === 'radio' && isset($out['between'])) {
1048: $options['between'] = $out['between'];
1049: $out['between'] = null;
1050: }
1051: $out['input'] = $this->_getInput(compact('type', 'fieldName', 'options', 'radioOptions', 'selected', 'dateFormat', 'timeFormat'));
1052:
1053: $output = '';
1054: foreach ($format as $element) {
1055: $output .= $out[$element];
1056: }
1057:
1058: if (!empty($divOptions['tag'])) {
1059: $tag = $divOptions['tag'];
1060: unset($divOptions['tag']);
1061: $output = $this->Html->tag($tag, $output, $divOptions);
1062: }
1063: return $output;
1064: }
1065:
1066: 1067: 1068: 1069: 1070: 1071:
1072: protected function _getInput($args) {
1073: extract($args);
1074: switch ($type) {
1075: case 'hidden':
1076: return $this->hidden($fieldName, $options);
1077: case 'checkbox':
1078: return $this->checkbox($fieldName, $options);
1079: case 'radio':
1080: return $this->radio($fieldName, $radioOptions, $options);
1081: case 'file':
1082: return $this->file($fieldName, $options);
1083: case 'select':
1084: $options += array('options' => array(), 'value' => $selected);
1085: $list = $options['options'];
1086: unset($options['options']);
1087: return $this->select($fieldName, $list, $options);
1088: case 'time':
1089: $options['value'] = $selected;
1090: return $this->dateTime($fieldName, null, $timeFormat, $options);
1091: case 'date':
1092: $options['value'] = $selected;
1093: return $this->dateTime($fieldName, $dateFormat, null, $options);
1094: case 'datetime':
1095: $options['value'] = $selected;
1096: return $this->dateTime($fieldName, $dateFormat, $timeFormat, $options);
1097: case 'textarea':
1098: return $this->textarea($fieldName, $options + array('cols' => '30', 'rows' => '6'));
1099: case 'url':
1100: return $this->text($fieldName, array('type' => 'url') + $options);
1101: default:
1102: return $this->{$type}($fieldName, $options);
1103: }
1104: }
1105:
1106: 1107: 1108: 1109: 1110: 1111:
1112: protected function _parseOptions($options) {
1113: $options = array_merge(
1114: array('before' => null, 'between' => null, 'after' => null, 'format' => null),
1115: $this->_inputDefaults,
1116: $options
1117: );
1118:
1119: if (!isset($options['type'])) {
1120: $options = $this->_magicOptions($options);
1121: }
1122:
1123: if (in_array($options['type'], array('radio', 'select'))) {
1124: $options = $this->_optionsOptions($options);
1125: }
1126:
1127: $options = $this->_maxLength($options);
1128:
1129: if (isset($options['rows']) || isset($options['cols'])) {
1130: $options['type'] = 'textarea';
1131: }
1132:
1133: if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time' || $options['type'] === 'select') {
1134: $options += array('empty' => false);
1135: }
1136: return $options;
1137: }
1138:
1139: 1140: 1141: 1142: 1143: 1144:
1145: protected function _optionsOptions($options) {
1146: if (isset($options['options'])) {
1147: return $options;
1148: }
1149: $varName = Inflector::variable(
1150: Inflector::pluralize(preg_replace('/_id$/', '', $this->field()))
1151: );
1152: $varOptions = $this->_View->get($varName);
1153: if (!is_array($varOptions)) {
1154: return $options;
1155: }
1156: if ($options['type'] !== 'radio') {
1157: $options['type'] = 'select';
1158: }
1159: $options['options'] = $varOptions;
1160: return $options;
1161: }
1162:
1163: 1164: 1165: 1166: 1167: 1168:
1169: protected function _magicOptions($options) {
1170: $modelKey = $this->model();
1171: $fieldKey = $this->field();
1172: $options['type'] = 'text';
1173: if (isset($options['options'])) {
1174: $options['type'] = 'select';
1175: } elseif (in_array($fieldKey, array('psword', 'passwd', 'password'))) {
1176: $options['type'] = 'password';
1177: } elseif (in_array($fieldKey, array('tel', 'telephone', 'phone'))) {
1178: $options['type'] = 'tel';
1179: } elseif ($fieldKey === 'email') {
1180: $options['type'] = 'email';
1181: } elseif (isset($options['checked'])) {
1182: $options['type'] = 'checkbox';
1183: } elseif ($fieldDef = $this->_introspectModel($modelKey, 'fields', $fieldKey)) {
1184: $type = $fieldDef['type'];
1185: $primaryKey = $this->fieldset[$modelKey]['key'];
1186: $map = array(
1187: 'string' => 'text', 'datetime' => 'datetime',
1188: 'boolean' => 'checkbox', 'timestamp' => 'datetime',
1189: 'text' => 'textarea', 'time' => 'time',
1190: 'date' => 'date', 'float' => 'number',
1191: 'integer' => 'number', 'decimal' => 'number',
1192: 'binary' => 'file'
1193: );
1194:
1195: if (isset($this->map[$type])) {
1196: $options['type'] = $this->map[$type];
1197: } elseif (isset($map[$type])) {
1198: $options['type'] = $map[$type];
1199: }
1200: if ($fieldKey === $primaryKey) {
1201: $options['type'] = 'hidden';
1202: }
1203: if ($options['type'] === 'number' &&
1204: !isset($options['step'])
1205: ) {
1206: if ($type === 'decimal') {
1207: $decimalPlaces = substr($fieldDef['length'], strpos($fieldDef['length'], ',') + 1);
1208: $options['step'] = sprintf('%.' . $decimalPlaces . 'F', pow(10, -1 * $decimalPlaces));
1209: } elseif ($type === 'float') {
1210: $options['step'] = 'any';
1211: }
1212: }
1213: }
1214:
1215: if (preg_match('/_id$/', $fieldKey) && $options['type'] !== 'hidden') {
1216: $options['type'] = 'select';
1217: }
1218:
1219: if ($modelKey === $fieldKey) {
1220: $options['type'] = 'select';
1221: if (!isset($options['multiple'])) {
1222: $options['multiple'] = 'multiple';
1223: }
1224: }
1225: if (in_array($options['type'], array('text', 'number'))) {
1226: $options = $this->_optionsOptions($options);
1227: }
1228: if ($options['type'] === 'select' && array_key_exists('step', $options)) {
1229: unset($options['step']);
1230: }
1231:
1232: return $options;
1233: }
1234:
1235: 1236: 1237: 1238: 1239: 1240:
1241: protected function _getFormat($options) {
1242: if ($options['type'] === 'hidden') {
1243: return array('input');
1244: }
1245: if (is_array($options['format']) && in_array('input', $options['format'])) {
1246: return $options['format'];
1247: }
1248: if ($options['type'] === 'checkbox') {
1249: return array('before', 'input', 'between', 'label', 'after', 'error');
1250: }
1251: return array('before', 'label', 'between', 'input', 'after', 'error');
1252: }
1253:
1254: 1255: 1256: 1257: 1258: 1259: 1260:
1261: protected function _getLabel($fieldName, $options) {
1262: if ($options['type'] === 'radio') {
1263: return false;
1264: }
1265:
1266: $label = null;
1267: if (isset($options['label'])) {
1268: $label = $options['label'];
1269: }
1270:
1271: if ($label === false) {
1272: return false;
1273: }
1274: return $this->_inputLabel($fieldName, $label, $options);
1275: }
1276:
1277: 1278: 1279: 1280: 1281: 1282:
1283: protected function _maxLength($options) {
1284: $fieldDef = $this->_introspectModel($this->model(), 'fields', $this->field());
1285: $autoLength = (
1286: !array_key_exists('maxlength', $options) &&
1287: isset($fieldDef['length']) &&
1288: is_scalar($fieldDef['length']) &&
1289: $fieldDef['length'] < 1000000 &&
1290: $fieldDef['type'] !== 'decimal' &&
1291: $options['type'] !== 'select'
1292: );
1293: if ($autoLength &&
1294: in_array($options['type'], array('text', 'textarea', 'email', 'tel', 'url', 'search'))
1295: ) {
1296: $options['maxlength'] = (int)$fieldDef['length'];
1297: }
1298: return $options;
1299: }
1300:
1301: 1302: 1303: 1304: 1305: 1306:
1307: protected function _divOptions($options) {
1308: if ($options['type'] === 'hidden') {
1309: return array();
1310: }
1311: $div = $this->_extractOption('div', $options, true);
1312: if (!$div) {
1313: return array();
1314: }
1315:
1316: $divOptions = array('class' => 'input');
1317: $divOptions = $this->addClass($divOptions, $options['type']);
1318: if (is_string($div)) {
1319: $divOptions['class'] = $div;
1320: } elseif (is_array($div)) {
1321: $divOptions = array_merge($divOptions, $div);
1322: }
1323: if ($this->_extractOption('required', $options) !== false &&
1324: $this->_introspectModel($this->model(), 'validates', $this->field())
1325: ) {
1326: $divOptions = $this->addClass($divOptions, 'required');
1327: }
1328: if (!isset($divOptions['tag'])) {
1329: $divOptions['tag'] = 'div';
1330: }
1331: return $divOptions;
1332: }
1333:
1334: 1335: 1336: 1337: 1338: 1339: 1340: 1341:
1342: protected function _extractOption($name, $options, $default = null) {
1343: if (array_key_exists($name, $options)) {
1344: return $options[$name];
1345: }
1346: return $default;
1347: }
1348:
1349: 1350: 1351: 1352: 1353: 1354: 1355: 1356: 1357: 1358: 1359: 1360:
1361: protected function _inputLabel($fieldName, $label, $options) {
1362: $labelAttributes = $this->domId(array(), 'for');
1363: $idKey = null;
1364: if ($options['type'] === 'date' || $options['type'] === 'datetime') {
1365: $firstInput = 'M';
1366: if (array_key_exists('dateFormat', $options) &&
1367: ($options['dateFormat'] === null || $options['dateFormat'] === 'NONE')
1368: ) {
1369: $firstInput = 'H';
1370: } elseif (!empty($options['dateFormat'])) {
1371: $firstInput = substr($options['dateFormat'], 0, 1);
1372: }
1373: switch ($firstInput) {
1374: case 'D':
1375: $idKey = 'day';
1376: $labelAttributes['for'] .= 'Day';
1377: break;
1378: case 'Y':
1379: $idKey = 'year';
1380: $labelAttributes['for'] .= 'Year';
1381: break;
1382: case 'M':
1383: $idKey = 'month';
1384: $labelAttributes['for'] .= 'Month';
1385: break;
1386: case 'H':
1387: $idKey = 'hour';
1388: $labelAttributes['for'] .= 'Hour';
1389: }
1390: }
1391: if ($options['type'] === 'time') {
1392: $labelAttributes['for'] .= 'Hour';
1393: $idKey = 'hour';
1394: }
1395: if (isset($idKey) && isset($options['id']) && isset($options['id'][$idKey])) {
1396: $labelAttributes['for'] = $options['id'][$idKey];
1397: }
1398:
1399: if (is_array($label)) {
1400: $labelText = null;
1401: if (isset($label['text'])) {
1402: $labelText = $label['text'];
1403: unset($label['text']);
1404: }
1405: $labelAttributes = array_merge($labelAttributes, $label);
1406: } else {
1407: $labelText = $label;
1408: }
1409:
1410: if (isset($options['id']) && is_string($options['id'])) {
1411: $labelAttributes = array_merge($labelAttributes, array('for' => $options['id']));
1412: }
1413: return $this->label($fieldName, $labelText, $labelAttributes);
1414: }
1415:
1416: 1417: 1418: 1419: 1420: 1421: 1422: 1423: 1424: 1425: 1426: 1427: 1428: 1429: 1430: 1431: 1432: 1433: 1434:
1435: public function checkbox($fieldName, $options = array()) {
1436: $valueOptions = array();
1437: if (isset($options['default'])) {
1438: $valueOptions['default'] = $options['default'];
1439: unset($options['default']);
1440: }
1441:
1442: $options += array('value' => 1, 'required' => false);
1443: $options = $this->_initInputField($fieldName, $options) + array('hiddenField' => true);
1444: $value = current($this->value($valueOptions));
1445: $output = '';
1446:
1447: if ((!isset($options['checked']) && !empty($value) && $value == $options['value']) ||
1448: !empty($options['checked'])
1449: ) {
1450: $options['checked'] = 'checked';
1451: }
1452: if ($options['hiddenField']) {
1453: $hiddenOptions = array(
1454: 'id' => $options['id'] . '_',
1455: 'name' => $options['name'],
1456: 'value' => ($options['hiddenField'] !== true ? $options['hiddenField'] : '0'),
1457: 'form' => isset($options['form']) ? $options['form'] : null,
1458: 'secure' => false,
1459: );
1460: if (isset($options['disabled']) && $options['disabled']) {
1461: $hiddenOptions['disabled'] = 'disabled';
1462: }
1463: $output = $this->hidden($fieldName, $hiddenOptions);
1464: }
1465: unset($options['hiddenField']);
1466:
1467: return $output . $this->Html->useTag('checkbox', $options['name'], array_diff_key($options, array('name' => null)));
1468: }
1469:
1470: 1471: 1472: 1473: 1474: 1475: 1476: 1477: 1478: 1479: 1480: 1481: 1482: 1483: 1484: 1485: 1486: 1487: 1488: 1489: 1490: 1491: 1492: 1493: 1494: 1495: 1496: 1497: 1498: 1499: 1500: 1501: 1502:
1503: public function radio($fieldName, $options = array(), $attributes = array()) {
1504: $attributes['options'] = $options;
1505: $attributes = $this->_initInputField($fieldName, $attributes);
1506: unset($attributes['options']);
1507:
1508: $showEmpty = $this->_extractOption('empty', $attributes);
1509: if ($showEmpty) {
1510: $showEmpty = ($showEmpty === true) ? __d('cake', 'empty') : $showEmpty;
1511: $options = array('' => $showEmpty) + $options;
1512: }
1513: unset($attributes['empty']);
1514:
1515: $legend = false;
1516: if (isset($attributes['legend'])) {
1517: $legend = $attributes['legend'];
1518: unset($attributes['legend']);
1519: } elseif (count($options) > 1) {
1520: $legend = __(Inflector::humanize($this->field()));
1521: }
1522:
1523: $label = true;
1524: if (isset($attributes['label'])) {
1525: $label = $attributes['label'];
1526: unset($attributes['label']);
1527: }
1528:
1529: $separator = null;
1530: if (isset($attributes['separator'])) {
1531: $separator = $attributes['separator'];
1532: unset($attributes['separator']);
1533: }
1534:
1535: $between = null;
1536: if (isset($attributes['between'])) {
1537: $between = $attributes['between'];
1538: unset($attributes['between']);
1539: }
1540:
1541: $value = null;
1542: if (isset($attributes['value'])) {
1543: $value = $attributes['value'];
1544: } else {
1545: $value = $this->value($fieldName);
1546: }
1547:
1548: $disabled = array();
1549: if (isset($attributes['disabled'])) {
1550: $disabled = $attributes['disabled'];
1551: }
1552:
1553: $out = array();
1554:
1555: $hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
1556: unset($attributes['hiddenField']);
1557:
1558: if (isset($value) && is_bool($value)) {
1559: $value = $value ? 1 : 0;
1560: }
1561:
1562: $this->_domIdSuffixes = array();
1563: foreach ($options as $optValue => $optTitle) {
1564: $optionsHere = array('value' => $optValue, 'disabled' => false);
1565: if (is_array($optTitle)) {
1566: if (isset($optTitle['value'])) {
1567: $optionsHere['value'] = $optTitle['value'];
1568: }
1569:
1570: $optionsHere += $optTitle;
1571: $optTitle = $optionsHere['name'];
1572: unset($optionsHere['name']);
1573: }
1574:
1575: if (isset($value) && strval($optValue) === strval($value)) {
1576: $optionsHere['checked'] = 'checked';
1577: }
1578: $isNumeric = is_numeric($optValue);
1579: if ($disabled && (!is_array($disabled) || in_array((string)$optValue, $disabled, !$isNumeric))) {
1580: $optionsHere['disabled'] = true;
1581: }
1582: $tagName = $attributes['id'] . $this->domIdSuffix($optValue);
1583:
1584: if ($label) {
1585: $labelOpts = is_array($label) ? $label : array();
1586: $labelOpts += array('for' => $tagName);
1587: $optTitle = $this->label($tagName, $optTitle, $labelOpts);
1588: }
1589:
1590: if (is_array($between)) {
1591: $optTitle .= array_shift($between);
1592: }
1593: $allOptions = $optionsHere + $attributes;
1594: $out[] = $this->Html->useTag('radio', $attributes['name'], $tagName,
1595: array_diff_key($allOptions, array('name' => null, 'type' => null, 'id' => null)),
1596: $optTitle
1597: );
1598: }
1599: $hidden = null;
1600:
1601: if ($hiddenField) {
1602: if (!isset($value) || $value === '') {
1603: $hidden = $this->hidden($fieldName, array(
1604: 'form' => isset($attributes['form']) ? $attributes['form'] : null,
1605: 'id' => $attributes['id'] . '_',
1606: 'value' => '',
1607: 'name' => $attributes['name']
1608: ));
1609: }
1610: }
1611: $out = $hidden . implode($separator, $out);
1612:
1613: if (is_array($between)) {
1614: $between = '';
1615: }
1616: if ($legend) {
1617: $out = $this->Html->useTag('fieldset', '', $this->Html->useTag('legend', $legend) . $between . $out);
1618: }
1619: return $out;
1620: }
1621:
1622: 1623: 1624: 1625: 1626: 1627: 1628: 1629: 1630: 1631: 1632: 1633: 1634: 1635: 1636: 1637: 1638: 1639: 1640: 1641: 1642:
1643: public function __call($method, $params) {
1644: $options = array();
1645: if (empty($params)) {
1646: throw new CakeException(__d('cake_dev', 'Missing field name for FormHelper::%s', $method));
1647: }
1648: if (isset($params[1])) {
1649: $options = $params[1];
1650: }
1651: if (!isset($options['type'])) {
1652: $options['type'] = $method;
1653: }
1654: $options = $this->_initInputField($params[0], $options);
1655: return $this->Html->useTag('input', $options['name'], array_diff_key($options, array('name' => null)));
1656: }
1657:
1658: 1659: 1660: 1661: 1662: 1663: 1664: 1665: 1666: 1667: 1668: 1669:
1670: public function textarea($fieldName, $options = array()) {
1671: $options = $this->_initInputField($fieldName, $options);
1672: $value = null;
1673:
1674: if (array_key_exists('value', $options)) {
1675: $value = $options['value'];
1676: if (!array_key_exists('escape', $options) || $options['escape'] !== false) {
1677: $value = h($value);
1678: }
1679: unset($options['value']);
1680: }
1681: return $this->Html->useTag('textarea', $options['name'], array_diff_key($options, array('type' => null, 'name' => null)), $value);
1682: }
1683:
1684: 1685: 1686: 1687: 1688: 1689: 1690: 1691:
1692: public function hidden($fieldName, $options = array()) {
1693: $options += array('required' => false, 'secure' => true);
1694:
1695: $secure = $options['secure'];
1696: unset($options['secure']);
1697:
1698: $options = $this->_initInputField($fieldName, array_merge(
1699: $options, array('secure' => self::SECURE_SKIP)
1700: ));
1701:
1702: if ($secure === true) {
1703: $this->_secure(true, null, '' . $options['value']);
1704: }
1705:
1706: return $this->Html->useTag('hidden', $options['name'], array_diff_key($options, array('name' => null)));
1707: }
1708:
1709: 1710: 1711: 1712: 1713: 1714: 1715: 1716:
1717: public function file($fieldName, $options = array()) {
1718: $options += array('secure' => true);
1719: $secure = $options['secure'];
1720: $options['secure'] = self::SECURE_SKIP;
1721:
1722: $options = $this->_initInputField($fieldName, $options);
1723: $field = $this->entity();
1724:
1725: foreach (array('name', 'type', 'tmp_name', 'error', 'size') as $suffix) {
1726: $this->_secure($secure, array_merge($field, array($suffix)));
1727: }
1728:
1729: $exclude = array('name' => null, 'value' => null);
1730: return $this->Html->useTag('file', $options['name'], array_diff_key($options, $exclude));
1731: }
1732:
1733: 1734: 1735: 1736: 1737: 1738: 1739: 1740: 1741: 1742: 1743: 1744: 1745:
1746: public function button($title, $options = array()) {
1747: $options += array('type' => 'submit', 'escape' => false, 'secure' => false);
1748: if ($options['escape']) {
1749: $title = h($title);
1750: }
1751: if (isset($options['name'])) {
1752: $name = str_replace(array('[', ']'), array('.', ''), $options['name']);
1753: $this->_secure($options['secure'], $name);
1754: }
1755: return $this->Html->useTag('button', $options, $title);
1756: }
1757:
1758: 1759: 1760: 1761: 1762: 1763: 1764: 1765: 1766: 1767: 1768: 1769: 1770: 1771: 1772: 1773: 1774:
1775: public function postButton($title, $url, $options = array()) {
1776: $out = $this->create(false, array('id' => false, 'url' => $url));
1777: if (isset($options['data']) && is_array($options['data'])) {
1778: foreach (Hash::flatten($options['data']) as $key => $value) {
1779: $out .= $this->hidden($key, array('value' => $value, 'id' => false));
1780: }
1781: unset($options['data']);
1782: }
1783: $out .= $this->button($title, $options);
1784: $out .= $this->end();
1785: return $out;
1786: }
1787:
1788: 1789: 1790: 1791: 1792: 1793: 1794: 1795: 1796: 1797: 1798: 1799: 1800: 1801: 1802: 1803: 1804: 1805: 1806: 1807: 1808: 1809: 1810: 1811: 1812: 1813: 1814: 1815:
1816: public function postLink($title, $url = null, $options = array(), $confirmMessage = false) {
1817: $options = (array)$options + array('inline' => true, 'block' => null);
1818: if (!$options['inline'] && empty($options['block'])) {
1819: $options['block'] = __FUNCTION__;
1820: }
1821: unset($options['inline']);
1822:
1823: $requestMethod = 'POST';
1824: if (!empty($options['method'])) {
1825: $requestMethod = strtoupper($options['method']);
1826: unset($options['method']);
1827: }
1828: if (!empty($options['confirm'])) {
1829: $confirmMessage = $options['confirm'];
1830: unset($options['confirm']);
1831: }
1832:
1833: $formName = str_replace('.', '', uniqid('post_', true));
1834: $formUrl = $this->url($url);
1835: $formOptions = array(
1836: 'name' => $formName,
1837: 'id' => $formName,
1838: 'style' => 'display:none;',
1839: 'method' => 'post',
1840: );
1841: if (isset($options['target'])) {
1842: $formOptions['target'] = $options['target'];
1843: unset($options['target']);
1844: }
1845:
1846: $this->_lastAction($url);
1847:
1848: $out = $this->Html->useTag('form', $formUrl, $formOptions);
1849: $out .= $this->Html->useTag('hidden', '_method', array(
1850: 'value' => $requestMethod
1851: ));
1852: $out .= $this->_csrfField();
1853:
1854: $fields = array();
1855: if (isset($options['data']) && is_array($options['data'])) {
1856: foreach (Hash::flatten($options['data']) as $key => $value) {
1857: $fields[$key] = $value;
1858: $out .= $this->hidden($key, array('value' => $value, 'id' => false));
1859: }
1860: unset($options['data']);
1861: }
1862: $out .= $this->secure($fields);
1863: $out .= $this->Html->useTag('formend');
1864:
1865: if ($options['block']) {
1866: $this->_View->append($options['block'], $out);
1867: $out = '';
1868: }
1869: unset($options['block']);
1870:
1871: $url = '#';
1872: $onClick = 'document.' . $formName . '.submit();';
1873: if ($confirmMessage) {
1874: $options['onclick'] = $this->_confirm($confirmMessage, $onClick, '', $options);
1875: } else {
1876: $options['onclick'] = $onClick . ' ';
1877: }
1878: $options['onclick'] .= 'event.returnValue = false; return false;';
1879:
1880: $out .= $this->Html->link($title, $url, $options);
1881: return $out;
1882: }
1883:
1884: 1885: 1886: 1887: 1888: 1889: 1890: 1891: 1892: 1893: 1894: 1895: 1896: 1897: 1898: 1899: 1900: 1901: 1902: 1903: 1904: 1905: 1906: 1907: 1908: 1909: 1910: 1911:
1912: public function submit($caption = null, $options = array()) {
1913: if (!is_string($caption) && empty($caption)) {
1914: $caption = __d('cake', 'Submit');
1915: }
1916: $out = null;
1917: $div = true;
1918:
1919: if (isset($options['div'])) {
1920: $div = $options['div'];
1921: unset($options['div']);
1922: }
1923: $options += array('type' => 'submit', 'before' => null, 'after' => null, 'secure' => false);
1924: $divOptions = array('tag' => 'div');
1925:
1926: if ($div === true) {
1927: $divOptions['class'] = 'submit';
1928: } elseif ($div === false) {
1929: unset($divOptions);
1930: } elseif (is_string($div)) {
1931: $divOptions['class'] = $div;
1932: } elseif (is_array($div)) {
1933: $divOptions = array_merge(array('class' => 'submit', 'tag' => 'div'), $div);
1934: }
1935:
1936: if (isset($options['name'])) {
1937: $name = str_replace(array('[', ']'), array('.', ''), $options['name']);
1938: $this->_secure($options['secure'], $name);
1939: }
1940: unset($options['secure']);
1941:
1942: $before = $options['before'];
1943: $after = $options['after'];
1944: unset($options['before'], $options['after']);
1945:
1946: $isUrl = strpos($caption, '://') !== false;
1947: $isImage = preg_match('/\.(jpg|jpe|jpeg|gif|png|ico)$/', $caption);
1948:
1949: if ($isUrl || $isImage) {
1950: $unlockFields = array('x', 'y');
1951: if (isset($options['name'])) {
1952: $unlockFields = array(
1953: $options['name'] . '_x', $options['name'] . '_y'
1954: );
1955: }
1956: foreach ($unlockFields as $ignore) {
1957: $this->unlockField($ignore);
1958: }
1959: }
1960:
1961: if ($isUrl) {
1962: unset($options['type']);
1963: $tag = $this->Html->useTag('submitimage', $caption, $options);
1964: } elseif ($isImage) {
1965: unset($options['type']);
1966: if ($caption{0} !== '/') {
1967: $url = $this->webroot(Configure::read('App.imageBaseUrl') . $caption);
1968: } else {
1969: $url = $this->webroot(trim($caption, '/'));
1970: }
1971: $url = $this->assetTimestamp($url);
1972: $tag = $this->Html->useTag('submitimage', $url, $options);
1973: } else {
1974: $options['value'] = $caption;
1975: $tag = $this->Html->useTag('submit', $options);
1976: }
1977: $out = $before . $tag . $after;
1978:
1979: if (isset($divOptions)) {
1980: $tag = $divOptions['tag'];
1981: unset($divOptions['tag']);
1982: $out = $this->Html->tag($tag, $out, $divOptions);
1983: }
1984: return $out;
1985: }
1986:
1987: 1988: 1989: 1990: 1991: 1992: 1993: 1994: 1995: 1996: 1997: 1998: 1999: 2000: 2001: 2002: 2003: 2004: 2005: 2006: 2007: 2008: 2009: 2010: 2011: 2012: 2013: 2014: 2015: 2016: 2017: 2018: 2019: 2020: 2021: 2022: 2023: 2024: 2025: 2026: 2027: 2028: 2029: 2030: 2031: 2032: 2033: 2034: 2035: 2036: 2037: 2038: 2039: 2040: 2041: 2042: 2043: 2044: 2045: 2046:
2047: public function select($fieldName, $options = array(), $attributes = array()) {
2048: $select = array();
2049: $style = null;
2050: $tag = null;
2051: $attributes += array(
2052: 'class' => null,
2053: 'escape' => true,
2054: 'secure' => true,
2055: 'empty' => '',
2056: 'showParents' => false,
2057: 'hiddenField' => true,
2058: 'disabled' => false
2059: );
2060:
2061: $escapeOptions = $this->_extractOption('escape', $attributes);
2062: $secure = $this->_extractOption('secure', $attributes);
2063: $showEmpty = $this->_extractOption('empty', $attributes);
2064: $showParents = $this->_extractOption('showParents', $attributes);
2065: $hiddenField = $this->_extractOption('hiddenField', $attributes);
2066: unset($attributes['escape'], $attributes['secure'], $attributes['empty'], $attributes['showParents'], $attributes['hiddenField']);
2067: $id = $this->_extractOption('id', $attributes);
2068:
2069: $attributes = $this->_initInputField($fieldName, array_merge(
2070: (array)$attributes, array('secure' => self::SECURE_SKIP)
2071: ));
2072:
2073: if (is_string($options) && isset($this->_options[$options])) {
2074: $options = $this->_generateOptions($options);
2075: } elseif (!is_array($options)) {
2076: $options = array();
2077: }
2078: if (isset($attributes['type'])) {
2079: unset($attributes['type']);
2080: }
2081:
2082: if (!empty($attributes['multiple'])) {
2083: $style = ($attributes['multiple'] === 'checkbox') ? 'checkbox' : null;
2084: $template = ($style) ? 'checkboxmultiplestart' : 'selectmultiplestart';
2085: $tag = $template;
2086: if ($hiddenField) {
2087: $hiddenAttributes = array(
2088: 'value' => '',
2089: 'id' => $attributes['id'] . ($style ? '' : '_'),
2090: 'secure' => false,
2091: 'form' => isset($attributes['form']) ? $attributes['form'] : null,
2092: 'name' => $attributes['name'],
2093: 'disabled' => $attributes['disabled'] === true || $attributes['disabled'] === 'disabled'
2094: );
2095: $select[] = $this->hidden(null, $hiddenAttributes);
2096: }
2097: } else {
2098: $tag = 'selectstart';
2099: }
2100:
2101: if ($tag === 'checkboxmultiplestart') {
2102: unset($attributes['required']);
2103: }
2104:
2105: if (!empty($tag) || isset($template)) {
2106: $hasOptions = (count($options) > 0 || $showEmpty);
2107:
2108:
2109: if ((!isset($secure) || $secure) &&
2110: empty($attributes['disabled']) &&
2111: (!empty($attributes['multiple']) || $hasOptions)
2112: ) {
2113: $this->_secure(true, $this->_secureFieldName($attributes));
2114: }
2115: $filter = array('name' => null, 'value' => null);
2116: if (is_array($attributes['disabled'])) {
2117: $filter['disabled'] = null;
2118: }
2119: $select[] = $this->Html->useTag($tag, $attributes['name'], array_diff_key($attributes, $filter));
2120: }
2121: $emptyMulti = (
2122: $showEmpty !== null && $showEmpty !== false && !(
2123: empty($showEmpty) && (isset($attributes) &&
2124: array_key_exists('multiple', $attributes))
2125: )
2126: );
2127:
2128: if ($emptyMulti) {
2129: $showEmpty = ($showEmpty === true) ? '' : $showEmpty;
2130: $options = array('' => $showEmpty) + $options;
2131: }
2132:
2133: if (!$id) {
2134: $attributes['id'] = Inflector::camelize($attributes['id']);
2135: }
2136:
2137: $select = array_merge($select, $this->_selectOptions(
2138: array_reverse($options, true),
2139: array(),
2140: $showParents,
2141: array(
2142: 'escape' => $escapeOptions,
2143: 'style' => $style,
2144: 'name' => $attributes['name'],
2145: 'value' => $attributes['value'],
2146: 'class' => $attributes['class'],
2147: 'id' => $attributes['id'],
2148: 'disabled' => $attributes['disabled'],
2149: )
2150: ));
2151:
2152: $template = ($style === 'checkbox') ? 'checkboxmultipleend' : 'selectend';
2153: $select[] = $this->Html->useTag($template);
2154: return implode("\n", $select);
2155: }
2156:
2157: 2158: 2159: 2160: 2161: 2162: 2163: 2164: 2165: 2166: 2167: 2168:
2169: public function domIdSuffix($value, $type = 'html4') {
2170: if ($type === 'html5') {
2171: $value = str_replace(array('@', '<', '>', ' ', '"', '\''), '_', $value);
2172: } else {
2173: $value = Inflector::camelize(Inflector::slug($value));
2174: }
2175: $value = Inflector::camelize($value);
2176: $count = 1;
2177: $suffix = $value;
2178: while (in_array($suffix, $this->_domIdSuffixes)) {
2179: $suffix = $value . $count++;
2180: }
2181: $this->_domIdSuffixes[] = $suffix;
2182: return $suffix;
2183: }
2184:
2185: 2186: 2187: 2188: 2189: 2190: 2191: 2192: 2193: 2194: 2195: 2196: 2197: 2198:
2199: public function day($fieldName = null, $attributes = array()) {
2200: $attributes += array('empty' => true, 'value' => null);
2201: $attributes = $this->_dateTimeSelected('day', $fieldName, $attributes);
2202:
2203: if (strlen($attributes['value']) > 2) {
2204: $date = date_create($attributes['value']);
2205: $attributes['value'] = null;
2206: if ($date) {
2207: $attributes['value'] = $date->format('d');
2208: }
2209: } elseif ($attributes['value'] === false) {
2210: $attributes['value'] = null;
2211: }
2212: return $this->select($fieldName . ".day", $this->_generateOptions('day'), $attributes);
2213: }
2214:
2215: 2216: 2217: 2218: 2219: 2220: 2221: 2222: 2223: 2224: 2225: 2226: 2227: 2228: 2229: 2230: 2231: 2232:
2233: public function year($fieldName, $minYear = null, $maxYear = null, $attributes = array()) {
2234: if (is_array($minYear)) {
2235: $attributes = $minYear;
2236: $minYear = null;
2237: }
2238:
2239: $attributes += array('empty' => true, 'value' => null);
2240: if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
2241: if (is_array($value)) {
2242: $year = null;
2243: extract($value);
2244: $attributes['value'] = $year;
2245: } else {
2246: if (empty($value)) {
2247: if (!$attributes['empty'] && !$maxYear) {
2248: $attributes['value'] = 'now';
2249:
2250: } elseif (!$attributes['empty'] && $maxYear && !$attributes['value']) {
2251: $attributes['value'] = $maxYear;
2252: }
2253: } else {
2254: $attributes['value'] = $value;
2255: }
2256: }
2257: }
2258:
2259: if (strlen($attributes['value']) > 4 || $attributes['value'] === 'now') {
2260: $date = date_create($attributes['value']);
2261: $attributes['value'] = null;
2262: if ($date) {
2263: $attributes['value'] = $date->format('Y');
2264: }
2265: } elseif ($attributes['value'] === false) {
2266: $attributes['value'] = null;
2267: }
2268: $yearOptions = array('value' => $attributes['value'], 'min' => $minYear, 'max' => $maxYear, 'order' => 'desc');
2269: if (isset($attributes['orderYear'])) {
2270: $yearOptions['order'] = $attributes['orderYear'];
2271: unset($attributes['orderYear']);
2272: }
2273: return $this->select(
2274: $fieldName . '.year', $this->_generateOptions('year', $yearOptions),
2275: $attributes
2276: );
2277: }
2278:
2279: 2280: 2281: 2282: 2283: 2284: 2285: 2286: 2287: 2288: 2289: 2290: 2291: 2292: 2293: 2294:
2295: public function month($fieldName, $attributes = array()) {
2296: $attributes += array('empty' => true, 'value' => null);
2297: $attributes = $this->_dateTimeSelected('month', $fieldName, $attributes);
2298:
2299: if (strlen($attributes['value']) > 2) {
2300: $date = date_create($attributes['value']);
2301: $attributes['value'] = null;
2302: if ($date) {
2303: $attributes['value'] = $date->format('m');
2304: }
2305: } elseif ($attributes['value'] === false) {
2306: $attributes['value'] = null;
2307: }
2308: $defaults = array('monthNames' => true);
2309: $attributes = array_merge($defaults, (array)$attributes);
2310: $monthNames = $attributes['monthNames'];
2311: unset($attributes['monthNames']);
2312:
2313: return $this->select(
2314: $fieldName . ".month",
2315: $this->_generateOptions('month', array('monthNames' => $monthNames)),
2316: $attributes
2317: );
2318: }
2319:
2320: 2321: 2322: 2323: 2324: 2325: 2326: 2327: 2328: 2329: 2330: 2331: 2332: 2333: 2334:
2335: public function hour($fieldName, $format24Hours = false, $attributes = array()) {
2336: if (is_array($format24Hours)) {
2337: $attributes = $format24Hours;
2338: $format24Hours = false;
2339: }
2340:
2341: $attributes += array('empty' => true, 'value' => null);
2342: $attributes = $this->_dateTimeSelected('hour', $fieldName, $attributes);
2343:
2344: if (strlen($attributes['value']) > 2) {
2345: try {
2346: $date = new DateTime($attributes['value']);
2347: if ($format24Hours) {
2348: $attributes['value'] = $date->format('H');
2349: } else {
2350: $attributes['value'] = $date->format('g');
2351: }
2352: } catch (Exception $e) {
2353: $attributes['value'] = null;
2354: }
2355: } elseif ($attributes['value'] === false) {
2356: $attributes['value'] = null;
2357: }
2358:
2359: if ($attributes['value'] > 12 && !$format24Hours) {
2360: $attributes['value'] -= 12;
2361: }
2362: if (($attributes['value'] === 0 || $attributes['value'] === '00') && !$format24Hours) {
2363: $attributes['value'] = 12;
2364: }
2365:
2366: return $this->select(
2367: $fieldName . ".hour",
2368: $this->_generateOptions($format24Hours ? 'hour24' : 'hour'),
2369: $attributes
2370: );
2371: }
2372:
2373: 2374: 2375: 2376: 2377: 2378: 2379: 2380: 2381: 2382: 2383: 2384: 2385: 2386:
2387: public function minute($fieldName, $attributes = array()) {
2388: $attributes += array('empty' => true, 'value' => null);
2389: $attributes = $this->_dateTimeSelected('min', $fieldName, $attributes);
2390:
2391: if (strlen($attributes['value']) > 2) {
2392: $date = date_create($attributes['value']);
2393: $attributes['value'] = null;
2394: if ($date) {
2395: $attributes['value'] = $date->format('i');
2396: }
2397: } elseif ($attributes['value'] === false) {
2398: $attributes['value'] = null;
2399: }
2400: $minuteOptions = array();
2401:
2402: if (isset($attributes['interval'])) {
2403: $minuteOptions['interval'] = $attributes['interval'];
2404: unset($attributes['interval']);
2405: }
2406: return $this->select(
2407: $fieldName . ".min", $this->_generateOptions('minute', $minuteOptions),
2408: $attributes
2409: );
2410: }
2411:
2412: 2413: 2414: 2415: 2416: 2417: 2418: 2419:
2420: protected function _dateTimeSelected($select, $fieldName, $attributes) {
2421: if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
2422: if (is_array($value)) {
2423: $attributes['value'] = isset($value[$select]) ? $value[$select] : null;
2424: } else {
2425: if (empty($value)) {
2426: if (!$attributes['empty']) {
2427: $attributes['value'] = 'now';
2428: }
2429: } else {
2430: $attributes['value'] = $value;
2431: }
2432: }
2433: }
2434: return $attributes;
2435: }
2436:
2437: 2438: 2439: 2440: 2441: 2442: 2443: 2444: 2445: 2446: 2447: 2448: 2449: 2450:
2451: public function meridian($fieldName, $attributes = array()) {
2452: $attributes += array('empty' => true, 'value' => null);
2453: if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
2454: if (is_array($value)) {
2455: $meridian = null;
2456: extract($value);
2457: $attributes['value'] = $meridian;
2458: } else {
2459: if (empty($value)) {
2460: if (!$attributes['empty']) {
2461: $attributes['value'] = date('a');
2462: }
2463: } else {
2464: $date = date_create($attributes['value']);
2465: $attributes['value'] = null;
2466: if ($date) {
2467: $attributes['value'] = $date->format('a');
2468: }
2469: }
2470: }
2471: }
2472:
2473: if ($attributes['value'] === false) {
2474: $attributes['value'] = null;
2475: }
2476: return $this->select(
2477: $fieldName . ".meridian", $this->_generateOptions('meridian'),
2478: $attributes
2479: );
2480: }
2481:
2482: 2483: 2484: 2485: 2486: 2487: 2488: 2489: 2490: 2491: 2492: 2493: 2494: 2495: 2496: 2497: 2498: 2499: 2500: 2501: 2502: 2503: 2504: 2505:
2506: public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $attributes = array()) {
2507: $attributes += array('empty' => true, 'value' => null);
2508: $year = $month = $day = $hour = $min = $meridian = null;
2509:
2510: if (empty($attributes['value'])) {
2511: $attributes = $this->value($attributes, $fieldName);
2512: }
2513:
2514: if ($attributes['value'] === null && $attributes['empty'] != true) {
2515: $attributes['value'] = time();
2516: if (!empty($attributes['maxYear']) && $attributes['maxYear'] < date('Y')) {
2517: $attributes['value'] = strtotime(date($attributes['maxYear'] . '-m-d'));
2518: }
2519: }
2520:
2521: if (!empty($attributes['value'])) {
2522: list($year, $month, $day, $hour, $min, $meridian) = $this->_getDateTimeValue(
2523: $attributes['value'],
2524: $timeFormat
2525: );
2526: }
2527:
2528: $defaults = array(
2529: 'minYear' => null, 'maxYear' => null, 'separator' => '-',
2530: 'interval' => 1, 'monthNames' => true, 'round' => null
2531: );
2532: $attributes = array_merge($defaults, (array)$attributes);
2533: if (isset($attributes['minuteInterval'])) {
2534: $attributes['interval'] = $attributes['minuteInterval'];
2535: unset($attributes['minuteInterval']);
2536: }
2537: $minYear = $attributes['minYear'];
2538: $maxYear = $attributes['maxYear'];
2539: $separator = $attributes['separator'];
2540: $interval = $attributes['interval'];
2541: $monthNames = $attributes['monthNames'];
2542: $round = $attributes['round'];
2543: $attributes = array_diff_key($attributes, $defaults);
2544:
2545: if (!empty($interval) && $interval > 1 && !empty($min)) {
2546: $current = new DateTime();
2547: if ($year !== null) {
2548: $current->setDate($year, $month, $day);
2549: }
2550: if ($hour !== null) {
2551: $current->setTime($hour, $min);
2552: }
2553: $changeValue = $min * (1 / $interval);
2554: switch ($round) {
2555: case 'up':
2556: $changeValue = ceil($changeValue);
2557: break;
2558: case 'down':
2559: $changeValue = floor($changeValue);
2560: break;
2561: default:
2562: $changeValue = round($changeValue);
2563: }
2564: $change = ($changeValue * $interval) - $min;
2565: $current->modify($change > 0 ? "+$change minutes" : "$change minutes");
2566: $format = ($timeFormat == 12) ? 'Y m d h i a' : 'Y m d H i a';
2567: $newTime = explode(' ', $current->format($format));
2568: list($year, $month, $day, $hour, $min, $meridian) = $newTime;
2569: }
2570:
2571: $keys = array('Day', 'Month', 'Year', 'Hour', 'Minute', 'Meridian');
2572: $attrs = array_fill_keys($keys, $attributes);
2573:
2574: $hasId = isset($attributes['id']);
2575: if ($hasId && is_array($attributes['id'])) {
2576:
2577: $attributes['id'] += array(
2578: 'month' => '',
2579: 'year' => '',
2580: 'day' => '',
2581: 'hour' => '',
2582: 'minute' => '',
2583: 'meridian' => ''
2584: );
2585: foreach ($keys as $key) {
2586: $attrs[$key]['id'] = $attributes['id'][strtolower($key)];
2587: }
2588: }
2589: if ($hasId && is_string($attributes['id'])) {
2590:
2591: foreach ($keys as $key) {
2592: $attrs[$key]['id'] = $attributes['id'] . $key;
2593: }
2594: }
2595:
2596: if (is_array($attributes['empty'])) {
2597: $attributes['empty'] += array(
2598: 'month' => true,
2599: 'year' => true,
2600: 'day' => true,
2601: 'hour' => true,
2602: 'minute' => true,
2603: 'meridian' => true
2604: );
2605: foreach ($keys as $key) {
2606: $attrs[$key]['empty'] = $attributes['empty'][strtolower($key)];
2607: }
2608: }
2609:
2610: $selects = array();
2611: foreach (preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY) as $char) {
2612: switch ($char) {
2613: case 'Y':
2614: $attrs['Year']['value'] = $year;
2615: $selects[] = $this->year(
2616: $fieldName, $minYear, $maxYear, $attrs['Year']
2617: );
2618: break;
2619: case 'M':
2620: $attrs['Month']['value'] = $month;
2621: $attrs['Month']['monthNames'] = $monthNames;
2622: $selects[] = $this->month($fieldName, $attrs['Month']);
2623: break;
2624: case 'D':
2625: $attrs['Day']['value'] = $day;
2626: $selects[] = $this->day($fieldName, $attrs['Day']);
2627: break;
2628: }
2629: }
2630: $opt = implode($separator, $selects);
2631:
2632: $attrs['Minute']['interval'] = $interval;
2633: switch ($timeFormat) {
2634: case '24':
2635: $attrs['Hour']['value'] = $hour;
2636: $attrs['Minute']['value'] = $min;
2637: $opt .= $this->hour($fieldName, true, $attrs['Hour']) . ':' .
2638: $this->minute($fieldName, $attrs['Minute']);
2639: break;
2640: case '12':
2641: $attrs['Hour']['value'] = $hour;
2642: $attrs['Minute']['value'] = $min;
2643: $attrs['Meridian']['value'] = $meridian;
2644: $opt .= $this->hour($fieldName, false, $attrs['Hour']) . ':' .
2645: $this->minute($fieldName, $attrs['Minute']) . ' ' .
2646: $this->meridian($fieldName, $attrs['Meridian']);
2647: break;
2648: }
2649: return $opt;
2650: }
2651:
2652: 2653: 2654: 2655: 2656: 2657: 2658:
2659: protected function _getDateTimeValue($value, $timeFormat) {
2660: $year = $month = $day = $hour = $min = $meridian = null;
2661: if (is_array($value)) {
2662: extract($value);
2663: if ($meridian === 'pm') {
2664: $hour += 12;
2665: }
2666: return array($year, $month, $day, $hour, $min, $meridian);
2667: }
2668:
2669: if (is_numeric($value)) {
2670: $value = strftime('%Y-%m-%d %H:%M:%S', $value);
2671: }
2672: $meridian = 'am';
2673: $pos = strpos($value, '-');
2674: if ($pos !== false) {
2675: $date = explode('-', $value);
2676: $days = explode(' ', $date[2]);
2677: $day = $days[0];
2678: $month = $date[1];
2679: $year = $date[0];
2680: } else {
2681: $days[1] = $value;
2682: }
2683:
2684: if (!empty($timeFormat)) {
2685: $time = explode(':', $days[1]);
2686:
2687: if ($time[0] >= 12) {
2688: $meridian = 'pm';
2689: }
2690: $hour = $min = null;
2691: if (isset($time[1])) {
2692: $hour = $time[0];
2693: $min = $time[1];
2694: }
2695: }
2696: return array($year, $month, $day, $hour, $min, $meridian);
2697: }
2698:
2699: 2700: 2701: 2702: 2703: 2704: 2705: 2706:
2707: protected function _name($options = array(), $field = null, $key = 'name') {
2708: if ($this->requestType === 'get') {
2709: if ($options === null) {
2710: $options = array();
2711: } elseif (is_string($options)) {
2712: $field = $options;
2713: $options = 0;
2714: }
2715:
2716: if (!empty($field)) {
2717: $this->setEntity($field);
2718: }
2719:
2720: if (is_array($options) && isset($options[$key])) {
2721: return $options;
2722: }
2723:
2724: $entity = $this->entity();
2725: $model = $this->model();
2726: $name = $model === $entity[0] && isset($entity[1]) ? $entity[1] : $entity[0];
2727: $last = $entity[count($entity) - 1];
2728: if (in_array($last, $this->_fieldSuffixes)) {
2729: $name .= '[' . $last . ']';
2730: }
2731:
2732: if (is_array($options)) {
2733: $options[$key] = $name;
2734: return $options;
2735: }
2736: return $name;
2737: }
2738: return parent::_name($options, $field, $key);
2739: }
2740:
2741: 2742: 2743: 2744: 2745: 2746: 2747: 2748: 2749:
2750: protected function _selectOptions($elements = array(), $parents = array(), $showParents = null, $attributes = array()) {
2751: $select = array();
2752: $attributes = array_merge(
2753: array('escape' => true, 'style' => null, 'value' => null, 'class' => null),
2754: $attributes
2755: );
2756: $selectedIsEmpty = ($attributes['value'] === '' || $attributes['value'] === null);
2757: $selectedIsArray = is_array($attributes['value']);
2758:
2759: $this->_domIdSuffixes = array();
2760: foreach ($elements as $name => $title) {
2761: $htmlOptions = array();
2762: if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
2763: if (!empty($name)) {
2764: if ($attributes['style'] === 'checkbox') {
2765: $select[] = $this->Html->useTag('fieldsetend');
2766: } else {
2767: $select[] = $this->Html->useTag('optiongroupend');
2768: }
2769: $parents[] = $name;
2770: }
2771: $select = array_merge($select, $this->_selectOptions(
2772: $title, $parents, $showParents, $attributes
2773: ));
2774:
2775: if (!empty($name)) {
2776: $name = $attributes['escape'] ? h($name) : $name;
2777: if ($attributes['style'] === 'checkbox') {
2778: $select[] = $this->Html->useTag('fieldsetstart', $name);
2779: } else {
2780: $select[] = $this->Html->useTag('optiongroup', $name, '');
2781: }
2782: }
2783: $name = null;
2784: } elseif (is_array($title)) {
2785: $htmlOptions = $title;
2786: $name = $title['value'];
2787: $title = $title['name'];
2788: unset($htmlOptions['name'], $htmlOptions['value']);
2789: }
2790:
2791: if ($name !== null) {
2792: $isNumeric = is_numeric($name);
2793: if ((!$selectedIsArray && !$selectedIsEmpty && (string)$attributes['value'] == (string)$name) ||
2794: ($selectedIsArray && in_array((string)$name, $attributes['value'], !$isNumeric))
2795: ) {
2796: if ($attributes['style'] === 'checkbox') {
2797: $htmlOptions['checked'] = true;
2798: } else {
2799: $htmlOptions['selected'] = 'selected';
2800: }
2801: }
2802:
2803: if ($showParents || (!in_array($title, $parents))) {
2804: $title = ($attributes['escape']) ? h($title) : $title;
2805:
2806: $hasDisabled = !empty($attributes['disabled']);
2807: if ($hasDisabled) {
2808: $disabledIsArray = is_array($attributes['disabled']);
2809: if ($disabledIsArray) {
2810: $disabledIsNumeric = is_numeric($name);
2811: }
2812: }
2813: if ($hasDisabled &&
2814: $disabledIsArray &&
2815: in_array((string)$name, $attributes['disabled'], !$disabledIsNumeric)
2816: ) {
2817: $htmlOptions['disabled'] = 'disabled';
2818: }
2819: if ($hasDisabled && !$disabledIsArray && $attributes['style'] === 'checkbox') {
2820: $htmlOptions['disabled'] = $attributes['disabled'] === true ? 'disabled' : $attributes['disabled'];
2821: }
2822:
2823: if ($attributes['style'] === 'checkbox') {
2824: $htmlOptions['value'] = $name;
2825:
2826: $tagName = $attributes['id'] . $this->domIdSuffix($name);
2827: $htmlOptions['id'] = $tagName;
2828: $label = array('for' => $tagName);
2829:
2830: if (isset($htmlOptions['checked']) && $htmlOptions['checked'] === true) {
2831: $label['class'] = 'selected';
2832: }
2833:
2834: $name = $attributes['name'];
2835:
2836: if (empty($attributes['class'])) {
2837: $attributes['class'] = 'checkbox';
2838: } elseif ($attributes['class'] === 'form-error') {
2839: $attributes['class'] = 'checkbox ' . $attributes['class'];
2840: }
2841: $label = $this->label(null, $title, $label);
2842: $item = $this->Html->useTag('checkboxmultiple', $name, $htmlOptions);
2843: $select[] = $this->Html->div($attributes['class'], $item . $label);
2844: } else {
2845: if ($attributes['escape']) {
2846: $name = h($name);
2847: }
2848: $select[] = $this->Html->useTag('selectoption', $name, $htmlOptions, $title);
2849: }
2850: }
2851: }
2852: }
2853:
2854: return array_reverse($select, true);
2855: }
2856:
2857: 2858: 2859: 2860: 2861: 2862: 2863:
2864: protected function _generateOptions($name, $options = array()) {
2865: if (!empty($this->options[$name])) {
2866: return $this->options[$name];
2867: }
2868: $data = array();
2869:
2870: switch ($name) {
2871: case 'minute':
2872: if (isset($options['interval'])) {
2873: $interval = $options['interval'];
2874: } else {
2875: $interval = 1;
2876: }
2877: $i = 0;
2878: while ($i < 60) {
2879: $data[sprintf('%02d', $i)] = sprintf('%02d', $i);
2880: $i += $interval;
2881: }
2882: break;
2883: case 'hour':
2884: for ($i = 1; $i <= 12; $i++) {
2885: $data[sprintf('%02d', $i)] = $i;
2886: }
2887: break;
2888: case 'hour24':
2889: for ($i = 0; $i <= 23; $i++) {
2890: $data[sprintf('%02d', $i)] = $i;
2891: }
2892: break;
2893: case 'meridian':
2894: $data = array('am' => 'am', 'pm' => 'pm');
2895: break;
2896: case 'day':
2897: for ($i = 1; $i <= 31; $i++) {
2898: $data[sprintf('%02d', $i)] = $i;
2899: }
2900: break;
2901: case 'month':
2902: if ($options['monthNames'] === true) {
2903: $data['01'] = __d('cake', 'January');
2904: $data['02'] = __d('cake', 'February');
2905: $data['03'] = __d('cake', 'March');
2906: $data['04'] = __d('cake', 'April');
2907: $data['05'] = __d('cake', 'May');
2908: $data['06'] = __d('cake', 'June');
2909: $data['07'] = __d('cake', 'July');
2910: $data['08'] = __d('cake', 'August');
2911: $data['09'] = __d('cake', 'September');
2912: $data['10'] = __d('cake', 'October');
2913: $data['11'] = __d('cake', 'November');
2914: $data['12'] = __d('cake', 'December');
2915: } elseif (is_array($options['monthNames'])) {
2916: $data = $options['monthNames'];
2917: } else {
2918: for ($m = 1; $m <= 12; $m++) {
2919: $data[sprintf("%02s", $m)] = strftime("%m", mktime(1, 1, 1, $m, 1, 1999));
2920: }
2921: }
2922: break;
2923: case 'year':
2924: $current = (int)date('Y');
2925:
2926: $min = !isset($options['min']) ? $current - 20 : (int)$options['min'];
2927: $max = !isset($options['max']) ? $current + 20 : (int)$options['max'];
2928:
2929: if ($min > $max) {
2930: list($min, $max) = array($max, $min);
2931: }
2932: if (!empty($options['value']) &&
2933: (int)$options['value'] < $min &&
2934: (int)$options['value'] > 0
2935: ) {
2936: $min = (int)$options['value'];
2937: } elseif (!empty($options['value']) && (int)$options['value'] > $max) {
2938: $max = (int)$options['value'];
2939: }
2940:
2941: for ($i = $min; $i <= $max; $i++) {
2942: $data[$i] = $i;
2943: }
2944: if ($options['order'] !== 'asc') {
2945: $data = array_reverse($data, true);
2946: }
2947: break;
2948: }
2949: $this->_options[$name] = $data;
2950: return $this->_options[$name];
2951: }
2952:
2953: 2954: 2955: 2956: 2957: 2958: 2959: 2960: 2961: 2962: 2963: 2964: 2965: 2966: 2967: 2968: 2969:
2970: protected function _initInputField($field, $options = array()) {
2971: if (isset($options['secure'])) {
2972: $secure = $options['secure'];
2973: unset($options['secure']);
2974: } else {
2975: $secure = (isset($this->request['_Token']) && !empty($this->request['_Token']));
2976: }
2977:
2978: $disabledIndex = array_search('disabled', $options, true);
2979: if (is_int($disabledIndex)) {
2980: unset($options[$disabledIndex]);
2981: $options['disabled'] = true;
2982: }
2983:
2984: $result = parent::_initInputField($field, $options);
2985: if ($this->tagIsInvalid() !== false) {
2986: $result = $this->addClass($result, 'form-error');
2987: }
2988:
2989: $isDisabled = false;
2990: if (isset($result['disabled'])) {
2991: $isDisabled = (
2992: $result['disabled'] === true ||
2993: $result['disabled'] === 'disabled' ||
2994: (is_array($result['disabled']) &&
2995: !empty($result['options']) &&
2996: array_diff($result['options'], $result['disabled']) === array()
2997: )
2998: );
2999: }
3000: if ($isDisabled) {
3001: return $result;
3002: }
3003:
3004: if (!isset($result['required']) &&
3005: $this->_introspectModel($this->model(), 'validates', $this->field())
3006: ) {
3007: $result['required'] = true;
3008: }
3009:
3010: if ($secure === self::SECURE_SKIP) {
3011: return $result;
3012: }
3013:
3014: $this->_secure($secure, $this->_secureFieldName($options));
3015: return $result;
3016: }
3017:
3018: 3019: 3020: 3021: 3022: 3023: 3024: 3025: 3026:
3027: protected function _secureFieldName($options) {
3028: if (isset($options['name'])) {
3029: preg_match_all('/\[(.*?)\]/', $options['name'], $matches);
3030: if (isset($matches[1])) {
3031: return $matches[1];
3032: }
3033: }
3034: return null;
3035: }
3036:
3037: 3038: 3039: 3040: 3041: 3042:
3043: protected function _lastAction($url) {
3044: $action = Router::url($url, true);
3045: $query = parse_url($action, PHP_URL_QUERY);
3046: $query = $query ? '?' . $query : '';
3047: $this->_lastAction = parse_url($action, PHP_URL_PATH) . $query;
3048: }
3049:
3050: 3051: 3052: 3053: 3054: 3055: 3056:
3057: public function inputDefaults($defaults = null, $merge = false) {
3058: if ($defaults !== null) {
3059: if ($merge) {
3060: $this->_inputDefaults = array_merge($this->_inputDefaults, (array)$defaults);
3061: } else {
3062: $this->_inputDefaults = (array)$defaults;
3063: }
3064: }
3065: return $this->_inputDefaults;
3066: }
3067:
3068: }
3069: