1<?php
2/**
3 * Copyright 2007-2008 Maintainable Software, LLC
4 * Copyright 2008-2016 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 */
24class Horde_View_Helper_Form extends Horde_View_Helper_Base
25{
26    private $_instanceTag = 'Horde_View_Helper_Form_InstanceTag_Form';
27
28    public function formFor($objectName)
29    {
30        $args = func_get_args();
31        $options = is_array(end($args)) ? array_pop($args) : array();
32
33        if (isset($options['url'])) {
34            $urlOptions = $options['url'];
35            unset($options['url']);
36        } else {
37            $urlOptions = array();
38        }
39
40        if (isset($options['html'])) {
41            $htmlOptions = $options['html'];
42            unset($options['url']);
43        } else {
44            $htmlOptions = array();
45        }
46        echo $this->formTag($urlOptions, $htmlOptions);
47
48        $options['end'] = '</form>';
49
50        $args[] = $options;
51        return call_user_func_array(array($this, 'fieldsFor'), $args);
52    }
53
54    public function fieldsFor($objectName)
55    {
56        $args = func_get_args();
57        $options = is_array(end($args)) ? array_pop($args) : array();
58        $object  = isset($args[1]) ? $args[1] : null;
59
60        $builder = isset($options['builder'])
61            ? $options['builder']
62            : Horde_View_Base::$defaultFormBuilder;
63
64        return new $builder($objectName, $object, $this->_view, $options);
65    }
66
67    /**
68     * Returns a label tag tailored for labelling an input field for a
69     * specified attribute (identified by $method) on an object assigned to the
70     * template (identified by $objectName).
71     *
72     * The text of label will default to the attribute name unless you specify
73     * it explicitly. Additional options on the label tag can be passed as a
74     * hash with $options. These options will be tagged onto the HTML as an
75     * HTML element attribute as in the example shown.
76     *
77     * Examples:
78     *
79     * <code>
80     * $this->label('post', 'title');
81     * // => <label for="post_title">Title</label>
82     *
83     * $this->label('post', 'title', 'A short title')
84     * // => <label for="post_title">A short title</label>
85     *
86     * $this->label('post', 'title', 'A short title',
87     *              array('class' => 'title_label'));
88     * // => <label for="post_title" class="title_label">A short title</label>
89     * </code>
90     */
91    public function label($objectName, $method, $text, $options = array())
92    {
93        $object = isset($options['object']) ? $options['object'] : null;
94        unset($options['object']);
95        $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
96        return $tag->toLabelTag($text, $options);
97    }
98
99    public function textField($objectName, $method, $options = array())
100    {
101        $object = isset($options['object']) ? $options['object'] : null;
102        unset($options['object']);
103        $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
104        return $tag->toInputFieldTag('text', $options);
105    }
106
107    public function passwordField($objectName, $method, $options = array())
108    {
109        $object = isset($options['object']) ? $options['object'] : null;
110        unset($options['object']);
111        $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
112        return $tag->toInputFieldTag('password', $options);
113    }
114
115    public function hiddenField($objectName, $method, $options = array())
116    {
117        $object = isset($options['object']) ? $options['object'] : null;
118        unset($options['object']);
119        $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
120        return $tag->toInputFieldTag('hidden', $options);
121    }
122
123    public function fileField($objectName, $method, $options = array())
124    {
125        $object = isset($options['object']) ? $options['object'] : null;
126        unset($options['object']);
127        $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
128        return $tag->toInputFieldTag('file', $options);
129    }
130
131    public function checkBox($objectName, $method, $options = array(),
132                             $checkedValue = '1', $uncheckedValue = '0')
133    {
134        $object = isset($options['object']) ? $options['object'] : null;
135        unset($options['object']);
136        $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
137        return $tag->toCheckBoxTag($options, $checkedValue, $uncheckedValue);
138    }
139
140    public function radioButton($objectName, $method, $tagValue, $options = array())
141    {
142        $object = isset($options['object']) ? $options['object'] : null;
143        unset($options['object']);
144        $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
145        return $tag->toRadioButtonTag($tagValue, $options);
146    }
147
148    public function textArea($objectName, $method, $options = array())
149    {
150        $object = isset($options['object']) ? $options['object'] : null;
151        unset($options['object']);
152        $tag = new $this->_instanceTag($objectName, $method, $this->_view, $object);
153        return $tag->toTextAreaTag($options);
154    }
155}
156