PageRenderTime 38ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/sunil_nextbits/magento2
PHP | 332 lines | 173 code | 36 blank | 123 comment | 33 complexity | 9b9126faa6e017c9d631650d4a32fc2e 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. #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-2010 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 22128 2010-05-06 11:18:02Z alab $
  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. $this->removeOption('tag');
  100. return $this;
  101. }
  102. /**
  103. * Get HTML tag, if any, with which to surround label
  104. *
  105. * @return void
  106. */
  107. public function getTag()
  108. {
  109. if (null === $this->_tag) {
  110. $tag = $this->getOption('tag');
  111. if (null !== $tag) {
  112. $this->removeOption('tag');
  113. $this->setTag($tag);
  114. }
  115. return $tag;
  116. }
  117. return $this->_tag;
  118. }
  119. /**
  120. * Get class with which to define label
  121. *
  122. * Appends either 'optional' or 'required' to class, depending on whether
  123. * or not the element is required.
  124. *
  125. * @return string
  126. */
  127. public function getClass()
  128. {
  129. $class = '';
  130. $element = $this->getElement();
  131. $decoratorClass = $this->getOption('class');
  132. if (!empty($decoratorClass)) {
  133. $class .= ' ' . $decoratorClass;
  134. }
  135. $type = $element->isRequired() ? 'required' : 'optional';
  136. if (!strstr($class, $type)) {
  137. $class .= ' ' . $type;
  138. $class = trim($class);
  139. }
  140. return $class;
  141. }
  142. /**
  143. * Load an optional/required suffix/prefix key
  144. *
  145. * @param string $key
  146. * @return void
  147. */
  148. protected function _loadOptReqKey($key)
  149. {
  150. if (!isset($this->$key)) {
  151. $value = $this->getOption($key);
  152. $this->$key = (string) $value;
  153. if (null !== $value) {
  154. $this->removeOption($key);
  155. }
  156. }
  157. }
  158. /**
  159. * Overloading
  160. *
  161. * Currently overloads:
  162. *
  163. * - getOpt(ional)Prefix()
  164. * - getOpt(ional)Suffix()
  165. * - getReq(uired)Prefix()
  166. * - getReq(uired)Suffix()
  167. * - setOpt(ional)Prefix()
  168. * - setOpt(ional)Suffix()
  169. * - setReq(uired)Prefix()
  170. * - setReq(uired)Suffix()
  171. *
  172. * @param string $method
  173. * @param array $args
  174. * @return mixed
  175. * @throws Zend_Form_Exception for unsupported methods
  176. */
  177. public function __call($method, $args)
  178. {
  179. $tail = substr($method, -6);
  180. $head = substr($method, 0, 3);
  181. if (in_array($head, array('get', 'set'))
  182. && (('Prefix' == $tail) || ('Suffix' == $tail))
  183. ) {
  184. $position = substr($method, -6);
  185. $type = strtolower(substr($method, 3, 3));
  186. switch ($type) {
  187. case 'req':
  188. $key = 'required' . $position;
  189. break;
  190. case 'opt':
  191. $key = 'optional' . $position;
  192. break;
  193. default:
  194. #require_once 'Zend/Form/Exception.php';
  195. throw new Zend_Form_Exception(sprintf('Invalid method "%s" called in Label decorator, and detected as type %s', $method, $type));
  196. }
  197. switch ($head) {
  198. case 'set':
  199. if (0 === count($args)) {
  200. #require_once 'Zend/Form/Exception.php';
  201. throw new Zend_Form_Exception(sprintf('Method "%s" requires at least one argument; none provided', $method));
  202. }
  203. $value = array_shift($args);
  204. $this->$key = $value;
  205. return $this;
  206. case 'get':
  207. default:
  208. if (null === ($element = $this->getElement())) {
  209. $this->_loadOptReqKey($key);
  210. } elseif (isset($element->$key)) {
  211. $this->$key = (string) $element->$key;
  212. } else {
  213. $this->_loadOptReqKey($key);
  214. }
  215. return $this->$key;
  216. }
  217. }
  218. #require_once 'Zend/Form/Exception.php';
  219. throw new Zend_Form_Exception(sprintf('Invalid method "%s" called in Label decorator', $method));
  220. }
  221. /**
  222. * Get label to render
  223. *
  224. * @return void
  225. */
  226. public function getLabel()
  227. {
  228. if (null === ($element = $this->getElement())) {
  229. return '';
  230. }
  231. $label = $element->getLabel();
  232. $label = trim($label);
  233. if (empty($label)) {
  234. return '';
  235. }
  236. if (null !== ($translator = $element->getTranslator())) {
  237. $label = $translator->translate($label);
  238. }
  239. $optPrefix = $this->getOptPrefix();
  240. $optSuffix = $this->getOptSuffix();
  241. $reqPrefix = $this->getReqPrefix();
  242. $reqSuffix = $this->getReqSuffix();
  243. $separator = $this->getSeparator();
  244. if (!empty($label)) {
  245. if ($element->isRequired()) {
  246. $label = $reqPrefix . $label . $reqSuffix;
  247. } else {
  248. $label = $optPrefix . $label . $optSuffix;
  249. }
  250. }
  251. return $label;
  252. }
  253. /**
  254. * Render a label
  255. *
  256. * @param string $content
  257. * @return string
  258. */
  259. public function render($content)
  260. {
  261. $element = $this->getElement();
  262. $view = $element->getView();
  263. if (null === $view) {
  264. return $content;
  265. }
  266. $label = $this->getLabel();
  267. $separator = $this->getSeparator();
  268. $placement = $this->getPlacement();
  269. $tag = $this->getTag();
  270. $id = $this->getId();
  271. $class = $this->getClass();
  272. $options = $this->getOptions();
  273. if (empty($label) && empty($tag)) {
  274. return $content;
  275. }
  276. if (!empty($label)) {
  277. $options['class'] = $class;
  278. $label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options);
  279. } else {
  280. $label = '&#160;';
  281. }
  282. if (null !== $tag) {
  283. #require_once 'Zend/Form/Decorator/HtmlTag.php';
  284. $decorator = new Zend_Form_Decorator_HtmlTag();
  285. $decorator->setOptions(array('tag' => $tag,
  286. 'id' => $id . '-label'));
  287. $label = $decorator->render($label);
  288. }
  289. switch ($placement) {
  290. case self::APPEND:
  291. return $content . $separator . $label;
  292. case self::PREPEND:
  293. return $label . $separator . $content;
  294. }
  295. }
  296. }