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.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2009, 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.
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2009-11-06 06:51:51 +00:00
|
|
|
* @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
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Form helper library.
|
|
|
|
*
|
|
|
|
* Automatic generation of HTML FORMs from given data.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
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
|
|
|
|
*/
|
|
|
|
var $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
|
|
|
|
*/
|
2009-10-20 05:32:24 +00:00
|
|
|
var $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
|
|
|
|
*/
|
|
|
|
var $__options = array(
|
|
|
|
'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
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $fields = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Defines the type of form being created. Set by FormHelper::create().
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
var $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
|
|
|
|
*/
|
2009-10-20 05:32:24 +00:00
|
|
|
var $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
|
|
|
|
*/
|
|
|
|
var $_inputDefaults = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Introspects model information and extracts information related
|
|
|
|
* to validation, field length and field type. Appends information into
|
|
|
|
* $this->fieldset.
|
|
|
|
*
|
|
|
|
* @return Model Returns a model instance
|
|
|
|
* @access protected
|
|
|
|
*/
|
2009-10-20 05:32:24 +00:00
|
|
|
function &_introspectModel($model) {
|
|
|
|
$object = null;
|
|
|
|
if (is_string($model) && strpos($model, '.') !== false) {
|
|
|
|
$path = explode('.', $model);
|
|
|
|
$model = end($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ClassRegistry::isKeySet($model)) {
|
|
|
|
$object =& ClassRegistry::getObject($model);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($object)) {
|
|
|
|
$fields = $object->schema();
|
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
unset($fields[$key]);
|
|
|
|
$fields[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($object->hasAndBelongsToMany)) {
|
|
|
|
foreach ($object->hasAndBelongsToMany as $alias => $assocData) {
|
|
|
|
$fields[$alias] = array('type' => 'multiple');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$validates = array();
|
|
|
|
if (!empty($object->validate)) {
|
|
|
|
foreach ($object->validate as $validateField => $validateProperties) {
|
|
|
|
if (is_array($validateProperties)) {
|
|
|
|
$dims = Set::countDim($validateProperties);
|
|
|
|
if (($dims == 1 && !isset($validateProperties['required']) || (array_key_exists('required', $validateProperties) && $validateProperties['required'] !== false))) {
|
|
|
|
$validates[] = $validateField;
|
|
|
|
} elseif ($dims > 1) {
|
|
|
|
foreach ($validateProperties as $rule => $validateProp) {
|
|
|
|
if (is_array($validateProp) && (array_key_exists('required', $validateProp) && $validateProp['required'] !== false)) {
|
|
|
|
$validates[] = $validateField;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$defaults = array('fields' => array(), 'key' => 'id', 'validates' => array());
|
|
|
|
$key = $object->primaryKey;
|
|
|
|
$this->fieldset[$object->name] = array_merge($defaults,compact('fields', 'key', 'validates'));
|
|
|
|
}
|
2009-10-22 03:45:42 +00:00
|
|
|
|
2009-10-20 05:32:24 +00:00
|
|
|
return $object;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns an HTML FORM element.
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2009-10-15 05:12:02 +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
|
|
|
|
* - `action` The Action the form submits to. Can be a string or array,
|
|
|
|
* - `url` The url the form submits to. Can be a string or a url array,
|
|
|
|
* - `default` Allows for the creation of Ajax forms.
|
|
|
|
* - `onsubmit` Used in conjunction with 'default' to create ajax forms.
|
|
|
|
* - `inputDefaults' set the default $options for FormHelper::input(). Any options that would
|
2009-10-15 05:12:02 +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.
|
|
|
|
*/
|
|
|
|
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
|
|
|
$view =& ClassRegistry::getObject('view');
|
|
|
|
|
|
|
|
if (is_array($model) && empty($options)) {
|
|
|
|
$options = $model;
|
|
|
|
$model = null;
|
2009-10-20 05:32:24 +00:00
|
|
|
} elseif (empty($model) && $model !== false && !empty($this->params['models'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$model = $this->params['models'][0];
|
2009-10-20 05:32:24 +00:00
|
|
|
$this->defaultModel = $this->params['models'][0];
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif (empty($model) && empty($this->params['models'])) {
|
|
|
|
$model = false;
|
|
|
|
}
|
2009-10-22 03:45:42 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$models = ClassRegistry::keys();
|
|
|
|
foreach ($models as $currentModel) {
|
|
|
|
if (ClassRegistry::isKeySet($currentModel)) {
|
|
|
|
$currentObject =& ClassRegistry::getObject($currentModel);
|
|
|
|
if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) {
|
|
|
|
$this->validationErrors[Inflector::camelize($currentModel)] =& $currentObject->validationErrors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-20 05:32:24 +00:00
|
|
|
$object =& $this->_introspectModel($model);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->setEntity($model . '.', true);
|
2009-10-22 03:45:42 +00:00
|
|
|
|
2009-10-20 05:32:24 +00:00
|
|
|
if (isset($this->fieldset[$this->model()]['key'])) {
|
|
|
|
$data = $this->fieldset[$this->model()];
|
|
|
|
$recordExists = (
|
|
|
|
isset($this->data[$model]) &&
|
|
|
|
!empty($this->data[$model][$data['key']])
|
|
|
|
);
|
2009-10-22 03:45:42 +00:00
|
|
|
|
2009-10-20 05:32:24 +00:00
|
|
|
if ($recordExists) {
|
|
|
|
$created = true;
|
|
|
|
$id = $this->data[$model][$data['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']);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (empty($options['url']) || is_array($options['url'])) {
|
|
|
|
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));
|
|
|
|
} elseif (!empty($this->params['controller'])) {
|
|
|
|
$options['url']['controller'] = Inflector::underscore($this->params['controller']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($options['action'])) {
|
2009-10-30 04:45:31 +00:00
|
|
|
$options['action'] = $this->params['action'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$actionDefaults = array(
|
|
|
|
'plugin' => $this->plugin,
|
|
|
|
'controller' => $view->viewPath,
|
|
|
|
'action' => $options['action'],
|
2009-09-30 02:46:45 +00:00
|
|
|
0 => $id
|
2008-05-30 11:40:08 +00:00
|
|
|
);
|
|
|
|
if (!empty($options['action']) && !isset($options['id'])) {
|
|
|
|
$options['id'] = $model . Inflector::camelize($options['action']) . 'Form';
|
|
|
|
}
|
|
|
|
$options['action'] = array_merge($actionDefaults, (array)$options['url']);
|
|
|
|
} 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(
|
|
|
|
'name' => '_method', 'value' => strtoupper($options['type']), 'id' => null
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
default:
|
|
|
|
$htmlAttributes['method'] = 'post';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$this->requestType = strtolower($options['type']);
|
|
|
|
|
|
|
|
$htmlAttributes['action'] = $this->url($options['action']);
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (isset($this->params['_Token']) && !empty($this->params['_Token'])) {
|
2008-12-25 22:13:06 +00:00
|
|
|
$append .= $this->hidden('_Token.key', array(
|
|
|
|
'value' => $this->params['_Token']['key'], 'id' => 'Token' . mt_rand())
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($append)) {
|
|
|
|
$append = sprintf($this->Html->tags['fieldset'], ' style="display:none;"', $append);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setEntity($model . '.', true);
|
2008-12-25 22:13:06 +00:00
|
|
|
$attributes = $this->_parseAttributes($htmlAttributes, null, '');
|
|
|
|
return $this->output(sprintf($this->Html->tags['form'], $attributes)) . $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:
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
function end($options = null) {
|
|
|
|
if (!empty($this->params['models'])) {
|
|
|
|
$models = $this->params['models'][0];
|
|
|
|
}
|
|
|
|
$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;
|
|
|
|
|
|
|
|
if (!$submit) {
|
|
|
|
$submit = __('Submit', true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$out .= $this->submit($submit, $submitOptions);
|
2008-10-07 16:57:22 +00:00
|
|
|
}
|
2008-09-29 01:58:27 +00:00
|
|
|
if (isset($this->params['_Token']) && !empty($this->params['_Token'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out .= $this->secure($this->fields);
|
|
|
|
$this->fields = array();
|
|
|
|
}
|
|
|
|
$this->setEntity(null);
|
|
|
|
$out .= $this->Html->tags['formend'];
|
|
|
|
|
|
|
|
$view =& ClassRegistry::getObject('view');
|
|
|
|
$view->modelScope = false;
|
|
|
|
return $this->output($out);
|
|
|
|
}
|
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
|
|
|
|
* @access public
|
|
|
|
*/
|
2008-10-23 03:18:08 +00:00
|
|
|
function secure($fields = array()) {
|
2008-09-24 23:02:14 +00:00
|
|
|
if (!isset($this->params['_Token']) || empty($this->params['_Token'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$out = '<fieldset style="display:none;">';
|
|
|
|
$locked = array();
|
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
|
|
|
}
|
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
sort($fields, SORT_STRING);
|
|
|
|
ksort($locked, SORT_STRING);
|
|
|
|
$fields += $locked;
|
|
|
|
|
|
|
|
$fields = Security::hash(serialize($fields) . Configure::read('Security.salt'));
|
|
|
|
$locked = str_rot13(serialize(array_keys($locked)));
|
|
|
|
|
|
|
|
$out .= $this->hidden('_Token.fields', array(
|
|
|
|
'value' => urlencode($fields . ':' . $locked),
|
|
|
|
'id' => 'TokenFields' . mt_rand()
|
|
|
|
));
|
|
|
|
return $out .= '</fieldset>';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Determine which fields of a form should be used for hash
|
|
|
|
*
|
2008-09-24 23:02:14 +00:00
|
|
|
* @param mixed $field Reference to field to be secured
|
|
|
|
* @param mixed $value Field value, if value should not be tampered with
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2008-09-24 23:02:14 +00:00
|
|
|
function __secure($field = null, $value = null) {
|
|
|
|
if (!$field) {
|
|
|
|
$view =& ClassRegistry::getObject('view');
|
|
|
|
$field = $view->entity();
|
|
|
|
} elseif (is_string($field)) {
|
|
|
|
$field = Set::filter(explode('.', $field), true);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-21 04:06:37 +00:00
|
|
|
if (!empty($this->params['_Token']['disabledFields'])) {
|
2008-09-24 23:02:14 +00:00
|
|
|
foreach ((array)$this->params['_Token']['disabledFields'] as $disabled) {
|
|
|
|
$disabled = explode('.', $disabled);
|
|
|
|
if (array_values(array_intersect($field, $disabled)) === $disabled) {
|
2008-09-21 04:06:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-11-25 16:32:54 +00:00
|
|
|
$field = join('.', $field);
|
|
|
|
if (!in_array($field, $this->fields)) {
|
|
|
|
if ($value !== null) {
|
|
|
|
return $this->fields[$field] = $value;
|
|
|
|
}
|
|
|
|
$this->fields[] = $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
|
|
|
|
*/
|
|
|
|
function isFieldError($field) {
|
|
|
|
$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.
|
|
|
|
*
|
2009-02-03 18:48:28 +00:00
|
|
|
* Options:
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2009-02-03 18:48:28 +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
|
|
|
|
* string, will be used as the HTML tag to use.
|
|
|
|
* - '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"
|
|
|
|
* @param mixed $text Error message or array of $options
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
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()) {
|
|
|
|
if (is_array($error)) {
|
|
|
|
list(,,$field) = explode('.', $field);
|
|
|
|
if (isset($error[$field])) {
|
|
|
|
$error = $error[$field];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($text) && is_numeric($error) && $error > 0) {
|
|
|
|
$error--;
|
|
|
|
}
|
2009-09-10 13:56:20 +00:00
|
|
|
if (is_array($text)) {
|
2008-06-20 11:57:56 +00:00
|
|
|
$options = array_merge($options, $text);
|
2009-09-10 13:56:20 +00:00
|
|
|
$text = isset($text[$error]) ? $text[$error] : null;
|
|
|
|
unset($options[$error]);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($text != null) {
|
|
|
|
$error = $text;
|
|
|
|
} elseif (is_numeric($error)) {
|
|
|
|
$error = sprintf(__('Error in field %s', true), Inflector::humanize($this->field()));
|
|
|
|
}
|
|
|
|
if ($options['escape']) {
|
|
|
|
$error = h($error);
|
|
|
|
unset($options['escape']);
|
|
|
|
}
|
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
|
|
|
/**
|
|
|
|
* Returns a formatted LABEL element for HTML FORMs.
|
|
|
|
*
|
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
|
|
|
|
*/
|
2009-10-17 04:55:17 +00:00
|
|
|
function label($fieldName = null, $text = null, $options = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (empty($fieldName)) {
|
|
|
|
$view = ClassRegistry::getObject('view');
|
|
|
|
$fieldName = implode('.', $view->entity());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($text === null) {
|
2008-09-18 03:09:19 +00:00
|
|
|
if (strpos($fieldName, '.') !== false) {
|
|
|
|
$text = array_pop(explode('.', $fieldName));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$text = $fieldName;
|
|
|
|
}
|
|
|
|
if (substr($text, -3) == '_id') {
|
|
|
|
$text = substr($text, 0, strlen($text) - 3);
|
|
|
|
}
|
|
|
|
$text = __(Inflector::humanize(Inflector::underscore($text)), true);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
return $this->output(sprintf(
|
|
|
|
$this->Html->tags['label'],
|
|
|
|
$labelFor,
|
2009-10-17 04:55:17 +00:00
|
|
|
$this->_parseAttributes($options), $text
|
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
|
|
|
/**
|
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')
|
|
|
|
* ));
|
|
|
|
* }}}
|
|
|
|
*
|
|
|
|
* @param mixed $fields An array of fields to generate inputs for, or null.
|
|
|
|
* @param array $blacklist a simple array of fields to skip.
|
|
|
|
* @return string Completed form inputs.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function inputs($fields = null, $blacklist = null) {
|
|
|
|
$fieldset = $legend = true;
|
|
|
|
|
|
|
|
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)) {
|
2009-10-20 05:32:24 +00:00
|
|
|
$fields = array_keys($this->fieldset[$this->model()]['fields']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($legend === true) {
|
2009-08-28 02:38:49 +00:00
|
|
|
$actionName = __('New %s', true);
|
2008-12-25 22:13:06 +00:00
|
|
|
$isEdit = (
|
|
|
|
strpos($this->action, 'update') !== false ||
|
|
|
|
strpos($this->action, 'edit') !== false
|
|
|
|
);
|
|
|
|
if ($isEdit) {
|
2009-08-28 02:38:49 +00:00
|
|
|
$actionName = __('Edit %s', true);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$modelName = Inflector::humanize(Inflector::underscore($this->model()));
|
2009-08-28 02:38:49 +00:00
|
|
|
$legend = sprintf($actionName, __($modelName, true));
|
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) {
|
2008-09-24 23:02:14 +00:00
|
|
|
return sprintf(
|
|
|
|
$this->Html->tags['fieldset'],
|
|
|
|
$fieldsetClass,
|
|
|
|
sprintf($this->Html->tags['legend'], $legend) . $out
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif ($fieldset) {
|
|
|
|
return sprintf($this->Html->tags['fieldset'], $fieldsetClass, $out);
|
|
|
|
} 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
|
|
|
*
|
2009-07-24 19:18:37 +00:00
|
|
|
* Options - See each field type method for more information. Any options that are part of
|
2009-04-29 12:40:15 +00:00
|
|
|
* $attributes or $options for the different type methods can be included in $options for 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'`
|
|
|
|
* - `label` - control the label
|
|
|
|
* - `div` - control the wrapping div element
|
|
|
|
* - `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.
|
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.
|
2009-03-12 17:33:47 +00:00
|
|
|
* @return string Completed form widget
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
function input($fieldName, $options = array()) {
|
|
|
|
$this->setEntity($fieldName);
|
2008-12-25 22:13:06 +00:00
|
|
|
|
2009-10-15 04:44:38 +00:00
|
|
|
$options = array_merge(
|
|
|
|
array('before' => null, 'between' => null, 'after' => null),
|
|
|
|
$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();
|
|
|
|
if (!isset($this->fieldset[$modelKey])) {
|
|
|
|
$this->_introspectModel($modelKey);
|
2009-10-20 05:32:24 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-10-31 05:45:17 +00:00
|
|
|
$userType = isset($options['type']) ? true : false;
|
|
|
|
|
|
|
|
if (!$userType) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$options['type'] = 'text';
|
2009-10-20 05:32:24 +00:00
|
|
|
$fieldDef = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($options['options'])) {
|
|
|
|
$options['type'] = 'select';
|
2009-10-31 05:45:17 +00:00
|
|
|
} elseif (in_array($fieldKey, array('psword', 'passwd', 'password'))) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$options['type'] = 'password';
|
2009-10-31 05:45:17 +00:00
|
|
|
} elseif (isset($this->fieldset[$modelKey]['fields'][$fieldKey])) {
|
|
|
|
$fieldDef = $this->fieldset[$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(
|
2008-12-25 22:13:06 +00:00
|
|
|
'string' => 'text', 'datetime' => 'datetime',
|
|
|
|
'boolean' => 'checkbox', 'timestamp' => 'datetime',
|
|
|
|
'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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-21 04:06:37 +00:00
|
|
|
$types = array('text', 'checkbox', 'radio', 'select');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-10-31 05:45:17 +00:00
|
|
|
if (!isset($options['options']) && in_array($options['type'], $types) && !$userType) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$view =& ClassRegistry::getObject('view');
|
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
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
$varOptions = $view->getVar($varName);
|
|
|
|
if (is_array($varOptions)) {
|
|
|
|
if ($options['type'] !== 'radio') {
|
|
|
|
$options['type'] = 'select';
|
|
|
|
}
|
|
|
|
$options['options'] = $varOptions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$autoLength = (!array_key_exists('maxlength', $options) && isset($fieldDef['length']));
|
|
|
|
if ($autoLength && $options['type'] == 'text') {
|
|
|
|
$options['maxlength'] = $fieldDef['length'];
|
|
|
|
}
|
|
|
|
if ($autoLength && $fieldDef['type'] == 'float') {
|
|
|
|
$options['maxlength'] = array_sum(explode(',', $fieldDef['length']))+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$out = '';
|
|
|
|
$div = true;
|
2008-05-30 16:05:53 +00:00
|
|
|
$divOptions = array();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (array_key_exists('div', $options)) {
|
|
|
|
$div = $options['div'];
|
|
|
|
unset($options['div']);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2009-10-20 05:32:24 +00:00
|
|
|
if (
|
2009-10-31 05:45:17 +00:00
|
|
|
isset($this->fieldset[$modelKey]) &&
|
|
|
|
in_array($fieldKey, $this->fieldset[$modelKey]['validates'])
|
2009-10-20 05:32:24 +00:00
|
|
|
) {
|
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'])) {
|
|
|
|
if (is_array($options['options'])) {
|
|
|
|
$radioOptions = $options['options'];
|
|
|
|
} else {
|
|
|
|
$radioOptions = array($options['options']);
|
|
|
|
}
|
|
|
|
unset($options['options']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($label !== false) {
|
2008-06-14 21:48:57 +00:00
|
|
|
$labelAttributes = $this->domId(array(), 'for');
|
2009-10-15 01:17:00 +00:00
|
|
|
if ($options['type'] === 'date' || $options['type'] === 'datetime') {
|
|
|
|
if (isset($options['dateFormat']) && $options['dateFormat'] === 'NONE') {
|
|
|
|
$labelAttributes['for'] .= 'Hour';
|
|
|
|
} else {
|
|
|
|
$labelAttributes['for'] .= 'Month';
|
|
|
|
}
|
|
|
|
} elseif ($options['type'] === 'time') {
|
2008-06-14 21:40:01 +00:00
|
|
|
$labelAttributes['for'] .= 'Hour';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($label)) {
|
|
|
|
$labelText = null;
|
|
|
|
if (isset($label['text'])) {
|
|
|
|
$labelText = $label['text'];
|
|
|
|
unset($label['text']);
|
|
|
|
}
|
|
|
|
$labelAttributes = array_merge($labelAttributes, $label);
|
|
|
|
} else {
|
|
|
|
$labelText = $label;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['id'])) {
|
|
|
|
$labelAttributes = array_merge($labelAttributes, array('for' => $options['id']));
|
|
|
|
}
|
|
|
|
$out = $this->label($fieldName, $labelText, $labelAttributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
$error = null;
|
|
|
|
if (isset($options['error'])) {
|
|
|
|
$error = $options['error'];
|
|
|
|
unset($options['error']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$selected = null;
|
|
|
|
if (array_key_exists('selected', $options)) {
|
|
|
|
$selected = $options['selected'];
|
|
|
|
unset($options['selected']);
|
|
|
|
}
|
|
|
|
if (isset($options['rows']) || isset($options['cols'])) {
|
|
|
|
$options['type'] = 'textarea';
|
|
|
|
}
|
|
|
|
|
|
|
|
$timeFormat = 12;
|
|
|
|
if (isset($options['timeFormat'])) {
|
|
|
|
$timeFormat = $options['timeFormat'];
|
|
|
|
unset($options['timeFormat']);
|
|
|
|
}
|
2008-06-14 21:40:01 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$dateFormat = 'MDY';
|
|
|
|
if (isset($options['dateFormat'])) {
|
|
|
|
$dateFormat = $options['dateFormat'];
|
|
|
|
unset($options['dateFormat']);
|
|
|
|
}
|
2008-06-14 21:40:01 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-10-15 04:44:38 +00:00
|
|
|
$type = $options['type'];
|
|
|
|
$before = $options['before'];
|
2008-05-30 11:40:08 +00:00
|
|
|
$between = $options['between'];
|
2009-10-15 04:44:38 +00:00
|
|
|
$after = $options['after'];
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($options['type'], $options['before'], $options['between'], $options['after']);
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case 'hidden':
|
|
|
|
$out = $this->hidden($fieldName, $options);
|
|
|
|
unset($divOptions);
|
|
|
|
break;
|
|
|
|
case 'checkbox':
|
|
|
|
$out = $before . $this->checkbox($fieldName, $options) . $between . $out;
|
|
|
|
break;
|
|
|
|
case 'radio':
|
|
|
|
$out = $before . $out . $this->radio($fieldName, $radioOptions, $options) . $between;
|
|
|
|
break;
|
|
|
|
case 'text':
|
|
|
|
case 'password':
|
|
|
|
$out = $before . $out . $between . $this->{$type}($fieldName, $options);
|
|
|
|
break;
|
|
|
|
case 'file':
|
|
|
|
$out = $before . $out . $between . $this->file($fieldName, $options);
|
|
|
|
break;
|
|
|
|
case 'select':
|
|
|
|
$options = array_merge(array('options' => array()), $options);
|
|
|
|
$list = $options['options'];
|
|
|
|
unset($options['options']);
|
2008-09-21 04:06:37 +00:00
|
|
|
$out = $before . $out . $between . $this->select(
|
2009-10-17 05:29:16 +00:00
|
|
|
$fieldName, $list, $selected, $options
|
2008-09-21 04:06:37 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
case 'time':
|
2008-09-21 04:06:37 +00:00
|
|
|
$out = $before . $out . $between . $this->dateTime(
|
2009-10-17 05:29:16 +00:00
|
|
|
$fieldName, null, $timeFormat, $selected, $options
|
2008-09-21 04:06:37 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
case 'date':
|
2008-09-21 04:06:37 +00:00
|
|
|
$out = $before . $out . $between . $this->dateTime(
|
2009-10-17 05:29:16 +00:00
|
|
|
$fieldName, $dateFormat, null, $selected, $options
|
2008-09-21 04:06:37 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
case 'datetime':
|
2008-09-21 04:06:37 +00:00
|
|
|
$out = $before . $out . $between . $this->dateTime(
|
2009-10-17 05:29:16 +00:00
|
|
|
$fieldName, $dateFormat, $timeFormat, $selected, $options
|
2008-09-21 04:06:37 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
case 'textarea':
|
|
|
|
default:
|
2008-09-21 04:06:37 +00:00
|
|
|
$out = $before . $out . $between . $this->textarea($fieldName, array_merge(
|
|
|
|
array('cols' => '30', 'rows' => '6'), $options
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type != 'hidden') {
|
|
|
|
$out .= $after;
|
|
|
|
if ($error !== false) {
|
|
|
|
$errMsg = $this->error($fieldName, $error);
|
|
|
|
if ($errMsg) {
|
|
|
|
$out .= $errMsg;
|
|
|
|
$divOptions = $this->addClass($divOptions, 'error');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 16:06:49 +00:00
|
|
|
if (isset($divOptions) && isset($divOptions['tag'])) {
|
2008-06-20 11:57:56 +00:00
|
|
|
$tag = $divOptions['tag'];
|
|
|
|
unset($divOptions['tag']);
|
|
|
|
$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-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates a checkbox input widget.
|
|
|
|
*
|
2009-02-18 01:39:01 +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
|
|
|
|
* a hidden input with a value of ''.
|
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.
|
|
|
|
* @return string An HTML text input element
|
|
|
|
*/
|
|
|
|
function checkbox($fieldName, $options = array()) {
|
2009-10-31 05:04:33 +00:00
|
|
|
$options = $this->_initInputField($fieldName, $options) + array('hiddenField' => true);
|
2008-09-21 04:06:37 +00:00
|
|
|
$value = current($this->value());
|
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;
|
2008-09-21 04:06:37 +00:00
|
|
|
} elseif (!empty($value) && $value === $options['value']) {
|
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
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
return $this->output($output . sprintf(
|
2008-09-21 04:06:37 +00:00
|
|
|
$this->Html->tags['checkbox'],
|
|
|
|
$options['name'],
|
|
|
|
$this->_parseAttributes($options, array('name'), null, ' ')
|
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
|
|
|
/**
|
|
|
|
* Creates a set of radio widgets.
|
|
|
|
*
|
2009-02-03 18:48:28 +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
|
|
|
|
* 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.
|
|
|
|
* @param array $attributes Array of HTML attributes.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (isset($attributes['legend'])) {
|
|
|
|
$legend = $attributes['legend'];
|
|
|
|
unset($attributes['legend']);
|
|
|
|
} elseif (count($options) > 1) {
|
|
|
|
$legend = __(Inflector::humanize($this->field()), true);
|
|
|
|
}
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
$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';
|
|
|
|
}
|
2008-09-21 04:06:37 +00:00
|
|
|
$parsedOptions = $this->_parseAttributes(
|
|
|
|
array_merge($attributes, $optionsHere),
|
|
|
|
array('name', 'type', 'id'), '', ' '
|
|
|
|
);
|
|
|
|
$tagName = Inflector::camelize(
|
2008-12-17 01:49:58 +00:00
|
|
|
$attributes['id'] . '_' . Inflector::underscore($optValue)
|
2008-09-21 04:06:37 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($label) {
|
|
|
|
$optTitle = sprintf($this->Html->tags['label'], $tagName, null, $optTitle);
|
|
|
|
}
|
2008-09-21 04:06:37 +00:00
|
|
|
$out[] = sprintf(
|
|
|
|
$this->Html->tags['radio'], $attributes['name'],
|
|
|
|
$tagName, $parsedOptions, $optTitle
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
$out = $hidden . join($inbetween, $out);
|
|
|
|
|
|
|
|
if ($legend) {
|
2008-09-21 04:06:37 +00:00
|
|
|
$out = sprintf(
|
|
|
|
$this->Html->tags['fieldset'], '',
|
|
|
|
sprintf($this->Html->tags['legend'], $legend) . $out
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $this->output($out);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates a text input widget.
|
|
|
|
*
|
2009-02-18 01:39:01 +00:00
|
|
|
* @param string $fieldName Name of a field, in the form "Modelname.fieldname"
|
2009-10-17 04:55:17 +00:00
|
|
|
* @param array $options Array of HTML attributes.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string An HTML text input element
|
|
|
|
*/
|
|
|
|
function text($fieldName, $options = array()) {
|
2008-09-21 04:06:37 +00:00
|
|
|
$options = $this->_initInputField($fieldName, array_merge(
|
|
|
|
array('type' => 'text'), $options
|
|
|
|
));
|
|
|
|
return $this->output(sprintf(
|
|
|
|
$this->Html->tags['input'],
|
|
|
|
$options['name'],
|
|
|
|
$this->_parseAttributes($options, array('name'), null, ' ')
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates a password input widget.
|
|
|
|
*
|
2009-10-17 04:55:17 +00:00
|
|
|
* @param string $fieldName Name of a field, like in the form "Modelname.fieldname"
|
|
|
|
* @param array $options Array of HTML attributes.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function password($fieldName, $options = array()) {
|
2008-09-21 04:06:37 +00:00
|
|
|
$options = $this->_initInputField($fieldName, $options);
|
|
|
|
return $this->output(sprintf(
|
|
|
|
$this->Html->tags['password'],
|
|
|
|
$options['name'],
|
|
|
|
$this->_parseAttributes($options, array('name'), null, ' ')
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates a textarea widget.
|
|
|
|
*
|
2009-02-18 01:39:01 +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.
|
|
|
|
* @return string An HTML text input element
|
|
|
|
*/
|
|
|
|
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']);
|
|
|
|
}
|
2008-09-21 04:06:37 +00:00
|
|
|
return $this->output(sprintf(
|
|
|
|
$this->Html->tags['textarea'],
|
|
|
|
$options['name'],
|
|
|
|
$this->_parseAttributes($options, array('type', 'name'), null, ' '),
|
|
|
|
$value
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Creates a hidden input field.
|
|
|
|
*
|
2009-02-18 01:39:01 +00:00
|
|
|
* @param string $fieldName Name of a field, in the form"Modelname.fieldname"
|
|
|
|
* @param array $options Array of HTML attributes.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string
|
|
|
|
* @access 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(
|
|
|
|
$options, array('secure' => false)
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
$model = $this->model();
|
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
if ($fieldName !== '_method' && $model !== '_Token' && $secure) {
|
|
|
|
$this->__secure(null, '' . $options['value']);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-21 04:06:37 +00:00
|
|
|
return $this->output(sprintf(
|
|
|
|
$this->Html->tags['hidden'],
|
|
|
|
$options['name'],
|
|
|
|
$this->_parseAttributes($options, array('name', 'class'), '', ' ')
|
|
|
|
));
|
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.
|
|
|
|
* @return string
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function file($fieldName, $options = array()) {
|
2008-09-29 16:23:39 +00:00
|
|
|
$options = array_merge($options, array('secure' => false));
|
2008-09-21 04:06:37 +00:00
|
|
|
$options = $this->_initInputField($fieldName, $options);
|
2008-09-29 16:23:39 +00:00
|
|
|
$view =& ClassRegistry::getObject('view');
|
|
|
|
$field = $view->entity();
|
|
|
|
|
|
|
|
foreach (array('name', 'type', 'tmp_name', 'error', 'size') as $suffix) {
|
|
|
|
$this->__secure(array_merge($field, array($suffix)));
|
|
|
|
}
|
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
$attributes = $this->_parseAttributes($options, array('name'), '', ' ');
|
|
|
|
return $this->output(sprintf($this->Html->tags['file'], $options['name'], $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
|
|
|
/**
|
2009-10-20 03:57:23 +00:00
|
|
|
* Creates a <button> tag.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-10-20 03:57:23 +00:00
|
|
|
* Options:
|
|
|
|
*
|
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
|
|
|
*
|
|
|
|
* @param string $title The button's caption. Not automatically HTML encoded
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
function button($title, $options = array()) {
|
2009-10-20 03:57:23 +00:00
|
|
|
$options += array('type' => 'submit', 'escape' => false);
|
|
|
|
if ($options['escape']) {
|
|
|
|
$title = h($title);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-21 04:06:37 +00:00
|
|
|
return $this->output(sprintf(
|
|
|
|
$this->Html->tags['button'],
|
|
|
|
$options['type'],
|
2009-10-20 03:57:23 +00:00
|
|
|
$this->_parseAttributes($options, array('type'), '', ' '),
|
|
|
|
$title
|
2008-09-21 04:06:37 +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
|
|
|
/**
|
|
|
|
* Creates a submit button 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.
|
2009-07-24 19:18:37 +00:00
|
|
|
* @param array $options
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string A HTML submit button
|
|
|
|
*/
|
|
|
|
function submit($caption = null, $options = array()) {
|
|
|
|
if (!$caption) {
|
|
|
|
$caption = __('Submit', true);
|
|
|
|
}
|
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']);
|
|
|
|
}
|
2009-10-31 20:28:43 +00:00
|
|
|
$options += array('type' => 'submit', 'before' => null, 'after' => null);
|
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
|
|
|
|
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']);
|
2009-10-31 19:50:49 +00:00
|
|
|
$out .= $this->output($before . sprintf(
|
2008-09-21 04:06:37 +00:00
|
|
|
$this->Html->tags['submitimage'],
|
|
|
|
$caption,
|
|
|
|
$this->_parseAttributes($options, null, '', ' ')
|
2009-10-31 19:50:49 +00:00
|
|
|
) . $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);
|
|
|
|
}
|
2009-10-31 19:50:49 +00:00
|
|
|
$out .= $this->output($before . sprintf(
|
2008-09-21 04:06:37 +00:00
|
|
|
$this->Html->tags['submitimage'],
|
|
|
|
$url,
|
|
|
|
$this->_parseAttributes($options, null, '', ' ')
|
2009-10-31 19:50:49 +00:00
|
|
|
) . $after);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$options['value'] = $caption;
|
2009-10-31 19:50:49 +00:00
|
|
|
$out .= $this->output($before . sprintf(
|
2008-09-21 04:06:37 +00:00
|
|
|
$this->Html->tags['submit'],
|
|
|
|
$this->_parseAttributes($options, null, '', ' ')
|
2009-10-31 19:50:49 +00:00
|
|
|
). $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.
|
|
|
|
*
|
2009-02-03 18:48:28 +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
|
2009-02-03 18:48:28 +00:00
|
|
|
* will be added for the parent of each option group.
|
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.
|
2009-03-12 17:33:47 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $fieldName Name attribute of the SELECT
|
2008-09-24 23:02:14 +00:00
|
|
|
* @param array $options Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the
|
2009-02-03 18:48:28 +00:00
|
|
|
* SELECT element
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param mixed $selected The option selected by default. If null, the default value
|
2009-02-03 18:48:28 +00:00
|
|
|
* from POST data will be used when available.
|
|
|
|
* @param array $attributes The HTML attributes of the select element.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Formatted SELECT element
|
|
|
|
*/
|
2009-10-17 05:29:16 +00:00
|
|
|
function select($fieldName, $options = array(), $selected = null, $attributes = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$select = array();
|
|
|
|
$showParents = false;
|
|
|
|
$escapeOptions = true;
|
|
|
|
$style = null;
|
|
|
|
$tag = null;
|
2009-10-17 05:29:16 +00:00
|
|
|
$showEmpty = '';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (isset($attributes['escape'])) {
|
|
|
|
$escapeOptions = $attributes['escape'];
|
|
|
|
unset($attributes['escape']);
|
|
|
|
}
|
2009-10-17 05:29:16 +00:00
|
|
|
if (isset($attributes['empty'])) {
|
|
|
|
$showEmpty = $attributes['empty'];
|
|
|
|
unset($attributes['empty']);
|
|
|
|
}
|
2008-09-21 04:06:37 +00:00
|
|
|
$attributes = $this->_initInputField($fieldName, array_merge(
|
2008-10-03 21:55:41 +00:00
|
|
|
(array)$attributes, array('secure' => false)
|
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 (in_array('showParents', $attributes)) {
|
|
|
|
$showParents = true;
|
|
|
|
unset($attributes['showParents']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($selected)) {
|
|
|
|
$selected = $attributes['value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($attributes) && array_key_exists('multiple', $attributes)) {
|
2008-12-23 08:13:00 +00:00
|
|
|
$style = ($attributes['multiple'] === 'checkbox') ? 'checkbox' : null;
|
|
|
|
$template = ($style) ? 'checkboxmultiplestart' : 'selectmultiplestart';
|
|
|
|
$tag = $this->Html->tags[$template];
|
2008-10-23 03:18:08 +00:00
|
|
|
$select[] = $this->hidden(null, array('value' => '', 'id' => null, 'secure' => false));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
$tag = $this->Html->tags['selectstart'];
|
|
|
|
}
|
|
|
|
|
2008-12-25 22:13:06 +00:00
|
|
|
if (!empty($tag) || isset($template)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->__secure();
|
2008-09-24 23:02:14 +00:00
|
|
|
$select[] = sprintf($tag, $attributes['name'], $this->_parseAttributes(
|
|
|
|
$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
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
$select = array_merge($select, $this->__selectOptions(
|
|
|
|
array_reverse($options, true),
|
|
|
|
$selected,
|
|
|
|
array(),
|
|
|
|
$showParents,
|
|
|
|
array('escape' => $escapeOptions, 'style' => $style)
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-12-23 08:13:00 +00:00
|
|
|
$template = ($style == 'checkbox') ? 'checkboxmultipleend' : 'selectend';
|
|
|
|
$select[] = $this->Html->tags[$template];
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->output(implode("\n", $select));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a SELECT element for days.
|
|
|
|
*
|
2009-10-17 05:29:16 +00:00
|
|
|
* Attributes:
|
|
|
|
*
|
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
|
|
|
* that string is displayed as the empty element.
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $fieldName Prefix name for the SELECT element
|
|
|
|
* @param string $selected Option which is selected.
|
2009-10-17 04:55:17 +00:00
|
|
|
* @param array $attributes HTML attributes for the select element
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2009-10-17 05:29:16 +00:00
|
|
|
function day($fieldName, $selected = null, $attributes = array()) {
|
|
|
|
$attributes += array('empty' => true);
|
2009-10-20 01:58:37 +00:00
|
|
|
$selected = $this->__dateTimeSelected('day', $fieldName, $selected, $attributes);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (strlen($selected) > 2) {
|
|
|
|
$selected = date('d', strtotime($selected));
|
|
|
|
} elseif ($selected === false) {
|
|
|
|
$selected = null;
|
|
|
|
}
|
2009-10-17 05:29:16 +00:00
|
|
|
return $this->select($fieldName . ".day", $this->__generateOptions('day'), $selected, $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
|
|
|
|
*
|
2009-10-17 05:29:16 +00:00
|
|
|
* Attributes:
|
|
|
|
*
|
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
|
|
|
* that string is displayed as the empty element.
|
|
|
|
*
|
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 string $selected Option which is selected.
|
|
|
|
* @param array $attributes Attribute array for the select elements.
|
2009-10-20 03:34:34 +00:00
|
|
|
* @return string Completed year select input
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-10-17 05:29:16 +00:00
|
|
|
function year($fieldName, $minYear = null, $maxYear = null, $selected = null, $attributes = array()) {
|
|
|
|
$attributes += array('empty' => true);
|
2008-05-30 11:40:08 +00:00
|
|
|
if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
extract($value);
|
|
|
|
$selected = $year;
|
|
|
|
} else {
|
|
|
|
if (empty($value)) {
|
2009-10-17 05:29:16 +00:00
|
|
|
if (!$attributes['empty'] && !$maxYear) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$selected = 'now';
|
|
|
|
|
2009-10-17 05:29:16 +00:00
|
|
|
} elseif (!$attributes['empty'] && $maxYear && !$selected) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$selected = $maxYear;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$selected = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen($selected) > 4 || $selected === 'now') {
|
|
|
|
$selected = date('Y', strtotime($selected));
|
|
|
|
} elseif ($selected === false) {
|
|
|
|
$selected = null;
|
|
|
|
}
|
|
|
|
$yearOptions = array('min' => $minYear, 'max' => $maxYear);
|
2008-09-24 23:02:14 +00:00
|
|
|
return $this->select(
|
|
|
|
$fieldName . ".year", $this->__generateOptions('year', $yearOptions),
|
2009-10-17 05:29:16 +00:00
|
|
|
$selected, $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.
|
|
|
|
*
|
2009-02-03 18:48:28 +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.
|
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 string $selected Option which is selected.
|
|
|
|
* @param array $attributes Attributes for the select element
|
|
|
|
* @return string
|
|
|
|
*/
|
2009-10-17 05:29:16 +00:00
|
|
|
function month($fieldName, $selected = null, $attributes = array()) {
|
|
|
|
$attributes += array('empty' => true);
|
2009-10-20 01:58:37 +00:00
|
|
|
$selected = $this->__dateTimeSelected('month', $fieldName, $selected, $attributes);
|
2008-06-14 21:40:01 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (strlen($selected) > 2) {
|
|
|
|
$selected = date('m', strtotime($selected));
|
|
|
|
} elseif ($selected === false) {
|
|
|
|
$selected = null;
|
|
|
|
}
|
|
|
|
$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)),
|
2009-10-17 05:29:16 +00:00
|
|
|
$selected, $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.
|
|
|
|
*
|
2009-10-17 05:29:16 +00:00
|
|
|
* Attributes:
|
|
|
|
*
|
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
|
|
|
* that string is displayed as the empty element.
|
|
|
|
*
|
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 string $selected Option which is selected.
|
|
|
|
* @param array $attributes List of HTML attributes
|
2009-10-20 03:34:34 +00:00
|
|
|
* @return string Completed hour select input
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-10-17 05:29:16 +00:00
|
|
|
function hour($fieldName, $format24Hours = false, $selected = null, $attributes = array()) {
|
|
|
|
$attributes += array('empty' => true);
|
2009-10-20 01:58:37 +00:00
|
|
|
$selected = $this->__dateTimeSelected('hour', $fieldName, $selected, $attributes);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (strlen($selected) > 2) {
|
|
|
|
if ($format24Hours) {
|
2009-10-20 01:58:37 +00:00
|
|
|
$selected = date('H', strtotime($selected));
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2009-10-20 01:58:37 +00:00
|
|
|
$selected = date('g', strtotime($selected));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} elseif ($selected === false) {
|
|
|
|
$selected = null;
|
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
return $this->select(
|
|
|
|
$fieldName . ".hour",
|
|
|
|
$this->__generateOptions($format24Hours ? 'hour24' : 'hour'),
|
2009-10-17 05:29:16 +00:00
|
|
|
$selected, $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.
|
|
|
|
*
|
2009-10-17 05:29:16 +00:00
|
|
|
* Attributes:
|
|
|
|
*
|
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
|
|
|
* that string is displayed as the empty element.
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $fieldName Prefix name for the SELECT element
|
|
|
|
* @param string $selected Option which is selected.
|
|
|
|
* @param string $attributes Array of Attributes
|
2009-10-20 03:34:34 +00:00
|
|
|
* @return string Completed minute select input
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-10-17 05:29:16 +00:00
|
|
|
function minute($fieldName, $selected = null, $attributes = array()) {
|
|
|
|
$attributes += array('empty' => true);
|
2009-10-20 01:58:37 +00:00
|
|
|
$selected = $this->__dateTimeSelected('minute', $fieldName, $selected, $attributes);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (strlen($selected) > 2) {
|
|
|
|
$selected = date('i', strtotime($selected));
|
|
|
|
} elseif ($selected === false) {
|
|
|
|
$selected = null;
|
|
|
|
}
|
|
|
|
$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),
|
2009-10-17 05:29:16 +00:00
|
|
|
$selected, $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 mixed $selected The current selected value.
|
|
|
|
* @param array $attributes Array of attributes, must contain 'empty' key.
|
|
|
|
* @return string Currently selected value.
|
|
|
|
*/
|
|
|
|
function __dateTimeSelected($select, $fieldName, $selected, $attributes) {
|
|
|
|
if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) {
|
|
|
|
if (is_array($value) && isset($value[$select])) {
|
|
|
|
$selected = $value[$select];
|
|
|
|
} else {
|
|
|
|
if (empty($value)) {
|
|
|
|
if (!$attributes['empty']) {
|
|
|
|
$selected = 'now';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$selected = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $selected;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a SELECT element for AM or PM.
|
|
|
|
*
|
2009-10-17 05:29:16 +00:00
|
|
|
* Attributes:
|
|
|
|
*
|
|
|
|
* - `empty` - If true, the empty select option is shown. If a string,
|
|
|
|
* that string is displayed as the empty element.
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $fieldName Prefix name for the SELECT element
|
|
|
|
* @param string $selected Option which is selected.
|
|
|
|
* @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
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-10-17 05:29:16 +00:00
|
|
|
function meridian($fieldName, $selected = null, $attributes = array()) {
|
|
|
|
$attributes += array('empty' => true);
|
2008-05-30 11:40:08 +00:00
|
|
|
if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
extract($value);
|
|
|
|
$selected = $meridian;
|
|
|
|
} else {
|
|
|
|
if (empty($value)) {
|
2009-10-17 05:29:16 +00:00
|
|
|
if (!$attribues['empty']) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$selected = date('a');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$selected = date('a', strtotime($value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($selected === false) {
|
|
|
|
$selected = null;
|
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
return $this->select(
|
|
|
|
$fieldName . ".meridian", $this->__generateOptions('meridian'),
|
2009-10-17 05:29:16 +00:00
|
|
|
$selected, $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.
|
|
|
|
*
|
2009-02-03 18:48:28 +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.
|
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 $selected Option which is selected.
|
|
|
|
* @param string $attributes array of Attributes
|
|
|
|
* @return string The HTML formatted OPTION element
|
|
|
|
*/
|
2009-10-17 05:29:16 +00:00
|
|
|
function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $selected = null, $attributes = array()) {
|
|
|
|
$attributes += array('empty' => true);
|
2008-05-30 11:40:08 +00:00
|
|
|
$year = $month = $day = $hour = $min = $meridian = null;
|
|
|
|
|
|
|
|
if (empty($selected)) {
|
|
|
|
$selected = $this->value($fieldName);
|
|
|
|
}
|
|
|
|
|
2009-10-17 05:29:16 +00:00
|
|
|
if ($selected === null && $attributes['empty'] != true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$selected = time();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($selected)) {
|
|
|
|
if (is_array($selected)) {
|
|
|
|
extract($selected);
|
|
|
|
} else {
|
2008-10-26 14:53:06 +00:00
|
|
|
if (is_numeric($selected)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$selected = strftime('%Y-%m-%d %H:%M:%S', $selected);
|
|
|
|
}
|
|
|
|
$meridian = 'am';
|
|
|
|
$pos = strpos($selected, '-');
|
|
|
|
if ($pos !== false) {
|
|
|
|
$date = explode('-', $selected);
|
|
|
|
$days = explode(' ', $date[2]);
|
|
|
|
$day = $days[0];
|
|
|
|
$month = $date[1];
|
|
|
|
$year = $date[0];
|
|
|
|
} else {
|
|
|
|
$days[1] = $selected;
|
|
|
|
}
|
|
|
|
|
2009-10-15 05:12:02 +00:00
|
|
|
if (!empty($timeFormat)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$time = explode(':', $days[1]);
|
|
|
|
$check = str_replace(':', '', $days[1]);
|
|
|
|
|
|
|
|
if (($check > 115959) && $timeFormat == '12') {
|
|
|
|
$time[0] = $time[0] - 12;
|
|
|
|
$meridian = 'pm';
|
|
|
|
} elseif ($time[0] == '00' && $timeFormat == '12') {
|
|
|
|
$time[0] = 12;
|
|
|
|
} elseif ($time[0] > 12) {
|
|
|
|
$meridian = 'pm';
|
|
|
|
}
|
|
|
|
if ($time[0] == 0 && $timeFormat == '12') {
|
|
|
|
$time[0] = 12;
|
|
|
|
}
|
|
|
|
$hour = $time[0];
|
|
|
|
$min = $time[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-06-14 21:40:01 +00:00
|
|
|
|
2008-05-30 11:40:08 +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
|
|
|
|
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) {
|
|
|
|
case 'Y':
|
|
|
|
$selects[] = $this->year(
|
2009-10-17 05:29:16 +00:00
|
|
|
$fieldName, $minYear, $maxYear, $year, $selectYearAttr
|
2009-10-15 05:12:02 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
$selectMonthAttr['monthNames'] = $monthNames;
|
2009-10-17 05:29:16 +00:00
|
|
|
$selects[] = $this->month($fieldName, $month, $selectMonthAttr);
|
2009-10-15 05:12:02 +00:00
|
|
|
break;
|
|
|
|
case 'D':
|
2009-10-17 05:29:16 +00:00
|
|
|
$selects[] = $this->day($fieldName, $day, $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) {
|
2008-05-30 11:40:08 +00:00
|
|
|
case '24':
|
2009-10-17 05:29:16 +00:00
|
|
|
$opt .= $this->hour($fieldName, true, $hour, $selectHourAttr) . ':' .
|
|
|
|
$this->minute($fieldName, $min, $selectMinuteAttr);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
case '12':
|
2009-10-17 05:29:16 +00:00
|
|
|
$opt .= $this->hour($fieldName, false, $hour, $selectHourAttr) . ':' .
|
|
|
|
$this->minute($fieldName, $min, $selectMinuteAttr) . ' ' .
|
|
|
|
$this->meridian($fieldName, $meridian, $selectMeridianAttr);
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$opt .= '';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $opt;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function __name($options = array(), $field = null, $key = 'name') {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
$name = $this->field();
|
|
|
|
|
|
|
|
if (is_array($options)) {
|
|
|
|
$options[$key] = $name;
|
|
|
|
return $options;
|
|
|
|
} else {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parent::__name($options, $field, $key);
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
function __selectOptions($elements = array(), $selected = null, $parents = array(), $showParents = null, $attributes = array()) {
|
|
|
|
$select = array();
|
|
|
|
$attributes = array_merge(array('escape' => true, 'style' => null), $attributes);
|
|
|
|
$selectedIsEmpty = ($selected === '' || $selected === null);
|
|
|
|
$selectedIsArray = is_array($selected);
|
|
|
|
|
|
|
|
foreach ($elements as $name => $title) {
|
|
|
|
$htmlOptions = array();
|
|
|
|
if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
|
|
|
|
if (!empty($name)) {
|
|
|
|
if ($attributes['style'] === 'checkbox') {
|
|
|
|
$select[] = $this->Html->tags['fieldsetend'];
|
2008-06-22 19:16:34 +00:00
|
|
|
} else {
|
2008-05-30 11:40:08 +00:00
|
|
|
$select[] = $this->Html->tags['optiongroupend'];
|
|
|
|
}
|
|
|
|
$parents[] = $name;
|
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
$select = array_merge($select, $this->__selectOptions(
|
|
|
|
$title, $selected, $parents, $showParents, $attributes
|
|
|
|
));
|
2008-07-30 13:40:59 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($name)) {
|
|
|
|
if ($attributes['style'] === 'checkbox') {
|
|
|
|
$select[] = sprintf($this->Html->tags['fieldsetstart'], $name);
|
2008-06-22 19:16:34 +00:00
|
|
|
} else {
|
2008-05-30 11:40:08 +00:00
|
|
|
$select[] = sprintf($this->Html->tags['optiongroup'], $name, '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$name = null;
|
|
|
|
} elseif (is_array($title)) {
|
|
|
|
$htmlOptions = $title;
|
|
|
|
$name = $title['value'];
|
|
|
|
$title = $title['name'];
|
|
|
|
unset($htmlOptions['name'], $htmlOptions['value']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($name !== null) {
|
2009-10-19 02:05:17 +00:00
|
|
|
if ((!$selectedIsEmpty && (string)$selected == (string)$name) || ($selectedIsArray && in_array($name, $selected))) {
|
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;
|
|
|
|
|
2008-09-24 23:02:14 +00:00
|
|
|
$tagName = Inflector::camelize(
|
|
|
|
$this->model() . '_' . $this->field().'_'.Inflector::underscore($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';
|
|
|
|
}
|
|
|
|
|
|
|
|
list($name) = array_values($this->__name());
|
|
|
|
|
|
|
|
if (empty($attributes['class'])) {
|
|
|
|
$attributes['class'] = 'checkbox';
|
|
|
|
}
|
2008-09-24 23:02:14 +00:00
|
|
|
$label = $this->label(null, $title, $label);
|
|
|
|
$item = sprintf(
|
|
|
|
$this->Html->tags['checkboxmultiple'], $name,
|
2008-12-01 19:39:40 +00:00
|
|
|
$this->_parseAttributes($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 {
|
2008-09-24 23:02:14 +00:00
|
|
|
$select[] = sprintf(
|
|
|
|
$this->Html->tags['selectoption'],
|
2008-12-01 19:39:40 +00:00
|
|
|
$name, $this->_parseAttributes($htmlOptions), $title
|
2008-09-24 23:02:14 +00:00
|
|
|
);
|
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) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$data['01'] = __('January', true);
|
|
|
|
$data['02'] = __('February', true);
|
|
|
|
$data['03'] = __('March', true);
|
|
|
|
$data['04'] = __('April', true);
|
|
|
|
$data['05'] = __('May', true);
|
|
|
|
$data['06'] = __('June', true);
|
|
|
|
$data['07'] = __('July', true);
|
|
|
|
$data['08'] = __('August', true);
|
|
|
|
$data['09'] = __('September', true);
|
|
|
|
$data['10'] = __('October', true);
|
|
|
|
$data['11'] = __('November', true);
|
|
|
|
$data['12'] = __('December', true);
|
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;
|
|
|
|
}
|
|
|
|
$data = array_reverse($data, true);
|
|
|
|
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
|
|
|
*
|
2009-02-18 01:39:01 +00:00
|
|
|
* Options
|
2009-10-17 04:55:17 +00:00
|
|
|
*
|
|
|
|
* - `secure` - boolean whether or not the the field should be added to the security fields.
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2008-09-21 04:06:37 +00:00
|
|
|
* @param string $field
|
|
|
|
* @param array $options
|
|
|
|
* @return array
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
function _initInputField($field, $options = array()) {
|
|
|
|
if (isset($options['secure'])) {
|
|
|
|
$secure = $options['secure'];
|
|
|
|
unset($options['secure']);
|
2008-09-24 23:02:14 +00:00
|
|
|
} else {
|
|
|
|
$secure = (isset($this->params['_Token']) && !empty($this->params['_Token']));
|
2008-09-21 04:06:37 +00:00
|
|
|
}
|
|
|
|
$result = parent::_initInputField($field, $options);
|
|
|
|
|
|
|
|
if ($secure) {
|
|
|
|
$this->__secure();
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-21 04:06:37 +00:00
|
|
|
|
2008-06-20 11:57:56 +00:00
|
|
|
?>
|