PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Zend/Form/Decorator/Label.php

https://bitbucket.org/simukti/zf1
PHP | 470 lines | 267 code | 56 blank | 147 comment | 42 complexity | 9d5d4042a9432b0538556cf36b553b24 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-2012 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. * - tagClass: if tag option is set, used to add a class to the label wrapper
  31. * - opt(ional)Prefix: a prefix to the label to use when the element is optional
  32. * - opt(ional)Suffix: a suffix to the label to use when the element is optional
  33. * - req(uired)Prefix: a prefix to the label to use when the element is required
  34. * - req(uired)Suffix: a suffix to the label to use when the element is required
  35. *
  36. * Any other options passed will be used as HTML attributes of the label tag.
  37. *
  38. * @category Zend
  39. * @package Zend_Form
  40. * @subpackage Decorator
  41. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. * @version $Id: Label.php 24846 2012-05-31 19:12:45Z rob $
  44. */
  45. class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract
  46. {
  47. /**
  48. * Placement constants
  49. */
  50. const IMPLICIT = 'IMPLICIT';
  51. const IMPLICIT_PREPEND = 'IMPLICIT_PREPEND';
  52. const IMPLICIT_APPEND = 'IMPLICIT_APPEND';
  53. /**
  54. * Default placement: prepend
  55. * @var string
  56. */
  57. protected $_placement = 'PREPEND';
  58. /**
  59. * HTML tag with which to surround label
  60. * @var string
  61. */
  62. protected $_tag;
  63. /**
  64. * Class for the HTML tag with which to surround label
  65. * @var string
  66. */
  67. protected $_tagClass;
  68. /**
  69. * Set element ID
  70. *
  71. * @param string $id
  72. * @return Zend_Form_Decorator_Label
  73. */
  74. public function setId($id)
  75. {
  76. $this->setOption('id', $id);
  77. return $this;
  78. }
  79. /**
  80. * Retrieve element ID (used in 'for' attribute)
  81. *
  82. * If none set in decorator, looks first for element 'id' attribute, and
  83. * defaults to element name.
  84. *
  85. * @return string
  86. */
  87. public function getId()
  88. {
  89. $id = $this->getOption('id');
  90. if (null === $id) {
  91. if (null !== ($element = $this->getElement())) {
  92. $id = $element->getId();
  93. $this->setId($id);
  94. }
  95. }
  96. return $id;
  97. }
  98. /**
  99. * Set HTML tag with which to surround label
  100. *
  101. * @param string $tag
  102. * @return Zend_Form_Decorator_Label
  103. */
  104. public function setTag($tag)
  105. {
  106. if (empty($tag)) {
  107. $this->_tag = null;
  108. } else {
  109. $this->_tag = (string) $tag;
  110. }
  111. $this->removeOption('tag');
  112. return $this;
  113. }
  114. /**
  115. * Get HTML tag, if any, with which to surround label
  116. *
  117. * @return void
  118. */
  119. public function getTag()
  120. {
  121. if (null === $this->_tag) {
  122. $tag = $this->getOption('tag');
  123. if (null !== $tag) {
  124. $this->removeOption('tag');
  125. $this->setTag($tag);
  126. }
  127. return $tag;
  128. }
  129. return $this->_tag;
  130. }
  131. /**
  132. * Set the class to apply to the HTML tag with which to surround label
  133. *
  134. * @param string $tagClass
  135. * @return Zend_Form_Decorator_Label
  136. */
  137. public function setTagClass($tagClass)
  138. {
  139. if (empty($tagClass)) {
  140. $this->_tagClass = null;
  141. } else {
  142. $this->_tagClass = (string) $tagClass;
  143. }
  144. $this->removeOption('tagClass');
  145. return $this;
  146. }
  147. /**
  148. * Get the class to apply to the HTML tag, if any, with which to surround label
  149. *
  150. * @return void
  151. */
  152. public function getTagClass()
  153. {
  154. if (null === $this->_tagClass) {
  155. $tagClass = $this->getOption('tagClass');
  156. if (null !== $tagClass) {
  157. $this->removeOption('tagClass');
  158. $this->setTagClass($tagClass);
  159. }
  160. }
  161. return $this->_tagClass;
  162. }
  163. /**
  164. * Get class with which to define label
  165. *
  166. * Appends either 'optional' or 'required' to class, depending on whether
  167. * or not the element is required.
  168. *
  169. * @return string
  170. */
  171. public function getClass()
  172. {
  173. $class = '';
  174. $element = $this->getElement();
  175. $decoratorClass = $this->getOption('class');
  176. if (!empty($decoratorClass)) {
  177. $class .= ' ' . $decoratorClass;
  178. }
  179. $type = $element->isRequired() ? 'required' : 'optional';
  180. if (!strstr($class, $type)) {
  181. $class .= ' ' . $type;
  182. $class = trim($class);
  183. }
  184. return $class;
  185. }
  186. /**
  187. * Load an optional/required suffix/prefix key
  188. *
  189. * @param string $key
  190. * @return void
  191. */
  192. protected function _loadOptReqKey($key)
  193. {
  194. if (!isset($this->$key)) {
  195. $value = $this->getOption($key);
  196. $this->$key = (string) $value;
  197. if (null !== $value) {
  198. $this->removeOption($key);
  199. }
  200. }
  201. }
  202. /**
  203. * Overloading
  204. *
  205. * Currently overloads:
  206. *
  207. * - getOpt(ional)Prefix()
  208. * - getOpt(ional)Suffix()
  209. * - getReq(uired)Prefix()
  210. * - getReq(uired)Suffix()
  211. * - setOpt(ional)Prefix()
  212. * - setOpt(ional)Suffix()
  213. * - setReq(uired)Prefix()
  214. * - setReq(uired)Suffix()
  215. *
  216. * @param string $method
  217. * @param array $args
  218. * @return mixed
  219. * @throws Zend_Form_Exception for unsupported methods
  220. */
  221. public function __call($method, $args)
  222. {
  223. $tail = substr($method, -6);
  224. $head = substr($method, 0, 3);
  225. if (in_array($head, array('get', 'set'))
  226. && (('Prefix' == $tail) || ('Suffix' == $tail))
  227. ) {
  228. $position = substr($method, -6);
  229. $type = strtolower(substr($method, 3, 3));
  230. switch ($type) {
  231. case 'req':
  232. $key = 'required' . $position;
  233. break;
  234. case 'opt':
  235. $key = 'optional' . $position;
  236. break;
  237. default:
  238. require_once 'Zend/Form/Exception.php';
  239. throw new Zend_Form_Exception(sprintf('Invalid method "%s" called in Label decorator, and detected as type %s', $method, $type));
  240. }
  241. switch ($head) {
  242. case 'set':
  243. if (0 === count($args)) {
  244. require_once 'Zend/Form/Exception.php';
  245. throw new Zend_Form_Exception(sprintf('Method "%s" requires at least one argument; none provided', $method));
  246. }
  247. $value = array_shift($args);
  248. $this->$key = $value;
  249. return $this;
  250. case 'get':
  251. default:
  252. if (null === ($element = $this->getElement())) {
  253. $this->_loadOptReqKey($key);
  254. } elseif (isset($element->$key)) {
  255. $this->$key = (string) $element->$key;
  256. } else {
  257. $this->_loadOptReqKey($key);
  258. }
  259. return $this->$key;
  260. }
  261. }
  262. require_once 'Zend/Form/Exception.php';
  263. throw new Zend_Form_Exception(sprintf('Invalid method "%s" called in Label decorator', $method));
  264. }
  265. /**
  266. * Get label to render
  267. *
  268. * @return string
  269. */
  270. public function getLabel()
  271. {
  272. if (null === ($element = $this->getElement())) {
  273. return '';
  274. }
  275. $label = $element->getLabel();
  276. $label = trim($label);
  277. if (empty($label)) {
  278. return '';
  279. }
  280. if (null !== ($translator = $element->getTranslator())) {
  281. $label = $translator->translate($label);
  282. }
  283. $optPrefix = $this->getOptPrefix();
  284. $optSuffix = $this->getOptSuffix();
  285. $reqPrefix = $this->getReqPrefix();
  286. $reqSuffix = $this->getReqSuffix();
  287. $separator = $this->getSeparator();
  288. if (!empty($label)) {
  289. if ($element->isRequired()) {
  290. $label = $reqPrefix . $label . $reqSuffix;
  291. } else {
  292. $label = $optPrefix . $label . $optSuffix;
  293. }
  294. }
  295. return $label;
  296. }
  297. /**
  298. * Determine if label should append, prepend or implicit content
  299. *
  300. * @return string
  301. */
  302. public function getPlacement()
  303. {
  304. $placement = $this->_placement;
  305. if (null !== ($placementOpt = $this->getOption('placement'))) {
  306. $placementOpt = strtoupper($placementOpt);
  307. switch ($placementOpt) {
  308. case self::APPEND:
  309. case self::PREPEND:
  310. case self::IMPLICIT:
  311. case self::IMPLICIT_PREPEND:
  312. case self::IMPLICIT_APPEND:
  313. $placement = $this->_placement = $placementOpt;
  314. break;
  315. case false:
  316. $placement = $this->_placement = null;
  317. break;
  318. default:
  319. break;
  320. }
  321. $this->removeOption('placement');
  322. }
  323. return $placement;
  324. }
  325. /**
  326. * Render a label
  327. *
  328. * @param string $content
  329. * @return string
  330. */
  331. public function render($content)
  332. {
  333. $element = $this->getElement();
  334. $view = $element->getView();
  335. if (null === $view) {
  336. return $content;
  337. }
  338. $label = $this->getLabel();
  339. $separator = $this->getSeparator();
  340. $placement = $this->getPlacement();
  341. $tag = $this->getTag();
  342. $tagClass = $this->getTagClass();
  343. $id = $this->getId();
  344. $class = $this->getClass();
  345. $options = $this->getOptions();
  346. if (empty($label) && empty($tag)) {
  347. return $content;
  348. }
  349. if (!empty($label)) {
  350. $options['class'] = $class;
  351. $label = trim($label);
  352. switch ($placement) {
  353. case self::IMPLICIT:
  354. // Break was intentionally omitted
  355. case self::IMPLICIT_PREPEND:
  356. $options['escape'] = false;
  357. $options['disableFor'] = true;
  358. $label = $view->formLabel(
  359. $element->getFullyQualifiedName(),
  360. $label . $separator . $content,
  361. $options
  362. );
  363. break;
  364. case self::IMPLICIT_APPEND:
  365. $options['escape'] = false;
  366. $options['disableFor'] = true;
  367. $label = $view->formLabel(
  368. $element->getFullyQualifiedName(),
  369. $content . $separator . $label,
  370. $options
  371. );
  372. break;
  373. case self::APPEND:
  374. // Break was intentionally omitted
  375. case self::PREPEND:
  376. // Break was intentionally omitted
  377. default:
  378. $label = $view->formLabel(
  379. $element->getFullyQualifiedName(),
  380. $label,
  381. $options
  382. );
  383. break;
  384. }
  385. } else {
  386. $label = '&#160;';
  387. }
  388. if (null !== $tag) {
  389. require_once 'Zend/Form/Decorator/HtmlTag.php';
  390. $decorator = new Zend_Form_Decorator_HtmlTag();
  391. if (null !== $this->_tagClass) {
  392. $decorator->setOptions(array('tag' => $tag,
  393. 'id' => $id . '-label',
  394. 'class' => $tagClass));
  395. } else {
  396. $decorator->setOptions(array('tag' => $tag,
  397. 'id' => $id . '-label'));
  398. }
  399. $label = $decorator->render($label);
  400. }
  401. switch ($placement) {
  402. case self::APPEND:
  403. return $content . $separator . $label;
  404. case self::PREPEND:
  405. return $label . $separator . $content;
  406. case self::IMPLICIT:
  407. // Break was intentionally omitted
  408. case self::IMPLICIT_PREPEND:
  409. // Break was intentionally omitted
  410. case self::IMPLICIT_APPEND:
  411. return $label;
  412. }
  413. }
  414. }