CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.8 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.8
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 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

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper
  • None

Classes

  • CacheHelper
  • FlashHelper
  • FormHelper
  • HtmlHelper
  • JqueryEngineHelper
  • JsBaseEngineHelper
  • JsHelper
  • MootoolsEngineHelper
  • NumberHelper
  • PaginatorHelper
  • PrototypeEngineHelper
  • RssHelper
  • SessionHelper
  • TextHelper
  • TimeHelper
   1: <?php
   2: /**
   3:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
   4:  * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11:  * @link          http://cakephp.org CakePHP(tm) Project
  12:  * @package       Cake.View.Helper
  13:  * @since         CakePHP(tm) v 0.10.0.1076
  14:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
  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:  * Form helper library.
  24:  *
  25:  * Automatic generation of HTML FORMs from given data.
  26:  *
  27:  * @package       Cake.View.Helper
  28:  * @property      HtmlHelper $Html
  29:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
  30:  */
  31: class FormHelper extends AppHelper {
  32: 
  33: /**
  34:  * Other helpers used by FormHelper
  35:  *
  36:  * @var array
  37:  */
  38:     public $helpers = array('Html');
  39: 
  40: /**
  41:  * Options used by DateTime fields
  42:  *
  43:  * @var array
  44:  */
  45:     protected $_options = array(
  46:         'day' => array(), 'minute' => array(), 'hour' => array(),
  47:         'month' => array(), 'year' => array(), 'meridian' => array()
  48:     );
  49: 
  50: /**
  51:  * List of fields created, used with secure forms.
  52:  *
  53:  * @var array
  54:  */
  55:     public $fields = array();
  56: 
  57: /**
  58:  * Constant used internally to skip the securing process,
  59:  * and neither add the field to the hash or to the unlocked fields.
  60:  *
  61:  * @var string
  62:  */
  63:     const SECURE_SKIP = 'skip';
  64: 
  65: /**
  66:  * Defines the type of form being created. Set by FormHelper::create().
  67:  *
  68:  * @var string
  69:  */
  70:     public $requestType = null;
  71: 
  72: /**
  73:  * The default model being used for the current form.
  74:  *
  75:  * @var string
  76:  */
  77:     public $defaultModel = null;
  78: 
  79: /**
  80:  * Persistent default options used by input(). Set by FormHelper::create().
  81:  *
  82:  * @var array
  83:  */
  84:     protected $_inputDefaults = array();
  85: 
  86: /**
  87:  * An array of field names that have been excluded from
  88:  * the Token hash used by SecurityComponent's validatePost method
  89:  *
  90:  * @see FormHelper::_secure()
  91:  * @see SecurityComponent::validatePost()
  92:  * @var array
  93:  */
  94:     protected $_unlockedFields = array();
  95: 
  96: /**
  97:  * Holds the model references already loaded by this helper
  98:  * product of trying to inspect them out of field names
  99:  *
 100:  * @var array
 101:  */
 102:     protected $_models = array();
 103: 
 104: /**
 105:  * Holds all the validation errors for models loaded and inspected
 106:  * it can also be set manually to be able to display custom error messages
 107:  * in the any of the input fields generated by this helper
 108:  *
 109:  * @var array
 110:  */
 111:     public $validationErrors = array();
 112: 
 113: /**
 114:  * Holds already used DOM ID suffixes to avoid collisions with multiple form field elements.
 115:  *
 116:  * @var array
 117:  */
 118:     protected $_domIdSuffixes = array();
 119: 
 120: /**
 121:  * The action attribute value of the last created form.
 122:  * Used to make form/request specific hashes for SecurityComponent.
 123:  *
 124:  * @var string
 125:  */
 126:     protected $_lastAction = '';
 127: 
 128: /**
 129:  * Copies the validationErrors variable from the View object into this instance
 130:  *
 131:  * @param View $View The View this helper is being attached to.
 132:  * @param array $settings Configuration settings for the helper.
 133:  */
 134:     public function __construct(View $View, $settings = array()) {
 135:         parent::__construct($View, $settings);
 136:         $this->validationErrors =& $View->validationErrors;
 137:     }
 138: 
 139: /**
 140:  * Guess the location for a model based on its name and tries to create a new instance
 141:  * or get an already created instance of the model
 142:  *
 143:  * @param string $model Model name.
 144:  * @return Model|null Model instance
 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:  * Inspects the model properties to extract information from them.
 185:  * Currently it can extract information from the the fields, the primary key and required fields
 186:  *
 187:  * The $key parameter accepts the following list of values:
 188:  *
 189:  * - key: Returns the name of the primary key for the model
 190:  * - fields: Returns the model schema
 191:  * - validates: returns the list of fields that are required
 192:  * - errors: returns the list of validation errors
 193:  *
 194:  * If the $field parameter is passed if will return the information for that sole field.
 195:  *
 196:  * `$this->_introspectModel('Post', 'fields', 'title');` will return the schema information for title column
 197:  *
 198:  * @param string $model name of the model to extract information from
 199:  * @param string $key name of the special information key to obtain (key, fields, validates, errors)
 200:  * @param string $field name of the model field to get information from
 201:  * @return mixed information extracted for the special key and field in a model
 202:  */
 203:     protected function _introspectModel($model, $key, $field = null) {
 204:         $object = $this->_getModel($model);
 205:         if (!$object) {
 206:             return null;
 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:  * Returns if a field is required to be filled based on validation properties from the validating object.
 256:  *
 257:  * @param CakeValidationSet $validationRules Validation rules set.
 258:  * @return bool true if field is required to be filled, false otherwise
 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:  * Returns false if given form field described by the current entity has no errors.
 279:  * Otherwise it returns the validation message
 280:  *
 281:  * @return mixed Either false when there are no errors, or an array of error
 282:  *    strings. An error string could be ''.
 283:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::tagIsInvalid
 284:  */
 285:     public function tagIsInvalid() {
 286:         $entity = $this->entity();
 287:         $model = array_shift($entity);
 288: 
 289:         // 0.Model.field. Fudge entity path
 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:  * Returns an HTML FORM element.
 311:  *
 312:  * ### Options:
 313:  *
 314:  * - `type` Form method defaults to POST
 315:  * - `action`  The controller action the form submits to, (optional). Deprecated since 2.8, use `url`.
 316:  * - `url`  The URL the form submits to. Can be a string or a URL array. If you use 'url'
 317:  *    you should leave 'action' undefined.
 318:  * - `default`  Allows for the creation of AJAX forms. Set this to false to prevent the default event handler.
 319:  *   Will create an onsubmit attribute if it doesn't not exist. If it does, default action suppression
 320:  *   will be appended.
 321:  * - `onsubmit` Used in conjunction with 'default' to create AJAX forms.
 322:  * - `inputDefaults` set the default $options for FormHelper::input(). Any options that would
 323:  *   be set when using FormHelper::input() can be set here. Options set with `inputDefaults`
 324:  *   can be overridden when calling input()
 325:  * - `encoding` Set the accept-charset encoding for the form. Defaults to `Configure::read('App.encoding')`
 326:  *
 327:  * @param mixed|null $model The model name for which the form is being defined. Should
 328:  *   include the plugin name for plugin models. e.g. `ContactManager.Contact`.
 329:  *   If an array is passed and $options argument is empty, the array will be used as options.
 330:  *   If `false` no model is used.
 331:  * @param array $options An array of html attributes and options.
 332:  * @return string A formatted opening FORM tag.
 333:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-create
 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['action'])) {
 383:             trigger_error('Using key `action` is deprecated, use `url` directly instead.', E_USER_DEPRECATED);
 384:         }
 385: 
 386:         if (is_array($options['url']) && isset($options['url']['action'])) {
 387:             $options['action'] = $options['url']['action'];
 388:         }
 389: 
 390:         if (!isset($options['id'])) {
 391:             $domId = isset($options['action']) ? $options['action'] : $this->request['action'];
 392:             $options['id'] = $this->domId($domId . 'Form');
 393:         }
 394: 
 395:         if ($options['action'] === null && $options['url'] === null) {
 396:             $options['action'] = $this->request->here(false);
 397:         } elseif (empty($options['url']) || is_array($options['url'])) {
 398:             if (empty($options['url']['controller'])) {
 399:                 if (!empty($model)) {
 400:                     $options['url']['controller'] = Inflector::underscore(Inflector::pluralize($model));
 401:                 } elseif (!empty($this->request->params['controller'])) {
 402:                     $options['url']['controller'] = Inflector::underscore($this->request->params['controller']);
 403:                 }
 404:             }
 405:             if (empty($options['action'])) {
 406:                 $options['action'] = $this->request->params['action'];
 407:             }
 408: 
 409:             $plugin = null;
 410:             if ($this->plugin) {
 411:                 $plugin = Inflector::underscore($this->plugin);
 412:             }
 413:             $actionDefaults = array(
 414:                 'plugin' => $plugin,
 415:                 'controller' => $this->_View->viewPath,
 416:                 'action' => $options['action'],
 417:             );
 418:             $options['action'] = array_merge($actionDefaults, (array)$options['url']);
 419:             if (!isset($options['action'][0]) && !empty($id)) {
 420:                 $options['action'][0] = $id;
 421:             }
 422:         } elseif (is_string($options['url'])) {
 423:             $options['action'] = $options['url'];
 424:         }
 425: 
 426:         switch (strtolower($options['type'])) {
 427:             case 'get':
 428:                 $htmlAttributes['method'] = 'get';
 429:                 break;
 430:             case 'file':
 431:                 $htmlAttributes['enctype'] = 'multipart/form-data';
 432:                 $options['type'] = ($created) ? 'put' : 'post';
 433:             case 'post':
 434:             case 'put':
 435:             case 'delete':
 436:                 $append .= $this->hidden('_method', array(
 437:                     'name' => '_method', 'value' => strtoupper($options['type']), 'id' => null,
 438:                     'secure' => static::SECURE_SKIP
 439:                 ));
 440:             default:
 441:                 $htmlAttributes['method'] = 'post';
 442:         }
 443:         $this->requestType = strtolower($options['type']);
 444: 
 445:         $action = null;
 446:         if ($options['action'] !== false && $options['url'] !== false) {
 447:             $action = $this->url($options['action']);
 448:         }
 449:         unset($options['url']);
 450: 
 451:         $this->_lastAction($options['action']);
 452:         unset($options['type'], $options['action']);
 453: 
 454:         if (!$options['default']) {
 455:             if (!isset($options['onsubmit'])) {
 456:                 $options['onsubmit'] = '';
 457:             }
 458:             $htmlAttributes['onsubmit'] = $options['onsubmit'] . 'event.returnValue = false; return false;';
 459:         }
 460:         unset($options['default']);
 461: 
 462:         if (!empty($options['encoding'])) {
 463:             $htmlAttributes['accept-charset'] = $options['encoding'];
 464:             unset($options['encoding']);
 465:         }
 466: 
 467:         $htmlAttributes = array_merge($options, $htmlAttributes);
 468: 
 469:         $this->fields = array();
 470:         if ($this->requestType !== 'get') {
 471:             $append .= $this->_csrfField();
 472:         }
 473: 
 474:         if (!empty($append)) {
 475:             $append = $this->Html->useTag('hiddenblock', $append);
 476:         }
 477: 
 478:         if ($model !== false) {
 479:             $this->setEntity($model, true);
 480:             $this->_introspectModel($model, 'fields');
 481:         }
 482: 
 483:         if ($action === null) {
 484:             return $this->Html->useTag('formwithoutaction', $htmlAttributes) . $append;
 485:         }
 486: 
 487:         return $this->Html->useTag('form', $action, $htmlAttributes) . $append;
 488:     }
 489: 
 490: /**
 491:  * Return a CSRF input if the _Token is present.
 492:  * Used to secure forms in conjunction with SecurityComponent
 493:  *
 494:  * @return string
 495:  */
 496:     protected function _csrfField() {
 497:         if (empty($this->request->params['_Token'])) {
 498:             return '';
 499:         }
 500:         if (!empty($this->request['_Token']['unlockedFields'])) {
 501:             foreach ((array)$this->request['_Token']['unlockedFields'] as $unlocked) {
 502:                 $this->_unlockedFields[] = $unlocked;
 503:             }
 504:         }
 505:         return $this->hidden('_Token.key', array(
 506:             'value' => $this->request->params['_Token']['key'], 'id' => 'Token' . mt_rand(),
 507:             'secure' => static::SECURE_SKIP
 508:         ));
 509:     }
 510: 
 511: /**
 512:  * Closes an HTML form, cleans up values set by FormHelper::create(), and writes hidden
 513:  * input fields where appropriate.
 514:  *
 515:  * If $options is set a form submit button will be created. Options can be either a string or an array.
 516:  *
 517:  * ```
 518:  * array usage:
 519:  *
 520:  * array('label' => 'save'); value="save"
 521:  * array('label' => 'save', 'name' => 'Whatever'); value="save" name="Whatever"
 522:  * array('name' => 'Whatever'); value="Submit" name="Whatever"
 523:  * array('label' => 'save', 'name' => 'Whatever', 'div' => 'good') <div class="good"> value="save" name="Whatever"
 524:  * array('label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')); <div class="good"> value="save" name="Whatever"
 525:  * ```
 526:  *
 527:  * If $secureAttributes is set, these html attributes will be merged into the hidden input tags generated for the
 528:  * Security Component. This is especially useful to set HTML5 attributes like 'form'
 529:  *
 530:  * @param string|array $options as a string will use $options as the value of button,
 531:  * @param array $secureAttributes will be passed as html attributes into the hidden input elements generated for the
 532:  *   Security Component.
 533:  * @return string a closing FORM tag optional submit button.
 534:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#closing-the-form
 535:  */
 536:     public function end($options = null, $secureAttributes = array()) {
 537:         $out = null;
 538:         $submit = null;
 539: 
 540:         if ($options !== null) {
 541:             $submitOptions = array();
 542:             if (is_string($options)) {
 543:                 $submit = $options;
 544:             } else {
 545:                 if (isset($options['label'])) {
 546:                     $submit = $options['label'];
 547:                     unset($options['label']);
 548:                 }
 549:                 $submitOptions = $options;
 550:             }
 551:             $out .= $this->submit($submit, $submitOptions);
 552:         }
 553:         if ($this->requestType !== 'get' &&
 554:             isset($this->request['_Token']) &&
 555:             !empty($this->request['_Token'])
 556:         ) {
 557:             $out .= $this->secure($this->fields, $secureAttributes);
 558:             $this->fields = array();
 559:         }
 560:         $this->setEntity(null);
 561:         $out .= $this->Html->useTag('formend');
 562: 
 563:         $this->_unlockedFields = array();
 564:         $this->_View->modelScope = false;
 565:         $this->requestType = null;
 566:         return $out;
 567:     }
 568: 
 569: /**
 570:  * Generates a hidden field with a security hash based on the fields used in
 571:  * the form.
 572:  *
 573:  * If $secureAttributes is set, these html attributes will be merged into
 574:  * the hidden input tags generated for the Security Component. This is
 575:  * especially useful to set HTML5 attributes like 'form'.
 576:  *
 577:  * @param array|null $fields If set specifies the list of fields to use when
 578:  *    generating the hash, else $this->fields is being used.
 579:  * @param array $secureAttributes will be passed as html attributes into the hidden
 580:  *    input elements generated for the Security Component.
 581:  * @return string|null A hidden input field with a security hash, otherwise null.
 582:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::secure
 583:  */
 584:     public function secure($fields = array(), $secureAttributes = array()) {
 585:         if (!isset($this->request['_Token']) || empty($this->request['_Token'])) {
 586:             return null;
 587:         }
 588:         $locked = array();
 589:         $unlockedFields = $this->_unlockedFields;
 590: 
 591:         foreach ($fields as $key => $value) {
 592:             if (!is_int($key)) {
 593:                 $locked[$key] = $value;
 594:                 unset($fields[$key]);
 595:             }
 596:         }
 597: 
 598:         sort($unlockedFields, SORT_STRING);
 599:         sort($fields, SORT_STRING);
 600:         ksort($locked, SORT_STRING);
 601:         $fields += $locked;
 602: 
 603:         $locked = implode(array_keys($locked), '|');
 604:         $unlocked = implode($unlockedFields, '|');
 605:         $hashParts = array(
 606:             $this->_lastAction,
 607:             serialize($fields),
 608:             $unlocked,
 609:             Configure::read('Security.salt')
 610:         );
 611:         $fields = Security::hash(implode('', $hashParts), 'sha1');
 612: 
 613:         $tokenFields = array_merge($secureAttributes, array(
 614:             'value' => urlencode($fields . ':' . $locked),
 615:             'id' => 'TokenFields' . mt_rand(),
 616:             'secure' => static::SECURE_SKIP,
 617:         ));
 618:         $out = $this->hidden('_Token.fields', $tokenFields);
 619:         $tokenUnlocked = array_merge($secureAttributes, array(
 620:             'value' => urlencode($unlocked),
 621:             'id' => 'TokenUnlocked' . mt_rand(),
 622:             'secure' => static::SECURE_SKIP,
 623:         ));
 624:         $out .= $this->hidden('_Token.unlocked', $tokenUnlocked);
 625:         return $this->Html->useTag('hiddenblock', $out);
 626:     }
 627: 
 628: /**
 629:  * Add to or get the list of fields that are currently unlocked.
 630:  * Unlocked fields are not included in the field hash used by SecurityComponent
 631:  * unlocking a field once its been added to the list of secured fields will remove
 632:  * it from the list of fields.
 633:  *
 634:  * @param string $name The dot separated name for the field.
 635:  * @return mixed Either null, or the list of fields.
 636:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::unlockField
 637:  */
 638:     public function unlockField($name = null) {
 639:         if ($name === null) {
 640:             return $this->_unlockedFields;
 641:         }
 642:         if (!in_array($name, $this->_unlockedFields)) {
 643:             $this->_unlockedFields[] = $name;
 644:         }
 645:         $index = array_search($name, $this->fields);
 646:         if ($index !== false) {
 647:             unset($this->fields[$index]);
 648:         }
 649:         unset($this->fields[$name]);
 650:     }
 651: 
 652: /**
 653:  * Determine which fields of a form should be used for hash.
 654:  * Populates $this->fields
 655:  *
 656:  * @param bool $lock Whether this field should be part of the validation
 657:  *     or excluded as part of the unlockedFields.
 658:  * @param string|array $field Reference to field to be secured. Should be dot separated to indicate nesting.
 659:  * @param mixed $value Field value, if value should not be tampered with.
 660:  * @return void
 661:  */
 662:     protected function _secure($lock, $field = null, $value = null) {
 663:         if (!$field) {
 664:             $field = $this->entity();
 665:         } elseif (is_string($field)) {
 666:             $field = explode('.', $field);
 667:         }
 668:         if (is_array($field)) {
 669:             $field = Hash::filter($field);
 670:         }
 671: 
 672:         foreach ($this->_unlockedFields as $unlockField) {
 673:             $unlockParts = explode('.', $unlockField);
 674:             if (array_values(array_intersect($field, $unlockParts)) === $unlockParts) {
 675:                 return;
 676:             }
 677:         }
 678: 
 679:         $field = implode('.', $field);
 680:         $field = preg_replace('/(\.\d+)+$/', '', $field);
 681: 
 682:         if ($lock) {
 683:             if (!in_array($field, $this->fields)) {
 684:                 if ($value !== null) {
 685:                     return $this->fields[$field] = $value;
 686:                 } elseif (isset($this->fields[$field]) && $value === null) {
 687:                     unset($this->fields[$field]);
 688:                 }
 689:                 $this->fields[] = $field;
 690:             }
 691:         } else {
 692:             $this->unlockField($field);
 693:         }
 694:     }
 695: 
 696: /**
 697:  * Returns true if there is an error for the given field, otherwise false
 698:  *
 699:  * @param string $field This should be "Modelname.fieldname"
 700:  * @return bool If there are errors this method returns true, else false.
 701:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::isFieldError
 702:  */
 703:     public function isFieldError($field) {
 704:         $this->setEntity($field);
 705:         return (bool)$this->tagIsInvalid();
 706:     }
 707: 
 708: /**
 709:  * Returns a formatted error message for given FORM field, NULL if no errors.
 710:  *
 711:  * ### Options:
 712:  *
 713:  * - `escape` boolean - Whether or not to html escape the contents of the error.
 714:  * - `wrap` mixed - Whether or not the error message should be wrapped in a div. If a
 715:  *   string, will be used as the HTML tag to use.
 716:  * - `class` string - The class name for the error message
 717:  *
 718:  * @param string $field A field name, like "Modelname.fieldname"
 719:  * @param string|array $text Error message as string or array of messages.
 720:  *   If array contains `attributes` key it will be used as options for error container
 721:  * @param array $options Rendering options for <div /> wrapper tag
 722:  * @return string|null If there are errors this method returns an error message, otherwise null.
 723:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::error
 724:  */
 725:     public function error($field, $text = null, $options = array()) {
 726:         $defaults = array('wrap' => true, 'class' => 'error-message', 'escape' => true);
 727:         $options += $defaults;
 728:         $this->setEntity($field);
 729: 
 730:         $error = $this->tagIsInvalid();
 731:         if ($error === false) {
 732:             return null;
 733:         }
 734:         if (is_array($text)) {
 735:             if (isset($text['attributes']) && is_array($text['attributes'])) {
 736:                 $options = array_merge($options, $text['attributes']);
 737:                 unset($text['attributes']);
 738:             }
 739:             $tmp = array();
 740:             foreach ($error as &$e) {
 741:                 if (isset($text[$e])) {
 742:                     $tmp[] = $text[$e];
 743:                 } else {
 744:                     $tmp[] = $e;
 745:                 }
 746:             }
 747:             $text = $tmp;
 748:         }
 749: 
 750:         if ($text !== null) {
 751:             $error = $text;
 752:         }
 753:         if (is_array($error)) {
 754:             foreach ($error as &$e) {
 755:                 if (is_numeric($e)) {
 756:                     $e = __d('cake', 'Error in field %s', Inflector::humanize($this->field()));
 757:                 }
 758:             }
 759:         }
 760:         if ($options['escape']) {
 761:             $error = h($error);
 762:             unset($options['escape']);
 763:         }
 764:         if (is_array($error)) {
 765:             if (count($error) > 1) {
 766:                 $listParams = array();
 767:                 if (isset($options['listOptions'])) {
 768:                     if (is_string($options['listOptions'])) {
 769:                         $listParams[] = $options['listOptions'];
 770:                     } else {
 771:                         if (isset($options['listOptions']['itemOptions'])) {
 772:                             $listParams[] = $options['listOptions']['itemOptions'];
 773:                             unset($options['listOptions']['itemOptions']);
 774:                         } else {
 775:                             $listParams[] = array();
 776:                         }
 777:                         if (isset($options['listOptions']['tag'])) {
 778:                             $listParams[] = $options['listOptions']['tag'];
 779:                             unset($options['listOptions']['tag']);
 780:                         }
 781:                         array_unshift($listParams, $options['listOptions']);
 782:                     }
 783:                     unset($options['listOptions']);
 784:                 }
 785:                 array_unshift($listParams, $error);
 786:                 $error = call_user_func_array(array($this->Html, 'nestedList'), $listParams);
 787:             } else {
 788:                 $error = array_pop($error);
 789:             }
 790:         }
 791:         if ($options['wrap']) {
 792:             $tag = is_string($options['wrap']) ? $options['wrap'] : 'div';
 793:             unset($options['wrap']);
 794:             return $this->Html->tag($tag, $error, $options);
 795:         }
 796:         return $error;
 797:     }
 798: 
 799: /**
 800:  * Returns a formatted LABEL element for HTML FORMs. Will automatically generate
 801:  * a `for` attribute if one is not provided.
 802:  *
 803:  * ### Options
 804:  *
 805:  * - `for` - Set the for attribute, if its not defined the for attribute
 806:  *   will be generated from the $fieldName parameter using
 807:  *   FormHelper::domId().
 808:  *
 809:  * Examples:
 810:  *
 811:  * The text and for attribute are generated off of the fieldname
 812:  *
 813:  * ```
 814:  * echo $this->Form->label('Post.published');
 815:  * <label for="PostPublished">Published</label>
 816:  * ```
 817:  *
 818:  * Custom text:
 819:  *
 820:  * ```
 821:  * echo $this->Form->label('Post.published', 'Publish');
 822:  * <label for="PostPublished">Publish</label>
 823:  * ```
 824:  *
 825:  * Custom class name:
 826:  *
 827:  * ```
 828:  * echo $this->Form->label('Post.published', 'Publish', 'required');
 829:  * <label for="PostPublished" class="required">Publish</label>
 830:  * ```
 831:  *
 832:  * Custom attributes:
 833:  *
 834:  * ```
 835:  * echo $this->Form->label('Post.published', 'Publish', array(
 836:  *      'for' => 'post-publish'
 837:  * ));
 838:  * <label for="post-publish">Publish</label>
 839:  * ```
 840:  *
 841:  * *Warning* Unlike most FormHelper methods, this method does not automatically
 842:  * escape the $text parameter. You must escape the $text parameter yourself if you
 843:  * are using user supplied data.
 844:  *
 845:  * @param string $fieldName This should be "Modelname.fieldname"
 846:  * @param string $text Text that will appear in the label field. If
 847:  *   $text is left undefined the text will be inflected from the
 848:  *   fieldName.
 849:  * @param array|string $options An array of HTML attributes, or a string, to be used as a class name.
 850:  * @return string The formatted LABEL element
 851:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::label
 852:  */
 853:     public function label($fieldName = null, $text = null, $options = array()) {
 854:         if ($fieldName === null) {
 855:             $fieldName = implode('.', $this->entity());
 856:         }
 857: 
 858:         if ($text === null) {
 859:             if (strpos($fieldName, '.') !== false) {
 860:                 $fieldElements = explode('.', $fieldName);
 861:                 $text = array_pop($fieldElements);
 862:             } else {
 863:                 $text = $fieldName;
 864:             }
 865:             if (substr($text, -3) === '_id') {
 866:                 $text = substr($text, 0, -3);
 867:             }
 868:             $text = __(Inflector::humanize(Inflector::underscore($text)));
 869:         }
 870: 
 871:         if (is_string($options)) {
 872:             $options = array('class' => $options);
 873:         }
 874: 
 875:         if (isset($options['for'])) {
 876:             $labelFor = $options['for'];
 877:             unset($options['for']);
 878:         } else {
 879:             $labelFor = $this->domId($fieldName);
 880:         }
 881: 
 882:         return $this->Html->useTag('label', $labelFor, $options, $text);
 883:     }
 884: 
 885: /**
 886:  * Generate a set of inputs for `$fields`. If $fields is null the fields of current model
 887:  * will be used.
 888:  *
 889:  * You can customize individual inputs through `$fields`.
 890:  * ```
 891:  *  $this->Form->inputs(array(
 892:  *      'name' => array('label' => 'custom label')
 893:  *  ));
 894:  * ```
 895:  *
 896:  * In addition to controller fields output, `$fields` can be used to control legend
 897:  * and fieldset rendering.
 898:  * `$this->Form->inputs('My legend');` Would generate an input set with a custom legend.
 899:  * Passing `fieldset` and `legend` key in `$fields` array has been deprecated since 2.3,
 900:  * for more fine grained control use the `fieldset` and `legend` keys in `$options` param.
 901:  *
 902:  * @param array $fields An array of fields to generate inputs for, or null.
 903:  * @param array $blacklist A simple array of fields to not create inputs for.
 904:  * @param array $options Options array. Valid keys are:
 905:  * - `fieldset` Set to false to disable the fieldset. If a string is supplied it will be used as
 906:  *    the class name for the fieldset element.
 907:  * - `legend` Set to false to disable the legend for the generated input set. Or supply a string
 908:  *    to customize the legend text.
 909:  * @return string Completed form inputs.
 910:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::inputs
 911:  */
 912:     public function inputs($fields = null, $blacklist = null, $options = array()) {
 913:         $fieldset = $legend = true;
 914:         $modelFields = array();
 915:         $model = $this->model();
 916:         if ($model) {
 917:             $modelFields = array_keys((array)$this->_introspectModel($model, 'fields'));
 918:         }
 919:         if (is_array($fields)) {
 920:             if (array_key_exists('legend', $fields) && !in_array('legend', $modelFields)) {
 921:                 $legend = $fields['legend'];
 922:                 unset($fields['legend']);
 923:             }
 924: 
 925:             if (isset($fields['fieldset']) && !in_array('fieldset', $modelFields)) {
 926:                 $fieldset = $fields['fieldset'];
 927:                 unset($fields['fieldset']);
 928:             }
 929:         } elseif ($fields !== null) {
 930:             $fieldset = $legend = $fields;
 931:             if (!is_bool($fieldset)) {
 932:                 $fieldset = true;
 933:             }
 934:             $fields = array();
 935:         }
 936: 
 937:         if (isset($options['legend'])) {
 938:             $legend = $options['legend'];
 939:             unset($options['legend']);
 940:         }
 941: 
 942:         if (isset($options['fieldset'])) {
 943:             $fieldset = $options['fieldset'];
 944:             unset($options['fieldset']);
 945:         }
 946: 
 947:         if (empty($fields)) {
 948:             $fields = $modelFields;
 949:         }
 950: 
 951:         if ($legend === true) {
 952:             $actionName = __d('cake', 'New %s');
 953:             $isEdit = (
 954:                 strpos($this->request->params['action'], 'update') !== false ||
 955:                 strpos($this->request->params['action'], 'edit') !== false
 956:             );
 957:             if ($isEdit) {
 958:                 $actionName = __d('cake', 'Edit %s');
 959:             }
 960:             $modelName = Inflector::humanize(Inflector::underscore($model));
 961:             $legend = sprintf($actionName, __($modelName));
 962:         }
 963: 
 964:         $out = null;
 965:         foreach ($fields as $name => $options) {
 966:             if (is_numeric($name) && !is_array($options)) {
 967:                 $name = $options;
 968:                 $options = array();
 969:             }
 970:             $entity = explode('.', $name);
 971:             $blacklisted = (
 972:                 is_array($blacklist) &&
 973:                 (in_array($name, $blacklist) || in_array(end($entity), $blacklist))
 974:             );
 975:             if ($blacklisted) {
 976:                 continue;
 977:             }
 978:             $out .= $this->input($name, $options);
 979:         }
 980: 
 981:         if (is_string($fieldset)) {
 982:             $fieldsetClass = array('class' => $fieldset);
 983:         } else {
 984:             $fieldsetClass = '';
 985:         }
 986: 
 987:         if ($fieldset) {
 988:             if ($legend) {
 989:                 $out = $this->Html->useTag('legend', $legend) . $out;
 990:             }
 991:             $out = $this->Html->useTag('fieldset', $fieldsetClass, $out);
 992:         }
 993:         return $out;
 994:     }
 995: 
 996: /**
 997:  * Generates a form input element complete with label and wrapper div
 998:  *
 999:  * ### Options
1000:  *
1001:  * See each field type method for more information. Any options that are part of
1002:  * $attributes or $options for the different **type** methods can be included in `$options` for input().i
1003:  * Additionally, any unknown keys that are not in the list below, or part of the selected type's options
1004:  * will be treated as a regular html attribute for the generated input.
1005:  *
1006:  * - `type` - Force the type of widget you want. e.g. `type => 'select'`
1007:  * - `label` - Either a string label, or an array of options for the label. See FormHelper::label().
1008:  * - `div` - Either `false` to disable the div, or an array of options for the div.
1009:  *  See HtmlHelper::div() for more options.
1010:  * - `options` - For widgets that take options e.g. radio, select.
1011:  * - `error` - Control the error message that is produced. Set to `false` to disable any kind of error reporting (field
1012:  *    error and error messages).
1013:  * - `errorMessage` - Boolean to control rendering error messages (field error will still occur).
1014:  * - `empty` - String or boolean to enable empty select box options.
1015:  * - `before` - Content to place before the label + input.
1016:  * - `after` - Content to place after the label + input.
1017:  * - `between` - Content to place between the label + input.
1018:  * - `format` - Format template for element order. Any element that is not in the array, will not be in the output.
1019:  *  - Default input format order: array('before', 'label', 'between', 'input', 'after', 'error')
1020:  *  - Default checkbox format order: array('before', 'input', 'between', 'label', 'after', 'error')
1021:  *  - Hidden input will not be formatted
1022:  *  - Radio buttons cannot have the order of input and label elements controlled with these settings.
1023:  *
1024:  * @param string $fieldName This should be "Modelname.fieldname"
1025:  * @param array $options Each type of input takes different options.
1026:  * @return string Completed form widget.
1027:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#creating-form-elements
1028:  */
1029:     public function input($fieldName, $options = array()) {
1030:         $this->setEntity($fieldName);
1031:         $options = $this->_parseOptions($options);
1032: 
1033:         $divOptions = $this->_divOptions($options);
1034:         unset($options['div']);
1035: 
1036:         if ($options['type'] === 'radio' && isset($options['options'])) {
1037:             $radioOptions = (array)$options['options'];
1038:             unset($options['options']);
1039:         }
1040: 
1041:         $label = $this->_getLabel($fieldName, $options);
1042:         if ($options['type'] !== 'radio') {
1043:             unset($options['label']);
1044:         }
1045: 
1046:         $error = $this->_extractOption('error', $options, null);
1047:         unset($options['error']);
1048: 
1049:         $errorMessage = $this->_extractOption('errorMessage', $options, true);
1050:         unset($options['errorMessage']);
1051: 
1052:         $selected = $this->_extractOption('selected', $options, null);
1053:         unset($options['selected']);
1054: 
1055:         if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time') {
1056:             $dateFormat = $this->_extractOption('dateFormat', $options, 'MDY');
1057:             $timeFormat = $this->_extractOption('timeFormat', $options, 12);
1058:             unset($options['dateFormat'], $options['timeFormat']);
1059:         }
1060: 
1061:         $type = $options['type'];
1062:         $out = array('before' => $options['before'], 'label' => $label, 'between' => $options['between'], 'after' => $options['after']);
1063:         $format = $this->_getFormat($options);
1064: 
1065:         unset($options['type'], $options['before'], $options['between'], $options['after'], $options['format']);
1066: 
1067:         $out['error'] = null;
1068:         if ($type !== 'hidden' && $error !== false) {
1069:             $errMsg = $this->error($fieldName, $error);
1070:             if ($errMsg) {
1071:                 $divOptions = $this->addClass($divOptions, 'error');
1072:                 if ($errorMessage) {
1073:                     $out['error'] = $errMsg;
1074:                 }
1075:             }
1076:         }
1077: 
1078:         if ($type === 'radio' && isset($out['between'])) {
1079:             $options['between'] = $out['between'];
1080:             $out['between'] = null;
1081:         }
1082:         $out['input'] = $this->_getInput(compact('type', 'fieldName', 'options', 'radioOptions', 'selected', 'dateFormat', 'timeFormat'));
1083: 
1084:         $output = '';
1085:         foreach ($format as $element) {
1086:             $output .= $out[$element];
1087:         }
1088: 
1089:         if (!empty($divOptions['tag'])) {
1090:             $tag = $divOptions['tag'];
1091:             unset($divOptions['tag']);
1092:             $output = $this->Html->tag($tag, $output, $divOptions);
1093:         }
1094:         return $output;
1095:     }
1096: 
1097: /**
1098:  * Generates an input element
1099:  *
1100:  * @param array $args The options for the input element
1101:  * @return string The generated input element
1102:  */
1103:     protected function _getInput($args) {
1104:         extract($args);
1105:         switch ($type) {
1106:             case 'hidden':
1107:                 return $this->hidden($fieldName, $options);
1108:             case 'checkbox':
1109:                 return $this->checkbox($fieldName, $options);
1110:             case 'radio':
1111:                 return $this->radio($fieldName, $radioOptions, $options);
1112:             case 'file':
1113:                 return $this->file($fieldName, $options);
1114:             case 'select':
1115:                 $options += array('options' => array(), 'value' => $selected);
1116:                 $list = $options['options'];
1117:                 unset($options['options']);
1118:                 return $this->select($fieldName, $list, $options);
1119:             case 'time':
1120:                 $options += array('value' => $selected);
1121:                 return $this->dateTime($fieldName, null, $timeFormat, $options);
1122:             case 'date':
1123:                 $options += array('value' => $selected);
1124:                 return $this->dateTime($fieldName, $dateFormat, null, $options);
1125:             case 'datetime':
1126:                 $options += array('value' => $selected);
1127:                 return $this->dateTime($fieldName, $dateFormat, $timeFormat, $options);
1128:             case 'textarea':
1129:                 return $this->textarea($fieldName, $options + array('cols' => '30', 'rows' => '6'));
1130:             case 'url':
1131:                 return $this->text($fieldName, array('type' => 'url') + $options);
1132:             default:
1133:                 return $this->{$type}($fieldName, $options);
1134:         }
1135:     }
1136: 
1137: /**
1138:  * Generates input options array
1139:  *
1140:  * @param array $options Options list.
1141:  * @return array Options
1142:  */
1143:     protected function _parseOptions($options) {
1144:         $options = array_merge(
1145:             array('before' => null, 'between' => null, 'after' => null, 'format' => null),
1146:             $this->_inputDefaults,
1147:             $options
1148:         );
1149: 
1150:         if (!isset($options['type'])) {
1151:             $options = $this->_magicOptions($options);
1152:         }
1153: 
1154:         if (in_array($options['type'], array('radio', 'select'))) {
1155:             $options = $this->_optionsOptions($options);
1156:         }
1157: 
1158:         $options = $this->_maxLength($options);
1159: 
1160:         if (isset($options['rows']) || isset($options['cols'])) {
1161:             $options['type'] = 'textarea';
1162:         }
1163: 
1164:         if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time' || $options['type'] === 'select') {
1165:             $options += array('empty' => false);
1166:         }
1167:         return $options;
1168:     }
1169: 
1170: /**
1171:  * Generates list of options for multiple select
1172:  *
1173:  * @param array $options Options list.
1174:  * @return array
1175:  */
1176:     protected function _optionsOptions($options) {
1177:         if (isset($options['options'])) {
1178:             return $options;
1179:         }
1180:         $varName = Inflector::variable(
1181:             Inflector::pluralize(preg_replace('/_id$/', '', $this->field()))
1182:         );
1183:         $varOptions = $this->_View->get($varName);
1184:         if (!is_array($varOptions)) {
1185:             return $options;
1186:         }
1187:         if ($options['type'] !== 'radio') {
1188:             $options['type'] = 'select';
1189:         }
1190:         $options['options'] = $varOptions;
1191:         return $options;
1192:     }
1193: 
1194: /**
1195:  * Magically set option type and corresponding options
1196:  *
1197:  * @param array $options Options list.
1198:  * @return array
1199:  */
1200:     protected function _magicOptions($options) {
1201:         $modelKey = $this->model();
1202:         $fieldKey = $this->field();
1203:         $options['type'] = 'text';
1204:         if (isset($options['options'])) {
1205:             $options['type'] = 'select';
1206:         } elseif (in_array($fieldKey, array('psword', 'passwd', 'password'))) {
1207:             $options['type'] = 'password';
1208:         } elseif (in_array($fieldKey, array('tel', 'telephone', 'phone'))) {
1209:             $options['type'] = 'tel';
1210:         } elseif ($fieldKey === 'email') {
1211:             $options['type'] = 'email';
1212:         } elseif (isset($options['checked'])) {
1213:             $options['type'] = 'checkbox';
1214:         } elseif ($fieldDef = $this->_introspectModel($modelKey, 'fields', $fieldKey)) {
1215:             $type = $fieldDef['type'];
1216:             $primaryKey = $this->fieldset[$modelKey]['key'];
1217:             $map = array(
1218:                 'string' => 'text', 'datetime' => 'datetime',
1219:                 'boolean' => 'checkbox', 'timestamp' => 'datetime',
1220:                 'text' => 'textarea', 'time' => 'time',
1221:                 'date' => 'date', 'float' => 'number',
1222:                 'integer' => 'number', 'decimal' => 'number',
1223:                 'binary' => 'file'
1224:             );
1225: 
1226:             if (isset($this->map[$type])) {
1227:                 $options['type'] = $this->map[$type];
1228:             } elseif (isset($map[$type])) {
1229:                 $options['type'] = $map[$type];
1230:             }
1231:             if ($fieldKey === $primaryKey) {
1232:                 $options['type'] = 'hidden';
1233:             }
1234:             if ($options['type'] === 'number' &&
1235:                 !isset($options['step'])
1236:             ) {
1237:                 if ($type === 'decimal' && isset($fieldDef['length'])) {
1238:                     $decimalPlaces = substr($fieldDef['length'], strpos($fieldDef['length'], ',') + 1);
1239:                     $options['step'] = sprintf('%.' . $decimalPlaces . 'F', pow(10, -1 * $decimalPlaces));
1240:                 } elseif ($type === 'float' || $type === 'decimal') {
1241:                     $options['step'] = 'any';
1242:                 }
1243:             }
1244:         }
1245: 
1246:         if (preg_match('/_id$/', $fieldKey) && $options['type'] !== 'hidden') {
1247:             $options['type'] = 'select';
1248:         }
1249: 
1250:         if ($modelKey === $fieldKey) {
1251:             $options['type'] = 'select';
1252:             if (!isset($options['multiple'])) {
1253:                 $options['multiple'] = 'multiple';
1254:             }
1255:         }
1256:         if (in_array($options['type'], array('text', 'number'))) {
1257:             $options = $this->_optionsOptions($options);
1258:         }
1259:         if ($options['type'] === 'select' && array_key_exists('step', $options)) {
1260:             unset($options['step']);
1261:         }
1262: 
1263:         return $options;
1264:     }
1265: 
1266: /**
1267:  * Generate format options
1268:  *
1269:  * @param array $options Options list.
1270:  * @return array
1271:  */
1272:     protected function _getFormat($options) {
1273:         if ($options['type'] === 'hidden') {
1274:             return array('input');
1275:         }
1276:         if (is_array($options['format']) && in_array('input', $options['format'])) {
1277:             return $options['format'];
1278:         }
1279:         if ($options['type'] === 'checkbox') {
1280:             return array('before', 'input', 'between', 'label', 'after', 'error');
1281:         }
1282:         return array('before', 'label', 'between', 'input', 'after', 'error');
1283:     }
1284: 
1285: /**
1286:  * Generate label for input
1287:  *
1288:  * @param string $fieldName Field name.
1289:  * @param array $options Options list.
1290:  * @return bool|string false or Generated label element
1291:  */
1292:     protected function _getLabel($fieldName, $options) {
1293:         if ($options['type'] === 'radio') {
1294:             return false;
1295:         }
1296: 
1297:         $label = null;
1298:         if (isset($options['label'])) {
1299:             $label = $options['label'];
1300:         }
1301: 
1302:         if ($label === false) {
1303:             return false;
1304:         }
1305:         return $this->_inputLabel($fieldName, $label, $options);
1306:     }
1307: 
1308: /**
1309:  * Calculates maxlength option
1310:  *
1311:  * @param array $options Options list.
1312:  * @return array
1313:  */
1314:     protected function _maxLength($options) {
1315:         $fieldDef = $this->_introspectModel($this->model(), 'fields', $this->field());
1316:         $autoLength = (
1317:             !array_key_exists('maxlength', $options) &&
1318:             isset($fieldDef['length']) &&
1319:             is_scalar($fieldDef['length']) &&
1320:             $fieldDef['length'] < 1000000 &&
1321:             $fieldDef['type'] !== 'decimal' &&
1322:             $options['type'] !== 'select'
1323:         );
1324:         if ($autoLength &&
1325:             in_array($options['type'], array('text', 'textarea', 'email', 'tel', 'url', 'search'))
1326:         ) {
1327:             $options['maxlength'] = (int)$fieldDef['length'];
1328:         }
1329:         return $options;
1330:     }
1331: 
1332: /**
1333:  * Generate div options for input
1334:  *
1335:  * @param array $options Options list.
1336:  * @return array
1337:  */
1338:     protected function _divOptions($options) {
1339:         if ($options['type'] === 'hidden') {
1340:             return array();
1341:         }
1342:         $div = $this->_extractOption('div', $options, true);
1343:         if (!$div) {
1344:             return array();
1345:         }
1346: 
1347:         $divOptions = array('class' => 'input');
1348:         $divOptions = $this->addClass($divOptions, $options['type']);
1349:         if (is_string($div)) {
1350:             $divOptions['class'] = $div;
1351:         } elseif (is_array($div)) {
1352:             $divOptions = array_merge($divOptions, $div);
1353:         }
1354:         if ($this->_extractOption('required', $options) !== false &&
1355:             $this->_introspectModel($this->model(), 'validates', $this->field())
1356:         ) {
1357:             $divOptions = $this->addClass($divOptions, 'required');
1358:         }
1359:         if (!isset($divOptions['tag'])) {
1360:             $divOptions['tag'] = 'div';
1361:         }
1362:         return $divOptions;
1363:     }
1364: 
1365: /**
1366:  * Extracts a single option from an options array.
1367:  *
1368:  * @param string $name The name of the option to pull out.
1369:  * @param array $options The array of options you want to extract.
1370:  * @param mixed $default The default option value
1371:  * @return mixed the contents of the option or default
1372:  */
1373:     protected function _extractOption($name, $options, $default = null) {
1374:         if (array_key_exists($name, $options)) {
1375:             return $options[$name];
1376:         }
1377:         return $default;
1378:     }
1379: 
1380: /**
1381:  * Generate a label for an input() call.
1382:  *
1383:  * $options can contain a hash of id overrides. These overrides will be
1384:  * used instead of the generated values if present.
1385:  *
1386:  * @param string $fieldName Field name.
1387:  * @param string|array $label Label text or array with text and options.
1388:  * @param array $options Options for the label element. 'NONE' option is
1389:  *   deprecated and will be removed in 3.0
1390:  * @return string Generated label element
1391:  */
1392:     protected function _inputLabel($fieldName, $label, $options) {
1393:         $labelAttributes = $this->domId(array(), 'for');
1394:         $idKey = null;
1395:         if ($options['type'] === 'date' || $options['type'] === 'datetime') {
1396:             $firstInput = 'M';
1397:             if (array_key_exists('dateFormat', $options) &&
1398:                 ($options['dateFormat'] === null || $options['dateFormat'] === 'NONE')
1399:             ) {
1400:                 $firstInput = 'H';
1401:             } elseif (!empty($options['dateFormat'])) {
1402:                 $firstInput = substr($options['dateFormat'], 0, 1);
1403:             }
1404:             switch ($firstInput) {
1405:                 case 'D':
1406:                     $idKey = 'day';
1407:                     $labelAttributes['for'] .= 'Day';
1408:                     break;
1409:                 case 'Y':
1410:                     $idKey = 'year';
1411:                     $labelAttributes['for'] .= 'Year';
1412:                     break;
1413:                 case 'M':
1414:                     $idKey = 'month';
1415:                     $labelAttributes['for'] .= 'Month';
1416:                     break;
1417:                 case 'H':
1418:                     $idKey = 'hour';
1419:                     $labelAttributes['for'] .= 'Hour';
1420:             }
1421:         }
1422:         if ($options['type'] === 'time') {
1423:             $labelAttributes['for'] .= 'Hour';
1424:             $idKey = 'hour';
1425:         }
1426:         if (isset($idKey) && isset($options['id']) && isset($options['id'][$idKey])) {
1427:             $labelAttributes['for'] = $options['id'][$idKey];
1428:         }
1429: 
1430:         if (is_array($label)) {
1431:             $labelText = null;
1432:             if (isset($label['text'])) {
1433:                 $labelText = $label['text'];
1434:                 unset($label['text']);
1435:             }
1436:             $labelAttributes = array_merge($labelAttributes, $label);
1437:         } else {
1438:             $labelText = $label;
1439:         }
1440: 
1441:         if (isset($options['id']) && is_string($options['id'])) {
1442:             $labelAttributes = array_merge($labelAttributes, array('for' => $options['id']));
1443:         }
1444:         return $this->label($fieldName, $labelText, $labelAttributes);
1445:     }
1446: 
1447: /**
1448:  * Creates a checkbox input widget.
1449:  *
1450:  * ### Options:
1451:  *
1452:  * - `value` - the value of the checkbox
1453:  * - `checked` - boolean indicate that this checkbox is checked.
1454:  * - `hiddenField` - boolean to indicate if you want the results of checkbox() to include
1455:  *    a hidden input with a value of ''.
1456:  * - `disabled` - create a disabled input.
1457:  * - `default` - Set the default value for the checkbox. This allows you to start checkboxes
1458:  *    as checked, without having to check the POST data. A matching POST data value, will overwrite
1459:  *    the default value.
1460:  *
1461:  * @param string $fieldName Name of a field, like this "Modelname.fieldname"
1462:  * @param array $options Array of HTML attributes.
1463:  * @return string An HTML text input element.
1464:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
1465:  */
1466:     public function checkbox($fieldName, $options = array()) {
1467:         $valueOptions = array();
1468:         if (isset($options['default'])) {
1469:             $valueOptions['default'] = $options['default'];
1470:             unset($options['default']);
1471:         }
1472: 
1473:         $options += array('value' => 1, 'required' => false);
1474:         $options = $this->_initInputField($fieldName, $options) + array('hiddenField' => true);
1475:         $value = current($this->value($valueOptions));
1476:         $output = '';
1477: 
1478:         if ((!isset($options['checked']) && !empty($value) && $value == $options['value']) ||
1479:             !empty($options['checked'])
1480:         ) {
1481:             $options['checked'] = 'checked';
1482:         }
1483:         if ($options['hiddenField']) {
1484:             $hiddenOptions = array(
1485:                 'id' => $options['id'] . '_',
1486:                 'name' => $options['name'],
1487:                 'value' => ($options['hiddenField'] !== true ? $options['hiddenField'] : '0'),
1488:                 'form' => isset($options['form']) ? $options['form'] : null,
1489:                 'secure' => false,
1490:             );
1491:             if (isset($options['disabled']) && $options['disabled']) {
1492:                 $hiddenOptions['disabled'] = 'disabled';
1493:             }
1494:             $output = $this->hidden($fieldName, $hiddenOptions);
1495:         }
1496:         unset($options['hiddenField']);
1497: 
1498:         return $output . $this->Html->useTag('checkbox', $options['name'], array_diff_key($options, array('name' => null)));
1499:     }
1500: 
1501: /**
1502:  * Creates a set of radio widgets. Will create a legend and fieldset
1503:  * by default. Use $options to control this
1504:  *
1505:  * You can also customize each radio input element using an array of arrays:
1506:  *
1507:  * ```
1508:  * $options = array(
1509:  *  array('name' => 'United states', 'value' => 'US', 'title' => 'My title'),
1510:  *  array('name' => 'Germany', 'value' => 'DE', 'class' => 'de-de', 'title' => 'Another title'),
1511:  * );
1512:  * ```
1513:  *
1514:  * ### Attributes:
1515:  *
1516:  * - `separator` - define the string in between the radio buttons
1517:  * - `between` - the string between legend and input set or array of strings to insert
1518:  *    strings between each input block
1519:  * - `legend` - control whether or not the widget set has a fieldset & legend
1520:  * - `fieldset` - sets the class of the fieldset. Fieldset is only generated if legend attribute is provided
1521:  * - `value` - indicate a value that is should be checked
1522:  * - `label` - boolean to indicate whether or not labels for widgets show be displayed
1523:  * - `hiddenField` - boolean to indicate if you want the results of radio() to include
1524:  *    a hidden input with a value of ''. This is useful for creating radio sets that non-continuous
1525:  * - `disabled` - Set to `true` or `disabled` to disable all the radio buttons.
1526:  * - `empty` - Set to `true` to create an input with the value '' as the first option. When `true`
1527:  *   the radio label will be 'empty'. Set this option to a string to control the label value.
1528:  *
1529:  * @param string $fieldName Name of a field, like this "Modelname.fieldname"
1530:  * @param array $options Radio button options array.
1531:  * @param array $attributes Array of HTML attributes, and special attributes above.
1532:  * @return string Completed radio widget set.
1533:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
1534:  */
1535:     public function radio($fieldName, $options = array(), $attributes = array()) {
1536:         $attributes['options'] = $options;
1537:         $attributes = $this->_initInputField($fieldName, $attributes);
1538:         unset($attributes['options']);
1539: 
1540:         $showEmpty = $this->_extractOption('empty', $attributes);
1541:         if ($showEmpty) {
1542:             $showEmpty = ($showEmpty === true) ? __d('cake', 'empty') : $showEmpty;
1543:             $options = array('' => $showEmpty) + $options;
1544:         }
1545:         unset($attributes['empty']);
1546: 
1547:         $legend = false;
1548:         if (isset($attributes['legend'])) {
1549:             $legend = $attributes['legend'];
1550:             unset($attributes['legend']);
1551:         } elseif (count($options) > 1) {
1552:             $legend = __(Inflector::humanize($this->field()));
1553:         }
1554: 
1555:         $fieldsetAttrs = '';
1556:         if (isset($attributes['fieldset'])) {
1557:             $fieldsetAttrs = array('class' => $attributes['fieldset']);
1558:             unset($attributes['fieldset']);
1559:         }
1560: 
1561:         $label = true;
1562:         if (isset($attributes['label'])) {
1563:             $label = $attributes['label'];
1564:             unset($attributes['label']);
1565:         }
1566: 
1567:         $separator = null;
1568:         if (isset($attributes['separator'])) {
1569:             $separator = $attributes['separator'];
1570:             unset($attributes['separator']);
1571:         }
1572: 
1573:         $between = null;
1574:         if (isset($attributes['between'])) {
1575:             $between = $attributes['between'];
1576:             unset($attributes['between']);
1577:         }
1578: 
1579:         $value = null;
1580:         if (isset($attributes['value'])) {
1581:             $value = $attributes['value'];
1582:         } else {
1583:             $value = $this->value($fieldName);
1584:         }
1585: 
1586:         $disabled = array();
1587:         if (isset($attributes['disabled'])) {
1588:             $disabled = $attributes['disabled'];
1589:         }
1590: 
1591:         $out = array();
1592: 
1593:         $hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
1594:         unset($attributes['hiddenField']);
1595: 
1596:         if (isset($value) && is_bool($value)) {
1597:             $value = $value ? 1 : 0;
1598:         }
1599: 
1600:         $this->_domIdSuffixes = array();
1601:         foreach ($options as $optValue => $optTitle) {
1602:             $optionsHere = array('value' => $optValue, 'disabled' => false);
1603:             if (is_array($optTitle)) {
1604:                 if (isset($optTitle['value'])) {
1605:                     $optionsHere['value'] = $optTitle['value'];
1606:                 }
1607: 
1608:                 $optionsHere += $optTitle;
1609:                 $optTitle = $optionsHere['name'];
1610:                 unset($optionsHere['name']);
1611:             }
1612: 
1613:             if (isset($value) && strval($optValue) === strval($value)) {
1614:                 $optionsHere['checked'] = 'checked';
1615:             }
1616:             $isNumeric = is_numeric($optValue);
1617:             if ($disabled && (!is_array($disabled) || in_array((string)$optValue, $disabled, !$isNumeric))) {
1618:                 $optionsHere['disabled'] = true;
1619:             }
1620:             $tagName = $attributes['id'] . $this->domIdSuffix($optValue);
1621: 
1622:             if ($label) {
1623:                 $labelOpts = is_array($label) ? $label : array();
1624:                 $labelOpts += array('for' => $tagName);
1625:                 $optTitle = $this->label($tagName, $optTitle, $labelOpts);
1626:             }
1627: 
1628:             if (is_array($between)) {
1629:                 $optTitle .= array_shift($between);
1630:             }
1631:             $allOptions = $optionsHere + $attributes;
1632:             $out[] = $this->Html->useTag('radio', $attributes['name'], $tagName,
1633:                 array_diff_key($allOptions, array('name' => null, 'type' => null, 'id' => null)),
1634:                 $optTitle
1635:             );
1636:         }
1637:         $hidden = null;
1638: 
1639:         if ($hiddenField) {
1640:             if (!isset($value) || $value === '') {
1641:                 $hidden = $this->hidden($fieldName, array(
1642:                     'form' => isset($attributes['form']) ? $attributes['form'] : null,
1643:                     'id' => $attributes['id'] . '_',
1644:                     'value' => '',
1645:                     'name' => $attributes['name']
1646:                 ));
1647:             }
1648:         }
1649:         $out = $hidden . implode($separator, $out);
1650: 
1651:         if (is_array($between)) {
1652:             $between = '';
1653:         }
1654: 
1655:         if ($legend) {
1656:             $out = $this->Html->useTag('legend', $legend) . $between . $out;
1657:             $out = $this->Html->useTag('fieldset', $fieldsetAttrs, $out);
1658:         }
1659:         return $out;
1660:     }
1661: 
1662: /**
1663:  * Missing method handler - implements various simple input types. Is used to create inputs
1664:  * of various types. e.g. `$this->Form->text();` will create `<input type="text" />` while
1665:  * `$this->Form->range();` will create `<input type="range" />`
1666:  *
1667:  * ### Usage
1668:  *
1669:  * `$this->Form->search('User.query', array('value' => 'test'));`
1670:  *
1671:  * Will make an input like:
1672:  *
1673:  * `<input type="search" id="UserQuery" name="data[User][query]" value="test" />`
1674:  *
1675:  * The first argument to an input type should always be the fieldname, in `Model.field` format.
1676:  * The second argument should always be an array of attributes for the input.
1677:  *
1678:  * @param string $method Method name / input type to make.
1679:  * @param array $params Parameters for the method call
1680:  * @return string Formatted input method.
1681:  * @throws CakeException When there are no params for the method call.
1682:  */
1683:     public function __call($method, $params) {
1684:         $options = array();
1685:         if (empty($params)) {
1686:             throw new CakeException(__d('cake_dev', 'Missing field name for FormHelper::%s', $method));
1687:         }
1688:         if (isset($params[1])) {
1689:             $options = $params[1];
1690:         }
1691:         if (!isset($options['type'])) {
1692:             $options['type'] = $method;
1693:         }
1694:         $options = $this->_initInputField($params[0], $options);
1695:         return $this->Html->useTag('input', $options['name'], array_diff_key($options, array('name' => null)));
1696:     }
1697: 
1698: /**
1699:  * Creates a textarea widget.
1700:  *
1701:  * ### Options:
1702:  *
1703:  * - `escape` - Whether or not the contents of the textarea should be escaped. Defaults to true.
1704:  *
1705:  * @param string $fieldName Name of a field, in the form "Modelname.fieldname"
1706:  * @param array $options Array of HTML attributes, and special options above.
1707:  * @return string A generated HTML text input element
1708:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::textarea
1709:  */
1710:     public function textarea($fieldName, $options = array()) {
1711:         $options = $this->_initInputField($fieldName, $options);
1712:         $value = null;
1713: 
1714:         if (array_key_exists('value', $options)) {
1715:             $value = $options['value'];
1716:             if (!array_key_exists('escape', $options) || $options['escape'] !== false) {
1717:                 $value = h($value);
1718:             }
1719:             unset($options['value']);
1720:         }
1721:         return $this->Html->useTag('textarea', $options['name'], array_diff_key($options, array('type' => null, 'name' => null)), $value);
1722:     }
1723: 
1724: /**
1725:  * Creates a hidden input field.
1726:  *
1727:  * @param string $fieldName Name of a field, in the form of "Modelname.fieldname"
1728:  * @param array $options Array of HTML attributes.
1729:  * @return string A generated hidden input
1730:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::hidden
1731:  */
1732:     public function hidden($fieldName, $options = array()) {
1733:         $options += array('required' => false, 'secure' => true);
1734: 
1735:         $secure = $options['secure'];
1736:         unset($options['secure']);
1737: 
1738:         $options = $this->_initInputField($fieldName, array_merge(
1739:             $options, array('secure' => static::SECURE_SKIP)
1740:         ));
1741: 
1742:         if ($secure === true) {
1743:             $this->_secure(true, null, '' . $options['value']);
1744:         }
1745: 
1746:         return $this->Html->useTag('hidden', $options['name'], array_diff_key($options, array('name' => null)));
1747:     }
1748: 
1749: /**
1750:  * Creates file input widget.
1751:  *
1752:  * @param string $fieldName Name of a field, in the form "Modelname.fieldname"
1753:  * @param array $options Array of HTML attributes.
1754:  * @return string A generated file input.
1755:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::file
1756:  */
1757:     public function file($fieldName, $options = array()) {
1758:         $options += array('secure' => true);
1759:         $secure = $options['secure'];
1760:         $options['secure'] = static::SECURE_SKIP;
1761: 
1762:         $options = $this->_initInputField($fieldName, $options);
1763:         $field = $this->entity();
1764: 
1765:         foreach (array('name', 'type', 'tmp_name', 'error', 'size') as $suffix) {
1766:             $this->_secure($secure, array_merge($field, array($suffix)));
1767:         }
1768: 
1769:         $exclude = array('name' => null, 'value' => null);
1770:         return $this->Html->useTag('file', $options['name'], array_diff_key($options, $exclude));
1771:     }
1772: 
1773: /**
1774:  * Creates a `<button>` tag. The type attribute defaults to `type="submit"`
1775:  * You can change it to a different value by using `$options['type']`.
1776:  *
1777:  * ### Options:
1778:  *
1779:  * - `escape` - HTML entity encode the $title of the button. Defaults to false.
1780:  *
1781:  * @param string $title The button's caption. Not automatically HTML encoded
1782:  * @param array $options Array of options and HTML attributes.
1783:  * @return string A HTML button tag.
1784:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::button
1785:  */
1786:     public function button($title, $options = array()) {
1787:         $options += array('type' => 'submit', 'escape' => false, 'secure' => false);
1788:         if ($options['escape']) {
1789:             $title = h($title);
1790:         }
1791:         if (isset($options['name'])) {
1792:             $name = str_replace(array('[', ']'), array('.', ''), $options['name']);
1793:             $this->_secure($options['secure'], $name);
1794:         }
1795:         return $this->Html->useTag('button', $options, $title);
1796:     }
1797: 
1798: /**
1799:  * Create a `<button>` tag with a surrounding `<form>` that submits via POST.
1800:  *
1801:  * This method creates a `<form>` element. So do not use this method in an already opened form.
1802:  * Instead use FormHelper::submit() or FormHelper::button() to create buttons inside opened forms.
1803:  *
1804:  * ### Options:
1805:  *
1806:  * - `data` - Array with key/value to pass in input hidden
1807:  * - Other options is the same of button method.
1808:  *
1809:  * @param string $title The button's caption. Not automatically HTML encoded
1810:  * @param string|array $url URL as string or array
1811:  * @param array $options Array of options and HTML attributes.
1812:  * @return string A HTML button tag.
1813:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::postButton
1814:  */
1815:     public function postButton($title, $url, $options = array()) {
1816:         $out = $this->create(false, array('id' => false, 'url' => $url));
1817:         if (isset($options['data']) && is_array($options['data'])) {
1818:             foreach (Hash::flatten($options['data']) as $key => $value) {
1819:                 $out .= $this->hidden($key, array('value' => $value, 'id' => false));
1820:             }
1821:             unset($options['data']);
1822:         }
1823:         $out .= $this->button($title, $options);
1824:         $out .= $this->end();
1825:         return $out;
1826:     }
1827: 
1828: /**
1829:  * Creates an HTML link, but access the URL using the method you specify (defaults to POST).
1830:  * Requires javascript to be enabled in browser.
1831:  *
1832:  * This method creates a `<form>` element. If you want to use this method inside of an
1833:  * existing form, you must use the `inline` or `block` options so that the new form is
1834:  * being set to a view block that can be rendered outside of the main form.
1835:  *
1836:  * If all you are looking for is a button to submit your form, then you should use
1837:  * `FormHelper::submit()` instead.
1838:  *
1839:  * ### Options:
1840:  *
1841:  * - `data` - Array with key/value to pass in input hidden
1842:  * - `method` - Request method to use. Set to 'delete' to simulate HTTP/1.1 DELETE request. Defaults to 'post'.
1843:  * - `confirm` - Can be used instead of $confirmMessage.
1844:  * - `inline` - Whether or not the associated form tag should be output inline.
1845:  *   Set to false to have the form tag appended to the 'postLink' view block.
1846:  *   Defaults to true.
1847:  * - `block` - Choose a custom block to append the form tag to. Using this option
1848:  *   will override the inline option.
1849:  * - Other options are the same of HtmlHelper::link() method.
1850:  * - The option `onclick` will be replaced.
1851:  *
1852:  * @param string $title The content to be wrapped by <a> tags.
1853:  * @param string|array $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
1854:  * @param array $options Array of HTML attributes.
1855:  * @param bool|string $confirmMessage JavaScript confirmation message. This
1856:  *   argument is deprecated as of 2.6. Use `confirm` key in $options instead.
1857:  * @return string An `<a />` element.
1858:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::postLink
1859:  */
1860:     public function postLink($title, $url = null, $options = array(), $confirmMessage = false) {
1861:         $options = (array)$options + array('inline' => true, 'block' => null);
1862:         if (!$options['inline'] && empty($options['block'])) {
1863:             $options['block'] = __FUNCTION__;
1864:         }
1865:         unset($options['inline']);
1866: 
1867:         $requestMethod = 'POST';
1868:         if (!empty($options['method'])) {
1869:             $requestMethod = strtoupper($options['method']);
1870:             unset($options['method']);
1871:         }
1872:         if (!empty($options['confirm'])) {
1873:             $confirmMessage = $options['confirm'];
1874:             unset($options['confirm']);
1875:         }
1876: 
1877:         $formName = str_replace('.', '', uniqid('post_', true));
1878:         $formUrl = $this->url($url);
1879:         $formOptions = array(
1880:             'name' => $formName,
1881:             'id' => $formName,
1882:             'style' => 'display:none;',
1883:             'method' => 'post',
1884:         );
1885:         if (isset($options['target'])) {
1886:             $formOptions['target'] = $options['target'];
1887:             unset($options['target']);
1888:         }
1889: 
1890:         $previousLastAction = $this->_lastAction;
1891:         $this->_lastAction($url);
1892: 
1893:         $out = $this->Html->useTag('form', $formUrl, $formOptions);
1894:         $out .= $this->Html->useTag('hidden', '_method', array(
1895:             'value' => $requestMethod
1896:         ));
1897:         $out .= $this->_csrfField();
1898: 
1899:         $fields = array();
1900:         if (isset($options['data']) && is_array($options['data'])) {
1901:             foreach (Hash::flatten($options['data']) as $key => $value) {
1902:                 $fields[$key] = $value;
1903:                 $out .= $this->hidden($key, array('value' => $value, 'id' => false, 'secure' => static::SECURE_SKIP));
1904:             }
1905:             unset($options['data']);
1906:         }
1907:         $out .= $this->secure($fields);
1908:         $out .= $this->Html->useTag('formend');
1909: 
1910:         if ($options['block']) {
1911:             $this->_View->append($options['block'], $out);
1912:             $out = '';
1913:             // Reset security-relevant fields for outer form
1914:             $this->_lastAction = $previousLastAction;
1915:         }
1916:         unset($options['block']);
1917: 
1918:         $url = '#';
1919:         $onClick = 'document.' . $formName . '.submit();';
1920:         if ($confirmMessage) {
1921:             $options['onclick'] = $this->_confirm($confirmMessage, $onClick, '', $options);
1922:         } else {
1923:             $options['onclick'] = $onClick . ' ';
1924:         }
1925:         $options['onclick'] .= 'event.returnValue = false; return false;';
1926: 
1927:         $out .= $this->Html->link($title, $url, $options);
1928:         return $out;
1929:     }
1930: 
1931: /**
1932:  * Creates a submit button element. This method will generate `<input />` elements that
1933:  * can be used to submit, and reset forms by using $options. image submits can be created by supplying an
1934:  * image path for $caption.
1935:  *
1936:  * ### Options
1937:  *
1938:  * - `div` - Include a wrapping div?  Defaults to true. Accepts sub options similar to
1939:  *   FormHelper::input().
1940:  * - `before` - Content to include before the input.
1941:  * - `after` - Content to include after the input.
1942:  * - `type` - Set to 'reset' for reset inputs. Defaults to 'submit'
1943:  * - `confirm` - JavaScript confirmation message.
1944:  * - Other attributes will be assigned to the input element.
1945:  *
1946:  * ### Options
1947:  *
1948:  * - `div` - Include a wrapping div?  Defaults to true. Accepts sub options similar to
1949:  *   FormHelper::input().
1950:  * - Other attributes will be assigned to the input element.
1951:  *
1952:  * @param string $caption The label appearing on the button OR if string contains :// or the
1953:  *  extension .jpg, .jpe, .jpeg, .gif, .png use an image if the extension
1954:  *  exists, AND the first character is /, image is relative to webroot,
1955:  *  OR if the first character is not /, image is relative to webroot/img.
1956:  * @param array $options Array of options. See above.
1957:  * @return string A HTML submit button
1958:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::submit
1959:  */
1960:     public function submit($caption = null, $options = array()) {
1961:         $confirmMessage = false;
1962:         if (!is_string($caption) && empty($caption)) {
1963:             $caption = __d('cake', 'Submit');
1964:         }
1965:         $out = null;
1966:         $div = true;
1967: 
1968:         if (!empty($options['confirm'])) {
1969:             $confirmMessage = $options['confirm'];
1970:             unset($options['confirm']);
1971:         }
1972:         if (isset($options['div'])) {
1973:             $div = $options['div'];
1974:             unset($options['div']);
1975:         }
1976:         $options += array('type' => 'submit', 'before' => null, 'after' => null, 'secure' => false);
1977:         $divOptions = array('tag' => 'div');
1978: 
1979:         if ($div === true) {
1980:             $divOptions['class'] = 'submit';
1981:         } elseif ($div === false) {
1982:             unset($divOptions);
1983:         } elseif (is_string($div)) {
1984:             $divOptions['class'] = $div;
1985:         } elseif (is_array($div)) {
1986:             $divOptions = array_merge(array('class' => 'submit', 'tag' => 'div'), $div);
1987:         }
1988: 
1989:         if (isset($options['name'])) {
1990:             $name = str_replace(array('[', ']'), array('.', ''), $options['name']);
1991:             $this->_secure($options['secure'], $name);
1992:         }
1993:         unset($options['secure']);
1994: 
1995:         $before = $options['before'];
1996:         $after = $options['after'];
1997:         unset($options['before'], $options['after']);
1998: 
1999:         $isUrl = strpos($caption, '://') !== false;
2000:         $isImage = preg_match('/\.(jpg|jpe|jpeg|gif|png|ico)$/', $caption);
2001: 
2002:         if ($isUrl || $isImage) {
2003:             $unlockFields = array('x', 'y');
2004:             if (isset($options['name'])) {
2005:                 $unlockFields = array(
2006:                     $options['name'] . '_x', $options['name'] . '_y'
2007:                 );
2008:             }
2009:             foreach ($unlockFields as $ignore) {
2010:                 $this->unlockField($ignore);
2011:             }
2012:         }
2013: 
2014:         if ($confirmMessage) {
2015:             $okCode = 'return true;';
2016:             $cancelCode = 'event.returnValue = false; return false;';
2017:             $options['onclick'] = $this->_confirm($confirmMessage, $okCode, $cancelCode, $options);
2018:         }
2019: 
2020:         if ($isUrl) {
2021:             unset($options['type']);
2022:             $tag = $this->Html->useTag('submitimage', $caption, $options);
2023:         } elseif ($isImage) {
2024:             unset($options['type']);
2025:             if ($caption{0} !== '/') {
2026:                 $url = $this->webroot(Configure::read('App.imageBaseUrl') . $caption);
2027:             } else {
2028:                 $url = $this->webroot(trim($caption, '/'));
2029:             }
2030:             $url = $this->assetTimestamp($url);
2031:             $tag = $this->Html->useTag('submitimage', $url, $options);
2032:         } else {
2033:             $options['value'] = $caption;
2034:             $tag = $this->Html->useTag('submit', $options);
2035:         }
2036:         $out = $before . $tag . $after;
2037: 
2038:         if (isset($divOptions)) {
2039:             $tag = $divOptions['tag'];
2040:             unset($divOptions['tag']);
2041:             $out = $this->Html->tag($tag, $out, $divOptions);
2042:         }
2043:         return $out;
2044:     }
2045: 
2046: /**
2047:  * Returns a formatted SELECT element.
2048:  *
2049:  * ### Attributes:
2050:  *
2051:  * - `showParents` - If included in the array and set to true, an additional option element
2052:  *   will be added for the parent of each option group. You can set an option with the same name
2053:  *   and it's key will be used for the value of the option.
2054:  * - `multiple` - show a multiple select box. If set to 'checkbox' multiple checkboxes will be
2055:  *   created instead.
2056:  * - `empty` - If true, the empty select option is shown. If a string,
2057:  *   that string is displayed as the empty element.
2058:  * - `escape` - If true contents of options will be HTML entity encoded. Defaults to true.
2059:  * - `value` The selected value of the input.
2060:  * - `class` - When using multiple = checkbox the class name to apply to the divs. Defaults to 'checkbox'.
2061:  * - `disabled` - Control the disabled attribute. When creating a select box, set to true to disable the
2062:  *   select box. When creating checkboxes, `true` will disable all checkboxes. You can also set disabled
2063:  *   to a list of values you want to disable when creating checkboxes.
2064:  *
2065:  * ### Using options
2066:  *
2067:  * A simple array will create normal options:
2068:  *
2069:  * ```
2070:  * $options = array(1 => 'one', 2 => 'two);
2071:  * $this->Form->select('Model.field', $options));
2072:  * ```
2073:  *
2074:  * While a nested options array will create optgroups with options inside them.
2075:  * ```
2076:  * $options = array(
2077:  *  1 => 'bill',
2078:  *  'fred' => array(
2079:  *     2 => 'fred',
2080:  *     3 => 'fred jr.'
2081:  *  )
2082:  * );
2083:  * $this->Form->select('Model.field', $options);
2084:  * ```
2085:  *
2086:  * In the above `2 => 'fred'` will not generate an option element. You should enable the `showParents`
2087:  * attribute to show the fred option.
2088:  *
2089:  * If you have multiple options that need to have the same value attribute, you can
2090:  * use an array of arrays to express this:
2091:  *
2092:  * ```
2093:  * $options = array(
2094:  *  array('name' => 'United states', 'value' => 'USA'),
2095:  *  array('name' => 'USA', 'value' => 'USA'),
2096:  * );
2097:  * ```
2098:  *
2099:  * @param string $fieldName Name attribute of the SELECT
2100:  * @param array $options Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the
2101:  *  SELECT element
2102:  * @param array $attributes The HTML attributes of the select element.
2103:  * @return string Formatted SELECT element
2104:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
2105:  */
2106:     public function select($fieldName, $options = array(), $attributes = array()) {
2107:         $select = array();
2108:         $style = null;
2109:         $tag = null;
2110:         $attributes += array(
2111:             'class' => null,
2112:             'escape' => true,
2113:             'secure' => true,
2114:             'empty' => '',
2115:             'showParents' => false,
2116:             'hiddenField' => true,
2117:             'disabled' => false
2118:         );
2119: 
2120:         $escapeOptions = $this->_extractOption('escape', $attributes);
2121:         $secure = $this->_extractOption('secure', $attributes);
2122:         $showEmpty = $this->_extractOption('empty', $attributes);
2123:         $showParents = $this->_extractOption('showParents', $attributes);
2124:         $hiddenField = $this->_extractOption('hiddenField', $attributes);
2125:         unset($attributes['escape'], $attributes['secure'], $attributes['empty'], $attributes['showParents'], $attributes['hiddenField']);
2126:         $id = $this->_extractOption('id', $attributes);
2127: 
2128:         $attributes = $this->_initInputField($fieldName, array_merge(
2129:             (array)$attributes, array('secure' => static::SECURE_SKIP)
2130:         ));
2131: 
2132:         if (is_string($options) && isset($this->_options[$options])) {
2133:             $options = $this->_generateOptions($options);
2134:         } elseif (!is_array($options)) {
2135:             $options = array();
2136:         }
2137:         if (isset($attributes['type'])) {
2138:             unset($attributes['type']);
2139:         }
2140: 
2141:         if (!empty($attributes['multiple'])) {
2142:             $style = ($attributes['multiple'] === 'checkbox') ? 'checkbox' : null;
2143:             $template = ($style) ? 'checkboxmultiplestart' : 'selectmultiplestart';
2144:             $tag = $template;
2145:             if ($hiddenField) {
2146:                 $hiddenAttributes = array(
2147:                     'value' => '',
2148:                     'id' => $attributes['id'] . ($style ? '' : '_'),
2149:                     'secure' => false,
2150:                     'form' => isset($attributes['form']) ? $attributes['form'] : null,
2151:                     'name' => $attributes['name'],
2152:                     'disabled' => $attributes['disabled'] === true || $attributes['disabled'] === 'disabled'
2153:                 );
2154:                 $select[] = $this->hidden(null, $hiddenAttributes);
2155:             }
2156:         } else {
2157:             $tag = 'selectstart';
2158:         }
2159: 
2160:         if ($tag === 'checkboxmultiplestart') {
2161:             unset($attributes['required']);
2162:         }
2163: 
2164:         if (!empty($tag) || isset($template)) {
2165:             $hasOptions = (count($options) > 0 || $showEmpty);
2166:             // Secure the field if there are options, or its a multi select.
2167:             // Single selects with no options don't submit, but multiselects do.
2168:             if ((!isset($secure) || $secure) &&
2169:                 empty($attributes['disabled']) &&
2170:                 (!empty($attributes['multiple']) || $hasOptions)
2171:             ) {
2172:                 $this->_secure(true, $this->_secureFieldName($attributes));
2173:             }
2174:             $filter = array('name' => null, 'value' => null);
2175:             if (is_array($attributes['disabled'])) {
2176:                 $filter['disabled'] = null;
2177:             }
2178:             $select[] = $this->Html->useTag($tag, $attributes['name'], array_diff_key($attributes, $filter));
2179:         }
2180:         $emptyMulti = (
2181:             $showEmpty !== null && $showEmpty !== false && !(
2182:                 empty($showEmpty) && (isset($attributes) &&
2183:                 array_key_exists('multiple', $attributes))
2184:             )
2185:         );
2186: 
2187:         if ($emptyMulti) {
2188:             $showEmpty = ($showEmpty === true) ? '' : $showEmpty;
2189:             $options = array('' => $showEmpty) + $options;
2190:         }
2191: 
2192:         if (!$id) {
2193:             $attributes['id'] = Inflector::camelize($attributes['id']);
2194:         }
2195: 
2196:         $select = array_merge($select, $this->_selectOptions(
2197:             array_reverse($options, true),
2198:             array(),
2199:             $showParents,
2200:             array(
2201:                 'escape' => $escapeOptions,
2202:                 'style' => $style,
2203:                 'name' => $attributes['name'],
2204:                 'value' => $attributes['value'],
2205:                 'class' => $attributes['class'],
2206:                 'id' => $attributes['id'],
2207:                 'disabled' => $attributes['disabled'],
2208:             )
2209:         ));
2210: 
2211:         $template = ($style === 'checkbox') ? 'checkboxmultipleend' : 'selectend';
2212:         $select[] = $this->Html->useTag($template);
2213:         return implode("\n", $select);
2214:     }
2215: 
2216: /**
2217:  * Generates a valid DOM ID suffix from a string.
2218:  * Also avoids collisions when multiple values are coverted to the same suffix by
2219:  * appending a numeric value.
2220:  *
2221:  * For pre-HTML5 IDs only characters like a-z 0-9 - _ are valid. HTML5 doesn't have that
2222:  * limitation, but to avoid layout issues it still filters out some sensitive chars.
2223:  *
2224:  * @param string $value The value that should be transferred into a DOM ID suffix.
2225:  * @param string $type Doctype to use. Defaults to html4.
2226:  * @return string DOM ID
2227:  */
2228:     public function domIdSuffix($value, $type = 'html4') {
2229:         if ($type === 'html5') {
2230:             $value = str_replace(array('@', '<', '>', ' ', '"', '\''), '_', $value);
2231:         } else {
2232:             $value = Inflector::camelize(Inflector::slug($value));
2233:         }
2234:         $value = Inflector::camelize($value);
2235:         $count = 1;
2236:         $suffix = $value;
2237:         while (in_array($suffix, $this->_domIdSuffixes)) {
2238:             $suffix = $value . $count++;
2239:         }
2240:         $this->_domIdSuffixes[] = $suffix;
2241:         return $suffix;
2242:     }
2243: 
2244: /**
2245:  * Returns a SELECT element for days.
2246:  *
2247:  * ### Attributes:
2248:  *
2249:  * - `empty` - If true, the empty select option is shown. If a string,
2250:  *   that string is displayed as the empty element.
2251:  * - `value` The selected value of the input.
2252:  *
2253:  * @param string $fieldName Prefix name for the SELECT element
2254:  * @param array $attributes HTML attributes for the select element
2255:  * @return string A generated day select box.
2256:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::day
2257:  */
2258:     public function day($fieldName = null, $attributes = array()) {
2259:         $attributes += array('empty' => true, 'value' => null);
2260:         $attributes = $this->_dateTimeSelected('day', $fieldName, $attributes);
2261: 
2262:         if (strlen($attributes['value']) > 2) {
2263:             $date = date_create($attributes['value']);
2264:             $attributes['value'] = null;
2265:             if ($date) {
2266:                 $attributes['value'] = $date->format('d');
2267:             }
2268:         } elseif ($attributes['value'] === false) {
2269:             $attributes['value'] = null;
2270:         }
2271:         return $this->select($fieldName . ".day", $this->_generateOptions('day'), $attributes);
2272:     }
2273: 
2274: /**
2275:  * Returns a SELECT element for years
2276:  *
2277:  * ### Attributes:
2278:  *
2279:  * - `empty` - If true, the empty select option is shown. If a string,
2280:  *   that string is displayed as the empty element.
2281:  * - `orderYear` - Ordering of year values in select options.
2282:  *   Possible values 'asc', 'desc'. Default 'desc'
2283:  * - `value` The selected value of the input.
2284:  *
2285:  * @param string $fieldName Prefix name for the SELECT element
2286:  * @param int $minYear First year in sequence
2287:  * @param int $maxYear Last year in sequence
2288:  * @param array $attributes Attribute array for the select elements.
2289:  * @return string Completed year select input
2290:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::year
2291:  */
2292:     public function year($fieldName, $minYear = null, $maxYear = null, $attributes = array()) {
2293:         if (is_array($minYear)) {
2294:             $attributes = $minYear;
2295:             $minYear = null;
2296:         }
2297: 
2298:         $attributes += array('empty' => true, 'value' => null);
2299:         if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
2300:             if (is_array($value)) {
2301:                 $year = null;
2302:                 extract($value);
2303:                 $attributes['value'] = $year;
2304:             } else {
2305:                 if (empty($value)) {
2306:                     if (!$attributes['empty'] && !$maxYear) {
2307:                         $attributes['value'] = 'now';
2308: 
2309:                     } elseif (!$attributes['empty'] && $maxYear && !$attributes['value']) {
2310:                         $attributes['value'] = $maxYear;
2311:                     }
2312:                 } else {
2313:                     $attributes['value'] = $value;
2314:                 }
2315:             }
2316:         }
2317: 
2318:         if (strlen($attributes['value']) > 4 || $attributes['value'] === 'now') {
2319:             $date = date_create($attributes['value']);
2320:             $attributes['value'] = null;
2321:             if ($date) {
2322:                 $attributes['value'] = $date->format('Y');
2323:             }
2324:         } elseif ($attributes['value'] === false) {
2325:             $attributes['value'] = null;
2326:         }
2327:         $yearOptions = array('value' => $attributes['value'], 'min' => $minYear, 'max' => $maxYear, 'order' => 'desc');
2328:         if (isset($attributes['orderYear'])) {
2329:             $yearOptions['order'] = $attributes['orderYear'];
2330:             unset($attributes['orderYear']);
2331:         }
2332:         return $this->select(
2333:             $fieldName . '.year', $this->_generateOptions('year', $yearOptions),
2334:             $attributes
2335:         );
2336:     }
2337: 
2338: /**
2339:  * Returns a SELECT element for months.
2340:  *
2341:  * ### Attributes:
2342:  *
2343:  * - `monthNames` - If false, 2 digit numbers will be used instead of text.
2344:  *   If an array, the given array will be used.
2345:  * - `empty` - If true, the empty select option is shown. If a string,
2346:  *   that string is displayed as the empty element.
2347:  * - `value` The selected value of the input.
2348:  *
2349:  * @param string $fieldName Prefix name for the SELECT element
2350:  * @param array $attributes Attributes for the select element
2351:  * @return string A generated month select dropdown.
2352:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::month
2353:  */
2354:     public function month($fieldName, $attributes = array()) {
2355:         $attributes += array('empty' => true, 'value' => null);
2356:         $attributes = $this->_dateTimeSelected('month', $fieldName, $attributes);
2357: 
2358:         if (strlen($attributes['value']) > 2) {
2359:             $date = date_create($attributes['value']);
2360:             $attributes['value'] = null;
2361:             if ($date) {
2362:                 $attributes['value'] = $date->format('m');
2363:             }
2364:         } elseif ($attributes['value'] === false) {
2365:             $attributes['value'] = null;
2366:         }
2367:         $defaults = array('monthNames' => true);
2368:         $attributes = array_merge($defaults, (array)$attributes);
2369:         $monthNames = $attributes['monthNames'];
2370:         unset($attributes['monthNames']);
2371: 
2372:         return $this->select(
2373:             $fieldName . ".month",
2374:             $this->_generateOptions('month', array('monthNames' => $monthNames)),
2375:             $attributes
2376:         );
2377:     }
2378: 
2379: /**
2380:  * Returns a SELECT element for hours.
2381:  *
2382:  * ### Attributes:
2383:  *
2384:  * - `empty` - If true, the empty select option is shown. If a string,
2385:  *   that string is displayed as the empty element.
2386:  * - `value` The selected value of the input.
2387:  *
2388:  * @param string $fieldName Prefix name for the SELECT element
2389:  * @param bool $format24Hours True for 24 hours format
2390:  * @param array $attributes List of HTML attributes
2391:  * @return string Completed hour select input
2392:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::hour
2393:  */
2394:     public function hour($fieldName, $format24Hours = false, $attributes = array()) {
2395:         if (is_array($format24Hours)) {
2396:             $attributes = $format24Hours;
2397:             $format24Hours = false;
2398:         }
2399: 
2400:         $attributes += array('empty' => true, 'value' => null);
2401:         $attributes = $this->_dateTimeSelected('hour', $fieldName, $attributes);
2402: 
2403:         if (strlen($attributes['value']) > 2) {
2404:             try {
2405:                 $date = new DateTime($attributes['value']);
2406:                 if ($format24Hours) {
2407:                     $attributes['value'] = $date->format('H');
2408:                 } else {
2409:                     $attributes['value'] = $date->format('g');
2410:                 }
2411:             } catch (Exception $e) {
2412:                 $attributes['value'] = null;
2413:             }
2414:         } elseif ($attributes['value'] === false) {
2415:             $attributes['value'] = null;
2416:         }
2417: 
2418:         if ($attributes['value'] > 12 && !$format24Hours) {
2419:             $attributes['value'] -= 12;
2420:         }
2421:         if (($attributes['value'] === 0 || $attributes['value'] === '00') && !$format24Hours) {
2422:             $attributes['value'] = 12;
2423:         }
2424: 
2425:         return $this->select(
2426:             $fieldName . ".hour",
2427:             $this->_generateOptions($format24Hours ? 'hour24' : 'hour'),
2428:             $attributes
2429:         );
2430:     }
2431: 
2432: /**
2433:  * Returns a SELECT element for minutes.
2434:  *
2435:  * ### Attributes:
2436:  *
2437:  * - `empty` - If true, the empty select option is shown. If a string,
2438:  *   that string is displayed as the empty element.
2439:  * - `value` The selected value of the input.
2440:  *
2441:  * @param string $fieldName Prefix name for the SELECT element
2442:  * @param array $attributes Array of Attributes
2443:  * @return string Completed minute select input.
2444:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::minute
2445:  */
2446:     public function minute($fieldName, $attributes = array()) {
2447:         $attributes += array('empty' => true, 'value' => null);
2448:         $attributes = $this->_dateTimeSelected('min', $fieldName, $attributes);
2449: 
2450:         if (strlen($attributes['value']) > 2) {
2451:             $date = date_create($attributes['value']);
2452:             $attributes['value'] = null;
2453:             if ($date) {
2454:                 $attributes['value'] = $date->format('i');
2455:             }
2456:         } elseif ($attributes['value'] === false) {
2457:             $attributes['value'] = null;
2458:         }
2459:         $minuteOptions = array();
2460: 
2461:         if (isset($attributes['interval'])) {
2462:             $minuteOptions['interval'] = $attributes['interval'];
2463:             unset($attributes['interval']);
2464:         }
2465:         return $this->select(
2466:             $fieldName . ".min", $this->_generateOptions('minute', $minuteOptions),
2467:             $attributes
2468:         );
2469:     }
2470: 
2471: /**
2472:  * Selects values for dateTime selects.
2473:  *
2474:  * @param string $select Name of element field. ex. 'day'
2475:  * @param string $fieldName Name of fieldName being generated ex. Model.created
2476:  * @param array $attributes Array of attributes, must contain 'empty' key.
2477:  * @return array Attributes array with currently selected value.
2478:  */
2479:     protected function _dateTimeSelected($select, $fieldName, $attributes) {
2480:         if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
2481:             if (is_array($value)) {
2482:                 $attributes['value'] = isset($value[$select]) ? $value[$select] : null;
2483:             } else {
2484:                 if (empty($value)) {
2485:                     if (!$attributes['empty']) {
2486:                         $attributes['value'] = 'now';
2487:                     }
2488:                 } else {
2489:                     $attributes['value'] = $value;
2490:                 }
2491:             }
2492:         }
2493:         return $attributes;
2494:     }
2495: 
2496: /**
2497:  * Returns a SELECT element for AM or PM.
2498:  *
2499:  * ### Attributes:
2500:  *
2501:  * - `empty` - If true, the empty select option is shown. If a string,
2502:  *   that string is displayed as the empty element.
2503:  * - `value` The selected value of the input.
2504:  *
2505:  * @param string $fieldName Prefix name for the SELECT element
2506:  * @param array $attributes Array of Attributes
2507:  * @return string Completed meridian select input
2508:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::meridian
2509:  */
2510:     public function meridian($fieldName, $attributes = array()) {
2511:         $attributes += array('empty' => true, 'value' => null);
2512:         if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
2513:             if (is_array($value)) {
2514:                 $meridian = null;
2515:                 extract($value);
2516:                 $attributes['value'] = $meridian;
2517:             } else {
2518:                 if (empty($value)) {
2519:                     if (!$attributes['empty']) {
2520:                         $attributes['value'] = date('a');
2521:                     }
2522:                 } else {
2523:                     $date = date_create($attributes['value']);
2524:                     $attributes['value'] = null;
2525:                     if ($date) {
2526:                         $attributes['value'] = $date->format('a');
2527:                     }
2528:                 }
2529:             }
2530:         }
2531: 
2532:         if ($attributes['value'] === false) {
2533:             $attributes['value'] = null;
2534:         }
2535:         return $this->select(
2536:             $fieldName . ".meridian", $this->_generateOptions('meridian'),
2537:             $attributes
2538:         );
2539:     }
2540: 
2541: /**
2542:  * Returns a set of SELECT elements for a full datetime setup: day, month and year, and then time.
2543:  *
2544:  * ### Attributes:
2545:  *
2546:  * - `monthNames` If false, 2 digit numbers will be used instead of text.
2547:  *   If an array, the given array will be used.
2548:  * - `minYear` The lowest year to use in the year select
2549:  * - `maxYear` The maximum year to use in the year select
2550:  * - `interval` The interval for the minutes select. Defaults to 1
2551:  * - `separator` The contents of the string between select elements. Defaults to '-'
2552:  * - `empty` - If true, the empty select option is shown. If a string,
2553:  *   that string is displayed as the empty element.
2554:  * - `round` - Set to `up` or `down` if you want to force rounding in either direction. Defaults to null.
2555:  * - `value` | `default` The default value to be used by the input. A value in `$this->data`
2556:  *   matching the field name will override this value. If no default is provided `time()` will be used.
2557:  *
2558:  * @param string $fieldName Prefix name for the SELECT element
2559:  * @param string $dateFormat DMY, MDY, YMD, or null to not generate date inputs.
2560:  * @param string $timeFormat 12, 24, or null to not generate time inputs.
2561:  * @param array $attributes Array of Attributes
2562:  * @return string Generated set of select boxes for the date and time formats chosen.
2563:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::dateTime
2564:  */
2565:     public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $attributes = array()) {
2566:         $attributes += array('empty' => true, 'value' => null);
2567:         $year = $month = $day = $hour = $min = $meridian = null;
2568: 
2569:         if (empty($attributes['value'])) {
2570:             $attributes = $this->value($attributes, $fieldName);
2571:         }
2572: 
2573:         if ($attributes['value'] === null && $attributes['empty'] != true) {
2574:             $attributes['value'] = time();
2575:             if (!empty($attributes['maxYear']) && $attributes['maxYear'] < date('Y')) {
2576:                 $attributes['value'] = strtotime(date($attributes['maxYear'] . '-m-d'));
2577:             }
2578:         }
2579: 
2580:         if (!empty($attributes['value'])) {
2581:             list($year, $month, $day, $hour, $min, $meridian) = $this->_getDateTimeValue(
2582:                 $attributes['value'],
2583:                 $timeFormat
2584:             );
2585:         }
2586: 
2587:         $defaults = array(
2588:             'minYear' => null, 'maxYear' => null, 'separator' => '-',
2589:             'interval' => 1, 'monthNames' => true, 'round' => null
2590:         );
2591:         $attributes = array_merge($defaults, (array)$attributes);
2592:         if (isset($attributes['minuteInterval'])) {
2593:             $attributes['interval'] = $attributes['minuteInterval'];
2594:             unset($attributes['minuteInterval']);
2595:         }
2596:         $minYear = $attributes['minYear'];
2597:         $maxYear = $attributes['maxYear'];
2598:         $separator = $attributes['separator'];
2599:         $interval = $attributes['interval'];
2600:         $monthNames = $attributes['monthNames'];
2601:         $round = $attributes['round'];
2602:         $attributes = array_diff_key($attributes, $defaults);
2603: 
2604:         if (!empty($interval) && $interval > 1 && !empty($min)) {
2605:             $current = new DateTime();
2606:             if ($year !== null) {
2607:                 $current->setDate($year, $month, $day);
2608:             }
2609:             if ($hour !== null) {
2610:                 $current->setTime($hour, $min);
2611:             }
2612:             $changeValue = $min * (1 / $interval);
2613:             switch ($round) {
2614:                 case 'up':
2615:                     $changeValue = ceil($changeValue);
2616:                     break;
2617:                 case 'down':
2618:                     $changeValue = floor($changeValue);
2619:                     break;
2620:                 default:
2621:                     $changeValue = round($changeValue);
2622:             }
2623:             $change = ($changeValue * $interval) - $min;
2624:             $current->modify($change > 0 ? "+$change minutes" : "$change minutes");
2625:             $format = ($timeFormat == 12) ? 'Y m d h i a' : 'Y m d H i a';
2626:             $newTime = explode(' ', $current->format($format));
2627:             list($year, $month, $day, $hour, $min, $meridian) = $newTime;
2628:         }
2629: 
2630:         $keys = array('Day', 'Month', 'Year', 'Hour', 'Minute', 'Meridian');
2631:         $attrs = array_fill_keys($keys, $attributes);
2632: 
2633:         $hasId = isset($attributes['id']);
2634:         if ($hasId && is_array($attributes['id'])) {
2635:             // check for missing ones and build selectAttr for each element
2636:             $attributes['id'] += array(
2637:                 'month' => '',
2638:                 'year' => '',
2639:                 'day' => '',
2640:                 'hour' => '',
2641:                 'minute' => '',
2642:                 'meridian' => ''
2643:             );
2644:             foreach ($keys as $key) {
2645:                 $attrs[$key]['id'] = $attributes['id'][strtolower($key)];
2646:             }
2647:         }
2648:         if ($hasId && is_string($attributes['id'])) {
2649:             // build out an array version
2650:             foreach ($keys as $key) {
2651:                 $attrs[$key]['id'] = $attributes['id'] . $key;
2652:             }
2653:         }
2654: 
2655:         if (is_array($attributes['empty'])) {
2656:             $attributes['empty'] += array(
2657:                 'month' => true,
2658:                 'year' => true,
2659:                 'day' => true,
2660:                 'hour' => true,
2661:                 'minute' => true,
2662:                 'meridian' => true
2663:             );
2664:             foreach ($keys as $key) {
2665:                 $attrs[$key]['empty'] = $attributes['empty'][strtolower($key)];
2666:             }
2667:         }
2668: 
2669:         $selects = array();
2670:         foreach (preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY) as $char) {
2671:             switch ($char) {
2672:                 case 'Y':
2673:                     $attrs['Year']['value'] = $year;
2674:                     $selects[] = $this->year(
2675:                         $fieldName, $minYear, $maxYear, $attrs['Year']
2676:                     );
2677:                     break;
2678:                 case 'M':
2679:                     $attrs['Month']['value'] = $month;
2680:                     $attrs['Month']['monthNames'] = $monthNames;
2681:                     $selects[] = $this->month($fieldName, $attrs['Month']);
2682:                     break;
2683:                 case 'D':
2684:                     $attrs['Day']['value'] = $day;
2685:                     $selects[] = $this->day($fieldName, $attrs['Day']);
2686:                     break;
2687:             }
2688:         }
2689:         $opt = implode($separator, $selects);
2690: 
2691:         $attrs['Minute']['interval'] = $interval;
2692:         switch ($timeFormat) {
2693:             case '24':
2694:                 $attrs['Hour']['value'] = $hour;
2695:                 $attrs['Minute']['value'] = $min;
2696:                 $opt .= $this->hour($fieldName, true, $attrs['Hour']) . ':' .
2697:                 $this->minute($fieldName, $attrs['Minute']);
2698:                 break;
2699:             case '12':
2700:                 $attrs['Hour']['value'] = $hour;
2701:                 $attrs['Minute']['value'] = $min;
2702:                 $attrs['Meridian']['value'] = $meridian;
2703:                 $opt .= $this->hour($fieldName, false, $attrs['Hour']) . ':' .
2704:                 $this->minute($fieldName, $attrs['Minute']) . ' ' .
2705:                 $this->meridian($fieldName, $attrs['Meridian']);
2706:                 break;
2707:         }
2708:         return $opt;
2709:     }
2710: 
2711: /**
2712:  * Parse the value for a datetime selected value
2713:  *
2714:  * @param string|array $value The selected value.
2715:  * @param int $timeFormat The time format
2716:  * @return array Array of selected value.
2717:  */
2718:     protected function _getDateTimeValue($value, $timeFormat) {
2719:         $year = $month = $day = $hour = $min = $meridian = null;
2720:         if (is_array($value)) {
2721:             extract($value);
2722:             if ($meridian === 'pm') {
2723:                 $hour += 12;
2724:             }
2725:             return array($year, $month, $day, $hour, $min, $meridian);
2726:         }
2727: 
2728:         if (is_numeric($value)) {
2729:             $value = strftime('%Y-%m-%d %H:%M:%S', $value);
2730:         }
2731:         $meridian = 'am';
2732:         $pos = strpos($value, '-');
2733:         if ($pos !== false) {
2734:             $date = explode('-', $value);
2735:             $days = explode(' ', $date[2]);
2736:             $day = $days[0];
2737:             $month = $date[1];
2738:             $year = $date[0];
2739:         } else {
2740:             $days[1] = $value;
2741:         }
2742: 
2743:         if (!empty($timeFormat)) {
2744:             $time = explode(':', $days[1]);
2745: 
2746:             if ($time[0] >= 12) {
2747:                 $meridian = 'pm';
2748:             }
2749:             $hour = $min = null;
2750:             if (isset($time[1])) {
2751:                 $hour = $time[0];
2752:                 $min = $time[1];
2753:             }
2754:         }
2755:         return array($year, $month, $day, $hour, $min, $meridian);
2756:     }
2757: 
2758: /**
2759:  * Gets the input field name for the current tag
2760:  *
2761:  * @param array $options Options list.
2762:  * @param string $field Field name.
2763:  * @param string $key Key name.
2764:  * @return array
2765:  */
2766:     protected function _name($options = array(), $field = null, $key = 'name') {
2767:         if ($this->requestType === 'get') {
2768:             if ($options === null) {
2769:                 $options = array();
2770:             } elseif (is_string($options)) {
2771:                 $field = $options;
2772:                 $options = 0;
2773:             }
2774: 
2775:             if (!empty($field)) {
2776:                 $this->setEntity($field);
2777:             }
2778: 
2779:             if (is_array($options) && isset($options[$key])) {
2780:                 return $options;
2781:             }
2782: 
2783:             $entity = $this->entity();
2784:             $model = $this->model();
2785:             $name = $model === $entity[0] && isset($entity[1]) ? $entity[1] : $entity[0];
2786:             $last = $entity[count($entity) - 1];
2787:             if (in_array($last, $this->_fieldSuffixes)) {
2788:                 $name .= '[' . $last . ']';
2789:             }
2790: 
2791:             if (is_array($options)) {
2792:                 $options[$key] = $name;
2793:                 return $options;
2794:             }
2795:             return $name;
2796:         }
2797:         return parent::_name($options, $field, $key);
2798:     }
2799: 
2800: /**
2801:  * Returns an array of formatted OPTION/OPTGROUP elements
2802:  *
2803:  * @param array $elements Elements to format.
2804:  * @param array $parents Parents for OPTGROUP.
2805:  * @param bool $showParents Whether to show parents.
2806:  * @param array $attributes HTML attributes.
2807:  * @return array
2808:  */
2809:     protected function _selectOptions($elements = array(), $parents = array(), $showParents = null, $attributes = array()) {
2810:         $select = array();
2811:         $attributes = array_merge(
2812:             array('escape' => true, 'style' => null, 'value' => null, 'class' => null),
2813:             $attributes
2814:         );
2815:         $selectedIsEmpty = ($attributes['value'] === '' || $attributes['value'] === null);
2816:         $selectedIsArray = is_array($attributes['value']);
2817: 
2818:         // Cast boolean false into an integer so string comparisons can work.
2819:         if ($attributes['value'] === false) {
2820:             $attributes['value'] = 0;
2821:         }
2822: 
2823:         $this->_domIdSuffixes = array();
2824:         foreach ($elements as $name => $title) {
2825:             $htmlOptions = array();
2826:             if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
2827:                 if (!empty($name)) {
2828:                     if ($attributes['style'] === 'checkbox') {
2829:                         $select[] = $this->Html->useTag('fieldsetend');
2830:                     } else {
2831:                         $select[] = $this->Html->useTag('optiongroupend');
2832:                     }
2833:                     $parents[] = $name;
2834:                 }
2835:                 $select = array_merge($select, $this->_selectOptions(
2836:                     $title, $parents, $showParents, $attributes
2837:                 ));
2838: 
2839:                 if (!empty($name)) {
2840:                     $name = $attributes['escape'] ? h($name) : $name;
2841:                     if ($attributes['style'] === 'checkbox') {
2842:                         $select[] = $this->Html->useTag('fieldsetstart', $name);
2843:                     } else {
2844:                         $select[] = $this->Html->useTag('optiongroup', $name, '');
2845:                     }
2846:                 }
2847:                 $name = null;
2848:             } elseif (is_array($title)) {
2849:                 $htmlOptions = $title;
2850:                 $name = $title['value'];
2851:                 $title = $title['name'];
2852:                 unset($htmlOptions['name'], $htmlOptions['value']);
2853:             }
2854: 
2855:             if ($name !== null) {
2856:                 $isNumeric = is_numeric($name);
2857:                 if ((!$selectedIsArray && !$selectedIsEmpty && (string)$attributes['value'] == (string)$name) ||
2858:                     ($selectedIsArray && in_array((string)$name, $attributes['value'], !$isNumeric))
2859:                 ) {
2860:                     if ($attributes['style'] === 'checkbox') {
2861:                         $htmlOptions['checked'] = true;
2862:                     } else {
2863:                         $htmlOptions['selected'] = 'selected';
2864:                     }
2865:                 }
2866: 
2867:                 if ($showParents || (!in_array($title, $parents))) {
2868:                     $title = ($attributes['escape']) ? h($title) : $title;
2869: 
2870:                     $hasDisabled = !empty($attributes['disabled']);
2871:                     if ($hasDisabled) {
2872:                         $disabledIsArray = is_array($attributes['disabled']);
2873:                         if ($disabledIsArray) {
2874:                             $disabledIsNumeric = is_numeric($name);
2875:                         }
2876:                     }
2877:                     if ($hasDisabled &&
2878:                         $disabledIsArray &&
2879:                         in_array((string)$name, $attributes['disabled'], !$disabledIsNumeric)
2880:                     ) {
2881:                         $htmlOptions['disabled'] = 'disabled';
2882:                     }
2883:                     if ($hasDisabled && !$disabledIsArray && $attributes['style'] === 'checkbox') {
2884:                         $htmlOptions['disabled'] = $attributes['disabled'] === true ? 'disabled' : $attributes['disabled'];
2885:                     }
2886: 
2887:                     if ($attributes['style'] === 'checkbox') {
2888:                         $htmlOptions['value'] = $name;
2889: 
2890:                         $tagName = $attributes['id'] . $this->domIdSuffix($name);
2891:                         $htmlOptions['id'] = $tagName;
2892:                         $label = array('for' => $tagName);
2893: 
2894:                         if (isset($htmlOptions['checked']) && $htmlOptions['checked'] === true) {
2895:                             $label['class'] = 'selected';
2896:                         }
2897: 
2898:                         $name = $attributes['name'];
2899: 
2900:                         if (empty($attributes['class'])) {
2901:                             $attributes['class'] = 'checkbox';
2902:                         } elseif ($attributes['class'] === 'form-error') {
2903:                             $attributes['class'] = 'checkbox ' . $attributes['class'];
2904:                         }
2905:                         $label = $this->label(null, $title, $label);
2906:                         $item = $this->Html->useTag('checkboxmultiple', $name, $htmlOptions);
2907:                         $select[] = $this->Html->div($attributes['class'], $item . $label);
2908:                     } else {
2909:                         if ($attributes['escape']) {
2910:                             $name = h($name);
2911:                         }
2912:                         $select[] = $this->Html->useTag('selectoption', $name, $htmlOptions, $title);
2913:                     }
2914:                 }
2915:             }
2916:         }
2917: 
2918:         return array_reverse($select, true);
2919:     }
2920: 
2921: /**
2922:  * Generates option lists for common <select /> menus
2923:  *
2924:  * @param string $name List type name.
2925:  * @param array $options Options list.
2926:  * @return array
2927:  */
2928:     protected function _generateOptions($name, $options = array()) {
2929:         if (!empty($this->options[$name])) {
2930:             return $this->options[$name];
2931:         }
2932:         $data = array();
2933: 
2934:         switch ($name) {
2935:             case 'minute':
2936:                 if (isset($options['interval'])) {
2937:                     $interval = $options['interval'];
2938:                 } else {
2939:                     $interval = 1;
2940:                 }
2941:                 $i = 0;
2942:                 while ($i < 60) {
2943:                     $data[sprintf('%02d', $i)] = sprintf('%02d', $i);
2944:                     $i += $interval;
2945:                 }
2946:                 break;
2947:             case 'hour':
2948:                 for ($i = 1; $i <= 12; $i++) {
2949:                     $data[sprintf('%02d', $i)] = $i;
2950:                 }
2951:                 break;
2952:             case 'hour24':
2953:                 for ($i = 0; $i <= 23; $i++) {
2954:                     $data[sprintf('%02d', $i)] = $i;
2955:                 }
2956:                 break;
2957:             case 'meridian':
2958:                 $data = array('am' => 'am', 'pm' => 'pm');
2959:                 break;
2960:             case 'day':
2961:                 for ($i = 1; $i <= 31; $i++) {
2962:                     $data[sprintf('%02d', $i)] = $i;
2963:                 }
2964:                 break;
2965:             case 'month':
2966:                 if ($options['monthNames'] === true) {
2967:                     $data['01'] = __d('cake', 'January');
2968:                     $data['02'] = __d('cake', 'February');
2969:                     $data['03'] = __d('cake', 'March');
2970:                     $data['04'] = __d('cake', 'April');
2971:                     $data['05'] = __d('cake', 'May');
2972:                     $data['06'] = __d('cake', 'June');
2973:                     $data['07'] = __d('cake', 'July');
2974:                     $data['08'] = __d('cake', 'August');
2975:                     $data['09'] = __d('cake', 'September');
2976:                     $data['10'] = __d('cake', 'October');
2977:                     $data['11'] = __d('cake', 'November');
2978:                     $data['12'] = __d('cake', 'December');
2979:                 } elseif (is_array($options['monthNames'])) {
2980:                     $data = $options['monthNames'];
2981:                 } else {
2982:                     for ($m = 1; $m <= 12; $m++) {
2983:                         $data[sprintf("%02s", $m)] = strftime("%m", mktime(1, 1, 1, $m, 1, 1999));
2984:                     }
2985:                 }
2986:                 break;
2987:             case 'year':
2988:                 $current = (int)date('Y');
2989: 
2990:                 $min = !isset($options['min']) ? $current - 20 : (int)$options['min'];
2991:                 $max = !isset($options['max']) ? $current + 20 : (int)$options['max'];
2992: 
2993:                 if ($min > $max) {
2994:                     list($min, $max) = array($max, $min);
2995:                 }
2996:                 if (!empty($options['value']) &&
2997:                     (int)$options['value'] < $min &&
2998:                     (int)$options['value'] > 0
2999:                 ) {
3000:                     $min = (int)$options['value'];
3001:                 } elseif (!empty($options['value']) && (int)$options['value'] > $max) {
3002:                     $max = (int)$options['value'];
3003:                 }
3004: 
3005:                 for ($i = $min; $i <= $max; $i++) {
3006:                     $data[$i] = $i;
3007:                 }
3008:                 if ($options['order'] !== 'asc') {
3009:                     $data = array_reverse($data, true);
3010:                 }
3011:                 break;
3012:         }
3013:         $this->_options[$name] = $data;
3014:         return $this->_options[$name];
3015:     }
3016: 
3017: /**
3018:  * Sets field defaults and adds field to form security input hash.
3019:  * Will also add a 'form-error' class if the field contains validation errors.
3020:  *
3021:  * ### Options
3022:  *
3023:  * - `secure` - boolean whether or not the field should be added to the security fields.
3024:  *   Disabling the field using the `disabled` option, will also omit the field from being
3025:  *   part of the hashed key.
3026:  *
3027:  * This method will convert a numerically indexed 'disabled' into an associative
3028:  * value. FormHelper's internals expect associative options.
3029:  *
3030:  * @param string $field Name of the field to initialize options for.
3031:  * @param array $options Array of options to append options into.
3032:  * @return array Array of options for the input.
3033:  */
3034:     protected function _initInputField($field, $options = array()) {
3035:         if (isset($options['secure'])) {
3036:             $secure = $options['secure'];
3037:             unset($options['secure']);
3038:         } else {
3039:             $secure = (isset($this->request['_Token']) && !empty($this->request['_Token']));
3040:         }
3041: 
3042:         $disabledIndex = array_search('disabled', $options, true);
3043:         if (is_int($disabledIndex)) {
3044:             unset($options[$disabledIndex]);
3045:             $options['disabled'] = true;
3046:         }
3047: 
3048:         $result = parent::_initInputField($field, $options);
3049:         if ($this->tagIsInvalid() !== false) {
3050:             $result = $this->addClass($result, 'form-error');
3051:         }
3052: 
3053:         $isDisabled = false;
3054:         if (isset($result['disabled'])) {
3055:             $isDisabled = (
3056:                 $result['disabled'] === true ||
3057:                 $result['disabled'] === 'disabled' ||
3058:                 (is_array($result['disabled']) &&
3059:                     !empty($result['options']) &&
3060:                     array_diff($result['options'], $result['disabled']) === array()
3061:                 )
3062:             );
3063:         }
3064:         if ($isDisabled) {
3065:             return $result;
3066:         }
3067: 
3068:         if (!isset($result['required']) &&
3069:             $this->_introspectModel($this->model(), 'validates', $this->field())
3070:         ) {
3071:             $result['required'] = true;
3072:         }
3073: 
3074:         if ($secure === static::SECURE_SKIP) {
3075:             return $result;
3076:         }
3077: 
3078:         $this->_secure($secure, $this->_secureFieldName($options));
3079:         return $result;
3080:     }
3081: 
3082: /**
3083:  * Get the field name for use with _secure().
3084:  *
3085:  * Parses the name attribute to create a dot separated name value for use
3086:  * in secured field hash.
3087:  *
3088:  * @param array $options An array of options possibly containing a name key.
3089:  * @return string|null
3090:  */
3091:     protected function _secureFieldName($options) {
3092:         if (isset($options['name'])) {
3093:             preg_match_all('/\[(.*?)\]/', $options['name'], $matches);
3094:             if (isset($matches[1])) {
3095:                 return $matches[1];
3096:             }
3097:         }
3098:         return null;
3099:     }
3100: 
3101: /**
3102:  * Sets the last created form action.
3103:  *
3104:  * @param string|array $url URL.
3105:  * @return void
3106:  */
3107:     protected function _lastAction($url) {
3108:         $action = html_entity_decode($this->url($url), ENT_QUOTES);
3109:         $query = parse_url($action, PHP_URL_QUERY);
3110:         $query = $query ? '?' . $query : '';
3111:         $this->_lastAction = parse_url($action, PHP_URL_PATH) . $query;
3112:     }
3113: 
3114: /**
3115:  * Set/Get inputDefaults for form elements
3116:  *
3117:  * @param array $defaults New default values
3118:  * @param bool $merge Merge with current defaults
3119:  * @return array inputDefaults
3120:  */
3121:     public function inputDefaults($defaults = null, $merge = false) {
3122:         if ($defaults !== null) {
3123:             if ($merge) {
3124:                 $this->_inputDefaults = array_merge($this->_inputDefaults, (array)$defaults);
3125:             } else {
3126:                 $this->_inputDefaults = (array)$defaults;
3127:             }
3128:         }
3129:         return $this->_inputDefaults;
3130:     }
3131: 
3132: }
3133: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs