Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Cadastro\View\Helper;
- use Zend\View\Helper\AbstractHelper;
- class FormFormat extends AbstractHelper
- {
- /**
- *
- * @var AbstractHelper
- */
- protected $peeringHelper;
- /**
- *
- * @var boolean
- */
- protected $isView;
- /**
- * Invoke helper as functor
- *
- * Proxies to {@link render()}.
- *
- * @param ElementInterface|null $element
- * @param AbstractHelper|null $render
- * @return string FormInput
- */
- public function __invoke($element = null, $peeringHelper = null, $class = false)
- {
- if (! $element) {
- return $this;
- }
- if ($peeringHelper) {
- $this->peeringHelper = $peeringHelper;
- }
- if ($element instanceof \Zend\Form\Element\Collection)
- return $this->renderCollection($element);
- return $this->renderElement($element, $class);
- }
- /**
- * Render unique fields
- *
- * @param unknown $element
- * @param unknown $class
- * @return string
- */
- public function renderElement($element)
- {
- $return = '';
- if (count($element->getMessages()) > 0) {
- $return .= '<div class="form-group has-error">';
- } else {
- $return .= '<div class="form-group">';
- }
- $label = ($this->isView) ? '<strong>' . $element->getLabel() . '</strong>' : $element->getLabel();
- $return .= '<label class="col-md-3 control-label">' . $label . '</label>';
- $return .= '<div class="col-md-4">';
- $return .= $this->renderInput($element);
- foreach ($element->getMessages() as $mensagem) {
- $return .= "<span class='help-block'>" . $mensagem . "</span>";
- }
- $return .= '</div>';
- $return .= '</div>';
- return $return;
- }
- /**
- * Render a form collection
- *
- * @param unknown $element
- * @return string
- */
- public function renderCollection($element)
- {
- $return = '';
- if (count($element->getMessages()) > 0) {
- $return .= '<div class="form-group has-error">';
- } else {
- $return .= '<div class="form-group">';
- }
- $label = ($this->isView) ? '<strong>' . $element->getLabel() . '</strong>' : $element->getLabel();
- $return .= '<label class="col-md-3 control-label">' . $label . '</label>';
- $return .= '<div class="col-md-6">';
- $element->setLabel('');
- $return .= $this->getView()->formrow($element);
- foreach ($element->getMessages() as $mensagem) {
- //$return .= "<span class='help-block'>" . $mensagem . "</span>";
- }
- $return .= '</div>';
- $return .= '</div>';
- return $return;
- }
- /**
- *
- * @param unknown $element
- */
- public function renderInput($element)
- {
- if ($this->isView) {
- if ($element instanceof \Zend\Form\Element\Checkbox) {
- // $element->setAttribute('disabled', true);
- return $this->getView()->formCheckbox($element);
- } elseif ($element instanceof \Zend\Form\Element\Select) {
- //$element->setAttribute('disabled', true);
- return $this->getView()->formSelect($element);
- } elseif ($element instanceof \Cadastro\Form\Element\Money) {
- return '<span class="list-group-item">' . $this->getView()->currencyformat($element->getValue(), 'BRL') . '</span>';
- }
- return '<span class="list-group-item">' . $element->getValue($element) . '</span>';
- } else {
- return $this->inputType($element);
- }
- }
- public function inputType($element)
- {
- if ($this->peeringHelper) {
- return $this->peeringHelper->render($element);
- }
- if ($element instanceof \Cadastro\Form\Element\Money) {
- return $this->getView()->formMoney($element);
- } elseif ($element instanceof \Zend\Form\Element\Text) {
- return $this->getView()->formText($element);
- } elseif ($element instanceof \Zend\Form\Element\Textarea) {
- return $this->getView()->formTextarea($element);
- } elseif ($element instanceof \Zend\Form\Element\Date) {
- return $this->getView()->formText($element);
- } elseif ($element instanceof \Zend\Form\Element\DateTime) {
- return $this->getView()->formDateTime($element);
- } elseif ($element instanceof \Zend\Form\Element\Checkbox) {
- return $this->getView()->formCheckbox($element);
- } elseif ($element instanceof \Zend\Form\Element\Select) {
- return $this->getView()->formSelect($element);
- } elseif ($element instanceof \Zend\Form\Element\Button) {
- return $this->getView()->formButton($element);
- } elseif ($element instanceof \Zend\Form\Element\Submit) {
- return $this->getView()->formSubmit($element);
- } else {
- throw new \Exception('Tipo de input invΓ‘lido: ' . get_class($element));
- }
- }
- /**
- * Set form to not show input fields, only its value
- *
- * @param boolean $isView
- */
- public function setIsView($isView = false)
- {
- $this->isView = $isView;
- }
- /**
- * Return if form is in view mode (not showing input fields)
- *
- * @return boolean
- */
- public function isView()
- {
- return $this->isView;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement