cake/libs/view/helpers/form.php
| 1 | <?php |
|---|---|
| 2 | /** |
| 3 | * Automatic generation of HTML FORMs from given data. |
| 4 | * |
| 5 | * Used for scaffolding. |
| 6 | * |
| 7 | * PHP versions 4 and 5 |
| 8 | * |
| 9 | * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
| 10 | * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) |
| 11 | * |
| 12 | * Licensed under The MIT License |
| 13 | * Redistributions of files must retain the above copyright notice. |
| 14 | * |
| 15 | * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) |
| 16 | * @link http://cakephp.org CakePHP(tm) Project |
| 17 | * @package cake |
| 18 | * @subpackage cake.cake.libs.view.helpers |
| 19 | * @since CakePHP(tm) v 0.10.0.1076 |
| 20 | * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * Form helper library. |
| 25 | * |
| 26 | * Automatic generation of HTML FORMs from given data. |
| 27 | * |
| 28 | * @package cake |
| 29 | * @subpackage cake.cake.libs.view.helpers |
| 30 | * @link http://book.cakephp.org/view/1383/Form |
| 31 | */ |
| 32 | class FormHelper extends AppHelper { |
| 33 | |
| 34 | /** |
| 35 | * Other helpers used by FormHelper |
| 36 | * |
| 37 | * @var array |
| 38 | * @access public |
| 39 | */ |
| 40 | var $helpers = array('Html'); |
| 41 | |
| 42 | /** |
| 43 | * Holds the fields array('field_name' => array('type'=> 'string', 'length'=> 100), |
| 44 | * primaryKey and validates array('field_name') |
| 45 | * |
| 46 | * @access public |
| 47 | */ |
| 48 | var $fieldset = array(); |
| 49 | |
| 50 | /** |
| 51 | * Options used by DateTime fields |
| 52 | * |
| 53 | * @var array |
| 54 | */ |
| 55 | var $__options = array( |
| 56 | 'day' => array(), 'minute' => array(), 'hour' => array(), |
| 57 | 'month' => array(), 'year' => array(), 'meridian' => array() |
| 58 | ); |
| 59 | |
| 60 | /** |
| 61 | * List of fields created, used with secure forms. |
| 62 | * |
| 63 | * @var array |
| 64 | * @access public |
| 65 | */ |
| 66 | var $fields = array(); |
| 67 | |
| 68 | /** |
| 69 | * Defines the type of form being created. Set by FormHelper::create(). |
| 70 | * |
| 71 | * @var string |
| 72 | * @access public |
| 73 | */ |
| 74 | var $requestType = null; |
| 75 | |
| 76 | /** |
| 77 | * The default model being used for the current form. |
| 78 | * |
| 79 | * @var string |
| 80 | * @access public |
| 81 | */ |
| 82 | var $defaultModel = null; |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * Persistent default options used by input(). Set by FormHelper::create(). |
| 87 | * |
| 88 | * @var array |
| 89 | * @access protected |
| 90 | */ |
| 91 | var $_inputDefaults = array(); |
| 92 | |
| 93 | /** |
| 94 | * Introspects model information and extracts information related |
| 95 | * to validation, field length and field type. Appends information into |
| 96 | * $this->fieldset. |
| 97 | * |
| 98 | * @return Model Returns a model instance |
| 99 | * @access protected |
| 100 | */ |
| 101 | function &_introspectModel($model) { |
| 102 | $object = null; |
| 103 | if (is_string($model) && strpos($model, '.') !== false) { |
| 104 | $path = explode('.', $model); |
| 105 | $model = end($path); |
| 106 | } |
| 107 | |
| 108 | if (ClassRegistry::isKeySet($model)) { |
| 109 | $object =& ClassRegistry::getObject($model); |
| 110 | } |
| 111 | |
| 112 | if (!empty($object)) { |
| 113 | $fields = $object->schema(); |
| 114 | foreach ($fields as $key => $value) { |
| 115 | unset($fields[$key]); |
| 116 | $fields[$key] = $value; |
| 117 | } |
| 118 | |
| 119 | if (!empty($object->hasAndBelongsToMany)) { |
| 120 | foreach ($object->hasAndBelongsToMany as $alias => $assocData) { |
| 121 | $fields[$alias] = array('type' => 'multiple'); |
| 122 | } |
| 123 | } |
| 124 | $validates = array(); |
| 125 | if (!empty($object->validate)) { |
| 126 | foreach ($object->validate as $validateField => $validateProperties) { |
| 127 | if ($this->_isRequiredField($validateProperties)) { |
| 128 | $validates[] = $validateField; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | $defaults = array('fields' => array(), 'key' => 'id', 'validates' => array()); |
| 133 | $key = $object->primaryKey; |
| 134 | $this->fieldset[$model] = array_merge($defaults, compact('fields', 'key', 'validates')); |
| 135 | } |
| 136 | |
| 137 | return $object; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Returns if a field is required to be filled based on validation properties from the validating object |
| 142 | * |
| 143 | * @return boolean true if field is required to be filled, false otherwise |
| 144 | * @access protected |
| 145 | */ |
| 146 | function _isRequiredField($validateProperties) { |
| 147 | $required = false; |
| 148 | if (is_array($validateProperties)) { |
| 149 | |
| 150 | $dims = Set::countDim($validateProperties); |
| 151 | if ($dims == 1 || ($dims == 2 && isset($validateProperties['rule']))) { |
| 152 | $validateProperties = array($validateProperties); |
| 153 | } |
| 154 | |
| 155 | foreach ($validateProperties as $rule => $validateProp) { |
| 156 | if (isset($validateProp['allowEmpty']) && $validateProp['allowEmpty'] === true) { |
| 157 | return false; |
| 158 | } |
| 159 | $rule = isset($validateProp['rule']) ? $validateProp['rule'] : false; |
| 160 | $required = $rule || empty($validateProp); |
| 161 | if ($required) { |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | return $required; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Returns an HTML FORM element. |
| 171 | * |
| 172 | * ### Options: |
| 173 | * |
| 174 | * - `type` Form method defaults to POST |
| 175 | * - `action` The controller action the form submits to, (optional). |
| 176 | * - `url` The url the form submits to. Can be a string or a url array, |
| 177 | * - `default` Allows for the creation of Ajax forms. |
| 178 | * - `onsubmit` Used in conjunction with 'default' to create ajax forms. |
| 179 | * - `inputDefaults` set the default $options for FormHelper::input(). Any options that would |
| 180 | * be set when using FormHelper::input() can be set here. Options set with `inputDefaults` |
| 181 | * can be overridden when calling input() |
| 182 | * - `encoding` Set the accept-charset encoding for the form. Defaults to `Configure::read('App.encoding')` |
| 183 | * |
| 184 | * @access public |
| 185 | * @param string $model The model object which the form is being defined for |
| 186 | * @param array $options An array of html attributes and options. |
| 187 | * @return string An formatted opening FORM tag. |
| 188 | * @link http://book.cakephp.org/view/1384/Creating-Forms |
| 189 | */ |
| 190 | function create($model = null, $options = array()) { |
| 191 | $created = $id = false; |
| 192 | $append = ''; |
| 193 | $view =& ClassRegistry::getObject('view'); |
| 194 | |
| 195 | if (is_array($model) && empty($options)) { |
| 196 | $options = $model; |
| 197 | $model = null; |
| 198 | } |
| 199 | if (empty($model) && $model !== false && !empty($this->params['models'])) { |
| 200 | $model = $this->params['models'][0]; |
| 201 | $this->defaultModel = $this->params['models'][0]; |
| 202 | } elseif (empty($model) && empty($this->params['models'])) { |
| 203 | $model = false; |
| 204 | } |
| 205 | |
| 206 | $models = ClassRegistry::keys(); |
| 207 | foreach ($models as $currentModel) { |
| 208 | if (ClassRegistry::isKeySet($currentModel)) { |
| 209 | $currentObject =& ClassRegistry::getObject($currentModel); |
| 210 | if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) { |
| 211 | $this->validationErrors[Inflector::camelize($currentModel)] =& $currentObject->validationErrors; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | $object = $this->_introspectModel($model); |
| 217 | $this->setEntity($model . '.', true); |
| 218 | |
| 219 | $modelEntity = $this->model(); |
| 220 | if (isset($this->fieldset[$modelEntity]['key'])) { |
| 221 | $data = $this->fieldset[$modelEntity]; |
| 222 | $recordExists = ( |
| 223 | isset($this->data[$model]) && |
| 224 | !empty($this->data[$model][$data['key']]) && |
| 225 | !is_array($this->data[$model][$data['key']]) |
| 226 | ); |
| 227 | |
| 228 | if ($recordExists) { |
| 229 | $created = true; |
| 230 | $id = $this->data[$model][$data['key']]; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | $options = array_merge(array( |
| 235 | 'type' => ($created && empty($options['action'])) ? 'put' : 'post', |
| 236 | 'action' => null, |
| 237 | 'url' => null, |
| 238 | 'default' => true, |
| 239 | 'encoding' => strtolower(Configure::read('App.encoding')), |
| 240 | 'inputDefaults' => array()), |
| 241 | $options); |
| 242 | $this->_inputDefaults = $options['inputDefaults']; |
| 243 | unset($options['inputDefaults']); |
| 244 | |
| 245 | if (empty($options['url']) || is_array($options['url'])) { |
| 246 | if (empty($options['url']['controller'])) { |
| 247 | if (!empty($model) && $model != $this->defaultModel) { |
| 248 | $options['url']['controller'] = Inflector::underscore(Inflector::pluralize($model)); |
| 249 | } elseif (!empty($this->params['controller'])) { |
| 250 | $options['url']['controller'] = Inflector::underscore($this->params['controller']); |
| 251 | } |
| 252 | } |
| 253 | if (empty($options['action'])) { |
| 254 | $options['action'] = $this->params['action']; |
| 255 | } |
| 256 | |
| 257 | $actionDefaults = array( |
| 258 | 'plugin' => $this->plugin, |
| 259 | 'controller' => $view->viewPath, |
| 260 | 'action' => $options['action'] |
| 261 | ); |
| 262 | if (!empty($options['action']) && !isset($options['id'])) { |
| 263 | $options['id'] = $this->domId($options['action'] . 'Form'); |
| 264 | } |
| 265 | $options['action'] = array_merge($actionDefaults, (array)$options['url']); |
| 266 | if (empty($options['action'][0])) { |
| 267 | $options['action'][0] = $id; |
| 268 | } |
| 269 | } elseif (is_string($options['url'])) { |
| 270 | $options['action'] = $options['url']; |
| 271 | } |
| 272 | unset($options['url']); |
| 273 | |
| 274 | switch (strtolower($options['type'])) { |
| 275 | case 'get': |
| 276 | $htmlAttributes['method'] = 'get'; |
| 277 | break; |
| 278 | case 'file': |
| 279 | $htmlAttributes['enctype'] = 'multipart/form-data'; |
| 280 | $options['type'] = ($created) ? 'put' : 'post'; |
| 281 | case 'post': |
| 282 | case 'put': |
| 283 | case 'delete': |
| 284 | $append .= $this->hidden('_method', array( |
| 285 | 'name' => '_method', 'value' => strtoupper($options['type']), 'id' => null |
| 286 | )); |
| 287 | default: |
| 288 | $htmlAttributes['method'] = 'post'; |
| 289 | break; |
| 290 | } |
| 291 | $this->requestType = strtolower($options['type']); |
| 292 | |
| 293 | $htmlAttributes['action'] = $this->url($options['action']); |
| 294 | unset($options['type'], $options['action']); |
| 295 | |
| 296 | if ($options['default'] == false) { |
| 297 | if (isset($htmlAttributes['onSubmit']) || isset($htmlAttributes['onsubmit'])) { |
| 298 | $htmlAttributes['onsubmit'] .= ' event.returnValue = false; return false;'; |
| 299 | } else { |
| 300 | $htmlAttributes['onsubmit'] = 'event.returnValue = false; return false;'; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if (!empty($options['encoding'])) { |
| 305 | $htmlAttributes['accept-charset'] = $options['encoding']; |
| 306 | unset($options['encoding']); |
| 307 | } |
| 308 | |
| 309 | unset($options['default']); |
| 310 | $htmlAttributes = array_merge($options, $htmlAttributes); |
| 311 | |
| 312 | $this->fields = array(); |
| 313 | if (isset($this->params['_Token']) && !empty($this->params['_Token'])) { |
| 314 | $append .= $this->hidden('_Token.key', array( |
| 315 | 'value' => $this->params['_Token']['key'], 'id' => 'Token' . mt_rand()) |
| 316 | ); |
| 317 | } |
| 318 | |
| 319 | if (!empty($append)) { |
| 320 | $append = sprintf($this->Html->tags['block'], ' style="display:none;"', $append); |
| 321 | } |
| 322 | |
| 323 | $this->setEntity($model . '.', true); |
| 324 | $attributes = $this->_parseAttributes($htmlAttributes, null, ''); |
| 325 | return sprintf($this->Html->tags['form'], $attributes) . $append; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Closes an HTML form, cleans up values set by FormHelper::create(), and writes hidden |
| 330 | * input fields where appropriate. |
| 331 | * |
| 332 | * If $options is set a form submit button will be created. Options can be either a string or an array. |
| 333 | * |
| 334 | * {{{ |
| 335 | * array usage: |
| 336 | * |
| 337 | * array('label' => 'save'); value="save" |
| 338 | * array('label' => 'save', 'name' => 'Whatever'); value="save" name="Whatever" |
| 339 | * array('name' => 'Whatever'); value="Submit" name="Whatever" |
| 340 | * array('label' => 'save', 'name' => 'Whatever', 'div' => 'good') <div class="good"> value="save" name="Whatever" |
| 341 | * array('label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')); <div class="good"> value="save" name="Whatever" |
| 342 | * }}} |
| 343 | * |
| 344 | * @param mixed $options as a string will use $options as the value of button, |
| 345 | * @return string a closing FORM tag optional submit button. |
| 346 | * @access public |
| 347 | * @link http://book.cakephp.org/view/1389/Closing-the-Form |
| 348 | */ |
| 349 | function end($options = null) { |
| 350 | if (!empty($this->params['models'])) { |
| 351 | $models = $this->params['models'][0]; |
| 352 | } |
| 353 | $out = null; |
| 354 | $submit = null; |
| 355 | |
| 356 | if ($options !== null) { |
| 357 | $submitOptions = array(); |
| 358 | if (is_string($options)) { |
| 359 | $submit = $options; |
| 360 | } else { |
| 361 | if (isset($options['label'])) { |
| 362 | $submit = $options['label']; |
| 363 | unset($options['label']); |
| 364 | } |
| 365 | $submitOptions = $options; |
| 366 | |
| 367 | if (!$submit) { |
| 368 | $submit = __('Submit', true); |
| 369 | } |
| 370 | } |
| 371 | $out .= $this->submit($submit, $submitOptions); |
| 372 | } |
| 373 | if (isset($this->params['_Token']) && !empty($this->params['_Token'])) { |
| 374 | $out .= $this->secure($this->fields); |
| 375 | $this->fields = array(); |
| 376 | } |
| 377 | $this->setEntity(null); |
| 378 | $out .= $this->Html->tags['formend']; |
| 379 | |
| 380 | $view =& ClassRegistry::getObject('view'); |
| 381 | $view->modelScope = false; |
| 382 | return $out; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Generates a hidden field with a security hash based on the fields used in the form. |
| 387 | * |
| 388 | * @param array $fields The list of fields to use when generating the hash |
| 389 | * @return string A hidden input field with a security hash |
| 390 | * @access public |
| 391 | */ |
| 392 | function secure($fields = array()) { |
| 393 | if (!isset($this->params['_Token']) || empty($this->params['_Token'])) { |
| 394 | return; |
| 395 | } |
| 396 | $locked = array(); |
| 397 | |
| 398 | foreach ($fields as $key => $value) { |
| 399 | if (!is_int($key)) { |
| 400 | $locked[$key] = $value; |
| 401 | unset($fields[$key]); |
| 402 | } |
| 403 | } |
| 404 | sort($fields, SORT_STRING); |
| 405 | ksort($locked, SORT_STRING); |
| 406 | $fields += $locked; |
| 407 | |
| 408 | $fields = Security::hash(serialize($fields) . Configure::read('Security.salt')); |
| 409 | $locked = implode(array_keys($locked), '|'); |
| 410 | |
| 411 | $out = $this->hidden('_Token.fields', array( |
| 412 | 'value' => urlencode($fields . ':' . $locked), |
| 413 | 'id' => 'TokenFields' . mt_rand() |
| 414 | )); |
| 415 | $out = sprintf($this->Html->tags['block'], ' style="display:none;"', $out); |
| 416 | return $out; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Determine which fields of a form should be used for hash. |
| 421 | * Populates $this->fields |
| 422 | * |
| 423 | * @param mixed $field Reference to field to be secured |
| 424 | * @param mixed $value Field value, if value should not be tampered with. |
| 425 | * @return void |
| 426 | * @access private |
| 427 | */ |
| 428 | function __secure($field = null, $value = null) { |
| 429 | if (!$field) { |
| 430 | $view =& ClassRegistry::getObject('view'); |
| 431 | $field = $view->entity(); |
| 432 | } elseif (is_string($field)) { |
| 433 | $field = Set::filter(explode('.', $field), true); |
| 434 | } |
| 435 | |
| 436 | if (!empty($this->params['_Token']['disabledFields'])) { |
| 437 | foreach ((array)$this->params['_Token']['disabledFields'] as $disabled) { |
| 438 | $disabled = explode('.', $disabled); |
| 439 | if (array_values(array_intersect($field, $disabled)) === $disabled) { |
| 440 | return; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | $field = implode('.', $field); |
| 445 | if (!in_array($field, $this->fields)) { |
| 446 | if ($value !== null) { |
| 447 | return $this->fields[$field] = $value; |
| 448 | } |
| 449 | $this->fields[] = $field; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Returns true if there is an error for the given field, otherwise false |
| 455 | * |
| 456 | * @param string $field This should be "Modelname.fieldname" |
| 457 | * @return boolean If there are errors this method returns true, else false. |
| 458 | * @access public |
| 459 | * @link http://book.cakephp.org/view/1426/isFieldError |
| 460 | */ |
| 461 | function isFieldError($field) { |
| 462 | $this->setEntity($field); |
| 463 | return (bool)$this->tagIsInvalid(); |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Returns a formatted error message for given FORM field, NULL if no errors. |
| 468 | * |
| 469 | * ### Options: |
| 470 | * |
| 471 | * - `escape` bool Whether or not to html escape the contents of the error. |
| 472 | * - `wrap` mixed Whether or not the error message should be wrapped in a div. If a |
| 473 | * string, will be used as the HTML tag to use. |
| 474 | * - `class` string The classname for the error message |
| 475 | * |
| 476 | * @param string $field A field name, like "Modelname.fieldname" |
| 477 | * @param mixed $text Error message or array of $options. If array, `attributes` key |
| 478 | * will get used as html attributes for error container |
| 479 | * @param array $options Rendering options for <div /> wrapper tag |
| 480 | * @return string If there are errors this method returns an error message, otherwise null. |
| 481 | * @access public |
| 482 | * @link http://book.cakephp.org/view/1423/error |
| 483 | */ |
| 484 | function error($field, $text = null, $options = array()) { |
| 485 | $defaults = array('wrap' => true, 'class' => 'error-message', 'escape' => true); |
| 486 | $options = array_merge($defaults, $options); |
| 487 | $this->setEntity($field); |
| 488 | |
| 489 | if ($error = $this->tagIsInvalid()) { |
| 490 | if (is_array($error)) { |
| 491 | list(,,$field) = explode('.', $field); |
| 492 | if (isset($error[$field])) { |
| 493 | $error = $error[$field]; |
| 494 | } else { |
| 495 | return null; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | if (is_array($text) && is_numeric($error) && $error > 0) { |
| 500 | $error--; |
| 501 | } |
| 502 | if (is_array($text)) { |
| 503 | $options = array_merge($options, array_intersect_key($text, $defaults)); |
| 504 | if (isset($text['attributes']) && is_array($text['attributes'])) { |
| 505 | $options = array_merge($options, $text['attributes']); |
| 506 | } |
| 507 | $text = isset($text[$error]) ? $text[$error] : null; |
| 508 | unset($options[$error]); |
| 509 | } |
| 510 | |
| 511 | if ($text != null) { |
| 512 | $error = $text; |
| 513 | } elseif (is_numeric($error)) { |
| 514 | $error = sprintf(__('Error in field %s', true), Inflector::humanize($this->field())); |
| 515 | } |
| 516 | if ($options['escape']) { |
| 517 | $error = h($error); |
| 518 | unset($options['escape']); |
| 519 | } |
| 520 | if ($options['wrap']) { |
| 521 | $tag = is_string($options['wrap']) ? $options['wrap'] : 'div'; |
| 522 | unset($options['wrap']); |
| 523 | return $this->Html->tag($tag, $error, $options); |
| 524 | } else { |
| 525 | return $error; |
| 526 | } |
| 527 | } else { |
| 528 | return null; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Returns a formatted LABEL element for HTML FORMs. Will automatically generate |
| 534 | * a for attribute if one is not provided. |
| 535 | * |
| 536 | * @param string $fieldName This should be "Modelname.fieldname" |
| 537 | * @param string $text Text that will appear in the label field. |
| 538 | * @param mixed $options An array of HTML attributes, or a string, to be used as a class name. |
| 539 | * @return string The formatted LABEL element |
| 540 | * @link http://book.cakephp.org/view/1427/label |
| 541 | */ |
| 542 | function label($fieldName = null, $text = null, $options = array()) { |
| 543 | if (empty($fieldName)) { |
| 544 | $view = ClassRegistry::getObject('view'); |
| 545 | $fieldName = implode('.', $view->entity()); |
| 546 | } |
| 547 | |
| 548 | if ($text === null) { |
| 549 | if (strpos($fieldName, '.') !== false) { |
| 550 | $text = array_pop(explode('.', $fieldName)); |
| 551 | } else { |
| 552 | $text = $fieldName; |
| 553 | } |
| 554 | if (substr($text, -3) == '_id') { |
| 555 | $text = substr($text, 0, strlen($text) - 3); |
| 556 | } |
| 557 | $text = __(Inflector::humanize(Inflector::underscore($text)), true); |
| 558 | } |
| 559 | |
| 560 | if (is_string($options)) { |
| 561 | $options = array('class' => $options); |
| 562 | } |
| 563 | |
| 564 | if (isset($options['for'])) { |
| 565 | $labelFor = $options['for']; |
| 566 | unset($options['for']); |
| 567 | } else { |
| 568 | $labelFor = $this->domId($fieldName); |
| 569 | } |
| 570 | |
| 571 | return sprintf( |
| 572 | $this->Html->tags['label'], |
| 573 | $labelFor, |
| 574 | $this->_parseAttributes($options), $text |
| 575 | ); |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Generate a set of inputs for `$fields`. If $fields is null the current model |
| 580 | * will be used. |
| 581 | * |
| 582 | * In addition to controller fields output, `$fields` can be used to control legend |
| 583 | * and fieldset rendering with the `fieldset` and `legend` keys. |
| 584 | * `$form->inputs(array('legend' => 'My legend'));` Would generate an input set with |
| 585 | * a custom legend. You can customize individual inputs through `$fields` as well. |
| 586 | * |
| 587 | * {{{ |
| 588 | * $form->inputs(array( |
| 589 | * 'name' => array('label' => 'custom label') |
| 590 | * )); |
| 591 | * }}} |
| 592 | * |
| 593 | * In addition to fields control, inputs() allows you to use a few additional options. |
| 594 | * |
| 595 | * - `fieldset` Set to false to disable the fieldset. If a string is supplied it will be used as |
| 596 | * the classname for the fieldset element. |
| 597 | * - `legend` Set to false to disable the legend for the generated input set. Or supply a string |
| 598 | * to customize the legend text. |
| 599 | * |
| 600 | * @param mixed $fields An array of fields to generate inputs for, or null. |
| 601 | * @param array $blacklist a simple array of fields to not create inputs for. |
| 602 | * @return string Completed form inputs. |
| 603 | * @access public |
| 604 | */ |
| 605 | function inputs($fields = null, $blacklist = null) { |
| 606 | $fieldset = $legend = true; |
| 607 | $model = $this->model(); |
| 608 | if (is_array($fields)) { |
| 609 | if (array_key_exists('legend', $fields)) { |
| 610 | $legend = $fields['legend']; |
| 611 | unset($fields['legend']); |
| 612 | } |
| 613 | |
| 614 | if (isset($fields['fieldset'])) { |
| 615 | $fieldset = $fields['fieldset']; |
| 616 | unset($fields['fieldset']); |
| 617 | } |
| 618 | } elseif ($fields !== null) { |
| 619 | $fieldset = $legend = $fields; |
| 620 | if (!is_bool($fieldset)) { |
| 621 | $fieldset = true; |
| 622 | } |
| 623 | $fields = array(); |
| 624 | } |
| 625 | |
| 626 | if (empty($fields)) { |
| 627 | $fields = array_keys($this->fieldset[$model]['fields']); |
| 628 | } |
| 629 | |
| 630 | if ($legend === true) { |
| 631 | $actionName = __('New %s', true); |
| 632 | $isEdit = ( |
| 633 | strpos($this->action, 'update') !== false || |
| 634 | strpos($this->action, 'edit') !== false |
| 635 | ); |
| 636 | if ($isEdit) { |
| 637 | $actionName = __('Edit %s', true); |
| 638 | } |
| 639 | $modelName = Inflector::humanize(Inflector::underscore($model)); |
| 640 | $legend = sprintf($actionName, __($modelName, true)); |
| 641 | } |
| 642 | |
| 643 | $out = null; |
| 644 | foreach ($fields as $name => $options) { |
| 645 | if (is_numeric($name) && !is_array($options)) { |
| 646 | $name = $options; |
| 647 | $options = array(); |
| 648 | } |
| 649 | $entity = explode('.', $name); |
| 650 | $blacklisted = ( |
| 651 | is_array($blacklist) && |
| 652 | (in_array($name, $blacklist) || in_array(end($entity), $blacklist)) |
| 653 | ); |
| 654 | if ($blacklisted) { |
| 655 | continue; |
| 656 | } |
| 657 | $out .= $this->input($name, $options); |
| 658 | } |
| 659 | |
| 660 | if (is_string($fieldset)) { |
| 661 | $fieldsetClass = sprintf(' class="%s"', $fieldset); |
| 662 | } else { |
| 663 | $fieldsetClass = ''; |
| 664 | } |
| 665 | |
| 666 | if ($fieldset && $legend) { |
| 667 | return sprintf( |
| 668 | $this->Html->tags['fieldset'], |
| 669 | $fieldsetClass, |
| 670 | sprintf($this->Html->tags['legend'], $legend) . $out |
| 671 | ); |
| 672 | } elseif ($fieldset) { |
| 673 | return sprintf($this->Html->tags['fieldset'], $fieldsetClass, $out); |
| 674 | } else { |
| 675 | return $out; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * Generates a form input element complete with label and wrapper div |
| 681 | * |
| 682 | * ### Options |
| 683 | * |
| 684 | * See each field type method for more information. Any options that are part of |
| 685 | * $attributes or $options for the different **type** methods can be included in `$options` for input(). |
| 686 | * |
| 687 | * - `type` - Force the type of widget you want. e.g. `type => 'select'` |
| 688 | * - `label` - Either a string label, or an array of options for the label. See FormHelper::label() |
| 689 | * - `div` - Either `false` to disable the div, or an array of options for the div. |
| 690 | * See HtmlHelper::div() for more options. |
| 691 | * - `options` - for widgets that take options e.g. radio, select |
| 692 | * - `error` - control the error message that is produced |
| 693 | * - `empty` - String or boolean to enable empty select box options. |
| 694 | * - `before` - Content to place before the label + input. |
| 695 | * - `after` - Content to place after the label + input. |
| 696 | * - `between` - Content to place between the label + input. |
| 697 | * - `format` - format template for element order. Any element that is not in the array, will not be in the output. |
| 698 | * - Default input format order: array('before', 'label', 'between', 'input', 'after', 'error') |
| 699 | * - Default checkbox format order: array('before', 'input', 'between', 'label', 'after', 'error') |
| 700 | * - Hidden input will not be formatted |
| 701 | * - Radio buttons cannot have the order of input and label elements controlled with these settings. |
| 702 | * |
| 703 | * @param string $fieldName This should be "Modelname.fieldname" |
| 704 | * @param array $options Each type of input takes different options. |
| 705 | * @return string Completed form widget. |
| 706 | * @access public |
| 707 | * @link http://book.cakephp.org/view/1390/Automagic-Form-Elements |
| 708 | */ |
| 709 | function input($fieldName, $options = array()) { |
| 710 | $this->setEntity($fieldName); |
| 711 | |
| 712 | $options = array_merge( |
| 713 | array('before' => null, 'between' => null, 'after' => null, 'format' => null), |
| 714 | $this->_inputDefaults, |
| 715 | $options |
| 716 | ); |
| 717 | |
| 718 | $modelKey = $this->model(); |
| 719 | $fieldKey = $this->field(); |
| 720 | if (!isset($this->fieldset[$modelKey])) { |
| 721 | $this->_introspectModel($modelKey); |
| 722 | } |
| 723 | |
| 724 | if (!isset($options['type'])) { |
| 725 | $magicType = true; |
| 726 | $options['type'] = 'text'; |
| 727 | if (isset($options['options'])) { |
| 728 | $options['type'] = 'select'; |
| 729 | } elseif (in_array($fieldKey, array('psword', 'passwd', 'password'))) { |
| 730 | $options['type'] = 'password'; |
| 731 | } elseif (isset($this->fieldset[$modelKey]['fields'][$fieldKey])) { |
| 732 | $fieldDef = $this->fieldset[$modelKey]['fields'][$fieldKey]; |
| 733 | $type = $fieldDef['type']; |
| 734 | $primaryKey = $this->fieldset[$modelKey]['key']; |
| 735 | } |
| 736 | |
| 737 | if (isset($type)) { |
| 738 | $map = array( |
| 739 | 'string' => 'text', 'datetime' => 'datetime', |
| 740 | 'boolean' => 'checkbox', 'timestamp' => 'datetime', |
| 741 | 'text' => 'textarea', 'time' => 'time', |
| 742 | 'date' => 'date', 'float' => 'text' |
| 743 | ); |
| 744 | |
| 745 | if (isset($this->map[$type])) { |
| 746 | $options['type'] = $this->map[$type]; |
| 747 | } elseif (isset($map[$type])) { |
| 748 | $options['type'] = $map[$type]; |
| 749 | } |
| 750 | if ($fieldKey == $primaryKey) { |
| 751 | $options['type'] = 'hidden'; |
| 752 | } |
| 753 | } |
| 754 | if (preg_match('/_id$/', $fieldKey) && $options['type'] !== 'hidden') { |
| 755 | $options['type'] = 'select'; |
| 756 | } |
| 757 | |
| 758 | if ($modelKey === $fieldKey) { |
| 759 | $options['type'] = 'select'; |
| 760 | if (!isset($options['multiple'])) { |
| 761 | $options['multiple'] = 'multiple'; |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | $types = array('checkbox', 'radio', 'select'); |
| 766 | |
| 767 | if ( |
| 768 | (!isset($options['options']) && in_array($options['type'], $types)) || |
| 769 | (isset($magicType) && $options['type'] == 'text') |
| 770 | ) { |
| 771 | $view =& ClassRegistry::getObject('view'); |
| 772 | $varName = Inflector::variable( |
| 773 | Inflector::pluralize(preg_replace('/_id$/', '', $fieldKey)) |
| 774 | ); |
| 775 | $varOptions = $view->getVar($varName); |
| 776 | if (is_array($varOptions)) { |
| 777 | if ($options['type'] !== 'radio') { |
| 778 | $options['type'] = 'select'; |
| 779 | } |
| 780 | $options['options'] = $varOptions; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | $autoLength = (!array_key_exists('maxlength', $options) && isset($fieldDef['length'])); |
| 785 | if ($autoLength && $options['type'] == 'text') { |
| 786 | $options['maxlength'] = $fieldDef['length']; |
| 787 | } |
| 788 | if ($autoLength && $fieldDef['type'] == 'float') { |
| 789 | $options['maxlength'] = array_sum(explode(',', $fieldDef['length']))+1; |
| 790 | } |
| 791 | |
| 792 | $divOptions = array(); |
| 793 | $div = $this->_extractOption('div', $options, true); |
| 794 | unset($options['div']); |
| 795 | |
| 796 | if (!empty($div)) { |
| 797 | $divOptions['class'] = 'input'; |
| 798 | $divOptions = $this->addClass($divOptions, $options['type']); |
| 799 | if (is_string($div)) { |
| 800 | $divOptions['class'] = $div; |
| 801 | } elseif (is_array($div)) { |
| 802 | $divOptions = array_merge($divOptions, $div); |
| 803 | } |
| 804 | if ( |
| 805 | isset($this->fieldset[$modelKey]) && |
| 806 | in_array($fieldKey, $this->fieldset[$modelKey]['validates']) |
| 807 | ) { |
| 808 | $divOptions = $this->addClass($divOptions, 'required'); |
| 809 | } |
| 810 | if (!isset($divOptions['tag'])) { |
| 811 | $divOptions['tag'] = 'div'; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | $label = null; |
| 816 | if (isset($options['label']) && $options['type'] !== 'radio') { |
| 817 | $label = $options['label']; |
| 818 | unset($options['label']); |
| 819 | } |
| 820 | |
| 821 | if ($options['type'] === 'radio') { |
| 822 | $label = false; |
| 823 | if (isset($options['options'])) { |
| 824 | $radioOptions = (array)$options['options']; |
| 825 | unset($options['options']); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | if ($label !== false) { |
| 830 | $label = $this->_inputLabel($fieldName, $label, $options); |
| 831 | } |
| 832 | |
| 833 | $error = $this->_extractOption('error', $options, null); |
| 834 | unset($options['error']); |
| 835 | |
| 836 | $selected = $this->_extractOption('selected', $options, null); |
| 837 | unset($options['selected']); |
| 838 | |
| 839 | if (isset($options['rows']) || isset($options['cols'])) { |
| 840 | $options['type'] = 'textarea'; |
| 841 | } |
| 842 | |
| 843 | if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time' || $options['type'] === 'select') { |
| 844 | $options += array('empty' => false); |
| 845 | } |
| 846 | if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time') { |
| 847 | $dateFormat = $this->_extractOption('dateFormat', $options, 'MDY'); |
| 848 | $timeFormat = $this->_extractOption('timeFormat', $options, 12); |
| 849 | unset($options['dateFormat'], $options['timeFormat']); |
| 850 | } |
| 851 | |
| 852 | $type = $options['type']; |
| 853 | $out = array_merge( |
| 854 | array('before' => null, 'label' => null, 'between' => null, 'input' => null, 'after' => null, 'error' => null), |
| 855 | array('before' => $options['before'], 'label' => $label, 'between' => $options['between'], 'after' => $options['after']) |
| 856 | ); |
| 857 | $format = null; |
| 858 | if (is_array($options['format']) && in_array('input', $options['format'])) { |
| 859 | $format = $options['format']; |
| 860 | } |
| 861 | unset($options['type'], $options['before'], $options['between'], $options['after'], $options['format']); |
| 862 | |
| 863 | switch ($type) { |
| 864 | case 'hidden': |
| 865 | $input = $this->hidden($fieldName, $options); |
| 866 | $format = array('input'); |
| 867 | unset($divOptions); |
| 868 | break; |
| 869 | case 'checkbox': |
| 870 | $input = $this->checkbox($fieldName, $options); |
| 871 | $format = $format ? $format : array('before', 'input', 'between', 'label', 'after', 'error'); |
| 872 | break; |
| 873 | case 'radio': |
| 874 | $input = $this->radio($fieldName, $radioOptions, $options); |
| 875 | break; |
| 876 | case 'text': |
| 877 | case 'password': |
| 878 | case 'file': |
| 879 | $input = $this->{$type}($fieldName, $options); |
| 880 | break; |
| 881 | case 'select': |
| 882 | $options += array('options' => array()); |
| 883 | $list = $options['options']; |
| 884 | unset($options['options']); |
| 885 | $input = $this->select($fieldName, $list, $selected, $options); |
| 886 | break; |
| 887 | case 'time': |
| 888 | $input = $this->dateTime($fieldName, null, $timeFormat, $selected, $options); |
| 889 | break; |
| 890 | case 'date': |
| 891 | $input = $this->dateTime($fieldName, $dateFormat, null, $selected, $options); |
| 892 | break; |
| 893 | case 'datetime': |
| 894 | $input = $this->dateTime($fieldName, $dateFormat, $timeFormat, $selected, $options); |
| 895 | break; |
| 896 | case 'textarea': |
| 897 | default: |
| 898 | $input = $this->textarea($fieldName, $options + array('cols' => '30', 'rows' => '6')); |
| 899 | break; |
| 900 | } |
| 901 | |
| 902 | if ($type != 'hidden' && $error !== false) { |
| 903 | $errMsg = $this->error($fieldName, $error); |
| 904 | if ($errMsg) { |
| 905 | $divOptions = $this->addClass($divOptions, 'error'); |
| 906 | $out['error'] = $errMsg; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | $out['input'] = $input; |
| 911 | $format = $format ? $format : array('before', 'label', 'between', 'input', 'after', 'error'); |
| 912 | $output = ''; |
| 913 | foreach ($format as $element) { |
| 914 | $output .= $out[$element]; |
| 915 | unset($out[$element]); |
| 916 | } |
| 917 | |
| 918 | if (!empty($divOptions['tag'])) { |
| 919 | $tag = $divOptions['tag']; |
| 920 | unset($divOptions['tag']); |
| 921 | $output = $this->Html->tag($tag, $output, $divOptions); |
| 922 | } |
| 923 | return $output; |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * Extracts a single option from an options array. |
| 928 | * |
| 929 | * @param string $name The name of the option to pull out. |
| 930 | * @param array $options The array of options you want to extract. |
| 931 | * @param mixed $default The default option value |
| 932 | * @return the contents of the option or default |
| 933 | * @access protected |
| 934 | */ |
| 935 | function _extractOption($name, $options, $default = null) { |
| 936 | if (array_key_exists($name, $options)) { |
| 937 | return $options[$name]; |
| 938 | } |
| 939 | return $default; |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * Generate a label for an input() call. |
| 944 | * |
| 945 | * @param array $options Options for the label element. |
| 946 | * @return string Generated label element |
| 947 | * @access protected |
| 948 | */ |
| 949 | function _inputLabel($fieldName, $label, $options) { |
| 950 | $labelAttributes = $this->domId(array(), 'for'); |
| 951 | if ($options['type'] === 'date' || $options['type'] === 'datetime') { |
| 952 | if (isset($options['dateFormat']) && $options['dateFormat'] === 'NONE') { |
| 953 | $labelAttributes['for'] .= 'Hour'; |
| 954 | $idKey = 'hour'; |
| 955 | } else { |
| 956 | $labelAttributes['for'] .= 'Month'; |
| 957 | $idKey = 'month'; |
| 958 | } |
| 959 | if (isset($options['id']) && isset($options['id'][$idKey])) { |
| 960 | $labelAttributes['for'] = $options['id'][$idKey]; |
| 961 | } |
| 962 | } elseif ($options['type'] === 'time') { |
| 963 | $labelAttributes['for'] .= 'Hour'; |
| 964 | if (isset($options['id']) && isset($options['id']['hour'])) { |
| 965 | $labelAttributes['for'] = $options['id']['hour']; |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | if (is_array($label)) { |
| 970 | $labelText = null; |
| 971 | if (isset($label['text'])) { |
| 972 | $labelText = $label['text']; |
| 973 | unset($label['text']); |
| 974 | } |
| 975 | $labelAttributes = array_merge($labelAttributes, $label); |
| 976 | } else { |
| 977 | $labelText = $label; |
| 978 | } |
| 979 | |
| 980 | if (isset($options['id']) && is_string($options['id'])) { |
| 981 | $labelAttributes = array_merge($labelAttributes, array('for' => $options['id'])); |
| 982 | } |
| 983 | return $this->label($fieldName, $labelText, $labelAttributes); |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * Creates a checkbox input widget. |
| 988 | * |
| 989 | * ### Options: |
| 990 | * |
| 991 | * - `value` - the value of the checkbox |
| 992 | * - `checked` - boolean indicate that this checkbox is checked. |
| 993 | * - `hiddenField` - boolean to indicate if you want the results of checkbox() to include |
| 994 | * a hidden input with a value of ''. |
| 995 | * - `disabled` - create a disabled input. |
| 996 | * |
| 997 | * @param string $fieldName Name of a field, like this "Modelname.fieldname" |
| 998 | * @param array $options Array of HTML attributes. |
| 999 | * @return string An HTML text input element. |
| 1000 | * @access public |
| 1001 | * @link http://book.cakephp.org/view/1414/checkbox |
| 1002 | */ |
| 1003 | function checkbox($fieldName, $options = array()) { |
| 1004 | $options = $this->_initInputField($fieldName, $options) + array('hiddenField' => true); |
| 1005 | $value = current($this->value()); |
| 1006 | $output = ""; |
| 1007 | |
| 1008 | if (empty($options['value'])) { |
| 1009 | $options['value'] = 1; |
| 1010 | } elseif ( |
| 1011 | (!isset($options['checked']) && !empty($value) && $value === $options['value']) || |
| 1012 | !empty($options['checked']) |
| 1013 | ) { |
| 1014 | $options['checked'] = 'checked'; |
| 1015 | } |
| 1016 | if ($options['hiddenField']) { |
| 1017 | $hiddenOptions = array( |
| 1018 | 'id' => $options['id'] . '_', 'name' => $options['name'], |
| 1019 | 'value' => '0', 'secure' => false |
| 1020 | ); |
| 1021 | if (isset($options['disabled']) && $options['disabled'] == true) { |
| 1022 | $hiddenOptions['disabled'] = 'disabled'; |
| 1023 | } |
| 1024 | $output = $this->hidden($fieldName, $hiddenOptions); |
| 1025 | } |
| 1026 | unset($options['hiddenField']); |
| 1027 | |
| 1028 | return $output . sprintf( |
| 1029 | $this->Html->tags['checkbox'], |
| 1030 | $options['name'], |
| 1031 | $this->_parseAttributes($options, array('name'), null, ' ') |
| 1032 | ); |
| 1033 | } |
| 1034 | |
| 1035 | /** |
| 1036 | * Creates a set of radio widgets. Will create a legend and fieldset |
| 1037 | * by default. Use $options to control this |
| 1038 | * |
| 1039 | * ### Attributes: |
| 1040 | * |
| 1041 | * - `separator` - define the string in between the radio buttons |
| 1042 | * - `legend` - control whether or not the widget set has a fieldset & legend |
| 1043 | * - `value` - indicate a value that is should be checked |
| 1044 | * - `label` - boolean to indicate whether or not labels for widgets show be displayed |
| 1045 | * - `hiddenField` - boolean to indicate if you want the results of radio() to include |
| 1046 | * a hidden input with a value of ''. This is useful for creating radio sets that non-continuous |
| 1047 | * |
| 1048 | * @param string $fieldName Name of a field, like this "Modelname.fieldname" |
| 1049 | * @param array $options Radio button options array. |
| 1050 | * @param array $attributes Array of HTML attributes, and special attributes above. |
| 1051 | * @return string Completed radio widget set. |
| 1052 | * @access public |
| 1053 | * @link http://book.cakephp.org/view/1429/radio |
| 1054 | */ |
| 1055 | function radio($fieldName, $options = array(), $attributes = array()) { |
| 1056 | $attributes = $this->_initInputField($fieldName, $attributes); |
| 1057 | $legend = false; |
| 1058 | |
| 1059 | if (isset($attributes['legend'])) { |
| 1060 | $legend = $attributes['legend']; |
| 1061 | unset($attributes['legend']); |
| 1062 | } elseif (count($options) > 1) { |
| 1063 | $legend = __(Inflector::humanize($this->field()), true); |
| 1064 | } |
| 1065 | $label = true; |
| 1066 | |
| 1067 | if (isset($attributes['label'])) { |
| 1068 | $label = $attributes['label']; |
| 1069 | unset($attributes['label']); |
| 1070 | } |
| 1071 | $inbetween = null; |
| 1072 | |
| 1073 | if (isset($attributes['separator'])) { |
| 1074 | $inbetween = $attributes['separator']; |
| 1075 | unset($attributes['separator']); |
| 1076 | } |
| 1077 | |
| 1078 | if (isset($attributes['value'])) { |
| 1079 | $value = $attributes['value']; |
| 1080 | } else { |
| 1081 | $value = $this->value($fieldName); |
| 1082 | } |
| 1083 | $out = array(); |
| 1084 | |
| 1085 | $hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true; |
| 1086 | unset($attributes['hiddenField']); |
| 1087 | |
| 1088 | foreach ($options as $optValue => $optTitle) { |
| 1089 | $optionsHere = array('value' => $optValue); |
| 1090 | |
| 1091 | if (isset($value) && $optValue == $value) { |
| 1092 | $optionsHere['checked'] = 'checked'; |
| 1093 | } |
| 1094 | $parsedOptions = $this->_parseAttributes( |
| 1095 | array_merge($attributes, $optionsHere), |
| 1096 | array('name', 'type', 'id'), '', ' ' |
| 1097 | ); |
| 1098 | $tagName = Inflector::camelize( |
| 1099 | $attributes['id'] . '_' . Inflector::slug($optValue) |
| 1100 | ); |
| 1101 | |
| 1102 | if ($label) { |
| 1103 | $optTitle = sprintf($this->Html->tags['label'], $tagName, null, $optTitle); |
| 1104 | } |
| 1105 | $out[] = sprintf( |
| 1106 | $this->Html->tags['radio'], $attributes['name'], |
| 1107 | $tagName, $parsedOptions, $optTitle |
| 1108 | ); |
| 1109 | } |
| 1110 | $hidden = null; |
| 1111 | |
| 1112 | if ($hiddenField) { |
| 1113 | if (!isset($value) || $value === '') { |
| 1114 | $hidden = $this->hidden($fieldName, array( |
| 1115 | 'id' => $attributes['id'] . '_', 'value' => '', 'name' => $attributes['name'] |
| 1116 | )); |
| 1117 | } |
| 1118 | } |
| 1119 | $out = $hidden . implode($inbetween, $out); |
| 1120 | |
| 1121 | if ($legend) { |
| 1122 | $out = sprintf( |
| 1123 | $this->Html->tags['fieldset'], '', |
| 1124 | sprintf($this->Html->tags['legend'], $legend) . $out |
| 1125 | ); |
| 1126 | } |
| 1127 | return $out; |
| 1128 | } |
| 1129 | |
| 1130 | /** |
| 1131 | * Creates a text input widget. |
| 1132 | * |
| 1133 | * @param string $fieldName Name of a field, in the form "Modelname.fieldname" |
| 1134 | * @param array $options Array of HTML attributes. |
| 1135 | * @return string A generated HTML text input element |
| 1136 | * @access public |
| 1137 | * @link http://book.cakephp.org/view/1432/text |
| 1138 | */ |
| 1139 | function text($fieldName, $options = array()) { |
| 1140 | $options = $this->_initInputField($fieldName, array_merge( |
| 1141 | array('type' => 'text'), $options |
| 1142 | )); |
| 1143 | return sprintf( |
| 1144 | $this->Html->tags['input'], |
| 1145 | $options['name'], |
| 1146 | $this->_parseAttributes($options, array('name'), null, ' ') |
| 1147 | ); |
| 1148 | } |
| 1149 | |
| 1150 | /** |
| 1151 | * Creates a password input widget. |
| 1152 | * |
| 1153 | * @param string $fieldName Name of a field, like in the form "Modelname.fieldname" |
| 1154 | * @param array $options Array of HTML attributes. |
| 1155 | * @return string A generated password input. |
| 1156 | * @access public |
| 1157 | * @link http://book.cakephp.org/view/1428/password |
| 1158 | */ |
| 1159 | function password($fieldName, $options = array()) { |
| 1160 | $options = $this->_initInputField($fieldName, $options); |
| 1161 | return sprintf( |
| 1162 | $this->Html->tags['password'], |
| 1163 | $options['name'], |
| 1164 | $this->_parseAttributes($options, array('name'), null, ' ') |
| 1165 | ); |
| 1166 | } |
| 1167 | |
| 1168 | /** |
| 1169 | * Creates a textarea widget. |
| 1170 | * |
| 1171 | * ### Options: |
| 1172 | * |
| 1173 | * - `escape` - Whether or not the contents of the textarea should be escaped. Defaults to true. |
| 1174 | * |
| 1175 | * @param string $fieldName Name of a field, in the form "Modelname.fieldname" |
| 1176 | * @param array $options Array of HTML attributes, and special options above. |
| 1177 | * @return string A generated HTML text input element |
| 1178 | * @access public |
| 1179 | * @link http://book.cakephp.org/view/1433/textarea |
| 1180 | */ |
| 1181 | function textarea($fieldName, $options = array()) { |
| 1182 | $options = $this->_initInputField($fieldName, $options); |
| 1183 | $value = null; |
| 1184 | |
| 1185 | if (array_key_exists('value', $options)) { |
| 1186 | $value = $options['value']; |
| 1187 | if (!array_key_exists('escape', $options) || $options['escape'] !== false) { |
| 1188 | $value = h($value); |
| 1189 | } |
| 1190 | unset($options['value']); |
| 1191 | } |
| 1192 | return sprintf( |
| 1193 | $this->Html->tags['textarea'], |
| 1194 | $options['name'], |
| 1195 | $this->_parseAttributes($options, array('type', 'name'), null, ' '), |
| 1196 | $value |
| 1197 | ); |
| 1198 | } |
| 1199 | |
| 1200 | /** |
| 1201 | * Creates a hidden input field. |
| 1202 | * |
| 1203 | * @param string $fieldName Name of a field, in the form of "Modelname.fieldname" |
| 1204 | * @param array $options Array of HTML attributes. |
| 1205 | * @return string A generated hidden input |
| 1206 | * @access public |
| 1207 | * @link http://book.cakephp.org/view/1425/hidden |
| 1208 | */ |
| 1209 | function hidden($fieldName, $options = array()) { |
| 1210 | $secure = true; |
| 1211 | |
| 1212 | if (isset($options['secure'])) { |
| 1213 | $secure = $options['secure']; |
| 1214 | unset($options['secure']); |
| 1215 | } |
| 1216 | $options = $this->_initInputField($fieldName, array_merge( |
| 1217 | $options, array('secure' => false) |
| 1218 | )); |
| 1219 | $model = $this->model(); |
| 1220 | |
| 1221 | if ($fieldName !== '_method' && $model !== '_Token' && $secure) { |
| 1222 | $this->__secure(null, '' . $options['value']); |
| 1223 | } |
| 1224 | |
| 1225 | return sprintf( |
| 1226 | $this->Html->tags['hidden'], |
| 1227 | $options['name'], |
| 1228 | $this->_parseAttributes($options, array('name', 'class'), '', ' ') |
| 1229 | ); |
| 1230 | } |
| 1231 | |
| 1232 | /** |
| 1233 | * Creates file input widget. |
| 1234 | * |
| 1235 | * @param string $fieldName Name of a field, in the form "Modelname.fieldname" |
| 1236 | * @param array $options Array of HTML attributes. |
| 1237 | * @return string A generated file input. |
| 1238 | * @access public |
| 1239 | * @link http://book.cakephp.org/view/1424/file |
| 1240 | */ |
| 1241 | function file($fieldName, $options = array()) { |
| 1242 | $options = array_merge($options, array('secure' => false)); |
| 1243 | $options = $this->_initInputField($fieldName, $options); |
| 1244 | $view =& ClassRegistry::getObject('view'); |
| 1245 | $field = $view->entity(); |
| 1246 | |
| 1247 | foreach (array('name', 'type', 'tmp_name', 'error', 'size') as $suffix) { |
| 1248 | $this->__secure(array_merge($field, array($suffix))); |
| 1249 | } |
| 1250 | |
| 1251 | $attributes = $this->_parseAttributes($options, array('name'), '', ' '); |
| 1252 | return sprintf($this->Html->tags['file'], $options['name'], $attributes); |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * Creates a `<button>` tag. The type attribute defaults to `type="submit"` |
| 1257 | * You can change it to a different value by using `$options['type']`. |
| 1258 | * |
| 1259 | * ### Options: |
| 1260 | * |
| 1261 | * - `escape` - HTML entity encode the $title of the button. Defaults to false. |
| 1262 | * |
| 1263 | * @param string $title The button's caption. Not automatically HTML encoded |
| 1264 | * @param array $options Array of options and HTML attributes. |
| 1265 | * @return string A HTML button tag. |
| 1266 | * @access public |
| 1267 | * @link http://book.cakephp.org/view/1415/button |
| 1268 | */ |
| 1269 | function button($title, $options = array()) { |
| 1270 | $options += array('type' => 'submit', 'escape' => false); |
| 1271 | if ($options['escape']) { |
| 1272 | $title = h($title); |
| 1273 | } |
| 1274 | return sprintf( |
| 1275 | $this->Html->tags['button'], |
| 1276 | $options['type'], |
| 1277 | $this->_parseAttributes($options, array('type'), ' ', ''), |
| 1278 | $title |
| 1279 | ); |
| 1280 | } |
| 1281 | |
| 1282 | /** |
| 1283 | * Creates a submit button element. This method will generate `<input />` elements that |
| 1284 | * can be used to submit, and reset forms by using $options. image submits can be created by supplying an |
| 1285 | * image path for $caption. |
| 1286 | * |
| 1287 | * ### Options |
| 1288 | * |
| 1289 | * - `div` - Include a wrapping div? Defaults to true. Accepts sub options similar to |
| 1290 | * FormHelper::input(). |
| 1291 | * - `before` - Content to include before the input. |
| 1292 | * - `after` - Content to include after the input. |
| 1293 | * - `type` - Set to 'reset' for reset inputs. Defaults to 'submit' |
| 1294 | * - Other attributes will be assigned to the input element. |
| 1295 | * |
| 1296 | * ### Options |
| 1297 | * |
| 1298 | * - `div` - Include a wrapping div? Defaults to true. Accepts sub options similar to |
| 1299 | * FormHelper::input(). |
| 1300 | * - Other attributes will be assigned to the input element. |
| 1301 | * |
| 1302 | * @param string $caption The label appearing on the button OR if string contains :// or the |
| 1303 | * extension .jpg, .jpe, .jpeg, .gif, .png use an image if the extension |
| 1304 | * exists, AND the first character is /, image is relative to webroot, |
| 1305 | * OR if the first character is not /, image is relative to webroot/img. |
| 1306 | * @param array $options Array of options. See above. |
| 1307 | * @return string A HTML submit button |
| 1308 | * @access public |
| 1309 | * @link http://book.cakephp.org/view/1431/submit |
| 1310 | */ |
| 1311 | function submit($caption = null, $options = array()) { |
| 1312 | if (!$caption) { |
| 1313 | $caption = __('Submit', true); |
| 1314 | } |
| 1315 | $out = null; |
| 1316 | $div = true; |
| 1317 | |
| 1318 | if (isset($options['div'])) { |
| 1319 | $div = $options['div']; |
| 1320 | unset($options['div']); |
| 1321 | } |
| 1322 | $options += array('type' => 'submit', 'before' => null, 'after' => null); |
| 1323 | $divOptions = array('tag' => 'div'); |
| 1324 | |
| 1325 | if ($div === true) { |
| 1326 | $divOptions['class'] = 'submit'; |
| 1327 | } elseif ($div === false) { |
| 1328 | unset($divOptions); |
| 1329 | } elseif (is_string($div)) { |
| 1330 | $divOptions['class'] = $div; |
| 1331 | } elseif (is_array($div)) { |
| 1332 | $divOptions = array_merge(array('class' => 'submit', 'tag' => 'div'), $div); |
| 1333 | } |
| 1334 | |
| 1335 | $before = $options['before']; |
| 1336 | $after = $options['after']; |
| 1337 | unset($options['before'], $options['after']); |
| 1338 | |
| 1339 | if (strpos($caption, '://') !== false) { |
| 1340 | unset($options['type']); |
| 1341 | $out .= $before . sprintf( |
| 1342 | $this->Html->tags['submitimage'], |
| 1343 | $caption, |
| 1344 | $this->_parseAttributes($options, null, '', ' ') |
| 1345 | ) . $after; |
| 1346 | } elseif (preg_match('/\.(jpg|jpe|jpeg|gif|png|ico)$/', $caption)) { |
| 1347 | unset($options['type']); |
| 1348 | if ($caption{0} !== '/') { |
| 1349 | $url = $this->webroot(IMAGES_URL . $caption); |
| 1350 | } else { |
| 1351 | $caption = trim($caption, '/'); |
| 1352 | $url = $this->webroot($caption); |
| 1353 | } |
| 1354 | $out .= $before . sprintf( |
| 1355 | $this->Html->tags['submitimage'], |
| 1356 | $url, |
| 1357 | $this->_parseAttributes($options, null, '', ' ') |
| 1358 | ) . $after; |
| 1359 | } else { |
| 1360 | $options['value'] = $caption; |
| 1361 | $out .= $before . sprintf( |
| 1362 | $this->Html->tags['submit'], |
| 1363 | $this->_parseAttributes($options, null, '', ' ') |
| 1364 | ). $after; |
| 1365 | } |
| 1366 | |
| 1367 | if (isset($divOptions)) { |
| 1368 | $tag = $divOptions['tag']; |
| 1369 | unset($divOptions['tag']); |
| 1370 | $out = $this->Html->tag($tag, $out, $divOptions); |
| 1371 | } |
| 1372 | return $out; |
| 1373 | } |
| 1374 | |
| 1375 | /** |
| 1376 | * Returns a formatted SELECT element. |
| 1377 | * |
| 1378 | * ### Attributes: |
| 1379 | * |
| 1380 | * - `showParents` - If included in the array and set to true, an additional option element |
| 1381 | * will be added for the parent of each option group. You can set an option with the same name |
| 1382 | * and it's key will be used for the value of the option. |
| 1383 | * - `multiple` - show a multiple select box. If set to 'checkbox' multiple checkboxes will be |
| 1384 | * created instead. |
| 1385 | * - `empty` - If true, the empty select option is shown. If a string, |
| 1386 | * that string is displayed as the empty element. |
| 1387 | * - `escape` - If true contents of options will be HTML entity encoded. Defaults to true. |
| 1388 | * - `class` - When using multiple = checkbox the classname to apply to the divs. Defaults to 'checkbox'. |
| 1389 | * |
| 1390 | * ### Using options |
| 1391 | * |
| 1392 | * A simple array will create normal options: |
| 1393 | * |
| 1394 | * {{{ |
| 1395 | * $options = array(1 => 'one', 2 => 'two); |
| 1396 | * $this->Form->select('Model.field', $options)); |
| 1397 | * }}} |
| 1398 | * |
| 1399 | * While a nested options array will create optgroups with options inside them. |
| 1400 | * {{{ |
| 1401 | * $options = array( |
| 1402 | * 1 => 'bill', |
| 1403 | * 'fred' => array( |
| 1404 | * 2 => 'fred', |
| 1405 | * 3 => 'fred jr.' |
| 1406 | * ) |
| 1407 | * ); |
| 1408 | * $this->Form->select('Model.field', $options); |
| 1409 | * }}} |
| 1410 | * |
| 1411 | * In the above `2 => 'fred'` will not generate an option element. You should enable the `showParents` |
| 1412 | * attribute to show the fred option. |
| 1413 | * |
| 1414 | * @param string $fieldName Name attribute of the SELECT |
| 1415 | * @param array $options Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the |
| 1416 | * SELECT element |
| 1417 | * @param mixed $selected The option selected by default. If null, the default value |
| 1418 | * from POST data will be used when available. |
| 1419 | * @param array $attributes The HTML attributes of the select element. |
| 1420 | * @return string Formatted SELECT element |
| 1421 | * @access public |
| 1422 | * @link http://book.cakephp.org/view/1430/select |
| 1423 | */ |
| 1424 | function select($fieldName, $options = array(), $selected = null, $attributes = array()) { |
| 1425 | $select = array(); |
| 1426 | $style = null; |
| 1427 | $tag = null; |
| 1428 | $attributes += array( |
| 1429 | 'class' => null, |
| 1430 | 'escape' => true, |
| 1431 | 'secure' => null, |
| 1432 | 'empty' => '', |
| 1433 | 'showParents' => false |
| 1434 | ); |
| 1435 | |
| 1436 | $escapeOptions = $this->_extractOption('escape', $attributes); |
| 1437 | $secure = $this->_extractOption('secure', $attributes); |
| 1438 | $showEmpty = $this->_extractOption('empty', $attributes); |
| 1439 | $showParents = $this->_extractOption('showParents', $attributes); |
| 1440 | unset($attributes['escape'], $attributes['secure'], $attributes['empty'], $attributes['showParents']); |
| 1441 | |
| 1442 | $attributes = $this->_initInputField($fieldName, array_merge( |
| 1443 | (array)$attributes, array('secure' => false) |
| 1444 | )); |
| 1445 | |
| 1446 | if (is_string($options) && isset($this->__options[$options])) { |
| 1447 | $options = $this->__generateOptions($options); |
| 1448 | } elseif (!is_array($options)) { |
| 1449 | $options = array(); |
| 1450 | } |
| 1451 | if (isset($attributes['type'])) { |
| 1452 | unset($attributes['type']); |
| 1453 | } |
| 1454 | |
| 1455 | if (!isset($selected)) { |
| 1456 | $selected = $attributes['value']; |
| 1457 | } |
| 1458 | |
| 1459 | if (isset($attributes) && array_key_exists('multiple', $attributes)) { |
| 1460 | $style = ($attributes['multiple'] === 'checkbox') ? 'checkbox' : null; |
| 1461 | $template = ($style) ? 'checkboxmultiplestart' : 'selectmultiplestart'; |
| 1462 | $tag = $this->Html->tags[$template]; |
| 1463 | $hiddenAttributes = array( |
| 1464 | 'value' => '', |
| 1465 | 'id' => $attributes['id'] . ($style ? '' : '_'), |
| 1466 | 'secure' => false, |
| 1467 | 'name' => $attributes['name'] |
| 1468 | ); |
| 1469 | $select[] = $this->hidden(null, $hiddenAttributes); |
| 1470 | } else { |
| 1471 | $tag = $this->Html->tags['selectstart']; |
| 1472 | } |
| 1473 | |
| 1474 | if (!empty($tag) || isset($template)) { |
| 1475 | if (!isset($secure) || $secure == true) { |
| 1476 | $this->__secure(); |
| 1477 | } |
| 1478 | $select[] = sprintf($tag, $attributes['name'], $this->_parseAttributes( |
| 1479 | $attributes, array('name', 'value')) |
| 1480 | ); |
| 1481 | } |
| 1482 | $emptyMulti = ( |
| 1483 | $showEmpty !== null && $showEmpty !== false && !( |
| 1484 | empty($showEmpty) && (isset($attributes) && |
| 1485 | array_key_exists('multiple', $attributes)) |
| 1486 | ) |
| 1487 | ); |
| 1488 | |
| 1489 | if ($emptyMulti) { |
| 1490 | $showEmpty = ($showEmpty === true) ? '' : $showEmpty; |
| 1491 | $options = array_reverse($options, true); |
| 1492 | $options[''] = $showEmpty; |
| 1493 | $options = array_reverse($options, true); |
| 1494 | } |
| 1495 | |
| 1496 | $select = array_merge($select, $this->__selectOptions( |
| 1497 | array_reverse($options, true), |
| 1498 | $selected, |
| 1499 | array(), |
| 1500 | $showParents, |
| 1501 | array('escape' => $escapeOptions, 'style' => $style, 'name' => $attributes['name'], 'class' => $attributes['class']) |
| 1502 | )); |
| 1503 | |
| 1504 | $template = ($style == 'checkbox') ? 'checkboxmultipleend' : 'selectend'; |
| 1505 | $select[] = $this->Html->tags[$template]; |
| 1506 | return implode("\n", $select); |
| 1507 | } |
| 1508 | |
| 1509 | /** |
| 1510 | * Returns a SELECT element for days. |
| 1511 | * |
| 1512 | * ### Attributes: |
| 1513 | * |
| 1514 | * - `empty` - If true, the empty select option is shown. If a string, |
| 1515 | * that string is displayed as the empty element. |
| 1516 | * |
| 1517 | * @param string $fieldName Prefix name for the SELECT element |
| 1518 | * @param string $selected Option which is selected. |
| 1519 | * @param array $attributes HTML attributes for the select element |
| 1520 | * @return string A generated day select box. |
| 1521 | * @access public |
| 1522 | * @link http://book.cakephp.org/view/1419/day |
| 1523 | */ |
| 1524 | function day($fieldName, $selected = null, $attributes = array()) { |
| 1525 | $attributes += array('empty' => true); |
| 1526 | $selected = $this->__dateTimeSelected('day', $fieldName, $selected, $attributes); |
| 1527 | |
| 1528 | if (strlen($selected) > 2) { |
| 1529 | $selected = date('d', strtotime($selected)); |
| 1530 | } elseif ($selected === false) { |
| 1531 | $selected = null; |
| 1532 | } |
| 1533 | return $this->select($fieldName . ".day", $this->__generateOptions('day'), $selected, $attributes); |
| 1534 | } |
| 1535 | |
| 1536 | /** |
| 1537 | * Returns a SELECT element for years |
| 1538 | * |
| 1539 | * ### Attributes: |
| 1540 | * |
| 1541 | * - `empty` - If true, the empty select option is shown. If a string, |
| 1542 | * that string is displayed as the empty element. |
| 1543 | * - `orderYear` - Ordering of year values in select options. |
| 1544 | * Possible values 'asc', 'desc'. Default 'desc' |
| 1545 | * |
| 1546 | * @param string $fieldName Prefix name for the SELECT element |
| 1547 | * @param integer $minYear First year in sequence |
| 1548 | * @param integer $maxYear Last year in sequence |
| 1549 | * @param string $selected Option which is selected. |
| 1550 | * @param array $attributes Attribute array for the select elements. |
| 1551 | * @return string Completed year select input |
| 1552 | * @access public |
| 1553 | * @link http://book.cakephp.org/view/1416/year |
| 1554 | */ |
| 1555 | function year($fieldName, $minYear = null, $maxYear = null, $selected = null, $attributes = array()) { |
| 1556 | $attributes += array('empty' => true); |
| 1557 | if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) { |
| 1558 | if (is_array($value)) { |
| 1559 | extract($value); |
| 1560 | $selected = $year; |
| 1561 | } else { |
| 1562 | if (empty($value)) { |
| 1563 | if (!$attributes['empty'] && !$maxYear) { |
| 1564 | $selected = 'now'; |
| 1565 | |
| 1566 | } elseif (!$attributes['empty'] && $maxYear && !$selected) { |
| 1567 | $selected = $maxYear; |
| 1568 | } |
| 1569 | } else { |
| 1570 | $selected = $value; |
| 1571 | } |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | if (strlen($selected) > 4 || $selected === 'now') { |
| 1576 | $selected = date('Y', strtotime($selected)); |
| 1577 | } elseif ($selected === false) { |
| 1578 | $selected = null; |
| 1579 | } |
| 1580 | $yearOptions = array('min' => $minYear, 'max' => $maxYear, 'order' => 'desc'); |
| 1581 | if (isset($attributes['orderYear'])) { |
| 1582 | $yearOptions['order'] = $attributes['orderYear']; |
| 1583 | unset($attributes['orderYear']); |
| 1584 | } |
| 1585 | return $this->select( |
| 1586 | $fieldName . '.year', $this->__generateOptions('year', $yearOptions), |
| 1587 | $selected, $attributes |
| 1588 | ); |
| 1589 | } |
| 1590 | |
| 1591 | /** |
| 1592 | * Returns a SELECT element for months. |
| 1593 | * |
| 1594 | * ### Attributes: |
| 1595 | * |
| 1596 | * - `monthNames` - If false, 2 digit numbers will be used instead of text. |
| 1597 | * If a array, the given array will be used. |
| 1598 | * - `empty` - If true, the empty select option is shown. If a string, |
| 1599 | * that string is displayed as the empty element. |
| 1600 | * |
| 1601 | * @param string $fieldName Prefix name for the SELECT element |
| 1602 | * @param string $selected Option which is selected. |
| 1603 | * @param array $attributes Attributes for the select element |
| 1604 | * @return string A generated month select dropdown. |
| 1605 | * @access public |
| 1606 | * @link http://book.cakephp.org/view/1417/month |
| 1607 | */ |
| 1608 | function month($fieldName, $selected = null, $attributes = array()) { |
| 1609 | $attributes += array('empty' => true); |
| 1610 | $selected = $this->__dateTimeSelected('month', $fieldName, $selected, $attributes); |
| 1611 | |
| 1612 | if (strlen($selected) > 2) { |
| 1613 | $selected = date('m', strtotime($selected)); |
| 1614 | } elseif ($selected === false) { |
| 1615 | $selected = null; |
| 1616 | } |
| 1617 | $defaults = array('monthNames' => true); |
| 1618 | $attributes = array_merge($defaults, (array) $attributes); |
| 1619 | $monthNames = $attributes['monthNames']; |
| 1620 | unset($attributes['monthNames']); |
| 1621 | |
| 1622 | return $this->select( |
| 1623 | $fieldName . ".month", |
| 1624 | $this->__generateOptions('month', array('monthNames' => $monthNames)), |
| 1625 | $selected, $attributes |
| 1626 | ); |
| 1627 | } |
| 1628 | |
| 1629 | /** |
| 1630 | * Returns a SELECT element for hours. |
| 1631 | * |
| 1632 | * ### Attributes: |
| 1633 | * |
| 1634 | * - `empty` - If true, the empty select option is shown. If a string, |
| 1635 | * that string is displayed as the empty element. |
| 1636 | * |
| 1637 | * @param string $fieldName Prefix name for the SELECT element |
| 1638 | * @param boolean $format24Hours True for 24 hours format |
| 1639 | * @param string $selected Option which is selected. |
| 1640 | * @param array $attributes List of HTML attributes |
| 1641 | * @return string Completed hour select input |
| 1642 | * @access public |
| 1643 | * @link http://book.cakephp.org/view/1420/hour |
| 1644 | */ |
| 1645 | function hour($fieldName, $format24Hours = false, $selected = null, $attributes = array()) { |
| 1646 | $attributes += array('empty' => true); |
| 1647 | $selected = $this->__dateTimeSelected('hour', $fieldName, $selected, $attributes); |
| 1648 | |
| 1649 | if (strlen($selected) > 2) { |
| 1650 | if ($format24Hours) { |
| 1651 | $selected = date('H', strtotime($selected)); |
| 1652 | } else { |
| 1653 | $selected = date('g', strtotime($selected)); |
| 1654 | } |
| 1655 | } elseif ($selected === false) { |
| 1656 | $selected = null; |
| 1657 | } |
| 1658 | return $this->select( |
| 1659 | $fieldName . ".hour", |
| 1660 | $this->__generateOptions($format24Hours ? 'hour24' : 'hour'), |
| 1661 | $selected, $attributes |
| 1662 | ); |
| 1663 | } |
| 1664 | |
| 1665 | /** |
| 1666 | * Returns a SELECT element for minutes. |
| 1667 | * |
| 1668 | * ### Attributes: |
| 1669 | * |
| 1670 | * - `empty` - If true, the empty select option is shown. If a string, |
| 1671 | * that string is displayed as the empty element. |
| 1672 | * |
| 1673 | * @param string $fieldName Prefix name for the SELECT element |
| 1674 | * @param string $selected Option which is selected. |
| 1675 | * @param string $attributes Array of Attributes |
| 1676 | * @return string Completed minute select input. |
| 1677 | * @access public |
| 1678 | * @link http://book.cakephp.org/view/1421/minute |
| 1679 | */ |
| 1680 | function minute($fieldName, $selected = null, $attributes = array()) { |
| 1681 | $attributes += array('empty' => true); |
| 1682 | $selected = $this->__dateTimeSelected('min', $fieldName, $selected, $attributes); |
| 1683 | |
| 1684 | if (strlen($selected) > 2) { |
| 1685 | $selected = date('i', strtotime($selected)); |
| 1686 | } elseif ($selected === false) { |
| 1687 | $selected = null; |
| 1688 | } |
| 1689 | $minuteOptions = array(); |
| 1690 | |
| 1691 | if (isset($attributes['interval'])) { |
| 1692 | $minuteOptions['interval'] = $attributes['interval']; |
| 1693 | unset($attributes['interval']); |
| 1694 | } |
| 1695 | return $this->select( |
| 1696 | $fieldName . ".min", $this->__generateOptions('minute', $minuteOptions), |
| 1697 | $selected, $attributes |
| 1698 | ); |
| 1699 | } |
| 1700 | |
| 1701 | /** |
| 1702 | * Selects values for dateTime selects. |
| 1703 | * |
| 1704 | * @param string $select Name of element field. ex. 'day' |
| 1705 | * @param string $fieldName Name of fieldName being generated ex. Model.created |
| 1706 | * @param mixed $selected The current selected value. |
| 1707 | * @param array $attributes Array of attributes, must contain 'empty' key. |
| 1708 | * @return string Currently selected value. |
| 1709 | * @access private |
| 1710 | */ |
| 1711 | function __dateTimeSelected($select, $fieldName, $selected, $attributes) { |
| 1712 | if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) { |
| 1713 | if (is_array($value) && isset($value[$select])) { |
| 1714 | $selected = $value[$select]; |
| 1715 | } else { |
| 1716 | if (empty($value)) { |
| 1717 | if (!$attributes['empty']) { |
| 1718 | $selected = 'now'; |
| 1719 | } |
| 1720 | } else { |
| 1721 | $selected = $value; |
| 1722 | } |
| 1723 | } |
| 1724 | } |
| 1725 | return $selected; |
| 1726 | } |
| 1727 | |
| 1728 | /** |
| 1729 | * Returns a SELECT element for AM or PM. |
| 1730 | * |
| 1731 | * ### Attributes: |
| 1732 | * |
| 1733 | * - `empty` - If true, the empty select option is shown. If a string, |
| 1734 | * that string is displayed as the empty element. |
| 1735 | * |
| 1736 | * @param string $fieldName Prefix name for the SELECT element |
| 1737 | * @param string $selected Option which is selected. |
| 1738 | * @param string $attributes Array of Attributes |
| 1739 | * @param bool $showEmpty Show/Hide an empty option |
| 1740 | * @return string Completed meridian select input |
| 1741 | * @access public |
| 1742 | * @link http://book.cakephp.org/view/1422/meridian |
| 1743 | */ |
| 1744 | function meridian($fieldName, $selected = null, $attributes = array()) { |
| 1745 | $attributes += array('empty' => true); |
| 1746 | if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) { |
| 1747 | if (is_array($value)) { |
| 1748 | extract($value); |
| 1749 | $selected = $meridian; |
| 1750 | } else { |
| 1751 | if (empty($value)) { |
| 1752 | if (!$attribues['empty']) { |
| 1753 | $selected = date('a'); |
| 1754 | } |
| 1755 | } else { |
| 1756 | $selected = date('a', strtotime($value)); |
| 1757 | } |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | if ($selected === false) { |
| 1762 | $selected = null; |
| 1763 | } |
| 1764 | return $this->select( |
| 1765 | $fieldName . ".meridian", $this->__generateOptions('meridian'), |
| 1766 | $selected, $attributes |
| 1767 | ); |
| 1768 | } |
| 1769 | |
| 1770 | /** |
| 1771 | * Returns a set of SELECT elements for a full datetime setup: day, month and year, and then time. |
| 1772 | * |
| 1773 | * ### Attributes: |
| 1774 | * |
| 1775 | * - `monthNames` If false, 2 digit numbers will be used instead of text. |
| 1776 | * If a array, the given array will be used. |
| 1777 | * - `minYear` The lowest year to use in the year select |
| 1778 | * - `maxYear` The maximum year to use in the year select |
| 1779 | * - `interval` The interval for the minutes select. Defaults to 1 |
| 1780 | * - `separator` The contents of the string between select elements. Defaults to '-' |
| 1781 | * - `empty` - If true, the empty select option is shown. If a string, |
| 1782 | * that string is displayed as the empty element. |
| 1783 | * - `value` | `default` The default value to be used by the input. A value in `$this->data` |
| 1784 | * matching the field name will override this value. If no default is provided `time()` will be used. |
| 1785 | * |
| 1786 | * @param string $fieldName Prefix name for the SELECT element |
| 1787 | * @param string $dateFormat DMY, MDY, YMD. |
| 1788 | * @param string $timeFormat 12, 24. |
| 1789 | * @param string $selected Option which is selected. |
| 1790 | * @param string $attributes array of Attributes |
| 1791 | * @return string Generated set of select boxes for the date and time formats chosen. |
| 1792 | * @access public |
| 1793 | * @link http://book.cakephp.org/view/1418/dateTime |
| 1794 | */ |
| 1795 | function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $selected = null, $attributes = array()) { |
| 1796 | $attributes += array('empty' => true); |
| 1797 | $year = $month = $day = $hour = $min = $meridian = null; |
| 1798 | |
| 1799 | if (empty($selected)) { |
| 1800 | $selected = $this->value($attributes, $fieldName); |
| 1801 | if (isset($selected['value'])) { |
| 1802 | $selected = $selected['value']; |
| 1803 | } else { |
| 1804 | $selected = null; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | if ($selected === null && $attributes['empty'] != true) { |
| 1809 | $selected = time(); |
| 1810 | } |
| 1811 | |
| 1812 | if (!empty($selected)) { |
| 1813 | if (is_array($selected)) { |
| 1814 | extract($selected); |
| 1815 | } else { |
| 1816 | if (is_numeric($selected)) { |
| 1817 | $selected = strftime('%Y-%m-%d %H:%M:%S', $selected); |
| 1818 | } |
| 1819 | $meridian = 'am'; |
| 1820 | $pos = strpos($selected, '-'); |
| 1821 | if ($pos !== false) { |
| 1822 | $date = explode('-', $selected); |
| 1823 | $days = explode(' ', $date[2]); |
| 1824 | $day = $days[0]; |
| 1825 | $month = $date[1]; |
| 1826 | $year = $date[0]; |
| 1827 | } else { |
| 1828 | $days[1] = $selected; |
| 1829 | } |
| 1830 | |
| 1831 | if (!empty($timeFormat)) { |
| 1832 | $time = explode(':', $days[1]); |
| 1833 | $check = str_replace(':', '', $days[1]); |
| 1834 | |
| 1835 | if (($check > 115959) && $timeFormat == '12') { |
| 1836 | $time[0] = $time[0] - 12; |
| 1837 | $meridian = 'pm'; |
| 1838 | } elseif ($time[0] == '00' && $timeFormat == '12') { |
| 1839 | $time[0] = 12; |
| 1840 | } elseif ($time[0] > 12) { |
| 1841 | $meridian = 'pm'; |
| 1842 | } |
| 1843 | if ($time[0] == 0 && $timeFormat == '12') { |
| 1844 | $time[0] = 12; |
| 1845 | } |
| 1846 | $hour = $time[0]; |
| 1847 | $min = $time[1]; |
| 1848 | } |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | $elements = array('Day', 'Month', 'Year', 'Hour', 'Minute', 'Meridian'); |
| 1853 | $defaults = array( |
| 1854 | 'minYear' => null, 'maxYear' => null, 'separator' => '-', |
| 1855 | 'interval' => 1, 'monthNames' => true |
| 1856 | ); |
| 1857 | $attributes = array_merge($defaults, (array) $attributes); |
| 1858 | if (isset($attributes['minuteInterval'])) { |
| 1859 | $attributes['interval'] = $attributes['minuteInterval']; |
| 1860 | unset($attributes['minuteInterval']); |
| 1861 | } |
| 1862 | $minYear = $attributes['minYear']; |
| 1863 | $maxYear = $attributes['maxYear']; |
| 1864 | $separator = $attributes['separator']; |
| 1865 | $interval = $attributes['interval']; |
| 1866 | $monthNames = $attributes['monthNames']; |
| 1867 | $attributes = array_diff_key($attributes, $defaults); |
| 1868 | |
| 1869 | if (isset($attributes['id'])) { |
| 1870 | if (is_string($attributes['id'])) { |
| 1871 | // build out an array version |
| 1872 | foreach ($elements as $element) { |
| 1873 | $selectAttrName = 'select' . $element . 'Attr'; |
| 1874 | ${$selectAttrName} = $attributes; |
| 1875 | ${$selectAttrName}['id'] = $attributes['id'] . $element; |
| 1876 | } |
| 1877 | } elseif (is_array($attributes['id'])) { |
| 1878 | // check for missing ones and build selectAttr for each element |
| 1879 | $attributes['id'] += array( |
| 1880 | 'month' => '', 'year' => '', 'day' => '', |
| 1881 | 'hour' => '', 'minute' => '', 'meridian' => '' |
| 1882 | ); |
| 1883 | foreach ($elements as $element) { |
| 1884 | $selectAttrName = 'select' . $element . 'Attr'; |
| 1885 | ${$selectAttrName} = $attributes; |
| 1886 | ${$selectAttrName}['id'] = $attributes['id'][strtolower($element)]; |
| 1887 | } |
| 1888 | } |
| 1889 | } else { |
| 1890 | // build the selectAttrName with empty id's to pass |
| 1891 | foreach ($elements as $element) { |
| 1892 | $selectAttrName = 'select' . $element . 'Attr'; |
| 1893 | ${$selectAttrName} = $attributes; |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | $selects = array(); |
| 1898 | foreach (preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY) as $char) { |
| 1899 | switch ($char) { |
| 1900 | case 'Y': |
| 1901 | $selects[] = $this->year( |
| 1902 | $fieldName, $minYear, $maxYear, $year, $selectYearAttr |
| 1903 | ); |
| 1904 | break; |
| 1905 | case 'M': |
| 1906 | $selectMonthAttr['monthNames'] = $monthNames; |
| 1907 | $selects[] = $this->month($fieldName, $month, $selectMonthAttr); |
| 1908 | break; |
| 1909 | case 'D': |
| 1910 | $selects[] = $this->day($fieldName, $day, $selectDayAttr); |
| 1911 | break; |
| 1912 | } |
| 1913 | } |
| 1914 | $opt = implode($separator, $selects); |
| 1915 | |
| 1916 | if (!empty($interval) && $interval > 1 && !empty($min)) { |
| 1917 | $min = round($min * (1 / $interval)) * $interval; |
| 1918 | } |
| 1919 | $selectMinuteAttr['interval'] = $interval; |
| 1920 | switch ($timeFormat) { |
| 1921 | case '24': |
| 1922 | $opt .= $this->hour($fieldName, true, $hour, $selectHourAttr) . ':' . |
| 1923 | $this->minute($fieldName, $min, $selectMinuteAttr); |
| 1924 | break; |
| 1925 | case '12': |
| 1926 | $opt .= $this->hour($fieldName, false, $hour, $selectHourAttr) . ':' . |
| 1927 | $this->minute($fieldName, $min, $selectMinuteAttr) . ' ' . |
| 1928 | $this->meridian($fieldName, $meridian, $selectMeridianAttr); |
| 1929 | break; |
| 1930 | default: |
| 1931 | $opt .= ''; |
| 1932 | break; |
| 1933 | } |
| 1934 | return $opt; |
| 1935 | } |
| 1936 | |
| 1937 | /** |
| 1938 | * Gets the input field name for the current tag |
| 1939 | * |
| 1940 | * @param array $options |
| 1941 | * @param string $key |
| 1942 | * @return array |
| 1943 | * @access protected |
| 1944 | */ |
| 1945 | function _name($options = array(), $field = null, $key = 'name') { |
| 1946 | if ($this->requestType == 'get') { |
| 1947 | if ($options === null) { |
| 1948 | $options = array(); |
| 1949 | } elseif (is_string($options)) { |
| 1950 | $field = $options; |
| 1951 | $options = 0; |
| 1952 | } |
| 1953 | |
| 1954 | if (!empty($field)) { |
| 1955 | $this->setEntity($field); |
| 1956 | } |
| 1957 | |
| 1958 | if (is_array($options) && isset($options[$key])) { |
| 1959 | return $options; |
| 1960 | } |
| 1961 | |
| 1962 | $view = ClassRegistry::getObject('view'); |
| 1963 | $name = !empty($view->field) ? $view->field : $view->model; |
| 1964 | if (!empty($view->fieldSuffix)) { |
| 1965 | $name .= '[' . $view->fieldSuffix . ']'; |
| 1966 | } |
| 1967 | |
| 1968 | if (is_array($options)) { |
| 1969 | $options[$key] = $name; |
| 1970 | return $options; |
| 1971 | } else { |
| 1972 | return $name; |
| 1973 | } |
| 1974 | } |
| 1975 | return parent::_name($options, $field, $key); |
| 1976 | } |
| 1977 | |
| 1978 | /** |
| 1979 | * Returns an array of formatted OPTION/OPTGROUP elements |
| 1980 | * @access private |
| 1981 | * @return array |
| 1982 | */ |
| 1983 | function __selectOptions($elements = array(), $selected = null, $parents = array(), $showParents = null, $attributes = array()) { |
| 1984 | $select = array(); |
| 1985 | $attributes = array_merge(array('escape' => true, 'style' => null, 'class' => null), $attributes); |
| 1986 | $selectedIsEmpty = ($selected === '' || $selected === null); |
| 1987 | $selectedIsArray = is_array($selected); |
| 1988 | |
| 1989 | foreach ($elements as $name => $title) { |
| 1990 | $htmlOptions = array(); |
| 1991 | if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) { |
| 1992 | if (!empty($name)) { |
| 1993 | if ($attributes['style'] === 'checkbox') { |
| 1994 | $select[] = $this->Html->tags['fieldsetend']; |
| 1995 | } else { |
| 1996 | $select[] = $this->Html->tags['optiongroupend']; |
| 1997 | } |
| 1998 | $parents[] = $name; |
| 1999 | } |
| 2000 | $select = array_merge($select, $this->__selectOptions( |
| 2001 | $title, $selected, $parents, $showParents, $attributes |
| 2002 | )); |
| 2003 | |
| 2004 | if (!empty($name)) { |
| 2005 | $name = $attributes['escape'] ? h($name) : $name; |
| 2006 | if ($attributes['style'] === 'checkbox') { |
| 2007 | $select[] = sprintf($this->Html->tags['fieldsetstart'], $name); |
| 2008 | } else { |
| 2009 | $select[] = sprintf($this->Html->tags['optiongroup'], $name, ''); |
| 2010 | } |
| 2011 | } |
| 2012 | $name = null; |
| 2013 | } elseif (is_array($title)) { |
| 2014 | $htmlOptions = $title; |
| 2015 | $name = $title['value']; |
| 2016 | $title = $title['name']; |
| 2017 | unset($htmlOptions['name'], $htmlOptions['value']); |
| 2018 | } |
| 2019 | |
| 2020 | if ($name !== null) { |
| 2021 | if ( |
| 2022 | (!$selectedIsArray && !$selectedIsEmpty && (string)$selected == (string)$name) || |
| 2023 | ($selectedIsArray && in_array($name, $selected)) |
| 2024 | ) { |
| 2025 | if ($attributes['style'] === 'checkbox') { |
| 2026 | $htmlOptions['checked'] = true; |
| 2027 | } else { |
| 2028 | $htmlOptions['selected'] = 'selected'; |
| 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | if ($showParents || (!in_array($title, $parents))) { |
| 2033 | $title = ($attributes['escape']) ? h($title) : $title; |
| 2034 | |
| 2035 | if ($attributes['style'] === 'checkbox') { |
| 2036 | $htmlOptions['value'] = $name; |
| 2037 | |
| 2038 | $tagName = Inflector::camelize( |
| 2039 | $this->model() . '_' . $this->field().'_'.Inflector::slug($name) |
| 2040 | ); |
| 2041 | $htmlOptions['id'] = $tagName; |
| 2042 | $label = array('for' => $tagName); |
| 2043 | |
| 2044 | if (isset($htmlOptions['checked']) && $htmlOptions['checked'] === true) { |
| 2045 | $label['class'] = 'selected'; |
| 2046 | } |
| 2047 | |
| 2048 | $name = $attributes['name']; |
| 2049 | |
| 2050 | if (empty($attributes['class'])) { |
| 2051 | $attributes['class'] = 'checkbox'; |
| 2052 | } |
| 2053 | $label = $this->label(null, $title, $label); |
| 2054 | $item = sprintf( |
| 2055 | $this->Html->tags['checkboxmultiple'], $name, |
| 2056 | $this->_parseAttributes($htmlOptions) |
| 2057 | ); |
| 2058 | $select[] = $this->Html->div($attributes['class'], $item . $label); |
| 2059 | } else { |
| 2060 | $select[] = sprintf( |
| 2061 | $this->Html->tags['selectoption'], |
| 2062 | $name, $this->_parseAttributes($htmlOptions), $title |
| 2063 | ); |
| 2064 | } |
| 2065 | } |
| 2066 | } |
| 2067 | } |
| 2068 | |
| 2069 | return array_reverse($select, true); |
| 2070 | } |
| 2071 | |
| 2072 | /** |
| 2073 | * Generates option lists for common <select /> menus |
| 2074 | * @access private |
| 2075 | */ |
| 2076 | function __generateOptions($name, $options = array()) { |
| 2077 | if (!empty($this->options[$name])) { |
| 2078 | return $this->options[$name]; |
| 2079 | } |
| 2080 | $data = array(); |
| 2081 | |
| 2082 | switch ($name) { |
| 2083 | case 'minute': |
| 2084 | if (isset($options['interval'])) { |
| 2085 | $interval = $options['interval']; |
| 2086 | } else { |
| 2087 | $interval = 1; |
| 2088 | } |
| 2089 | $i = 0; |
| 2090 | while ($i < 60) { |
| 2091 | $data[sprintf('%02d', $i)] = sprintf('%02d', $i); |
| 2092 | $i += $interval; |
| 2093 | } |
| 2094 | break; |
| 2095 | case 'hour': |
| 2096 | for ($i = 1; $i <= 12; $i++) { |
| 2097 | $data[sprintf('%02d', $i)] = $i; |
| 2098 | } |
| 2099 | break; |
| 2100 | case 'hour24': |
| 2101 | for ($i = 0; $i <= 23; $i++) { |
| 2102 | $data[sprintf('%02d', $i)] = $i; |
| 2103 | } |
| 2104 | break; |
| 2105 | case 'meridian': |
| 2106 | $data = array('am' => 'am', 'pm' => 'pm'); |
| 2107 | break; |
| 2108 | case 'day': |
| 2109 | $min = 1; |
| 2110 | $max = 31; |
| 2111 | |
| 2112 | if (isset($options['min'])) { |
| 2113 | $min = $options['min']; |
| 2114 | } |
| 2115 | if (isset($options['max'])) { |
| 2116 | $max = $options['max']; |
| 2117 | } |
| 2118 | |
| 2119 | for ($i = $min; $i <= $max; $i++) { |
| 2120 | $data[sprintf('%02d', $i)] = $i; |
| 2121 | } |
| 2122 | break; |
| 2123 | case 'month': |
| 2124 | if ($options['monthNames'] === true) { |
| 2125 | $data['01'] = __('January', true); |
| 2126 | $data['02'] = __('February', true); |
| 2127 | $data['03'] = __('March', true); |
| 2128 | $data['04'] = __('April', true); |
| 2129 | $data['05'] = __('May', true); |
| 2130 | $data['06'] = __('June', true); |
| 2131 | $data['07'] = __('July', true); |
| 2132 | $data['08'] = __('August', true); |
| 2133 | $data['09'] = __('September', true); |
| 2134 | $data['10'] = __('October', true); |
| 2135 | $data['11'] = __('November', true); |
| 2136 | $data['12'] = __('December', true); |
| 2137 | } else if (is_array($options['monthNames'])) { |
| 2138 | $data = $options['monthNames']; |
| 2139 | } else { |
| 2140 | for ($m = 1; $m <= 12; $m++) { |
| 2141 | $data[sprintf("%02s", $m)] = strftime("%m", mktime(1, 1, 1, $m, 1, 1999)); |
| 2142 | } |
| 2143 | } |
| 2144 | break; |
| 2145 | case 'year': |
| 2146 | $current = intval(date('Y')); |
| 2147 | |
| 2148 | if (!isset($options['min'])) { |
| 2149 | $min = $current - 20; |
| 2150 | } else { |
| 2151 | $min = $options['min']; |
| 2152 | } |
| 2153 | |
| 2154 | if (!isset($options['max'])) { |
| 2155 | $max = $current + 20; |
| 2156 | } else { |
| 2157 | $max = $options['max']; |
| 2158 | } |
| 2159 | if ($min > $max) { |
| 2160 | list($min, $max) = array($max, $min); |
| 2161 | } |
| 2162 | for ($i = $min; $i <= $max; $i++) { |
| 2163 | $data[$i] = $i; |
| 2164 | } |
| 2165 | if ($options['order'] != 'asc') { |
| 2166 | $data = array_reverse($data, true); |
| 2167 | } |
| 2168 | break; |
| 2169 | } |
| 2170 | $this->__options[$name] = $data; |
| 2171 | return $this->__options[$name]; |
| 2172 | } |
| 2173 | |
| 2174 | /** |
| 2175 | * Sets field defaults and adds field to form security input hash |
| 2176 | * |
| 2177 | * Options |
| 2178 | * |
| 2179 | * - `secure` - boolean whether or not the field should be added to the security fields. |
| 2180 | * |
| 2181 | * @param string $field Name of the field to initialize options for. |
| 2182 | * @param array $options Array of options to append options into. |
| 2183 | * @return array Array of options for the input. |
| 2184 | * @access protected |
| 2185 | */ |
| 2186 | function _initInputField($field, $options = array()) { |
| 2187 | if (isset($options['secure'])) { |
| 2188 | $secure = $options['secure']; |
| 2189 | unset($options['secure']); |
| 2190 | } else { |
| 2191 | $secure = (isset($this->params['_Token']) && !empty($this->params['_Token'])); |
| 2192 | } |
| 2193 | |
| 2194 | $fieldName = null; |
| 2195 | if ($secure && !empty($options['name'])) { |
| 2196 | preg_match_all('/\[(.*?)\]/', $options['name'], $matches); |
| 2197 | if (isset($matches[1])) { |
| 2198 | $fieldName = $matches[1]; |
| 2199 | } |
| 2200 | } |
| 2201 | |
| 2202 | $result = parent::_initInputField($field, $options); |
| 2203 | |
| 2204 | if ($secure) { |
| 2205 | $this->__secure($fieldName); |
| 2206 | } |
| 2207 | return $result; |
| 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 |