diff --git a/cake/libs/view/helper.php b/cake/libs/view/helper.php index 251f0bc70..7181f003d 100644 --- a/cake/libs/view/helper.php +++ b/cake/libs/view/helper.php @@ -60,6 +60,285 @@ class Helper extends Object { } return $cakeConfig; } +/** + * Finds URL for specified action. + * + * Returns an URL pointing to a combination of controller and action. Param + * $url can be: + * + Empty - the method will find adress to actuall controller/action. + * + '/' - the method will find base URL of application. + * + A combination of controller/action - the method will find url for it. + * + * @param mixed $url Cake-relative URL, like "/products/edit/92" or "/presidents/elect/4" + * or an array specifying any of the following: 'controller', 'action', + * and/or 'plugin', in addition to named arguments (keyed array elements), + * and standard URL arguments (indexed array elements) + * @param boolean $full If true, the full base URL will be prepended to the result + * @return string Full translated URL with base path. + */ + function url($url = null, $full = false) { + $base = strip_plugin($this->base, $this->plugin); + $extension = null; + + if (is_array($url) && !empty($url)) { + if (!isset($url['action'])) { + $url['action'] = $this->params['action']; + } + if (!isset($url['controller'])) { + $url['controller'] = $this->params['controller']; + } + if (!isset($url['plugin'])) { + $url['plugin'] = $this->plugin; + } + if (isset($url['ext'])) { + $extension = '.' . $url['ext']; + } + if (defined('CAKE_ADMIN') && !isset($url[CAKE_ADMIN]) && isset($this->params['admin'])) { + $url[CAKE_ADMIN] = $this->params['admin']; + } + + $named = $args = array(); + $keys = array_keys($url); + $count = count($keys); + for ($i = 0; $i < $count; $i++) { + if (is_numeric($keys[$i])) { + $args[] = $url[$keys[$i]]; + } else { + if (!in_array($keys[$i], array('action', 'controller', 'plugin', 'ext'))) { + $named[] = array($keys[$i], $url[$keys[$i]]); + } + } + } + + $combined = ''; + if ($this->namedArgs) { + if ($this->namedArgs === true) { + $sep = $this->argSeparator; + } elseif (is_array($this->namedArgs)) { + $sep = '/'; + } + + $count = count($named); + for ($i = 0; $i < $count; $i++) { + $named[$i] = join($this->argSeparator, $named[$i]); + } + if (defined('CAKE_ADMIN') && isset($named[CAKE_ADMIN])) { + unset($named[CAKE_ADMIN]); + } + $combined = join('/', $named); + } + + $urlOut = array_filter(array($url['plugin'], $url['controller'], $url['action'], join('/', array_filter($args)), $combined)); + if (defined('CAKE_ADMIN') && isset($url[CAKE_ADMIN]) && $url[CAKE_ADMIN]) { + array_unshift($urlOut, CAKE_ADMIN); + } + $output = $base . '/' . join('/', $urlOut); + } else { + if (((strpos($url, '://')) || (strpos($url, 'javascript:') === 0) || (strpos($url, 'mailto:') === 0)) || $url == '#') { + return $this->output($url); + } + + if (empty($url)) { + return $this->here; + } elseif($url{0} == '/') { + $output = $base . $url; + } else { + $output = $base . '/' . strtolower($this->params['controller']) . '/' . $url; + } + } + if ($full) { + $output = FULL_BASE_URL . $output; + } + return $this->output($output . $extension); + } +/** + * Returns a space-delimited string with items of the $options array. If a + * key of $options array happens to be one of: + * + 'compact' + * + 'checked' + * + 'declare' + * + 'readonly' + * + 'disabled' + * + 'selected' + * + 'defer' + * + 'ismap' + * + 'nohref' + * + 'noshade' + * + 'nowrap' + * + 'multiple' + * + 'noresize' + * + * And its value is one of: + * + 1 + * + true + * + 'true' + * + * Then the value will be reset to be identical with key's name. + * If the value is not one of these 3, the parameter is not output. + * + * @param array $options Array of options. + * @param array $exclude Array of options to be excluded. + * @param string $insertBefore String to be inserted before options. + * @param string $insertAfter String to be inserted ater options. + * @return string + */ + function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) { + $minimizedAttributes = array('compact', 'checked', 'declare', 'readonly', 'disabled', 'selected', 'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize'); + if (!is_array($exclude)) { + $exclude = array(); + } + + if (is_array($options)) { + $out = array(); + + foreach($options as $key => $value) { + if (!in_array($key, $exclude)) { + if (in_array($key, $minimizedAttributes) && ($value === 1 || $value === true || $value === 'true' || in_array($value, $minimizedAttributes))) { + $value = $key; + } elseif(in_array($key, $minimizedAttributes)) { + continue; + } + $out[] = "{$key}=\"{$value}\""; + } + } + $out = join(' ', $out); + return $out ? $insertBefore . $out . $insertAfter : null; + } else { + return $options ? $insertBefore . $options . $insertAfter : null; + } + } +/** + * @deprecated Name changed to '_parseAttributes'. Version 0.9.2. + * @see HtmlHelper::_parseAttributes() + * @param array $options Array of options. + * @param array $exclude Array of options to be excluded. + * @param string $insertBefore String to be inserted before options. + * @param string $insertAfter String to be inserted ater options. + * @return string + */ + function parseHtmlOptions($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) { + if (!is_array($exclude)) { + $exclude = array(); + } + + if (is_array($options)) { + $out = array(); + + foreach($options as $k => $v) { + if (!in_array($k, $exclude)) { + $out[] = "{$k}=\"{$v}\""; + } + } + $out = join(' ', $out); + return $out ? $insertBefore . $out . $insertAfter : null; + } else { + return $options ? $insertBefore . $options . $insertAfter : null; + } + } +/** + * Sets this helper's model and field properties to the slash-separated value-pair in $tagValue. + * + * @param string $tagValue A field name, like "Modelname/fieldname" + */ + function setFormTag($tagValue) { + return list($this->view->model, $this->view->field) = explode("/", $tagValue); + } +/** + * Enter description here... + * + * @return string + */ + function model() { + return $this->view->model; + } +/** + * Enter description here... + * + * @return string + */ + function field() { + return $this->view->field; + } +/** + * Returns false if given FORM field has no errors. Otherwise it returns the constant set in the array Model->validationErrors. + * + * @param string $model Model name as string + * @param string $field Fieldname as string + * @return boolean True on errors. + */ + function tagIsInvalid($model = null, $field = null) { + if ($model == null) { + $model = $this->model(); + } + if ($field == null) { + $field = $this->field(); + } + return empty($this->validationErrors[$model][$field]) ? 0 : $this->validationErrors[$model][$field]; + } +/** + * Generates a DOM ID for the selected element, if one is not set. + * + * @param array $options + * @param string $id + * @return array + */ + function domId($options = array(), $id = 'id') { + if (!isset($options[$id])) { + $options[$id] = $this->model() . Inflector::camelize($this->field()); + } + return $options; + } +/** + * Gets the data for the current tag + * + * @param array $options + * @param string $key + * @return array + */ + function __value($options = array(), $field = null, $key = 'value') { + if (is_string($options)) { + $field = $options; + $options = 0; + } + + if ($field != null) { + $this->setFormTag($field); + } + + if (is_array($options) && isset($options[$key])) { + return $options; + } + + $result = null; + if (isset($this->params['data'][$this->model()][$this->field()])) { + $result = h($this->params['data'][$this->model()][$this->field()]); + } elseif(isset($this->data[$this->model()][$this->field()])) { + $result = h($this->data[$this->model()][$this->field()]); + } + + if ($options !== 0) { + $options[$key] = $result; + return $options; + } else { + return $result; + } + } +/** + * Adds the given class to the element options + * + * @param array $options + * @param string $class + * @param string $key + * @return array + */ + function addClass($options = array(), $class = null, $key = 'class') { + if (isset($options[$key]) && trim($options[$key]) != '') { + $options[$key] .= ' ' . $class; + } else { + $options[$key] = $class; + } + return $options; + } /** * Returns a string generated by a helper method * diff --git a/cake/libs/view/helpers/ajax.php b/cake/libs/view/helpers/ajax.php index b2b7f0c9d..bb767b94a 100644 --- a/cake/libs/view/helpers/ajax.php +++ b/cake/libs/view/helpers/ajax.php @@ -249,7 +249,7 @@ class AjaxHelper extends Helper { $func = "new Ajax.Request("; } - $func .= "'" . $this->Html->url(isset($options['url']) ? $options['url'] : "") . "'"; + $func .= "'" . $this->url(isset($options['url']) ? $options['url'] : "") . "'"; $func .= ", " . $this->__optionsForAjax($options) . ")"; if (isset($options['before'])) { @@ -495,7 +495,7 @@ class AjaxHelper extends Helper { return ''; } } - $attr = $this->Html->_parseAttributes(am($options, array('id' => $id))); + $attr = $this->_parseAttributes(am($options, array('id' => $id))); return $this->output(sprintf($this->tags['blockstart'], $attr)); } /** @@ -611,7 +611,7 @@ class AjaxHelper extends Helper { * @link http://wiki.script.aculo.us/scriptaculous/show/Ajax.InPlaceEditor */ function editor($id, $url, $options = array()) { - $url = $this->Html->url($url); + $url = $this->url($url); $options['ajaxOptions'] = $this->__optionsForAjax($options); foreach($this->ajaxOptions as $opt) { diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index c906d6b67..8a549a8d6 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -66,7 +66,7 @@ class FormHelper extends Helper { * @return string An formatted opening FORM tag. */ function create($target = null, $type = 'post', $htmlAttributes = null) { - $htmlAttributes['action'] = $this->Html->url($target); + $htmlAttributes['action'] = $this->url($target); $htmlAttributes['method'] = low($type) == 'get' ? 'get' : 'post'; $type == 'file' ? $htmlAttributes['enctype'] = 'multipart/form-data' : null; $token = ''; @@ -85,9 +85,9 @@ class FormHelper extends Helper { */ function isFieldError($field) { $error = 1; - $this->Html->setFormTag($field); + $this->setFormTag($field); - if ($error == $this->Html->tagIsInvalid($this->Html->model, $this->Html->field)) { + if ($error == $this->Html->tagIsInvalid()) { return true; } else { return false; @@ -106,11 +106,85 @@ class FormHelper extends Helper { * @param string $text Text that will appear in the label field. * @return string The formatted LABEL element */ - function label($tagName, $text, $attributes = array()) { + function label($tagName, $text = null, $attributes = array()) { + if ($text == null) { + if (strpos($tagName, '/') !== false) { + list( , $text) = explode('/', $tagName); + } else { + $text = $tagName; + } + $text = Inflector::humanize($text); + } if (strpos($tagName, '/') !== false) { $tagName = Inflector::camelize(r('/', '_', $tagName)); } - return $this->output(sprintf($this->tags['label'], $tagName, $this->Html->_parseAttributes($attributes), $text)); + return $this->output(sprintf($this->tags['label'], $tagName, $this->_parseAttributes($attributes), $text)); + } +/** + * Generates a form input element complete with label and wrapper div + * + * @param string $tagName This should be "Modelname/fieldname" + * @param array $options + * @return string + */ + function input($tagName, $options = array()) { + if (!isset($options['type'])) { + if (isset($options['options'])) { + $options['type'] = 'select'; + } else { + $options['type'] = 'text'; + } + } + + $wrap = true; + if (isset($options['wrap'])) { + $wrap = $options['wrap']; + unset($options['wrap']); + } + + $divOptions = array(); + if (!isset($options['class']) || empty($options['class'])) { + $divOptions['class'] = 'input'; + } else { + $divOptions['class'] = $options['class']; + } + + $label = null; + if (isset($options['label'])) { + $label = $options['label']; + unset($options['label']); + } + $out = $this->label($tagName, $label); + + $error = null; + if (isset($options['error'])) { + $error = $options['error']; + unset($options['error']); + } + + switch ($options['type']) { + case 'text': + $out .= $this->text($tagName); + break; + case 'file': + $out .= $this->Html->file($tagName); + break; + case 'select': + $list = $options['options']; + $empty = (isset($options['empty']) ? $options['empty'] : ''); + unset($options['options'], $options['empty']); + $out .= $this->select($tagName, $list, null, $options, $empty); + break; + } + + if ($error != null) { + $out .= $this->Html->tagErrorMsg($tagName, $error); + } + + if ($wrap) { + $out = $this->Html->div($divOptions['class'], $out); + } + return $this->output($out); } /** * @deprecated @@ -119,11 +193,7 @@ class FormHelper extends Helper { return sprintf(TAG_DIV, $class, $text); } /** - * Returns a formatted P tag with class for HTML FORMs. - * - * @param string $class CSS class name of the p element. - * @param string $text Text that will appear inside the p element. - * @return string The formatted P element + * @deprecated */ function pTag($class, $text) { return sprintf(TAG_P_CLASS, $class, $text); @@ -136,29 +206,17 @@ class FormHelper extends Helper { * @return string An HTML text input element */ function text($fieldName, $htmlAttributes = null) { - $this->Html->setFormTag($fieldName); - - if (!isset($htmlAttributes['value'])) { - $htmlAttributes['value'] = $this->Html->tagValue($fieldName); - } + $htmlAttributes = $this->__value($htmlAttributes, $fieldName); + $htmlAttributes = $this->domId($htmlAttributes); if (!isset($htmlAttributes['type'])) { $htmlAttributes['type'] = 'text'; } - if (!isset($htmlAttributes['id'])) { - $htmlAttributes['id'] = $this->Html->model . Inflector::camelize($this->Html->field); + if ($this->tagIsInvalid()) { + $htmlAttributes = $this->Html->addClass($htmlAttributes, 'form_error'); } - - if ($this->Html->tagIsInvalid($this->Html->model, $this->Html->field)) { - if (isset($htmlAttributes['class']) && trim($htmlAttributes['class']) != "") { - $htmlAttributes['class'] .= ' form_error'; - } else { - $htmlAttributes['class'] = 'form_error'; - } - } - - return sprintf($this->tags['input'], $this->Html->model, $this->Html->field, $this->Html->_parseAttributes($htmlAttributes, null, ' ', ' ')); + return $this->output(sprintf($this->tags['input'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, ' ', ' '))); } /** * Creates a textarea widget. @@ -173,16 +231,10 @@ class FormHelper extends Helper { $value = $htmlAttributes['value']; unset($htmlAttributes['value']); } - if (!isset($htmlAttributes['id'])) { - $htmlAttributes['id'] = $this->Html->model . Inflector::camelize($this->Html->field); - } + $htmlAttributes = $this->domId($htmlAttributes); - if ($this->Html->tagIsInvalid($this->Html->model, $this->Html->field)) { - if (isset($htmlAttributes['class']) && trim($htmlAttributes['class']) != "") { - $htmlAttributes['class'] .= ' form_error'; - } else { - $htmlAttributes['class'] = 'form_error'; - } + if ($this->tagIsInvalid()) { + $htmlAttributes = $this->Html->addClass($htmlAttributes, 'form_error'); } return $this->output(sprintf($this->tags['textarea'], $this->Html->model, $this->Html->field, $this->Html->_parseAttributes($htmlAttributes, null, ' '), $value)); } @@ -229,7 +281,7 @@ class FormHelper extends Helper { } else { $url = $this->webroot . $this->themeWeb . IMAGES_URL . $path; } - return sprintf($this->tags['submitimage'], $url, $this->_parseAttributes($htmlAttributes, null, '', ' ')); + return $this->output(sprintf($this->tags['submitimage'], $url, $this->_parseAttributes($htmlAttributes, null, '', ' '))); } /** * Returns a formatted SELECT element. @@ -244,29 +296,23 @@ class FormHelper extends Helper { * @return string Formatted SELECT element */ function select($fieldName, $options = array(), $selected = null, $attributes = array(), $showEmpty = '') { - $this->Html->setFormTag($fieldName); + $this->setFormTag($fieldName); + $attributes = $this->domId($attributes); - if ($this->Html->tagIsInvalid($this->Html->model, $this->Html->field)) { - if (isset($attributes['class']) && trim($attributes['class']) != '') { - $attributes['class'] .= ' form_error'; - } else { - $attributes['class'] = 'form_error'; - } - } - - if (!isset($attributes['id'])) { - $attributes['id'] = $this->Html->model . Inflector::camelize($this->Html->field); + if ($this->tagIsInvalid()) { + $htmlAttributes = $this->Html->addClass($htmlAttributes, 'form_error'); } if (!isset($selected)) { - $selected = $this->Html->tagValue($fieldName); + $selected = $this->__value($fieldName); } if (isset($attributes) && array_key_exists("multiple", $attributes)) { - $select[] = sprintf($this->tags['selectmultiplestart'], $this->Html->model, $this->Html->field, $this->Html->parseHtmlOptions($attributes)); + $tag = $this->tags['selectmultiplestart']; } else { - $select[] = sprintf($this->tags['selectstart'], $this->Html->model, $this->Html->field, $this->Html->parseHtmlOptions($attributes)); + $tag = $this->tags['selectstart']; } + $select[] = sprintf($tag, $this->model(), $this->field(), $this->Html->parseHtmlOptions($attributes)); if ($showEmpty !== null && $showEmpty !== false) { $keys = array_keys($options); @@ -342,7 +388,7 @@ class FormHelper extends Helper { $strError = ""; if ($this->isFieldError($tagName)) { - $strError = $this->pTag('error', $errorMsg); + $strError = $this->Html->para('error', $errorMsg); $divClass = sprintf("%s error", $divClass); } $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); @@ -370,7 +416,7 @@ class FormHelper extends Helper { $strError = ""; if ($this->isFieldError($tagName)) { - $strError = $this->pTag('error', $errorMsg); + $strError = $this->Html->para('error', $errorMsg); $divClass = sprintf("%s error", $divClass); } $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); @@ -399,7 +445,7 @@ class FormHelper extends Helper { $strError = ""; if ($this->isFieldError($tagName)) { - $strError = $this->pTag('error', $errorMsg); + $strError = $this->Html->para('error', $errorMsg); $divClass = sprintf("%s error", $divClass); } $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); @@ -428,7 +474,7 @@ class FormHelper extends Helper { $strError = ""; if ($this->isFieldError($tagName)) { - $strError = $this->pTag('error', $errorMsg); + $strError = $this->Html->para('error', $errorMsg); $divClass = sprintf("%s error", $divClass); } $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); @@ -464,7 +510,7 @@ class FormHelper extends Helper { $strError = ""; if ($this->isFieldError($tagName)) { - $strError = $this->pTag('error', $errorMsg); + $strError = $this->Html->para('error', $errorMsg); $divClass = sprintf("%s error", $divClass); } $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); @@ -498,7 +544,7 @@ class FormHelper extends Helper { $strError = ""; if ($this->isFieldError($tagName)) { - $strError = $this->pTag('error', $errorMsg); + $strError = $this->Html->para('error', $errorMsg); $divClass = sprintf("%s error", $divClass); } $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); @@ -529,8 +575,8 @@ class FormHelper extends Helper { $strError = ""; if ($this->isFieldError($tagName)) { - $strError=$this->pTag('error', $errorMsg); - $divClass=sprintf("%s error", $divClass); + $strError = $this->Html->para('error', $errorMsg); + $divClass = sprintf("%s error", $divClass); } $divTagInside = sprintf("%s %s %s", $strError, $strLabel, $str); return $this->divTag($divClass, $divTagInside); diff --git a/cake/libs/view/helpers/html.php b/cake/libs/view/helpers/html.php index 0e87e37fe..6385ae592 100644 --- a/cake/libs/view/helpers/html.php +++ b/cake/libs/view/helpers/html.php @@ -70,18 +70,6 @@ class HtmlHelper extends Helper { * @var array */ var $data = null; -/** - * Name of model this helper is attached to. - * - * @var string - */ - var $model = null; -/** - * Enter description here... - * - * @var string - */ - var $field = null; /**#@-*/ /************************************************************************* * Private variables @@ -185,97 +173,6 @@ class HtmlHelper extends Helper { function charset($charset = 'UTF-8') { return $this->output(sprintf($this->tags['charset'], $charset)); } -/** - * Finds URL for specified action. - * - * Returns an URL pointing to a combination of controller and action. Param - * $url can be: - * + Empty - the method will find adress to actuall controller/action. - * + '/' - the method will find base URL of application. - * + A combination of controller/action - the method will find url for it. - * - * @param mixed $url Cake-relative URL, like "/products/edit/92" or "/presidents/elect/4" - * or an array specifying any of the following: 'controller', 'action', - * and/or 'plugin', in addition to named arguments (keyed array elements), - * and standard URL arguments (indexed array elements) - * @param boolean $full If true, the full base URL will be prepended to the result - * @return string Full translated URL with base path. - */ - function url($url = null, $full = false) { - $base = strip_plugin($this->base, $this->plugin); - $extension = null; - - if (is_array($url) && !empty($url)) { - if (!isset($url['action'])) { - $url['action'] = $this->params['action']; - } - if (!isset($url['controller'])) { - $url['controller'] = $this->params['controller']; - } - if (!isset($url['plugin'])) { - $url['plugin'] = $this->plugin; - } - if (isset($url['ext'])) { - $extension = '.' . $url['ext']; - } - if (defined('CAKE_ADMIN') && !isset($url[CAKE_ADMIN]) && isset($this->params['admin'])) { - $url[CAKE_ADMIN] = $this->params['admin']; - } - - $named = $args = array(); - $keys = array_keys($url); - $count = count($keys); - for ($i = 0; $i < $count; $i++) { - if (is_numeric($keys[$i])) { - $args[] = $url[$keys[$i]]; - } else { - if (!in_array($keys[$i], array('action', 'controller', 'plugin', 'ext'))) { - $named[] = array($keys[$i], $url[$keys[$i]]); - } - } - } - - $combined = ''; - if ($this->namedArgs) { - if ($this->namedArgs === true) { - $sep = $this->argSeparator; - } elseif (is_array($this->namedArgs)) { - $sep = '/'; - } - - $count = count($named); - for ($i = 0; $i < $count; $i++) { - $named[$i] = join($this->argSeparator, $named[$i]); - } - if (defined('CAKE_ADMIN') && isset($named[CAKE_ADMIN])) { - unset($named[CAKE_ADMIN]); - } - $combined = join('/', $named); - } - - $urlOut = array_filter(array($url['plugin'], $url['controller'], $url['action'], join('/', array_filter($args)), $combined)); - if (defined('CAKE_ADMIN') && isset($url[CAKE_ADMIN]) && $url[CAKE_ADMIN]) { - array_unshift($urlOut, CAKE_ADMIN); - } - $output = $base . '/' . join('/', $urlOut); - } else { - if (((strpos($url, '://')) || (strpos($url, 'javascript:') === 0) || (strpos($url, 'mailto:') === 0)) || $url == '#') { - return $this->output($url); - } - - if (empty($url)) { - return $this->here; - } elseif($url{0} == '/') { - $output = $base . $url; - } else { - $output = $base . '/' . strtolower($this->params['controller']) . '/' . $url; - } - } - if ($full) { - $output = FULL_BASE_URL . $output; - } - return $this->output($output . $extension); - } /** * Creates an HTML link. * @@ -351,22 +248,12 @@ class HtmlHelper extends Helper { * @return string */ function password($fieldName, $htmlAttributes = null) { - $this->setFormTag($fieldName); - if (!isset($htmlAttributes['value'])) { - $htmlAttributes['value'] = $this->tagValue($fieldName); + $htmlAttributes = $this->__value($htmlAttributes, $fieldName); + $htmlAttributes = $this->domId($htmlAttributes); + if ($this->tagIsInvalid()) { + $htmlAttributes = $this->addClass($htmlAttributes, 'form_error'); } - if (!isset($htmlAttributes['id'])) { - $htmlAttributes['id'] = $this->model . Inflector::camelize($this->field); - } - - if ($this->tagIsInvalid($this->model, $this->field)) { - if (isset($htmlAttributes['class']) && trim($htmlAttributes['class']) != "") { - $htmlAttributes['class'] .= ' form_error'; - } else { - $htmlAttributes['class'] = 'form_error'; - } - } - return $this->output(sprintf($this->tags['password'], $this->model, $this->field, $this->_parseAttributes($htmlAttributes, null, ' ', ' '))); + return $this->output(sprintf($this->tags['password'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, ' ', ' '))); } /** * Creates a textarea widget. @@ -375,25 +262,20 @@ class HtmlHelper extends Helper { * @param array $htmlAttributes Array of HTML attributes. * @return string */ - function textarea($fieldName, $htmlAttributes = null) { - $this->setFormTag($fieldName); - $value = $this->tagValue($fieldName); - if (!empty($htmlAttributes['value'])) { + function textarea($fieldName, $htmlAttributes = array()) { + $htmlAttributes = $this->__value($htmlAttributes, $fieldName); + + $value = null; + if (isset($htmlAttributes['value']) && !empty($htmlAttributes['value'])) { $value = $htmlAttributes['value']; unset($htmlAttributes['value']); } - if (!isset($htmlAttributes['id'])) { - $htmlAttributes['id'] = $this->model . Inflector::camelize($this->field); - } + $htmlAttributes = $this->domId($htmlAttributes); - if ($this->tagIsInvalid($this->model, $this->field)) { - if (isset($htmlAttributes['class']) && trim($htmlAttributes['class']) != "") { - $htmlAttributes['class'] .= ' form_error'; - } else { - $htmlAttributes['class'] = 'form_error'; - } + if ($this->tagIsInvalid()) { + $htmlAttributes = $this->addClass($htmlAttributes, 'form_error'); } - return $this->output(sprintf($this->tags['textarea'], $this->model, $this->field, $this->_parseAttributes($htmlAttributes, null, ' '), $value)); + return $this->output(sprintf($this->tags['textarea'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, ' '), $value)); } /** * Creates a checkbox widget. @@ -422,18 +304,17 @@ class HtmlHelper extends Helper { $notCheckedValue = -1; } } else { - $model = new $this->model; + $model = $this->model(); + $model = new $model; $db =& ConnectionManager::getDataSource($model->useDbConfig); $value = $db->boolean($value); $htmlAttributes['checked'] = $value ? 'checked' : null; $htmlAttributes['value'] = 1; } } - if (!isset($htmlAttributes['id'])) { - $htmlAttributes['id'] = $this->model . Inflector::camelize($this->field); - } + $htmlAttributes = $this->domId($htmlAttributes); $output = $this->hidden($fieldName, array('value' => $notCheckedValue, 'id' => $htmlAttributes['id'] . '_'), true); - $output .= sprintf($this->tags['checkbox'], $this->model, $this->field, $this->_parseAttributes($htmlAttributes, null, '', ' ')); + $output .= sprintf($this->tags['checkbox'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, '', ' ')); return $this->output($output); } /** @@ -446,10 +327,8 @@ class HtmlHelper extends Helper { function file($fieldName, $htmlAttributes = null) { if (strpos($fieldName, '/')) { $this->setFormTag($fieldName); - if (!isset($htmlAttributes['id'])) { - $htmlAttributes['id'] = $this->model . Inflector::camelize($this->field); - } - return $this->output(sprintf($this->tags['file'], $this->model, $this->field, $this->_parseAttributes($htmlAttributes, null, '', ' '))); + $htmlAttributes = $this->domId($htmlAttributes); + return $this->output(sprintf($this->tags['file'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, '', ' '))); } return $this->output(sprintf($this->tags['file_no_model'], $fieldName, $this->_parseAttributes($htmlAttributes, null, '', ' '))); } @@ -482,15 +361,10 @@ class HtmlHelper extends Helper { * @param array $htmlAttributes Array of HTML attributes. * @return string */ - function hidden($fieldName, $htmlAttributes = null) { - $this->setFormTag($fieldName); - if (!isset($htmlAttributes['value'])) { - $htmlAttributes['value'] = $this->tagValue($fieldName); - } - if (!isset($htmlAttributes['id'])) { - $htmlAttributes['id'] = $this->model . Inflector::camelize($this->field); - } - return $this->output(sprintf($this->tags['hidden'], $this->model, $this->field, $this->_parseAttributes($htmlAttributes, null, ' ', ' '))); + function hidden($fieldName, $htmlAttributes = array()) { + $htmlAttributes = $this->__value($htmlAttributes, $fieldName); + $htmlAttributes = $this->domId($htmlAttributes); + return $this->output(sprintf($this->tags['hidden'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, ' ', ' '))); } /** * Creates a formatted IMG element. @@ -519,27 +393,17 @@ class HtmlHelper extends Helper { * @return string */ function input($fieldName, $htmlAttributes = null) { - $this->setFormTag($fieldName); - if (!isset($htmlAttributes['value'])) { - $htmlAttributes['value'] = $this->tagValue($fieldName); - } + $htmlAttributes = $this->__value($htmlAttributes, $fieldName); + $htmlAttributes = $this->domId($htmlAttributes); if (!isset($htmlAttributes['type'])) { $htmlAttributes['type'] = 'text'; } - if (!isset($htmlAttributes['id'])) { - $htmlAttributes['id'] = $this->model . Inflector::camelize($this->field); + if ($this->tagIsInvalid()) { + $htmlAttributes = $this->addClass($htmlAttributes, 'form_error'); } - - if ($this->tagIsInvalid($this->model, $this->field)) { - if (isset($htmlAttributes['class']) && trim($htmlAttributes['class']) != "") { - $htmlAttributes['class'] .= ' form_error'; - } else { - $htmlAttributes['class'] = 'form_error'; - } - } - return $this->output(sprintf($this->tags['input'], $this->model, $this->field, $this->_parseAttributes($htmlAttributes, null, ' ', ' '))); + return $this->output(sprintf($this->tags['input'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, ' ', ' '))); } /** * Creates a set of radio widgets. @@ -561,7 +425,7 @@ class HtmlHelper extends Helper { $optValue == $value ? $optionsHere['checked'] = 'checked' : null; $parsedOptions = $this->parseHtmlOptions(array_merge($htmlAttributes, $optionsHere), null, '', ' '); $individualTagName = "{$this->field}_{$optValue}"; - $out[] = sprintf($this->tags['radio'], $this->model, $this->field, $individualTagName, $parsedOptions, $optTitle); + $out[] = sprintf($this->tags['radio'], $this->model(), $this->field(), $individualTagName, $parsedOptions, $optTitle); } $out = join($inbetween, $out); @@ -580,7 +444,6 @@ class HtmlHelper extends Helper { foreach($names as $arg) { $out[] = sprintf($this->tags['tableheader'], $this->parseHtmlOptions($thOptions), $arg); } - $data = sprintf($this->tags['tablerow'], $this->parseHtmlOptions($trOptions), join(' ', $out)); return $this->output($data); } @@ -618,23 +481,13 @@ class HtmlHelper extends Helper { */ function tagValue($fieldName) { $this->setFormTag($fieldName); - if (isset($this->params['data'][$this->model][$this->field])) { - return h($this->params['data'][$this->model][$this->field]); - } elseif(isset($this->data[$this->model][$this->field])) { - return h($this->data[$this->model][$this->field]); + if (isset($this->params['data'][$this->model()][$this->field()])) { + return h($this->params['data'][$this->model()][$this->field()]); + } elseif(isset($this->data[$this->model()][$this->field()])) { + return h($this->data[$this->model()][$this->field()]); } return false; } -/** - * Returns false if given FORM field has no errors. Otherwise it returns the constant set in the array Model->validationErrors. - * - * @param string $model Model name as string - * @param string $field Fieldname as string - * @return boolean True on errors. - */ - function tagIsInvalid($model, $field) { - return empty($this->validationErrors[$model][$field]) ? 0 : $this->validationErrors[$model][$field]; - } /** * Returns number of errors in a submitted FORM. * @@ -672,115 +525,16 @@ class HtmlHelper extends Helper { function tagErrorMsg($field, $text) { $error = 1; $this->setFormTag($field); - if ($error == $this->tagIsInvalid($this->model, $this->field)) { + if ($error == $this->tagIsInvalid()) { return sprintf('
', is_array($text) ? (empty($text[$error - 1]) ? 'Error in field' : $text[$error - 1]) : $text); } else { return null; } } -/** - * Sets this helper's model and field properties to the slash-separated value-pair in $tagValue. - * - * @param string $tagValue A field name, like "Modelname/fieldname" - */ - function setFormTag($tagValue) { - return list($this->model, $this->field) = explode("/", $tagValue); - } -/**#@-*/ -/************************************************************************* - * Private methods - *************************************************************************/ -/**#@+ - * @access private - */ -/** - * Returns a space-delimited string with items of the $options array. If a - * key of $options array happens to be one of: - * + 'compact' - * + 'checked' - * + 'declare' - * + 'readonly' - * + 'disabled' - * + 'selected' - * + 'defer' - * + 'ismap' - * + 'nohref' - * + 'noshade' - * + 'nowrap' - * + 'multiple' - * + 'noresize' - * - * And its value is one of: - * + 1 - * + true - * + 'true' - * - * Then the value will be reset to be identical with key's name. - * If the value is not one of these 3, the parameter is not output. - * - * @param array $options Array of options. - * @param array $exclude Array of options to be excluded. - * @param string $insertBefore String to be inserted before options. - * @param string $insertAfter String to be inserted ater options. - * @return string - */ - function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) { - $minimizedAttributes = array('compact', 'checked', 'declare', 'readonly', 'disabled', 'selected', 'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize'); - if (!is_array($exclude)) { - $exclude = array(); - } - - if (is_array($options)) { - $out = array(); - - foreach($options as $key => $value) { - if (!in_array($key, $exclude)) { - if (in_array($key, $minimizedAttributes) && ($value === 1 || $value === true || $value === 'true' || in_array($value, $minimizedAttributes))) { - $value = $key; - } elseif(in_array($key, $minimizedAttributes)) { - continue; - } - $out[] = "{$key}=\"{$value}\""; - } - } - $out = join(' ', $out); - return $out ? $insertBefore . $out . $insertAfter : null; - } else { - return $options ? $insertBefore . $options . $insertAfter : null; - } - } /**#@-*/ /************************************************************************* * Renamed methods *************************************************************************/ -/** - * @deprecated Name changed to '_parseAttributes'. Version 0.9.2. - * @see HtmlHelper::_parseAttributes() - * @param array $options Array of options. - * @param array $exclude Array of options to be excluded. - * @param string $insertBefore String to be inserted before options. - * @param string $insertAfter String to be inserted ater options. - * @return string - */ - function parseHtmlOptions($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) { - if (!is_array($exclude)) { - $exclude = array(); - } - - if (is_array($options)) { - $out = array(); - - foreach($options as $k => $v) { - if (!in_array($k, $exclude)) { - $out[] = "{$k}=\"{$v}\""; - } - } - $out = join(' ', $out); - return $out ? $insertBefore . $out . $insertAfter : null; - } else { - return $options ? $insertBefore . $options . $insertAfter : null; - } - } /** * Returns a formatted SELECT element. * @@ -794,16 +548,14 @@ class HtmlHelper extends Helper { */ function selectTag($fieldName, $optionElements, $selected = null, $selectAttr = null, $optionAttr = null, $showEmpty = true) { $this->setFormTag($fieldName); - if ($this->tagIsInvalid($this->model, $this->field)) { + if ($this->tagIsInvalid()) { if (isset($selectAttr['class']) && trim($selectAttr['class']) != "") { $selectAttr['class'] .= ' form_error'; } else { $selectAttr['class'] = 'form_error'; } } - if (!isset($selectAttr['id'])) { - $selectAttr['id'] = $this->model . Inflector::camelize($this->field); - } + $selectAttr = $this->domId($selectAttr); if (!is_array($optionElements)) { return null; @@ -814,9 +566,9 @@ class HtmlHelper extends Helper { } if (isset($selectAttr) && array_key_exists("multiple", $selectAttr)) { - $select[] = sprintf($this->tags['selectmultiplestart'], $this->model, $this->field, $this->parseHtmlOptions($selectAttr)); + $select[] = sprintf($this->tags['selectmultiplestart'], $this->model(), $this->field(), $this->parseHtmlOptions($selectAttr)); } else { - $select[] = sprintf($this->tags['selectstart'], $this->model, $this->field, $this->parseHtmlOptions($selectAttr)); + $select[] = sprintf($this->tags['selectstart'], $this->model(), $this->field(), $this->parseHtmlOptions($selectAttr)); } if ($showEmpty == true) { diff --git a/cake/libs/view/view.php b/cake/libs/view/view.php index 0d02a8816..c7d70204e 100644 --- a/cake/libs/view/view.php +++ b/cake/libs/view/view.php @@ -71,7 +71,18 @@ class View extends Object{ * @access public */ var $action = null; - +/** + * Name of current model this view context is attached to + * + * @var string + */ + var $model = null; +/** + * Name of current model field this view context is attached to + * + * @var string + */ + var $field = null; /** * An array of names of models the particular controller wants to use. *