~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Form/Decorator/HtmlTag.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @category   Zend
 
16
 * @package    Zend_Form
 
17
 * @subpackage Decorator
 
18
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
19
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
20
 */
 
21
 
 
22
/**
 
23
 * @see Zend_Form_Decorator_Abstract
 
24
 */
 
25
require_once 'Zend/Form/Decorator/Abstract.php';
 
26
 
 
27
/**
 
28
 * Zend_Form_Decorator_Element_HtmlTag
 
29
 *
 
30
 * Wraps content in an HTML block tag.
 
31
 *
 
32
 * Options accepted are:
 
33
 * - tag: tag to use in decorator
 
34
 * - noAttribs: do not render attributes in the opening tag
 
35
 * - placement: 'append' or 'prepend'. If 'append', renders opening and 
 
36
 *   closing tag after content; if prepend, renders opening and closing tag
 
37
 *   before content.
 
38
 * - openOnly: render opening tag only
 
39
 * - closeOnly: render closing tag only
 
40
 *
 
41
 * Any other options passed are processed as HTML attributes of the tag.
 
42
 * 
 
43
 * @category   Zend
 
44
 * @package    Zend_Form
 
45
 * @subpackage Decorator
 
46
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
47
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
48
 * @version    $Id: HtmlTag.php 12514 2008-11-10 16:30:24Z matthew $
 
49
 */
 
50
class Zend_Form_Decorator_HtmlTag extends Zend_Form_Decorator_Abstract
 
51
{
 
52
    /**
 
53
     * Placement; default to surround content
 
54
     * @var string
 
55
     */
 
56
    protected $_placement = null;
 
57
 
 
58
    /**
 
59
     * HTML tag to use
 
60
     * @var string
 
61
     */
 
62
    protected $_tag;
 
63
 
 
64
    /**
 
65
     * @var Zend_Filter
 
66
     */
 
67
    protected $_tagFilter;
 
68
 
 
69
    /**
 
70
     * Convert options to tag attributes
 
71
     * 
 
72
     * @return string
 
73
     */
 
74
    protected function _htmlAttribs(array $attribs)
 
75
    {
 
76
        $xhtml = '';
 
77
        foreach ((array) $attribs as $key => $val) {
 
78
            $key = htmlspecialchars($key, ENT_COMPAT, 'UTF-8');
 
79
            if (is_array($val)) {
 
80
                $val = implode(' ', $val);
 
81
            }
 
82
            $val    = htmlspecialchars($val, ENT_COMPAT, 'UTF-8');
 
83
            $xhtml .= " $key=\"$val\"";
 
84
        }
 
85
        return $xhtml;
 
86
    }
 
87
 
 
88
    /**
 
89
     * Normalize tag
 
90
     *
 
91
     * Ensures tag is alphanumeric characters only, and all lowercase.
 
92
     * 
 
93
     * @param  string $tag 
 
94
     * @return string
 
95
     */
 
96
    public function normalizeTag($tag)
 
97
    {
 
98
        if (!isset($this->_tagFilter)) {
 
99
            require_once 'Zend/Filter.php';
 
100
            require_once 'Zend/Filter/Alnum.php';
 
101
            require_once 'Zend/Filter/StringToLower.php';
 
102
            $this->_tagFilter = new Zend_Filter();
 
103
            $this->_tagFilter->addFilter(new Zend_Filter_Alnum())
 
104
                             ->addFilter(new Zend_Filter_StringToLower());
 
105
        }
 
106
        return $this->_tagFilter->filter($tag);
 
107
    }
 
108
 
 
109
    /**
 
110
     * Set tag to use
 
111
     * 
 
112
     * @param  string $tag 
 
113
     * @return Zend_Form_Decorator_HtmlTag
 
114
     */
 
115
    public function setTag($tag)
 
116
    {
 
117
        $this->_tag = $this->normalizeTag($tag);
 
118
        return $this;
 
119
    }
 
120
 
 
121
    /**
 
122
     * Get tag
 
123
     *
 
124
     * If no tag is registered, either via setTag() or as an option, uses 'div'.
 
125
     * 
 
126
     * @return string
 
127
     */
 
128
    public function getTag()
 
129
    {
 
130
        if (null === $this->_tag) {
 
131
            if (null === ($tag = $this->getOption('tag'))) {
 
132
                $this->setTag('div');
 
133
            } else {
 
134
                $this->setTag($tag);
 
135
                $this->removeOption('tag');
 
136
            }
 
137
        }
 
138
 
 
139
        return $this->_tag;
 
140
    }
 
141
 
 
142
    /**
 
143
     * Get the formatted open tag
 
144
     * 
 
145
     * @param  string $tag 
 
146
     * @param  array $attribs 
 
147
     * @return string
 
148
     */
 
149
    protected function _getOpenTag($tag, array $attribs = null)
 
150
    {
 
151
        $html = '<' . $tag;
 
152
        if (null !== $attribs) {
 
153
            $html .= $this->_htmlAttribs($attribs);
 
154
        }
 
155
        $html .= '>';
 
156
        return $html;
 
157
    }
 
158
 
 
159
    /**
 
160
     * Get formatted closing tag
 
161
     * 
 
162
     * @param  string $tag 
 
163
     * @return string
 
164
     */
 
165
    protected function _getCloseTag($tag)
 
166
    {
 
167
        return '</' . $tag . '>';
 
168
    }
 
169
 
 
170
    /**
 
171
     * Render content wrapped in an HTML tag
 
172
     * 
 
173
     * @param  string $content 
 
174
     * @return string
 
175
     */
 
176
    public function render($content)
 
177
    {
 
178
        $tag       = $this->getTag();
 
179
        $placement = $this->getPlacement();
 
180
        $noAttribs = $this->getOption('noAttribs');
 
181
        $openOnly  = $this->getOption('openOnly');
 
182
        $closeOnly = $this->getOption('closeOnly');
 
183
        $this->removeOption('noAttribs');
 
184
        $this->removeOption('openOnly');
 
185
        $this->removeOption('closeOnly');
 
186
 
 
187
        $attribs = null;
 
188
        if (!$noAttribs) {
 
189
            $attribs = $this->getOptions();
 
190
        }
 
191
 
 
192
        switch ($placement) {
 
193
            case self::APPEND:
 
194
                if ($closeOnly) {
 
195
                    return $content . $this->_getCloseTag($tag);
 
196
                }
 
197
                if ($openOnly) {
 
198
                    return $content . $this->_getOpenTag($tag, $attribs);
 
199
                }
 
200
                return $content 
 
201
                     . $this->_getOpenTag($tag, $attribs) 
 
202
                     . $this->_getCloseTag($tag);
 
203
            case self::PREPEND:
 
204
                if ($closeOnly) {
 
205
                    return $this->_getCloseTag($tag) . $content;
 
206
                }
 
207
                if ($openOnly) {
 
208
                    return $this->_getOpenTag($tag, $attribs) . $content;
 
209
                }
 
210
                return $this->_getOpenTag($tag, $attribs)
 
211
                     . $this->_getCloseTag($tag)
 
212
                     . $content;
 
213
            default:
 
214
                return (($openOnly || !$closeOnly) ? $this->_getOpenTag($tag, $attribs) : '')
 
215
                     . $content
 
216
                     . (($closeOnly || !$openOnly) ? $this->_getCloseTag($tag) : '');
 
217
        }
 
218
    }
 
219
}