Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * AdRally
- *
- * @category Fuse
- * @copyright Copyright (c) FusePowered Inc.
- * @version 1.0 (2014-12-31)
- * @author Saad Imran <simran@fusepowered.com>
- */
- namespace Fuse\View\Helper\Decorator;
- use Fuse\Forms\Element\Button,
- Phalcon\Forms\ElementInterface,
- Phalcon\Forms\Element\Check,
- Phalcon\Forms\Element\Radio,
- Phalcon\Forms\Element\Submit,
- Phalcon\Forms\Element\File,
- Fuse\Forms\Element\AjaxFile,
- Phalcon\Tag;
- use Fuse\Forms\Element\Time;
- class FormElementDecorator extends Decorator
- {
- /**
- * @var array
- */
- protected $elementAttribs;
- /**
- * @var string
- */
- protected $elementColumnGridClass;
- /**
- * @var array
- */
- protected $elementWrapperAttribs;
- /**
- * @var string
- */
- protected $elementDisplay;
- /**
- * @var array
- */
- protected $elementDisplayAttribs;
- /**
- * @var array
- */
- protected $labelAttribs;
- /**
- * @var string
- */
- protected $labelColumnGridClass;
- /**
- * @var array
- */
- protected $labelLinkAttribs;
- /**
- * @var string
- */
- protected $textBefore;
- /**
- * @var array
- */
- protected $textBeforeAttribs;
- /**
- * @var string
- */
- protected $textAfter;
- /**
- * @var array
- */
- protected $textAfterAttribs;
- /**
- * @var string
- */
- protected $addonBefore;
- /**
- * @var array
- */
- protected $addonBeforeAttribs;
- /**
- * @var string
- */
- protected $addonAfter;
- /**
- * @var array
- */
- protected $addonAfterAttribs;
- /**
- * @var string
- */
- protected $helpBlock;
- /**
- * @var array
- */
- protected $helpBlockAttribs;
- /**
- * @var array
- */
- protected $inputGroupAttribs;
- /**
- * @var array
- */
- protected $formGroupAttribs;
- /**
- * @var string
- */
- protected $fieldsetName;
- /**
- * @return string
- */
- public function getAddonAfter()
- {
- return $this->addonAfter;
- }
- /**
- * @param string $addonAfter
- * @return $this
- */
- public function setAddonAfter($addonAfter)
- {
- $this->addonAfter = $addonAfter;
- return $this;
- }
- /**
- * @return array
- */
- public function &getAddonAfterAttribs()
- {
- is_array($this->addonAfterAttribs) or ($this->addonAfterAttribs = (array) $this->addonAfterAttribs);
- return $this->addonAfterAttribs;
- }
- /**
- * @param array $addonAfterAttribs
- * @param int $mergeType Indicates how new attributes should be merged with current attributes.
- * @return $this
- */
- public function setAddonAfterAttribs($addonAfterAttribs, $mergeType = self::MERGE_TYPE_NONE)
- {
- $this->addonAfterAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $addonAfterAttribs : self::merge($mergeType, $this->addonAfterAttribs, $addonAfterAttribs);
- return $this;
- }
- /**
- * @return string
- */
- public function getAddonBefore()
- {
- return $this->addonBefore;
- }
- /**
- * @param string $addonBefore
- * @return $this
- */
- public function setAddonBefore($addonBefore)
- {
- $this->addonBefore = $addonBefore;
- return $this;
- }
- /**
- * @return array
- */
- public function &getAddonBeforeAttribs()
- {
- is_array($this->addonBeforeAttribs) or ($this->addonBeforeAttribs = (array) $this->addonBeforeAttribs);
- return $this->addonBeforeAttribs;
- }
- /**
- * @param array $addonBeforeAttribs
- * @param int $mergeType Indicates how new attributes should be merged with current attributes.
- * @return $this
- */
- public function setAddonBeforeAttribs($addonBeforeAttribs, $mergeType = self::MERGE_TYPE_NONE)
- {
- $this->addonBeforeAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $addonBeforeAttribs : self::merge($mergeType, $this->addonBeforeAttribs, $addonBeforeAttribs);
- return $this;
- }
- /**
- * @return array
- */
- public function &getElementAttribs()
- {
- is_array($this->elementAttribs) or ($this->elementAttribs = (array) $this->elementAttribs);
- return $this->elementAttribs;
- }
- /**
- * @param array $elementAttribs
- * @param int $mergeType Indicates how new attributes should be merged with current attributes.
- * @return $this
- */
- public function setElementAttribs($elementAttribs, $mergeType = self::MERGE_TYPE_NONE)
- {
- $this->elementAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $elementAttribs : self::merge($mergeType, $this->elementAttribs, $elementAttribs);
- return $this;
- }
- /**
- * @return string
- */
- public function getElementColumnGridClass()
- {
- return $this->elementColumnGridClass;
- }
- /**
- * @param string $elementColumnGridClass
- * @return $this
- */
- public function setElementColumnGridClass($elementColumnGridClass)
- {
- $this->elementColumnGridClass = $elementColumnGridClass;
- return $this;
- }
- /**
- * @return string
- */
- public function getElementDisplay()
- {
- return $this->elementDisplay;
- }
- /**
- * @param string $elementDisplay
- * @return $this
- */
- public function setElementDisplay($elementDisplay)
- {
- $this->elementDisplay = $elementDisplay;
- return $this;
- }
- /**
- * @return array
- */
- public function &getElementDisplayAttribs()
- {
- is_array($this->elementDisplayAttribs) or ($this->elementDisplayAttribs = (array) $this->elementDisplayAttribs);
- return $this->elementDisplayAttribs;
- }
- /**
- * @param array $elementDisplayAttribs
- * @param int $mergeType Indicates how new attributes should be merged with current attributes.
- * @return $this
- */
- public function setElementDisplayAttribs($elementDisplayAttribs, $mergeType = self::MERGE_TYPE_NONE)
- {
- $this->elementDisplayAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $elementDisplayAttribs : self::merge($mergeType, $this->elementDisplayAttribs, $elementDisplayAttribs);
- return $this;
- }
- /**
- * @return array
- */
- public function &getElementWrapperAttribs()
- {
- is_array($this->elementWrapperAttribs) or ($this->elementWrapperAttribs = (array) $this->elementWrapperAttribs);
- return $this->elementWrapperAttribs;
- }
- /**
- * @param array $elementWrapperAttribs
- * @param int $mergeType Indicates how new attributes should be merged with current attributes.
- * @return $this
- */
- public function setElementWrapperAttribs($elementWrapperAttribs, $mergeType = self::MERGE_TYPE_NONE)
- {
- $this->elementWrapperAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $elementWrapperAttribs : self::merge($mergeType, $this->elementWrapperAttribs, $elementWrapperAttribs);
- return $this;
- }
- /**
- * @return string
- */
- public function getFieldsetName()
- {
- return $this->fieldsetName;
- }
- /**
- * @param string $fieldsetName
- * @return $this
- */
- public function setFieldsetName($fieldsetName)
- {
- $this->fieldsetName = $fieldsetName;
- return $this;
- }
- /**
- * @return array
- */
- public function &getFormGroupAttribs()
- {
- is_array($this->formGroupAttribs) or ($this->formGroupAttribs = (array) $this->formGroupAttribs);
- return $this->formGroupAttribs;
- }
- /**
- * @param array $formGroupAttribs
- * @param int $mergeType Indicates how new attributes should be merged with current attributes.
- * @return $this
- */
- public function setFormGroupAttribs($formGroupAttribs, $mergeType = self::MERGE_TYPE_NONE)
- {
- $this->formGroupAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $formGroupAttribs : self::merge($mergeType, $this->formGroupAttribs, $formGroupAttribs);
- return $this;
- }
- /**
- * @return string
- */
- public function getHelpBlock()
- {
- return $this->helpBlock;
- }
- /**
- * @param string $helpBlock
- * @return $this
- */
- public function setHelpBlock($helpBlock)
- {
- $this->helpBlock = $helpBlock;
- return $this;
- }
- /**
- * @return array
- */
- public function &getHelpBlockAttribs()
- {
- is_array($this->helpBlockAttribs) or ($this->helpBlockAttribs = (array) $this->helpBlockAttribs);
- return $this->helpBlockAttribs;
- }
- /**
- * @param array $helpBlockAttribs
- * @param int $mergeType Indicates how new attributes should be merged with current attributes.
- * @return $this
- */
- public function setHelpBlockAttribs($helpBlockAttribs, $mergeType = self::MERGE_TYPE_NONE)
- {
- $this->helpBlockAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $helpBlockAttribs : self::merge($mergeType, $this->helpBlockAttribs, $helpBlockAttribs);
- return $this;
- }
- /**
- * @return array
- */
- public function &getInputGroupAttribs()
- {
- is_array($this->inputGroupAttribs) or ($this->inputGroupAttribs = (array) $this->inputGroupAttribs);
- return $this->inputGroupAttribs;
- }
- /**
- * @param array $inputGroupAttribs
- * @param int $mergeType Indicates how new attributes should be merged with current attributes.
- * @return $this
- */
- public function setInputGroupAttribs($inputGroupAttribs, $mergeType = self::MERGE_TYPE_NONE)
- {
- $this->inputGroupAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $inputGroupAttribs : self::merge($mergeType, $this->inputGroupAttribs, $inputGroupAttribs);
- return $this;
- }
- /**
- * @return array
- */
- public function &getLabelAttribs()
- {
- is_array($this->labelAttribs) or ($this->labelAttribs = (array) $this->labelAttribs);
- return $this->labelAttribs;
- }
- /**
- * @param array $labelAttribs
- * @param int $mergeType Indicates how new attributes should be merged with current attributes.
- * @return $this
- */
- public function setLabelAttribs($labelAttribs, $mergeType = self::MERGE_TYPE_NONE)
- {
- $this->labelAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $labelAttribs : self::merge($mergeType, $this->labelAttribs, $labelAttribs);
- return $this;
- }
- /**
- * @return string
- */
- public function getLabelColumnGridClass()
- {
- return $this->labelColumnGridClass;
- }
- /**
- * @param string $labelColumnGridClass
- * @return $this
- */
- public function setLabelColumnGridClass($labelColumnGridClass)
- {
- $this->labelColumnGridClass = $labelColumnGridClass;
- return $this;
- }
- /**
- * @return array
- */
- public function &getLabelLinkAttribs()
- {
- is_array($this->labelLinkAttribs) or ($this->labelLinkAttribs = (array) $this->labelLinkAttribs);
- return $this->labelLinkAttribs;
- }
- /**
- * @param array $labelLinkAttribs
- * @param int $mergeType Indicates how new attributes should be merged with current attributes.
- * @return $this
- */
- public function setLabelLinkAttribs($labelLinkAttribs, $mergeType = self::MERGE_TYPE_NONE)
- {
- $this->labelLinkAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $labelLinkAttribs : self::merge($mergeType, $this->labelLinkAttribs, $labelLinkAttribs);
- return $this;
- }
- /**
- * @return string
- */
- public function getTextAfter()
- {
- return $this->textAfter;
- }
- /**
- * @param string $textAfter
- * @return $this
- */
- public function setTextAfter($textAfter)
- {
- $this->textAfter = $textAfter;
- return $this;
- }
- /**
- * @return array
- */
- public function &getTextAfterAttribs()
- {
- is_array($this->textAfterAttribs) or ($this->textAfterAttribs = (array) $this->textAfterAttribs);
- return $this->textAfterAttribs;
- }
- /**
- * @param array $textAfterAttribs
- * @param int $mergeType Indicates how new attributes should be merged with current attributes.
- * @return $this
- */
- public function setTextAfterAttribs($textAfterAttribs, $mergeType = self::MERGE_TYPE_NONE)
- {
- $this->textAfterAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $textAfterAttribs : self::merge($mergeType, $this->textAfterAttribs, $textAfterAttribs);
- return $this;
- }
- /**
- * @return string
- */
- public function getTextBefore()
- {
- return $this->textBefore;
- }
- /**
- * @param string $textBefore
- * @return $this
- */
- public function setTextBefore($textBefore)
- {
- $this->textBefore = $textBefore;
- return $this;
- }
- /**
- * @return array
- */
- public function &getTextBeforeAttribs()
- {
- is_array($this->textBeforeAttribs) or ($this->textBeforeAttribs = (array) $this->textBeforeAttribs);
- return $this->textBeforeAttribs;
- }
- /**
- * @param array $textBeforeAttribs
- * @param int $mergeType Indicates how new attributes should be merged with current attributes.
- * @return $this
- */
- public function setTextBeforeAttribs($textBeforeAttribs, $mergeType = self::MERGE_TYPE_NONE)
- {
- $this->textBeforeAttribs = ($mergeType == self::MERGE_TYPE_NONE) ? $textBeforeAttribs : self::merge($mergeType, $this->textBeforeAttribs, $textBeforeAttribs);
- return $this;
- }
- /**
- * Decorates the form element and returns the decorated HTML.
- *
- * @param $element ElementInterface
- * @param $decorator FormElementDecorator
- *
- * @return string
- */
- public static function decorate($element, $decorator)
- {
- // get form ID and generate element ID
- $formID = $element->getForm() ? $element->getForm()->getID() : '';
- $elementName = $element->getName();
- $elementID = self::generateID($formID, 'el', $elementName);
- $isRadio = $element instanceof Radio;
- $isCheckbox = get_class($element) == Check::class;
- $isButton = $element instanceof Submit || $element instanceof Button;
- $isFile = $element instanceof File;
- // get config options
- $elementAttribs = $decorator->getElementAttribs();
- $elementWrapperAttribs = $decorator->getElementWrapperAttribs();
- $elementDisplayAttribs = $decorator->getElementDisplayAttribs();
- $labelAttribs = $decorator->getLabelAttribs();
- $labelLinkAttribs = $decorator->getLabelLinkAttribs();
- $textBeforeAttribs = $decorator->getTextBeforeAttribs();
- $textAfterAttribs = $decorator->getTextAfterAttribs();
- $addonBeforeAttribs = $decorator->getAddonBeforeAttribs();
- $addonAfterAttribs = $decorator->getAddonAfterAttribs();
- $helpBlockAttribs = $decorator->getHelpBlockAttribs();
- $inputGroupAttribs = $decorator->getInputGroupAttribs();
- $formGroupAttribs = $decorator->getFormGroupAttribs();
- // element config
- $elementAttribs = self::merge(
- self::MERGE_TYPE_CONCAT_CLASS,
- ['id' => $elementID],
- $element->getAttributes(),
- $elementAttribs
- );
- if (!$isButton && !$isRadio && !$isCheckbox) {
- self::appendToArrayValue($elementAttribs, 'class', 'form-control');
- }
- if ($isButton) {
- self::appendToArrayValue($elementAttribs, 'class', 'btn');
- }
- // element wrapper config
- if ($element instanceof Time) {
- self::appendToArrayValue($elementWrapperAttribs, 'class', 'bootstrap-timepicker');
- }
- // label config
- $labelAttribs = self::merge(
- self::MERGE_TYPE_CONCAT_CLASS,
- [
- 'for' => $elementID,
- 'class' => $decorator->getLabelColumnGridClass()
- ],
- $labelAttribs
- );
- // label link config
- $hasLabelLink = (bool)count($labelLinkAttribs);
- // text before config
- self::appendToArrayValue($textBeforeAttribs, 'class', 'form-control-static');
- // text after config
- self::appendToArrayValue($textAfterAttribs, 'class', 'form-control-static');
- // addon before config
- self::appendToArrayValue($addonBeforeAttribs, 'class', 'input-group-addon');
- // addon after config
- self::appendToArrayValue($addonAfterAttribs, 'class', 'input-group-addon');
- // help block config
- self::appendToArrayValue($helpBlockAttribs, 'class', 'help-block');
- // input group options
- $hasInputGroup = (bool)($decorator->getAddonBefore() || $decorator->getAddonAfter());
- self::appendToArrayValue($inputGroupAttribs, 'class', 'input-group');
- // form group options
- $formGroupAttribs = self::merge(
- self::MERGE_TYPE_CONCAT_CLASS,
- [
- 'class' => 'form-group ' . ($elementName . '-form-group')
- ],
- $formGroupAttribs
- );
- // final config modifications
- if ($hasInputGroup) {
- self::appendToArrayValue($inputGroupAttribs, 'class', $decorator->getElementColumnGridClass());
- } else {
- if ($isRadio || $isCheckbox) {
- self::appendToArrayValue($elementWrapperAttribs, 'class', $isRadio ? 'radio' : 'checkbox');
- }
- self::appendToArrayValue($elementWrapperAttribs, 'class', $decorator->getElementColumnGridClass());
- }
- $genTextBeforeHtml = function () use (
- $element, $decorator, $textBeforeAttribs
- ) {
- $html = null;
- if ($textBefore = $decorator->getTextBefore()) {
- $html .= Tag::tagHtml('p', $textBeforeAttribs); // text before open
- $html .= $textBefore; // text before
- $html .= Tag::tagHtmlClose('p'); // text before close
- }
- return $html;
- };
- $genTextAftereHtml = function () use (
- $element, $decorator, $textAfterAttribs
- ) {
- $html = null;
- if ($textAfter = $decorator->getTextAfter()) {
- $html .= Tag::tagHtml('p', $textAfterAttribs); // text after open
- $html .= $textAfter; // text after
- $html .= Tag::tagHtmlClose('p'); // text after close
- }
- return $html;
- };
- $genLabelHtml = function () use (
- $element, $decorator,
- $isCheckbox, $isRadio,
- $labelAttribs, $labelLinkAttribs, $hasLabelLink
- ) {
- $html = null;
- if ($label = $element->getLabel()) {
- $html .= Tag::tagHtml('label', $labelAttribs); // label open
- if ($hasLabelLink) {
- $html .= Tag::tagHtml('a', $labelLinkAttribs); // label link open
- }
- $html .= $label; // label
- if ($hasLabelLink) {
- $html .= Tag::tagHtmlClose('a'); // label link close
- }
- $html .= Tag::tagHtmlClose('label'); // label close
- }
- return $html;
- };
- $genElementHtml = function () use (
- $element, $decorator, $elementAttribs, $elementDisplayAttribs, $isRadio, $isCheckbox
- ) {
- $html = null;
- // if radio or checkbox wrap in label, and print display option
- if ($isRadio || $isCheckbox) {
- $class = $isRadio ? 'radio-custom' : 'checkbox-custom';
- $html .= Tag::tagHtml('label class="'.$class.'"'); // element wrapper open
- $html .= $element->render($elementAttribs) . PHP_EOL; // element
- if ($isCheckbox)
- {
- $html .= Tag::tagHtml('i class="fa fa-fw fa-square-o"').Tag::tagHtmlClose('i');
- }
- elseif ($isRadio)
- {
- $html .= Tag::tagHtml('i class="fa fa-circle-o"').Tag::tagHtmlClose('i');
- }
- if ($elementDisplay = $decorator->getElementDisplay()) {
- $html .= Tag::tagHtml('span', $elementDisplayAttribs); // element display open
- $html .= $elementDisplay; // element display
- $html .= Tag::tagHtmlClose('span'); // element display close
- }
- $html .= Tag::tagHtmlClose('label'); // element wrapper close
- } else {
- $html .= $element->render($elementAttribs); // element
- }
- return $html;
- };
- $genInputGroupHtml = function ($innerHtml) use (
- $element, $decorator, $isFile,
- $inputGroupAttribs, $elementWrapperAttribs,
- $addonBeforeAttribs, $addonAfterAttribs
- ) {
- $html = null;
- $hasInputGroup = (bool) ($decorator->getAddonBefore() || $decorator->getAddonAfter());
- $isAjaxFile = (get_class($element) == AjaxFile::class) ? true : false;
- if ($hasInputGroup) {
- $html .= Tag::tagHtml('div', $inputGroupAttribs); // input group open
- if ($addonBefore = $decorator->getAddonBefore()) {
- $html .= Tag::tagHtml('div', $addonBeforeAttribs); // addon before open
- $html .= $addonBefore; // addon before value
- $html .= Tag::tagHtmlClose('div'); // addon before close
- }
- $html .= $innerHtml; // inner html
- if ($addonAfter = $decorator->getAddonAfter()) {
- $html .= Tag::tagHtml('div', $addonAfterAttribs); // addon after open
- $html .= $addonAfter; // addon after value
- $html .= Tag::tagHtmlClose('div'); // addon after close
- }
- $html .= Tag::tagHtmlClose('div'); // input group close
- } elseif ($isFile && !$isAjaxFile) {
- $html = Tag::tagHtml('div')
- . Tag::tagHtml('div class="fileinput fileinput-new" data-provides="fileinput" ')
- . Tag::tagHtml('span', $elementWrapperAttribs)
- . '<span class="fileinput-new">Select File</span>'
- . '<span class="fileinput-exists">Change</span>'
- . $innerHtml . $isAjaxFile
- . Tag::tagHtmlClose('span')
- . '<span class="fileinput-filename"></span>'
- . '<a href="#" class="close fileinput-exists" data-dismiss="fileinput" style="float: none"><i class="icon-close"></i></a>'
- . Tag::tagHtmlClose('div')
- . Tag::tagHtmlClose('div');
- } else {
- // if there's no input group, create a wrapper
- $html = Tag::tagHtml('div', $elementWrapperAttribs) . $innerHtml . Tag::tagHtmlClose('div');
- }
- return $html;
- };
- $genFormGroupHtml = function() use (
- $decorator,
- $isRadio, $isCheckbox, $formGroupAttribs, $helpBlockAttribs,
- $genLabelHtml, $genElementHtml, $genInputGroupHtml, $genTextBeforeHtml, $genTextAftereHtml
- ) {
- $html = null;
- $html .= Tag::tagHtml('div', $formGroupAttribs); // form group open
- $html .= $genLabelHtml(); // label
- $html .= $genTextBeforeHtml(); // text before
- $html .= $genInputGroupHtml($genElementHtml()); // element and input group
- $html .= $genTextAftereHtml(); // text after
- if ($helpBlock = $decorator->getHelpBlock()) {
- $html .= Tag::tagHtml('p', $helpBlockAttribs); // help block open
- $html .= $helpBlock; // help block
- $html .= Tag::tagHtmlClose('p'); // help block close
- }
- $html .= Tag::tagHtmlClose('div'); // form group close
- return $html;
- };
- $html = null;
- // generate form-group
- $html = $genFormGroupHtml();
- if ($postDecorateCallback = $decorator->getPostDecorateCallback()) {
- $html = $postDecorateCallback($html);
- }
- return $html . PHP_EOL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement