PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/Form/Decorator/Label.php

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