PageRenderTime 62ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/zendframework/zend-form/src/View/Helper/FormRow.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 442 lines | 238 code | 61 blank | 143 comment | 51 complexity | 50abacf5e4d330eb3dd429ba361e1295 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Form\View\Helper;
  10. use Zend\Form\Element\Button;
  11. use Zend\Form\Element\MonthSelect;
  12. use Zend\Form\Element\Captcha;
  13. use Zend\Form\ElementInterface;
  14. use Zend\Form\Exception;
  15. use Zend\Form\LabelAwareInterface;
  16. class FormRow extends AbstractHelper
  17. {
  18. const LABEL_APPEND = 'append';
  19. const LABEL_PREPEND = 'prepend';
  20. /**
  21. * The class that is added to element that have errors
  22. *
  23. * @var string
  24. */
  25. protected $inputErrorClass = 'input-error';
  26. /**
  27. * The attributes for the row label
  28. *
  29. * @var array
  30. */
  31. protected $labelAttributes;
  32. /**
  33. * Where will be label rendered?
  34. *
  35. * @var string
  36. */
  37. protected $labelPosition = self::LABEL_PREPEND;
  38. /**
  39. * Are the errors are rendered by this helper?
  40. *
  41. * @var bool
  42. */
  43. protected $renderErrors = true;
  44. /**
  45. * Form label helper instance
  46. *
  47. * @var FormLabel
  48. */
  49. protected $labelHelper;
  50. /**
  51. * Form element helper instance
  52. *
  53. * @var FormElement
  54. */
  55. protected $elementHelper;
  56. /**
  57. * Form element errors helper instance
  58. *
  59. * @var FormElementErrors
  60. */
  61. protected $elementErrorsHelper;
  62. /**
  63. * @var string
  64. */
  65. protected $partial;
  66. /**
  67. * Invoke helper as functor
  68. *
  69. * Proxies to {@link render()}.
  70. *
  71. * @param null|ElementInterface $element
  72. * @param null|string $labelPosition
  73. * @param bool $renderErrors
  74. * @param string|null $partial
  75. * @return string|FormRow
  76. */
  77. public function __invoke(ElementInterface $element = null, $labelPosition = null, $renderErrors = null, $partial = null)
  78. {
  79. if (!$element) {
  80. return $this;
  81. }
  82. if (is_null($labelPosition)) {
  83. $labelPosition = $this->getLabelPosition();
  84. }
  85. if ($renderErrors !== null) {
  86. $this->setRenderErrors($renderErrors);
  87. }
  88. if ($partial !== null) {
  89. $this->setPartial($partial);
  90. }
  91. return $this->render($element, $labelPosition);
  92. }
  93. /**
  94. * Utility form helper that renders a label (if it exists), an element and errors
  95. *
  96. * @param ElementInterface $element
  97. * @param null|string $labelPosition
  98. * @throws \Zend\Form\Exception\DomainException
  99. * @return string
  100. */
  101. public function render(ElementInterface $element, $labelPosition = null)
  102. {
  103. $escapeHtmlHelper = $this->getEscapeHtmlHelper();
  104. $labelHelper = $this->getLabelHelper();
  105. $elementHelper = $this->getElementHelper();
  106. $elementErrorsHelper = $this->getElementErrorsHelper();
  107. $label = $element->getLabel();
  108. $inputErrorClass = $this->getInputErrorClass();
  109. if (is_null($labelPosition)) {
  110. $labelPosition = $this->labelPosition;
  111. }
  112. if (isset($label) && '' !== $label) {
  113. // Translate the label
  114. if (null !== ($translator = $this->getTranslator())) {
  115. $label = $translator->translate($label, $this->getTranslatorTextDomain());
  116. }
  117. }
  118. // Does this element have errors ?
  119. if (count($element->getMessages()) > 0 && !empty($inputErrorClass)) {
  120. $classAttributes = ($element->hasAttribute('class') ? $element->getAttribute('class') . ' ' : '');
  121. $classAttributes = $classAttributes . $inputErrorClass;
  122. $element->setAttribute('class', $classAttributes);
  123. }
  124. if ($this->partial) {
  125. $vars = array(
  126. 'element' => $element,
  127. 'label' => $label,
  128. 'labelAttributes' => $this->labelAttributes,
  129. 'labelPosition' => $labelPosition,
  130. 'renderErrors' => $this->renderErrors,
  131. );
  132. return $this->view->render($this->partial, $vars);
  133. }
  134. if ($this->renderErrors) {
  135. $elementErrors = $elementErrorsHelper->render($element);
  136. }
  137. $elementString = $elementHelper->render($element);
  138. // hidden elements do not need a <label> -https://github.com/zendframework/zf2/issues/5607
  139. $type = $element->getAttribute('type');
  140. if (isset($label) && '' !== $label && $type !== 'hidden') {
  141. $labelAttributes = array();
  142. if ($element instanceof LabelAwareInterface) {
  143. $labelAttributes = $element->getLabelAttributes();
  144. }
  145. if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) {
  146. $label = $escapeHtmlHelper($label);
  147. }
  148. if (empty($labelAttributes)) {
  149. $labelAttributes = $this->labelAttributes;
  150. }
  151. // Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
  152. // labels. The semantic way is to group them inside a fieldset
  153. if ($type === 'multi_checkbox'
  154. || $type === 'radio'
  155. || $element instanceof MonthSelect
  156. || $element instanceof Captcha
  157. ) {
  158. $markup = sprintf(
  159. '<fieldset><legend>%s</legend>%s</fieldset>',
  160. $label,
  161. $elementString
  162. );
  163. } else {
  164. // Ensure element and label will be separated if element has an `id`-attribute.
  165. // If element has label option `always_wrap` it will be nested in any case.
  166. if ($element->hasAttribute('id')
  167. && ($element instanceof LabelAwareInterface && !$element->getLabelOption('always_wrap'))
  168. ) {
  169. $labelOpen = '';
  170. $labelClose = '';
  171. $label = $labelHelper($element);
  172. } else {
  173. $labelOpen = $labelHelper->openTag($labelAttributes);
  174. $labelClose = $labelHelper->closeTag();
  175. }
  176. if ($label !== '' && (!$element->hasAttribute('id'))
  177. || ($element instanceof LabelAwareInterface && $element->getLabelOption('always_wrap'))
  178. ) {
  179. $label = '<span>' . $label . '</span>';
  180. }
  181. // Button element is a special case, because label is always rendered inside it
  182. if ($element instanceof Button) {
  183. $labelOpen = $labelClose = $label = '';
  184. }
  185. if ($element instanceof LabelAwareInterface && $element->getLabelOption('label_position')) {
  186. $labelPosition = $element->getLabelOption('label_position');
  187. }
  188. switch ($labelPosition) {
  189. case self::LABEL_PREPEND:
  190. $markup = $labelOpen . $label . $elementString . $labelClose;
  191. break;
  192. case self::LABEL_APPEND:
  193. default:
  194. $markup = $labelOpen . $elementString . $label . $labelClose;
  195. break;
  196. }
  197. }
  198. if ($this->renderErrors) {
  199. $markup .= $elementErrors;
  200. }
  201. } else {
  202. if ($this->renderErrors) {
  203. $markup = $elementString . $elementErrors;
  204. } else {
  205. $markup = $elementString;
  206. }
  207. }
  208. return $markup;
  209. }
  210. /**
  211. * Set the class that is added to element that have errors
  212. *
  213. * @param string $inputErrorClass
  214. * @return FormRow
  215. */
  216. public function setInputErrorClass($inputErrorClass)
  217. {
  218. $this->inputErrorClass = $inputErrorClass;
  219. return $this;
  220. }
  221. /**
  222. * Get the class that is added to element that have errors
  223. *
  224. * @return string
  225. */
  226. public function getInputErrorClass()
  227. {
  228. return $this->inputErrorClass;
  229. }
  230. /**
  231. * Set the attributes for the row label
  232. *
  233. * @param array $labelAttributes
  234. * @return FormRow
  235. */
  236. public function setLabelAttributes($labelAttributes)
  237. {
  238. $this->labelAttributes = $labelAttributes;
  239. return $this;
  240. }
  241. /**
  242. * Get the attributes for the row label
  243. *
  244. * @return array
  245. */
  246. public function getLabelAttributes()
  247. {
  248. return $this->labelAttributes;
  249. }
  250. /**
  251. * Set the label position
  252. *
  253. * @param string $labelPosition
  254. * @throws \Zend\Form\Exception\InvalidArgumentException
  255. * @return FormRow
  256. */
  257. public function setLabelPosition($labelPosition)
  258. {
  259. $labelPosition = strtolower($labelPosition);
  260. if (!in_array($labelPosition, array(self::LABEL_APPEND, self::LABEL_PREPEND))) {
  261. throw new Exception\InvalidArgumentException(sprintf(
  262. '%s expects either %s::LABEL_APPEND or %s::LABEL_PREPEND; received "%s"',
  263. __METHOD__,
  264. __CLASS__,
  265. __CLASS__,
  266. (string) $labelPosition
  267. ));
  268. }
  269. $this->labelPosition = $labelPosition;
  270. return $this;
  271. }
  272. /**
  273. * Get the label position
  274. *
  275. * @return string
  276. */
  277. public function getLabelPosition()
  278. {
  279. return $this->labelPosition;
  280. }
  281. /**
  282. * Set if the errors are rendered by this helper
  283. *
  284. * @param bool $renderErrors
  285. * @return FormRow
  286. */
  287. public function setRenderErrors($renderErrors)
  288. {
  289. $this->renderErrors = (bool) $renderErrors;
  290. return $this;
  291. }
  292. /**
  293. * Retrieve if the errors are rendered by this helper
  294. *
  295. * @return bool
  296. */
  297. public function getRenderErrors()
  298. {
  299. return $this->renderErrors;
  300. }
  301. /**
  302. * Set a partial view script to use for rendering the row
  303. *
  304. * @param null|string $partial
  305. * @return FormRow
  306. */
  307. public function setPartial($partial)
  308. {
  309. $this->partial = $partial;
  310. return $this;
  311. }
  312. /**
  313. * Retrieve current partial
  314. *
  315. * @return null|string
  316. */
  317. public function getPartial()
  318. {
  319. return $this->partial;
  320. }
  321. /**
  322. * Retrieve the FormLabel helper
  323. *
  324. * @return FormLabel
  325. */
  326. protected function getLabelHelper()
  327. {
  328. if ($this->labelHelper) {
  329. return $this->labelHelper;
  330. }
  331. if (method_exists($this->view, 'plugin')) {
  332. $this->labelHelper = $this->view->plugin('form_label');
  333. }
  334. if (!$this->labelHelper instanceof FormLabel) {
  335. $this->labelHelper = new FormLabel();
  336. }
  337. if ($this->hasTranslator()) {
  338. $this->labelHelper->setTranslator(
  339. $this->getTranslator(),
  340. $this->getTranslatorTextDomain()
  341. );
  342. }
  343. return $this->labelHelper;
  344. }
  345. /**
  346. * Retrieve the FormElement helper
  347. *
  348. * @return FormElement
  349. */
  350. protected function getElementHelper()
  351. {
  352. if ($this->elementHelper) {
  353. return $this->elementHelper;
  354. }
  355. if (method_exists($this->view, 'plugin')) {
  356. $this->elementHelper = $this->view->plugin('form_element');
  357. }
  358. if (!$this->elementHelper instanceof FormElement) {
  359. $this->elementHelper = new FormElement();
  360. }
  361. return $this->elementHelper;
  362. }
  363. /**
  364. * Retrieve the FormElementErrors helper
  365. *
  366. * @return FormElementErrors
  367. */
  368. protected function getElementErrorsHelper()
  369. {
  370. if ($this->elementErrorsHelper) {
  371. return $this->elementErrorsHelper;
  372. }
  373. if (method_exists($this->view, 'plugin')) {
  374. $this->elementErrorsHelper = $this->view->plugin('form_element_errors');
  375. }
  376. if (!$this->elementErrorsHelper instanceof FormElementErrors) {
  377. $this->elementErrorsHelper = new FormElementErrors();
  378. }
  379. return $this->elementErrorsHelper;
  380. }
  381. }