2008-08-25 14:57:37 +00:00
|
|
|
<?php
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-05-30 22:11:14 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @package Cake.View.Helper
|
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2012-04-05 12:33:12 +00:00
|
|
|
App::uses('ClassRegistry', 'Utility');
|
2010-12-04 18:10:24 +00:00
|
|
|
App::uses('AppHelper', 'View/Helper');
|
2012-03-14 02:06:20 +00:00
|
|
|
App::uses('Hash', 'Utility');
|
2013-12-04 00:30:57 +00:00
|
|
|
App::uses('Inflector', 'Utility');
|
2010-12-04 18:10:24 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Form helper library.
|
|
|
|
*
|
|
|
|
* Automatic generation of HTML FORMs from given data.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View.Helper
|
2011-08-01 00:28:35 +00:00
|
|
|
* @property HtmlHelper $Html
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class FormHelper extends AppHelper {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Other helpers used by FormHelper
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $helpers = array('Html');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-08-08 12:29:49 +00:00
|
|
|
* Options used by DateTime fields
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected $_options = array(
|
2008-05-30 11:40:08 +00:00
|
|
|
'day' => array(), 'minute' => array(), 'hour' => array(),
|
|
|
|
'month' => array(), 'year' => array(), 'meridian' => array()
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-08-08 12:29:49 +00:00
|
|
|
* List of fields created, used with secure forms.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $fields = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-06-09 01:14:22 +00:00
|
|
|
/**
|
2011-08-16 03:55:08 +00:00
|
|
|
* Constant used internally to skip the securing process,
|
2011-06-14 01:39:49 +00:00
|
|
|
* and neither add the field to the hash or to the unlocked fields.
|
2011-06-09 01:14:22 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const SECURE_SKIP = 'skip';
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Defines the type of form being created. Set by FormHelper::create().
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $requestType = null;
|
2009-10-22 03:45:42 +00:00
|
|
|
|
2009-10-31 05:11:02 +00:00
|
|
|
/**
|
|
|
|
* The default model being used for the current form.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $defaultModel = null;
|
2009-10-22 03:45:42 +00:00
|
|
|
|
2009-10-31 05:11:02 +00:00
|
|
|
/**
|
|
|
|
* Persistent default options used by input(). Set by FormHelper::create().
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_inputDefaults = array();
|
2011-07-15 01:05:44 +00:00
|
|
|
|
2011-06-09 01:14:22 +00:00
|
|
|
/**
|
2011-12-02 05:58:09 +00:00
|
|
|
* An array of field names that have been excluded from
|
2011-06-09 01:14:22 +00:00
|
|
|
* the Token hash used by SecurityComponent's validatePost method
|
|
|
|
*
|
2011-08-20 05:39:30 +00:00
|
|
|
* @see FormHelper::_secure()
|
2011-06-09 01:14:22 +00:00
|
|
|
* @see SecurityComponent::validatePost()
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-06-14 01:39:49 +00:00
|
|
|
protected $_unlockedFields = array();
|
2011-07-14 04:19:50 +00:00
|
|
|
|
2011-07-15 01:05:44 +00:00
|
|
|
/**
|
|
|
|
* Holds the model references already loaded by this helper
|
|
|
|
* product of trying to inspect them out of field names
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-07-14 04:19:50 +00:00
|
|
|
protected $_models = array();
|
2011-07-15 22:54:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds all the validation errors for models loaded and inspected
|
|
|
|
* it can also be set manually to be able to display custom error messages
|
|
|
|
* in the any of the input fields generated by this helper
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $validationErrors = array();
|
|
|
|
|
2013-12-04 00:30:57 +00:00
|
|
|
/**
|
|
|
|
* Holds already used DOM ID suffixes to avoid collisions with multiple form field elements.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_domIdSuffixes = array();
|
|
|
|
|
2014-04-26 02:05:58 +00:00
|
|
|
/**
|
|
|
|
* The action attribute value of the last created form.
|
|
|
|
* Used to make form/request specific hashes for SecurityComponent.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_lastAction = '';
|
|
|
|
|
2011-07-15 22:54:27 +00:00
|
|
|
/**
|
|
|
|
* Copies the validationErrors variable from the View object into this instance
|
|
|
|
*
|
|
|
|
* @param View $View The View this helper is being attached to.
|
|
|
|
* @param array $settings Configuration settings for the helper.
|
|
|
|
*/
|
|
|
|
public function __construct(View $View, $settings = array()) {
|
|
|
|
parent::__construct($View, $settings);
|
|
|
|
$this->validationErrors =& $View->validationErrors;
|
|
|
|
}
|
|
|
|
|
2009-10-31 05:11:02 +00:00
|
|
|
/**
|
2011-07-14 04:19:50 +00:00
|
|
|
* Guess the location for a model based on its name and tries to create a new instance
|
|
|
|
* or get an already created instance of the model
|
2009-10-31 05:11:02 +00:00
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param string $model Model name.
|
2014-11-05 12:03:27 +00:00
|
|
|
* @return Model|null Model instance
|
2009-10-31 05:11:02 +00:00
|
|
|
*/
|
2011-07-14 04:19:50 +00:00
|
|
|
protected function _getModel($model) {
|
2009-10-20 05:32:24 +00:00
|
|
|
$object = null;
|
2011-07-14 15:43:51 +00:00
|
|
|
if (!$model || $model === 'Model') {
|
2011-07-14 04:19:50 +00:00
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
2011-07-14 21:56:55 +00:00
|
|
|
if (array_key_exists($model, $this->_models)) {
|
2011-07-14 04:19:50 +00:00
|
|
|
return $this->_models[$model];
|
2009-10-20 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ClassRegistry::isKeySet($model)) {
|
2010-08-05 04:24:38 +00:00
|
|
|
$object = ClassRegistry::getObject($model);
|
2011-07-14 04:19:50 +00:00
|
|
|
} elseif (isset($this->request->params['models'][$model])) {
|
|
|
|
$plugin = $this->request->params['models'][$model]['plugin'];
|
|
|
|
$plugin .= ($plugin) ? '.' : null;
|
|
|
|
$object = ClassRegistry::init(array(
|
|
|
|
'class' => $plugin . $this->request->params['models'][$model]['className'],
|
|
|
|
'alias' => $model
|
|
|
|
));
|
2012-04-18 01:35:03 +00:00
|
|
|
} elseif (ClassRegistry::isKeySet($this->defaultModel)) {
|
|
|
|
$defaultObject = ClassRegistry::getObject($this->defaultModel);
|
2012-09-14 13:54:10 +00:00
|
|
|
if ($defaultObject && in_array($model, array_keys($defaultObject->getAssociated()), true) && isset($defaultObject->{$model})) {
|
2012-04-18 01:35:03 +00:00
|
|
|
$object = $defaultObject->{$model};
|
|
|
|
}
|
2011-07-14 04:19:50 +00:00
|
|
|
} else {
|
2011-07-14 05:33:29 +00:00
|
|
|
$object = ClassRegistry::init($model, true);
|
2009-10-20 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 21:56:55 +00:00
|
|
|
$this->_models[$model] = $object;
|
2012-03-03 22:10:12 +00:00
|
|
|
if (!$object) {
|
2011-07-14 04:19:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-03-03 22:10:12 +00:00
|
|
|
$this->fieldset[$model] = array('fields' => null, 'key' => $object->primaryKey, 'validates' => null);
|
2011-07-14 04:19:50 +00:00
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
2011-07-15 01:05:44 +00:00
|
|
|
/**
|
|
|
|
* Inspects the model properties to extract information from them.
|
|
|
|
* Currently it can extract information from the the fields, the primary key and required fields
|
|
|
|
*
|
|
|
|
* The $key parameter accepts the following list of values:
|
|
|
|
*
|
2012-09-13 02:56:46 +00:00
|
|
|
* - key: Returns the name of the primary key for the model
|
|
|
|
* - fields: Returns the model schema
|
|
|
|
* - validates: returns the list of fields that are required
|
|
|
|
* - errors: returns the list of validation errors
|
2011-07-15 01:05:44 +00:00
|
|
|
*
|
|
|
|
* If the $field parameter is passed if will return the information for that sole field.
|
|
|
|
*
|
2012-09-13 02:56:46 +00:00
|
|
|
* `$this->_introspectModel('Post', 'fields', 'title');` will return the schema information for title column
|
2011-07-15 01:05:44 +00:00
|
|
|
*
|
|
|
|
* @param string $model name of the model to extract information from
|
2011-07-15 22:54:27 +00:00
|
|
|
* @param string $key name of the special information key to obtain (key, fields, validates, errors)
|
2011-07-15 01:05:44 +00:00
|
|
|
* @param string $field name of the model field to get information from
|
|
|
|
* @return mixed information extracted for the special key and field in a model
|
|
|
|
*/
|
2011-07-14 04:19:50 +00:00
|
|
|
protected function _introspectModel($model, $key, $field = null) {
|
2012-03-03 22:10:12 +00:00
|
|
|
$object = $this->_getModel($model);
|
|
|
|
if (!$object) {
|
|
|
|
return;
|
|
|
|
}
|
2011-07-14 04:19:50 +00:00
|
|
|
|
2012-03-03 22:10:12 +00:00
|
|
|
if ($key === 'key') {
|
|
|
|
return $this->fieldset[$model]['key'] = $object->primaryKey;
|
|
|
|
}
|
2011-07-14 04:19:50 +00:00
|
|
|
|
2012-03-03 22:10:12 +00:00
|
|
|
if ($key === 'fields') {
|
|
|
|
if (!isset($this->fieldset[$model]['fields'])) {
|
2012-09-05 22:22:30 +00:00
|
|
|
$this->fieldset[$model]['fields'] = $object->schema();
|
2012-03-03 22:10:12 +00:00
|
|
|
foreach ($object->hasAndBelongsToMany as $alias => $assocData) {
|
|
|
|
$this->fieldset[$object->alias]['fields'][$alias] = array('type' => 'multiple');
|
2009-10-20 05:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-15 02:27:12 +00:00
|
|
|
if ($field === null || $field === false) {
|
2012-03-03 22:10:12 +00:00
|
|
|
return $this->fieldset[$model]['fields'];
|
|
|
|
} elseif (isset($this->fieldset[$model]['fields'][$field])) {
|
|
|
|
return $this->fieldset[$model]['fields'][$field];
|
2011-07-15 22:54:27 +00:00
|
|
|
}
|
2013-07-02 23:14:41 +00:00
|
|
|
return isset($object->hasAndBelongsToMany[$field]) ? array('type' => 'multiple') : null;
|
2012-03-03 22:10:12 +00:00
|
|
|
}
|
2011-07-15 22:54:27 +00:00
|
|
|
|
2012-03-03 22:10:12 +00:00
|
|
|
if ($key === 'errors' && !isset($this->validationErrors[$model])) {
|
|
|
|
$this->validationErrors[$model] =& $object->validationErrors;
|
|
|
|
return $this->validationErrors[$model];
|
|
|
|
} elseif ($key === 'errors' && isset($this->validationErrors[$model])) {
|
|
|
|
return $this->validationErrors[$model];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($key === 'validates' && !isset($this->fieldset[$model]['validates'])) {
|
|
|
|
$validates = array();
|
2014-04-25 20:58:17 +00:00
|
|
|
foreach (iterator_to_array($object->validator(), true) as $validateField => $validateProperties) {
|
2013-07-30 01:09:43 +00:00
|
|
|
if ($this->_isRequiredField($validateProperties)) {
|
|
|
|
$validates[$validateField] = true;
|
2009-10-20 05:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-03 22:10:12 +00:00
|
|
|
$this->fieldset[$model]['validates'] = $validates;
|
|
|
|
}
|
2009-10-22 03:45:42 +00:00
|
|
|
|
2012-03-03 22:10:12 +00:00
|
|
|
if ($key === 'validates') {
|
|
|
|
if (empty($field)) {
|
|
|
|
return $this->fieldset[$model]['validates'];
|
2011-07-14 04:19:50 +00:00
|
|
|
}
|
2013-07-02 23:14:41 +00:00
|
|
|
return isset($this->fieldset[$model]['validates'][$field]) ?
|
|
|
|
$this->fieldset[$model]['validates'] : null;
|
2012-03-03 22:10:12 +00:00
|
|
|
}
|
2009-10-20 05:32:24 +00:00
|
|
|
}
|
2010-02-23 14:25:01 +00:00
|
|
|
|
|
|
|
/**
|
2011-10-29 01:35:43 +00:00
|
|
|
* Returns if a field is required to be filled based on validation properties from the validating object.
|
2010-02-23 14:25:01 +00:00
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param CakeValidationSet $validationRules Validation rules set.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true if field is required to be filled, false otherwise
|
2010-02-23 14:25:01 +00:00
|
|
|
*/
|
2012-05-11 03:44:26 +00:00
|
|
|
protected function _isRequiredField($validationRules) {
|
2012-09-12 01:59:46 +00:00
|
|
|
if (empty($validationRules) || count($validationRules) === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-31 23:34:37 +00:00
|
|
|
|
|
|
|
$isUpdate = $this->requestType === 'put';
|
2012-05-11 03:44:26 +00:00
|
|
|
foreach ($validationRules as $rule) {
|
2012-10-31 23:34:37 +00:00
|
|
|
$rule->isUpdate($isUpdate);
|
|
|
|
if ($rule->skip()) {
|
|
|
|
continue;
|
2012-03-20 02:53:58 +00:00
|
|
|
}
|
2012-10-31 23:34:37 +00:00
|
|
|
|
|
|
|
return !$rule->allowEmpty;
|
2012-03-20 02:53:58 +00:00
|
|
|
}
|
2012-10-26 07:28:24 +00:00
|
|
|
return false;
|
2012-03-20 02:53:58 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 04:19:01 +00:00
|
|
|
/**
|
2011-07-16 22:06:47 +00:00
|
|
|
* Returns false if given form field described by the current entity has no errors.
|
|
|
|
* Otherwise it returns the validation message
|
2011-07-14 04:19:01 +00:00
|
|
|
*
|
2012-09-18 13:51:24 +00:00
|
|
|
* @return mixed Either false when there are no errors, or an array of error
|
2012-02-11 00:39:58 +00:00
|
|
|
* strings. An error string could be ''.
|
2011-11-30 05:23:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::tagIsInvalid
|
2011-07-14 04:19:01 +00:00
|
|
|
*/
|
2011-07-16 22:06:47 +00:00
|
|
|
public function tagIsInvalid() {
|
2011-07-14 04:19:01 +00:00
|
|
|
$entity = $this->entity();
|
2011-07-15 22:54:27 +00:00
|
|
|
$model = array_shift($entity);
|
2013-05-14 01:08:19 +00:00
|
|
|
|
|
|
|
// 0.Model.field. Fudge entity path
|
|
|
|
if (empty($model) || is_numeric($model)) {
|
|
|
|
array_splice($entity, 1, 0, $model);
|
|
|
|
$model = array_shift($entity);
|
|
|
|
}
|
|
|
|
|
2011-10-02 02:38:20 +00:00
|
|
|
$errors = array();
|
2011-07-15 22:54:27 +00:00
|
|
|
if (!empty($entity) && isset($this->validationErrors[$model])) {
|
2011-10-02 02:38:20 +00:00
|
|
|
$errors = $this->validationErrors[$model];
|
2011-07-15 22:54:27 +00:00
|
|
|
}
|
2011-10-02 02:38:20 +00:00
|
|
|
if (!empty($entity) && empty($errors)) {
|
2011-07-15 22:54:27 +00:00
|
|
|
$errors = $this->_introspectModel($model, 'errors');
|
2011-07-14 04:19:01 +00:00
|
|
|
}
|
2011-10-02 02:38:20 +00:00
|
|
|
if (empty($errors)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-15 10:06:02 +00:00
|
|
|
$errors = Hash::get($errors, implode('.', $entity));
|
2012-02-11 00:39:58 +00:00
|
|
|
return $errors === null ? false : $errors;
|
2011-07-14 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an HTML FORM element.
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Options:
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2009-10-31 05:11:02 +00:00
|
|
|
* - `type` Form method defaults to POST
|
2010-09-02 04:29:04 +00:00
|
|
|
* - `action` The controller action the form submits to, (optional).
|
2013-10-22 04:09:34 +00:00
|
|
|
* - `url` The URL the form submits to. Can be a string or a URL array. If you use 'url'
|
2011-03-16 01:39:15 +00:00
|
|
|
* you should leave 'action' undefined.
|
2011-09-08 00:57:28 +00:00
|
|
|
* - `default` Allows for the creation of Ajax forms. Set this to false to prevent the default event handler.
|
|
|
|
* Will create an onsubmit attribute if it doesn't not exist. If it does, default action suppression
|
|
|
|
* will be appended.
|
2009-10-31 05:11:02 +00:00
|
|
|
* - `onsubmit` Used in conjunction with 'default' to create ajax forms.
|
2010-01-25 22:59:05 +00:00
|
|
|
* - `inputDefaults` set the default $options for FormHelper::input(). Any options that would
|
2012-12-22 22:48:15 +00:00
|
|
|
* be set when using FormHelper::input() can be set here. Options set with `inputDefaults`
|
2011-10-26 00:33:55 +00:00
|
|
|
* can be overridden when calling input()
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `encoding` Set the accept-charset encoding for the form. Defaults to `Configure::read('App.encoding')`
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2012-12-19 20:24:09 +00:00
|
|
|
* @param mixed $model The model name for which the form is being defined. Should
|
|
|
|
* include the plugin name for plugin models. e.g. `ContactManager.Contact`.
|
2012-10-11 14:30:30 +00:00
|
|
|
* If an array is passed and $options argument is empty, the array will be used as options.
|
2012-12-19 20:24:09 +00:00
|
|
|
* If `false` no model is used.
|
2009-02-28 22:17:56 +00:00
|
|
|
* @param array $options An array of html attributes and options.
|
2014-07-30 20:11:03 +00:00
|
|
|
* @return string A formatted opening FORM tag.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-create
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function create($model = null, $options = array()) {
|
2009-10-20 05:32:24 +00:00
|
|
|
$created = $id = false;
|
|
|
|
$append = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (is_array($model) && empty($options)) {
|
|
|
|
$options = $model;
|
|
|
|
$model = null;
|
2009-11-13 14:53:31 +00:00
|
|
|
}
|
2012-04-18 01:35:03 +00:00
|
|
|
|
2011-07-14 04:19:50 +00:00
|
|
|
if (empty($model) && $model !== false && !empty($this->request->params['models'])) {
|
|
|
|
$model = key($this->request->params['models']);
|
|
|
|
} elseif (empty($model) && empty($this->request->params['models'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$model = false;
|
|
|
|
}
|
2012-04-18 01:35:03 +00:00
|
|
|
$this->defaultModel = $model;
|
2009-10-22 03:45:42 +00:00
|
|
|
|
2011-07-15 01:05:44 +00:00
|
|
|
$key = null;
|
2010-12-24 18:52:15 +00:00
|
|
|
if ($model !== false) {
|
2013-01-30 03:00:36 +00:00
|
|
|
list($plugin, $model) = pluginSplit($model, true);
|
|
|
|
$key = $this->_introspectModel($plugin . $model, 'key');
|
2011-07-19 00:33:45 +00:00
|
|
|
$this->setEntity($model, true);
|
2010-12-24 18:52:15 +00:00
|
|
|
}
|
2009-10-22 03:45:42 +00:00
|
|
|
|
2011-07-14 04:19:50 +00:00
|
|
|
if ($model !== false && $key) {
|
2009-10-20 05:32:24 +00:00
|
|
|
$recordExists = (
|
2010-05-14 04:21:22 +00:00
|
|
|
isset($this->request->data[$model]) &&
|
2011-07-14 04:19:50 +00:00
|
|
|
!empty($this->request->data[$model][$key]) &&
|
|
|
|
!is_array($this->request->data[$model][$key])
|
2009-10-20 05:32:24 +00:00
|
|
|
);
|
2009-10-22 03:45:42 +00:00
|
|
|
|
2009-10-20 05:32:24 +00:00
|
|
|
if ($recordExists) {
|
|
|
|
$created = true;
|
2011-07-14 04:19:50 +00:00
|
|
|
$id = $this->request->data[$model][$key];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-31 04:08:49 +00:00
|
|
|
|
2014-04-07 23:25:14 +00:00
|
|
|
$options += array(
|
2008-05-30 11:40:08 +00:00
|
|
|
'type' => ($created && empty($options['action'])) ? 'put' : 'post',
|
|
|
|
'action' => null,
|
|
|
|
'url' => null,
|
2009-10-15 04:44:38 +00:00
|
|
|
'default' => true,
|
2009-10-30 05:02:54 +00:00
|
|
|
'encoding' => strtolower(Configure::read('App.encoding')),
|
2014-04-07 23:25:14 +00:00
|
|
|
'inputDefaults' => array()
|
|
|
|
);
|
2012-05-20 20:44:54 +00:00
|
|
|
$this->inputDefaults($options['inputDefaults']);
|
2009-10-15 04:44:38 +00:00
|
|
|
unset($options['inputDefaults']);
|
2011-03-19 17:29:31 +00:00
|
|
|
|
2011-03-16 01:19:15 +00:00
|
|
|
if (!isset($options['id'])) {
|
|
|
|
$domId = isset($options['action']) ? $options['action'] : $this->request['action'];
|
|
|
|
$options['id'] = $this->domId($domId . 'Form');
|
|
|
|
}
|
2011-03-19 17:29:31 +00:00
|
|
|
|
2011-03-02 01:31:02 +00:00
|
|
|
if ($options['action'] === null && $options['url'] === null) {
|
2011-03-03 02:47:11 +00:00
|
|
|
$options['action'] = $this->request->here(false);
|
2011-03-02 01:31:02 +00:00
|
|
|
} elseif (empty($options['url']) || is_array($options['url'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($options['url']['controller'])) {
|
2012-04-18 01:35:03 +00:00
|
|
|
if (!empty($model)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$options['url']['controller'] = Inflector::underscore(Inflector::pluralize($model));
|
2010-11-28 05:33:01 +00:00
|
|
|
} elseif (!empty($this->request->params['controller'])) {
|
|
|
|
$options['url']['controller'] = Inflector::underscore($this->request->params['controller']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($options['action'])) {
|
2010-11-28 05:33:01 +00:00
|
|
|
$options['action'] = $this->request->params['action'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-05-25 19:14:23 +00:00
|
|
|
$plugin = null;
|
|
|
|
if ($this->plugin) {
|
|
|
|
$plugin = Inflector::underscore($this->plugin);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$actionDefaults = array(
|
2011-05-25 19:14:23 +00:00
|
|
|
'plugin' => $plugin,
|
2010-07-15 03:09:35 +00:00
|
|
|
'controller' => $this->_View->viewPath,
|
2008-05-30 11:40:08 +00:00
|
|
|
'action' => $options['action'],
|
|
|
|
);
|
|
|
|
$options['action'] = array_merge($actionDefaults, (array)$options['url']);
|
2011-02-03 02:52:41 +00:00
|
|
|
if (empty($options['action'][0]) && !empty($id)) {
|
2010-10-14 01:36:05 +00:00
|
|
|
$options['action'][0] = $id;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (is_string($options['url'])) {
|
|
|
|
$options['action'] = $options['url'];
|
|
|
|
}
|
|
|
|
unset($options['url']);
|
|
|
|
|
|
|
|
switch (strtolower($options['type'])) {
|
|
|
|
case 'get':
|
|
|
|
$htmlAttributes['method'] = 'get';
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'file':
|
|
|
|
$htmlAttributes['enctype'] = 'multipart/form-data';
|
2008-07-30 20:34:01 +00:00
|
|
|
$options['type'] = ($created) ? 'put' : 'post';
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'post':
|
|
|
|
case 'put':
|
|
|
|
case 'delete':
|
2008-09-21 04:06:37 +00:00
|
|
|
$append .= $this->hidden('_method', array(
|
2011-06-25 05:40:50 +00:00
|
|
|
'name' => '_method', 'value' => strtoupper($options['type']), 'id' => null,
|
|
|
|
'secure' => self::SECURE_SKIP
|
2008-09-21 04:06:37 +00:00
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
default:
|
|
|
|
$htmlAttributes['method'] = 'post';
|
|
|
|
}
|
|
|
|
$this->requestType = strtolower($options['type']);
|
|
|
|
|
2011-06-23 21:06:35 +00:00
|
|
|
$action = $this->url($options['action']);
|
2014-05-19 02:40:21 +00:00
|
|
|
$this->_lastAction($options['action']);
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($options['type'], $options['action']);
|
|
|
|
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$options['default']) {
|
2011-09-08 00:57:28 +00:00
|
|
|
if (!isset($options['onsubmit'])) {
|
|
|
|
$options['onsubmit'] = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-09-08 00:57:28 +00:00
|
|
|
$htmlAttributes['onsubmit'] = $options['onsubmit'] . 'event.returnValue = false; return false;';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-09-08 00:57:28 +00:00
|
|
|
unset($options['default']);
|
2009-10-30 05:02:54 +00:00
|
|
|
|
|
|
|
if (!empty($options['encoding'])) {
|
|
|
|
$htmlAttributes['accept-charset'] = $options['encoding'];
|
|
|
|
unset($options['encoding']);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$htmlAttributes = array_merge($options, $htmlAttributes);
|
|
|
|
|
2010-06-13 02:50:09 +00:00
|
|
|
$this->fields = array();
|
2013-01-25 20:21:16 +00:00
|
|
|
if ($this->requestType !== 'get') {
|
|
|
|
$append .= $this->_csrfField();
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!empty($append)) {
|
2012-09-03 14:04:19 +00:00
|
|
|
$append = $this->Html->useTag('hiddenblock', $append);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-07-19 00:33:45 +00:00
|
|
|
if ($model !== false) {
|
|
|
|
$this->setEntity($model, true);
|
2011-10-26 02:21:27 +00:00
|
|
|
$this->_introspectModel($model, 'fields');
|
2011-07-19 00:33:45 +00:00
|
|
|
}
|
2014-05-09 21:01:22 +00:00
|
|
|
|
2011-07-25 19:28:51 +00:00
|
|
|
return $this->Html->useTag('form', $action, $htmlAttributes) . $append;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-08-11 01:53:33 +00:00
|
|
|
/**
|
|
|
|
* Return a CSRF input if the _Token is present.
|
|
|
|
* Used to secure forms in conjunction with SecurityComponent
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function _csrfField() {
|
|
|
|
if (empty($this->request->params['_Token'])) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if (!empty($this->request['_Token']['unlockedFields'])) {
|
|
|
|
foreach ((array)$this->request['_Token']['unlockedFields'] as $unlocked) {
|
|
|
|
$this->_unlockedFields[] = $unlocked;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->hidden('_Token.key', array(
|
|
|
|
'value' => $this->request->params['_Token']['key'], 'id' => 'Token' . mt_rand(),
|
|
|
|
'secure' => self::SECURE_SKIP
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Closes an HTML form, cleans up values set by FormHelper::create(), and writes hidden
|
|
|
|
* input fields where appropriate.
|
|
|
|
*
|
2009-10-17 04:55:17 +00:00
|
|
|
* If $options is set a form submit button will be created. Options can be either a string or an array.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2009-10-17 04:55:17 +00:00
|
|
|
* array usage:
|
2010-01-27 20:18:53 +00:00
|
|
|
*
|
2009-10-17 04:55:17 +00:00
|
|
|
* array('label' => 'save'); value="save"
|
|
|
|
* array('label' => 'save', 'name' => 'Whatever'); value="save" name="Whatever"
|
|
|
|
* array('name' => 'Whatever'); value="Submit" name="Whatever"
|
|
|
|
* array('label' => 'save', 'name' => 'Whatever', 'div' => 'good') <div class="good"> value="save" name="Whatever"
|
|
|
|
* array('label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')); <div class="good"> value="save" name="Whatever"
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2013-12-30 15:28:10 +00:00
|
|
|
* If $secureAttributes is set, these html attributes will be merged into the hidden input tags generated for the
|
|
|
|
* Security Component. This is especially useful to set HTML5 attributes like 'form'
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $options as a string will use $options as the value of button,
|
2013-12-30 15:28:10 +00:00
|
|
|
* @param array $secureAttributes will be passed as html attributes into the hidden input elements generated for the
|
|
|
|
* Security Component.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string a closing FORM tag optional submit button.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#closing-the-form
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-12-30 15:28:10 +00:00
|
|
|
public function end($options = null, $secureAttributes = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out = null;
|
|
|
|
$submit = null;
|
|
|
|
|
|
|
|
if ($options !== null) {
|
|
|
|
$submitOptions = array();
|
|
|
|
if (is_string($options)) {
|
|
|
|
$submit = $options;
|
|
|
|
} else {
|
|
|
|
if (isset($options['label'])) {
|
|
|
|
$submit = $options['label'];
|
|
|
|
unset($options['label']);
|
|
|
|
}
|
|
|
|
$submitOptions = $options;
|
|
|
|
}
|
|
|
|
$out .= $this->submit($submit, $submitOptions);
|
2008-10-07 16:57:22 +00:00
|
|
|
}
|
2014-12-09 02:17:35 +00:00
|
|
|
if ($this->requestType !== 'get' &&
|
2013-01-25 20:21:16 +00:00
|
|
|
isset($this->request['_Token']) &&
|
|
|
|
!empty($this->request['_Token'])
|
|
|
|
) {
|
2013-12-30 15:28:10 +00:00
|
|
|
$out .= $this->secure($this->fields, $secureAttributes);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->fields = array();
|
|
|
|
}
|
|
|
|
$this->setEntity(null);
|
2011-01-23 14:26:14 +00:00
|
|
|
$out .= $this->Html->useTag('formend');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-15 03:09:35 +00:00
|
|
|
$this->_View->modelScope = false;
|
2013-11-12 02:56:17 +00:00
|
|
|
$this->requestType = null;
|
2009-11-21 21:43:54 +00:00
|
|
|
return $out;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2014-02-17 16:03:49 +00:00
|
|
|
* Generates a hidden field with a security hash based on the fields used in
|
|
|
|
* the form.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-02-17 16:03:49 +00:00
|
|
|
* If $secureAttributes is set, these html attributes will be merged into
|
|
|
|
* the hidden input tags generated for the Security Component. This is
|
|
|
|
* especially useful to set HTML5 attributes like 'form'.
|
2013-12-30 15:28:10 +00:00
|
|
|
*
|
2014-02-17 16:03:49 +00:00
|
|
|
* @param array|null $fields If set specifies the list of fields to use when
|
|
|
|
* generating the hash, else $this->fields is being used.
|
|
|
|
* @param array $secureAttributes will be passed as html attributes into the hidden
|
|
|
|
* input elements generated for the Security Component.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string A hidden input field with a security hash
|
2011-11-30 05:23:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::secure
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-12-30 15:28:10 +00:00
|
|
|
public function secure($fields = array(), $secureAttributes = array()) {
|
2010-05-14 04:21:22 +00:00
|
|
|
if (!isset($this->request['_Token']) || empty($this->request['_Token'])) {
|
2008-09-24 23:02:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$locked = array();
|
2011-06-14 01:39:49 +00:00
|
|
|
$unlockedFields = $this->_unlockedFields;
|
2014-02-26 05:10:53 +00:00
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
if (!is_int($key)) {
|
|
|
|
$locked[$key] = $value;
|
|
|
|
unset($fields[$key]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-09 02:46:29 +00:00
|
|
|
|
2011-06-14 01:39:49 +00:00
|
|
|
sort($unlockedFields, SORT_STRING);
|
2008-09-24 23:02:14 +00:00
|
|
|
sort($fields, SORT_STRING);
|
|
|
|
ksort($locked, SORT_STRING);
|
|
|
|
$fields += $locked;
|
|
|
|
|
2010-11-21 05:09:45 +00:00
|
|
|
$locked = implode(array_keys($locked), '|');
|
2011-06-14 01:39:49 +00:00
|
|
|
$unlocked = implode($unlockedFields, '|');
|
2014-04-26 02:05:58 +00:00
|
|
|
$hashParts = array(
|
|
|
|
$this->_lastAction,
|
|
|
|
serialize($fields),
|
|
|
|
$unlocked,
|
|
|
|
Configure::read('Security.salt')
|
|
|
|
);
|
|
|
|
$fields = Security::hash(implode('', $hashParts), 'sha1');
|
2008-09-24 23:02:14 +00:00
|
|
|
|
2013-12-30 15:28:10 +00:00
|
|
|
$tokenFields = array_merge($secureAttributes, array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'value' => urlencode($fields . ':' . $locked),
|
2013-12-30 15:28:10 +00:00
|
|
|
'id' => 'TokenFields' . mt_rand(),
|
2008-09-24 23:02:14 +00:00
|
|
|
));
|
2013-12-30 15:28:10 +00:00
|
|
|
$out = $this->hidden('_Token.fields', $tokenFields);
|
|
|
|
$tokenUnlocked = array_merge($secureAttributes, array(
|
2011-06-14 01:39:49 +00:00
|
|
|
'value' => urlencode($unlocked),
|
2013-12-30 15:28:10 +00:00
|
|
|
'id' => 'TokenUnlocked' . mt_rand(),
|
2011-06-09 02:46:29 +00:00
|
|
|
));
|
2013-12-30 15:28:10 +00:00
|
|
|
$out .= $this->hidden('_Token.unlocked', $tokenUnlocked);
|
2012-09-03 14:04:19 +00:00
|
|
|
return $this->Html->useTag('hiddenblock', $out);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-06-09 02:17:32 +00:00
|
|
|
/**
|
2011-06-14 01:39:49 +00:00
|
|
|
* Add to or get the list of fields that are currently unlocked.
|
|
|
|
* Unlocked fields are not included in the field hash used by SecurityComponent
|
|
|
|
* unlocking a field once its been added to the list of secured fields will remove
|
2011-06-09 02:17:32 +00:00
|
|
|
* it from the list of fields.
|
|
|
|
*
|
|
|
|
* @param string $name The dot separated name for the field.
|
|
|
|
* @return mixed Either null, or the list of fields.
|
2011-11-14 06:08:20 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::unlockField
|
2011-06-09 02:17:32 +00:00
|
|
|
*/
|
2011-06-14 01:39:49 +00:00
|
|
|
public function unlockField($name = null) {
|
2011-06-09 02:17:32 +00:00
|
|
|
if ($name === null) {
|
2011-06-14 01:39:49 +00:00
|
|
|
return $this->_unlockedFields;
|
2011-06-09 02:17:32 +00:00
|
|
|
}
|
2011-06-14 01:39:49 +00:00
|
|
|
if (!in_array($name, $this->_unlockedFields)) {
|
|
|
|
$this->_unlockedFields[] = $name;
|
2011-06-09 02:17:32 +00:00
|
|
|
}
|
|
|
|
$index = array_search($name, $this->fields);
|
|
|
|
if ($index !== false) {
|
|
|
|
unset($this->fields[$index]);
|
|
|
|
}
|
|
|
|
unset($this->fields[$name]);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-27 20:18:53 +00:00
|
|
|
* Determine which fields of a form should be used for hash.
|
2010-01-25 22:59:05 +00:00
|
|
|
* Populates $this->fields
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $lock Whether this field should be part of the validation
|
2011-06-14 01:39:49 +00:00
|
|
|
* or excluded as part of the unlockedFields.
|
2014-04-07 23:25:14 +00:00
|
|
|
* @param string|array $field Reference to field to be secured. Should be dot separated to indicate nesting.
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param mixed $value Field value, if value should not be tampered with.
|
2014-04-07 23:25:14 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-20 05:39:30 +00:00
|
|
|
protected function _secure($lock, $field = null, $value = null) {
|
2008-09-24 23:02:14 +00:00
|
|
|
if (!$field) {
|
2011-06-26 00:02:34 +00:00
|
|
|
$field = $this->entity();
|
2008-09-24 23:02:14 +00:00
|
|
|
} elseif (is_string($field)) {
|
2012-03-11 01:57:18 +00:00
|
|
|
$field = Hash::filter(explode('.', $field));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-06-14 01:39:49 +00:00
|
|
|
foreach ($this->_unlockedFields as $unlockField) {
|
|
|
|
$unlockParts = explode('.', $unlockField);
|
|
|
|
if (array_values(array_intersect($field, $unlockParts)) === $unlockParts) {
|
2011-06-09 01:14:22 +00:00
|
|
|
return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-09 01:14:22 +00:00
|
|
|
|
2009-11-19 22:13:35 +00:00
|
|
|
$field = implode('.', $field);
|
2012-03-13 16:02:22 +00:00
|
|
|
$field = preg_replace('/(\.\d+)+$/', '', $field);
|
2011-06-09 01:14:22 +00:00
|
|
|
|
|
|
|
if ($lock) {
|
|
|
|
if (!in_array($field, $this->fields)) {
|
|
|
|
if ($value !== null) {
|
|
|
|
return $this->fields[$field] = $value;
|
|
|
|
}
|
|
|
|
$this->fields[] = $field;
|
|
|
|
}
|
|
|
|
} else {
|
2011-06-14 01:39:49 +00:00
|
|
|
$this->unlockField($field);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if there is an error for the given field, otherwise false
|
|
|
|
*
|
2008-09-24 23:02:14 +00:00
|
|
|
* @param string $field This should be "Modelname.fieldname"
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool If there are errors this method returns true, else false.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::isFieldError
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function isFieldError($field) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->setEntity($field);
|
|
|
|
return (bool)$this->tagIsInvalid();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a formatted error message for given FORM field, NULL if no errors.
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Options:
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2013-09-17 12:53:07 +00:00
|
|
|
* - `escape` boolean - Whether or not to html escape the contents of the error.
|
2013-07-05 12:15:18 +00:00
|
|
|
* - `wrap` mixed - Whether or not the error message should be wrapped in a div. If a
|
2009-02-03 18:48:28 +00:00
|
|
|
* string, will be used as the HTML tag to use.
|
2013-10-23 02:59:50 +00:00
|
|
|
* - `class` string - The class name for the error message
|
2009-02-18 01:39:01 +00:00
|
|
|
*
|
2009-10-17 04:55:17 +00:00
|
|
|
* @param string $field A field name, like "Modelname.fieldname"
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $text Error message as string or array of messages.
|
2013-07-05 12:15:18 +00:00
|
|
|
* If array contains `attributes` key it will be used as options for error container
|
2009-10-17 04:55:17 +00:00
|
|
|
* @param array $options Rendering options for <div /> wrapper tag
|
2014-11-08 19:07:47 +00:00
|
|
|
* @return string|null If there are errors this method returns an error message, otherwise null.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::error
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function error($field, $text = null, $options = array()) {
|
2008-12-25 22:13:06 +00:00
|
|
|
$defaults = array('wrap' => true, 'class' => 'error-message', 'escape' => true);
|
2014-04-07 23:25:14 +00:00
|
|
|
$options += $defaults;
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->setEntity($field);
|
|
|
|
|
2011-09-28 15:05:32 +00:00
|
|
|
$error = $this->tagIsInvalid();
|
2011-10-02 02:38:20 +00:00
|
|
|
if ($error === false) {
|
2011-07-14 05:33:29 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (is_array($text)) {
|
|
|
|
if (isset($text['attributes']) && is_array($text['attributes'])) {
|
|
|
|
$options = array_merge($options, $text['attributes']);
|
|
|
|
unset($text['attributes']);
|
|
|
|
}
|
|
|
|
$tmp = array();
|
|
|
|
foreach ($error as &$e) {
|
|
|
|
if (isset($text[$e])) {
|
2012-03-03 22:10:12 +00:00
|
|
|
$tmp[] = $text[$e];
|
2011-07-14 05:33:29 +00:00
|
|
|
} else {
|
2012-03-03 22:10:12 +00:00
|
|
|
$tmp[] = $e;
|
2010-04-20 03:55:57 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-07-14 05:33:29 +00:00
|
|
|
$text = $tmp;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-10-02 02:38:20 +00:00
|
|
|
if ($text !== null) {
|
2011-07-14 05:33:29 +00:00
|
|
|
$error = $text;
|
|
|
|
}
|
|
|
|
if (is_array($error)) {
|
|
|
|
foreach ($error as &$e) {
|
|
|
|
if (is_numeric($e)) {
|
2011-10-23 18:19:14 +00:00
|
|
|
$e = __d('cake', 'Error in field %s', Inflector::humanize($this->field()));
|
2010-12-26 06:40:02 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-07-14 05:33:29 +00:00
|
|
|
}
|
|
|
|
if ($options['escape']) {
|
|
|
|
$error = h($error);
|
|
|
|
unset($options['escape']);
|
|
|
|
}
|
|
|
|
if (is_array($error)) {
|
|
|
|
if (count($error) > 1) {
|
|
|
|
$listParams = array();
|
|
|
|
if (isset($options['listOptions'])) {
|
|
|
|
if (is_string($options['listOptions'])) {
|
2012-03-03 22:10:12 +00:00
|
|
|
$listParams[] = $options['listOptions'];
|
2011-07-14 05:33:29 +00:00
|
|
|
} else {
|
|
|
|
if (isset($options['listOptions']['itemOptions'])) {
|
2012-03-03 22:10:12 +00:00
|
|
|
$listParams[] = $options['listOptions']['itemOptions'];
|
2011-07-14 05:33:29 +00:00
|
|
|
unset($options['listOptions']['itemOptions']);
|
2010-12-26 06:40:02 +00:00
|
|
|
} else {
|
2012-03-03 22:10:12 +00:00
|
|
|
$listParams[] = array();
|
2010-12-26 06:40:02 +00:00
|
|
|
}
|
2011-07-14 05:33:29 +00:00
|
|
|
if (isset($options['listOptions']['tag'])) {
|
2012-03-03 22:10:12 +00:00
|
|
|
$listParams[] = $options['listOptions']['tag'];
|
2011-07-14 05:33:29 +00:00
|
|
|
unset($options['listOptions']['tag']);
|
|
|
|
}
|
|
|
|
array_unshift($listParams, $options['listOptions']);
|
2010-12-26 06:40:02 +00:00
|
|
|
}
|
2011-07-14 05:33:29 +00:00
|
|
|
unset($options['listOptions']);
|
2010-12-26 06:40:02 +00:00
|
|
|
}
|
2011-07-14 05:33:29 +00:00
|
|
|
array_unshift($listParams, $error);
|
|
|
|
$error = call_user_func_array(array($this->Html, 'nestedList'), $listParams);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-07-14 05:33:29 +00:00
|
|
|
$error = array_pop($error);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-07-14 05:33:29 +00:00
|
|
|
}
|
|
|
|
if ($options['wrap']) {
|
|
|
|
$tag = is_string($options['wrap']) ? $options['wrap'] : 'div';
|
|
|
|
unset($options['wrap']);
|
|
|
|
return $this->Html->tag($tag, $error, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-07-03 17:27:17 +00:00
|
|
|
return $error;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 22:59:05 +00:00
|
|
|
* Returns a formatted LABEL element for HTML FORMs. Will automatically generate
|
2012-09-18 13:56:36 +00:00
|
|
|
* a `for` attribute if one is not provided.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-12-27 15:08:36 +00:00
|
|
|
* ### Options
|
|
|
|
*
|
2012-02-11 00:39:58 +00:00
|
|
|
* - `for` - Set the for attribute, if its not defined the for attribute
|
2011-12-27 15:08:36 +00:00
|
|
|
* will be generated from the $fieldName parameter using
|
|
|
|
* FormHelper::domId().
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* The text and for attribute are generated off of the fieldname
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-12-27 15:08:36 +00:00
|
|
|
* echo $this->Form->label('Post.published');
|
|
|
|
* <label for="PostPublished">Published</label>
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-12-27 15:08:36 +00:00
|
|
|
*
|
|
|
|
* Custom text:
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-12-27 15:08:36 +00:00
|
|
|
* echo $this->Form->label('Post.published', 'Publish');
|
|
|
|
* <label for="PostPublished">Publish</label>
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-12-27 15:08:36 +00:00
|
|
|
*
|
|
|
|
* Custom class name:
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-12-27 15:08:36 +00:00
|
|
|
* echo $this->Form->label('Post.published', 'Publish', 'required');
|
|
|
|
* <label for="PostPublished" class="required">Publish</label>
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-12-27 15:08:36 +00:00
|
|
|
*
|
|
|
|
* Custom attributes:
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-12-27 15:08:36 +00:00
|
|
|
* echo $this->Form->label('Post.published', 'Publish', array(
|
|
|
|
* 'for' => 'post-publish'
|
|
|
|
* ));
|
|
|
|
* <label for="post-publish">Publish</label>
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-12-27 15:08:36 +00:00
|
|
|
*
|
2008-09-24 23:02:14 +00:00
|
|
|
* @param string $fieldName This should be "Modelname.fieldname"
|
2012-12-22 22:48:15 +00:00
|
|
|
* @param string $text Text that will appear in the label field. If
|
2012-02-11 00:39:58 +00:00
|
|
|
* $text is left undefined the text will be inflected from the
|
2011-12-27 15:08:36 +00:00
|
|
|
* fieldName.
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array|string $options An array of HTML attributes, or a string, to be used as a class name.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string The formatted LABEL element
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::label
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function label($fieldName = null, $text = null, $options = array()) {
|
2012-11-15 02:27:12 +00:00
|
|
|
if ($fieldName === null) {
|
2011-06-26 00:02:34 +00:00
|
|
|
$fieldName = implode('.', $this->entity());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($text === null) {
|
2008-09-18 03:09:19 +00:00
|
|
|
if (strpos($fieldName, '.') !== false) {
|
2010-12-13 01:21:14 +00:00
|
|
|
$fieldElements = explode('.', $fieldName);
|
|
|
|
$text = array_pop($fieldElements);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$text = $fieldName;
|
|
|
|
}
|
2013-02-12 02:38:08 +00:00
|
|
|
if (substr($text, -3) === '_id') {
|
2012-03-28 20:07:26 +00:00
|
|
|
$text = substr($text, 0, -3);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-10-15 01:01:17 +00:00
|
|
|
$text = __(Inflector::humanize(Inflector::underscore($text)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-10-17 04:55:17 +00:00
|
|
|
if (is_string($options)) {
|
|
|
|
$options = array('class' => $options);
|
2009-02-09 16:39:37 +00:00
|
|
|
}
|
|
|
|
|
2009-10-17 04:55:17 +00:00
|
|
|
if (isset($options['for'])) {
|
|
|
|
$labelFor = $options['for'];
|
|
|
|
unset($options['for']);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$labelFor = $this->domId($fieldName);
|
|
|
|
}
|
|
|
|
|
2011-01-23 14:26:14 +00:00
|
|
|
return $this->Html->useTag('label', $labelFor, $options, $text);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Generate a set of inputs for `$fields`. If $fields is null the fields of current model
|
2009-09-24 03:05:08 +00:00
|
|
|
* will be used.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-11-12 18:41:02 +00:00
|
|
|
* You can customize individual inputs through `$fields`.
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2012-11-12 18:41:02 +00:00
|
|
|
* $this->Form->inputs(array(
|
2009-09-24 03:05:08 +00:00
|
|
|
* 'name' => array('label' => 'custom label')
|
|
|
|
* ));
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2009-09-24 03:05:08 +00:00
|
|
|
*
|
2012-11-12 18:41:02 +00:00
|
|
|
* In addition to controller fields output, `$fields` can be used to control legend
|
|
|
|
* and fieldset rendering.
|
|
|
|
* `$this->Form->inputs('My legend');` Would generate an input set with a custom legend.
|
|
|
|
* Passing `fieldset` and `legend` key in `$fields` array has been deprecated since 2.3,
|
|
|
|
* for more fine grained control use the `fieldset` and `legend` keys in `$options` param.
|
2010-06-27 04:39:31 +00:00
|
|
|
*
|
2012-11-12 18:41:02 +00:00
|
|
|
* @param array $fields An array of fields to generate inputs for, or null.
|
|
|
|
* @param array $blacklist A simple array of fields to not create inputs for.
|
|
|
|
* @param array $options Options array. Valid keys are:
|
2010-10-10 17:46:48 +00:00
|
|
|
* - `fieldset` Set to false to disable the fieldset. If a string is supplied it will be used as
|
2013-10-23 02:59:50 +00:00
|
|
|
* the class name for the fieldset element.
|
2010-06-27 04:39:31 +00:00
|
|
|
* - `legend` Set to false to disable the legend for the generated input set. Or supply a string
|
2012-08-27 02:53:48 +00:00
|
|
|
* to customize the legend text.
|
2009-09-24 03:05:08 +00:00
|
|
|
* @return string Completed form inputs.
|
2011-11-30 05:23:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::inputs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-11-12 18:41:02 +00:00
|
|
|
public function inputs($fields = null, $blacklist = null, $options = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$fieldset = $legend = true;
|
2012-12-19 21:02:24 +00:00
|
|
|
$modelFields = array();
|
2010-02-26 23:09:33 +00:00
|
|
|
$model = $this->model();
|
2012-12-19 21:02:24 +00:00
|
|
|
if ($model) {
|
2013-04-10 01:46:51 +00:00
|
|
|
$modelFields = array_keys((array)$this->_introspectModel($model, 'fields'));
|
2012-12-19 21:02:24 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($fields)) {
|
2012-11-12 18:41:02 +00:00
|
|
|
if (array_key_exists('legend', $fields) && !in_array('legend', $modelFields)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$legend = $fields['legend'];
|
|
|
|
unset($fields['legend']);
|
|
|
|
}
|
|
|
|
|
2012-11-12 18:41:02 +00:00
|
|
|
if (isset($fields['fieldset']) && !in_array('fieldset', $modelFields)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$fieldset = $fields['fieldset'];
|
|
|
|
unset($fields['fieldset']);
|
|
|
|
}
|
|
|
|
} elseif ($fields !== null) {
|
|
|
|
$fieldset = $legend = $fields;
|
|
|
|
if (!is_bool($fieldset)) {
|
|
|
|
$fieldset = true;
|
|
|
|
}
|
|
|
|
$fields = array();
|
|
|
|
}
|
|
|
|
|
2012-11-12 18:41:02 +00:00
|
|
|
if (isset($options['legend'])) {
|
|
|
|
$legend = $options['legend'];
|
|
|
|
}
|
|
|
|
if (isset($options['fieldset'])) {
|
|
|
|
$fieldset = $options['fieldset'];
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($fields)) {
|
2012-11-12 18:41:02 +00:00
|
|
|
$fields = $modelFields;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($legend === true) {
|
2011-10-23 18:19:14 +00:00
|
|
|
$actionName = __d('cake', 'New %s');
|
2008-12-25 22:13:06 +00:00
|
|
|
$isEdit = (
|
2010-05-18 03:31:47 +00:00
|
|
|
strpos($this->request->params['action'], 'update') !== false ||
|
|
|
|
strpos($this->request->params['action'], 'edit') !== false
|
2008-12-25 22:13:06 +00:00
|
|
|
);
|
|
|
|
if ($isEdit) {
|
2011-10-23 18:19:14 +00:00
|
|
|
$actionName = __d('cake', 'Edit %s');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-02-26 23:09:33 +00:00
|
|
|
$modelName = Inflector::humanize(Inflector::underscore($model));
|
2011-03-19 17:29:31 +00:00
|
|
|
$legend = sprintf($actionName, __($modelName));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$out = null;
|
|
|
|
foreach ($fields as $name => $options) {
|
|
|
|
if (is_numeric($name) && !is_array($options)) {
|
2008-12-25 22:13:06 +00:00
|
|
|
$name = $options;
|
|
|
|
$options = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-12-25 22:13:06 +00:00
|
|
|
$entity = explode('.', $name);
|
|
|
|
$blacklisted = (
|
|
|
|
is_array($blacklist) &&
|
|
|
|
(in_array($name, $blacklist) || in_array(end($entity), $blacklist))
|
|
|
|
);
|
|
|
|
if ($blacklisted) {
|
2008-05-30 11:40:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$out .= $this->input($name, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($fieldset)) {
|
|
|
|
$fieldsetClass = sprintf(' class="%s"', $fieldset);
|
|
|
|
} else {
|
|
|
|
$fieldsetClass = '';
|
|
|
|
}
|
|
|
|
|
2012-11-12 18:41:02 +00:00
|
|
|
if ($fieldset) {
|
|
|
|
if ($legend) {
|
|
|
|
$out = $this->Html->useTag('legend', $legend) . $out;
|
|
|
|
}
|
|
|
|
$out = $this->Html->useTag('fieldset', $fieldsetClass, $out);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-11-12 18:41:02 +00:00
|
|
|
return $out;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generates a form input element complete with label and wrapper div
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Options
|
|
|
|
*
|
|
|
|
* See each field type method for more information. Any options that are part of
|
2011-04-13 22:48:06 +00:00
|
|
|
* $attributes or $options for the different **type** methods can be included in `$options` for input().i
|
|
|
|
* Additionally, any unknown keys that are not in the list below, or part of the selected type's options
|
|
|
|
* will be treated as a regular html attribute for the generated input.
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2009-10-17 05:29:16 +00:00
|
|
|
* - `type` - Force the type of widget you want. e.g. `type => 'select'`
|
2012-09-20 16:24:22 +00:00
|
|
|
* - `label` - Either a string label, or an array of options for the label. See FormHelper::label().
|
2010-01-27 20:18:53 +00:00
|
|
|
* - `div` - Either `false` to disable the div, or an array of options for the div.
|
2010-08-05 23:31:51 +00:00
|
|
|
* See HtmlHelper::div() for more options.
|
2012-09-20 16:24:22 +00:00
|
|
|
* - `options` - For widgets that take options e.g. radio, select.
|
|
|
|
* - `error` - Control the error message that is produced. Set to `false` to disable any kind of error reporting (field
|
|
|
|
* error and error messages).
|
|
|
|
* - `errorMessage` - Boolean to control rendering error messages (field error will still occur).
|
2009-10-17 05:29:16 +00:00
|
|
|
* - `empty` - String or boolean to enable empty select box options.
|
2010-01-25 22:59:05 +00:00
|
|
|
* - `before` - Content to place before the label + input.
|
|
|
|
* - `after` - Content to place after the label + input.
|
|
|
|
* - `between` - Content to place between the label + input.
|
2012-09-20 16:24:22 +00:00
|
|
|
* - `format` - Format template for element order. Any element that is not in the array, will not be in the output.
|
2010-08-05 23:31:51 +00:00
|
|
|
* - Default input format order: array('before', 'label', 'between', 'input', 'after', 'error')
|
|
|
|
* - Default checkbox format order: array('before', 'input', 'between', 'label', 'after', 'error')
|
|
|
|
* - Hidden input will not be formatted
|
|
|
|
* - Radio buttons cannot have the order of input and label elements controlled with these settings.
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2008-09-24 23:02:14 +00:00
|
|
|
* @param string $fieldName This should be "Modelname.fieldname"
|
2009-02-28 22:17:56 +00:00
|
|
|
* @param array $options Each type of input takes different options.
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return string Completed form widget.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#creating-form-elements
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function input($fieldName, $options = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->setEntity($fieldName);
|
2012-12-08 17:10:36 +00:00
|
|
|
$options = $this->_parseOptions($options);
|
2008-12-25 22:13:06 +00:00
|
|
|
|
2012-09-28 12:50:32 +00:00
|
|
|
$divOptions = $this->_divOptions($options);
|
2010-02-20 18:04:15 +00:00
|
|
|
unset($options['div']);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-09-28 12:50:32 +00:00
|
|
|
if ($options['type'] === 'radio' && isset($options['options'])) {
|
|
|
|
$radioOptions = (array)$options['options'];
|
|
|
|
unset($options['options']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2012-09-28 12:50:32 +00:00
|
|
|
$label = $this->_getLabel($fieldName, $options);
|
|
|
|
if ($options['type'] !== 'radio') {
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($options['label']);
|
|
|
|
}
|
|
|
|
|
2010-02-20 18:04:15 +00:00
|
|
|
$error = $this->_extractOption('error', $options, null);
|
|
|
|
unset($options['error']);
|
|
|
|
|
2012-09-20 16:24:22 +00:00
|
|
|
$errorMessage = $this->_extractOption('errorMessage', $options, true);
|
|
|
|
unset($options['errorMessage']);
|
|
|
|
|
2010-02-20 18:04:15 +00:00
|
|
|
$selected = $this->_extractOption('selected', $options, null);
|
|
|
|
unset($options['selected']);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-02-20 18:04:15 +00:00
|
|
|
if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time') {
|
|
|
|
$dateFormat = $this->_extractOption('dateFormat', $options, 'MDY');
|
|
|
|
$timeFormat = $this->_extractOption('timeFormat', $options, 12);
|
|
|
|
unset($options['dateFormat'], $options['timeFormat']);
|
|
|
|
}
|
2009-10-17 05:29:16 +00:00
|
|
|
|
2009-10-15 04:44:38 +00:00
|
|
|
$type = $options['type'];
|
2012-09-28 12:50:32 +00:00
|
|
|
$out = array('before' => $options['before'], 'label' => $label, 'between' => $options['between'], 'after' => $options['after']);
|
|
|
|
$format = $this->_getFormat($options);
|
|
|
|
|
2010-02-26 23:09:33 +00:00
|
|
|
unset($options['type'], $options['before'], $options['between'], $options['after'], $options['format']);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-09-28 12:50:32 +00:00
|
|
|
$out['error'] = null;
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($type !== 'hidden' && $error !== false) {
|
2012-09-28 12:50:32 +00:00
|
|
|
$errMsg = $this->error($fieldName, $error);
|
|
|
|
if ($errMsg) {
|
|
|
|
$divOptions = $this->addClass($divOptions, 'error');
|
|
|
|
if ($errorMessage) {
|
|
|
|
$out['error'] = $errMsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type === 'radio' && isset($out['between'])) {
|
|
|
|
$options['between'] = $out['between'];
|
|
|
|
$out['between'] = null;
|
|
|
|
}
|
|
|
|
$out['input'] = $this->_getInput(compact('type', 'fieldName', 'options', 'radioOptions', 'selected', 'dateFormat', 'timeFormat'));
|
|
|
|
|
|
|
|
$output = '';
|
|
|
|
foreach ($format as $element) {
|
|
|
|
$output .= $out[$element];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($divOptions['tag'])) {
|
|
|
|
$tag = $divOptions['tag'];
|
|
|
|
unset($divOptions['tag']);
|
|
|
|
$output = $this->Html->tag($tag, $output, $divOptions);
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-08 17:10:36 +00:00
|
|
|
* Generates an input element
|
2012-09-28 12:50:32 +00:00
|
|
|
*
|
2013-07-06 17:04:06 +00:00
|
|
|
* @param array $args The options for the input element
|
|
|
|
* @return string The generated input element
|
2012-09-28 12:50:32 +00:00
|
|
|
*/
|
|
|
|
protected function _getInput($args) {
|
|
|
|
extract($args);
|
2008-05-30 11:40:08 +00:00
|
|
|
switch ($type) {
|
|
|
|
case 'hidden':
|
2012-09-28 12:50:32 +00:00
|
|
|
return $this->hidden($fieldName, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'checkbox':
|
2012-09-28 12:50:32 +00:00
|
|
|
return $this->checkbox($fieldName, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'radio':
|
2012-09-28 12:50:32 +00:00
|
|
|
return $this->radio($fieldName, $radioOptions, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'file':
|
2012-09-28 12:50:32 +00:00
|
|
|
return $this->file($fieldName, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'select':
|
2010-08-05 18:14:36 +00:00
|
|
|
$options += array('options' => array(), 'value' => $selected);
|
2008-05-30 11:40:08 +00:00
|
|
|
$list = $options['options'];
|
|
|
|
unset($options['options']);
|
2012-09-28 12:50:32 +00:00
|
|
|
return $this->select($fieldName, $list, $options);
|
2010-08-05 23:31:51 +00:00
|
|
|
case 'time':
|
|
|
|
$options['value'] = $selected;
|
2012-09-28 12:50:32 +00:00
|
|
|
return $this->dateTime($fieldName, null, $timeFormat, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'date':
|
2010-08-05 23:31:51 +00:00
|
|
|
$options['value'] = $selected;
|
2012-09-28 12:50:32 +00:00
|
|
|
return $this->dateTime($fieldName, $dateFormat, null, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'datetime':
|
2010-08-05 23:31:51 +00:00
|
|
|
$options['value'] = $selected;
|
2012-09-28 12:50:32 +00:00
|
|
|
return $this->dateTime($fieldName, $dateFormat, $timeFormat, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'textarea':
|
2012-09-28 12:50:32 +00:00
|
|
|
return $this->textarea($fieldName, $options + array('cols' => '30', 'rows' => '6'));
|
2011-08-09 14:02:00 +00:00
|
|
|
case 'url':
|
2012-09-28 12:50:32 +00:00
|
|
|
return $this->text($fieldName, array('type' => 'url') + $options);
|
2010-08-17 04:13:22 +00:00
|
|
|
default:
|
2012-09-28 12:50:32 +00:00
|
|
|
return $this->{$type}($fieldName, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-09-28 12:50:32 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-09-28 12:50:32 +00:00
|
|
|
/**
|
2012-12-08 17:10:36 +00:00
|
|
|
* Generates input options array
|
2012-09-28 12:50:32 +00:00
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param array $options Options list.
|
2012-12-08 17:10:36 +00:00
|
|
|
* @return array Options
|
2012-09-28 12:50:32 +00:00
|
|
|
*/
|
2012-12-08 17:10:36 +00:00
|
|
|
protected function _parseOptions($options) {
|
2012-09-28 12:50:32 +00:00
|
|
|
$options = array_merge(
|
|
|
|
array('before' => null, 'between' => null, 'after' => null, 'format' => null),
|
|
|
|
$this->_inputDefaults,
|
|
|
|
$options
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!isset($options['type'])) {
|
|
|
|
$options = $this->_magicOptions($options);
|
|
|
|
}
|
|
|
|
|
2013-12-17 04:26:20 +00:00
|
|
|
if (in_array($options['type'], array('radio', 'select'))) {
|
2012-09-28 12:50:32 +00:00
|
|
|
$options = $this->_optionsOptions($options);
|
|
|
|
}
|
|
|
|
|
2014-09-13 02:03:33 +00:00
|
|
|
$options = $this->_maxLength($options);
|
|
|
|
|
2012-09-28 12:50:32 +00:00
|
|
|
if (isset($options['rows']) || isset($options['cols'])) {
|
|
|
|
$options['type'] = 'textarea';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time' || $options['type'] === 'select') {
|
|
|
|
$options += array('empty' => false);
|
|
|
|
}
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-08 17:10:36 +00:00
|
|
|
* Generates list of options for multiple select
|
2012-09-28 12:50:32 +00:00
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param array $options Options list.
|
2012-09-28 12:50:32 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _optionsOptions($options) {
|
|
|
|
if (isset($options['options'])) {
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
$varName = Inflector::variable(
|
|
|
|
Inflector::pluralize(preg_replace('/_id$/', '', $this->field()))
|
|
|
|
);
|
2013-07-06 17:04:06 +00:00
|
|
|
$varOptions = $this->_View->get($varName);
|
2012-09-28 12:50:32 +00:00
|
|
|
if (!is_array($varOptions)) {
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
if ($options['type'] !== 'radio') {
|
|
|
|
$options['type'] = 'select';
|
|
|
|
}
|
|
|
|
$options['options'] = $varOptions;
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-08 17:10:36 +00:00
|
|
|
* Magically set option type and corresponding options
|
2012-09-28 12:50:32 +00:00
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param array $options Options list.
|
2012-09-28 12:50:32 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _magicOptions($options) {
|
|
|
|
$modelKey = $this->model();
|
|
|
|
$fieldKey = $this->field();
|
|
|
|
$options['type'] = 'text';
|
|
|
|
if (isset($options['options'])) {
|
|
|
|
$options['type'] = 'select';
|
|
|
|
} elseif (in_array($fieldKey, array('psword', 'passwd', 'password'))) {
|
|
|
|
$options['type'] = 'password';
|
2013-01-25 21:47:45 +00:00
|
|
|
} elseif (in_array($fieldKey, array('tel', 'telephone', 'phone'))) {
|
|
|
|
$options['type'] = 'tel';
|
|
|
|
} elseif ($fieldKey === 'email') {
|
|
|
|
$options['type'] = 'email';
|
2012-09-28 12:50:32 +00:00
|
|
|
} elseif (isset($options['checked'])) {
|
|
|
|
$options['type'] = 'checkbox';
|
|
|
|
} elseif ($fieldDef = $this->_introspectModel($modelKey, 'fields', $fieldKey)) {
|
|
|
|
$type = $fieldDef['type'];
|
|
|
|
$primaryKey = $this->fieldset[$modelKey]['key'];
|
|
|
|
$map = array(
|
|
|
|
'string' => 'text', 'datetime' => 'datetime',
|
|
|
|
'boolean' => 'checkbox', 'timestamp' => 'datetime',
|
|
|
|
'text' => 'textarea', 'time' => 'time',
|
|
|
|
'date' => 'date', 'float' => 'number',
|
2013-10-15 20:25:51 +00:00
|
|
|
'integer' => 'number', 'decimal' => 'number',
|
|
|
|
'binary' => 'file'
|
2012-09-28 12:50:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($this->map[$type])) {
|
|
|
|
$options['type'] = $this->map[$type];
|
|
|
|
} elseif (isset($map[$type])) {
|
|
|
|
$options['type'] = $map[$type];
|
|
|
|
}
|
2014-04-29 12:19:33 +00:00
|
|
|
if ($fieldKey === $primaryKey) {
|
2012-09-28 12:50:32 +00:00
|
|
|
$options['type'] = 'hidden';
|
|
|
|
}
|
2014-12-09 02:17:35 +00:00
|
|
|
if ($options['type'] === 'number' &&
|
2012-09-28 12:50:32 +00:00
|
|
|
!isset($options['step'])
|
|
|
|
) {
|
2013-09-29 14:44:50 +00:00
|
|
|
if ($type === 'decimal') {
|
2014-01-30 22:07:23 +00:00
|
|
|
$decimalPlaces = substr($fieldDef['length'], strpos($fieldDef['length'], ',') + 1);
|
|
|
|
$options['step'] = sprintf('%.' . $decimalPlaces . 'F', pow(10, -1 * $decimalPlaces));
|
2013-09-29 14:44:50 +00:00
|
|
|
} elseif ($type === 'float') {
|
|
|
|
$options['step'] = 'any';
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-26 23:09:33 +00:00
|
|
|
|
2012-09-28 12:50:32 +00:00
|
|
|
if (preg_match('/_id$/', $fieldKey) && $options['type'] !== 'hidden') {
|
|
|
|
$options['type'] = 'select';
|
2010-02-26 23:09:33 +00:00
|
|
|
}
|
|
|
|
|
2012-09-28 12:50:32 +00:00
|
|
|
if ($modelKey === $fieldKey) {
|
|
|
|
$options['type'] = 'select';
|
|
|
|
if (!isset($options['multiple'])) {
|
|
|
|
$options['multiple'] = 'multiple';
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-09-28 12:50:32 +00:00
|
|
|
if (in_array($options['type'], array('text', 'number'))) {
|
|
|
|
$options = $this->_optionsOptions($options);
|
|
|
|
}
|
|
|
|
if ($options['type'] === 'select' && array_key_exists('step', $options)) {
|
|
|
|
unset($options['step']);
|
|
|
|
}
|
2014-09-13 02:03:33 +00:00
|
|
|
|
2012-09-28 12:50:32 +00:00
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-08 17:10:36 +00:00
|
|
|
* Generate format options
|
2012-09-28 12:50:32 +00:00
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param array $options Options list.
|
2012-09-28 12:50:32 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _getFormat($options) {
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($options['type'] === 'hidden') {
|
2012-09-28 12:50:32 +00:00
|
|
|
return array('input');
|
|
|
|
}
|
|
|
|
if (is_array($options['format']) && in_array('input', $options['format'])) {
|
|
|
|
return $options['format'];
|
|
|
|
}
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($options['type'] === 'checkbox') {
|
2012-09-28 12:50:32 +00:00
|
|
|
return array('before', 'input', 'between', 'label', 'after', 'error');
|
|
|
|
}
|
|
|
|
return array('before', 'label', 'between', 'input', 'after', 'error');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-08 17:10:36 +00:00
|
|
|
* Generate label for input
|
2012-09-28 12:50:32 +00:00
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param string $fieldName Field name.
|
|
|
|
* @param array $options Options list.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool|string false or Generated label element
|
2012-09-28 12:50:32 +00:00
|
|
|
*/
|
|
|
|
protected function _getLabel($fieldName, $options) {
|
|
|
|
if ($options['type'] === 'radio') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$label = null;
|
|
|
|
if (isset($options['label'])) {
|
|
|
|
$label = $options['label'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($label === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->_inputLabel($fieldName, $label, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-08 17:10:36 +00:00
|
|
|
* Calculates maxlength option
|
2012-09-28 12:50:32 +00:00
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param array $options Options list.
|
2012-09-28 12:50:32 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _maxLength($options) {
|
|
|
|
$fieldDef = $this->_introspectModel($this->model(), 'fields', $this->field());
|
|
|
|
$autoLength = (
|
|
|
|
!array_key_exists('maxlength', $options) &&
|
|
|
|
isset($fieldDef['length']) &&
|
|
|
|
is_scalar($fieldDef['length']) &&
|
2014-09-13 02:03:33 +00:00
|
|
|
$fieldDef['length'] < 1000000 &&
|
2012-09-28 12:50:32 +00:00
|
|
|
$options['type'] !== 'select'
|
|
|
|
);
|
2013-01-25 21:47:45 +00:00
|
|
|
if ($autoLength &&
|
2014-09-13 02:03:33 +00:00
|
|
|
in_array($options['type'], array('text', 'textarea', 'email', 'tel', 'url', 'search'))
|
2013-01-25 21:47:45 +00:00
|
|
|
) {
|
2015-02-06 03:28:02 +00:00
|
|
|
$options['maxlength'] = (int)$fieldDef['length'];
|
2012-09-28 12:50:32 +00:00
|
|
|
}
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-08 17:10:36 +00:00
|
|
|
* Generate div options for input
|
2012-09-28 12:50:32 +00:00
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param array $options Options list.
|
2012-09-28 12:50:32 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _divOptions($options) {
|
|
|
|
if ($options['type'] === 'hidden') {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
$div = $this->_extractOption('div', $options, true);
|
|
|
|
if (!$div) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$divOptions = array('class' => 'input');
|
|
|
|
$divOptions = $this->addClass($divOptions, $options['type']);
|
|
|
|
if (is_string($div)) {
|
|
|
|
$divOptions['class'] = $div;
|
|
|
|
} elseif (is_array($div)) {
|
|
|
|
$divOptions = array_merge($divOptions, $div);
|
|
|
|
}
|
2014-12-09 02:17:35 +00:00
|
|
|
if ($this->_extractOption('required', $options) !== false &&
|
2013-04-26 13:41:35 +00:00
|
|
|
$this->_introspectModel($this->model(), 'validates', $this->field())
|
|
|
|
) {
|
2012-09-28 12:50:32 +00:00
|
|
|
$divOptions = $this->addClass($divOptions, 'required');
|
|
|
|
}
|
|
|
|
if (!isset($divOptions['tag'])) {
|
|
|
|
$divOptions['tag'] = 'div';
|
|
|
|
}
|
|
|
|
return $divOptions;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-02-20 18:04:15 +00:00
|
|
|
/**
|
|
|
|
* Extracts a single option from an options array.
|
|
|
|
*
|
|
|
|
* @param string $name The name of the option to pull out.
|
|
|
|
* @param array $options The array of options you want to extract.
|
|
|
|
* @param mixed $default The default option value
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return mixed the contents of the option or default
|
2010-02-20 18:04:15 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _extractOption($name, $options, $default = null) {
|
2010-02-20 18:04:15 +00:00
|
|
|
if (array_key_exists($name, $options)) {
|
|
|
|
return $options[$name];
|
|
|
|
}
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
2010-03-05 03:46:43 +00:00
|
|
|
/**
|
|
|
|
* Generate a label for an input() call.
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* $options can contain a hash of id overrides. These overrides will be
|
2011-12-20 03:09:46 +00:00
|
|
|
* used instead of the generated values if present.
|
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param string $fieldName Field name.
|
|
|
|
* @param string|array $label Label text or array with text and options.
|
|
|
|
* @param array $options Options for the label element. 'NONE' option is
|
|
|
|
* deprecated and will be removed in 3.0
|
2010-03-05 03:46:43 +00:00
|
|
|
* @return string Generated label element
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _inputLabel($fieldName, $label, $options) {
|
2010-03-05 03:46:43 +00:00
|
|
|
$labelAttributes = $this->domId(array(), 'for');
|
2011-12-20 03:09:46 +00:00
|
|
|
$idKey = null;
|
2010-03-05 03:46:43 +00:00
|
|
|
if ($options['type'] === 'date' || $options['type'] === 'datetime') {
|
2011-12-20 03:09:46 +00:00
|
|
|
$firstInput = 'M';
|
2014-12-09 02:17:35 +00:00
|
|
|
if (array_key_exists('dateFormat', $options) &&
|
2011-12-20 03:09:46 +00:00
|
|
|
($options['dateFormat'] === null || $options['dateFormat'] === 'NONE')
|
|
|
|
) {
|
|
|
|
$firstInput = 'H';
|
|
|
|
} elseif (!empty($options['dateFormat'])) {
|
|
|
|
$firstInput = substr($options['dateFormat'], 0, 1);
|
2010-03-05 03:46:43 +00:00
|
|
|
}
|
2011-12-20 03:09:46 +00:00
|
|
|
switch ($firstInput) {
|
|
|
|
case 'D':
|
|
|
|
$idKey = 'day';
|
|
|
|
$labelAttributes['for'] .= 'Day';
|
|
|
|
break;
|
|
|
|
case 'Y':
|
|
|
|
$idKey = 'year';
|
|
|
|
$labelAttributes['for'] .= 'Year';
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
$idKey = 'month';
|
|
|
|
$labelAttributes['for'] .= 'Month';
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
$idKey = 'hour';
|
|
|
|
$labelAttributes['for'] .= 'Hour';
|
2010-03-05 03:46:43 +00:00
|
|
|
}
|
2011-12-20 03:09:46 +00:00
|
|
|
}
|
|
|
|
if ($options['type'] === 'time') {
|
2010-03-05 03:46:43 +00:00
|
|
|
$labelAttributes['for'] .= 'Hour';
|
2011-12-20 03:09:46 +00:00
|
|
|
$idKey = 'hour';
|
|
|
|
}
|
|
|
|
if (isset($idKey) && isset($options['id']) && isset($options['id'][$idKey])) {
|
|
|
|
$labelAttributes['for'] = $options['id'][$idKey];
|
2010-03-05 03:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($label)) {
|
|
|
|
$labelText = null;
|
|
|
|
if (isset($label['text'])) {
|
|
|
|
$labelText = $label['text'];
|
|
|
|
unset($label['text']);
|
|
|
|
}
|
|
|
|
$labelAttributes = array_merge($labelAttributes, $label);
|
|
|
|
} else {
|
|
|
|
$labelText = $label;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['id']) && is_string($options['id'])) {
|
|
|
|
$labelAttributes = array_merge($labelAttributes, array('for' => $options['id']));
|
|
|
|
}
|
|
|
|
return $this->label($fieldName, $labelText, $labelAttributes);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates a checkbox input widget.
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Options:
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2009-10-17 04:55:17 +00:00
|
|
|
* - `value` - the value of the checkbox
|
|
|
|
* - `checked` - boolean indicate that this checkbox is checked.
|
2009-10-31 05:04:33 +00:00
|
|
|
* - `hiddenField` - boolean to indicate if you want the results of checkbox() to include
|
2011-03-16 02:05:14 +00:00
|
|
|
* a hidden input with a value of ''.
|
2010-01-25 22:59:05 +00:00
|
|
|
* - `disabled` - create a disabled input.
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `default` - Set the default value for the checkbox. This allows you to start checkboxes
|
|
|
|
* as checked, without having to check the POST data. A matching POST data value, will overwrite
|
2011-03-16 02:05:14 +00:00
|
|
|
* the default value.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-12-23 08:13:00 +00:00
|
|
|
* @param string $fieldName Name of a field, like this "Modelname.fieldname"
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $options Array of HTML attributes.
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return string An HTML text input element.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function checkbox($fieldName, $options = array()) {
|
2010-06-09 15:03:23 +00:00
|
|
|
$valueOptions = array();
|
2011-11-30 15:44:11 +00:00
|
|
|
if (isset($options['default'])) {
|
2010-06-09 15:03:23 +00:00
|
|
|
$valueOptions['default'] = $options['default'];
|
|
|
|
unset($options['default']);
|
|
|
|
}
|
|
|
|
|
2014-01-31 02:12:06 +00:00
|
|
|
$options += array('value' => 1, 'required' => false);
|
2009-10-31 05:04:33 +00:00
|
|
|
$options = $this->_initInputField($fieldName, $options) + array('hiddenField' => true);
|
2010-06-09 15:03:23 +00:00
|
|
|
$value = current($this->value($valueOptions));
|
2013-01-23 15:08:24 +00:00
|
|
|
$output = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2014-12-09 02:17:35 +00:00
|
|
|
if ((!isset($options['checked']) && !empty($value) && $value == $options['value']) ||
|
2011-01-15 02:48:23 +00:00
|
|
|
!empty($options['checked'])
|
|
|
|
) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$options['checked'] = 'checked';
|
|
|
|
}
|
2009-10-31 05:04:33 +00:00
|
|
|
if ($options['hiddenField']) {
|
2009-10-30 05:34:41 +00:00
|
|
|
$hiddenOptions = array(
|
2011-12-19 07:34:19 +00:00
|
|
|
'id' => $options['id'] . '_',
|
|
|
|
'name' => $options['name'],
|
|
|
|
'value' => ($options['hiddenField'] !== true ? $options['hiddenField'] : '0'),
|
2014-03-19 02:11:57 +00:00
|
|
|
'form' => isset($options['form']) ? $options['form'] : null,
|
|
|
|
'secure' => false,
|
2009-10-30 05:34:41 +00:00
|
|
|
);
|
2012-09-14 17:54:29 +00:00
|
|
|
if (isset($options['disabled']) && $options['disabled']) {
|
2009-10-30 05:34:41 +00:00
|
|
|
$hiddenOptions['disabled'] = 'disabled';
|
|
|
|
}
|
|
|
|
$output = $this->hidden($fieldName, $hiddenOptions);
|
2009-03-25 16:58:29 +00:00
|
|
|
}
|
2009-10-30 05:34:41 +00:00
|
|
|
unset($options['hiddenField']);
|
2008-09-21 04:06:37 +00:00
|
|
|
|
2013-03-29 14:24:08 +00:00
|
|
|
return $output . $this->Html->useTag('checkbox', $options['name'], array_diff_key($options, array('name' => null)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 22:59:05 +00:00
|
|
|
* Creates a set of radio widgets. Will create a legend and fieldset
|
2012-12-22 22:48:15 +00:00
|
|
|
* by default. Use $options to control this
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Attributes:
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2009-10-17 04:55:17 +00:00
|
|
|
* - `separator` - define the string in between the radio buttons
|
2012-09-20 22:06:26 +00:00
|
|
|
* - `between` - the string between legend and input set or array of strings to insert
|
|
|
|
* strings between each input block
|
2009-10-17 04:55:17 +00:00
|
|
|
* - `legend` - control whether or not the widget set has a fieldset & legend
|
|
|
|
* - `value` - indicate a value that is should be checked
|
|
|
|
* - `label` - boolean to indicate whether or not labels for widgets show be displayed
|
2009-10-31 04:58:21 +00:00
|
|
|
* - `hiddenField` - boolean to indicate if you want the results of radio() to include
|
2011-10-24 02:27:07 +00:00
|
|
|
* a hidden input with a value of ''. This is useful for creating radio sets that non-continuous
|
2012-03-04 14:59:40 +00:00
|
|
|
* - `disabled` - Set to `true` or `disabled` to disable all the radio buttons.
|
2014-07-30 20:11:03 +00:00
|
|
|
* - `empty` - Set to `true` to create an input with the value '' as the first option. When `true`
|
2012-12-22 22:48:15 +00:00
|
|
|
* the radio label will be 'empty'. Set this option to a string to control the label value.
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2009-02-18 01:39:01 +00:00
|
|
|
* @param string $fieldName Name of a field, like this "Modelname.fieldname"
|
|
|
|
* @param array $options Radio button options array.
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param array $attributes Array of HTML attributes, and special attributes above.
|
|
|
|
* @return string Completed radio widget set.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function radio($fieldName, $options = array(), $attributes = array()) {
|
2015-01-09 04:20:30 +00:00
|
|
|
$attributes['options'] = $options;
|
2008-09-21 04:06:37 +00:00
|
|
|
$attributes = $this->_initInputField($fieldName, $attributes);
|
2015-01-09 04:20:30 +00:00
|
|
|
unset($attributes['options']);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-03-21 18:54:48 +00:00
|
|
|
$showEmpty = $this->_extractOption('empty', $attributes);
|
|
|
|
if ($showEmpty) {
|
2012-12-09 18:25:00 +00:00
|
|
|
$showEmpty = ($showEmpty === true) ? __d('cake', 'empty') : $showEmpty;
|
2012-04-07 01:46:34 +00:00
|
|
|
$options = array('' => $showEmpty) + $options;
|
2012-03-21 18:54:48 +00:00
|
|
|
}
|
2012-03-21 19:06:11 +00:00
|
|
|
unset($attributes['empty']);
|
2012-03-21 18:54:48 +00:00
|
|
|
|
2011-12-18 16:25:42 +00:00
|
|
|
$legend = false;
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($attributes['legend'])) {
|
|
|
|
$legend = $attributes['legend'];
|
|
|
|
unset($attributes['legend']);
|
|
|
|
} elseif (count($options) > 1) {
|
2011-03-19 17:29:31 +00:00
|
|
|
$legend = __(Inflector::humanize($this->field()));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 16:25:42 +00:00
|
|
|
$label = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($attributes['label'])) {
|
|
|
|
$label = $attributes['label'];
|
|
|
|
unset($attributes['label']);
|
|
|
|
}
|
|
|
|
|
2011-12-18 16:25:42 +00:00
|
|
|
$separator = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($attributes['separator'])) {
|
2011-12-18 16:25:42 +00:00
|
|
|
$separator = $attributes['separator'];
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($attributes['separator']);
|
|
|
|
}
|
|
|
|
|
2011-12-18 16:25:42 +00:00
|
|
|
$between = null;
|
|
|
|
if (isset($attributes['between'])) {
|
|
|
|
$between = $attributes['between'];
|
|
|
|
unset($attributes['between']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($attributes['value'])) {
|
|
|
|
$value = $attributes['value'];
|
|
|
|
} else {
|
2011-12-18 16:25:42 +00:00
|
|
|
$value = $this->value($fieldName);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-03-13 23:35:37 +00:00
|
|
|
|
2011-12-18 16:25:42 +00:00
|
|
|
$disabled = array();
|
2011-03-13 23:35:37 +00:00
|
|
|
if (isset($attributes['disabled'])) {
|
|
|
|
$disabled = $attributes['disabled'];
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$out = array();
|
|
|
|
|
2009-10-31 04:08:49 +00:00
|
|
|
$hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
|
|
|
|
unset($attributes['hiddenField']);
|
|
|
|
|
2013-01-08 02:12:53 +00:00
|
|
|
if (isset($value) && is_bool($value)) {
|
|
|
|
$value = $value ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2013-12-04 01:14:08 +00:00
|
|
|
$this->_domIdSuffixes = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($options as $optValue => $optTitle) {
|
2013-09-25 12:01:35 +00:00
|
|
|
$optionsHere = array('value' => $optValue, 'disabled' => false);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-09-19 14:14:29 +00:00
|
|
|
if (isset($value) && strval($optValue) === strval($value)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$optionsHere['checked'] = 'checked';
|
|
|
|
}
|
2013-02-07 14:36:00 +00:00
|
|
|
$isNumeric = is_numeric($optValue);
|
|
|
|
if ($disabled && (!is_array($disabled) || in_array((string)$optValue, $disabled, !$isNumeric))) {
|
2011-03-13 23:35:37 +00:00
|
|
|
$optionsHere['disabled'] = true;
|
|
|
|
}
|
2013-12-04 01:14:08 +00:00
|
|
|
$tagName = $attributes['id'] . $this->domIdSuffix($optValue);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($label) {
|
2013-09-17 02:38:07 +00:00
|
|
|
$labelOpts = is_array($label) ? $label : array();
|
|
|
|
$labelOpts += array('for' => $tagName);
|
|
|
|
$optTitle = $this->label($tagName, $optTitle, $labelOpts);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-09-05 14:21:24 +00:00
|
|
|
|
2012-09-20 22:06:26 +00:00
|
|
|
if (is_array($between)) {
|
|
|
|
$optTitle .= array_shift($between);
|
|
|
|
}
|
2011-01-23 14:26:14 +00:00
|
|
|
$allOptions = array_merge($attributes, $optionsHere);
|
|
|
|
$out[] = $this->Html->useTag('radio', $attributes['name'], $tagName,
|
2013-03-29 14:24:08 +00:00
|
|
|
array_diff_key($allOptions, array('name' => null, 'type' => null, 'id' => null)),
|
2011-01-23 14:26:14 +00:00
|
|
|
$optTitle
|
2008-09-21 04:06:37 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$hidden = null;
|
|
|
|
|
2009-10-30 05:34:41 +00:00
|
|
|
if ($hiddenField) {
|
|
|
|
if (!isset($value) || $value === '') {
|
|
|
|
$hidden = $this->hidden($fieldName, array(
|
2014-03-19 02:11:57 +00:00
|
|
|
'form' => isset($attributes['form']) ? $attributes['form'] : null,
|
|
|
|
'id' => $attributes['id'] . '_',
|
|
|
|
'value' => '',
|
|
|
|
'name' => $attributes['name']
|
2009-10-30 05:34:41 +00:00
|
|
|
));
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-12-18 16:25:42 +00:00
|
|
|
$out = $hidden . implode($separator, $out);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-09-20 22:06:26 +00:00
|
|
|
if (is_array($between)) {
|
|
|
|
$between = '';
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($legend) {
|
2011-12-18 16:25:42 +00:00
|
|
|
$out = $this->Html->useTag('fieldset', '', $this->Html->useTag('legend', $legend) . $between . $out);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-11-21 21:43:54 +00:00
|
|
|
return $out;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-08-17 04:06:23 +00:00
|
|
|
/**
|
2011-03-19 17:29:31 +00:00
|
|
|
* Missing method handler - implements various simple input types. Is used to create inputs
|
2012-12-22 22:48:15 +00:00
|
|
|
* of various types. e.g. `$this->Form->text();` will create `<input type="text" />` while
|
2010-08-17 04:06:23 +00:00
|
|
|
* `$this->Form->range();` will create `<input type="range" />`
|
|
|
|
*
|
2010-08-17 04:08:41 +00:00
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* `$this->Form->search('User.query', array('value' => 'test'));`
|
|
|
|
*
|
|
|
|
* Will make an input like:
|
|
|
|
*
|
|
|
|
* `<input type="search" id="UserQuery" name="data[User][query]" value="test" />`
|
|
|
|
*
|
|
|
|
* The first argument to an input type should always be the fieldname, in `Model.field` format.
|
|
|
|
* The second argument should always be an array of attributes for the input.
|
|
|
|
*
|
2010-12-12 00:01:07 +00:00
|
|
|
* @param string $method Method name / input type to make.
|
2010-08-17 04:06:23 +00:00
|
|
|
* @param array $params Parameters for the method call
|
|
|
|
* @return string Formatted input method.
|
2010-12-12 00:01:07 +00:00
|
|
|
* @throws CakeException When there are no params for the method call.
|
2010-08-17 04:06:23 +00:00
|
|
|
*/
|
|
|
|
public function __call($method, $params) {
|
|
|
|
$options = array();
|
|
|
|
if (empty($params)) {
|
2011-03-20 15:35:43 +00:00
|
|
|
throw new CakeException(__d('cake_dev', 'Missing field name for FormHelper::%s', $method));
|
2010-08-17 04:06:23 +00:00
|
|
|
}
|
|
|
|
if (isset($params[1])) {
|
|
|
|
$options = $params[1];
|
|
|
|
}
|
2010-08-17 04:13:22 +00:00
|
|
|
if (!isset($options['type'])) {
|
|
|
|
$options['type'] = $method;
|
|
|
|
}
|
2010-08-17 04:06:23 +00:00
|
|
|
$options = $this->_initInputField($params[0], $options);
|
2013-03-29 14:24:08 +00:00
|
|
|
return $this->Html->useTag('input', $options['name'], array_diff_key($options, array('name' => null)));
|
2010-08-17 04:06:23 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates a textarea widget.
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Options:
|
|
|
|
*
|
|
|
|
* - `escape` - Whether or not the contents of the textarea should be escaped. Defaults to true.
|
|
|
|
*
|
2009-02-18 01:39:01 +00:00
|
|
|
* @param string $fieldName Name of a field, in the form "Modelname.fieldname"
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param array $options Array of HTML attributes, and special options above.
|
|
|
|
* @return string A generated HTML text input element
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::textarea
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function textarea($fieldName, $options = array()) {
|
2008-09-21 04:06:37 +00:00
|
|
|
$options = $this->_initInputField($fieldName, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
$value = null;
|
|
|
|
|
|
|
|
if (array_key_exists('value', $options)) {
|
|
|
|
$value = $options['value'];
|
|
|
|
if (!array_key_exists('escape', $options) || $options['escape'] !== false) {
|
|
|
|
$value = h($value);
|
|
|
|
}
|
|
|
|
unset($options['value']);
|
|
|
|
}
|
2013-03-29 14:24:08 +00:00
|
|
|
return $this->Html->useTag('textarea', $options['name'], array_diff_key($options, array('type' => null, 'name' => null)), $value);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates a hidden input field.
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param string $fieldName Name of a field, in the form of "Modelname.fieldname"
|
2009-02-18 01:39:01 +00:00
|
|
|
* @param array $options Array of HTML attributes.
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return string A generated hidden input
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::hidden
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function hidden($fieldName, $options = array()) {
|
2013-10-04 15:05:38 +00:00
|
|
|
$options += array('required' => false, 'secure' => true);
|
|
|
|
|
|
|
|
$secure = $options['secure'];
|
|
|
|
unset($options['secure']);
|
2008-09-24 23:02:14 +00:00
|
|
|
|
2008-09-21 04:06:37 +00:00
|
|
|
$options = $this->_initInputField($fieldName, array_merge(
|
2011-06-09 01:14:22 +00:00
|
|
|
$options, array('secure' => self::SECURE_SKIP)
|
2008-09-21 04:06:37 +00:00
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2013-10-04 15:05:38 +00:00
|
|
|
if ($secure === true) {
|
2011-08-20 05:39:30 +00:00
|
|
|
$this->_secure(true, null, '' . $options['value']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 14:24:08 +00:00
|
|
|
return $this->Html->useTag('hidden', $options['name'], array_diff_key($options, array('name' => null)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates file input widget.
|
|
|
|
*
|
2008-09-24 23:02:14 +00:00
|
|
|
* @param string $fieldName Name of a field, in the form "Modelname.fieldname"
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $options Array of HTML attributes.
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return string A generated file input.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::file
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function file($fieldName, $options = array()) {
|
2011-06-09 01:14:22 +00:00
|
|
|
$options += array('secure' => true);
|
|
|
|
$secure = $options['secure'];
|
|
|
|
$options['secure'] = self::SECURE_SKIP;
|
|
|
|
|
2008-09-21 04:06:37 +00:00
|
|
|
$options = $this->_initInputField($fieldName, $options);
|
2011-06-26 00:02:34 +00:00
|
|
|
$field = $this->entity();
|
2008-09-29 16:23:39 +00:00
|
|
|
|
|
|
|
foreach (array('name', 'type', 'tmp_name', 'error', 'size') as $suffix) {
|
2011-08-20 05:39:30 +00:00
|
|
|
$this->_secure($secure, array_merge($field, array($suffix)));
|
2008-09-29 16:23:39 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 14:24:08 +00:00
|
|
|
$exclude = array('name' => null, 'value' => null);
|
2012-12-09 18:31:01 +00:00
|
|
|
return $this->Html->useTag('file', $options['name'], array_diff_key($options, $exclude));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Creates a `<button>` tag. The type attribute defaults to `type="submit"`
|
2010-02-13 04:58:35 +00:00
|
|
|
* You can change it to a different value by using `$options['type']`.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Options:
|
2009-10-20 03:57:23 +00:00
|
|
|
*
|
2009-10-22 03:45:42 +00:00
|
|
|
* - `escape` - HTML entity encode the $title of the button. Defaults to false.
|
2009-10-20 03:57:23 +00:00
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param string $title The button's caption. Not automatically HTML encoded
|
2009-10-20 03:57:23 +00:00
|
|
|
* @param array $options Array of options and HTML attributes.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string A HTML button tag.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::button
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function button($title, $options = array()) {
|
2011-06-15 02:37:02 +00:00
|
|
|
$options += array('type' => 'submit', 'escape' => false, 'secure' => false);
|
2009-10-20 03:57:23 +00:00
|
|
|
if ($options['escape']) {
|
|
|
|
$title = h($title);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-06-15 02:37:02 +00:00
|
|
|
if (isset($options['name'])) {
|
2011-11-19 16:43:09 +00:00
|
|
|
$name = str_replace(array('[', ']'), array('.', ''), $options['name']);
|
|
|
|
$this->_secure($options['secure'], $name);
|
2011-06-15 02:37:02 +00:00
|
|
|
}
|
2012-02-14 02:00:57 +00:00
|
|
|
return $this->Html->useTag('button', $options, $title);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-18 19:53:27 +00:00
|
|
|
/**
|
2011-05-13 02:41:35 +00:00
|
|
|
* Create a `<button>` tag with a surrounding `<form>` that submits via POST.
|
2010-10-18 19:53:27 +00:00
|
|
|
*
|
2012-04-18 00:46:19 +00:00
|
|
|
* This method creates a `<form>` element. So do not use this method in an already opened form.
|
2011-05-13 02:41:35 +00:00
|
|
|
* Instead use FormHelper::submit() or FormHelper::button() to create buttons inside opened forms.
|
2010-10-30 02:32:14 +00:00
|
|
|
*
|
2010-10-18 19:53:27 +00:00
|
|
|
* ### Options:
|
|
|
|
*
|
|
|
|
* - `data` - Array with key/value to pass in input hidden
|
|
|
|
* - Other options is the same of button method.
|
|
|
|
*
|
|
|
|
* @param string $title The button's caption. Not automatically HTML encoded
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $url URL as string or array
|
2010-10-18 19:53:27 +00:00
|
|
|
* @param array $options Array of options and HTML attributes.
|
|
|
|
* @return string A HTML button tag.
|
2011-11-30 05:23:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::postButton
|
2010-10-18 19:53:27 +00:00
|
|
|
*/
|
|
|
|
public function postButton($title, $url, $options = array()) {
|
2012-04-18 00:46:19 +00:00
|
|
|
$out = $this->create(false, array('id' => false, 'url' => $url));
|
2010-10-18 19:53:27 +00:00
|
|
|
if (isset($options['data']) && is_array($options['data'])) {
|
2014-02-25 02:17:31 +00:00
|
|
|
foreach (Hash::flatten($options['data']) as $key => $value) {
|
2010-10-18 19:53:27 +00:00
|
|
|
$out .= $this->hidden($key, array('value' => $value, 'id' => false));
|
|
|
|
}
|
|
|
|
unset($options['data']);
|
|
|
|
}
|
|
|
|
$out .= $this->button($title, $options);
|
|
|
|
$out .= $this->end();
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2010-10-18 23:51:17 +00:00
|
|
|
/**
|
2013-10-08 03:17:58 +00:00
|
|
|
* Creates an HTML link, but access the URL using the method you specify (defaults to POST).
|
2011-05-13 02:41:35 +00:00
|
|
|
* Requires javascript to be enabled in browser.
|
2010-10-18 23:51:17 +00:00
|
|
|
*
|
2011-05-13 02:41:35 +00:00
|
|
|
* This method creates a `<form>` element. So do not use this method inside an existing form.
|
|
|
|
* Instead you should add a submit button using FormHelper::submit()
|
2010-10-30 02:32:14 +00:00
|
|
|
*
|
2010-10-18 23:51:17 +00:00
|
|
|
* ### Options:
|
|
|
|
*
|
|
|
|
* - `data` - Array with key/value to pass in input hidden
|
2013-06-12 16:14:02 +00:00
|
|
|
* - `method` - Request method to use. Set to 'delete' to simulate HTTP/1.1 DELETE request. Defaults to 'post'.
|
2011-05-13 02:41:35 +00:00
|
|
|
* - `confirm` - Can be used instead of $confirmMessage.
|
2013-11-03 17:29:10 +00:00
|
|
|
* - `inline` - Whether or not the associated form tag should be output inline.
|
|
|
|
* Set to false to have the form tag appended to the 'postLink' view block.
|
|
|
|
* Defaults to true.
|
|
|
|
* - `block` - Choose a custom block to append the form tag to. Using this option
|
|
|
|
* will override the inline option.
|
|
|
|
* - Other options are the same of HtmlHelper::link() method.
|
2010-10-18 23:51:17 +00:00
|
|
|
* - The option `onclick` will be replaced.
|
|
|
|
*
|
|
|
|
* @param string $title The content to be wrapped by <a> tags.
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
|
2010-10-18 23:51:17 +00:00
|
|
|
* @param array $options Array of HTML attributes.
|
2014-07-10 18:01:51 +00:00
|
|
|
* @param bool|string $confirmMessage JavaScript confirmation message. This
|
|
|
|
* argument is deprecated as of 2.6. Use `confirm` key in $options instead.
|
2010-10-18 23:51:17 +00:00
|
|
|
* @return string An `<a />` element.
|
2011-11-30 05:23:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::postLink
|
2010-10-18 23:51:17 +00:00
|
|
|
*/
|
|
|
|
public function postLink($title, $url = null, $options = array(), $confirmMessage = false) {
|
2014-05-13 18:51:39 +00:00
|
|
|
$options = (array)$options + array('inline' => true, 'block' => null);
|
2013-11-03 17:29:10 +00:00
|
|
|
if (!$options['inline'] && empty($options['block'])) {
|
|
|
|
$options['block'] = __FUNCTION__;
|
|
|
|
}
|
|
|
|
unset($options['inline']);
|
|
|
|
|
2012-09-04 00:09:13 +00:00
|
|
|
$requestMethod = 'POST';
|
2012-09-02 17:50:08 +00:00
|
|
|
if (!empty($options['method'])) {
|
|
|
|
$requestMethod = strtoupper($options['method']);
|
|
|
|
unset($options['method']);
|
|
|
|
}
|
2010-10-18 23:51:17 +00:00
|
|
|
if (!empty($options['confirm'])) {
|
|
|
|
$confirmMessage = $options['confirm'];
|
|
|
|
unset($options['confirm']);
|
|
|
|
}
|
|
|
|
|
2013-05-01 13:46:57 +00:00
|
|
|
$formName = str_replace('.', '', uniqid('post_', true));
|
2011-07-19 00:33:45 +00:00
|
|
|
$formUrl = $this->url($url);
|
2012-12-12 10:23:41 +00:00
|
|
|
$formOptions = array(
|
2012-09-04 00:09:13 +00:00
|
|
|
'name' => $formName,
|
|
|
|
'id' => $formName,
|
|
|
|
'style' => 'display:none;',
|
2012-12-12 10:23:41 +00:00
|
|
|
'method' => 'post',
|
|
|
|
);
|
|
|
|
if (isset($options['target'])) {
|
|
|
|
$formOptions['target'] = $options['target'];
|
|
|
|
unset($options['target']);
|
|
|
|
}
|
|
|
|
|
2014-05-19 02:40:21 +00:00
|
|
|
$this->_lastAction($url);
|
2014-04-29 15:23:52 +00:00
|
|
|
|
2012-12-12 10:23:41 +00:00
|
|
|
$out = $this->Html->useTag('form', $formUrl, $formOptions);
|
2012-09-04 00:09:13 +00:00
|
|
|
$out .= $this->Html->useTag('hidden', '_method', array(
|
|
|
|
'value' => $requestMethod
|
|
|
|
));
|
2011-08-11 01:53:33 +00:00
|
|
|
$out .= $this->_csrfField();
|
|
|
|
|
|
|
|
$fields = array();
|
2010-10-18 23:51:17 +00:00
|
|
|
if (isset($options['data']) && is_array($options['data'])) {
|
2014-02-25 02:17:31 +00:00
|
|
|
foreach (Hash::flatten($options['data']) as $key => $value) {
|
2011-08-11 01:53:33 +00:00
|
|
|
$fields[$key] = $value;
|
2010-10-18 23:51:17 +00:00
|
|
|
$out .= $this->hidden($key, array('value' => $value, 'id' => false));
|
|
|
|
}
|
|
|
|
unset($options['data']);
|
|
|
|
}
|
2011-08-11 01:53:33 +00:00
|
|
|
$out .= $this->secure($fields);
|
2011-07-19 00:33:45 +00:00
|
|
|
$out .= $this->Html->useTag('formend');
|
2010-10-18 23:51:17 +00:00
|
|
|
|
2013-11-03 17:29:10 +00:00
|
|
|
if ($options['block']) {
|
|
|
|
$this->_View->append($options['block'], $out);
|
|
|
|
$out = '';
|
|
|
|
}
|
|
|
|
unset($options['block']);
|
|
|
|
|
2010-10-18 23:51:17 +00:00
|
|
|
$url = '#';
|
|
|
|
$onClick = 'document.' . $formName . '.submit();';
|
|
|
|
if ($confirmMessage) {
|
2013-08-13 08:11:22 +00:00
|
|
|
$options['onclick'] = $this->_confirm($confirmMessage, $onClick, '', $options);
|
2010-10-18 23:51:17 +00:00
|
|
|
} else {
|
2013-08-05 14:20:53 +00:00
|
|
|
$options['onclick'] = $onClick . ' ';
|
2010-10-18 23:51:17 +00:00
|
|
|
}
|
2013-08-05 14:20:53 +00:00
|
|
|
$options['onclick'] .= 'event.returnValue = false; return false;';
|
2010-10-18 23:51:17 +00:00
|
|
|
|
|
|
|
$out .= $this->Html->link($title, $url, $options);
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Creates a submit button element. This method will generate `<input />` elements that
|
|
|
|
* can be used to submit, and reset forms by using $options. image submits can be created by supplying an
|
2010-02-25 03:58:09 +00:00
|
|
|
* image path for $caption.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-02-25 03:45:49 +00:00
|
|
|
* ### Options
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `div` - Include a wrapping div? Defaults to true. Accepts sub options similar to
|
2010-02-25 03:45:49 +00:00
|
|
|
* FormHelper::input().
|
2010-02-25 03:58:09 +00:00
|
|
|
* - `before` - Content to include before the input.
|
|
|
|
* - `after` - Content to include after the input.
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `type` - Set to 'reset' for reset inputs. Defaults to 'submit'
|
2010-02-25 03:45:49 +00:00
|
|
|
* - Other attributes will be assigned to the input element.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-02-25 03:45:49 +00:00
|
|
|
* ### Options
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `div` - Include a wrapping div? Defaults to true. Accepts sub options similar to
|
2010-02-25 03:45:49 +00:00
|
|
|
* FormHelper::input().
|
|
|
|
* - Other attributes will be assigned to the input element.
|
|
|
|
*
|
2009-02-03 18:48:28 +00:00
|
|
|
* @param string $caption The label appearing on the button OR if string contains :// or the
|
|
|
|
* extension .jpg, .jpe, .jpeg, .gif, .png use an image if the extension
|
|
|
|
* exists, AND the first character is /, image is relative to webroot,
|
|
|
|
* OR if the first character is not /, image is relative to webroot/img.
|
2012-12-22 22:48:15 +00:00
|
|
|
* @param array $options Array of options. See above.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string A HTML submit button
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::submit
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function submit($caption = null, $options = array()) {
|
2011-03-03 01:27:02 +00:00
|
|
|
if (!is_string($caption) && empty($caption)) {
|
2011-10-23 18:19:14 +00:00
|
|
|
$caption = __d('cake', 'Submit');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-29 01:58:27 +00:00
|
|
|
$out = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
$div = true;
|
|
|
|
|
|
|
|
if (isset($options['div'])) {
|
|
|
|
$div = $options['div'];
|
|
|
|
unset($options['div']);
|
|
|
|
}
|
2011-06-15 02:37:02 +00:00
|
|
|
$options += array('type' => 'submit', 'before' => null, 'after' => null, 'secure' => false);
|
2008-05-30 15:44:39 +00:00
|
|
|
$divOptions = array('tag' => 'div');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($div === true) {
|
|
|
|
$divOptions['class'] = 'submit';
|
|
|
|
} elseif ($div === false) {
|
|
|
|
unset($divOptions);
|
|
|
|
} elseif (is_string($div)) {
|
|
|
|
$divOptions['class'] = $div;
|
|
|
|
} elseif (is_array($div)) {
|
2008-05-30 15:44:39 +00:00
|
|
|
$divOptions = array_merge(array('class' => 'submit', 'tag' => 'div'), $div);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-21 04:06:37 +00:00
|
|
|
|
2011-06-15 02:37:02 +00:00
|
|
|
if (isset($options['name'])) {
|
2011-11-24 01:40:25 +00:00
|
|
|
$name = str_replace(array('[', ']'), array('.', ''), $options['name']);
|
|
|
|
$this->_secure($options['secure'], $name);
|
2011-06-15 02:37:02 +00:00
|
|
|
}
|
|
|
|
unset($options['secure']);
|
|
|
|
|
2009-10-31 20:28:43 +00:00
|
|
|
$before = $options['before'];
|
|
|
|
$after = $options['after'];
|
|
|
|
unset($options['before'], $options['after']);
|
2009-10-31 19:50:49 +00:00
|
|
|
|
2011-09-28 01:14:36 +00:00
|
|
|
$isUrl = strpos($caption, '://') !== false;
|
|
|
|
$isImage = preg_match('/\.(jpg|jpe|jpeg|gif|png|ico)$/', $caption);
|
|
|
|
|
|
|
|
if ($isUrl || $isImage) {
|
|
|
|
$unlockFields = array('x', 'y');
|
|
|
|
if (isset($options['name'])) {
|
|
|
|
$unlockFields = array(
|
|
|
|
$options['name'] . '_x', $options['name'] . '_y'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
foreach ($unlockFields as $ignore) {
|
|
|
|
$this->unlockField($ignore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($isUrl) {
|
2009-10-20 03:44:50 +00:00
|
|
|
unset($options['type']);
|
2011-09-28 01:14:36 +00:00
|
|
|
$tag = $this->Html->useTag('submitimage', $caption, $options);
|
|
|
|
} elseif ($isImage) {
|
2009-10-20 03:44:50 +00:00
|
|
|
unset($options['type']);
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($caption{0} !== '/') {
|
2013-07-31 14:36:57 +00:00
|
|
|
$url = $this->webroot(Configure::read('App.imageBaseUrl') . $caption);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-09-28 01:14:36 +00:00
|
|
|
$url = $this->webroot(trim($caption, '/'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-10-17 15:05:06 +00:00
|
|
|
$url = $this->assetTimestamp($url);
|
2011-09-28 01:14:36 +00:00
|
|
|
$tag = $this->Html->useTag('submitimage', $url, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$options['value'] = $caption;
|
2011-09-28 01:14:36 +00:00
|
|
|
$tag = $this->Html->useTag('submit', $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-09-28 01:14:36 +00:00
|
|
|
$out = $before . $tag . $after;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (isset($divOptions)) {
|
2008-06-20 11:57:56 +00:00
|
|
|
$tag = $divOptions['tag'];
|
|
|
|
unset($divOptions['tag']);
|
2008-05-30 15:44:39 +00:00
|
|
|
$out = $this->Html->tag($tag, $out, $divOptions);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-20 11:57:56 +00:00
|
|
|
/**
|
2008-05-30 11:40:08 +00:00
|
|
|
* Returns a formatted SELECT element.
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Attributes:
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2009-10-17 04:55:17 +00:00
|
|
|
* - `showParents` - If included in the array and set to true, an additional option element
|
2010-03-20 23:43:52 +00:00
|
|
|
* will be added for the parent of each option group. You can set an option with the same name
|
|
|
|
* and it's key will be used for the value of the option.
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `multiple` - show a multiple select box. If set to 'checkbox' multiple checkboxes will be
|
2009-02-03 18:48:28 +00:00
|
|
|
* created instead.
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
2009-10-17 05:29:16 +00:00
|
|
|
* that string is displayed as the empty element.
|
2010-01-25 22:59:05 +00:00
|
|
|
* - `escape` - If true contents of options will be HTML entity encoded. Defaults to true.
|
2010-10-30 03:56:22 +00:00
|
|
|
* - `value` The selected value of the input.
|
2013-10-23 02:59:50 +00:00
|
|
|
* - `class` - When using multiple = checkbox the class name to apply to the divs. Defaults to 'checkbox'.
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `disabled` - Control the disabled attribute. When creating a select box, set to true to disable the
|
|
|
|
* select box. When creating checkboxes, `true` will disable all checkboxes. You can also set disabled
|
2012-08-03 01:34:45 +00:00
|
|
|
* to a list of values you want to disable when creating checkboxes.
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2010-03-20 23:43:52 +00:00
|
|
|
* ### Using options
|
|
|
|
*
|
|
|
|
* A simple array will create normal options:
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2010-03-20 23:43:52 +00:00
|
|
|
* $options = array(1 => 'one', 2 => 'two);
|
|
|
|
* $this->Form->select('Model.field', $options));
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2010-03-20 23:43:52 +00:00
|
|
|
*
|
|
|
|
* While a nested options array will create optgroups with options inside them.
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2010-03-20 23:43:52 +00:00
|
|
|
* $options = array(
|
2012-08-03 01:34:45 +00:00
|
|
|
* 1 => 'bill',
|
|
|
|
* 'fred' => array(
|
|
|
|
* 2 => 'fred',
|
|
|
|
* 3 => 'fred jr.'
|
|
|
|
* )
|
2010-03-20 23:43:52 +00:00
|
|
|
* );
|
|
|
|
* $this->Form->select('Model.field', $options);
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2010-03-20 23:43:52 +00:00
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* In the above `2 => 'fred'` will not generate an option element. You should enable the `showParents`
|
2010-03-20 23:43:52 +00:00
|
|
|
* attribute to show the fred option.
|
|
|
|
*
|
2011-10-24 02:27:07 +00:00
|
|
|
* If you have multiple options that need to have the same value attribute, you can
|
|
|
|
* use an array of arrays to express this:
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-10-24 02:27:07 +00:00
|
|
|
* $options = array(
|
2012-08-03 01:34:45 +00:00
|
|
|
* array('name' => 'United states', 'value' => 'USA'),
|
|
|
|
* array('name' => 'USA', 'value' => 'USA'),
|
2011-10-24 02:27:07 +00:00
|
|
|
* );
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-10-24 02:27:07 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $fieldName Name attribute of the SELECT
|
2008-09-24 23:02:14 +00:00
|
|
|
* @param array $options Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the
|
2010-08-05 23:31:51 +00:00
|
|
|
* SELECT element
|
2009-02-03 18:48:28 +00:00
|
|
|
* @param array $attributes The HTML attributes of the select element.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Formatted SELECT element
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-08-05 18:14:36 +00:00
|
|
|
public function select($fieldName, $options = array(), $attributes = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$select = array();
|
|
|
|
$style = null;
|
|
|
|
$tag = null;
|
2010-10-16 17:40:27 +00:00
|
|
|
$attributes += array(
|
2011-03-19 17:29:31 +00:00
|
|
|
'class' => null,
|
2010-10-16 17:40:27 +00:00
|
|
|
'escape' => true,
|
2011-06-09 01:14:22 +00:00
|
|
|
'secure' => true,
|
2010-10-16 17:40:27 +00:00
|
|
|
'empty' => '',
|
2011-03-21 13:48:53 +00:00
|
|
|
'showParents' => false,
|
2012-07-23 14:39:46 +00:00
|
|
|
'hiddenField' => true,
|
|
|
|
'disabled' => false
|
2010-10-16 17:40:27 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$escapeOptions = $this->_extractOption('escape', $attributes);
|
|
|
|
$secure = $this->_extractOption('secure', $attributes);
|
|
|
|
$showEmpty = $this->_extractOption('empty', $attributes);
|
|
|
|
$showParents = $this->_extractOption('showParents', $attributes);
|
2011-03-21 13:48:53 +00:00
|
|
|
$hiddenField = $this->_extractOption('hiddenField', $attributes);
|
|
|
|
unset($attributes['escape'], $attributes['secure'], $attributes['empty'], $attributes['showParents'], $attributes['hiddenField']);
|
2011-06-07 02:32:55 +00:00
|
|
|
$id = $this->_extractOption('id', $attributes);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-09-21 04:06:37 +00:00
|
|
|
$attributes = $this->_initInputField($fieldName, array_merge(
|
2011-06-09 01:14:22 +00:00
|
|
|
(array)$attributes, array('secure' => self::SECURE_SKIP)
|
2008-09-21 04:06:37 +00:00
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-08-20 04:43:34 +00:00
|
|
|
if (is_string($options) && isset($this->_options[$options])) {
|
2011-08-20 05:39:30 +00:00
|
|
|
$options = $this->_generateOptions($options);
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (!is_array($options)) {
|
|
|
|
$options = array();
|
|
|
|
}
|
|
|
|
if (isset($attributes['type'])) {
|
|
|
|
unset($attributes['type']);
|
|
|
|
}
|
|
|
|
|
2011-04-13 04:32:15 +00:00
|
|
|
if (!empty($attributes['multiple'])) {
|
2008-12-23 08:13:00 +00:00
|
|
|
$style = ($attributes['multiple'] === 'checkbox') ? 'checkbox' : null;
|
|
|
|
$template = ($style) ? 'checkboxmultiplestart' : 'selectmultiplestart';
|
2011-01-23 14:26:14 +00:00
|
|
|
$tag = $template;
|
2011-03-21 13:48:53 +00:00
|
|
|
if ($hiddenField) {
|
|
|
|
$hiddenAttributes = array(
|
|
|
|
'value' => '',
|
|
|
|
'id' => $attributes['id'] . ($style ? '' : '_'),
|
|
|
|
'secure' => false,
|
2014-03-19 02:11:57 +00:00
|
|
|
'form' => isset($attributes['form']) ? $attributes['form'] : null,
|
2014-10-24 16:25:31 +00:00
|
|
|
'name' => $attributes['name'],
|
2014-10-25 19:05:37 +00:00
|
|
|
'disabled' => $attributes['disabled'] === true || $attributes['disabled'] === 'disabled'
|
2011-03-21 13:48:53 +00:00
|
|
|
);
|
|
|
|
$select[] = $this->hidden(null, $hiddenAttributes);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-11-30 05:23:02 +00:00
|
|
|
$tag = 'selectstart';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2013-10-04 15:05:38 +00:00
|
|
|
if ($tag === 'checkboxmultiplestart') {
|
|
|
|
unset($attributes['required']);
|
2013-04-15 18:32:04 +00:00
|
|
|
}
|
|
|
|
|
2008-12-25 22:13:06 +00:00
|
|
|
if (!empty($tag) || isset($template)) {
|
2012-08-27 02:53:48 +00:00
|
|
|
$hasOptions = (count($options) > 0 || $showEmpty);
|
2012-09-18 18:41:51 +00:00
|
|
|
// Secure the field if there are options, or its a multi select.
|
|
|
|
// Single selects with no options don't submit, but multiselects do.
|
2014-12-09 02:17:35 +00:00
|
|
|
if ((!isset($secure) || $secure) &&
|
2012-08-27 02:53:48 +00:00
|
|
|
empty($attributes['disabled']) &&
|
2012-09-18 18:41:51 +00:00
|
|
|
(!empty($attributes['multiple']) || $hasOptions)
|
2012-08-27 02:53:48 +00:00
|
|
|
) {
|
2013-04-12 16:28:40 +00:00
|
|
|
$this->_secure(true, $this->_secureFieldName($attributes));
|
2010-04-08 22:53:07 +00:00
|
|
|
}
|
2014-05-23 17:26:11 +00:00
|
|
|
$filter = array('name' => null, 'value' => null);
|
|
|
|
if (is_array($attributes['disabled'])) {
|
|
|
|
$filter['disabled'] = null;
|
|
|
|
}
|
|
|
|
$select[] = $this->Html->useTag($tag, $attributes['name'], array_diff_key($attributes, $filter));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
$emptyMulti = (
|
2008-12-23 08:13:00 +00:00
|
|
|
$showEmpty !== null && $showEmpty !== false && !(
|
|
|
|
empty($showEmpty) && (isset($attributes) &&
|
|
|
|
array_key_exists('multiple', $attributes))
|
|
|
|
)
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
if ($emptyMulti) {
|
2008-12-23 08:13:00 +00:00
|
|
|
$showEmpty = ($showEmpty === true) ? '' : $showEmpty;
|
2012-04-07 01:46:34 +00:00
|
|
|
$options = array('' => $showEmpty) + $options;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-12-23 08:13:00 +00:00
|
|
|
|
2011-06-07 02:32:55 +00:00
|
|
|
if (!$id) {
|
|
|
|
$attributes['id'] = Inflector::camelize($attributes['id']);
|
|
|
|
}
|
|
|
|
|
2011-08-20 05:39:30 +00:00
|
|
|
$select = array_merge($select, $this->_selectOptions(
|
2008-09-24 23:02:14 +00:00
|
|
|
array_reverse($options, true),
|
|
|
|
array(),
|
|
|
|
$showParents,
|
2011-06-07 02:32:55 +00:00
|
|
|
array(
|
|
|
|
'escape' => $escapeOptions,
|
|
|
|
'style' => $style,
|
|
|
|
'name' => $attributes['name'],
|
|
|
|
'value' => $attributes['value'],
|
|
|
|
'class' => $attributes['class'],
|
2012-07-23 14:39:46 +00:00
|
|
|
'id' => $attributes['id'],
|
|
|
|
'disabled' => $attributes['disabled'],
|
2011-06-07 02:32:55 +00:00
|
|
|
)
|
2008-09-24 23:02:14 +00:00
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2013-02-12 02:38:08 +00:00
|
|
|
$template = ($style === 'checkbox') ? 'checkboxmultipleend' : 'selectend';
|
2011-01-23 14:26:14 +00:00
|
|
|
$select[] = $this->Html->useTag($template);
|
2009-11-21 21:43:54 +00:00
|
|
|
return implode("\n", $select);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2013-12-04 00:30:57 +00:00
|
|
|
/**
|
|
|
|
* Generates a valid DOM ID suffix from a string.
|
|
|
|
* Also avoids collisions when multiple values are coverted to the same suffix by
|
|
|
|
* appending a numeric value.
|
|
|
|
*
|
|
|
|
* For pre-HTML5 IDs only characters like a-z 0-9 - _ are valid. HTML5 doesn't have that
|
|
|
|
* limitation, but to avoid layout issues it still filters out some sensitive chars.
|
|
|
|
*
|
|
|
|
* @param string $value The value that should be transferred into a DOM ID suffix.
|
2014-02-03 17:51:57 +00:00
|
|
|
* @param string $type Doctype to use. Defaults to html4.
|
2013-12-04 00:30:57 +00:00
|
|
|
* @return string DOM ID
|
|
|
|
*/
|
2014-02-03 17:51:57 +00:00
|
|
|
public function domIdSuffix($value, $type = 'html4') {
|
2013-12-04 00:30:57 +00:00
|
|
|
if ($type === 'html5') {
|
2014-02-03 02:20:31 +00:00
|
|
|
$value = str_replace(array('@', '<', '>', ' ', '"', '\''), '_', $value);
|
2013-12-04 00:30:57 +00:00
|
|
|
} else {
|
2014-02-03 17:51:57 +00:00
|
|
|
$value = Inflector::camelize(Inflector::slug($value));
|
2013-12-04 00:30:57 +00:00
|
|
|
}
|
|
|
|
$value = Inflector::camelize($value);
|
|
|
|
$count = 1;
|
|
|
|
$suffix = $value;
|
|
|
|
while (in_array($suffix, $this->_domIdSuffixes)) {
|
|
|
|
$suffix = $value . $count++;
|
|
|
|
}
|
|
|
|
$this->_domIdSuffixes[] = $suffix;
|
|
|
|
return $suffix;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a SELECT element for days.
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Attributes:
|
2009-10-17 05:29:16 +00:00
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
2009-10-17 05:29:16 +00:00
|
|
|
* that string is displayed as the empty element.
|
2010-10-30 03:56:22 +00:00
|
|
|
* - `value` The selected value of the input.
|
2009-10-17 05:29:16 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $fieldName Prefix name for the SELECT element
|
2009-10-17 04:55:17 +00:00
|
|
|
* @param array $attributes HTML attributes for the select element
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return string A generated day select box.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::day
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-08-05 23:31:51 +00:00
|
|
|
public function day($fieldName = null, $attributes = array()) {
|
2010-08-05 21:56:29 +00:00
|
|
|
$attributes += array('empty' => true, 'value' => null);
|
2011-08-20 05:39:30 +00:00
|
|
|
$attributes = $this->_dateTimeSelected('day', $fieldName, $attributes);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-08-05 18:14:36 +00:00
|
|
|
if (strlen($attributes['value']) > 2) {
|
2014-02-20 02:46:53 +00:00
|
|
|
$date = date_create($attributes['value']);
|
|
|
|
$attributes['value'] = null;
|
|
|
|
if ($date) {
|
|
|
|
$attributes['value'] = $date->format('d');
|
2014-02-19 23:50:30 +00:00
|
|
|
}
|
2010-08-05 18:14:36 +00:00
|
|
|
} elseif ($attributes['value'] === false) {
|
|
|
|
$attributes['value'] = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-08-20 05:39:30 +00:00
|
|
|
return $this->select($fieldName . ".day", $this->_generateOptions('day'), $attributes);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a SELECT element for years
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Attributes:
|
2009-10-17 05:29:16 +00:00
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
2009-10-17 05:29:16 +00:00
|
|
|
* that string is displayed as the empty element.
|
2010-01-27 20:18:53 +00:00
|
|
|
* - `orderYear` - Ordering of year values in select options.
|
|
|
|
* Possible values 'asc', 'desc'. Default 'desc'
|
2010-10-30 03:56:22 +00:00
|
|
|
* - `value` The selected value of the input.
|
2009-10-17 05:29:16 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $fieldName Prefix name for the SELECT element
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $minYear First year in sequence
|
|
|
|
* @param int $maxYear Last year in sequence
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $attributes Attribute array for the select elements.
|
2009-10-20 03:34:34 +00:00
|
|
|
* @return string Completed year select input
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::year
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-08-05 21:56:29 +00:00
|
|
|
public function year($fieldName, $minYear = null, $maxYear = null, $attributes = array()) {
|
2010-08-05 23:31:51 +00:00
|
|
|
$attributes += array('empty' => true, 'value' => null);
|
2010-08-05 21:56:29 +00:00
|
|
|
if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($value)) {
|
2013-06-25 10:11:10 +00:00
|
|
|
$year = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
extract($value);
|
2010-08-05 21:56:29 +00:00
|
|
|
$attributes['value'] = $year;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
if (empty($value)) {
|
2009-10-17 05:29:16 +00:00
|
|
|
if (!$attributes['empty'] && !$maxYear) {
|
2010-08-05 21:56:29 +00:00
|
|
|
$attributes['value'] = 'now';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-08-05 21:56:29 +00:00
|
|
|
} elseif (!$attributes['empty'] && $maxYear && !$attributes['value']) {
|
|
|
|
$attributes['value'] = $maxYear;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-08-05 21:56:29 +00:00
|
|
|
$attributes['value'] = $value;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-05 21:56:29 +00:00
|
|
|
if (strlen($attributes['value']) > 4 || $attributes['value'] === 'now') {
|
2014-02-20 02:46:53 +00:00
|
|
|
$date = date_create($attributes['value']);
|
|
|
|
$attributes['value'] = null;
|
|
|
|
if ($date) {
|
|
|
|
$attributes['value'] = $date->format('Y');
|
2014-02-19 23:50:30 +00:00
|
|
|
}
|
2010-08-05 21:56:29 +00:00
|
|
|
} elseif ($attributes['value'] === false) {
|
|
|
|
$attributes['value'] = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2013-03-01 12:37:52 +00:00
|
|
|
$yearOptions = array('value' => $attributes['value'], 'min' => $minYear, 'max' => $maxYear, 'order' => 'desc');
|
2010-01-27 20:18:53 +00:00
|
|
|
if (isset($attributes['orderYear'])) {
|
|
|
|
$yearOptions['order'] = $attributes['orderYear'];
|
|
|
|
unset($attributes['orderYear']);
|
2010-08-05 23:31:51 +00:00
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
return $this->select(
|
2011-08-20 05:39:30 +00:00
|
|
|
$fieldName . '.year', $this->_generateOptions('year', $yearOptions),
|
2010-08-05 18:14:36 +00:00
|
|
|
$attributes
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a SELECT element for months.
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Attributes:
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2009-11-02 15:51:16 +00:00
|
|
|
* - `monthNames` - If false, 2 digit numbers will be used instead of text.
|
2014-07-30 20:11:03 +00:00
|
|
|
* If an array, the given array will be used.
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
2009-10-17 05:29:16 +00:00
|
|
|
* that string is displayed as the empty element.
|
2010-10-30 03:56:22 +00:00
|
|
|
* - `value` The selected value of the input.
|
2009-02-03 18:48:28 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $fieldName Prefix name for the SELECT element
|
|
|
|
* @param array $attributes Attributes for the select element
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return string A generated month select dropdown.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::month
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-08-05 22:00:15 +00:00
|
|
|
public function month($fieldName, $attributes = array()) {
|
|
|
|
$attributes += array('empty' => true, 'value' => null);
|
2011-08-20 05:39:30 +00:00
|
|
|
$attributes = $this->_dateTimeSelected('month', $fieldName, $attributes);
|
2008-06-14 21:40:01 +00:00
|
|
|
|
2010-08-05 18:14:36 +00:00
|
|
|
if (strlen($attributes['value']) > 2) {
|
2014-02-20 02:46:53 +00:00
|
|
|
$date = date_create($attributes['value']);
|
|
|
|
$attributes['value'] = null;
|
|
|
|
if ($date) {
|
|
|
|
$attributes['value'] = $date->format('m');
|
2014-02-19 23:50:30 +00:00
|
|
|
}
|
2010-08-05 18:14:36 +00:00
|
|
|
} elseif ($attributes['value'] === false) {
|
|
|
|
$attributes['value'] = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$defaults = array('monthNames' => true);
|
2012-03-03 22:10:12 +00:00
|
|
|
$attributes = array_merge($defaults, (array)$attributes);
|
2008-05-30 11:40:08 +00:00
|
|
|
$monthNames = $attributes['monthNames'];
|
|
|
|
unset($attributes['monthNames']);
|
2008-06-14 21:40:01 +00:00
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
return $this->select(
|
|
|
|
$fieldName . ".month",
|
2011-08-20 05:39:30 +00:00
|
|
|
$this->_generateOptions('month', array('monthNames' => $monthNames)),
|
2010-08-05 18:14:36 +00:00
|
|
|
$attributes
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a SELECT element for hours.
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Attributes:
|
2009-10-17 05:29:16 +00:00
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
2009-10-17 05:29:16 +00:00
|
|
|
* that string is displayed as the empty element.
|
2010-10-30 03:56:22 +00:00
|
|
|
* - `value` The selected value of the input.
|
2009-10-17 05:29:16 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $fieldName Prefix name for the SELECT element
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $format24Hours True for 24 hours format
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param array $attributes List of HTML attributes
|
2009-10-20 03:34:34 +00:00
|
|
|
* @return string Completed hour select input
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::hour
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-08-05 22:04:00 +00:00
|
|
|
public function hour($fieldName, $format24Hours = false, $attributes = array()) {
|
|
|
|
$attributes += array('empty' => true, 'value' => null);
|
2011-08-20 05:39:30 +00:00
|
|
|
$attributes = $this->_dateTimeSelected('hour', $fieldName, $attributes);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-08-05 18:14:36 +00:00
|
|
|
if (strlen($attributes['value']) > 2) {
|
2014-02-19 23:50:30 +00:00
|
|
|
try {
|
2014-02-20 02:46:53 +00:00
|
|
|
$date = new DateTime($attributes['value']);
|
2014-02-19 23:50:30 +00:00
|
|
|
if ($format24Hours) {
|
2014-02-20 02:46:53 +00:00
|
|
|
$attributes['value'] = $date->format('H');
|
2014-02-19 23:50:30 +00:00
|
|
|
} else {
|
2014-02-20 02:46:53 +00:00
|
|
|
$attributes['value'] = $date->format('g');
|
2014-02-19 23:50:30 +00:00
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$attributes['value'] = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-08-05 18:14:36 +00:00
|
|
|
} elseif ($attributes['value'] === false) {
|
|
|
|
$attributes['value'] = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-10-23 16:47:12 +00:00
|
|
|
|
|
|
|
if ($attributes['value'] > 12 && !$format24Hours) {
|
|
|
|
$attributes['value'] -= 12;
|
|
|
|
}
|
2013-04-30 12:54:09 +00:00
|
|
|
if (($attributes['value'] === 0 || $attributes['value'] === '00') && !$format24Hours) {
|
2013-03-14 00:56:04 +00:00
|
|
|
$attributes['value'] = 12;
|
|
|
|
}
|
2012-10-23 16:47:12 +00:00
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
return $this->select(
|
|
|
|
$fieldName . ".hour",
|
2011-08-20 05:39:30 +00:00
|
|
|
$this->_generateOptions($format24Hours ? 'hour24' : 'hour'),
|
2010-08-05 18:14:36 +00:00
|
|
|
$attributes
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a SELECT element for minutes.
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Attributes:
|
2009-10-17 05:29:16 +00:00
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
2009-10-17 05:29:16 +00:00
|
|
|
* that string is displayed as the empty element.
|
2010-10-30 03:56:22 +00:00
|
|
|
* - `value` The selected value of the input.
|
2009-10-17 05:29:16 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $fieldName Prefix name for the SELECT element
|
2013-07-06 17:04:06 +00:00
|
|
|
* @param array $attributes Array of Attributes
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return string Completed minute select input.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::minute
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-08-05 22:07:08 +00:00
|
|
|
public function minute($fieldName, $attributes = array()) {
|
|
|
|
$attributes += array('empty' => true, 'value' => null);
|
2011-08-20 05:39:30 +00:00
|
|
|
$attributes = $this->_dateTimeSelected('min', $fieldName, $attributes);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-08-05 18:14:36 +00:00
|
|
|
if (strlen($attributes['value']) > 2) {
|
2014-02-20 02:46:53 +00:00
|
|
|
$date = date_create($attributes['value']);
|
|
|
|
$attributes['value'] = null;
|
|
|
|
if ($date) {
|
|
|
|
$attributes['value'] = $date->format('i');
|
2014-02-19 23:50:30 +00:00
|
|
|
}
|
2010-08-05 18:14:36 +00:00
|
|
|
} elseif ($attributes['value'] === false) {
|
|
|
|
$attributes['value'] = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$minuteOptions = array();
|
|
|
|
|
|
|
|
if (isset($attributes['interval'])) {
|
|
|
|
$minuteOptions['interval'] = $attributes['interval'];
|
|
|
|
unset($attributes['interval']);
|
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
return $this->select(
|
2011-08-20 05:39:30 +00:00
|
|
|
$fieldName . ".min", $this->_generateOptions('minute', $minuteOptions),
|
2010-08-05 18:14:36 +00:00
|
|
|
$attributes
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-10-20 01:58:37 +00:00
|
|
|
/**
|
|
|
|
* Selects values for dateTime selects.
|
|
|
|
*
|
|
|
|
* @param string $select Name of element field. ex. 'day'
|
|
|
|
* @param string $fieldName Name of fieldName being generated ex. Model.created
|
|
|
|
* @param array $attributes Array of attributes, must contain 'empty' key.
|
2010-08-05 21:47:14 +00:00
|
|
|
* @return array Attributes array with currently selected value.
|
2009-10-20 01:58:37 +00:00
|
|
|
*/
|
2011-08-20 05:39:30 +00:00
|
|
|
protected function _dateTimeSelected($select, $fieldName, $attributes) {
|
2010-08-05 21:47:14 +00:00
|
|
|
if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
|
2013-06-18 07:39:43 +00:00
|
|
|
if (is_array($value)) {
|
|
|
|
$attributes['value'] = isset($value[$select]) ? $value[$select] : null;
|
2009-10-20 01:58:37 +00:00
|
|
|
} else {
|
|
|
|
if (empty($value)) {
|
|
|
|
if (!$attributes['empty']) {
|
2010-08-05 21:47:14 +00:00
|
|
|
$attributes['value'] = 'now';
|
2009-10-20 01:58:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-08-05 21:47:14 +00:00
|
|
|
$attributes['value'] = $value;
|
2009-10-20 01:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-05 21:47:14 +00:00
|
|
|
return $attributes;
|
2009-10-20 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a SELECT element for AM or PM.
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Attributes:
|
2009-10-17 05:29:16 +00:00
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
2009-10-17 05:29:16 +00:00
|
|
|
* that string is displayed as the empty element.
|
2010-10-30 03:56:22 +00:00
|
|
|
* - `value` The selected value of the input.
|
2009-10-17 05:29:16 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $fieldName Prefix name for the SELECT element
|
2014-02-19 23:54:00 +00:00
|
|
|
* @param array $attributes Array of Attributes
|
2009-10-20 03:34:34 +00:00
|
|
|
* @return string Completed meridian select input
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::meridian
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-08-05 23:24:17 +00:00
|
|
|
public function meridian($fieldName, $attributes = array()) {
|
2010-08-05 23:31:51 +00:00
|
|
|
$attributes += array('empty' => true, 'value' => null);
|
2010-08-05 18:14:36 +00:00
|
|
|
if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($value)) {
|
2013-06-25 10:11:10 +00:00
|
|
|
$meridian = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
extract($value);
|
2010-08-05 18:14:36 +00:00
|
|
|
$attributes['value'] = $meridian;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
if (empty($value)) {
|
2011-04-24 07:24:56 +00:00
|
|
|
if (!$attributes['empty']) {
|
2010-08-05 18:14:36 +00:00
|
|
|
$attributes['value'] = date('a');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-02-20 02:46:53 +00:00
|
|
|
$date = date_create($attributes['value']);
|
|
|
|
$attributes['value'] = null;
|
|
|
|
if ($date) {
|
|
|
|
$attributes['value'] = $date->format('a');
|
2014-02-19 23:50:30 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-05 18:14:36 +00:00
|
|
|
if ($attributes['value'] === false) {
|
|
|
|
$attributes['value'] = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
return $this->select(
|
2011-08-20 05:39:30 +00:00
|
|
|
$fieldName . ".meridian", $this->_generateOptions('meridian'),
|
2010-08-05 18:14:36 +00:00
|
|
|
$attributes
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a set of SELECT elements for a full datetime setup: day, month and year, and then time.
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* ### Attributes:
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2009-11-02 15:57:05 +00:00
|
|
|
* - `monthNames` If false, 2 digit numbers will be used instead of text.
|
2014-07-30 20:11:03 +00:00
|
|
|
* If an array, the given array will be used.
|
2009-10-17 04:55:17 +00:00
|
|
|
* - `minYear` The lowest year to use in the year select
|
|
|
|
* - `maxYear` The maximum year to use in the year select
|
|
|
|
* - `interval` The interval for the minutes select. Defaults to 1
|
|
|
|
* - `separator` The contents of the string between select elements. Defaults to '-'
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
2009-10-17 05:29:16 +00:00
|
|
|
* that string is displayed as the empty element.
|
2013-07-16 13:42:08 +00:00
|
|
|
* - `round` - Set to `up` or `down` if you want to force rounding in either direction. Defaults to null.
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `value` | `default` The default value to be used by the input. A value in `$this->data`
|
|
|
|
* matching the field name will override this value. If no default is provided `time()` will be used.
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $fieldName Prefix name for the SELECT element
|
2011-12-20 03:11:36 +00:00
|
|
|
* @param string $dateFormat DMY, MDY, YMD, or null to not generate date inputs.
|
|
|
|
* @param string $timeFormat 12, 24, or null to not generate time inputs.
|
2014-02-19 23:54:00 +00:00
|
|
|
* @param array $attributes Array of Attributes
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return string Generated set of select boxes for the date and time formats chosen.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::dateTime
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-08-05 23:30:49 +00:00
|
|
|
public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $attributes = array()) {
|
2010-08-05 23:31:51 +00:00
|
|
|
$attributes += array('empty' => true, 'value' => null);
|
2008-05-30 11:40:08 +00:00
|
|
|
$year = $month = $day = $hour = $min = $meridian = null;
|
|
|
|
|
2010-08-05 23:30:49 +00:00
|
|
|
if (empty($attributes['value'])) {
|
2010-11-01 01:57:51 +00:00
|
|
|
$attributes = $this->value($attributes, $fieldName);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 23:30:49 +00:00
|
|
|
if ($attributes['value'] === null && $attributes['empty'] != true) {
|
|
|
|
$attributes['value'] = time();
|
2013-06-19 19:06:51 +00:00
|
|
|
if (!empty($attributes['maxYear']) && $attributes['maxYear'] < date('Y')) {
|
2013-06-13 19:10:18 +00:00
|
|
|
$attributes['value'] = strtotime(date($attributes['maxYear'] . '-m-d'));
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 23:30:49 +00:00
|
|
|
if (!empty($attributes['value'])) {
|
2012-10-26 01:28:01 +00:00
|
|
|
list($year, $month, $day, $hour, $min, $meridian) = $this->_getDateTimeValue(
|
|
|
|
$attributes['value'],
|
|
|
|
$timeFormat
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-06-14 21:40:01 +00:00
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
$defaults = array(
|
|
|
|
'minYear' => null, 'maxYear' => null, 'separator' => '-',
|
2013-07-16 13:42:08 +00:00
|
|
|
'interval' => 1, 'monthNames' => true, 'round' => null
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
2012-03-03 22:10:12 +00:00
|
|
|
$attributes = array_merge($defaults, (array)$attributes);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($attributes['minuteInterval'])) {
|
|
|
|
$attributes['interval'] = $attributes['minuteInterval'];
|
|
|
|
unset($attributes['minuteInterval']);
|
|
|
|
}
|
|
|
|
$minYear = $attributes['minYear'];
|
|
|
|
$maxYear = $attributes['maxYear'];
|
|
|
|
$separator = $attributes['separator'];
|
|
|
|
$interval = $attributes['interval'];
|
|
|
|
$monthNames = $attributes['monthNames'];
|
2013-07-16 13:42:08 +00:00
|
|
|
$round = $attributes['round'];
|
2008-05-30 11:40:08 +00:00
|
|
|
$attributes = array_diff_key($attributes, $defaults);
|
|
|
|
|
2012-09-29 02:01:53 +00:00
|
|
|
if (!empty($interval) && $interval > 1 && !empty($min)) {
|
|
|
|
$current = new DateTime();
|
|
|
|
if ($year !== null) {
|
|
|
|
$current->setDate($year, $month, $day);
|
|
|
|
}
|
|
|
|
if ($hour !== null) {
|
|
|
|
$current->setTime($hour, $min);
|
|
|
|
}
|
2013-07-16 13:42:08 +00:00
|
|
|
$changeValue = $min * (1 / $interval);
|
|
|
|
switch ($round) {
|
|
|
|
case 'up':
|
|
|
|
$changeValue = ceil($changeValue);
|
|
|
|
break;
|
|
|
|
case 'down':
|
|
|
|
$changeValue = floor($changeValue);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$changeValue = round($changeValue);
|
|
|
|
}
|
|
|
|
$change = ($changeValue * $interval) - $min;
|
2012-09-29 02:01:53 +00:00
|
|
|
$current->modify($change > 0 ? "+$change minutes" : "$change minutes");
|
2013-04-20 18:39:47 +00:00
|
|
|
$format = ($timeFormat == 12) ? 'Y m d h i a' : 'Y m d H i a';
|
2013-03-14 19:48:12 +00:00
|
|
|
$newTime = explode(' ', $current->format($format));
|
2012-09-29 02:01:53 +00:00
|
|
|
list($year, $month, $day, $hour, $min, $meridian) = $newTime;
|
|
|
|
}
|
|
|
|
|
2012-10-26 01:28:01 +00:00
|
|
|
$keys = array('Day', 'Month', 'Year', 'Hour', 'Minute', 'Meridian');
|
|
|
|
$attrs = array_fill_keys($keys, $attributes);
|
|
|
|
|
|
|
|
$hasId = isset($attributes['id']);
|
|
|
|
if ($hasId && is_array($attributes['id'])) {
|
|
|
|
// check for missing ones and build selectAttr for each element
|
|
|
|
$attributes['id'] += array(
|
|
|
|
'month' => '',
|
|
|
|
'year' => '',
|
|
|
|
'day' => '',
|
|
|
|
'hour' => '',
|
|
|
|
'minute' => '',
|
|
|
|
'meridian' => ''
|
|
|
|
);
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
$attrs[$key]['id'] = $attributes['id'][strtolower($key)];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-10-26 01:28:01 +00:00
|
|
|
}
|
|
|
|
if ($hasId && is_string($attributes['id'])) {
|
|
|
|
// build out an array version
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
$attrs[$key]['id'] = $attributes['id'] . $key;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-27 20:17:21 +00:00
|
|
|
if (is_array($attributes['empty'])) {
|
|
|
|
$attributes['empty'] += array(
|
2012-10-26 01:28:01 +00:00
|
|
|
'month' => true,
|
|
|
|
'year' => true,
|
|
|
|
'day' => true,
|
|
|
|
'hour' => true,
|
|
|
|
'minute' => true,
|
|
|
|
'meridian' => true
|
2012-07-27 20:17:21 +00:00
|
|
|
);
|
2012-10-26 01:28:01 +00:00
|
|
|
foreach ($keys as $key) {
|
|
|
|
$attrs[$key]['empty'] = $attributes['empty'][strtolower($key)];
|
2012-07-27 20:17:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-15 05:12:02 +00:00
|
|
|
$selects = array();
|
|
|
|
foreach (preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY) as $char) {
|
|
|
|
switch ($char) {
|
2010-08-05 23:31:51 +00:00
|
|
|
case 'Y':
|
2012-10-26 01:28:01 +00:00
|
|
|
$attrs['Year']['value'] = $year;
|
2009-10-15 05:12:02 +00:00
|
|
|
$selects[] = $this->year(
|
2012-10-26 01:28:01 +00:00
|
|
|
$fieldName, $minYear, $maxYear, $attrs['Year']
|
2009-10-15 05:12:02 +00:00
|
|
|
);
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2009-10-15 05:12:02 +00:00
|
|
|
case 'M':
|
2012-10-26 01:28:01 +00:00
|
|
|
$attrs['Month']['value'] = $month;
|
|
|
|
$attrs['Month']['monthNames'] = $monthNames;
|
|
|
|
$selects[] = $this->month($fieldName, $attrs['Month']);
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2010-08-05 23:31:51 +00:00
|
|
|
case 'D':
|
2012-10-26 01:28:01 +00:00
|
|
|
$attrs['Day']['value'] = $day;
|
|
|
|
$selects[] = $this->day($fieldName, $attrs['Day']);
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-15 05:12:02 +00:00
|
|
|
$opt = implode($separator, $selects);
|
|
|
|
|
2012-10-26 01:28:01 +00:00
|
|
|
$attrs['Minute']['interval'] = $interval;
|
2008-10-23 00:10:44 +00:00
|
|
|
switch ($timeFormat) {
|
2010-08-05 23:31:51 +00:00
|
|
|
case '24':
|
2012-10-26 01:28:01 +00:00
|
|
|
$attrs['Hour']['value'] = $hour;
|
|
|
|
$attrs['Minute']['value'] = $min;
|
|
|
|
$opt .= $this->hour($fieldName, true, $attrs['Hour']) . ':' .
|
|
|
|
$this->minute($fieldName, $attrs['Minute']);
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2010-08-05 23:31:51 +00:00
|
|
|
case '12':
|
2012-10-26 01:28:01 +00:00
|
|
|
$attrs['Hour']['value'] = $hour;
|
|
|
|
$attrs['Minute']['value'] = $min;
|
|
|
|
$attrs['Meridian']['value'] = $meridian;
|
|
|
|
$opt .= $this->hour($fieldName, false, $attrs['Hour']) . ':' .
|
|
|
|
$this->minute($fieldName, $attrs['Minute']) . ' ' .
|
|
|
|
$this->meridian($fieldName, $attrs['Meridian']);
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $opt;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-10-26 01:28:01 +00:00
|
|
|
/**
|
|
|
|
* Parse the value for a datetime selected value
|
|
|
|
*
|
|
|
|
* @param string|array $value The selected value.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $timeFormat The time format
|
2012-10-26 01:28:01 +00:00
|
|
|
* @return array Array of selected value.
|
|
|
|
*/
|
|
|
|
protected function _getDateTimeValue($value, $timeFormat) {
|
|
|
|
$year = $month = $day = $hour = $min = $meridian = null;
|
|
|
|
if (is_array($value)) {
|
|
|
|
extract($value);
|
|
|
|
if ($meridian === 'pm') {
|
|
|
|
$hour += 12;
|
|
|
|
}
|
|
|
|
return array($year, $month, $day, $hour, $min, $meridian);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_numeric($value)) {
|
|
|
|
$value = strftime('%Y-%m-%d %H:%M:%S', $value);
|
|
|
|
}
|
|
|
|
$meridian = 'am';
|
|
|
|
$pos = strpos($value, '-');
|
|
|
|
if ($pos !== false) {
|
|
|
|
$date = explode('-', $value);
|
|
|
|
$days = explode(' ', $date[2]);
|
|
|
|
$day = $days[0];
|
|
|
|
$month = $date[1];
|
|
|
|
$year = $date[0];
|
|
|
|
} else {
|
|
|
|
$days[1] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($timeFormat)) {
|
|
|
|
$time = explode(':', $days[1]);
|
|
|
|
|
2013-04-20 18:48:54 +00:00
|
|
|
if ($time[0] >= 12) {
|
2012-10-26 01:28:01 +00:00
|
|
|
$meridian = 'pm';
|
|
|
|
}
|
|
|
|
$hour = $min = null;
|
|
|
|
if (isset($time[1])) {
|
|
|
|
$hour = $time[0];
|
|
|
|
$min = $time[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array($year, $month, $day, $hour, $min, $meridian);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets the input field name for the current tag
|
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param array $options Options list.
|
|
|
|
* @param string $field Field name.
|
|
|
|
* @param string $key Key name.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _name($options = array(), $field = null, $key = 'name') {
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($this->requestType === 'get') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($options === null) {
|
|
|
|
$options = array();
|
|
|
|
} elseif (is_string($options)) {
|
|
|
|
$field = $options;
|
|
|
|
$options = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($field)) {
|
|
|
|
$this->setEntity($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($options) && isset($options[$key])) {
|
|
|
|
return $options;
|
|
|
|
}
|
2010-04-02 19:43:20 +00:00
|
|
|
|
2011-06-26 00:02:34 +00:00
|
|
|
$entity = $this->entity();
|
2011-06-25 23:19:53 +00:00
|
|
|
$model = $this->model();
|
|
|
|
$name = $model === $entity[0] && isset($entity[1]) ? $entity[1] : $entity[0];
|
|
|
|
$last = $entity[count($entity) - 1];
|
|
|
|
if (in_array($last, $this->_fieldSuffixes)) {
|
|
|
|
$name .= '[' . $last . ']';
|
2010-04-02 19:43:20 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (is_array($options)) {
|
|
|
|
$options[$key] = $name;
|
|
|
|
return $options;
|
|
|
|
}
|
2013-07-02 23:14:41 +00:00
|
|
|
return $name;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-11-15 10:18:46 +00:00
|
|
|
return parent::_name($options, $field, $key);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of formatted OPTION/OPTGROUP elements
|
2011-07-29 02:45:47 +00:00
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param array $elements Elements to format.
|
|
|
|
* @param array $parents Parents for OPTGROUP.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $showParents Whether to show parents.
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param array $attributes HTML attributes.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2011-08-20 05:39:30 +00:00
|
|
|
protected function _selectOptions($elements = array(), $parents = array(), $showParents = null, $attributes = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$select = array();
|
2010-11-01 01:56:59 +00:00
|
|
|
$attributes = array_merge(
|
2011-03-19 17:29:31 +00:00
|
|
|
array('escape' => true, 'style' => null, 'value' => null, 'class' => null),
|
2010-11-01 01:56:59 +00:00
|
|
|
$attributes
|
|
|
|
);
|
2010-08-05 22:48:17 +00:00
|
|
|
$selectedIsEmpty = ($attributes['value'] === '' || $attributes['value'] === null);
|
|
|
|
$selectedIsArray = is_array($attributes['value']);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2013-12-04 00:30:57 +00:00
|
|
|
$this->_domIdSuffixes = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($elements as $name => $title) {
|
|
|
|
$htmlOptions = array();
|
|
|
|
if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
|
|
|
|
if (!empty($name)) {
|
|
|
|
if ($attributes['style'] === 'checkbox') {
|
2011-01-23 14:26:14 +00:00
|
|
|
$select[] = $this->Html->useTag('fieldsetend');
|
2008-06-22 19:16:34 +00:00
|
|
|
} else {
|
2011-01-23 14:26:14 +00:00
|
|
|
$select[] = $this->Html->useTag('optiongroupend');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$parents[] = $name;
|
|
|
|
}
|
2011-08-20 05:39:30 +00:00
|
|
|
$select = array_merge($select, $this->_selectOptions(
|
2010-08-05 22:48:17 +00:00
|
|
|
$title, $parents, $showParents, $attributes
|
2008-09-24 23:02:14 +00:00
|
|
|
));
|
2008-07-30 13:40:59 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($name)) {
|
2010-10-14 01:59:53 +00:00
|
|
|
$name = $attributes['escape'] ? h($name) : $name;
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($attributes['style'] === 'checkbox') {
|
2011-01-23 14:26:14 +00:00
|
|
|
$select[] = $this->Html->useTag('fieldsetstart', $name);
|
2008-06-22 19:16:34 +00:00
|
|
|
} else {
|
2011-01-23 14:26:14 +00:00
|
|
|
$select[] = $this->Html->useTag('optiongroup', $name, '');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$name = null;
|
|
|
|
} elseif (is_array($title)) {
|
|
|
|
$htmlOptions = $title;
|
|
|
|
$name = $title['value'];
|
|
|
|
$title = $title['name'];
|
|
|
|
unset($htmlOptions['name'], $htmlOptions['value']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($name !== null) {
|
2013-01-30 12:54:09 +00:00
|
|
|
$isNumeric = is_numeric($name);
|
2014-12-09 02:17:35 +00:00
|
|
|
if ((!$selectedIsArray && !$selectedIsEmpty && (string)$attributes['value'] == (string)$name) ||
|
2013-02-07 14:36:00 +00:00
|
|
|
($selectedIsArray && in_array((string)$name, $attributes['value'], !$isNumeric))
|
2010-01-26 15:55:11 +00:00
|
|
|
) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($attributes['style'] === 'checkbox') {
|
|
|
|
$htmlOptions['checked'] = true;
|
|
|
|
} else {
|
|
|
|
$htmlOptions['selected'] = 'selected';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($showParents || (!in_array($title, $parents))) {
|
2008-08-01 04:53:16 +00:00
|
|
|
$title = ($attributes['escape']) ? h($title) : $title;
|
2008-07-30 13:40:59 +00:00
|
|
|
|
2013-02-07 14:36:00 +00:00
|
|
|
$hasDisabled = !empty($attributes['disabled']);
|
|
|
|
if ($hasDisabled) {
|
|
|
|
$disabledIsArray = is_array($attributes['disabled']);
|
|
|
|
if ($disabledIsArray) {
|
|
|
|
$disabledIsNumeric = is_numeric($name);
|
|
|
|
}
|
|
|
|
}
|
2014-12-09 02:17:35 +00:00
|
|
|
if ($hasDisabled &&
|
2013-02-07 14:36:00 +00:00
|
|
|
$disabledIsArray &&
|
|
|
|
in_array((string)$name, $attributes['disabled'], !$disabledIsNumeric)
|
|
|
|
) {
|
|
|
|
$htmlOptions['disabled'] = 'disabled';
|
|
|
|
}
|
2013-06-01 09:46:23 +00:00
|
|
|
if ($hasDisabled && !$disabledIsArray && $attributes['style'] === 'checkbox') {
|
2013-02-07 14:36:00 +00:00
|
|
|
$htmlOptions['disabled'] = $attributes['disabled'] === true ? 'disabled' : $attributes['disabled'];
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($attributes['style'] === 'checkbox') {
|
|
|
|
$htmlOptions['value'] = $name;
|
2012-08-03 01:25:27 +00:00
|
|
|
|
2013-12-04 00:30:57 +00:00
|
|
|
$tagName = $attributes['id'] . $this->domIdSuffix($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
$htmlOptions['id'] = $tagName;
|
|
|
|
$label = array('for' => $tagName);
|
|
|
|
|
|
|
|
if (isset($htmlOptions['checked']) && $htmlOptions['checked'] === true) {
|
|
|
|
$label['class'] = 'selected';
|
|
|
|
}
|
|
|
|
|
2010-09-05 05:28:13 +00:00
|
|
|
$name = $attributes['name'];
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (empty($attributes['class'])) {
|
|
|
|
$attributes['class'] = 'checkbox';
|
2011-03-11 14:50:55 +00:00
|
|
|
} elseif ($attributes['class'] === 'form-error') {
|
|
|
|
$attributes['class'] = 'checkbox ' . $attributes['class'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
$label = $this->label(null, $title, $label);
|
2011-01-23 14:26:14 +00:00
|
|
|
$item = $this->Html->useTag('checkboxmultiple', $name, $htmlOptions);
|
2008-09-24 23:02:14 +00:00
|
|
|
$select[] = $this->Html->div($attributes['class'], $item . $label);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2013-12-04 00:51:39 +00:00
|
|
|
if ($attributes['escape']) {
|
|
|
|
$name = h($name);
|
|
|
|
}
|
2011-01-23 14:26:14 +00:00
|
|
|
$select[] = $this->Html->useTag('selectoption', $name, $htmlOptions, $title);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_reverse($select, true);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generates option lists for common <select /> menus
|
2011-07-29 02:45:47 +00:00
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param string $name List type name.
|
|
|
|
* @param array $options Options list.
|
2011-07-29 02:45:47 +00:00
|
|
|
* @return array
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-08-20 05:39:30 +00:00
|
|
|
protected function _generateOptions($name, $options = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($this->options[$name])) {
|
|
|
|
return $this->options[$name];
|
|
|
|
}
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
switch ($name) {
|
|
|
|
case 'minute':
|
|
|
|
if (isset($options['interval'])) {
|
|
|
|
$interval = $options['interval'];
|
|
|
|
} else {
|
|
|
|
$interval = 1;
|
|
|
|
}
|
|
|
|
$i = 0;
|
|
|
|
while ($i < 60) {
|
2009-11-01 02:58:52 +00:00
|
|
|
$data[sprintf('%02d', $i)] = sprintf('%02d', $i);
|
2008-05-30 11:40:08 +00:00
|
|
|
$i += $interval;
|
|
|
|
}
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'hour':
|
|
|
|
for ($i = 1; $i <= 12; $i++) {
|
|
|
|
$data[sprintf('%02d', $i)] = $i;
|
|
|
|
}
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'hour24':
|
|
|
|
for ($i = 0; $i <= 23; $i++) {
|
|
|
|
$data[sprintf('%02d', $i)] = $i;
|
|
|
|
}
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'meridian':
|
|
|
|
$data = array('am' => 'am', 'pm' => 'pm');
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'day':
|
2014-06-06 12:17:49 +00:00
|
|
|
for ($i = 1; $i <= 31; $i++) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$data[sprintf('%02d', $i)] = $i;
|
|
|
|
}
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'month':
|
2009-11-02 15:51:16 +00:00
|
|
|
if ($options['monthNames'] === true) {
|
2011-10-23 18:19:14 +00:00
|
|
|
$data['01'] = __d('cake', 'January');
|
|
|
|
$data['02'] = __d('cake', 'February');
|
|
|
|
$data['03'] = __d('cake', 'March');
|
|
|
|
$data['04'] = __d('cake', 'April');
|
|
|
|
$data['05'] = __d('cake', 'May');
|
|
|
|
$data['06'] = __d('cake', 'June');
|
|
|
|
$data['07'] = __d('cake', 'July');
|
|
|
|
$data['08'] = __d('cake', 'August');
|
|
|
|
$data['09'] = __d('cake', 'September');
|
|
|
|
$data['10'] = __d('cake', 'October');
|
|
|
|
$data['11'] = __d('cake', 'November');
|
|
|
|
$data['12'] = __d('cake', 'December');
|
2012-02-23 13:38:02 +00:00
|
|
|
} elseif (is_array($options['monthNames'])) {
|
2009-11-02 15:51:16 +00:00
|
|
|
$data = $options['monthNames'];
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
for ($m = 1; $m <= 12; $m++) {
|
|
|
|
$data[sprintf("%02s", $m)] = strftime("%m", mktime(1, 1, 1, $m, 1, 1999));
|
|
|
|
}
|
|
|
|
}
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
case 'year':
|
2014-09-10 14:29:23 +00:00
|
|
|
$current = (int)date('Y');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-12-12 03:03:39 +00:00
|
|
|
$min = !isset($options['min']) ? $current - 20 : (int)$options['min'];
|
|
|
|
$max = !isset($options['max']) ? $current + 20 : (int)$options['max'];
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($min > $max) {
|
|
|
|
list($min, $max) = array($max, $min);
|
|
|
|
}
|
2014-12-09 02:17:35 +00:00
|
|
|
if (!empty($options['value']) &&
|
2014-01-15 15:23:45 +00:00
|
|
|
(int)$options['value'] < $min &&
|
|
|
|
(int)$options['value'] > 0
|
|
|
|
) {
|
2013-03-01 12:37:52 +00:00
|
|
|
$min = (int)$options['value'];
|
|
|
|
} elseif (!empty($options['value']) && (int)$options['value'] > $max) {
|
|
|
|
$max = (int)$options['value'];
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
for ($i = $min; $i <= $max; $i++) {
|
|
|
|
$data[$i] = $i;
|
|
|
|
}
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($options['order'] !== 'asc') {
|
2010-01-27 20:18:53 +00:00
|
|
|
$data = array_reverse($data, true);
|
|
|
|
}
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-08-20 04:43:34 +00:00
|
|
|
$this->_options[$name] = $data;
|
|
|
|
return $this->_options[$name];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-21 04:06:37 +00:00
|
|
|
/**
|
2012-12-19 19:52:09 +00:00
|
|
|
* Sets field defaults and adds field to form security input hash.
|
|
|
|
* Will also add a 'form-error' class if the field contains validation errors.
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2010-10-30 03:56:22 +00:00
|
|
|
* ### Options
|
2009-10-17 04:55:17 +00:00
|
|
|
*
|
2011-12-08 03:30:40 +00:00
|
|
|
* - `secure` - boolean whether or not the field should be added to the security fields.
|
|
|
|
* Disabling the field using the `disabled` option, will also omit the field from being
|
|
|
|
* part of the hashed key.
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2014-07-30 20:11:03 +00:00
|
|
|
* This method will convert a numerically indexed 'disabled' into an associative
|
2013-04-03 16:46:03 +00:00
|
|
|
* value. FormHelper's internals expect associative options.
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param string $field Name of the field to initialize options for.
|
|
|
|
* @param array $options Array of options to append options into.
|
|
|
|
* @return array Array of options for the input.
|
2008-09-21 04:06:37 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _initInputField($field, $options = array()) {
|
2008-09-21 04:06:37 +00:00
|
|
|
if (isset($options['secure'])) {
|
|
|
|
$secure = $options['secure'];
|
|
|
|
unset($options['secure']);
|
2008-09-24 23:02:14 +00:00
|
|
|
} else {
|
2010-05-14 04:21:22 +00:00
|
|
|
$secure = (isset($this->request['_Token']) && !empty($this->request['_Token']));
|
2008-09-21 04:06:37 +00:00
|
|
|
}
|
2011-03-19 17:29:31 +00:00
|
|
|
|
2013-04-03 16:46:03 +00:00
|
|
|
$disabledIndex = array_search('disabled', $options, true);
|
2013-05-07 20:13:27 +00:00
|
|
|
if (is_int($disabledIndex)) {
|
2013-04-03 16:46:03 +00:00
|
|
|
unset($options[$disabledIndex]);
|
|
|
|
$options['disabled'] = true;
|
|
|
|
}
|
|
|
|
|
2011-06-25 05:40:50 +00:00
|
|
|
$result = parent::_initInputField($field, $options);
|
2011-12-24 01:40:26 +00:00
|
|
|
if ($this->tagIsInvalid() !== false) {
|
|
|
|
$result = $this->addClass($result, 'form-error');
|
|
|
|
}
|
2013-10-04 15:05:38 +00:00
|
|
|
|
2015-01-09 04:20:30 +00:00
|
|
|
$isDisabled = false;
|
|
|
|
if (isset($result['disabled'])) {
|
|
|
|
$isDisabled = (
|
|
|
|
$result['disabled'] === true ||
|
|
|
|
$result['disabled'] === 'disabled' ||
|
|
|
|
(is_array($result['disabled']) &&
|
|
|
|
!empty($result['options']) &&
|
|
|
|
array_diff($result['options'], $result['disabled']) === array()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ($isDisabled) {
|
2011-06-25 05:40:50 +00:00
|
|
|
return $result;
|
|
|
|
}
|
2013-10-04 15:05:38 +00:00
|
|
|
|
2012-12-19 20:12:49 +00:00
|
|
|
if (!isset($result['required']) &&
|
|
|
|
$this->_introspectModel($this->model(), 'validates', $this->field())
|
|
|
|
) {
|
|
|
|
$result['required'] = true;
|
|
|
|
}
|
2011-06-25 05:40:50 +00:00
|
|
|
|
2013-10-04 15:05:38 +00:00
|
|
|
if ($secure === self::SECURE_SKIP) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2013-04-12 16:28:40 +00:00
|
|
|
$this->_secure($secure, $this->_secureFieldName($options));
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the field name for use with _secure().
|
|
|
|
*
|
|
|
|
* Parses the name attribute to create a dot separated name value for use
|
|
|
|
* in secured field hash.
|
|
|
|
*
|
|
|
|
* @param array $options An array of options possibly containing a name key.
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
protected function _secureFieldName($options) {
|
|
|
|
if (isset($options['name'])) {
|
2011-01-28 02:05:30 +00:00
|
|
|
preg_match_all('/\[(.*?)\]/', $options['name'], $matches);
|
|
|
|
if (isset($matches[1])) {
|
2013-04-12 16:28:40 +00:00
|
|
|
return $matches[1];
|
2011-01-28 02:05:30 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-12 16:28:40 +00:00
|
|
|
return null;
|
2008-09-21 04:06:37 +00:00
|
|
|
}
|
2012-03-03 22:10:12 +00:00
|
|
|
|
2014-05-19 02:40:21 +00:00
|
|
|
/**
|
2014-05-20 01:28:29 +00:00
|
|
|
* Sets the last created form action.
|
2014-05-19 02:40:21 +00:00
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param string|array $url URL.
|
2014-05-19 02:40:21 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _lastAction($url) {
|
|
|
|
$action = Router::url($url, true);
|
2014-05-20 01:28:29 +00:00
|
|
|
$query = parse_url($action, PHP_URL_QUERY);
|
|
|
|
$query = $query ? '?' . $query : '';
|
|
|
|
$this->_lastAction = parse_url($action, PHP_URL_PATH) . $query;
|
2014-05-19 02:40:21 +00:00
|
|
|
}
|
|
|
|
|
2012-05-20 19:42:47 +00:00
|
|
|
/**
|
|
|
|
* Set/Get inputDefaults for form elements
|
|
|
|
*
|
|
|
|
* @param array $defaults New default values
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $merge Merge with current defaults
|
2012-05-20 22:51:48 +00:00
|
|
|
* @return array inputDefaults
|
2012-05-20 19:42:47 +00:00
|
|
|
*/
|
|
|
|
public function inputDefaults($defaults = null, $merge = false) {
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($defaults !== null) {
|
2012-05-21 20:56:51 +00:00
|
|
|
if ($merge) {
|
|
|
|
$this->_inputDefaults = array_merge($this->_inputDefaults, (array)$defaults);
|
|
|
|
} else {
|
|
|
|
$this->_inputDefaults = (array)$defaults;
|
|
|
|
}
|
2012-05-20 19:42:47 +00:00
|
|
|
}
|
2012-05-20 22:51:48 +00:00
|
|
|
return $this->_inputDefaults;
|
2012-05-20 19:42:47 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|