Alfredao

FormGroupMultiCheckBoxes

Aug 8th, 2014
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.39 KB | None | 0 0
  1. <?php
  2. namespace Acl\View\Helper;
  3.  
  4. use Zend\Form\ElementInterface;
  5. use Zend\Form\Element\MultiCheckbox as MultiCheckboxElement;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Criteria;
  8.  
  9. class FormGroupMultiCheckbox extends \Zend\Form\View\Helper\FormMultiCheckbox
  10. {
  11.  
  12.     /**
  13.      *
  14.      * @var \Closure
  15.      */
  16.     protected $headCheckCallback;
  17.  
  18.     /**
  19.      *
  20.      * @var \Closure
  21.      */
  22.     protected $headLabelCallback;
  23.  
  24.     /**
  25.      *
  26.      * @var unknown
  27.      */
  28.     protected $lastOptionSpec;
  29.  
  30.     /**
  31.      *
  32.      * @var unknown
  33.      */
  34.     protected $currentOptionSpec;
  35.  
  36.     /**
  37.      *
  38.      * @var string
  39.      */
  40.     protected $groupContentOpenTag = '<div id="collapse_%d" class="panel-collapse %s">';
  41.  
  42.     /**
  43.      *
  44.      * @var string
  45.      */
  46.     protected $groupContentCloseTag = '</div>';
  47.  
  48.     /**
  49.      *
  50.      * @var string
  51.      */
  52.     protected $headContentOpenTag = '<div class="panel-heading">
  53.                     <h4 class="panel-title">
  54.                         <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion%1$d" href="#collapse_%1$d">';
  55.  
  56.     /**
  57.      *
  58.      * @var string
  59.      */
  60.     protected $headContentCloseTag = '</a>
  61.                     </h4>
  62.                 </div>';
  63.  
  64.     /**
  65.      *
  66.      * @var string
  67.      */
  68.     protected $groupOpenTag = '<div class="panel panel-default">';
  69.  
  70.     /**
  71.      *
  72.      * @var string
  73.      */
  74.     protected $groupCloseTag = '</div>';
  75.  
  76.     /**
  77.      */
  78.     protected $conainerTag = '%2$s';
  79.  
  80.     protected $groupCount = 0;
  81.  
  82.     /**
  83.      */
  84.     public function render(ElementInterface $element)
  85.     {
  86.         return parent::render($element);
  87.     }
  88.  
  89.     /**
  90.      */
  91.     protected function renderOptions(MultiCheckboxElement $element, array $options, array $selectedOptions, array $attributes)
  92.     {
  93.         $escapeHtmlHelper = $this->getEscapeHtmlHelper();
  94.         $labelHelper = $this->getLabelHelper();
  95.         $labelClose = $labelHelper->closeTag();
  96.         $labelPosition = $this->getLabelPosition();
  97.         $globalLabelAttributes = $element->getLabelAttributes();
  98.         $closingBracket = $this->getInlineClosingBracket();
  99.        
  100.         if (empty($globalLabelAttributes)) {
  101.             $globalLabelAttributes = $this->labelAttributes;
  102.         }
  103.        
  104.         $combinedMarkup = array();
  105.         $count = 0;
  106.        
  107.         $this->lastOptionSpec = false;
  108.        
  109.         $searchElements = new ArrayCollection($element->getProxy()->getObjects());
  110.        
  111.         foreach ($options as $key => $optionSpec) {
  112.            
  113.             $count ++;
  114.            
  115.             if ($count > 1 && array_key_exists('id', $attributes)) {
  116.                 unset($attributes['id']);
  117.             }
  118.            
  119.             $value = '';
  120.             $label = '';
  121.             $inputAttributes = $attributes;
  122.             $labelAttributes = $globalLabelAttributes;
  123.             $selected = isset($inputAttributes['selected']) && $inputAttributes['type'] != 'radio' && $inputAttributes['selected'] != false ? true : false;
  124.             $disabled = isset($inputAttributes['disabled']) && $inputAttributes['disabled'] != false ? true : false;
  125.            
  126.             if (is_scalar($optionSpec)) {
  127.                 $optionSpec = array(
  128.                     'label' => $optionSpec,
  129.                     'value' => $key
  130.                 );
  131.             }
  132.            
  133.             if (isset($optionSpec['value'])) {
  134.                 $value = $optionSpec['value'];
  135.             }
  136.             if (isset($optionSpec['label'])) {
  137.                 $label = $optionSpec['label'];
  138.             }
  139.             if (isset($optionSpec['selected'])) {
  140.                 $selected = $optionSpec['selected'];
  141.             }
  142.             if (isset($optionSpec['disabled'])) {
  143.                 $disabled = $optionSpec['disabled'];
  144.             }
  145.             if (isset($optionSpec['label_attributes'])) {
  146.                 $labelAttributes = (isset($labelAttributes)) ? array_merge($labelAttributes, $optionSpec['label_attributes']) : $optionSpec['label_attributes'];
  147.             }
  148.             if (isset($optionSpec['attributes'])) {
  149.                 $inputAttributes = array_merge($inputAttributes, $optionSpec['attributes']);
  150.             }
  151.            
  152.             if (in_array($value, $selectedOptions)) {
  153.                 $selected = true;
  154.             }
  155.            
  156.             $inputAttributes['value'] = $value;
  157.             $inputAttributes['checked'] = $selected;
  158.             $inputAttributes['disabled'] = $disabled;
  159.            
  160.             $input = sprintf('<input %s%s', $this->createAttributesString($inputAttributes), $closingBracket);
  161.            
  162.             if (null !== ($translator = $this->getTranslator())) {
  163.                 $label = $translator->translate($label, $this->getTranslatorTextDomain());
  164.             }
  165.            
  166.             $label = $escapeHtmlHelper($label);
  167.             $labelOpen = $labelHelper->openTag($labelAttributes);
  168.             $template = '<div>' . $labelOpen . '%s&nbsp;%s' . $labelClose . '</div>' . PHP_EOL;
  169.            
  170.             switch ($labelPosition) {
  171.                 case self::LABEL_PREPEND:
  172.                     $markup = sprintf($template, $label, $input);
  173.                     break;
  174.                 case self::LABEL_APPEND:
  175.                     $markup = sprintf($template, $input, $label);
  176.                     break;
  177.                 default:
  178.                     break;
  179.             }
  180.            
  181.             $criteria = new Criteria();
  182.             $criteria->where($criteria->expr()->eq('id', $optionSpec['value']));
  183.             $current = $searchElements->matching($criteria)->first();
  184.            
  185.             // se preencheu o callback do label e também
  186.             // não é a primeira função-ação ou a anterior é diferente da atual
  187.             if ($this->headLabelCallback && call_user_func($this->headCheckCallback, $this->lastOptionSpec, $current)) {
  188.                
  189.                 $this->groupCount ++;
  190.                 $headLabel = call_user_func($this->headLabelCallback, $current);
  191.                
  192.                 // se tem ultimo função-ação preenchido
  193.                 if ($this->lastOptionSpec) {
  194.                     // fecha div do grupo principal [panel-default]
  195.                     $combinedMarkup[] = $this->groupContentCloseTag;
  196.                     // fecha div do grupo de inputs [panel-collapse]
  197.                     $combinedMarkup[] = $this->groupCloseTag;
  198.                 }
  199.                
  200.                 // abre div [panel-default]
  201.                 $combinedMarkup[] = $this->groupOpenTag;
  202.                 // abre div [panel-heading]
  203.                 $combinedMarkup[] = sprintf($this->headContentOpenTag, $this->groupCount) . $headLabel . $this->headContentCloseTag;
  204.                 // abre div [panel-collapse]
  205.                 $combinedMarkup[] = sprintf($this->groupContentOpenTag, $this->groupCount, $selected ? 'in' : 'collapse');
  206.                
  207.                 // preenche a ultima função-ação a partir da atual
  208.                 $this->lastOptionSpec = $current;
  209.             }
  210.             // prenche div do input
  211.             $combinedMarkup[] = $markup;
  212.         }
  213.         // fecha div do grupo principal [panel-default]
  214.         $combinedMarkup[] = $this->groupContentCloseTag;
  215.        
  216.         $contents = implode($this->getSeparator(), $combinedMarkup);
  217.         return sprintf($this->conainerTag, $element->getName(), $contents);
  218.     }
  219.  
  220.     /**
  221.      *
  222.      * @param \Closure $headCheckCallback            
  223.      * @return \Application\View\Helper\FormGroupMultiCheckbox
  224.      */
  225.     public function setHeadCheckCallback($headCheckCallback)
  226.     {
  227.         $this->headCheckCallback = $headCheckCallback;
  228.         return $this;
  229.     }
  230.  
  231.     /**
  232.      *
  233.      * @param \Closure $headLabel            
  234.      * @return \Application\View\Helper\FormGroupMultiCheckbox
  235.      */
  236.     public function setHeadLabelCallback($headLabelCallback)
  237.     {
  238.         $this->headLabelCallback = $headLabelCallback;
  239.         return $this;
  240.     }
  241.  
  242.     /**
  243.      *
  244.      * @param array $options            
  245.      */
  246.     public function setOptions(array $options)
  247.     {
  248.         if (isset($options['head_check_callback'])) {
  249.             $this->setHeadCheckCallback($options['head_check_callback']);
  250.         }
  251.         if (isset($options['head_label_callback'])) {
  252.             $this->setHeadLabelCallback($options['head_label_callback']);
  253.         }
  254.         return $this;
  255.     }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment