PageRenderTime 78ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/vmihailenco/zf-blog
PHP | 332 lines | 173 code | 41 blank | 118 comment | 33 complexity | ef672907a9eb3f8b03f2d5bdf59b5102 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. /** Zend_Form_Decorator_Abstract */
  22. /**
  23. * Zend_Form_Decorator_Label
  24. *
  25. * Accepts the options:
  26. * - separator: separator to use between label and content (defaults to PHP_EOL)
  27. * - placement: whether to append or prepend label to content (defaults to prepend)
  28. * - tag: if set, used to wrap the label in an additional HTML tag
  29. * - opt(ional)Prefix: a prefix to the label to use when the element is optional
  30. * - opt(iona)lSuffix: a suffix to the label to use when the element is optional
  31. * - req(uired)Prefix: a prefix to the label to use when the element is required
  32. * - req(uired)Suffix: a suffix to the label to use when the element is required
  33. *
  34. * Any other options passed will be used as HTML attributes of the label tag.
  35. *
  36. * @category Zend
  37. * @package Zend_Form
  38. * @subpackage Decorator
  39. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. * @version $Id: Label.php 20096 2010-01-06 02:05:09Z bkarwin $
  42. */
  43. class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract
  44. {
  45. /**
  46. * Default placement: prepend
  47. * @var string
  48. */
  49. protected $_placement = 'PREPEND';
  50. /**
  51. * HTML tag with which to surround label
  52. * @var string
  53. */
  54. protected $_tag;
  55. /**
  56. * Set element ID
  57. *
  58. * @param string $id
  59. * @return Zend_Form_Decorator_Label
  60. */
  61. public function setId($id)
  62. {
  63. $this->setOption('id', $id);
  64. return $this;
  65. }
  66. /**
  67. * Retrieve element ID (used in 'for' attribute)
  68. *
  69. * If none set in decorator, looks first for element 'id' attribute, and
  70. * defaults to element name.
  71. *
  72. * @return string
  73. */
  74. public function getId()
  75. {
  76. $id = $this->getOption('id');
  77. if (null === $id) {
  78. if (null !== ($element = $this->getElement())) {
  79. $id = $element->getId();
  80. $this->setId($id);
  81. }
  82. }
  83. return $id;
  84. }
  85. /**
  86. * Set HTML tag with which to surround label
  87. *
  88. * @param string $tag
  89. * @return Zend_Form_Decorator_Label
  90. */
  91. public function setTag($tag)
  92. {
  93. if (empty($tag)) {
  94. $this->_tag = null;
  95. } else {
  96. $this->_tag = (string) $tag;
  97. }
  98. $this->removeOption('tag');
  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. throw new Zend_Form_Exception(sprintf('Invalid method "%s" called in Label decorator, and detected as type %s', $method, $type));
  194. }
  195. switch ($head) {
  196. case 'set':
  197. if (0 === count($args)) {
  198. throw new Zend_Form_Exception(sprintf('Method "%s" requires at least one argument; none provided', $method));
  199. }
  200. $value = array_shift($args);
  201. $this->$key = $value;
  202. return $this;
  203. case 'get':
  204. default:
  205. if (null === ($element = $this->getElement())) {
  206. $this->_loadOptReqKey($key);
  207. } elseif (isset($element->$key)) {
  208. $this->$key = (string) $element->$key;
  209. } else {
  210. $this->_loadOptReqKey($key);
  211. }
  212. return $this->$key;
  213. }
  214. }
  215. throw new Zend_Form_Exception(sprintf('Invalid method "%s" called in Label decorator', $method));
  216. }
  217. /**
  218. * Get label to render
  219. *
  220. * @return void
  221. */
  222. public function getLabel()
  223. {
  224. if (null === ($element = $this->getElement())) {
  225. return '';
  226. }
  227. $label = $element->getLabel();
  228. $label = trim($label);
  229. if (empty($label)) {
  230. return '';
  231. }
  232. if (null !== ($translator = $element->getTranslator())) {
  233. $label = $translator->translate($label);
  234. }
  235. $optPrefix = $this->getOptPrefix();
  236. $optSuffix = $this->getOptSuffix();
  237. $reqPrefix = $this->getReqPrefix();
  238. $reqSuffix = $this->getReqSuffix();
  239. $separator = $this->getSeparator();
  240. if (!empty($label)) {
  241. if ($element->isRequired()) {
  242. $label = $reqPrefix . $label . $reqSuffix;
  243. } else {
  244. $label = $optPrefix . $label . $optSuffix;
  245. }
  246. }
  247. return $label;
  248. }
  249. /**
  250. * Render a label
  251. *
  252. * @param string $content
  253. * @return string
  254. */
  255. public function render($content)
  256. {
  257. $element = $this->getElement();
  258. $view = $element->getView();
  259. if (null === $view) {
  260. return $content;
  261. }
  262. $label = $this->getLabel();
  263. $separator = $this->getSeparator();
  264. $placement = $this->getPlacement();
  265. $tag = $this->getTag();
  266. $id = $this->getId();
  267. $class = $this->getClass();
  268. $options = $this->getOptions();
  269. if (empty($label) && empty($tag)) {
  270. return $content;
  271. }
  272. if (!empty($label)) {
  273. $options['class'] = $class;
  274. $label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options);
  275. } else {
  276. $label = '&nbsp;';
  277. }
  278. if (null !== $tag) {
  279. $decorator = new Zend_Form_Decorator_HtmlTag();
  280. $decorator->setOptions(array('tag' => $tag,
  281. 'id' => $this->getElement()->getName() . '-label'));
  282. $label = $decorator->render($label);
  283. }
  284. switch ($placement) {
  285. case self::APPEND:
  286. return $content . $separator . $label;
  287. case self::PREPEND:
  288. return $label . $separator . $content;
  289. }
  290. }
  291. }