Advertisement
Alfredao

Form Format

Mar 7th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.76 KB | None | 0 0
  1. <?php
  2. namespace Cadastro\View\Helper;
  3.  
  4. use Zend\View\Helper\AbstractHelper;
  5.  
  6. class FormFormat extends AbstractHelper
  7. {
  8.  
  9.     /**
  10.      *
  11.      * @var AbstractHelper
  12.      */
  13.     protected $peeringHelper;
  14.  
  15.     /**
  16.      *
  17.      * @var boolean
  18.      */
  19.     protected $isView;
  20.  
  21.     /**
  22.      * Invoke helper as functor
  23.      *
  24.      * Proxies to {@link render()}.
  25.      *
  26.      * @param ElementInterface|null $element            
  27.      * @param AbstractHelper|null $render            
  28.      * @return string FormInput
  29.      */
  30.     public function __invoke($element = null, $peeringHelper = null, $class = false)
  31.     {
  32.         if (! $element) {
  33.             return $this;
  34.         }
  35.        
  36.         if ($peeringHelper) {
  37.             $this->peeringHelper = $peeringHelper;
  38.         }
  39.        
  40.         if ($element instanceof \Zend\Form\Element\Collection)
  41.             return $this->renderCollection($element);
  42.        
  43.         return $this->renderElement($element, $class);
  44.     }
  45.  
  46.     /**
  47.      * Render unique fields
  48.      *
  49.      * @param unknown $element            
  50.      * @param unknown $class            
  51.      * @return string
  52.      */
  53.     public function renderElement($element)
  54.     {
  55.         $return = '';
  56.        
  57.         if (count($element->getMessages()) > 0) {
  58.             $return .= '<div class="form-group has-error">';
  59.         } else {
  60.             $return .= '<div class="form-group">';
  61.         }
  62.        
  63.         $label = ($this->isView) ? '<strong>' . $element->getLabel() . '</strong>' : $element->getLabel();
  64.        
  65.         $return .= '<label class="col-md-3 control-label">' . $label . '</label>';
  66.         $return .= '<div class="col-md-4">';
  67.        
  68.         $return .= $this->renderInput($element);
  69.        
  70.         foreach ($element->getMessages() as $mensagem) {
  71.             $return .= "<span class='help-block'>" . $mensagem . "</span>";
  72.         }
  73.        
  74.         $return .= '</div>';
  75.         $return .= '</div>';
  76.        
  77.         return $return;
  78.     }
  79.    
  80.     /**
  81.      * Render a form collection
  82.      *
  83.      * @param unknown $element
  84.      * @return string
  85.      */
  86.     public function renderCollection($element)
  87.     {
  88.         $return = '';
  89.        
  90.         if (count($element->getMessages()) > 0) {
  91.             $return .= '<div class="form-group has-error">';
  92.         } else {
  93.             $return .= '<div class="form-group">';
  94.         }
  95.        
  96.         $label = ($this->isView) ? '<strong>' . $element->getLabel() . '</strong>' : $element->getLabel();
  97.        
  98.         $return .= '<label class="col-md-3 control-label">' . $label . '</label>';
  99.         $return .= '<div class="col-md-6">';        
  100.        
  101.         $element->setLabel('');
  102.         $return .= $this->getView()->formrow($element);
  103.  
  104.         foreach ($element->getMessages() as $mensagem) {
  105.             //$return .= "<span class='help-block'>" . $mensagem . "</span>";
  106.         }
  107.        
  108.         $return .= '</div>';
  109.         $return .= '</div>';
  110.        
  111.         return $return;
  112.     }
  113.  
  114.     /**
  115.      *
  116.      * @param unknown $element            
  117.      */
  118.     public function renderInput($element)
  119.     {
  120.         if ($this->isView) {
  121.            
  122.             if ($element instanceof \Zend\Form\Element\Checkbox) {
  123.                 // $element->setAttribute('disabled', true);
  124.                 return $this->getView()->formCheckbox($element);
  125.             } elseif ($element instanceof \Zend\Form\Element\Select) {
  126.                 //$element->setAttribute('disabled', true);
  127.                 return $this->getView()->formSelect($element);
  128.             } elseif ($element instanceof \Cadastro\Form\Element\Money) {
  129.                 return '<span class="list-group-item">' . $this->getView()->currencyformat($element->getValue(), 'BRL') . '</span>';
  130.             }
  131.            
  132.             return '<span class="list-group-item">' . $element->getValue($element) . '</span>';
  133.         } else {
  134.             return $this->inputType($element);
  135.         }
  136.     }
  137.  
  138.     public function inputType($element)
  139.     {
  140.         if ($this->peeringHelper) {
  141.             return $this->peeringHelper->render($element);
  142.         }
  143.        
  144.         if ($element instanceof \Cadastro\Form\Element\Money) {
  145.             return $this->getView()->formMoney($element);
  146.         } elseif ($element instanceof \Zend\Form\Element\Text) {
  147.             return $this->getView()->formText($element);
  148.         } elseif ($element instanceof \Zend\Form\Element\Textarea) {
  149.             return $this->getView()->formTextarea($element);
  150.         } elseif ($element instanceof \Zend\Form\Element\Date) {
  151.             return $this->getView()->formText($element);
  152.         } elseif ($element instanceof \Zend\Form\Element\DateTime) {
  153.             return $this->getView()->formDateTime($element);
  154.         } elseif ($element instanceof \Zend\Form\Element\Checkbox) {
  155.             return $this->getView()->formCheckbox($element);
  156.         } elseif ($element instanceof \Zend\Form\Element\Select) {
  157.             return $this->getView()->formSelect($element);
  158.         } elseif ($element instanceof \Zend\Form\Element\Button) {
  159.             return $this->getView()->formButton($element);
  160.         } elseif ($element instanceof \Zend\Form\Element\Submit) {
  161.             return $this->getView()->formSubmit($element);
  162.         } else {
  163.             throw new \Exception('Tipo de input invΓ‘lido: ' . get_class($element));
  164.         }
  165.     }
  166.  
  167.     /**
  168.      * Set form to not show input fields, only its value
  169.      *
  170.      * @param boolean $isView            
  171.      */
  172.     public function setIsView($isView = false)
  173.     {
  174.         $this->isView = $isView;
  175.     }
  176.  
  177.     /**
  178.      * Return if form is in view mode (not showing input fields)
  179.      *
  180.      * @return boolean
  181.      */
  182.     public function isView()
  183.     {
  184.         return $this->isView;
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement