2008-08-25 14:57:37 +00:00
|
|
|
<?php
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Automatic generation of HTML FORMs from given data.
|
|
|
|
*
|
|
|
|
* Used for scaffolding.
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-12-24 18:57:20 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @package cake.libs.view.helpers
|
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-04 18:10:24 +00:00
|
|
|
App::uses('AppHelper', 'View/Helper');
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Form helper library.
|
|
|
|
*
|
|
|
|
* Automatic generation of HTML FORMs from given data.
|
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs.view.helpers
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1383/Form
|
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
|
|
|
|
* @access public
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Holds the fields array('field_name' => array('type'=> 'string', 'length'=> 100),
|
|
|
|
* primaryKey and validates array('field_name')
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $fieldset = 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
|
|
|
* Options used by DateTime fields
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__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
|
|
|
/**
|
|
|
|
* 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
|
|
|
/**
|
|
|
|
* Defines the type of form being created. Set by FormHelper::create().
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
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-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2009-10-31 05:11:02 +00:00
|
|
|
*/
|
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
|
|
|
|
* @access protected
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_inputDefaults = array();
|
2011-06-09 01:14:22 +00:00
|
|
|
/**
|
|
|
|
* An array of fieldnames that have been excluded from
|
|
|
|
* the Token hash used by SecurityComponent's validatePost method
|
|
|
|
*
|
|
|
|
* @see FormHelper::__secure()
|
|
|
|
* @see SecurityComponent::validatePost()
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-06-14 01:39:49 +00:00
|
|
|
protected $_unlockedFields = array();
|
2011-07-14 04:19:50 +00:00
|
|
|
|
|
|
|
protected $_models = array();
|
2011-06-09 01:14:22 +00:00
|
|
|
|
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
|
|
|
*
|
2011-07-14 04:19:50 +00:00
|
|
|
* @return Model 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 04:19:50 +00:00
|
|
|
if (!$model) {
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->_models[$model])) {
|
|
|
|
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
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
$object = ClassRegistry::init($model);
|
2009-10-20 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 04:19:50 +00:00
|
|
|
if (get_class($object) === 'AppModel') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_models[$model] = $object;
|
|
|
|
$this->fieldset[$model] = array('fields' => null, 'key' => $object->primaryKey, 'validates' => null);
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _introspectModel($model, $key, $field = null) {
|
|
|
|
$object = $this->_getModel($model);
|
|
|
|
if (!$object) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($key === 'key') {
|
|
|
|
return $this->fieldset[$object->alias]['key'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($this->fieldset[$object->alias]['fields'])) {
|
|
|
|
$fields = $this->fieldset[$object->alias]['fields'] = $object->schema();
|
2009-10-20 05:32:24 +00:00
|
|
|
}
|
|
|
|
|
2011-07-14 04:19:50 +00:00
|
|
|
if ($key === 'fields') {
|
|
|
|
if (empty($field)) {
|
|
|
|
foreach ($object->hasAndBelongsToMany as $alias => $assocData) {
|
|
|
|
$this->fieldset[$object->alias]['fields'][$alias] = array('type' => 'multiple');
|
|
|
|
}
|
|
|
|
return $this->fieldset[$object->alias]['fields'];
|
|
|
|
} elseif (isset($this->fieldset[$object->alias]['fields'][$field])) {
|
|
|
|
return $this->fieldset[$object->alias]['fields'][$field];
|
|
|
|
} else {
|
|
|
|
return isset($object->hasAndBelongsToMany[$field]) ? array('type' => 'multiple') : null;
|
2009-10-20 05:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-14 04:19:50 +00:00
|
|
|
|
|
|
|
if ($key === 'validates' && !isset($this->fieldset[$object->alias]['validates'])) {
|
|
|
|
$validates = array();
|
|
|
|
if (!empty($object->validate)) {
|
|
|
|
foreach ($object->validate as $validateField => $validateProperties) {
|
|
|
|
if ($this->_isRequiredField($validateProperties)) {
|
|
|
|
$validates[$validateField] = true;
|
|
|
|
}
|
2009-10-20 05:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-14 04:19:50 +00:00
|
|
|
$this->fieldset[$object->alias]['validates'] = $validates;
|
2009-10-20 05:32:24 +00:00
|
|
|
}
|
2009-10-22 03:45:42 +00:00
|
|
|
|
2011-07-14 04:19:50 +00:00
|
|
|
if ($key === 'validates') {
|
|
|
|
if (empty($field)) {
|
|
|
|
return $this->fieldset[$object->alias]['validates'];
|
|
|
|
} else {
|
|
|
|
return isset($this->fieldset[$object->alias]['validates'][$field]) ?
|
|
|
|
$this->fieldset[$object->alias]['validates'] : null;
|
|
|
|
}
|
|
|
|
}
|
2009-10-20 05:32:24 +00:00
|
|
|
}
|
2010-02-23 14:25:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if a field is required to be filled based on validation properties from the validating object
|
|
|
|
*
|
|
|
|
* @return boolean true if field is required to be filled, false otherwise
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _isRequiredField($validateProperties) {
|
2010-02-23 14:25:01 +00:00
|
|
|
$required = false;
|
|
|
|
if (is_array($validateProperties)) {
|
|
|
|
|
|
|
|
$dims = Set::countDim($validateProperties);
|
2010-04-30 21:12:59 +00:00
|
|
|
if ($dims == 1 || ($dims == 2 && isset($validateProperties['rule']))) {
|
2010-02-23 14:25:01 +00:00
|
|
|
$validateProperties = array($validateProperties);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($validateProperties as $rule => $validateProp) {
|
2010-02-28 03:28:27 +00:00
|
|
|
if (isset($validateProp['allowEmpty']) && $validateProp['allowEmpty'] === true) {
|
|
|
|
return false;
|
2010-02-23 14:25:01 +00:00
|
|
|
}
|
2010-02-28 03:28:27 +00:00
|
|
|
$rule = isset($validateProp['rule']) ? $validateProp['rule'] : false;
|
|
|
|
$required = $rule || empty($validateProp);
|
2010-02-23 14:25:01 +00:00
|
|
|
if ($required) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $required;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-07-14 04:19:01 +00:00
|
|
|
/**
|
|
|
|
* Returns false if given FORM field has no errors. Otherwise it returns the validation message
|
|
|
|
*
|
|
|
|
* @param string $model Model name as a string
|
|
|
|
* @param string $field Fieldname as a string
|
|
|
|
* @param integer $modelID Unique index identifying this record within the form
|
|
|
|
* @return boolean True on errors.
|
|
|
|
*/
|
|
|
|
public function tagIsInvalid($model = null, $field = null, $modelID = null) {
|
|
|
|
$entity = $this->entity();
|
|
|
|
if (!empty($entity) && $object = $this->_getModel($entity[0])) {
|
|
|
|
array_shift($entity);
|
|
|
|
return Set::extract($object->validationErrors, join('.', $entity));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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).
|
2011-03-16 01:39:15 +00:00
|
|
|
* - `url` The url the form submits to. Can be a string or a url array. If you use 'url'
|
|
|
|
* you should leave 'action' undefined.
|
2009-10-31 05:11:02 +00:00
|
|
|
* - `default` Allows for the creation of Ajax forms.
|
|
|
|
* - `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
|
2010-08-05 23:31:51 +00:00
|
|
|
* be set when using FormHelper::input() can be set here. Options set with `inputDefaults`
|
|
|
|
* can be overridden when calling input()
|
2009-10-31 05:11:02 +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
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
* @param string $model The model object which the form is being defined for
|
2009-02-28 22:17:56 +00:00
|
|
|
* @param array $options An array of html attributes and options.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string An formatted opening FORM tag.
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1384/Creating-Forms
|
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
|
|
|
}
|
2011-07-14 04:19:50 +00:00
|
|
|
if (empty($model) && $model !== false && !empty($this->request->params['models'])) {
|
|
|
|
$model = key($this->request->params['models']);
|
|
|
|
$this->defaultModel = $model;
|
|
|
|
} elseif (empty($model) && empty($this->request->params['models'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$model = false;
|
|
|
|
}
|
2009-10-22 03:45:42 +00:00
|
|
|
|
2010-12-24 18:52:15 +00:00
|
|
|
if ($model !== false) {
|
2011-07-14 04:19:50 +00:00
|
|
|
$object = $this->_getModel($model);
|
2010-12-24 18:52:15 +00:00
|
|
|
}
|
2011-06-25 05:40:50 +00:00
|
|
|
$this->setEntity($model, true);
|
2009-10-22 03:45:42 +00:00
|
|
|
|
2011-07-14 04:19:50 +00:00
|
|
|
$key = $this->_introspectModel($model, 'key');
|
|
|
|
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
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$options = array_merge(array(
|
|
|
|
'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')),
|
2009-10-15 04:44:38 +00:00
|
|
|
'inputDefaults' => array()),
|
2008-05-30 11:40:08 +00:00
|
|
|
$options);
|
2009-10-15 04:44:38 +00:00
|
|
|
$this->_inputDefaults = $options['inputDefaults'];
|
|
|
|
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'])) {
|
2009-10-20 05:32:24 +00:00
|
|
|
if (!empty($model) && $model != $this->defaultModel) {
|
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';
|
|
|
|
break;
|
|
|
|
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';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$this->requestType = strtolower($options['type']);
|
|
|
|
|
2011-06-23 21:06:35 +00:00
|
|
|
|
|
|
|
$action = $this->url($options['action']);
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($options['type'], $options['action']);
|
|
|
|
|
|
|
|
if ($options['default'] == false) {
|
|
|
|
if (isset($htmlAttributes['onSubmit']) || isset($htmlAttributes['onsubmit'])) {
|
|
|
|
$htmlAttributes['onsubmit'] .= ' event.returnValue = false; return false;';
|
|
|
|
} else {
|
|
|
|
$htmlAttributes['onsubmit'] = 'event.returnValue = false; return false;';
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
unset($options['default']);
|
|
|
|
$htmlAttributes = array_merge($options, $htmlAttributes);
|
|
|
|
|
2010-06-13 02:50:09 +00:00
|
|
|
$this->fields = array();
|
2011-06-09 01:14:22 +00:00
|
|
|
if (!empty($this->request->params['_Token'])) {
|
2008-12-25 22:13:06 +00:00
|
|
|
$append .= $this->hidden('_Token.key', array(
|
2011-06-25 05:40:50 +00:00
|
|
|
'value' => $this->request->params['_Token']['key'], 'id' => 'Token' . mt_rand(),
|
|
|
|
'secure' => self::SECURE_SKIP
|
|
|
|
));
|
2011-06-09 01:14:22 +00:00
|
|
|
|
2011-06-14 01:39:49 +00:00
|
|
|
if (!empty($this->request['_Token']['unlockedFields'])) {
|
|
|
|
foreach ((array)$this->request['_Token']['unlockedFields'] as $unlocked) {
|
|
|
|
$this->_unlockedFields[] = $unlocked;
|
2011-06-09 01:14:22 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($append)) {
|
2011-01-23 14:26:14 +00:00
|
|
|
$append = $this->Html->useTag('block', ' style="display:none;"', $append);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-06-25 17:11:31 +00:00
|
|
|
$this->setEntity($model, true);
|
2011-06-23 21:06:35 +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
|
|
|
|
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
|
|
|
*
|
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"
|
|
|
|
* }}}
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-10-17 04:55:17 +00:00
|
|
|
* @param mixed $options as a string will use $options as the value of button,
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string a closing FORM tag optional submit button.
|
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1389/Closing-the-Form
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function end($options = null) {
|
2010-05-14 04:21:22 +00:00
|
|
|
if (!empty($this->request['models'])) {
|
|
|
|
$models = $this->request['models'][0];
|
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
|
|
|
}
|
2010-05-14 04:21:22 +00:00
|
|
|
if (isset($this->request['_Token']) && !empty($this->request['_Token'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out .= $this->secure($this->fields);
|
|
|
|
$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;
|
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
|
|
|
/**
|
|
|
|
* Generates a hidden field with a security hash based on the fields used in the form.
|
|
|
|
*
|
|
|
|
* @param array $fields The list of fields to use when generating the hash
|
|
|
|
* @return string A hidden input field with a security hash
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function secure($fields = 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;
|
2008-05-30 11:40:08 +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, '|');
|
|
|
|
$fields = Security::hash(serialize($fields) . $unlocked . Configure::read('Security.salt'));
|
2008-09-24 23:02:14 +00:00
|
|
|
|
2009-12-09 03:58:08 +00:00
|
|
|
$out = $this->hidden('_Token.fields', array(
|
2008-09-24 23:02:14 +00:00
|
|
|
'value' => urlencode($fields . ':' . $locked),
|
|
|
|
'id' => 'TokenFields' . mt_rand()
|
|
|
|
));
|
2011-06-14 01:39:49 +00:00
|
|
|
$out .= $this->hidden('_Token.unlocked', array(
|
|
|
|
'value' => urlencode($unlocked),
|
|
|
|
'id' => 'TokenUnlocked' . mt_rand()
|
2011-06-09 02:46:29 +00:00
|
|
|
));
|
2011-01-23 14:26:14 +00:00
|
|
|
return $this->Html->useTag('block', ' style="display:none;"', $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-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
|
|
|
*
|
2011-06-09 01:14:22 +00:00
|
|
|
* @param boolean $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.
|
2008-09-24 23:02:14 +00:00
|
|
|
* @param mixed $field Reference to field to be secured
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param mixed $value Field value, if value should not be tampered with.
|
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-06-09 01:14:22 +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)) {
|
|
|
|
$field = Set::filter(explode('.', $field), true);
|
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);
|
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"
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean If there are errors this method returns true, else false.
|
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1426/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
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* - `escape` bool Whether or not to html escape the contents of the error.
|
|
|
|
* - `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.
|
2010-01-25 22:59:05 +00:00
|
|
|
* - `class` string The classname 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"
|
2010-12-26 06:40:02 +00:00
|
|
|
* @param mixed $text Error message as string or array of messages.
|
|
|
|
* 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
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string If there are errors this method returns an error message, otherwise null.
|
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1423/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);
|
|
|
|
$options = array_merge($defaults, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->setEntity($field);
|
|
|
|
|
|
|
|
if ($error = $this->tagIsInvalid()) {
|
2009-09-10 13:56:20 +00:00
|
|
|
if (is_array($text)) {
|
2010-04-20 03:55:57 +00:00
|
|
|
if (isset($text['attributes']) && is_array($text['attributes'])) {
|
|
|
|
$options = array_merge($options, $text['attributes']);
|
2010-12-26 06:40:02 +00:00
|
|
|
unset($text['attributes']);
|
|
|
|
}
|
|
|
|
$tmp = array();
|
|
|
|
foreach ($error as &$e) {
|
|
|
|
if (isset($text[$e])) {
|
|
|
|
$tmp []= $text[$e];
|
|
|
|
} else {
|
|
|
|
$tmp []= $e;
|
|
|
|
}
|
2010-04-20 03:55:57 +00:00
|
|
|
}
|
2010-12-26 06:40:02 +00:00
|
|
|
$text = $tmp;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($text != null) {
|
|
|
|
$error = $text;
|
2010-12-26 06:40:02 +00:00
|
|
|
}
|
|
|
|
if (is_array($error)) {
|
|
|
|
foreach ($error as &$e) {
|
|
|
|
if (is_numeric($e)) {
|
|
|
|
$e = __d('cake', 'Error in field %s', Inflector::humanize($this->field()));
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if ($options['escape']) {
|
|
|
|
$error = h($error);
|
|
|
|
unset($options['escape']);
|
|
|
|
}
|
2010-12-26 06:40:02 +00:00
|
|
|
if (is_array($error)) {
|
|
|
|
if (count($error) > 1) {
|
|
|
|
$listParams = array();
|
|
|
|
if (isset($options['listOptions'])) {
|
|
|
|
if (is_string($options['listOptions'])) {
|
|
|
|
$listParams []= $options['listOptions'];
|
|
|
|
} else {
|
|
|
|
if (isset($options['listOptions']['itemOptions'])) {
|
|
|
|
$listParams []= $options['listOptions']['itemOptions'];
|
|
|
|
unset($options['listOptions']['itemOptions']);
|
|
|
|
} else {
|
|
|
|
$listParams []= array();
|
|
|
|
}
|
|
|
|
if (isset($options['listOptions']['tag'])) {
|
|
|
|
$listParams []= $options['listOptions']['tag'];
|
|
|
|
unset($options['listOptions']['tag']);
|
|
|
|
}
|
|
|
|
array_unshift($listParams, $options['listOptions']);
|
|
|
|
}
|
|
|
|
unset($options['listOptions']);
|
|
|
|
}
|
|
|
|
array_unshift($listParams, $error);
|
|
|
|
$error = call_user_func_array(array($this->Html, 'nestedList'), $listParams);
|
|
|
|
} else {
|
|
|
|
$error = array_pop($error);
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 15:07:32 +00:00
|
|
|
if ($options['wrap']) {
|
2008-06-20 11:57:56 +00:00
|
|
|
$tag = is_string($options['wrap']) ? $options['wrap'] : 'div';
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($options['wrap']);
|
2008-05-30 15:07:32 +00:00
|
|
|
return $this->Html->tag($tag, $error, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
* a for attribute if one is not provided.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-09-24 23:02:14 +00:00
|
|
|
* @param string $fieldName This should be "Modelname.fieldname"
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $text Text that will appear in the label field.
|
2009-10-17 04:55:17 +00:00
|
|
|
* @param mixed $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
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1427/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()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($fieldName)) {
|
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;
|
|
|
|
}
|
|
|
|
if (substr($text, -3) == '_id') {
|
|
|
|
$text = substr($text, 0, strlen($text) - 3);
|
|
|
|
}
|
2011-03-12 19:02:56 +00:00
|
|
|
$text = __d('cake', 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
|
|
|
/**
|
2009-09-24 03:05:08 +00:00
|
|
|
* Generate a set of inputs for `$fields`. If $fields is null the current model
|
|
|
|
* will be used.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-09-24 03:05:08 +00:00
|
|
|
* In addition to controller fields output, `$fields` can be used to control legend
|
2009-10-22 03:45:42 +00:00
|
|
|
* and fieldset rendering with the `fieldset` and `legend` keys.
|
|
|
|
* `$form->inputs(array('legend' => 'My legend'));` Would generate an input set with
|
2009-09-24 03:05:08 +00:00
|
|
|
* a custom legend. You can customize individual inputs through `$fields` as well.
|
2009-10-22 03:45:42 +00:00
|
|
|
*
|
2009-09-24 03:05:08 +00:00
|
|
|
* {{{
|
|
|
|
* $form->inputs(array(
|
|
|
|
* 'name' => array('label' => 'custom label')
|
|
|
|
* ));
|
|
|
|
* }}}
|
|
|
|
*
|
2010-06-27 04:39:31 +00:00
|
|
|
* In addition to fields control, inputs() allows you to use a few additional options.
|
|
|
|
*
|
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
|
2010-06-27 04:39:31 +00:00
|
|
|
* the classname for the fieldset element.
|
|
|
|
* - `legend` Set to false to disable the legend for the generated input set. Or supply a string
|
2010-08-05 23:31:51 +00:00
|
|
|
* to customize the legend text.
|
2010-06-27 04:39:31 +00:00
|
|
|
*
|
2009-09-24 03:05:08 +00:00
|
|
|
* @param mixed $fields An array of fields to generate inputs for, or null.
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param array $blacklist a simple array of fields to not create inputs for.
|
2009-09-24 03:05:08 +00:00
|
|
|
* @return string Completed form inputs.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function inputs($fields = null, $blacklist = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$fieldset = $legend = true;
|
2010-02-26 23:09:33 +00:00
|
|
|
$model = $this->model();
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($fields)) {
|
|
|
|
if (array_key_exists('legend', $fields)) {
|
|
|
|
$legend = $fields['legend'];
|
|
|
|
unset($fields['legend']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($fields['fieldset'])) {
|
|
|
|
$fieldset = $fields['fieldset'];
|
|
|
|
unset($fields['fieldset']);
|
|
|
|
}
|
|
|
|
} elseif ($fields !== null) {
|
|
|
|
$fieldset = $legend = $fields;
|
|
|
|
if (!is_bool($fieldset)) {
|
|
|
|
$fieldset = true;
|
|
|
|
}
|
|
|
|
$fields = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($fields)) {
|
2011-07-14 04:19:50 +00:00
|
|
|
$fields = array_keys($this->_introspectModel($model, 'fields'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($legend === true) {
|
2011-03-12 19:02:56 +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-03-12 19:02:56 +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 = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($fieldset && $legend) {
|
2011-01-23 14:26:14 +00:00
|
|
|
return $this->Html->useTag('fieldset', $fieldsetClass, $this->Html->useTag('legend', $legend) . $out);
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif ($fieldset) {
|
2011-01-23 14:26:14 +00:00
|
|
|
return $this->Html->useTag('fieldset', $fieldsetClass, $out);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
}
|
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'`
|
2010-01-25 22:59:05 +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.
|
2009-10-17 05:29:16 +00:00
|
|
|
* - `options` - for widgets that take options e.g. radio, select
|
|
|
|
* - `error` - control the error message that is produced
|
|
|
|
* - `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.
|
2010-02-26 23:09:33 +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.
|
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1390/Automagic-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);
|
2008-12-25 22:13:06 +00:00
|
|
|
|
2009-10-15 04:44:38 +00:00
|
|
|
$options = array_merge(
|
2010-02-26 23:09:33 +00:00
|
|
|
array('before' => null, 'between' => null, 'after' => null, 'format' => null),
|
2009-10-15 04:44:38 +00:00
|
|
|
$this->_inputDefaults,
|
|
|
|
$options
|
|
|
|
);
|
2009-11-02 15:51:16 +00:00
|
|
|
|
2009-10-31 05:45:17 +00:00
|
|
|
$modelKey = $this->model();
|
|
|
|
$fieldKey = $this->field();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-01-13 04:27:17 +00:00
|
|
|
if (!isset($options['type'])) {
|
2010-02-20 17:49:24 +00:00
|
|
|
$magicType = true;
|
2008-05-30 11:40:08 +00:00
|
|
|
$options['type'] = 'text';
|
|
|
|
if (isset($options['options'])) {
|
|
|
|
$options['type'] = 'select';
|
2009-10-31 05:45:17 +00:00
|
|
|
} elseif (in_array($fieldKey, array('psword', 'passwd', 'password'))) {
|
2010-08-05 23:31:51 +00:00
|
|
|
$options['type'] = 'password';
|
|
|
|
} elseif (isset($options['checked'])) {
|
|
|
|
$options['type'] = 'checkbox';
|
2011-07-14 04:19:50 +00:00
|
|
|
} elseif ($fieldDef = $this->_introspectModel($modelKey, 'fields', $fieldKey)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$type = $fieldDef['type'];
|
2009-10-31 05:45:17 +00:00
|
|
|
$primaryKey = $this->fieldset[$modelKey]['key'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($type)) {
|
|
|
|
$map = array(
|
2010-08-05 23:31:51 +00:00
|
|
|
'string' => 'text', 'datetime' => 'datetime',
|
2008-12-25 22:13:06 +00:00
|
|
|
'boolean' => 'checkbox', 'timestamp' => 'datetime',
|
2010-08-05 23:31:51 +00:00
|
|
|
'text' => 'textarea', 'time' => 'time',
|
|
|
|
'date' => 'date', 'float' => 'text'
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($this->map[$type])) {
|
|
|
|
$options['type'] = $this->map[$type];
|
|
|
|
} elseif (isset($map[$type])) {
|
|
|
|
$options['type'] = $map[$type];
|
|
|
|
}
|
2009-10-31 05:45:17 +00:00
|
|
|
if ($fieldKey == $primaryKey) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$options['type'] = 'hidden';
|
|
|
|
}
|
|
|
|
}
|
2010-06-04 04:20:14 +00:00
|
|
|
if (preg_match('/_id$/', $fieldKey) && $options['type'] !== 'hidden') {
|
2010-01-13 04:27:17 +00:00
|
|
|
$options['type'] = 'select';
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-10-31 05:45:17 +00:00
|
|
|
if ($modelKey === $fieldKey) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$options['type'] = 'select';
|
|
|
|
if (!isset($options['multiple'])) {
|
|
|
|
$options['multiple'] = 'multiple';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-13 04:27:17 +00:00
|
|
|
$types = array('checkbox', 'radio', 'select');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-02-20 17:49:24 +00:00
|
|
|
if (
|
|
|
|
(!isset($options['options']) && in_array($options['type'], $types)) ||
|
|
|
|
(isset($magicType) && $options['type'] == 'text')
|
|
|
|
) {
|
2008-09-24 23:02:14 +00:00
|
|
|
$varName = Inflector::variable(
|
2009-10-31 05:45:17 +00:00
|
|
|
Inflector::pluralize(preg_replace('/_id$/', '', $fieldKey))
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
2010-07-15 03:09:35 +00:00
|
|
|
$varOptions = $this->_View->getVar($varName);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($varOptions)) {
|
2010-02-20 17:49:24 +00:00
|
|
|
if ($options['type'] !== 'radio') {
|
|
|
|
$options['type'] = 'select';
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$options['options'] = $varOptions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$autoLength = (!array_key_exists('maxlength', $options) && isset($fieldDef['length']));
|
2010-08-05 23:31:51 +00:00
|
|
|
if ($autoLength && $options['type'] == 'text') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$options['maxlength'] = $fieldDef['length'];
|
|
|
|
}
|
|
|
|
if ($autoLength && $fieldDef['type'] == 'float') {
|
|
|
|
$options['maxlength'] = array_sum(explode(',', $fieldDef['length']))+1;
|
|
|
|
}
|
|
|
|
|
2008-05-30 16:05:53 +00:00
|
|
|
$divOptions = array();
|
2010-02-20 18:04:15 +00:00
|
|
|
$div = $this->_extractOption('div', $options, true);
|
|
|
|
unset($options['div']);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!empty($div)) {
|
2008-05-30 16:05:53 +00:00
|
|
|
$divOptions['class'] = 'input';
|
2008-05-30 11:40:08 +00:00
|
|
|
$divOptions = $this->addClass($divOptions, $options['type']);
|
|
|
|
if (is_string($div)) {
|
|
|
|
$divOptions['class'] = $div;
|
|
|
|
} elseif (is_array($div)) {
|
|
|
|
$divOptions = array_merge($divOptions, $div);
|
|
|
|
}
|
2011-07-14 04:19:50 +00:00
|
|
|
if ($this->_introspectModel($modelKey, 'validates', $fieldKey)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$divOptions = $this->addClass($divOptions, 'required');
|
|
|
|
}
|
2008-05-30 15:07:32 +00:00
|
|
|
if (!isset($divOptions['tag'])) {
|
2008-06-20 11:57:56 +00:00
|
|
|
$divOptions['tag'] = 'div';
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$label = null;
|
|
|
|
if (isset($options['label']) && $options['type'] !== 'radio') {
|
|
|
|
$label = $options['label'];
|
|
|
|
unset($options['label']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options['type'] === 'radio') {
|
|
|
|
$label = false;
|
|
|
|
if (isset($options['options'])) {
|
2010-02-20 18:04:15 +00:00
|
|
|
$radioOptions = (array)$options['options'];
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($options['options']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($label !== false) {
|
2010-03-05 03:46:43 +00:00
|
|
|
$label = $this->_inputLabel($fieldName, $label, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-02-20 18:04:15 +00:00
|
|
|
$error = $this->_extractOption('error', $options, null);
|
|
|
|
unset($options['error']);
|
|
|
|
|
|
|
|
$selected = $this->_extractOption('selected', $options, null);
|
|
|
|
unset($options['selected']);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (isset($options['rows']) || isset($options['cols'])) {
|
|
|
|
$options['type'] = 'textarea';
|
|
|
|
}
|
|
|
|
|
2009-10-17 05:29:16 +00:00
|
|
|
if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time' || $options['type'] === 'select') {
|
|
|
|
$options += array('empty' => false);
|
|
|
|
}
|
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'];
|
2010-02-26 23:09:33 +00:00
|
|
|
$out = array_merge(
|
|
|
|
array('before' => null, 'label' => null, 'between' => null, 'input' => null, 'after' => null, 'error' => null),
|
|
|
|
array('before' => $options['before'], 'label' => $label, 'between' => $options['between'], 'after' => $options['after'])
|
|
|
|
);
|
|
|
|
$format = null;
|
|
|
|
if (is_array($options['format']) && in_array('input', $options['format'])) {
|
|
|
|
$format = $options['format'];
|
|
|
|
}
|
|
|
|
unset($options['type'], $options['before'], $options['between'], $options['after'], $options['format']);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case 'hidden':
|
2010-02-26 23:09:33 +00:00
|
|
|
$input = $this->hidden($fieldName, $options);
|
|
|
|
$format = array('input');
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($divOptions);
|
|
|
|
break;
|
|
|
|
case 'checkbox':
|
2010-02-26 23:09:33 +00:00
|
|
|
$input = $this->checkbox($fieldName, $options);
|
2010-02-28 03:48:21 +00:00
|
|
|
$format = $format ? $format : array('before', 'input', 'between', 'label', 'after', 'error');
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
case 'radio':
|
2010-02-26 23:09:33 +00:00
|
|
|
$input = $this->radio($fieldName, $radioOptions, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
case 'file':
|
2010-08-17 04:13:22 +00:00
|
|
|
$input = $this->file($fieldName, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
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']);
|
2010-08-05 18:14:36 +00:00
|
|
|
$input = $this->select($fieldName, $list, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
2010-08-05 23:31:51 +00:00
|
|
|
case 'time':
|
|
|
|
$options['value'] = $selected;
|
2010-08-05 23:30:49 +00:00
|
|
|
$input = $this->dateTime($fieldName, null, $timeFormat, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
case 'date':
|
2010-08-05 23:31:51 +00:00
|
|
|
$options['value'] = $selected;
|
2010-08-05 23:30:49 +00:00
|
|
|
$input = $this->dateTime($fieldName, $dateFormat, null, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
case 'datetime':
|
2010-08-05 23:31:51 +00:00
|
|
|
$options['value'] = $selected;
|
2010-08-05 23:30:49 +00:00
|
|
|
$input = $this->dateTime($fieldName, $dateFormat, $timeFormat, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
case 'textarea':
|
2010-02-26 23:09:33 +00:00
|
|
|
$input = $this->textarea($fieldName, $options + array('cols' => '30', 'rows' => '6'));
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
2010-08-17 04:13:22 +00:00
|
|
|
default:
|
|
|
|
$input = $this->{$type}($fieldName, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-02-26 23:09:33 +00:00
|
|
|
if ($type != 'hidden' && $error !== false) {
|
|
|
|
$errMsg = $this->error($fieldName, $error);
|
|
|
|
if ($errMsg) {
|
|
|
|
$divOptions = $this->addClass($divOptions, 'error');
|
|
|
|
$out['error'] = $errMsg;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-26 23:09:33 +00:00
|
|
|
|
|
|
|
$out['input'] = $input;
|
2010-02-28 07:35:00 +00:00
|
|
|
$format = $format ? $format : array('before', 'label', 'between', 'input', 'after', 'error');
|
2010-02-26 23:09:33 +00:00
|
|
|
$output = '';
|
|
|
|
foreach ($format as $element) {
|
|
|
|
$output .= $out[$element];
|
|
|
|
unset($out[$element]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($divOptions['tag'])) {
|
2008-06-20 11:57:56 +00:00
|
|
|
$tag = $divOptions['tag'];
|
|
|
|
unset($divOptions['tag']);
|
2010-02-26 23:09:33 +00:00
|
|
|
$output = $this->Html->tag($tag, $output, $divOptions);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-02-26 23:09:33 +00:00
|
|
|
return $output;
|
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
|
|
|
|
* @return the contents of the option or default
|
|
|
|
*/
|
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.
|
|
|
|
*
|
|
|
|
* @param array $options Options for the label element.
|
|
|
|
* @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');
|
|
|
|
if ($options['type'] === 'date' || $options['type'] === 'datetime') {
|
|
|
|
if (isset($options['dateFormat']) && $options['dateFormat'] === 'NONE') {
|
|
|
|
$labelAttributes['for'] .= 'Hour';
|
|
|
|
$idKey = 'hour';
|
|
|
|
} else {
|
|
|
|
$labelAttributes['for'] .= 'Month';
|
|
|
|
$idKey = 'month';
|
|
|
|
}
|
|
|
|
if (isset($options['id']) && isset($options['id'][$idKey])) {
|
|
|
|
$labelAttributes['for'] = $options['id'][$idKey];
|
|
|
|
}
|
|
|
|
} elseif ($options['type'] === 'time') {
|
|
|
|
$labelAttributes['for'] .= 'Hour';
|
|
|
|
if (isset($options['id']) && isset($options['id']['hour'])) {
|
|
|
|
$labelAttributes['for'] = $options['id']['hour'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2011-03-16 02:05:14 +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
|
|
|
|
* 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.
|
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1414/checkbox
|
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();
|
|
|
|
if(isset($options['default'])){
|
|
|
|
$valueOptions['default'] = $options['default'];
|
|
|
|
unset($options['default']);
|
|
|
|
}
|
|
|
|
|
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));
|
2009-10-30 05:34:41 +00:00
|
|
|
$output = "";
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-10-31 05:04:33 +00:00
|
|
|
if (empty($options['value'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$options['value'] = 1;
|
2010-06-09 15:03:23 +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(
|
|
|
|
'id' => $options['id'] . '_', 'name' => $options['name'],
|
|
|
|
'value' => '0', 'secure' => false
|
|
|
|
);
|
|
|
|
if (isset($options['disabled']) && $options['disabled'] == true) {
|
|
|
|
$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
|
|
|
|
2011-01-23 14:26:14 +00:00
|
|
|
return $output . $this->Html->useTag('checkbox', $options['name'], array_diff_key($options, array('name' => '')));
|
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
|
|
|
|
* 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
|
|
|
|
* - `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
|
2010-08-05 23:31:51 +00:00
|
|
|
* a hidden input with a value of ''. This is useful for creating radio sets that non-continuous
|
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.
|
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1429/radio
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function radio($fieldName, $options = array(), $attributes = array()) {
|
2008-09-21 04:06:37 +00:00
|
|
|
$attributes = $this->_initInputField($fieldName, $attributes);
|
2008-05-30 11:40:08 +00:00
|
|
|
$legend = false;
|
2011-03-13 23:35:37 +00:00
|
|
|
$disabled = array();
|
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
|
|
|
}
|
|
|
|
$label = true;
|
|
|
|
|
|
|
|
if (isset($attributes['label'])) {
|
|
|
|
$label = $attributes['label'];
|
|
|
|
unset($attributes['label']);
|
|
|
|
}
|
|
|
|
$inbetween = null;
|
|
|
|
|
|
|
|
if (isset($attributes['separator'])) {
|
|
|
|
$inbetween = $attributes['separator'];
|
|
|
|
unset($attributes['separator']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($attributes['value'])) {
|
|
|
|
$value = $attributes['value'];
|
|
|
|
} else {
|
|
|
|
$value = $this->value($fieldName);
|
|
|
|
}
|
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']);
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($options as $optValue => $optTitle) {
|
|
|
|
$optionsHere = array('value' => $optValue);
|
|
|
|
|
|
|
|
if (isset($value) && $optValue == $value) {
|
|
|
|
$optionsHere['checked'] = 'checked';
|
|
|
|
}
|
2011-03-13 23:35:37 +00:00
|
|
|
if (!empty($disabled) && in_array($optValue, $disabled)) {
|
|
|
|
$optionsHere['disabled'] = true;
|
|
|
|
}
|
2008-09-21 04:06:37 +00:00
|
|
|
$tagName = Inflector::camelize(
|
2010-10-10 17:46:48 +00:00
|
|
|
$attributes['id'] . '_' . Inflector::slug($optValue)
|
2008-09-21 04:06:37 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($label) {
|
2011-01-23 14:26:14 +00:00
|
|
|
$optTitle = $this->Html->useTag('label', $tagName, '', $optTitle);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-01-23 14:26:14 +00:00
|
|
|
$allOptions = array_merge($attributes, $optionsHere);
|
|
|
|
$out[] = $this->Html->useTag('radio', $attributes['name'], $tagName,
|
|
|
|
array_diff_key($allOptions, array('name' => '', 'type' => '', 'id' => '')),
|
|
|
|
$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(
|
|
|
|
'id' => $attributes['id'] . '_', 'value' => '', 'name' => $attributes['name']
|
|
|
|
));
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-11-19 22:13:35 +00:00
|
|
|
$out = $hidden . implode($inbetween, $out);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($legend) {
|
2011-01-23 14:26:14 +00:00
|
|
|
$out = $this->Html->useTag('fieldset', '', $this->Html->useTag('legend', $legend) . $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
|
2010-08-17 04:06:23 +00:00
|
|
|
* of various types. e.g. `$this->Form->text();` will create `<input type="text" />` while
|
|
|
|
* `$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);
|
2011-01-23 14:26:14 +00:00
|
|
|
return $this->Html->useTag('input', $options['name'], array_diff_key($options, array('name' => '')));
|
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
|
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1433/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']);
|
|
|
|
}
|
2011-01-23 14:26:14 +00:00
|
|
|
return $this->Html->useTag('textarea', $options['name'], array_diff_key($options, array('type' => '', 'name' => '')), $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
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1425/hidden
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function hidden($fieldName, $options = array()) {
|
2008-09-24 23:02:14 +00:00
|
|
|
$secure = true;
|
|
|
|
|
|
|
|
if (isset($options['secure'])) {
|
|
|
|
$secure = $options['secure'];
|
|
|
|
unset($options['secure']);
|
|
|
|
}
|
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
|
|
|
|
2011-06-25 05:40:50 +00:00
|
|
|
if ($secure && $secure !== self::SECURE_SKIP) {
|
2011-06-09 01:14:22 +00:00
|
|
|
$this->__secure(true, null, '' . $options['value']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-04-21 01:37:47 +00:00
|
|
|
return $this->Html->useTag('hidden', $options['name'], array_diff_key($options, array('name' => '')));
|
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.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1424/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-06-09 01:14:22 +00:00
|
|
|
$this->__secure($secure, array_merge($field, array($suffix)));
|
2008-09-29 16:23:39 +00:00
|
|
|
}
|
|
|
|
|
2011-01-23 14:26:14 +00:00
|
|
|
return $this->Html->useTag('file', $options['name'], array_diff_key($options, array('name' => '')));
|
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-02-13 04:58:35 +00:00
|
|
|
* Creates a `<button>` tag. The type attribute defaults to `type="submit"`
|
|
|
|
* 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.
|
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1415/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'])) {
|
|
|
|
$this->__secure($options['secure'], $options['name']);
|
|
|
|
}
|
2011-01-23 14:26:14 +00:00
|
|
|
return $this->Html->useTag('button', $options['type'], array_diff_key($options, array('type' => '')), $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
|
|
|
*
|
2011-05-13 02:41:35 +00:00
|
|
|
* This method creates a `<form>` element. So do not use this method in some opened form.
|
|
|
|
* 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
|
|
|
|
* @param mixed $url URL as string or array
|
|
|
|
* @param array $options Array of options and HTML attributes.
|
|
|
|
* @return string A HTML button tag.
|
|
|
|
*/
|
|
|
|
public function postButton($title, $url, $options = array()) {
|
2010-10-31 00:39:31 +00:00
|
|
|
$out = $this->create(false, array('id' => false, 'url' => $url, 'style' => 'display:none;'));
|
2010-10-18 19:53:27 +00:00
|
|
|
if (isset($options['data']) && is_array($options['data'])) {
|
|
|
|
foreach ($options['data'] as $key => $value) {
|
|
|
|
$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
|
|
|
/**
|
2011-05-13 02:41:35 +00:00
|
|
|
* Creates an HTML link, but access the url using method POST.
|
|
|
|
* 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
|
2011-05-13 02:41:35 +00:00
|
|
|
* - `confirm` - Can be used instead of $confirmMessage.
|
2010-10-18 23:51:17 +00:00
|
|
|
* - Other options is the same of HtmlHelper::link() method.
|
|
|
|
* - The option `onclick` will be replaced.
|
|
|
|
*
|
|
|
|
* @param string $title The content to be wrapped by <a> tags.
|
|
|
|
* @param mixed $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
|
|
|
|
* @param array $options Array of HTML attributes.
|
|
|
|
* @param string $confirmMessage JavaScript confirmation message.
|
|
|
|
* @return string An `<a />` element.
|
|
|
|
*/
|
|
|
|
public function postLink($title, $url = null, $options = array(), $confirmMessage = false) {
|
|
|
|
if (!empty($options['confirm'])) {
|
|
|
|
$confirmMessage = $options['confirm'];
|
|
|
|
unset($options['confirm']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$formName = uniqid('post_');
|
2010-10-31 00:39:31 +00:00
|
|
|
$out = $this->create(false, array('url' => $url, 'name' => $formName, 'id' => $formName, 'style' => 'display:none;'));
|
2010-10-18 23:51:17 +00:00
|
|
|
if (isset($options['data']) && is_array($options['data'])) {
|
|
|
|
foreach ($options['data'] as $key => $value) {
|
|
|
|
$out .= $this->hidden($key, array('value' => $value, 'id' => false));
|
|
|
|
}
|
|
|
|
unset($options['data']);
|
|
|
|
}
|
|
|
|
$out .= $this->end();
|
|
|
|
|
|
|
|
$url = '#';
|
|
|
|
$onClick = 'document.' . $formName . '.submit();';
|
|
|
|
if ($confirmMessage) {
|
|
|
|
$confirmMessage = str_replace(array("'", '"'), array("\'", '\"'), $confirmMessage);
|
|
|
|
$options['onclick'] = "if (confirm('{$confirmMessage}')) { {$onClick} }";
|
|
|
|
} else {
|
|
|
|
$options['onclick'] = $onClick;
|
|
|
|
}
|
|
|
|
$options['onclick'] .= ' event.returnValue = false; return false;';
|
|
|
|
|
|
|
|
$out .= $this->Html->link($title, $url, $options);
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-02-25 03:58:09 +00:00
|
|
|
* Creates a submit button element. This method will generate `<input />` elements that
|
2010-02-28 07:35:00 +00:00
|
|
|
* 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
|
|
|
|
*
|
2010-02-28 07:35:00 +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.
|
|
|
|
* - `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
|
|
|
|
*
|
2010-02-28 07:35:00 +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.
|
2010-02-25 03:45:49 +00:00
|
|
|
* @param array $options Array of options. See above.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string A HTML submit button
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1431/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-03-12 19:02:56 +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'])) {
|
|
|
|
$this->__secure($options['secure'], $options['name']);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (strpos($caption, '://') !== false) {
|
2009-10-20 03:44:50 +00:00
|
|
|
unset($options['type']);
|
2011-01-23 14:26:14 +00:00
|
|
|
$out .= $before . $this->Html->useTag('submitimage', $caption, $options) . $after;
|
2008-09-21 04:06:37 +00:00
|
|
|
} elseif (preg_match('/\.(jpg|jpe|jpeg|gif|png|ico)$/', $caption)) {
|
2009-10-20 03:44:50 +00:00
|
|
|
unset($options['type']);
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($caption{0} !== '/') {
|
|
|
|
$url = $this->webroot(IMAGES_URL . $caption);
|
|
|
|
} else {
|
|
|
|
$caption = trim($caption, '/');
|
|
|
|
$url = $this->webroot($caption);
|
|
|
|
}
|
2011-01-23 14:26:14 +00:00
|
|
|
$out .= $before . $this->Html->useTag('submitimage', $url, $options) . $after;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$options['value'] = $caption;
|
2011-01-23 14:26:14 +00:00
|
|
|
$out .= $before . $this->Html->useTag('submit', $options) . $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.
|
2009-10-17 04:55:17 +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.
|
2009-10-17 05:29:16 +00:00
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
|
|
|
* 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.
|
2010-10-16 17:40:27 +00:00
|
|
|
* - `class` - When using multiple = checkbox the classname to apply to the divs. Defaults to 'checkbox'.
|
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:
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* $options = array(1 => 'one', 2 => 'two);
|
|
|
|
* $this->Form->select('Model.field', $options));
|
|
|
|
* }}}
|
|
|
|
*
|
|
|
|
* While a nested options array will create optgroups with options inside them.
|
|
|
|
* {{{
|
|
|
|
* $options = array(
|
2010-08-05 23:31:51 +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);
|
|
|
|
* }}}
|
|
|
|
*
|
|
|
|
* In the above `2 => 'fred'` will not generate an option element. You should enable the `showParents`
|
|
|
|
* attribute to show the fred option.
|
|
|
|
*
|
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
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1430/select
|
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,
|
|
|
|
'hiddenField' => true
|
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
|
|
|
|
|
|
|
if (is_string($options) && isset($this->__options[$options])) {
|
|
|
|
$options = $this->__generateOptions($options);
|
|
|
|
} elseif (!is_array($options)) {
|
|
|
|
$options = array();
|
|
|
|
}
|
|
|
|
if (isset($attributes['type'])) {
|
|
|
|
unset($attributes['type']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($selected)) {
|
|
|
|
$selected = $attributes['value'];
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
'name' => $attributes['name']
|
|
|
|
);
|
|
|
|
$select[] = $this->hidden(null, $hiddenAttributes);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-05-04 05:40:23 +00:00
|
|
|
$tag = 'selectstart';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-12-25 22:13:06 +00:00
|
|
|
if (!empty($tag) || isset($template)) {
|
2010-04-20 04:02:58 +00:00
|
|
|
if (!isset($secure) || $secure == true) {
|
2011-06-09 01:14:22 +00:00
|
|
|
$this->__secure(true);
|
2010-04-08 22:53:07 +00:00
|
|
|
}
|
2011-01-23 14:26:14 +00:00
|
|
|
$select[] = $this->Html->useTag($tag, $attributes['name'], array_diff_key($attributes, array('name' => '', 'value' => '')));
|
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;
|
2008-05-30 11:40:08 +00:00
|
|
|
$options = array_reverse($options, true);
|
|
|
|
$options[''] = $showEmpty;
|
|
|
|
$options = array_reverse($options, true);
|
|
|
|
}
|
2008-12-23 08:13:00 +00:00
|
|
|
|
2011-06-07 02:32:55 +00:00
|
|
|
if (!$id) {
|
|
|
|
$attributes['id'] = Inflector::camelize($attributes['id']);
|
|
|
|
}
|
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
$select = array_merge($select, $this->__selectOptions(
|
|
|
|
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'],
|
|
|
|
'id' => $attributes['id']
|
|
|
|
)
|
2008-09-24 23:02:14 +00:00
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-12-23 08:13:00 +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
|
|
|
|
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
|
|
|
*
|
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
|
|
|
* 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.
|
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1419/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);
|
2010-08-05 21:47:14 +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) {
|
|
|
|
$attributes['value'] = date('d', strtotime($attributes['value']));
|
|
|
|
} elseif ($attributes['value'] === false) {
|
|
|
|
$attributes['value'] = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-08-05 18:14:36 +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
|
|
|
*
|
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
|
|
|
* 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
|
|
|
|
* @param integer $minYear First year in sequence
|
|
|
|
* @param integer $maxYear Last year in sequence
|
|
|
|
* @param array $attributes Attribute array for the select elements.
|
2009-10-20 03:34:34 +00:00
|
|
|
* @return string Completed year select input
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1416/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)) {
|
|
|
|
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') {
|
|
|
|
$attributes['value'] = date('Y', strtotime($attributes['value']));
|
|
|
|
} elseif ($attributes['value'] === false) {
|
|
|
|
$attributes['value'] = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-27 20:18:53 +00:00
|
|
|
$yearOptions = array('min' => $minYear, 'max' => $maxYear, 'order' => 'desc');
|
|
|
|
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(
|
2010-01-27 20:18:53 +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.
|
|
|
|
* If a array, the given array will be used.
|
2009-10-17 05:29:16 +00:00
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
|
|
|
* 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.
|
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1417/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);
|
2010-08-05 21:47:14 +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) {
|
|
|
|
$attributes['value'] = date('m', strtotime($attributes['value']));
|
|
|
|
} elseif ($attributes['value'] === false) {
|
|
|
|
$attributes['value'] = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$defaults = array('monthNames' => true);
|
|
|
|
$attributes = array_merge($defaults, (array) $attributes);
|
|
|
|
$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",
|
|
|
|
$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
|
|
|
*
|
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
|
|
|
* 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
|
|
|
|
* @param boolean $format24Hours True for 24 hours format
|
|
|
|
* @param array $attributes List of HTML attributes
|
2009-10-20 03:34:34 +00:00
|
|
|
* @return string Completed hour select input
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1420/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);
|
2010-08-05 21:47:14 +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) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($format24Hours) {
|
2010-08-05 18:14:36 +00:00
|
|
|
$attributes['value'] = date('H', strtotime($attributes['value']));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-08-05 18:14:36 +00:00
|
|
|
$attributes['value'] = date('g', strtotime($attributes['value']));
|
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
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
return $this->select(
|
|
|
|
$fieldName . ".hour",
|
|
|
|
$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
|
|
|
*
|
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
|
|
|
* 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
|
|
|
|
* @param string $attributes Array of Attributes
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return string Completed minute select input.
|
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1421/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);
|
2010-08-05 21:47:14 +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) {
|
|
|
|
$attributes['value'] = date('i', strtotime($attributes['value']));
|
|
|
|
} 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(
|
|
|
|
$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.
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access private
|
2009-10-20 01:58:37 +00:00
|
|
|
*/
|
2010-08-05 21:47:14 +00:00
|
|
|
function __dateTimeSelected($select, $fieldName, $attributes) {
|
|
|
|
if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
|
2009-10-20 01:58:37 +00:00
|
|
|
if (is_array($value) && isset($value[$select])) {
|
2010-08-05 21:47:14 +00:00
|
|
|
$attributes['value'] = $value[$select];
|
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
|
|
|
*
|
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
|
|
|
* 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
|
|
|
|
* @param string $attributes Array of Attributes
|
|
|
|
* @param bool $showEmpty Show/Hide an empty option
|
2009-10-20 03:34:34 +00:00
|
|
|
* @return string Completed meridian select input
|
2010-01-25 22:59:05 +00:00
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1422/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)) {
|
|
|
|
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 {
|
2010-08-05 18:14:36 +00:00
|
|
|
$attributes['value'] = date('a', strtotime($value));
|
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(
|
|
|
|
$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.
|
|
|
|
* If a 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 '-'
|
2009-10-17 05:29:16 +00:00
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
|
|
|
* that string is displayed as the empty element.
|
2010-10-10 17:46:48 +00:00
|
|
|
* - `value` | `default` The default value to be used by the input. A value in `$this->data`
|
2010-08-10 03:49:18 +00:00
|
|
|
* 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
|
2009-10-15 05:12:02 +00:00
|
|
|
* @param string $dateFormat DMY, MDY, YMD.
|
|
|
|
* @param string $timeFormat 12, 24.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $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.
|
|
|
|
* @access public
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1418/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();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 23:30:49 +00:00
|
|
|
if (!empty($attributes['value'])) {
|
|
|
|
if (is_array($attributes['value'])) {
|
|
|
|
extract($attributes['value']);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-08-05 23:30:49 +00:00
|
|
|
if (is_numeric($attributes['value'])) {
|
|
|
|
$attributes['value'] = strftime('%Y-%m-%d %H:%M:%S', $attributes['value']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$meridian = 'am';
|
2010-08-05 23:30:49 +00:00
|
|
|
$pos = strpos($attributes['value'], '-');
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($pos !== false) {
|
2010-08-05 23:30:49 +00:00
|
|
|
$date = explode('-', $attributes['value']);
|
2008-05-30 11:40:08 +00:00
|
|
|
$days = explode(' ', $date[2]);
|
|
|
|
$day = $days[0];
|
|
|
|
$month = $date[1];
|
|
|
|
$year = $date[0];
|
|
|
|
} else {
|
2010-08-05 23:30:49 +00:00
|
|
|
$days[1] = $attributes['value'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-10-15 05:12:02 +00:00
|
|
|
if (!empty($timeFormat)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$time = explode(':', $days[1]);
|
|
|
|
|
2011-04-20 20:15:30 +00:00
|
|
|
if (($time[0] > 12) && $timeFormat == '12') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$time[0] = $time[0] - 12;
|
|
|
|
$meridian = 'pm';
|
|
|
|
} elseif ($time[0] == '00' && $timeFormat == '12') {
|
|
|
|
$time[0] = 12;
|
2011-07-02 17:01:08 +00:00
|
|
|
} elseif ($time[0] >= 12) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$meridian = 'pm';
|
|
|
|
}
|
|
|
|
if ($time[0] == 0 && $timeFormat == '12') {
|
|
|
|
$time[0] = 12;
|
|
|
|
}
|
2011-02-05 11:43:00 +00:00
|
|
|
$hour = $min = null;
|
|
|
|
if (isset($time[1])) {
|
|
|
|
$hour = $time[0];
|
|
|
|
$min = $time[1];
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-06-14 21:40:01 +00:00
|
|
|
|
2010-03-05 03:46:43 +00:00
|
|
|
$elements = array('Day', 'Month', 'Year', 'Hour', 'Minute', 'Meridian');
|
2008-09-24 23:02:14 +00:00
|
|
|
$defaults = array(
|
|
|
|
'minYear' => null, 'maxYear' => null, 'separator' => '-',
|
|
|
|
'interval' => 1, 'monthNames' => true
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
$attributes = array_merge($defaults, (array) $attributes);
|
|
|
|
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'];
|
|
|
|
$attributes = array_diff_key($attributes, $defaults);
|
|
|
|
|
|
|
|
if (isset($attributes['id'])) {
|
|
|
|
if (is_string($attributes['id'])) {
|
|
|
|
// build out an array version
|
|
|
|
foreach ($elements as $element) {
|
|
|
|
$selectAttrName = 'select' . $element . 'Attr';
|
2008-07-03 00:46:42 +00:00
|
|
|
${$selectAttrName} = $attributes;
|
2008-05-30 11:40:08 +00:00
|
|
|
${$selectAttrName}['id'] = $attributes['id'] . $element;
|
|
|
|
}
|
|
|
|
} elseif (is_array($attributes['id'])) {
|
|
|
|
// check for missing ones and build selectAttr for each element
|
2010-03-05 03:46:43 +00:00
|
|
|
$attributes['id'] += array(
|
|
|
|
'month' => '', 'year' => '', 'day' => '',
|
|
|
|
'hour' => '', 'minute' => '', 'meridian' => ''
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($elements as $element) {
|
|
|
|
$selectAttrName = 'select' . $element . 'Attr';
|
|
|
|
${$selectAttrName} = $attributes;
|
|
|
|
${$selectAttrName}['id'] = $attributes['id'][strtolower($element)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// build the selectAttrName with empty id's to pass
|
|
|
|
foreach ($elements as $element) {
|
|
|
|
$selectAttrName = 'select' . $element . 'Attr';
|
|
|
|
${$selectAttrName} = $attributes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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':
|
|
|
|
$selectYearAttr['value'] = $year;
|
2009-10-15 05:12:02 +00:00
|
|
|
$selects[] = $this->year(
|
2010-08-05 21:56:29 +00:00
|
|
|
$fieldName, $minYear, $maxYear, $selectYearAttr
|
2009-10-15 05:12:02 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'M':
|
2010-08-05 23:31:51 +00:00
|
|
|
$selectMonthAttr['value'] = $month;
|
2009-10-15 05:12:02 +00:00
|
|
|
$selectMonthAttr['monthNames'] = $monthNames;
|
2010-08-05 22:00:15 +00:00
|
|
|
$selects[] = $this->month($fieldName, $selectMonthAttr);
|
2009-10-15 05:12:02 +00:00
|
|
|
break;
|
2010-08-05 23:31:51 +00:00
|
|
|
case 'D':
|
|
|
|
$selectDayAttr['value'] = $day;
|
2010-08-05 21:47:14 +00:00
|
|
|
$selects[] = $this->day($fieldName, $selectDayAttr);
|
2009-10-15 05:12:02 +00:00
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-15 05:12:02 +00:00
|
|
|
$opt = implode($separator, $selects);
|
|
|
|
|
2009-09-04 02:47:17 +00:00
|
|
|
if (!empty($interval) && $interval > 1 && !empty($min)) {
|
|
|
|
$min = round($min * (1 / $interval)) * $interval;
|
|
|
|
}
|
|
|
|
$selectMinuteAttr['interval'] = $interval;
|
2008-10-23 00:10:44 +00:00
|
|
|
switch ($timeFormat) {
|
2010-08-05 23:31:51 +00:00
|
|
|
case '24':
|
|
|
|
$selectHourAttr['value'] = $hour;
|
|
|
|
$selectMinuteAttr['value'] = $min;
|
2010-08-05 22:04:00 +00:00
|
|
|
$opt .= $this->hour($fieldName, true, $selectHourAttr) . ':' .
|
2010-08-05 22:07:08 +00:00
|
|
|
$this->minute($fieldName, $selectMinuteAttr);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
2010-08-05 23:31:51 +00:00
|
|
|
case '12':
|
|
|
|
$selectHourAttr['value'] = $hour;
|
|
|
|
$selectMinuteAttr['value'] = $min;
|
|
|
|
$selectMeridianAttr['value'] = $meridian;
|
2010-08-05 22:04:00 +00:00
|
|
|
$opt .= $this->hour($fieldName, false, $selectHourAttr) . ':' .
|
2010-08-05 22:07:08 +00:00
|
|
|
$this->minute($fieldName, $selectMinuteAttr) . ' ' .
|
2010-08-05 23:24:17 +00:00
|
|
|
$this->meridian($fieldName, $selectMeridianAttr);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$opt .= '';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $opt;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-07-01 11:39:03 +00:00
|
|
|
/**
|
|
|
|
* Add support for special HABTM syntax.
|
|
|
|
*
|
|
|
|
* Sets this helper's model and field properties to the dot-separated value-pair in $entity.
|
|
|
|
*
|
|
|
|
* @param mixed $entity A field name, like "ModelName.fieldName" or "ModelName.ID.fieldName"
|
|
|
|
* @param boolean $setScope Sets the view scope to the model specified in $tagValue
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function setEntity($entity, $setScope = false) {
|
|
|
|
parent::setEntity($entity, $setScope);
|
|
|
|
$parts = explode('.', $entity);
|
2011-07-14 04:19:50 +00:00
|
|
|
$field = $this->_introspectModel($this->_modelScope, 'fields', $parts[0]);
|
|
|
|
if (!empty($field) && $field['type'] === 'multiple') {
|
2011-07-01 11:39:03 +00:00
|
|
|
$this->_entityPath = $parts[0] . '.' . $parts[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets the input field name for the current tag
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
* @param string $key
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _name($options = array(), $field = null, $key = 'name') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->requestType == 'get') {
|
|
|
|
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;
|
|
|
|
} else {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
* @access private
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-08-05 22:48:17 +00:00
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2008-09-24 23:02:14 +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) {
|
2010-01-26 15:55:11 +00:00
|
|
|
if (
|
2010-08-05 22:48:17 +00:00
|
|
|
(!$selectedIsArray && !$selectedIsEmpty && (string)$attributes['value'] == (string)$name) ||
|
|
|
|
($selectedIsArray && in_array($name, $attributes['value']))
|
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
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($attributes['style'] === 'checkbox') {
|
|
|
|
$htmlOptions['value'] = $name;
|
|
|
|
|
2011-06-07 02:32:55 +00:00
|
|
|
$tagName = $attributes['id'] . Inflector::camelize(Inflector::slug($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 {
|
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
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __generateOptions($name, $options = array()) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'hour':
|
|
|
|
for ($i = 1; $i <= 12; $i++) {
|
|
|
|
$data[sprintf('%02d', $i)] = $i;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'hour24':
|
|
|
|
for ($i = 0; $i <= 23; $i++) {
|
|
|
|
$data[sprintf('%02d', $i)] = $i;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'meridian':
|
|
|
|
$data = array('am' => 'am', 'pm' => 'pm');
|
|
|
|
break;
|
|
|
|
case 'day':
|
2008-06-20 11:57:56 +00:00
|
|
|
$min = 1;
|
|
|
|
$max = 31;
|
|
|
|
|
|
|
|
if (isset($options['min'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$min = $options['min'];
|
|
|
|
}
|
2008-06-20 11:57:56 +00:00
|
|
|
if (isset($options['max'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$max = $options['max'];
|
2008-06-20 11:57:56 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
for ($i = $min; $i <= $max; $i++) {
|
|
|
|
$data[sprintf('%02d', $i)] = $i;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'month':
|
2009-11-02 15:51:16 +00:00
|
|
|
if ($options['monthNames'] === true) {
|
2011-03-12 19:02:56 +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');
|
2009-11-02 15:51:16 +00:00
|
|
|
} else if (is_array($options['monthNames'])) {
|
|
|
|
$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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'year':
|
|
|
|
$current = intval(date('Y'));
|
|
|
|
|
|
|
|
if (!isset($options['min'])) {
|
|
|
|
$min = $current - 20;
|
|
|
|
} else {
|
|
|
|
$min = $options['min'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($options['max'])) {
|
|
|
|
$max = $current + 20;
|
|
|
|
} else {
|
|
|
|
$max = $options['max'];
|
|
|
|
}
|
|
|
|
if ($min > $max) {
|
|
|
|
list($min, $max) = array($max, $min);
|
|
|
|
}
|
|
|
|
for ($i = $min; $i <= $max; $i++) {
|
|
|
|
$data[$i] = $i;
|
|
|
|
}
|
2010-01-27 20:18:53 +00:00
|
|
|
if ($options['order'] != 'asc') {
|
|
|
|
$data = array_reverse($data, true);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
$this->__options[$name] = $data;
|
|
|
|
return $this->__options[$name];
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-21 04:06:37 +00:00
|
|
|
/**
|
|
|
|
* Sets field defaults and adds field to form security input hash
|
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
|
|
|
*
|
2010-07-19 22:32:32 +00:00
|
|
|
* - `secure` - boolean whether or not the field should be added to the security fields.
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
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
|
|
|
|
2011-06-25 05:40:50 +00:00
|
|
|
$result = parent::_initInputField($field, $options);
|
|
|
|
if ($secure === self::SECURE_SKIP) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2011-01-28 02:05:30 +00:00
|
|
|
$fieldName = null;
|
2011-06-25 05:40:50 +00:00
|
|
|
if (!empty($options['name'])) {
|
2011-01-28 02:05:30 +00:00
|
|
|
preg_match_all('/\[(.*?)\]/', $options['name'], $matches);
|
|
|
|
if (isset($matches[1])) {
|
|
|
|
$fieldName = $matches[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-25 05:40:50 +00:00
|
|
|
$this->__secure($secure, $fieldName);
|
2008-09-21 04:06:37 +00:00
|
|
|
return $result;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|