PageRenderTime 24ms CodeModel.GetById 12ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Form/Decorator/Label.php

https://bitbucket.org/skuda/rsslounge
PHP | 332 lines | 178 code | 36 blank | 118 comment | 33 complexity | 352dbee36b3dbfacabf8150d7218b6f2 MD5 | raw file
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 */
 21
 22/** Zend_Form_Decorator_Abstract */
 23require_once 'Zend/Form/Decorator/Abstract.php';
 24
 25/**
 26 * Zend_Form_Decorator_Label
 27 *
 28 * Accepts the options:
 29 * - separator: separator to use between label and content (defaults to PHP_EOL)
 30 * - placement: whether to append or prepend label to content (defaults to prepend)
 31 * - tag: if set, used to wrap the label in an additional HTML tag
 32 * - opt(ional)Prefix: a prefix to the label to use when the element is optional
 33 * - opt(iona)lSuffix: a suffix to the label to use when the element is optional
 34 * - req(uired)Prefix: a prefix to the label to use when the element is required
 35 * - req(uired)Suffix: a suffix to the label to use when the element is required
 36 *
 37 * Any other options passed will be used as HTML attributes of the label tag.
 38 *
 39 * @category   Zend
 40 * @package    Zend_Form
 41 * @subpackage Decorator
 42 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 43 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 44 * @version    $Id: Label.php 22128 2010-05-06 11:18:02Z alab $
 45 */
 46class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract
 47{
 48    /**
 49     * Default placement: prepend
 50     * @var string
 51     */
 52    protected $_placement = 'PREPEND';
 53
 54    /**
 55     * HTML tag with which to surround label
 56     * @var string
 57     */
 58    protected $_tag;
 59
 60    /**
 61     * Set element ID
 62     *
 63     * @param  string $id
 64     * @return Zend_Form_Decorator_Label
 65     */
 66    public function setId($id)
 67    {
 68        $this->setOption('id', $id);
 69        return $this;
 70    }
 71
 72    /**
 73     * Retrieve element ID (used in 'for' attribute)
 74     *
 75     * If none set in decorator, looks first for element 'id' attribute, and
 76     * defaults to element name.
 77     *
 78     * @return string
 79     */
 80    public function getId()
 81    {
 82        $id = $this->getOption('id');
 83        if (null === $id) {
 84            if (null !== ($element = $this->getElement())) {
 85                $id = $element->getId();
 86                $this->setId($id);
 87            }
 88        }
 89
 90        return $id;
 91    }
 92
 93    /**
 94     * Set HTML tag with which to surround label
 95     *
 96     * @param  string $tag
 97     * @return Zend_Form_Decorator_Label
 98     */
 99    public function setTag($tag)
100    {
101        if (empty($tag)) {
102            $this->_tag = null;
103        } else {
104            $this->_tag = (string) $tag;
105        }
106
107        $this->removeOption('tag');
108
109        return $this;
110    }
111
112    /**
113     * Get HTML tag, if any, with which to surround label
114     *
115     * @return void
116     */
117    public function getTag()
118    {
119        if (null === $this->_tag) {
120            $tag = $this->getOption('tag');
121            if (null !== $tag) {
122                $this->removeOption('tag');
123                $this->setTag($tag);
124            }
125            return $tag;
126        }
127
128        return $this->_tag;
129    }
130
131    /**
132     * Get class with which to define label
133     *
134     * Appends either 'optional' or 'required' to class, depending on whether
135     * or not the element is required.
136     *
137     * @return string
138     */
139    public function getClass()
140    {
141        $class   = '';
142        $element = $this->getElement();
143
144        $decoratorClass = $this->getOption('class');
145        if (!empty($decoratorClass)) {
146            $class .= ' ' . $decoratorClass;
147        }
148
149        $type  = $element->isRequired() ? 'required' : 'optional';
150
151        if (!strstr($class, $type)) {
152            $class .= ' ' . $type;
153            $class = trim($class);
154        }
155
156        return $class;
157    }
158
159    /**
160     * Load an optional/required suffix/prefix key
161     *
162     * @param  string $key
163     * @return void
164     */
165    protected function _loadOptReqKey($key)
166    {
167        if (!isset($this->$key)) {
168            $value = $this->getOption($key);
169            $this->$key = (string) $value;
170            if (null !== $value) {
171                $this->removeOption($key);
172            }
173        }
174    }
175
176    /**
177     * Overloading
178     *
179     * Currently overloads:
180     *
181     * - getOpt(ional)Prefix()
182     * - getOpt(ional)Suffix()
183     * - getReq(uired)Prefix()
184     * - getReq(uired)Suffix()
185     * - setOpt(ional)Prefix()
186     * - setOpt(ional)Suffix()
187     * - setReq(uired)Prefix()
188     * - setReq(uired)Suffix()
189     *
190     * @param  string $method
191     * @param  array $args
192     * @return mixed
193     * @throws Zend_Form_Exception for unsupported methods
194     */
195    public function __call($method, $args)
196    {
197        $tail = substr($method, -6);
198        $head = substr($method, 0, 3);
199        if (in_array($head, array('get', 'set'))
200            && (('Prefix' == $tail) || ('Suffix' == $tail))
201        ) {
202            $position = substr($method, -6);
203            $type     = strtolower(substr($method, 3, 3));
204            switch ($type) {
205                case 'req':
206                    $key = 'required' . $position;
207                    break;
208                case 'opt':
209                    $key = 'optional' . $position;
210                    break;
211                default:
212                    require_once 'Zend/Form/Exception.php';
213                    throw new Zend_Form_Exception(sprintf('Invalid method "%s" called in Label decorator, and detected as type %s', $method, $type));
214            }
215
216            switch ($head) {
217                case 'set':
218                    if (0 === count($args)) {
219                        require_once 'Zend/Form/Exception.php';
220                        throw new Zend_Form_Exception(sprintf('Method "%s" requires at least one argument; none provided', $method));
221                    }
222                    $value = array_shift($args);
223                    $this->$key = $value;
224                    return $this;
225                case 'get':
226                default:
227                    if (null === ($element = $this->getElement())) {
228                        $this->_loadOptReqKey($key);
229                    } elseif (isset($element->$key)) {
230                        $this->$key = (string) $element->$key;
231                    } else {
232                        $this->_loadOptReqKey($key);
233                    }
234                    return $this->$key;
235            }
236        }
237
238        require_once 'Zend/Form/Exception.php';
239        throw new Zend_Form_Exception(sprintf('Invalid method "%s" called in Label decorator', $method));
240    }
241
242    /**
243     * Get label to render
244     *
245     * @return void
246     */
247    public function getLabel()
248    {
249        if (null === ($element = $this->getElement())) {
250            return '';
251        }
252
253        $label = $element->getLabel();
254        $label = trim($label);
255
256        if (empty($label)) {
257            return '';
258        }
259
260        if (null !== ($translator = $element->getTranslator())) {
261            $label = $translator->translate($label);
262        }
263
264        $optPrefix = $this->getOptPrefix();
265        $optSuffix = $this->getOptSuffix();
266        $reqPrefix = $this->getReqPrefix();
267        $reqSuffix = $this->getReqSuffix();
268        $separator = $this->getSeparator();
269
270        if (!empty($label)) {
271            if ($element->isRequired()) {
272                $label = $reqPrefix . $label . $reqSuffix;
273            } else {
274                $label = $optPrefix . $label . $optSuffix;
275            }
276        }
277
278        return $label;
279    }
280
281
282    /**
283     * Render a label
284     *
285     * @param  string $content
286     * @return string
287     */
288    public function render($content)
289    {
290        $element = $this->getElement();
291        $view    = $element->getView();
292        if (null === $view) {
293            return $content;
294        }
295
296        $label     = $this->getLabel();
297        $separator = $this->getSeparator();
298        $placement = $this->getPlacement();
299        $tag       = $this->getTag();
300        $id        = $this->getId();
301        $class     = $this->getClass();
302        $options   = $this->getOptions();
303
304
305        if (empty($label) && empty($tag)) {
306            return $content;
307        }
308
309        if (!empty($label)) {
310            $options['class'] = $class;
311            $label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options);
312        } else {
313            $label = '&#160;';
314        }
315
316        if (null !== $tag) {
317            require_once 'Zend/Form/Decorator/HtmlTag.php';
318            $decorator = new Zend_Form_Decorator_HtmlTag();
319            $decorator->setOptions(array('tag' => $tag,
320                                         'id'  => $id . '-label'));
321
322            $label = $decorator->render($label);
323        }
324
325        switch ($placement) {
326            case self::APPEND:
327                return $content . $separator . $label;
328            case self::PREPEND:
329                return $label . $separator . $content;
330        }
331    }
332}