Overview

Packages

  • View
    • Helper

Classes

  • Horde_View_Helper_Base
  • Horde_View_Helper_Benchmark
  • Horde_View_Helper_Benchmark_Timer
  • Horde_View_Helper_Block
  • Horde_View_Helper_Capture
  • Horde_View_Helper_Capture_Base
  • Horde_View_Helper_Capture_ContentFor
  • Horde_View_Helper_Date
  • Horde_View_Helper_Debug
  • Horde_View_Helper_Form
  • Horde_View_Helper_Form_Builder
  • Horde_View_Helper_Form_InstanceTag_Base
  • Horde_View_Helper_Form_InstanceTag_Form
  • Horde_View_Helper_FormTag
  • Horde_View_Helper_Javascript
  • Horde_View_Helper_Number
  • Horde_View_Helper_Tag
  • Horde_View_Helper_Text
  • Horde_View_Helper_Text_Cycle
  • Horde_View_Helper_Url
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Copyright 2007-2008 Maintainable Software, LLC
  4:  * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
  5:  *
  6:  * @author     Mike Naberezny <mike@maintainable.com>
  7:  * @author     Derek DeVries <derek@maintainable.com>
  8:  * @author     Chuck Hagenbuch <chuck@horde.org>
  9:  * @license    http://www.horde.org/licenses/bsd
 10:  * @category   Horde
 11:  * @package    View
 12:  * @subpackage Helper
 13:  */
 14: 
 15: /**
 16:  * @author     Mike Naberezny <mike@maintainable.com>
 17:  * @author     Derek DeVries <derek@maintainable.com>
 18:  * @author     Chuck Hagenbuch <chuck@horde.org>
 19:  * @license    http://www.horde.org/licenses/bsd
 20:  * @category   Horde
 21:  * @package    View
 22:  * @subpackage Helper
 23:  */
 24: class Horde_View_Helper_FormTag extends Horde_View_Helper_Base
 25: {
 26:     public function formTag($urlForOptions = array(), $options = array()) // , *parameters_for_url
 27:     {
 28:         $htmlOptions = $this->htmlOptionsForForm($urlForOptions, $options );  // , *parameters_for_url
 29:         return $this->formTagHtml($htmlOptions);
 30:     }
 31: 
 32:     public function endFormTag()
 33:     {
 34:         return '</form>';
 35:     }
 36: 
 37:     public function selectTag($name, $optionTags = null, $options = array())
 38:     {
 39:         return $this->contentTag('select', $optionTags,
 40:                                  array_merge(array('name' => $name, 'id' => $name), $options));
 41:     }
 42: 
 43:     public function textFieldTag($name, $value = null, $options = array())
 44:     {
 45:         return $this->tag('input', array_merge(array('type'  => 'text',
 46:                                                      'name'  => $name,
 47:                                                      'id'    => $name,
 48:                                                      'value' => $value),
 49:                                                $options));
 50:     }
 51: 
 52:     public function hiddenFieldTag($name, $value = null, $options = array())
 53:     {
 54:         return $this->textFieldTag($name, $value, array_merge($options, array('type' => 'hidden')));
 55:     }
 56: 
 57:     public function fileFieldTag($name, $options = array())
 58:     {
 59:         return $this->textFieldTag($name, null, array_merge($options, array('type' => 'file')));
 60:     }
 61: 
 62:     public function passwordFieldTag($name = 'password', $value = null, $options = array())
 63:     {
 64:         return $this->textFieldTag($name, $value, array_merge($options, array('type' => 'password')));
 65:     }
 66: 
 67:     public function textAreaTag($name, $content = null, $options = array())
 68:     {
 69:         if (isset($options['size'])) {
 70:             $size = $options['size'];
 71:             unset($options['size']);
 72:             if (strpos($size, 'x') !== false) {
 73:                 list($options['cols'], $options['rows']) = explode('x', $size);
 74:             }
 75:         }
 76: 
 77:         return $this->contentTag('textarea', $content,
 78:                                  array_merge(array('name' => $name, 'id' => $name), $options));
 79:     }
 80: 
 81:     public function checkBoxTag($name, $value = '1', $checked = false, $options = array())
 82:     {
 83:         $htmlOptions = array_merge(array('type'  => 'checkbox',
 84:                                          'name'  => $name,
 85:                                          'id'    => $name,
 86:                                          'value' => $value,
 87:                                          'checked' => $checked), $options);
 88: 
 89:         return $this->tag('input', $htmlOptions);
 90:     }
 91: 
 92:     public function radioButtonTag($name, $value, $checked = false, $options = array())
 93:     {
 94:         $prettyTagValue = preg_replace('/\s/', '_', $value);
 95:         $prettyTagValue = strtolower(preg_replace('/(?!-)\W/', '', $prettyTagValue));
 96: 
 97:         $htmlOptions = array_merge(array('type'  => 'radio',
 98:                                          'name'  => $name,
 99:                                          'id'    => "{$name}_{$prettyTagValue}",
100:                                          'value' => $value,
101:                                          'checked' => $checked), $options);
102: 
103:         return $this->tag('input', $htmlOptions);
104:     }
105: 
106:     public function submitTag($value = 'Save changes', $options = array())
107:     {
108:         if (isset($options['disableWith'])) {
109:             $disableWith = $options['disableWith'];
110:             unset($options['disableWith']);
111: 
112:             $options['onclick'] = implode(';', array(
113:                 "this.setAttribute('originalValue', this.value)",
114:                 "this.disabled=true",
115:                 "this.value='$disableWith'",
116:                 "{$options['onclick']}",
117:                 "result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit())",
118:                 "if (result == false) { this.value = this.getAttribute('originalValue'); this.disabled = false }",
119:                 "return result"
120:             ));
121:         }
122: 
123:         return $this->tag('input', array_merge(array('type' => 'submit', 'name' => 'commit', 'value' => $value),
124:                                                $options));
125:     }
126: 
127:     public function imageSubmitTag($source, $options = array())
128:     {
129:         // source is passed to Horde_View_Helper_Asset->imagePath
130:         return $this->tag('input', array_merge(array('type' => 'image',
131:                                                      'src'  => $this->imagePath($source)),
132:                                                $options));
133:     }
134: 
135:     private function extraTagsForForm($htmlOptions)
136:     {
137:         $method = isset($htmlOptions['method']) ? strtolower($htmlOptions['method']) : '';
138:         if ($method == 'get') {
139:             $htmlOptions['method'] = 'get';
140:             return array('', $htmlOptions);
141:         } else if ($method == 'post' || $method == '') {
142:             $htmlOptions['method'] = 'post';
143:             return array('', $htmlOptions);
144:         } else {
145:             $htmlOptions['method'] = 'post';
146:             $extraTags = $this->contentTag('div',
147:                              $this->tag('input', array('type'  => 'hidden', 'name'  => '_method',
148:                                                        'value' => $method)), array('style' => 'margin:0;padding:0'));
149:             return array($extraTags, $htmlOptions);
150:         }
151: 
152:     }
153: 
154:     private function formTagHtml($htmlOptions)
155:     {
156:         list($extraTags, $htmlOptions) = $this->extraTagsForForm($htmlOptions);
157:         return $this->tag('form', $htmlOptions, true) . $extraTags;
158:     }
159: 
160:     /** @todo url_for */
161:     private function htmlOptionsForForm($urlForOptions, $options)
162:     {
163:         if (isset($options['multipart'])) {
164:             unset($options['multipart']);
165:             $options['enctype'] = 'multipart/form-data';
166:         }
167: 
168:         $options['action'] = $this->urlFor($urlForOptions); // , *parameters_for_url
169:         // @todo :
170:         // html_options["action"]  = url_for(url_for_options, *parameters_for_url)
171: 
172:         return $options;
173:     }
174: 
175: }
176: 
API documentation generated by ApiGen