abstract class Horde_View_Base
{
/**
+ * @var string
+ */
+ public static $defaultFormBuilder = 'Horde_View_Helper_Form_Builder';
+
+ /**
* Path stack for templates.
*
* @var array
--- /dev/null
+<?php
+/**
+ * Copyright 2007-2008 Maintainable Software, LLC
+ * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage Helper
+ */
+
+/**
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage Helper
+ */
+class Horde_View_Helper_Form extends Horde_View_Helper_Base
+{
+ private $_instanceTag = 'Horde_View_Helper_Form_InstanceTag_Form';
+
+ public function formFor($objectName)
+ {
+ $args = func_get_args();
+ $options = (is_array(end($args))) ? array_pop($args) : array();
+
+ if (isset($options['url'])) {
+ $urlOptions = $options['url'];
+ unset($options['url']);
+ } else {
+ $urlOptions = array();
+ }
+
+ if (isset($options['html'])) {
+ $htmlOptions = $options['html'];
+ unset($options['url']);
+ } else {
+ $htmlOptions = array();
+ }
+ echo $this->formTag($urlOptions, $htmlOptions);
+
+ $options['end'] = '</form>';
+
+ array_push($args, $options);
+ return call_user_func_array(array($this, 'fieldsFor'), $args);
+ }
+
+ public function fieldsFor($objectName)
+ {
+ $args = func_get_args();
+ $options = (is_array(end($args))) ? array_pop($args) : array();
+ $object = isset($args[1]) ? $args[1] : null;
+
+ $builder = isset($options['builder']) ? $options['builder']
+ : Horde_View_Base::$defaultFormBuilder;
+
+ return new $builder($objectName, $object, $this->_view, $options);
+ }
+
+ public function textField($objectName, $method, $options = array())
+ {
+ $object = isset($options['object']) ? $options['object'] : null;
+ unset($options['object']);
+ $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
+ return $tag->toInputFieldTag('text', $options);
+ }
+
+ public function passwordField($objectName, $method, $options = array())
+ {
+ $object = isset($options['object']) ? $options['object'] : null;
+ unset($options['object']);
+ $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
+ return $tag->toInputFieldTag('password', $options);
+ }
+
+ public function hiddenField($objectName, $method, $options = array())
+ {
+ $object = isset($options['object']) ? $options['object'] : null;
+ unset($options['object']);
+ $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
+ return $tag->toInputFieldTag('hidden', $options);
+ }
+
+ public function fileField($objectName, $method, $options = array())
+ {
+ $object = isset($options['object']) ? $options['object'] : null;
+ unset($options['object']);
+ $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
+ return $tag->toInputFieldTag('file', $options);
+ }
+
+ public function checkBox($objectName, $method, $options = array(),
+ $checkedValue = '1', $uncheckedValue = '0')
+ {
+ $object = isset($options['object']) ? $options['object'] : null;
+ unset($options['object']);
+ $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
+ return $tag->toCheckBoxTag($options, $checkedValue, $uncheckedValue);
+ }
+
+ public function radioButton($objectName, $method, $tagValue, $options = array())
+ {
+ $object = isset($options['object']) ? $options['object'] : null;
+ unset($options['object']);
+ $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
+ return $tag->toRadioButtonTag($tagValue, $options);
+ }
+
+ public function textArea($objectName, $method, $options = array())
+ {
+ $object = isset($options['object']) ? $options['object'] : null;
+ unset($options['object']);
+ $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
+ return $tag->toTextAreaTag($options);
+ }
+
+}
--- /dev/null
+<?php
+/**
+ * Copyright 2007-2008 Maintainable Software, LLC
+ * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage Helper
+ */
+
+/**
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage Helper
+ */
+class Horde_View_Helper_Form_Builder
+{
+ private $_objectName;
+ private $_object;
+ private $_view;
+ private $_options;
+ private $_end;
+
+ public function __construct($objectName, $object, $view, $options)
+ {
+ $this->_objectName = $objectName;
+ $this->_object = $object;
+ $this->_view = $view;
+
+ $this->_end = isset($options['end']) ? $options['end'] : '';
+ unset($options['end']);
+ $this->_options = $options;
+ }
+
+ public function __call($method, $args)
+ {
+ if (empty($args)) {
+ throw new InvalidArgumentException('No object property specified');
+ }
+ $objectProperty = $args[0];
+ $options = array_merge(isset($args[1]) ? $args[1] : array(),
+ array('object' => $this->_object));
+
+ return $this->_view->{$method}($this->_objectName, $objectProperty, $options);
+ }
+
+ public function fieldsFor($name) {
+ $name = "{$this->_objectName}[$name]";
+ $args = func_get_args();
+ $args[0] = $name;
+ return call_user_func_array(array($this->_view, 'fieldsFor'), $args);
+ }
+
+ public function checkBox($method, $options = array(), $checkedValue = '1', $uncheckedValue = '0')
+ {
+ $options = array_merge($options, array('object' => $this->_object));
+ return $this->_view->checkBox($this->_objectName, $method, $options, $checkedValue, $uncheckedValue);
+ }
+
+ public function radioButton($method, $tagValue, $options = array())
+ {
+ $options = array_merge($options, array('object' => $this->_object));
+ return $this->_view->radioButton($this->_objectName, $method, $tagValue, $options);
+ }
+
+ // @todo error_message_on
+ // @todo error_messages
+
+ public function submit($value = 'Save changes', $options = array())
+ {
+ $options = array_merge(array('id' => "{$this->_objectName}_submit"), $options);
+ return $this->_view->submitTag($value, $options);
+ }
+
+ public function end()
+ {
+ echo $this->_end;
+ }
+
+}
--- /dev/null
+<?php
+/**
+ * Copyright 2007-2008 Maintainable Software, LLC
+ * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage Helper
+ */
+
+/**
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage Helper
+ */
+class Horde_View_Helper_Form_InstanceTag_Base extends Horde_View_Helper_Tag
+{
+ protected $_defaultFieldOptions = array('size' => 30);
+ protected $_defaultRadioOptions = array();
+ protected $_defaultTextAreaOptions = array('cols' => 40, 'rows' => 20);
+ protected $_defaultDateOptions = array('discardType' => true);
+
+ protected $objectName;
+ protected $objectProperty;
+ protected $object;
+ protected $autoIndex;
+
+ /**
+ * @param array $values Values to cycle through
+ */
+ public function __construct($objectName, $objectProperty, $view, $object = null)
+ {
+ $this->_view = $view;
+ $this->objectProperty = $objectProperty;
+ $this->object = $object;
+
+ if (strpos($objectName, '[]')) {
+ $objectName = rtrim($objectName, '[]');
+ if (! isset($object)) {
+ $object = $view->{$objectName};
+ }
+ if (isset($object) && isset($object->id_before_type_cast)) {
+ $this->autoIndex = $object->id_before_type_cast;
+ } else {
+ $msg = "object[] naming but object param and @object var don't exist or don't respond to id_before_type_cast";
+ throw new InvalidArgumentException($msg);
+ }
+ }
+
+ $this->objectName = $objectName;
+ }
+
+ public function object()
+ {
+ if (isset($this->object)) {
+ return $this->object;
+ } else {
+ return $this->_view->{$this->objectName};
+ }
+ }
+
+ public function value($object)
+ {
+ if (is_object($object)) {
+ return $object->{$this->objectProperty};
+ } else {
+ return null;
+ }
+ }
+
+ protected function valueBeforeTypeCast($object)
+ {
+ if (is_object($object)) {
+ if (isset($object->{"{$this->objectProperty}_before_type_cast"})) {
+ return $object->{"{$this->objectProperty}_before_type_cast"};
+ } else {
+ if (isset($object->{$this->objectProperty})) {
+ return $object->{$this->objectProperty};
+ } else {
+ return null;
+ }
+ }
+ } else {
+ return null;
+ }
+ }
+
+ protected function addDefaultNameAndId($options)
+ {
+ if (isset($options['index'])) {
+ if (! isset($options['name'])) {
+ $options['name'] = $this->tagNameWithIndex($options['index']);
+ }
+ if (! isset($options['id'])) {
+ $options['id'] = $this->tagIdWithIndex($options['index']);
+ }
+ unset($options['index']);
+ } else if (isset($this->autoIndex)) {
+ if (! isset($options['name'])) {
+ $options['name'] = $this->tagNameWithIndex($this->autoIndex);
+ }
+ if (! isset($options['id'])) {
+ $options['id'] = $this->tagIdWithIndex($this->autoIndex);
+ }
+ } else {
+ if (! isset($options['name'])) {
+ $options['name'] = $this->tagName()
+ . (isset($options['multiple']) ? '[]' : '');
+ }
+ if (! isset($options['id'])) {
+ $options['id'] = $this->tagId();
+ }
+ }
+ return $options;
+ }
+
+ protected function tagName()
+ {
+ return "{$this->objectName}[$this->objectProperty]";
+ }
+
+ protected function tagNameWithIndex($index)
+ {
+ return "{$this->objectName}[$index][$this->objectProperty]";
+ }
+
+ protected function tagId()
+ {
+ return $this->sanitizedObjectName() . "_{$this->objectProperty}";
+ }
+
+ protected function tagIdWithIndex($index)
+ {
+ return $this->sanitizedObjectName() . "_{$index}_{$this->objectProperty}";
+ }
+
+ protected function sanitizedObjectName()
+ {
+ $name = preg_replace('/[^-a-zA-Z0-9:.]/', '_', $this->objectName);
+ $name = preg_replace('/_$/', '', $name);
+ return $name;
+ }
+
+}
--- /dev/null
+<?php
+/**
+ * Copyright 2007-2008 Maintainable Software, LLC
+ * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage Helper
+ */
+
+/**
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage Helper
+ */
+class Horde_View_Helper_Form_InstanceTag_Form extends Horde_View_Helper_Form_InstanceTag_Base
+{
+ public function toInputFieldTag($fieldType, $options = array())
+ {
+ if (! isset($options['size'])) {
+ $options['size'] = isset($options['maxlength']) ? $options['maxlength']
+ : $this->_defaultFieldOptions['size'];
+ }
+ $options = array_merge($this->_defaultFieldOptions, $options);
+
+ if ($fieldType == 'hidden') {
+ unset($options['size']);
+ }
+ $options['type'] = $fieldType;
+
+ if ($fieldType != 'file') {
+ if (! isset($options['value'])) {
+ $options['value'] = $this->valueBeforeTypeCast($this->object());
+ }
+ }
+ $options = $this->addDefaultNameAndId($options);
+ return $this->tag('input', $options);
+ }
+
+ public function toRadioButtonTag($tagValue, $options = array())
+ {
+ $options = array_merge($this->_defaultRadioOptions, $options);
+ $options['type'] = 'radio';
+ $options['value'] = $tagValue;
+ if (isset($options['checked'])) {
+ $cv = $options['checked'];
+ unset($options['checked']);
+ $checked = ($cv == true || $cv == 'checked');
+ } else {
+ $checked = $this->isRadioButtonChecked($this->value($this->object()), $tagValue);
+ }
+ if ($checked) {
+ $options['checked'] = 'checked';
+ }
+
+ $prettyTagValue = strval($tagValue);
+ $prettyTagValue = preg_replace('/\s/', '_', $prettyTagValue);
+ $prettyTagValue = preg_replace('/\W/', '', $prettyTagValue);
+ $prettyTagValue = strtolower($prettyTagValue);
+
+ if (! isset($options['id'])) {
+ if (isset($this->autoIndex)) {
+ $options['id'] = "{$this->objectName}_{$this->autoIndex}_{$this->objectProperty}_$prettyTagValue";
+ } else {
+ $options['id'] = "{$this->objectName}_{$this->objectProperty}_$prettyTagValue";
+ }
+ }
+
+ $options = $this->addDefaultNameAndId($options);
+ return $this->tag('input', $options);
+ }
+
+ public function toTextAreaTag($options = array())
+ {
+ $options = array_merge($this->_defaultTextAreaOptions, $options);
+ $options = $this->addDefaultNameAndId($options);
+
+ if (isset($options['size'])) {
+ $size = $options['size'];
+ unset($options['size']);
+
+ list($options['cols'], $options['rows']) = explode('x', $size);
+ }
+
+ if (isset($options['value'])) {
+ $value = $options['value'];
+ unset($options['value']);
+ } else {
+ $value = $this->valueBeforeTypeCast($this->object(), $options);
+ }
+
+ return $this->contentTag('textarea', htmlentities($value, ENT_QUOTES, 'utf-8'), $options);
+ }
+
+ public function toCheckBoxTag($options = array(), $checkedValue = '1', $uncheckedValue = '0')
+ {
+ $options['type'] = 'checkbox';
+ $options['value'] = $checkedValue;
+ if (isset($options['checked'])) {
+ $cv = $options['checked'];
+ unset($options['checked']);
+ $checked = ($cv == true || $cv == 'checked');
+ } else {
+ $checked = $this->isCheckBoxChecked($this->value($this->object()), $checkedValue);
+ }
+ if ($checked) {
+ $options['checked'] = 'checked';
+ }
+ $options = $this->addDefaultNameAndId($options);
+
+ // hidden must output first in PHP to not overwrite checkbox value
+ $tags = $this->tag('input', array('name' => $options['name'],
+ 'type' => 'hidden',
+ 'value' => $uncheckedValue)).
+ $this->tag('input', $options);
+ return $tags;
+ }
+
+ protected function isCheckBoxChecked($value, $checkedValue)
+ {
+ switch (gettype($value)) {
+ case 'boolean':
+ return $value;
+ case 'NULL':
+ return false;
+ case 'integer':
+ return $value != 0;
+ case 'string':
+ return $value == $checkedValue;
+ default:
+ return intval($value) != 0;
+ }
+ }
+
+ protected function isRadioButtonChecked($value, $checkedValue)
+ {
+ return (strval($value) == strval($checkedValue));
+ }
+
+}
--- /dev/null
+<?php
+/**
+ * Copyright 2007-2008 Maintainable Software, LLC
+ * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage Helper
+ */
+
+/**
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage Helper
+ */
+class Horde_View_Helper_FormTag extends Horde_View_Helper_Base
+{
+ public function formTag($urlForOptions = array(), $options = array()) // , *parameters_for_url
+ {
+ $htmlOptions = $this->htmlOptionsForForm($urlForOptions, $options ); // , *parameters_for_url
+ return $this->formTagHtml($htmlOptions);
+ }
+
+ public function endFormTag()
+ {
+ return '</form>';
+ }
+
+ public function selectTag($name, $optionTags = null, $options = array())
+ {
+ return $this->contentTag('select', $optionTags,
+ array_merge(array('name' => $name, 'id' => $name), $options));
+ }
+
+ public function textFieldTag($name, $value = null, $options = array())
+ {
+ return $this->tag('input', array_merge(array('type' => 'text',
+ 'name' => $name,
+ 'id' => $name,
+ 'value' => $value),
+ $options));
+ }
+
+ public function hiddenFieldTag($name, $value = null, $options = array())
+ {
+ return $this->textFieldTag($name, $value, array_merge($options, array('type' => 'hidden')));
+ }
+
+ public function fileFieldTag($name, $options = array())
+ {
+ return $this->textFieldTag($name, null, array_merge($options, array('type' => 'file')));
+ }
+
+ public function passwordFieldTag($name = 'password', $value = null, $options = array())
+ {
+ return $this->textFieldTag($name, $value, array_merge($options, array('type' => 'password')));
+ }
+
+ public function textAreaTag($name, $content = null, $options = array())
+ {
+ if (isset($options['size'])) {
+ $size = $options['size'];
+ unset($options['size']);
+ if (strpos($size, 'x') !== false) {
+ list($options['cols'], $options['rows']) = explode('x', $size);
+ }
+ }
+
+ return $this->contentTag('textarea', $content,
+ array_merge(array('name' => $name, 'id' => $name), $options));
+ }
+
+ public function checkBoxTag($name, $value = '1', $checked = false, $options = array())
+ {
+ $htmlOptions = array_merge(array('type' => 'checkbox',
+ 'name' => $name,
+ 'id' => $name,
+ 'value' => $value), $options);
+ if ($checked) {
+ $htmlOptions['checked'] = 'checked';
+ }
+
+ return $this->tag('input', $htmlOptions);
+ }
+
+ public function radioButtonTag($name, $value, $checked = false, $options = array())
+ {
+ $prettyTagValue = preg_replace('/\s/', '_', $value);
+ $prettyTagValue = strtolower(preg_replace('/(?!-)\W/', '', $prettyTagValue));
+
+ $htmlOptions = array_merge(array('type' => 'radio',
+ 'name' => $name,
+ 'id' => "{$name}_{$prettyTagValue}",
+ 'value' => $value), $options);
+ if ($checked) {
+ $htmlOptions['checked'] = 'checked';
+ }
+
+ return $this->tag('input', $htmlOptions);
+ }
+
+ public function submitTag($value = 'Save changes', $options = array())
+ {
+ if (isset($options['disableWith'])) {
+ $disableWith = $options['disableWith'];
+ unset($options['disableWith']);
+
+ $options['onclick'] = implode(';', array(
+ "this.setAttribute('originalValue', this.value)",
+ "this.disabled=true",
+ "this.value='$disableWith'",
+ "{$options['onclick']}",
+ "result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit())",
+ "if (result == false) { this.value = this.getAttribute('originalValue'); this.disabled = false }",
+ "return result"
+ ));
+ }
+
+ return $this->tag('input', array_merge(array('type' => 'submit', 'name' => 'commit', 'value' => $value),
+ $options));
+ }
+
+ public function imageSubmitTag($source, $options = array())
+ {
+ // source is passed to Horde_View_Helper_Asset->imagePath
+ return $this->tag('input', array_merge(array('type' => 'image',
+ 'src' => $this->imagePath($source)),
+ $options));
+ }
+
+ private function extraTagsForForm($htmlOptions)
+ {
+ $method = isset($htmlOptions['method']) ? strtolower($htmlOptions['method']) : '';
+ if ($method == 'get') {
+ $htmlOptions['method'] = 'get';
+ return array('', $htmlOptions);
+ } else if ($method == 'post' || $method == '') {
+ $htmlOptions['method'] = 'post';
+ return array('', $htmlOptions);
+ } else {
+ $htmlOptions['method'] = 'post';
+ $extraTags = $this->contentTag('div',
+ $this->tag('input', array('type' => 'hidden', 'name' => '_method',
+ 'value' => $method)), array('style' => 'margin:0;padding:0'));
+ return array($extraTags, $htmlOptions);
+ }
+
+ }
+
+ private function formTagHtml($htmlOptions)
+ {
+ list($extraTags, $htmlOptions) = $this->extraTagsForForm($htmlOptions);
+ return $this->tag('form', $htmlOptions, true) . $extraTags;
+ }
+
+ /** @todo url_for */
+ private function htmlOptionsForForm($urlForOptions, $options)
+ {
+ if (isset($options['multipart'])) {
+ unset($options['multipart']);
+ $options['enctype'] = 'multipart/form-data';
+ }
+
+ $options['action'] = $this->urlFor($urlForOptions); // , *parameters_for_url
+ // @todo :
+ // html_options["action"] = url_for(url_for_options, *parameters_for_url)
+
+ return $options;
+ }
+
+}
*/
public function escapeOnce($html)
{
- return $this->_fixDoubleEscape(htmlspecialchars($html));
+ return $this->_fixDoubleEscape($this->_view->escape($html));
}
/**
* @package Horde_View
* @subpackage Helper
*/
-class Horde_View_Helper_Url extends Horde_View_Helper
+class Horde_View_Helper_Url extends Horde_View_Helper_Base
{
/**
- * Creates a link tag of the given +name+ using a URL created by the set
- * of +options+. See the valid options in the documentation for
- * url_for. It's also possible to pass a string instead
- * of an options hash to get a link tag that uses the value of the string as the
- * href for the link, or use +:back+ to link to the referrer - a JavaScript back
- * link will be used in place of a referrer if none exists. If nil is passed as
- * a name, the link itself will become the name.
+ * Returns the URL for the set of +options+ provided. This takes the
+ * same options as url_for in ActionController (see the
+ * documentation for ActionController::Base#url_for). Note that by default
+ * <tt>:only_path</tt> is <tt>true</tt> so you'll get the relative /controller/action
+ * instead of the fully qualified URL like http://example.com/controller/action.
+ *
+ * When called from a view, url_for returns an HTML escaped url. If you
+ * need an unescaped url, pass :escape => false in the +options+.
+ *
+ * ==== Options
+ * * <tt>:anchor</tt> -- specifies the anchor name to be appended to the path.
+ * * <tt>:only_path</tt> -- if true, returns the relative URL (omitting the protocol, host name, and port) (<tt>true</tt> by default unless <tt>:host</tt> is specified)
+ * * <tt>:trailing_slash</tt> -- if true, adds a trailing slash, as in "/archive/2005/". Note that this
+ * is currently not recommended since it breaks caching.
+ * * <tt>:host</tt> -- overrides the default (current) host if provided
+ * * <tt>:protocol</tt> -- overrides the default (current) protocol if provided
+ * * <tt>:user</tt> -- Inline HTTP authentication (only plucked out if :password is also present)
+ * * <tt>:password</tt> -- Inline HTTP authentication (only plucked out if :user is also present)
+ * * <tt>:escape</tt> -- Determines whether the returned URL will be HTML escaped or not (<tt>true</tt> by default)
+ *
+ * ==== Relying on named routes
+ *
+ * If you instead of a hash pass a record (like an Active Record or Active Resource) as the options parameter,
+ * you'll trigger the named route for that record. The lookup will happen on the name of the class. So passing
+ * a Workshop object will attempt to use the workshop_path route. If you have a nested route, such as
+ * admin_workshop_path you'll have to call that explicitly (it's impossible for url_for to guess that route).
+ *
+ * ==== Examples
+ * <%= url_for(:action => 'index') %>
+ * # => /blog/
+ *
+ * <%= url_for(:action => 'find', :controller => 'books') %>
+ * # => /books/find
+ *
+ * <%= url_for(:action => 'login', :controller => 'members', :only_path => false, :protocol => 'https') %>
+ * # => https://www.railsapplication.com/members/login/
+ *
+ * <%= url_for(:action => 'play', :anchor => 'player') %>
+ * # => /messages/play/#player
+ *
+ * <%= url_for(:action => 'checkout', :anchor => 'tax&ship') %>
+ * # => /testing/jump/#tax&ship
+ *
+ * <%= url_for(:action => 'checkout', :anchor => 'tax&ship', :escape => false) %>
+ * # => /testing/jump/#tax&ship
+ *
+ * <%= url_for(Workshop.new) %>
+ * # relies on Workshop answering a new_record? call (and in this case returning true)
+ * # => /workshops
+ *
+ * <%= url_for(@workshop) %>
+ * # calls @workshop.to_s
+ * # => /workshops/5
+ *
+ * @return string
+ */
+ public function urlFor($first = array(), $second = array())
+ {
+ return is_string($first) ? $first : $this->controller->getUrlWriter()->urlFor($first, $second);
+ }
+
+ /**
+ * Creates a link tag of the given +name+ using a URL created by the set of
+ * +options+. See the valid options in the documentation for url_for. It's
+ * also possible to pass a string instead of an options hash to get a link
+ * tag that uses the value of the string as the href for the link, or use
+ * +:back+ to link to the referrer - a JavaScript back link will be used in
+ * place of a referrer if none exists. If nil is passed as a name, the link
+ * itself will become the name.
*
* ==== Options
* * <tt>:confirm => 'question?'</tt> -- This will add a JavaScript confirm
* Note that if the user has JavaScript disabled, the request will fall back
* to using GET. If :href=>'#' is used and the user has JavaScript disabled
* clicking the link will have no effect. If you are relying on the POST
- * behavior, your should check for it in your controller's action by using the
- * request object's methods for post?, delete? or put?.
+ * behavior, your should check for it in your controller's action by using
+ * the request object's methods for post?, delete? or put?.
+ *
+ * You can mix and match the +html_options+ with the exception of :popup and
+ * :method which will raise an ActionView::ActionViewError exception.
+ *
+ * ==== Examples
+ * link_to "Visit Other Site", "http://www.rubyonrails.org/", :confirm => "Are you sure?"
+ * # => <a href="http://www.rubyonrails.org/" onclick="return confirm('Are you sure?');">Visit Other Site</a>
+ *
+ * link_to "Help", { :action => "help" }, :popup => true
+ * # => <a href="/testing/help/" onclick="window.open(this.href);return false;">Help</a>
+ *
+ * link_to "View Image", { :action => "view" }, :popup => ['new_window_name', 'height=300,width=600']
+ * # => <a href="/testing/view/" onclick="window.open(this.href,'new_window_name','height=300,width=600');return false;">View Image</a>
+ *
+ * link_to "Delete Image", { :action => "delete", :id => @image.id }, :confirm => "Are you sure?", :method => :delete
+ * # => <a href="/testing/delete/9/" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form');
+ * f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;
+ * var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');
+ * m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete Image</a>
*/
- public function linkTo($name, $url, $htmlOptions = array())
+ public function linkTo($name, $options = array(), $htmlOptions = array())
{
+ $url = $this->urlFor($options);
+
if ($htmlOptions) {
$href = isset($htmlOptions['href']) ? $htmlOptions['href'] : null;
- // @todo convert_otpions_to_javascript!(html_options, url)
+ // @todo convert_options_to_javascript!(html_options, url)
$tagOptions = $this->tagOptions($htmlOptions);
} else {
$tagOptions = null;
$hrefAttr = isset($href) ? null : 'href="' . $url . '"';
$nameOrUrl = isset($name) ? $name : $url;
- return '<a ' . $hrefAttr . $tagOptions . '>' . $this->escape($nameOrUrl) . '</a>';
+ return '<a ' . $hrefAttr . $tagOptions . '>' . $nameOrUrl . '</a>';
}
/**
}
/**
- * True if the current request URI is the same as the current URL.
+ * Creates a mailto link tag to the specified +email_address+, which is
+ * also used as the name of the link unless +name+ is specified. Additional
+ * HTML attributes for the link can be passed in +html_options+.
+ *
+ * mail_to has several methods for hindering email harvestors and customizing
+ * the email itself by passing special keys to +html_options+.
+ *
+ * ==== Options
+ * * <tt>encode</tt> - This key will accept the strings "javascript" or "hex".
+ * Passing "javascript" will dynamically create and encode the mailto: link then
+ * eval it into the DOM of the page. This method will not show the link on
+ * the page if the user has JavaScript disabled. Passing "hex" will hex
+ * encode the +email_address+ before outputting the mailto: link.
+ * * <tt>replace_at</tt> - When the link +name+ isn't provided, the
+ * +email_address+ is used for the link label. You can use this option to
+ * obfuscate the +email_address+ by substituting the @ sign with the string
+ * given as the value.
+ * * <tt>replace_dot</tt> - When the link +name+ isn't provided, the
+ * +email_address+ is used for the link label. You can use this option to
+ * obfuscate the +email_address+ by substituting the . in the email with the
+ * string given as the value.
+ * * <tt>subject</tt> - Preset the subject line of the email.
+ * * <tt>body</tt> - Preset the body of the email.
+ * * <tt>cc</tt> - Carbon Copy addition recipients on the email.
+ * * <tt>bcc</tt> - Blind Carbon Copy additional recipients on the email.
+ *
+ * ==== Examples
+ * mailTo("me@domain.com")
+ * # => <a href="mailto:me@domain.com">me@domain.com</a>
*
- * @TODO Get REQUEST_URI from somewhere other than the global environment.
+ * mailTo("me@domain.com", "My email", array('encode' => "javascript"))
+ * # => <script type="text/javascript">eval(unescape('%64%6f%63...%6d%65%6e'))</script>
+ *
+ * mailTo("me@domain.com", "My email", array('encode' => "hex"))
+ * # => <a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a>
+ *
+ * mailTo("me@domain.com", null, array('replaceAt' => "_at_", 'replaceDot' => "_dot_", 'class' => "email"))
+ * # => <a href="mailto:me@domain.com" class="email">me_at_domain_dot_com</a>
+ *
+ * mailTo("me@domain.com", "My email", array('cc' => "ccaddress@domain.com",
+ * 'subject' => "This is an example email"))
+ * # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a>
+ */
+ public function mailTo($emailAddress, $name = null, $htmlOptions = array())
+ {
+ // extra options "cc", "bcc", "subject", "body"
+ $extras = '';
+ $extraParts = array('cc', 'bcc', 'body', 'subject');
+ foreach ($extraParts as $partName) {
+ if (isset($htmlOptions[$partName])) {
+ $partValue = str_replace('+', '%20', urlencode($htmlOptions[$partName]));
+ $extras .= "{$partName}={$partValue}&";
+ }
+ unset($htmlOptions[$partName]);
+ }
+ if (! empty($extras)) {
+ $extras = '?' . rtrim($extras, '&');
+ }
+
+ // obfuscation options "replaceAt" and "replaceDot"
+ $emailAddressObfuscated = $emailAddress;
+ foreach (array('replaceAt' => '@', 'replaceDot' => '.') as $option => $find) {
+ if (isset($htmlOptions[$option])) {
+ $emailAddressObfuscated = str_replace($find,
+ $htmlOptions[$option],
+ $emailAddressObfuscated);
+ }
+ unset($htmlOptions[$option]);
+ }
+
+ $string = '';
+
+ $encode = isset($htmlOptions['encode']) ? $htmlOptions['encode'] : null;
+ unset($htmlOptions['encode']);
+
+ if ($encode == 'javascript') {
+ $name = isset($name) ? $name : $emailAddress;
+ $htmlOptions = array_merge($htmlOptions,
+ array('href' => "mailto:{$emailAddress}{$extras}"));
+ $tag = $this->contentTag('a', $name, $htmlOptions);
+
+ foreach (str_split("document.write('$tag');") as $c) {
+ $string .= sprintf("%%%x", ord($c));
+ }
+
+ return "<script type=\"text/javascript\">eval(unescape('$string'))</script>";
+ } elseif ($encode == 'hex') {
+ $emailAddressEncoded = '';
+ foreach (str_split($emailAddressObfuscated) as $c) {
+ $emailAddressEncoded .= sprintf("&#%d;", ord($c));
+ }
+
+ foreach (str_split('mailto:') as $c) {
+ $string .= sprintf("&#%d;", ord($c));
+ }
+
+ foreach (str_split($emailAddress) as $c) {
+ if (preg_match('/\w/', $c)) {
+ $string .= sprintf("%%%x", ord($c));
+ } else {
+ $string .= $c;
+ }
+ }
+ $name = isset($name) ? $name : $emailAddressEncoded;
+ $htmlOptions = array_merge($htmlOptions,
+ array('href' => $string . $extras));
+ return $this->contentTag('a', $name, $htmlOptions);
+
+ } else {
+ $name = isset($name) ? $name : $emailAddressObfuscated;
+ $htmlOptions = array_merge($htmlOptions,
+ array('href' => "mailto:{$emailAddress}{$extras}"));
+ return $this->contentTag('a', $name, $htmlOptions);
+ }
+ }
+
+ /**
+ * True if the current request URI was generated by the given +options+.
+ *
+ * ==== Examples
+ * Let's say we're in the <tt>/shop/checkout</tt> action.
+ *
+ * current_page?(:action => 'process')
+ * # => false
+ *
+ * current_page?(:controller => 'shop', :action => 'checkout')
+ * # => true
+ *
+ * current_page?(:action => 'checkout')
+ * # => true
+ *
+ * current_page?(:controller => 'library', :action => 'checkout')
+ * # => false
+ *
+ * @todo finish implementation
*/
- public function isCurrentPage($url)
+ public function isCurrentPage($options)
{
- return $url == $_SERVER['REQUEST_URI'];
+ $urlString = htmlentities($this->urlFor($options));
+ if (preg_match('/^\w+:\/\//', $urlString)) {
+ // @todo implement
+ // url_string == "#{request.protocol}#{request.host_with_port}#{request.request_uri}"
+ throw new Horde_View_Exception('not implemented');
+ } else {
+ if ($this->controller) {
+ // @todo prepending "/" is a hack, need to fix request object
+ $request = $this->controller->getRequest();
+ $requestUri = '/' . ltrim($request->getUri(), '/');
+ } else {
+ // @todo accessing $_REQUEST directly is a hack
+ $requestUri = $_SERVER['REQUEST_URI'];
+ }
+
+ return $urlString == $requestUri;
+ }
}
}
--- /dev/null
+<?php
+/**
+ * Copyright 2007-2008 Maintainable Software, LLC
+ * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage UnitTests
+ */
+
+/**
+ * @group view
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage UnitTests
+ */
+class Horde_View_Helper_FormTagTest extends Horde_Test_Functional
+{
+ public function setUp()
+ {
+ $this->view = new Horde_View();
+ $this->view->addHelper('FormTag');
+ $this->view->addHelper('Tag');
+ $this->view->addHelper(new Horde_View_Helper_FormTagTest_MockUrlHelper($this->view));
+ }
+
+ public function testFormTag()
+ {
+ $actual = $this->view->formTag();
+ $expected = '<form action="http://www.example.com" method="post">';
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function testFormTagWithExplicitUrl()
+ {
+ $actual = $this->view->formTag('/controller/action');
+ $expected = '<form action="/controller/action" method="post">';
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function testFormTagMultipart()
+ {
+ $actual = $this->view->formTag(array(), array('multipart' => true));
+ $expected = '<form action="http://www.example.com" enctype="multipart/form-data" method="post">';
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function testFormTagWithMethod()
+ {
+ $actual = $this->view->formTag(array(), array('method' => 'put'));
+ $expected = '<form action="http://www.example.com" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div>';
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function testCheckBoxTag()
+ {
+ $actual = $this->view->checkBoxTag('admin');
+ $expected = '<input id="admin" name="admin" type="checkbox" value="1" />';
+ $this->assertDomEquals($expected, $actual);
+ }
+
+ public function testHiddenFieldTag()
+ {
+ $actual = $this->view->hiddenFieldTag('id', 3);
+ $expected = '<input id="id" name="id" type="hidden" value="3" />';
+ $this->assertDomEquals($expected, $actual);
+ }
+
+ public function testFileFieldTag()
+ {
+ $actual = $this->view->fileFieldTag('id');
+ $expected = '<input id="id" name="id" type="file" />';
+ $this->assertDomEquals($expected, $actual);
+ }
+
+ public function testPasswordFieldTag()
+ {
+ $actual = $this->view->passwordFieldTag();
+ $expected = '<input id="password" name="password" type="password" />';
+ $this->assertDomEquals($expected, $actual);
+ }
+
+ public function testRadioButtonTag()
+ {
+ $actual = $this->view->radioButtonTag('people', 'david');
+ $expected = '<input id="people_david" name="people" type="radio" value="david" />';
+ $this->assertDomEquals($expected, $actual);
+
+ $actual = $this->view->radioButtonTag('num_people', 5);
+ $expected = '<input id="num_people_5" name="num_people" type="radio" value="5" />';
+ $this->assertDomEquals($expected, $actual);
+
+ $actual = $this->view->radioButtonTag('gender', 'm')
+ . $this->view->radioButtonTag('gender', 'f');
+ $expected = '<input id="gender_m" name="gender" type="radio" value="m" />'
+ . '<input id="gender_f" name="gender" type="radio" value="f" />';
+ $this->assertEquals($expected, $actual); // @todo assertDomEquals
+
+ $actual = $this->view->radioButtonTag('opinion', '-1')
+ . $this->view->radioButtonTag('opinion', '1');
+ $expected = '<input id="opinion_-1" name="opinion" type="radio" value="-1" />'
+ . '<input id="opinion_1" name="opinion" type="radio" value="1" />';
+ $this->assertEquals($expected, $actual); // @todo assertDomEquals
+ }
+
+ public function testSelectTag()
+ {
+ $actual = $this->view->selectTag('people', '<option>david</option>');
+ $expected = '<select id="people" name="people"><option>david</option></select>';
+ $this->assertDomEquals($expected, $actual);
+ }
+
+ public function testTextAreaTagSizeString()
+ {
+ $actual = $this->view->textAreaTag('body', 'hello world', array('size' => '20x40'));
+ $expected = '<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>';
+ $this->assertDomEquals($expected, $actual);
+ }
+
+ public function testTextAreaTagShouldDisregardSizeIfGivenAsAnInteger()
+ {
+ $actual = $this->view->textAreaTag('body', 'hello world', array('size' => 20));
+ $expected = '<textarea id="body" name="body">hello world</textarea>';
+ $this->assertDomEquals($expected, $actual);
+ }
+
+ public function testTextFieldTag()
+ {
+ $actual = $this->view->textFieldTag('title', 'Hello!');
+ $expected = '<input id="title" name="title" type="text" value="Hello!" />';
+ $this->assertDomEquals($expected, $actual);
+ }
+
+ public function testTextFieldTagClassString()
+ {
+ $actual = $this->view->textFieldTag('title', 'Hello!', array('class' => 'admin'));
+ $expected = '<input class="admin" id="title" name="title" type="text" value="Hello!" />';
+ $this->assertDomEquals($expected, $actual);
+ }
+
+ public function testBooleanOptions()
+ {
+ $this->assertDomEquals('<input checked="checked" disabled="disabled" id="admin" name="admin" readonly="readonly" type="checkbox" value="1" />',
+ $this->view->checkBoxTag("admin", 1, true, array('disabled' => true, 'readonly' => "yes")));
+
+ $this->assertDomEquals('<input checked="checked" id="admin" name="admin" type="checkbox" value="1" />',
+ $this->view->checkBoxTag('admin', 1, true, array('disabled' => false, 'readonly' => null)));
+
+ $this->assertDomEquals('<select id="people" multiple="multiple" name="people"><option>david</option></select>',
+ $this->view->selectTag('people', '<option>david</option>', array('multiple' => true)));
+
+ $this->assertDomEquals('<select id="people" name="people"><option>david</option></select>',
+ $this->view->selectTag('people', '<option>david</option>', array('multiple' => null)));
+ }
+
+ public function testSubmitTag()
+ {
+ $expected = '<input name="commit" onclick="this.setAttribute(\'originalValue\', this.value);this.disabled=true;this.value=\'Saving...\';alert(\'hello!\');result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());if (result == false) { this.value = this.getAttribute(\'originalValue\'); this.disabled = false };return result" type="submit" value="Save" />';
+ $actual = $this->view->submitTag('Save', array('disableWith' => 'Saving...', 'onclick' => "alert('hello!')"));
+ $this->assertDomEquals($expected, $actual);
+ }
+
+}
+
+class Horde_View_Helper_FormTagTest_MockUrlHelper extends Horde_View_Helper_Url
+{
+ public function urlFor($first = array(), $second = array())
+ {
+ return $first ? parent::urlFor($first, $second) : 'http://www.example.com';
+ }
+}
--- /dev/null
+<?php
+/**
+ * Copyright 2007-2008 Maintainable Software, LLC
+ * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage UnitTests
+ */
+
+/**
+ * @group view
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage UnitTests
+ */
+class Horde_View_Helper_FormTest extends Horde_Test_Case
+{
+ public function setUp()
+ {
+ $this->view = new Horde_View();
+ $this->view->addHelper(new Horde_View_Helper_Form($this->view));
+ $this->view->addHelper(new Horde_View_Helper_FormTag($this->view));
+ $this->view->addHelper(new Horde_View_Helper_Tag($this->view));
+ $this->view->addHelper(new Horde_View_Helper_FormTest_MockUrlHelper($this->view));
+
+ $this->post = (object)array('title', 'authorName', 'body',
+ 'secret', 'writtenOn', 'cost');
+ $this->post->title = 'Hello World';
+ $this->post->authorName = '';
+ $this->post->body = 'Back to the hill and over it again!';
+ $this->post->secret = 1;
+ $this->post->writtenOn = mktime(2004, 6, 15);
+ $this->post->id = 123;
+ $this->post->id_before_type_cast = 123;
+
+ $this->view->post = $this->post;
+ }
+
+ public function testTextField()
+ {
+ $this->assertEquals(
+ '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />',
+ $this->view->textField('post', 'title'));
+
+ $this->assertEquals(
+ '<input id="post_title" name="post[title]" size="30" type="password" value="Hello World" />',
+ $this->view->passwordField('post', 'title'));
+
+ $this->assertEquals(
+ '<input id="person_name" name="person[name]" size="30" type="password" />',
+ $this->view->passwordField("person", "name"));
+ }
+
+ public function testTextFieldWithEscapes()
+ {
+ $this->post->title = '<b>Hello World</b>';
+ $this->assertEquals(
+ '<input id="post_title" name="post[title]" size="30" type="text" value="<b>Hello World</b>" />',
+ $this->view->textField('post', 'title'));
+ }
+
+ public function testTextFieldWithOptions()
+ {
+ $expected = '<input id="post_title" name="post[title]" size="35" type="text" value="Hello World" />';
+ $this->assertEquals($expected, $this->view->textField('post', 'title', array('size' => 35)));
+ }
+
+ public function testTextFieldAssumingSize()
+ {
+ $expected = '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />';
+ $this->assertEquals($expected, $this->view->textField('post', 'title', array('maxlength' => 35)));
+ }
+
+ public function testTextFieldDoesntChangeParamValues()
+ {
+ $objectName = 'post[]';
+ $expected = '<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />';
+ $this->assertEquals($expected, $this->view->textField($objectName, 'title'));
+ $this->assertEquals($objectName, 'post[]');
+ }
+
+ public function testCheckBox()
+ {
+ $this->assertEquals(
+ '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
+ $this->view->checkBox('post', 'secret'));
+
+ $this->post->secret = 0;
+
+ $this->assertEquals(
+ '<input name="post[secret]" type="hidden" value="0" /><input id="post_secret" name="post[secret]" type="checkbox" value="1" />',
+ $this->view->checkBox('post', 'secret'));
+
+ $this->assertEquals(
+ '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
+ $this->view->checkBox('post', 'secret', array('checked' => 'checked')));
+
+ $this->post->secret = true;
+
+ $this->assertEquals(
+ '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
+ $this->view->checkBox('post', 'secret'));
+ }
+
+ public function testCheckBoxWithExplicitCheckedAndUncheckedValues()
+ {
+ $this->post->secret = 'on';
+
+ $this->assertEquals(
+ '<input name="post[secret]" type="hidden" value="off" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="on" />',
+ $this->view->checkBox('post', 'secret', array(), 'on', 'off'));
+ }
+
+ public function testRadioButton()
+ {
+ $this->assertEquals(
+ '<input checked="checked" id="post_title_hello_world" name="post[title]" type="radio" value="Hello World" />',
+ $this->view->radioButton('post', 'title', 'Hello World'));
+
+ $this->assertEquals(
+ '<input id="post_title_goodbye_world" name="post[title]" type="radio" value="Goodbye World" />',
+ $this->view->radioButton('post', 'title', 'Goodbye World'));
+ }
+
+ public function testRadioButtonIsCheckedWithIntegers()
+ {
+ $this->assertEquals(
+ '<input checked="checked" id="post_secret_1" name="post[secret]" type="radio" value="1" />',
+ $this->view->radioButton('post', 'secret', '1'));
+ }
+
+ public function testRadioButtonRespectsPassedInId()
+ {
+ $this->assertEquals(
+ '<input checked="checked" id="foo" name="post[secret]" type="radio" value="1" />',
+ $this->view->radioButton('post', 'secret', '1', array('id' => 'foo')));
+ }
+
+ public function testTextArea()
+ {
+ $this->assertEquals(
+ '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
+ $this->view->textArea('post', 'body'));
+ }
+
+ public function testTextAreaWithEscapes()
+ {
+ $this->post->body = "Back to <i>the</i> hill and over it again!";
+ $this->assertEquals(
+ '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to <i>the</i> hill and over it again!</textarea>',
+ $this->view->textArea('post', 'body'));
+ }
+
+ public function testTextAreaWithAlternateValue()
+ {
+ $this->assertEquals(
+ '<textarea cols="40" id="post_body" name="post[body]" rows="20">Testing alternate values.</textarea>',
+ $this->view->textArea('post', 'body', array('value' => 'Testing alternate values.')));
+ }
+
+ public function testTextAreaWithSizeOption()
+ {
+ $this->assertEquals(
+ '<textarea cols="183" id="post_body" name="post[body]" rows="820">Back to the hill and over it again!</textarea>',
+ $this->view->textArea('post', 'body', array('size' => '183x820')));
+ }
+
+ public function testExplicitName()
+ {
+ $this->assertEquals(
+ '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />',
+ $this->view->textField("post", "title", array("name" => "dont guess")));
+
+ $this->assertEquals(
+ '<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>',
+ $this->view->textArea("post", "body", array("name" => "really!")));
+
+ $this->assertEquals(
+ '<input name="i mean it" type="hidden" value="0" /><input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" />',
+ $this->view->checkBox("post", "secret", array("name" => "i mean it")));
+ }
+
+ public function testExplicitId()
+ {
+ $this->assertEquals(
+ '<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />',
+ $this->view->textField("post", "title", array("id" => "dont guess")));
+
+ $this->assertEquals(
+ '<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
+ $this->view->textArea("post", "body", array("id" => "really!")));
+
+ $this->assertEquals(
+ '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" />',
+ $this->view->checkBox("post", "secret", array("id" => "i mean it")));
+ }
+
+ public function testAutoIndex()
+ {
+ $pid = $this->post->id;
+
+ $this->assertEquals(
+ "<input id=\"post_{$pid}_title\" name=\"post[{$pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />",
+ $this->view->textField("post[]", "title"));
+
+ $this->assertEquals(
+ "<textarea cols=\"40\" id=\"post_{$pid}_body\" name=\"post[{$pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
+ $this->view->textArea("post[]", "body"));
+
+ $this->assertEquals(
+ "<input name=\"post[{$pid}][secret]\" type=\"hidden\" value=\"0\" /><input checked=\"checked\" id=\"post_{$pid}_secret\" name=\"post[{$pid}][secret]\" type=\"checkbox\" value=\"1\" />",
+ $this->view->checkBox('post[]', 'secret'));
+
+ $this->assertEquals(
+ "<input checked=\"checked\" id=\"post_{$pid}_title_hello_world\" name=\"post[{$pid}][title]\" type=\"radio\" value=\"Hello World\" />",
+ $this->view->radioButton('post[]', 'title', 'Hello World'));
+
+ $this->assertEquals(
+ "<input id=\"post_{$pid}_title_goodbye_world\" name=\"post[{$pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
+ $this->view->radioButton('post[]', 'title', 'Goodbye World'));
+ }
+
+ public function testFormFor()
+ {
+ ob_start();
+ $form = $this->view->formFor('post', $this->post, array('html' => array('id' => 'create-post')));
+ echo $form->textField('title');
+ echo $form->textArea('body');
+ echo $form->checkBox('secret');
+ echo $form->submit('Create post');
+ $form->end();
+
+ $expected =
+ '<form action="http://www.example.com" id="create-post" method="post">' .
+ '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
+ '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
+ '<input name="post[secret]" type="hidden" value="0" />' .
+ '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />' .
+ '<input id="post_submit" name="commit" type="submit" value="Create post" />' .
+ "</form>";
+
+ $this->assertEquals($expected, ob_get_clean());
+ }
+
+ public function testFormForWithMethod()
+ {
+ ob_start();
+ $form = $this->view->formFor('post', $this->post, array('html' => array('id' => 'create-post',
+ 'method' => 'put')));
+ echo $form->textField('title');
+ echo $form->textArea('body');
+ echo $form->checkBox('secret');
+ $form->end();
+
+ $expected =
+ '<form action="http://www.example.com" id="create-post" method="post">' .
+ '<div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div>' .
+ '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
+ '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
+ '<input name="post[secret]" type="hidden" value="0" />' .
+ '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />' .
+ "</form>";
+
+ $this->assertEquals($expected, ob_get_clean());
+ }
+
+ public function testFormForWithoutObject()
+ {
+ ob_start();
+ $form = $this->view->formFor('post', array('html' => array('id' => 'create-post')));
+ echo $form->textField('title');
+ echo $form->textArea('body');
+ echo $form->checkBox('secret');
+ $form->end();
+
+ $expected =
+ '<form action="http://www.example.com" id="create-post" method="post">' .
+ '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
+ '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
+ '<input name="post[secret]" type="hidden" value="0" />' .
+ '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />' .
+ "</form>";
+
+ $this->assertEquals($expected, ob_get_clean());
+ }
+
+ public function testFormForWithIndex()
+ {
+ ob_start();
+ $form = $this->view->formFor('post[]', $this->post);
+ echo $form->textField('title');
+ echo $form->textArea('body');
+ echo $form->checkBox('secret');
+ $form->end();
+
+ $expected =
+ '<form action="http://www.example.com" method="post">' .
+ '<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />' .
+ '<textarea cols="40" id="post_123_body" name="post[123][body]" rows="20">Back to the hill and over it again!</textarea>' .
+ '<input name="post[123][secret]" type="hidden" value="0" />' .
+ '<input checked="checked" id="post_123_secret" name="post[123][secret]" type="checkbox" value="1" />' .
+ '</form>';
+
+ $this->assertEquals($expected, ob_get_clean());
+ }
+
+ public function testFieldsFor()
+ {
+ ob_start();
+ $fields = $this->view->fieldsFor('post', $this->post);
+ echo $fields->textField('title');
+ echo $fields->textArea('body');
+ echo $fields->checkBox('secret');
+ $fields->end();
+
+ $expected =
+ '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
+ '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
+ '<input name="post[secret]" type="hidden" value="0" />' .
+ '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />';
+
+ $this->assertEquals($expected, ob_get_clean());
+ }
+
+ public function testNestedFieldsFor()
+ {
+ ob_start();
+ $form = $this->view->formFor('post', $this->post);
+ $fields = $form->fieldsFor('comment', $this->post);
+ echo $fields->textField('title');
+ $fields->end();
+ $form->end();
+
+ $expected =
+ '<form action="http://www.example.com" method="post">' .
+ '<input id="post_comment_title" name="post[comment][title]" size="30" type="text" value="Hello World" />' .
+ '</form>';
+
+ $this->assertEquals($expected, ob_get_clean());
+ }
+
+ public function testFieldsForWithoutObject()
+ {
+ ob_start();
+ $fields = $this->view->fieldsFor('post');
+ echo $fields->textField('title');
+ echo $fields->textArea('body');
+ echo $fields->checkBox('secret');
+ $fields->end();
+
+ $expected =
+ '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
+ '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
+ '<input name="post[secret]" type="hidden" value="0" />' .
+ '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />';
+
+ $this->assertEquals($expected, ob_get_clean());
+ }
+
+ public function testFieldsForobjectWithBracketedName()
+ {
+ ob_start();
+ $fields = $this->view->fieldsFor('author[post]', $this->post);
+ echo $fields->textField('title');
+ $fields->end();
+
+ $this->assertEquals(
+ '<input id="author_post_title" name="author[post][title]" size="30" type="text" value="Hello World" />',
+ ob_get_clean());
+ }
+
+ public function testFormbuilderDoesNotHaveFormForMethod()
+ {
+ $methods = get_class_methods('Horde_View_Helper_Form_Builder');
+ $this->assertTrue(empty($methods['formFor']));
+ }
+
+ public function testFormForAndFieldsFor()
+ {
+ ob_start();
+ $postForm = $this->view->formFor('post', $this->post, array('html' => array('id' => 'create-post')));
+ echo $postForm->textField('title');
+ echo $postForm->textArea('body');
+
+ $parentFields = $this->view->fieldsFor('parent_post', $this->post);
+ echo $parentFields->checkBox('secret');
+ $parentFields->end();
+ $postForm->end();
+
+ $expected =
+ '<form action="http://www.example.com" id="create-post" method="post">' .
+ '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
+ '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
+ '<input name="parent_post[secret]" type="hidden" value="0" />' .
+ '<input checked="checked" id="parent_post_secret" name="parent_post[secret]" type="checkbox" value="1" />' .
+ '</form>';
+
+ $this->assertEquals($expected, ob_get_clean());
+ }
+
+ public function testFormForWithCustomBuilder()
+ {
+ ob_start();
+ $form = $this->view->formFor('post', $this->post, array('builder' => 'Horde_View_Helper_FormTest_BuilderMock'));
+ echo $form->textField('bar');
+ echo $form->foo();
+ $form->end();
+
+ $expected =
+ '<form action="http://www.example.com" method="post">' .
+ '<input id="post_bar" name="post[bar]" size="30" type="text" />' .
+ '<foo /></form>';
+
+ $this->assertEquals($expected, ob_get_clean());
+ }
+
+ public function testDefaultFormBuilder()
+ {
+ $oldDefaultFormBuilder = Horde_View_Base::$defaultFormBuilder;
+ Horde_View_Base::$defaultFormBuilder = 'Horde_View_Helper_FormTest_BuilderMock';
+
+ try {
+ ob_start();
+ $form = $this->view->formFor('post', $this->post);
+ echo $form->textField('bar');
+ echo $form->foo();
+ $form->end();
+
+ $expected =
+ '<form action="http://www.example.com" method="post">' .
+ '<input id="post_bar" name="post[bar]" size="30" type="text" />' .
+ '<foo /></form>';
+
+ $this->assertEquals($expected, ob_get_clean());
+ } catch (Exception $e) {}
+
+ Horde_View_Base::$defaultFormBuilder = $oldDefaultFormBuilder;
+ }
+
+ // @todo test_default_form_builder_with_active_record_helpers
+ // @todo test_remote_form_for_with_labelled_builder
+
+ public function testFieldsForWithCustomBuilder()
+ {
+ ob_start();
+ $fields = $this->view->fieldsFor('post', $this->post, array('builder' => 'Horde_View_Helper_FormTest_BuilderMock'));
+ echo $fields->textField('bar');
+ echo $fields->foo();
+ $fields->end();
+
+ $this->assertEquals(
+ '<input id="post_bar" name="post[bar]" size="30" type="text" /><foo />',
+ ob_get_clean());
+ }
+
+ public function testFormForWithHtmlOptionsAddsOptionsToFormTag()
+ {
+ ob_start();
+ $form = $this->view->formFor('post', $this->post, array('html' => array('id' => 'some_form',
+ 'class' => 'some_class')));
+ $form->end();
+
+ $this->assertEquals(
+ '<form action="http://www.example.com" class="some_class" id="some_form" method="post"></form>',
+ ob_get_clean());
+ }
+
+
+ public function testFormForWithHiddenFieldMadOnly()
+ {
+ ob_start();
+ $form = $this->view->formFor('post', $this->post);
+ echo $form->hiddenField('title');
+ $form->end();
+
+ $expected =
+ '<form action="http://www.example.com" method="post">' .
+ '<input id="post_title" name="post[title]" type="hidden" value="Hello World" />' .
+ '</form>';
+
+ $this->assertEquals($expected, ob_get_clean());
+ }
+
+ public function testFormForWithFileFieldMadOnly()
+ {
+ ob_start();
+ $form = $this->view->formFor('post', $this->post);
+ echo $form->fileField('title');
+ $form->end();
+
+ $expected =
+ '<form action="http://www.example.com" method="post">' .
+ '<input id="post_title" name="post[title]" size="30" type="file" />' .
+ '</form>';
+
+ $this->assertEquals($expected, ob_get_clean());
+ }
+
+ // @todo test_form_for_with_string_url_option
+ // @todo test_form_for_with_hash_url_option
+ // @todo test_remote_form_for_with_html_options_adds_options_to_form_tag
+}
+
+class Horde_View_Helper_FormTest_MockUrlHelper extends Horde_View_Helper_Base
+{
+ public function urlFor($options)
+ {
+ return 'http://www.example.com';
+ }
+}
+
+class Horde_View_Helper_FormTest_BuilderMock extends Horde_View_Helper_Form_Builder
+{
+ public function foo()
+ {
+ return '<foo />';
+ }
+}
--- /dev/null
+<?php
+/**
+ * Copyright 2007-2008 Maintainable Software, LLC
+ * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage UnitTests
+ */
+
+/**
+ * @group view
+ * @author Mike Naberezny <mike@maintainable.com>
+ * @author Derek DeVries <derek@maintainable.com>
+ * @author Chuck Hagenbuch <chuck@horde.org>
+ * @license http://opensource.org/licenses/bsd-license.php
+ * @category Horde
+ * @package Horde_View
+ * @subpackage UnitTests
+ */
+class Horde_View_Helper_UrlTest extends Horde_Test_Case
+{
+ public function setUp()
+ {
+ $controller = new Horde_View_Helper_UrlTest_MockController();
+ $this->view = new Horde_View();
+ $this->view->controller = $controller;
+ $this->view->addHelper('Url');
+ $this->view->addHelper('Tag');
+ }
+
+ public function testLinkTagWithStraightUrl()
+ {
+ $this->assertEquals('<a href="http://www.example.com">Hello</a>',
+ $this->view->linkTo('Hello', 'http://www.example.com'));
+ }
+
+ public function testLinkTagWithQuery()
+ {
+ $this->assertEquals('<a href="http://www.example.com?q1=v1&q2=v2">Hello</a>',
+ $this->view->linkTo('Hello', 'http://www.example.com?q1=v1&q2=v2'));
+ }
+
+ public function testLinkTagWithQueryAndNoName()
+ {
+ $this->assertEquals("<a href=\"http://www.example.com?q1=v1&q2=v2\">http://www.example.com?q1=v1&q2=v2</a>",
+ $this->view->linkTo(null, 'http://www.example.com?q1=v1&q2=v2'));
+ }
+
+ public function testLinkTagWithImg()
+ {
+ $this->assertEquals("<a href=\"http://www.example.com\"><img src='/favicon.jpg' /></a>",
+ $this->view->linkTo("<img src='/favicon.jpg' />", "http://www.example.com"));
+ }
+
+ public function testLinkToUnless()
+ {
+ $this->assertEquals('Showing',
+ $this->view->linkToUnless(true, 'Showing', array('action' => 'show', 'controller' => 'weblog')));
+ $this->assertEquals("<a href=\"/weblog/list\">Listing</a>", // @todo http://www.example.com
+ $this->view->linkToUnless(false, 'Listing', array('action' => 'list', 'controller' => 'weblog')));
+ $this->assertEquals('Showing',
+ $this->view->linkToUnless(true, 'Showing', array('action' => 'show', 'controller' => 'weblog', 'id' => 1)));
+ }
+
+ public function testLinkToIf()
+ {
+ $this->assertEquals('Showing',
+ $this->view->linkToIf(false, 'Showing', array('action' => 'show', 'controller' => 'weblog')));
+ $this->assertEquals("<a href=\"/weblog/list\">Listing</a>", // @todo http://www.example.com
+ $this->view->linkToIf(true, 'Listing', array('action' => 'list', 'controller' => 'weblog')));
+ $this->assertEquals('Showing',
+ $this->view->linkToIf(false, 'Showing', array('action' => 'show', 'controller' => 'weblog', 'id' => 1)));
+ }
+
+ public function testMailTo()
+ {
+ $this->assertEquals("<a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>",
+ $this->view->mailTo("david@loudthinking.com"));
+ $this->assertEquals("<a href=\"mailto:david@loudthinking.com\">David Heinemeier Hansson</a>",
+ $this->view->mailTo("david@loudthinking.com", "David Heinemeier Hansson"));
+ $this->assertEquals("<a class=\"admin\" href=\"mailto:david@loudthinking.com\">David Heinemeier Hansson</a>",
+ $this->view->mailTo("david@loudthinking.com", "David Heinemeier Hansson", array("class" => "admin")));
+ }
+
+
+ public function testMailToWithJavascript()
+ {
+ $this->assertEquals("<script type=\"text/javascript\">eval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))</script>",
+ $this->view->mailTo("me@domain.com", "My email", array('encode' => 'javascript')));
+ }
+
+ public function testMailWithOptions()
+ {
+ $this->assertEquals('<a href="mailto:me@example.com?cc=ccaddress%40example.com&bcc=bccaddress%40example.com&body=This%20is%20the%20body%20of%20the%20message.&subject=This%20is%20an%20example%20email">My email</a>',
+ $this->view->mailTo("me@example.com", "My email", array('cc' => "ccaddress@example.com", 'bcc' => "bccaddress@example.com", 'subject' => "This is an example email", 'body' => "This is the body of the message.")));
+ }
+
+ public function testMailToWithImg()
+ {
+ $this->assertEquals('<a href="mailto:feedback@example.com"><img src="/feedback.png" /></a>',
+ $this->view->mailTo('feedback@example.com', '<img src="/feedback.png" />'));
+ }
+
+ public function testMailToWithHex()
+ {
+ $this->assertEquals("<a href=\"mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">My email</a>",
+ $this->view->mailTo("me@domain.com", "My email", array('encode' => "hex")));
+ $this->assertEquals("<a href=\"mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">me@domain.com</a>",
+ $this->view->mailTo("me@domain.com", null, array('encode' => "hex")));
+ }
+
+ public function testMailToWithReplaceOptions()
+ {
+ $this->assertEquals("<a href=\"mailto:wolfgang@stufenlos.net\">wolfgang(at)stufenlos(dot)net</a>",
+ $this->view->mailTo("wolfgang@stufenlos.net", null, array('replaceAt' => "(at)", 'replaceDot' => "(dot)")));
+ $this->assertEquals("<a href=\"mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">me(at)domain.com</a>",
+ $this->view->mailTo("me@domain.com", null, array('encode' => "hex", 'replaceAt' => "(at)")));
+ $this->assertEquals("<a href=\"mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">My email</a>",
+ $this->view->mailTo("me@domain.com", "My email", array('encode' => "hex", 'replaceAt' => "(at)")));
+ $this->assertEquals("<a href=\"mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">me(at)domain(dot)com</a>",
+ $this->view->mailTo("me@domain.com", null, array('encode' => "hex", 'replaceAt' => "(at)", 'replaceDot' => "(dot)")));
+ }
+
+}
+
+class Horde_View_Helper_UrlTest_MockController extends Horde_Controller_Base
+{
+ public function getControllerName() { return 'mock'; }
+}