Advertisement
saadimran

FormElementDecorator

Mar 25th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 24.63 KB | None | 0 0
  1. <?php
  2. /**
  3.  * AdRally
  4.  *
  5.  * @category Fuse
  6.  * @copyright Copyright (c) FusePowered Inc.
  7.  * @version 1.0 (2014-12-31)
  8.  * @author Saad Imran <simran@fusepowered.com>
  9.  */
  10.  
  11. namespace Fuse\View\Helper\Decorator;
  12.  
  13. use Fuse\Forms\Element\Button,
  14.     Phalcon\Forms\ElementInterface,
  15.     Phalcon\Forms\Element\Check,
  16.     Phalcon\Forms\Element\Radio,
  17.     Phalcon\Forms\Element\Submit,
  18.     Phalcon\Forms\Element\File,
  19.     Fuse\Forms\Element\AjaxFile,
  20.     Phalcon\Tag;
  21. use Fuse\Forms\Element\Time;
  22.  
  23. class FormElementDecorator extends Decorator
  24. {
  25.     /**
  26.      * @var array
  27.      */
  28.     protected $elementAttribs;
  29.  
  30.     /**
  31.      * @var string
  32.      */
  33.     protected $elementColumnGridClass;
  34.  
  35.     /**
  36.      * @var array
  37.      */
  38.     protected $elementWrapperAttribs;
  39.  
  40.     /**
  41.      * @var string
  42.      */
  43.     protected $elementDisplay;
  44.  
  45.     /**
  46.      * @var array
  47.      */
  48.     protected $elementDisplayAttribs;
  49.  
  50.     /**
  51.      * @var array
  52.      */
  53.     protected $labelAttribs;
  54.  
  55.     /**
  56.      * @var string
  57.      */
  58.     protected $labelColumnGridClass;
  59.  
  60.     /**
  61.      * @var array
  62.      */
  63.     protected $labelLinkAttribs;
  64.  
  65.     /**
  66.      * @var string
  67.      */
  68.     protected $textBefore;
  69.  
  70.     /**
  71.      * @var array
  72.      */
  73.     protected $textBeforeAttribs;
  74.  
  75.     /**
  76.      * @var string
  77.      */
  78.     protected $textAfter;
  79.  
  80.     /**
  81.      * @var array
  82.      */
  83.     protected $textAfterAttribs;
  84.  
  85.     /**
  86.      * @var string
  87.      */
  88.     protected $addonBefore;
  89.  
  90.     /**
  91.      * @var array
  92.      */
  93.     protected $addonBeforeAttribs;
  94.  
  95.     /**
  96.      * @var string
  97.      */
  98.     protected $addonAfter;
  99.  
  100.     /**
  101.      * @var array
  102.      */
  103.     protected $addonAfterAttribs;
  104.  
  105.     /**
  106.      * @var string
  107.      */
  108.     protected $helpBlock;
  109.  
  110.     /**
  111.      * @var array
  112.      */
  113.     protected $helpBlockAttribs;
  114.  
  115.     /**
  116.      * @var array
  117.      */
  118.     protected $inputGroupAttribs;
  119.  
  120.     /**
  121.      * @var array
  122.      */
  123.     protected $formGroupAttribs;
  124.  
  125.     /**
  126.      * @var string
  127.      */
  128.     protected $fieldsetName;
  129.  
  130.     /**
  131.      * @return string
  132.      */
  133.     public function getAddonAfter()
  134.     {
  135.         return $this->addonAfter;
  136.     }
  137.  
  138.     /**
  139.      * @param string $addonAfter
  140.      * @return $this
  141.      */
  142.     public function setAddonAfter($addonAfter)
  143.     {
  144.         $this->addonAfter = $addonAfter;
  145.         return $this;
  146.     }
  147.  
  148.     /**
  149.      * @return array
  150.      */
  151.     public function &getAddonAfterAttribs()
  152.     {
  153.         is_array($this->addonAfterAttribs) or ($this->addonAfterAttribs = (array) $this->addonAfterAttribs);
  154.         return $this->addonAfterAttribs;
  155.     }
  156.  
  157.     /**
  158.      * @param array $addonAfterAttribs
  159.      * @param int $mergeType Indicates how new attributes should be merged with current attributes.
  160.      * @return $this
  161.      */
  162.     public function setAddonAfterAttribs($addonAfterAttribs, $mergeType = self::MERGE_TYPE_NONE)
  163.     {
  164.         $this->addonAfterAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $addonAfterAttribs : self::merge($mergeType, $this->addonAfterAttribs, $addonAfterAttribs);
  165.         return $this;
  166.     }
  167.  
  168.     /**
  169.      * @return string
  170.      */
  171.     public function getAddonBefore()
  172.     {
  173.         return $this->addonBefore;
  174.     }
  175.  
  176.     /**
  177.      * @param string $addonBefore
  178.      * @return $this
  179.      */
  180.     public function setAddonBefore($addonBefore)
  181.     {
  182.         $this->addonBefore = $addonBefore;
  183.         return $this;
  184.     }
  185.  
  186.     /**
  187.      * @return array
  188.      */
  189.     public function &getAddonBeforeAttribs()
  190.     {
  191.         is_array($this->addonBeforeAttribs) or ($this->addonBeforeAttribs = (array) $this->addonBeforeAttribs);
  192.         return $this->addonBeforeAttribs;
  193.     }
  194.  
  195.     /**
  196.      * @param array $addonBeforeAttribs
  197.      * @param int $mergeType Indicates how new attributes should be merged with current attributes.
  198.      * @return $this
  199.      */
  200.     public function setAddonBeforeAttribs($addonBeforeAttribs, $mergeType = self::MERGE_TYPE_NONE)
  201.     {
  202.         $this->addonBeforeAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $addonBeforeAttribs : self::merge($mergeType, $this->addonBeforeAttribs, $addonBeforeAttribs);
  203.         return $this;
  204.     }
  205.  
  206.     /**
  207.      * @return array
  208.      */
  209.     public function &getElementAttribs()
  210.     {
  211.         is_array($this->elementAttribs) or ($this->elementAttribs = (array) $this->elementAttribs);
  212.         return $this->elementAttribs;
  213.     }
  214.  
  215.     /**
  216.      * @param array $elementAttribs
  217.      * @param int $mergeType Indicates how new attributes should be merged with current attributes.
  218.      * @return $this
  219.      */
  220.     public function setElementAttribs($elementAttribs, $mergeType = self::MERGE_TYPE_NONE)
  221.     {
  222.         $this->elementAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $elementAttribs : self::merge($mergeType, $this->elementAttribs, $elementAttribs);
  223.         return $this;
  224.     }
  225.  
  226.     /**
  227.      * @return string
  228.      */
  229.     public function getElementColumnGridClass()
  230.     {
  231.         return $this->elementColumnGridClass;
  232.     }
  233.  
  234.     /**
  235.      * @param string $elementColumnGridClass
  236.      * @return $this
  237.      */
  238.     public function setElementColumnGridClass($elementColumnGridClass)
  239.     {
  240.         $this->elementColumnGridClass = $elementColumnGridClass;
  241.         return $this;
  242.     }
  243.  
  244.     /**
  245.      * @return string
  246.      */
  247.     public function getElementDisplay()
  248.     {
  249.         return $this->elementDisplay;
  250.     }
  251.  
  252.     /**
  253.      * @param string $elementDisplay
  254.      * @return $this
  255.      */
  256.     public function setElementDisplay($elementDisplay)
  257.     {
  258.         $this->elementDisplay = $elementDisplay;
  259.         return $this;
  260.     }
  261.  
  262.     /**
  263.      * @return array
  264.      */
  265.     public function &getElementDisplayAttribs()
  266.     {
  267.         is_array($this->elementDisplayAttribs) or ($this->elementDisplayAttribs = (array) $this->elementDisplayAttribs);
  268.         return $this->elementDisplayAttribs;
  269.     }
  270.  
  271.     /**
  272.      * @param array $elementDisplayAttribs
  273.      * @param int $mergeType Indicates how new attributes should be merged with current attributes.
  274.      * @return $this
  275.      */
  276.     public function setElementDisplayAttribs($elementDisplayAttribs, $mergeType = self::MERGE_TYPE_NONE)
  277.     {
  278.         $this->elementDisplayAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $elementDisplayAttribs : self::merge($mergeType, $this->elementDisplayAttribs, $elementDisplayAttribs);
  279.         return $this;
  280.     }
  281.  
  282.     /**
  283.      * @return array
  284.      */
  285.     public function &getElementWrapperAttribs()
  286.     {
  287.         is_array($this->elementWrapperAttribs) or ($this->elementWrapperAttribs = (array) $this->elementWrapperAttribs);
  288.         return $this->elementWrapperAttribs;
  289.     }
  290.  
  291.     /**
  292.      * @param array $elementWrapperAttribs
  293.      * @param int $mergeType Indicates how new attributes should be merged with current attributes.
  294.      * @return $this
  295.      */
  296.     public function setElementWrapperAttribs($elementWrapperAttribs, $mergeType = self::MERGE_TYPE_NONE)
  297.     {
  298.         $this->elementWrapperAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $elementWrapperAttribs : self::merge($mergeType, $this->elementWrapperAttribs, $elementWrapperAttribs);
  299.         return $this;
  300.     }
  301.  
  302.     /**
  303.      * @return string
  304.      */
  305.     public function getFieldsetName()
  306.     {
  307.         return $this->fieldsetName;
  308.     }
  309.  
  310.     /**
  311.      * @param string $fieldsetName
  312.      * @return $this
  313.      */
  314.     public function setFieldsetName($fieldsetName)
  315.     {
  316.         $this->fieldsetName = $fieldsetName;
  317.         return $this;
  318.     }
  319.  
  320.     /**
  321.      * @return array
  322.      */
  323.     public function &getFormGroupAttribs()
  324.     {
  325.         is_array($this->formGroupAttribs) or ($this->formGroupAttribs = (array) $this->formGroupAttribs);
  326.         return $this->formGroupAttribs;
  327.     }
  328.  
  329.     /**
  330.      * @param array $formGroupAttribs
  331.      * @param int $mergeType Indicates how new attributes should be merged with current attributes.
  332.      * @return $this
  333.      */
  334.     public function setFormGroupAttribs($formGroupAttribs, $mergeType = self::MERGE_TYPE_NONE)
  335.     {
  336.         $this->formGroupAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $formGroupAttribs : self::merge($mergeType, $this->formGroupAttribs, $formGroupAttribs);
  337.         return $this;
  338.     }
  339.  
  340.     /**
  341.      * @return string
  342.      */
  343.     public function getHelpBlock()
  344.     {
  345.         return $this->helpBlock;
  346.     }
  347.  
  348.     /**
  349.      * @param string $helpBlock
  350.      * @return $this
  351.      */
  352.     public function setHelpBlock($helpBlock)
  353.     {
  354.         $this->helpBlock = $helpBlock;
  355.         return $this;
  356.     }
  357.  
  358.     /**
  359.      * @return array
  360.      */
  361.     public function &getHelpBlockAttribs()
  362.     {
  363.         is_array($this->helpBlockAttribs) or ($this->helpBlockAttribs = (array) $this->helpBlockAttribs);
  364.         return $this->helpBlockAttribs;
  365.     }
  366.  
  367.     /**
  368.      * @param array $helpBlockAttribs
  369.      * @param int $mergeType Indicates how new attributes should be merged with current attributes.
  370.      * @return $this
  371.      */
  372.     public function setHelpBlockAttribs($helpBlockAttribs, $mergeType = self::MERGE_TYPE_NONE)
  373.     {
  374.         $this->helpBlockAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $helpBlockAttribs : self::merge($mergeType, $this->helpBlockAttribs, $helpBlockAttribs);
  375.         return $this;
  376.     }
  377.  
  378.     /**
  379.      * @return array
  380.      */
  381.     public function &getInputGroupAttribs()
  382.     {
  383.         is_array($this->inputGroupAttribs) or ($this->inputGroupAttribs = (array) $this->inputGroupAttribs);
  384.         return $this->inputGroupAttribs;
  385.     }
  386.  
  387.     /**
  388.      * @param array $inputGroupAttribs
  389.      * @param int $mergeType Indicates how new attributes should be merged with current attributes.
  390.      * @return $this
  391.      */
  392.     public function setInputGroupAttribs($inputGroupAttribs, $mergeType = self::MERGE_TYPE_NONE)
  393.     {
  394.         $this->inputGroupAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $inputGroupAttribs : self::merge($mergeType, $this->inputGroupAttribs, $inputGroupAttribs);
  395.         return $this;
  396.     }
  397.  
  398.     /**
  399.      * @return array
  400.      */
  401.     public function &getLabelAttribs()
  402.     {
  403.         is_array($this->labelAttribs) or ($this->labelAttribs = (array) $this->labelAttribs);
  404.         return $this->labelAttribs;
  405.     }
  406.  
  407.     /**
  408.      * @param array $labelAttribs
  409.      * @param int $mergeType Indicates how new attributes should be merged with current attributes.
  410.      * @return $this
  411.      */
  412.     public function setLabelAttribs($labelAttribs, $mergeType = self::MERGE_TYPE_NONE)
  413.     {
  414.         $this->labelAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $labelAttribs : self::merge($mergeType, $this->labelAttribs, $labelAttribs);
  415.         return $this;
  416.     }
  417.  
  418.     /**
  419.      * @return string
  420.      */
  421.     public function getLabelColumnGridClass()
  422.     {
  423.         return $this->labelColumnGridClass;
  424.     }
  425.  
  426.     /**
  427.      * @param string $labelColumnGridClass
  428.      * @return $this
  429.      */
  430.     public function setLabelColumnGridClass($labelColumnGridClass)
  431.     {
  432.         $this->labelColumnGridClass = $labelColumnGridClass;
  433.         return $this;
  434.     }
  435.  
  436.     /**
  437.      * @return array
  438.      */
  439.     public function &getLabelLinkAttribs()
  440.     {
  441.         is_array($this->labelLinkAttribs) or ($this->labelLinkAttribs = (array) $this->labelLinkAttribs);
  442.         return $this->labelLinkAttribs;
  443.     }
  444.  
  445.     /**
  446.      * @param array $labelLinkAttribs
  447.      * @param int $mergeType Indicates how new attributes should be merged with current attributes.
  448.      * @return $this
  449.      */
  450.     public function setLabelLinkAttribs($labelLinkAttribs, $mergeType = self::MERGE_TYPE_NONE)
  451.     {
  452.         $this->labelLinkAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $labelLinkAttribs : self::merge($mergeType, $this->labelLinkAttribs, $labelLinkAttribs);
  453.         return $this;
  454.     }
  455.  
  456.     /**
  457.      * @return string
  458.      */
  459.     public function getTextAfter()
  460.     {
  461.         return $this->textAfter;
  462.     }
  463.  
  464.     /**
  465.      * @param string $textAfter
  466.      * @return $this
  467.      */
  468.     public function setTextAfter($textAfter)
  469.     {
  470.         $this->textAfter = $textAfter;
  471.         return $this;
  472.     }
  473.  
  474.     /**
  475.      * @return array
  476.      */
  477.     public function &getTextAfterAttribs()
  478.     {
  479.         is_array($this->textAfterAttribs) or ($this->textAfterAttribs = (array) $this->textAfterAttribs);
  480.         return $this->textAfterAttribs;
  481.     }
  482.  
  483.     /**
  484.      * @param array $textAfterAttribs
  485.      * @param int $mergeType Indicates how new attributes should be merged with current attributes.
  486.      * @return $this
  487.      */
  488.     public function setTextAfterAttribs($textAfterAttribs, $mergeType = self::MERGE_TYPE_NONE)
  489.     {
  490.         $this->textAfterAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $textAfterAttribs : self::merge($mergeType, $this->textAfterAttribs, $textAfterAttribs);
  491.         return $this;
  492.     }
  493.  
  494.     /**
  495.      * @return string
  496.      */
  497.     public function getTextBefore()
  498.     {
  499.         return $this->textBefore;
  500.     }
  501.  
  502.     /**
  503.      * @param string $textBefore
  504.      * @return $this
  505.      */
  506.     public function setTextBefore($textBefore)
  507.     {
  508.         $this->textBefore = $textBefore;
  509.         return $this;
  510.     }
  511.  
  512.     /**
  513.      * @return array
  514.      */
  515.     public function &getTextBeforeAttribs()
  516.     {
  517.         is_array($this->textBeforeAttribs) or ($this->textBeforeAttribs = (array) $this->textBeforeAttribs);
  518.         return $this->textBeforeAttribs;
  519.     }
  520.  
  521.     /**
  522.      * @param array $textBeforeAttribs
  523.      * @param int $mergeType Indicates how new attributes should be merged with current attributes.
  524.      * @return $this
  525.      */
  526.     public function setTextBeforeAttribs($textBeforeAttribs, $mergeType = self::MERGE_TYPE_NONE)
  527.     {
  528.         $this->textBeforeAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $textBeforeAttribs : self::merge($mergeType, $this->textBeforeAttribs, $textBeforeAttribs);
  529.         return $this;
  530.     }
  531.  
  532.     /**
  533.      * Decorates the form element and returns the decorated HTML.
  534.      *
  535.      * @param $element ElementInterface
  536.      * @param $decorator FormElementDecorator
  537.      *
  538.      * @return string
  539.      */
  540.     public static function decorate($element, $decorator)
  541.     {
  542.         // get form ID and generate element ID
  543.         $formID = $element->getForm() ? $element->getForm()->getID() : '';
  544.         $elementName = $element->getName();
  545.         $elementID = self::generateID($formID, 'el', $elementName);
  546.  
  547.         $isRadio = $element instanceof Radio;
  548.         $isCheckbox = get_class($element) == Check::class;
  549.         $isButton = $element instanceof Submit || $element instanceof Button;
  550.         $isFile = $element instanceof File;
  551.  
  552.         // get config options
  553.         $elementAttribs = $decorator->getElementAttribs();
  554.         $elementWrapperAttribs = $decorator->getElementWrapperAttribs();
  555.         $elementDisplayAttribs = $decorator->getElementDisplayAttribs();
  556.  
  557.         $labelAttribs = $decorator->getLabelAttribs();
  558.         $labelLinkAttribs = $decorator->getLabelLinkAttribs();
  559.  
  560.         $textBeforeAttribs = $decorator->getTextBeforeAttribs();
  561.         $textAfterAttribs = $decorator->getTextAfterAttribs();
  562.  
  563.         $addonBeforeAttribs = $decorator->getAddonBeforeAttribs();
  564.         $addonAfterAttribs = $decorator->getAddonAfterAttribs();
  565.  
  566.         $helpBlockAttribs = $decorator->getHelpBlockAttribs();
  567.  
  568.         $inputGroupAttribs = $decorator->getInputGroupAttribs();
  569.         $formGroupAttribs = $decorator->getFormGroupAttribs();
  570.  
  571.         // element config
  572.         $elementAttribs = self::merge(
  573.             self::MERGE_TYPE_CONCAT_CLASS,
  574.             ['id' => $elementID],
  575.             $element->getAttributes(),
  576.             $elementAttribs
  577.         );
  578.  
  579.         if (!$isButton && !$isRadio && !$isCheckbox) {
  580.             self::appendToArrayValue($elementAttribs, 'class', 'form-control');
  581.         }
  582.  
  583.         if ($isButton) {
  584.             self::appendToArrayValue($elementAttribs, 'class', 'btn');
  585.         }
  586.  
  587.         // element wrapper config
  588.         if ($element instanceof Time) {
  589.             self::appendToArrayValue($elementWrapperAttribs, 'class', 'bootstrap-timepicker');
  590.         }
  591.  
  592.         // label config
  593.         $labelAttribs = self::merge(
  594.             self::MERGE_TYPE_CONCAT_CLASS,
  595.             [
  596.                 'for' => $elementID,
  597.                 'class' => $decorator->getLabelColumnGridClass()
  598.             ],
  599.             $labelAttribs
  600.         );
  601.  
  602.         // label link config
  603.         $hasLabelLink = (bool)count($labelLinkAttribs);
  604.  
  605.         // text before config
  606.         self::appendToArrayValue($textBeforeAttribs, 'class', 'form-control-static');
  607.  
  608.         // text after config
  609.         self::appendToArrayValue($textAfterAttribs, 'class', 'form-control-static');
  610.  
  611.         // addon before config
  612.         self::appendToArrayValue($addonBeforeAttribs, 'class', 'input-group-addon');
  613.  
  614.         // addon after config
  615.         self::appendToArrayValue($addonAfterAttribs, 'class', 'input-group-addon');
  616.  
  617.         // help block config
  618.         self::appendToArrayValue($helpBlockAttribs, 'class', 'help-block');
  619.  
  620.         // input group options
  621.         $hasInputGroup = (bool)($decorator->getAddonBefore() || $decorator->getAddonAfter());
  622.         self::appendToArrayValue($inputGroupAttribs, 'class', 'input-group');
  623.  
  624.  
  625.         // form group options
  626.         $formGroupAttribs = self::merge(
  627.             self::MERGE_TYPE_CONCAT_CLASS,
  628.             [
  629.                 'class' => 'form-group ' . ($elementName . '-form-group')
  630.             ],
  631.             $formGroupAttribs
  632.         );
  633.  
  634.         // final config modifications
  635.         if ($hasInputGroup) {
  636.             self::appendToArrayValue($inputGroupAttribs, 'class', $decorator->getElementColumnGridClass());
  637.         } else {
  638.             if ($isRadio || $isCheckbox) {
  639.                 self::appendToArrayValue($elementWrapperAttribs, 'class', $isRadio ? 'radio' : 'checkbox');
  640.             }
  641.             self::appendToArrayValue($elementWrapperAttribs, 'class', $decorator->getElementColumnGridClass());
  642.         }
  643.  
  644.        $genTextBeforeHtml = function () use (
  645.             $element, $decorator, $textBeforeAttribs
  646.         ) {
  647.             $html = null;
  648.  
  649.             if ($textBefore = $decorator->getTextBefore()) {
  650.                 $html .= Tag::tagHtml('p', $textBeforeAttribs); // text before open
  651.                 $html .= $textBefore; // text before
  652.                 $html .= Tag::tagHtmlClose('p'); // text before close
  653.             }
  654.  
  655.             return $html;
  656.         };
  657.  
  658.         $genTextAftereHtml = function () use (
  659.             $element, $decorator, $textAfterAttribs
  660.         ) {
  661.             $html = null;
  662.  
  663.             if ($textAfter = $decorator->getTextAfter()) {
  664.                 $html .= Tag::tagHtml('p', $textAfterAttribs); // text after open
  665.                 $html .= $textAfter; // text after
  666.                 $html .= Tag::tagHtmlClose('p'); // text after close
  667.             }
  668.  
  669.             return $html;
  670.         };
  671.  
  672.         $genLabelHtml = function () use (
  673.             $element, $decorator,
  674.             $isCheckbox, $isRadio,
  675.             $labelAttribs, $labelLinkAttribs, $hasLabelLink
  676.         ) {
  677.             $html = null;
  678.  
  679.             if ($label = $element->getLabel()) {
  680.                 $html .= Tag::tagHtml('label', $labelAttribs); // label open
  681.                 if ($hasLabelLink) {
  682.                     $html .= Tag::tagHtml('a', $labelLinkAttribs); // label link open
  683.                 }
  684.                 $html .= $label; // label
  685.                 if ($hasLabelLink) {
  686.                     $html .= Tag::tagHtmlClose('a'); // label link close
  687.                 }
  688.                 $html .= Tag::tagHtmlClose('label'); // label close
  689.             }
  690.  
  691.             return $html;
  692.         };
  693.  
  694.         $genElementHtml = function () use (
  695.             $element, $decorator, $elementAttribs, $elementDisplayAttribs, $isRadio, $isCheckbox
  696.         ) {
  697.             $html = null;
  698.  
  699.             // if radio or checkbox wrap in label, and print display option
  700.             if ($isRadio || $isCheckbox) {
  701.                 $class = $isRadio ? 'radio-custom' : 'checkbox-custom';
  702.                 $html .= Tag::tagHtml('label class="'.$class.'"'); // element wrapper open
  703.                 $html .= $element->render($elementAttribs) . PHP_EOL; // element
  704.                 if ($isCheckbox)
  705.                 {
  706.                     $html .= Tag::tagHtml('i class="fa fa-fw fa-square-o"').Tag::tagHtmlClose('i');
  707.                 }
  708.                 elseif ($isRadio)
  709.                 {
  710.                     $html .= Tag::tagHtml('i class="fa fa-circle-o"').Tag::tagHtmlClose('i');
  711.                 }
  712.                 if ($elementDisplay = $decorator->getElementDisplay()) {
  713.                     $html .= Tag::tagHtml('span', $elementDisplayAttribs); // element display open
  714.                     $html .= $elementDisplay; // element display
  715.                     $html .= Tag::tagHtmlClose('span'); // element display close
  716.                 }
  717.                 $html .= Tag::tagHtmlClose('label'); // element wrapper close
  718.             } else {
  719.                 $html .= $element->render($elementAttribs); // element
  720.             }
  721.  
  722.             return $html;
  723.         };
  724.  
  725.         $genInputGroupHtml = function ($innerHtml) use (
  726.             $element, $decorator, $isFile,
  727.             $inputGroupAttribs, $elementWrapperAttribs,
  728.             $addonBeforeAttribs, $addonAfterAttribs
  729.         ) {
  730.             $html = null;
  731.  
  732.             $hasInputGroup = (bool) ($decorator->getAddonBefore() || $decorator->getAddonAfter());
  733.             $isAjaxFile = (get_class($element) == AjaxFile::class) ? true : false;
  734.  
  735.             if ($hasInputGroup) {
  736.                
  737.                 $html .= Tag::tagHtml('div', $inputGroupAttribs); // input group open
  738.  
  739.                 if ($addonBefore = $decorator->getAddonBefore()) {
  740.                     $html .= Tag::tagHtml('div', $addonBeforeAttribs); // addon before open
  741.                     $html .= $addonBefore; // addon before value
  742.                     $html .= Tag::tagHtmlClose('div'); // addon before close
  743.                 }
  744.  
  745.                 $html .= $innerHtml; // inner html
  746.  
  747.                 if ($addonAfter = $decorator->getAddonAfter()) {
  748.                     $html .= Tag::tagHtml('div', $addonAfterAttribs); // addon after open
  749.                     $html .= $addonAfter; // addon after value
  750.                     $html .= Tag::tagHtmlClose('div'); // addon after close
  751.                 }
  752.  
  753.                 $html .= Tag::tagHtmlClose('div'); // input group close
  754.             } elseif ($isFile && !$isAjaxFile) {
  755.                 $html = Tag::tagHtml('div')
  756.                         . Tag::tagHtml('div class="fileinput fileinput-new" data-provides="fileinput" ')
  757.                         . Tag::tagHtml('span', $elementWrapperAttribs)
  758.                         . '<span class="fileinput-new">Select File</span>'
  759.                         . '<span class="fileinput-exists">Change</span>'
  760.                         . $innerHtml . $isAjaxFile
  761.                         . Tag::tagHtmlClose('span')
  762.                         . '<span class="fileinput-filename"></span>'
  763.                         . '<a href="#" class="close fileinput-exists" data-dismiss="fileinput" style="float: none"><i class="icon-close"></i></a>'
  764.                         . Tag::tagHtmlClose('div')
  765.                         . Tag::tagHtmlClose('div');
  766.             } else {
  767.                 // if there's no input group, create a wrapper
  768.                 $html = Tag::tagHtml('div', $elementWrapperAttribs) . $innerHtml . Tag::tagHtmlClose('div');
  769.             }
  770.  
  771.             return $html;
  772.         };
  773.  
  774.         $genFormGroupHtml = function() use (
  775.             $decorator,
  776.             $isRadio, $isCheckbox, $formGroupAttribs, $helpBlockAttribs,
  777.             $genLabelHtml, $genElementHtml, $genInputGroupHtml, $genTextBeforeHtml, $genTextAftereHtml
  778.         ) {
  779.             $html = null;
  780.  
  781.             $html .= Tag::tagHtml('div', $formGroupAttribs); // form group open
  782.             $html .= $genLabelHtml(); // label
  783.             $html .= $genTextBeforeHtml(); // text before
  784.             $html .= $genInputGroupHtml($genElementHtml()); // element and input group
  785.             $html .= $genTextAftereHtml(); // text after
  786.             if ($helpBlock = $decorator->getHelpBlock()) {
  787.                 $html .= Tag::tagHtml('p', $helpBlockAttribs); // help block open
  788.                 $html .= $helpBlock; // help block
  789.                 $html .= Tag::tagHtmlClose('p'); // help block close
  790.             }
  791.             $html .= Tag::tagHtmlClose('div'); // form group close
  792.  
  793.             return $html;
  794.         };
  795.  
  796.         $html = null;
  797.  
  798.         // generate form-group
  799.         $html = $genFormGroupHtml();
  800.  
  801.         if ($postDecorateCallback = $decorator->getPostDecorateCallback()) {
  802.             $html = $postDecorateCallback($html);
  803.         }
  804.  
  805.         return $html . PHP_EOL;
  806.     }
  807. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement