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

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

https://bitbucket.org/ilyabazhenov/speakplace
PHP | 382 lines | 207 code | 42 blank | 133 comment | 39 complexity | 2b181852a3c10ee4c0ac4c8cfc332238 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-2011 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-2011 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 23960 2011-05-03 10:58:52Z yoshida@zend.co.jp $
  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. * Class for the HTML tag with which to surround label
  58. * @var string
  59. */
  60. protected $_tagClass;
  61. /**
  62. * Set element ID
  63. *
  64. * @param string $id
  65. * @return Zend_Form_Decorator_Label
  66. */
  67. public function setId($id)
  68. {
  69. $this->setOption('id', $id);
  70. return $this;
  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. return $id;
  90. }
  91. /**
  92. * Set HTML tag with which to surround label
  93. *
  94. * @param string $tag
  95. * @return Zend_Form_Decorator_Label
  96. */
  97. public function setTag($tag)
  98. {
  99. if (empty($tag)) {
  100. $this->_tag = null;
  101. } else {
  102. $this->_tag = (string) $tag;
  103. }
  104. $this->removeOption('tag');
  105. return $this;
  106. }
  107. /**
  108. * Get HTML tag, if any, with which to surround label
  109. *
  110. * @return void
  111. */
  112. public function getTag()
  113. {
  114. if (null === $this->_tag) {
  115. $tag = $this->getOption('tag');
  116. if (null !== $tag) {
  117. $this->removeOption('tag');
  118. $this->setTag($tag);
  119. }
  120. return $tag;
  121. }
  122. return $this->_tag;
  123. }
  124. /**
  125. * Set the class to apply to the HTML tag with which to surround label
  126. *
  127. * @param string $tagClass
  128. * @return Zend_Form_Decorator_Label
  129. */
  130. public function setTagClass($tagClass)
  131. {
  132. if (empty($tagClass)) {
  133. $this->_tagClass = null;
  134. } else {
  135. $this->_tagClass = (string) $tagClass;
  136. }
  137. $this->removeOption('tagClass');
  138. return $this;
  139. }
  140. /**
  141. * Get the class to apply to the HTML tag, if any, with which to surround label
  142. *
  143. * @return void
  144. */
  145. public function getTagClass()
  146. {
  147. if (null === $this->_tagClass) {
  148. $tagClass = $this->getOption('tagClass');
  149. if (null !== $tagClass) {
  150. $this->removeOption('tagClass');
  151. $this->setTagClass($tagClass);
  152. }
  153. }
  154. return $this->_tagClass;
  155. }
  156. /**
  157. * Get class with which to define label
  158. *
  159. * Appends either 'optional' or 'required' to class, depending on whether
  160. * or not the element is required.
  161. *
  162. * @return string
  163. */
  164. public function getClass()
  165. {
  166. $class = '';
  167. $element = $this->getElement();
  168. $decoratorClass = $this->getOption('class');
  169. if (!empty($decoratorClass)) {
  170. $class .= ' ' . $decoratorClass;
  171. }
  172. $type = $element->isRequired() ? 'required' : 'optional';
  173. if (!strstr($class, $type)) {
  174. $class .= ' ' . $type;
  175. $class = trim($class);
  176. }
  177. return $class;
  178. }
  179. /**
  180. * Load an optional/required suffix/prefix key
  181. *
  182. * @param string $key
  183. * @return void
  184. */
  185. protected function _loadOptReqKey($key)
  186. {
  187. if (!isset($this->$key)) {
  188. $value = $this->getOption($key);
  189. $this->$key = (string) $value;
  190. if (null !== $value) {
  191. $this->removeOption($key);
  192. }
  193. }
  194. }
  195. /**
  196. * Overloading
  197. *
  198. * Currently overloads:
  199. *
  200. * - getOpt(ional)Prefix()
  201. * - getOpt(ional)Suffix()
  202. * - getReq(uired)Prefix()
  203. * - getReq(uired)Suffix()
  204. * - setOpt(ional)Prefix()
  205. * - setOpt(ional)Suffix()
  206. * - setReq(uired)Prefix()
  207. * - setReq(uired)Suffix()
  208. *
  209. * @param string $method
  210. * @param array $args
  211. * @return mixed
  212. * @throws Zend_Form_Exception for unsupported methods
  213. */
  214. public function __call($method, $args)
  215. {
  216. $tail = substr($method, -6);
  217. $head = substr($method, 0, 3);
  218. if (in_array($head, array('get', 'set'))
  219. && (('Prefix' == $tail) || ('Suffix' == $tail))
  220. ) {
  221. $position = substr($method, -6);
  222. $type = strtolower(substr($method, 3, 3));
  223. switch ($type) {
  224. case 'req':
  225. $key = 'required' . $position;
  226. break;
  227. case 'opt':
  228. $key = 'optional' . $position;
  229. break;
  230. default:
  231. require_once 'Zend/Form/Exception.php';
  232. throw new Zend_Form_Exception(sprintf('Invalid method "%s" called in Label decorator, and detected as type %s', $method, $type));
  233. }
  234. switch ($head) {
  235. case 'set':
  236. if (0 === count($args)) {
  237. require_once 'Zend/Form/Exception.php';
  238. throw new Zend_Form_Exception(sprintf('Method "%s" requires at least one argument; none provided', $method));
  239. }
  240. $value = array_shift($args);
  241. $this->$key = $value;
  242. return $this;
  243. case 'get':
  244. default:
  245. if (null === ($element = $this->getElement())) {
  246. $this->_loadOptReqKey($key);
  247. } elseif (isset($element->$key)) {
  248. $this->$key = (string) $element->$key;
  249. } else {
  250. $this->_loadOptReqKey($key);
  251. }
  252. return $this->$key;
  253. }
  254. }
  255. require_once 'Zend/Form/Exception.php';
  256. throw new Zend_Form_Exception(sprintf('Invalid method "%s" called in Label decorator', $method));
  257. }
  258. /**
  259. * Get label to render
  260. *
  261. * @return string
  262. */
  263. public function getLabel()
  264. {
  265. if (null === ($element = $this->getElement())) {
  266. return '';
  267. }
  268. $label = $element->getLabel();
  269. $label = trim($label);
  270. if (empty($label)) {
  271. return '';
  272. }
  273. if (null !== ($translator = $element->getTranslator())) {
  274. $label = $translator->translate($label);
  275. }
  276. $optPrefix = $this->getOptPrefix();
  277. $optSuffix = $this->getOptSuffix();
  278. $reqPrefix = $this->getReqPrefix();
  279. $reqSuffix = $this->getReqSuffix();
  280. $separator = $this->getSeparator();
  281. if (!empty($label)) {
  282. if ($element->isRequired()) {
  283. $label = $reqPrefix . $label . $reqSuffix;
  284. } else {
  285. $label = $optPrefix . $label . $optSuffix;
  286. }
  287. }
  288. return $label;
  289. }
  290. /**
  291. * Render a label
  292. *
  293. * @param string $content
  294. * @return string
  295. */
  296. public function render($content)
  297. {
  298. $element = $this->getElement();
  299. $view = $element->getView();
  300. if (null === $view) {
  301. return $content;
  302. }
  303. $label = $this->getLabel();
  304. $separator = $this->getSeparator();
  305. $placement = $this->getPlacement();
  306. $tag = $this->getTag();
  307. $tagClass = $this->getTagClass();
  308. $id = $this->getId();
  309. $class = $this->getClass();
  310. $options = $this->getOptions();
  311. if (empty($label) && empty($tag)) {
  312. return $content;
  313. }
  314. if (!empty($label)) {
  315. $options['class'] = $class;
  316. $label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options);
  317. } else {
  318. $label = '&#160;';
  319. }
  320. if (null !== $tag) {
  321. require_once 'Zend/Form/Decorator/HtmlTag.php';
  322. $decorator = new Zend_Form_Decorator_HtmlTag();
  323. if (null !== $this->_tagClass) {
  324. $decorator->setOptions(array('tag' => $tag,
  325. 'id' => $id . '-label',
  326. 'class' => $tagClass));
  327. } else {
  328. $decorator->setOptions(array('tag' => $tag,
  329. 'id' => $id . '-label'));
  330. }
  331. $label = $decorator->render($label);
  332. }
  333. switch ($placement) {
  334. case self::APPEND:
  335. return $content . $separator . $label;
  336. case self::PREPEND:
  337. return $label . $separator . $content;
  338. }
  339. }
  340. }