Esempio n. 1
0
    /**
     * Render form with ajax submit
     * @see \Zend\Form\View\Helper\Form::render()
     * @param \Zend\Form\FormInterface $oForm
     * @param string $sFormLayout : default 'horizontal'
     * @return string
     */
    public function renderForAjax(\Zend\Form\FormInterface $oForm, $sFormLayout = \TwbBundle\Form\View\Helper\TwbBundleForm::LAYOUT_HORIZONTAL, $bAjax = false)
    {
        $sAfter = '
			if(document.id){
				try{
		';
        if (!$oForm->getAttribute('action')) {
            $oForm->setAttribute('action', $this->getRequest()->getUri()->normalize());
        }
        if (!$oForm->getAttribute('id')) {
            $oForm->setAttribute('id', $oForm->getName());
        }
        if ($oForm->getAttribute('enctype') === 'multipart/form-data') {
            $sAfter .= '
					var eForm = document.id(' . $this->getEscapeJsonHelper()->__invoke($oForm->getAttribute('id')) . ');
					eForm.iFrameFormRequest({
				        onRequest: function(){
							if(eForm.validate())eForm.spin();
						},
						onComplete: function(sText){
							var sJavascript = null;
							var sHtml =	sText.stripScripts(function(sScript){
								sJavascript = sScript;
							});
							var aMatches = sHtml.match(/<body[^>]*>([\\s\\S]*?)<\\/body>/i);
							if(aMatches)sHtml = aMatches[1];
				           	var eContainer = eForm.getParent().empty().set(\'html\',sHtml);
							if(sJavascript)Browser.exec(sJavascript);
							eForm.unspin();
							window.behavior.apply(eContainer.getParent(),true);
				        },
				        onFailure: function(){
				        	alert(oController.translate(\'error_occurred\'));
				        }
				    });
		';
        } else {
            $sAfter .= '
					var eForm = document.id(' . $this->getEscapeJsonHelper()->__invoke($oForm->getAttribute('id')) . ');
					eForm.get(\'validator\').addEvent(\'formValidate\',function(bIsValid){
						if(bIsValid)new Form.Request(eForm,eForm.getParent()).send();
					});
			';
            $oForm->setAttribute('onsubmit', 'return false;');
        }
        $sAfter .= '
				}
			    catch(oException){
					if(oController != null)alert(' . $this->getEscapeJsonHelper()->__invoke($this->getTranslator()->translate('error_occurred')) . ');
	    		}
			}
		';
        return parent::render($oForm, $sFormLayout) . PHP_EOL . '<script type="text/javascript">' . $sAfter . '</script>';
    }
 /**
  * Outputs message depending on flag
  *
  * @param FormInterface $form
  * @param array         $staticElements
  * @param null          $formClass
  *
  * @return string
  */
 public function __invoke(FormInterface $form, array $staticElements = [], $formClass = null)
 {
     $submitElements = [];
     /** @var Form $form */
     $form->setAttribute('role', 'form');
     if ($formClass == 'form-inline') {
         $form->setAttribute('class', $formClass . ' pull-right');
     } elseif ($formClass) {
         $form->setAttribute('class', $formClass);
     } else {
         $formClass = $form->getAttribute('class');
     }
     $form->prepare();
     $output = $this->getView()->form()->openTag($form);
     foreach ($staticElements as $element) {
         $viewModel = new ViewModel();
         $viewModel->setVariable('label', $element['label']);
         $viewModel->setVariable('value', $element['value']);
         $viewModel->setTemplate('travello-view-helper/widget/bootstrap-form-static');
         $output .= $this->getView()->render($viewModel);
     }
     list($output, $submitElements) = $this->renderElements($form, $formClass, $output, $submitElements);
     if ($formClass == 'form-inline') {
         $template = 'bootstrap-form-submit-inline';
     } else {
         $template = 'bootstrap-form-submit';
     }
     $viewModel = new ViewModel();
     $viewModel->setVariable('submitElements', $submitElements);
     $viewModel->setTemplate('travello-view-helper/widget/' . $template);
     $output .= $this->getView()->render($viewModel);
     $output .= $this->getView()->form()->closeTag();
     return $output;
 }
 /**
  * Render single element
  * 
  * @access public
  * @param FormInterface $form
  * @param Zend\Form\Element $element
  * @return string element HTML content
  */
 public function renderElement($form, $element)
 {
     $inlineForm = false;
     if (strpos($form->getAttribute('class'), "form-horizontal") === false) {
         $inlineForm = true;
     }
     $elementContent = '';
     // add required class to all required elements
     if (!empty($element->getAttribute('required')) && !$element->getLabelOption("disable_html_escape")) {
         $labelAttributes = $element->getLabelAttributes();
         $labelClass = isset($labelAttributes["class"]) ? $labelAttributes["class"] : "";
         $labelAttributes["class"] = $labelClass . " required";
         $element->setLabelAttributes($labelAttributes);
     }
     // Add Id to all form elements
     // When element has an Id, Label tag won't enclose form element
     if (empty($element->getAttribute('id'))) {
         $element->setAttribute('id', $form->getAttribute('name') . "_" . $element->getAttribute('name'));
     }
     $labelAbsent = false;
     $formElementAppendString = '';
     if (empty($element->getLabel()) && $element->getAttribute('type') !== "hidden") {
         $labelAbsent = true;
     }
     if ($labelAbsent === true && (strpos($element->getAttribute('class'), "btn") === false || strpos($element->getAttribute('class'), "btn") !== false && strpos($element->getAttribute('class'), "pull") === false) && $inlineForm === false) {
         $elementContent .= "<dt>&nbsp;</dt>";
     } else {
         $divAttributes = "";
         if ($inlineForm === true) {
             $divAttributes = "class='form-group'";
         }
         $elementContent .= "<div {$divAttributes} >";
         $formElementAppendString = '</div>';
     }
     // Change submit button text to edit if form is an edit form
     if ($element instanceof Submit && $form->isEditForm === true) {
         if (property_exists($form, "isAdminUser") && $form->isAdminUser === false && $form->needAdminApproval === true) {
             $element->setValue(FormButtons::SUBMIT_FOR_ADMIN_APPROVAL_BUTTON_TEXT);
         } elseif ($element->getValue() == FormButtons::CREATE_BUTTON_TEXT) {
             $element->setValue(FormButtons::EDIT_BUTTON_TEXT);
         }
     }
     $elementContent .= $this->getView()->formRow($element);
     $elementContent .= $formElementAppendString;
     return $elementContent;
 }
 /**
  * Render a form from the provided $form,
  *
  * @param FormInterface $form
  * @param string $layout
  * @param array $parameter
  * @return string
  */
 public function renderBare(FormInterface $form, $layout = self::LAYOUT_INLINE, $parameter = array())
 {
     $renderer = $this->getView();
     $headscript = $renderer->plugin('headscript');
     $basepath = $renderer->plugin('basepath');
     $headscript->appendFile($basepath('Core/js/core.spinnerbutton.js'))->appendFile($basepath('js/select2.min.js'))->appendFile($basepath('Core/js/core.forms.js'));
     $renderer->headLink()->appendStylesheet($basepath('css/select2.css'))->appendStylesheet($basepath('css/select2-bootstrap.css'));
     if ($scripts = $form->getOption('headscript')) {
         if (!is_array($scripts)) {
             $scripts = array($scripts);
         }
         foreach ($scripts as $script) {
             $headscript->appendFile($basepath($script));
         }
     }
     $class = $form->getAttribute('class');
     $class = preg_replace('~\\bform-[^ ]+\\b~', '', $class);
     $class .= ' ' . $layout;
     $form->setAttribute('class', $class);
     if (method_exists($form, 'prepare')) {
         $form->prepare();
     }
     $formContent = '';
     if ($form instanceof ViewPartialProviderInterface) {
         return $this->getView()->partial($form->getViewPartial(), array('element' => $form));
     }
     foreach ($form as $element) {
         $parameterPartial = $parameter;
         if (!$element->hasAttribute('id')) {
             $elementId = preg_replace(array('~[^A-Za-z0-9_-]~', '~--+~', '~^-|-$~'), array('-', '-', ''), $form->getName() . '-' . $element->getName());
             $element->setAttribute('id', $elementId);
         }
         if ($element instanceof ExplicitParameterProviderInterface) {
             $parameterPartial = array_merge($element->getParams(), $parameterPartial);
         }
         if ($element instanceof ViewPartialProviderInterface) {
             $parameterPartial = array_merge(array('element' => $element, 'layout' => $layout), $parameterPartial);
             $formContent .= $this->getView()->partial($element->getViewPartial(), $parameterPartial);
         } else {
             if ($element instanceof ViewHelperProviderInterface) {
                 $helper = $element->getViewHelper();
                 if (is_string($helper)) {
                     $helper = $this->getView()->plugin($helper);
                 }
                 $formContent .= $helper($element);
             } else {
                 if ($element instanceof FieldsetInterface) {
                     $formContent .= $this->getView()->formCollection($element, true, $layout);
                 } else {
                     $formContent .= $this->getView()->formRow($element, null, null, $layout);
                 }
             }
         }
     }
     return $this->openTag($form) . $formContent . $this->closeTag();
 }
Esempio n. 5
0
 /**
  * @param FormInterface|null $form
  */
 private function setHorizontal($form)
 {
     if ($this->isHorizontal && $form !== null) {
         if ($form->hasAttribute('class')) {
             $form->setAttribute('class', 'form-horizontal ' . $form->getAttribute('class'));
         } else {
             $form->setAttribute('class', 'form-horizontal');
         }
     }
 }
Esempio n. 6
0
 /**
  * @param string $content
  * @param array $attribs
  * @param ElementInterface $element
  * @param FormInterface $form
  * @return string
  */
 public function render($content, array $attribs = [], ElementInterface $element = null, FormInterface $form = null)
 {
     if ($form && $form->hasAttribute('class')) {
         $class = $form->getAttribute('class');
         if (strpos($class, 'form-inline') !== false) {
             return $content;
         }
     }
     return parent::render($content, $attribs);
 }
Esempio n. 7
0
 /**
  * Sets form layout class
  *
  * @param FormInterface $oForm
  * @param string $sFormLayout
  * @return void
  */
 protected function setFormClass(FormInterface $oForm, $sFormLayout = self::LAYOUT_HORIZONTAL)
 {
     if (is_string($sFormLayout)) {
         $sLayoutClass = 'form-' . $sFormLayout;
         if ($sFormClass = $oForm->getAttribute('class')) {
             if (!preg_match('/(\\s|^)' . preg_quote($sLayoutClass, '/') . '(\\s|$)/', $sFormClass)) {
                 $oForm->setAttribute('class', trim($sFormClass . ' ' . $sLayoutClass));
             }
         } else {
             $oForm->setAttribute('class', $sLayoutClass);
         }
     }
 }
Esempio n. 8
0
File: Form.php Progetto: coolms/twbs
 /**
  * {@inheritDoc}
  */
 public function openTag(FormInterface $form = null)
 {
     if ($form) {
         if (!$form->hasAttribute('role')) {
             $form->setAttribute('role', 'form');
         }
         $class = $form->getAttribute('class');
         if (strpos($class, 'form-horizontal') === false && strpos($class, 'form-inline') === false) {
             $form->setAttribute('class', trim("{$this->defaultClass} {$class}"));
         }
     }
     return parent::openTag($form);
 }
Esempio n. 9
0
 /**
  * @param string $content
  * @param array $attribs
  * @param ElementInterface $element
  * @param FormInterface $form
  * @return string
  */
 public function render($content, array $attribs = [], ElementInterface $element = null, FormInterface $form = null)
 {
     if ($form && $form->hasAttribute('class')) {
         $class = $form->getAttribute('class');
         if (strpos($class, 'form-inline') !== false) {
             return $content;
         }
         if (strpos($class, 'form-horizontal') !== false && empty($attribs['class']) && $form->has('submit') && $form->has('reset')) {
             if ($form->get('submit') === $element) {
                 $attribs['class'] = 'col-xs-12 col-sm-8';
             } elseif ($form->get('reset') === $element) {
                 $attribs['class'] = 'col-xs-12 col-sm-4';
             }
         }
     }
     return parent::render($content, $attribs);
 }
Esempio n. 10
0
File: Btn.php Progetto: coolms/twbs
 /**
  * {@inheritDoc}
  */
 public function render($content, array $attribs = [], ElementInterface $element = null, FormInterface $form = null)
 {
     $renderAsBlock = $this->renderAsBlock;
     if ($form && $renderAsBlock) {
         $class = $form->getAttribute('class');
         if (strpos($class, 'form-inline') !== false || strpos($class, 'form-horizontal') === false) {
             $renderAsBlock = false;
         }
     }
     if ($renderAsBlock) {
         if (empty($attribs['class'])) {
             $attribs['class'] = 'btn-block';
         } elseif (strpos($attribs['class'], 'btn-block') === false && strpos($attribs['class'], 'btn-inline') === false) {
             $attribs['class'] = $attribs['class'] . ' btn-block';
         }
     }
     return parent::render($content, $attribs, $element, $form);
 }
Esempio n. 11
0
 /**
  * @param FormInterface $form
  */
 protected function detectBootstrapType(FormInterface $form)
 {
     $class = $form->getAttribute('class');
     $class = $class ? $class : '';
     switch (true) {
         case strpos($class, self::HORIZONTAL) !== FALSE:
             self::$boostrapType = self::HORIZONTAL;
             break;
         case strpos($class, self::SEARCH) !== FALSE:
             self::$boostrapType = self::SEARCH;
             break;
         case strpos($class, self::INLINE) !== FALSE:
             self::$boostrapType = self::INLINE;
             break;
         default:
             self::$boostrapType = self::VERTICAL;
     }
 }
Esempio n. 12
0
 /**
  * Generate an opening form tag
  * @param  null|FormInterface $form
  * @param null|string $formType
  * @param array $displayOptions
  * @throws \DluTwBootstrap\Form\Exception\UnsupportedFormTypeException
  * @return string
  */
 public function openTag(FormInterface $form = null, $formType = null, $displayOptions = array())
 {
     $formType = $this->formUtil->filterFormType($formType);
     if (!array_key_exists($formType, $this->formTypeMap)) {
         throw new UnsupportedFormTypeException("Unsupported form type '{$formType}'.");
     }
     if ($form) {
         $class = $this->genUtil->addWords($this->formTypeMap[$formType], $form->getAttribute('class'));
         if (array_key_exists('class', $displayOptions)) {
             $class = $this->genUtil->addWords($displayOptions['class'], $class);
         }
         $escapeHtmlAttrHelper = $this->getEscapeHtmlAttrHelper();
         $class = $this->genUtil->escapeWords($class, $escapeHtmlAttrHelper);
         $form->setAttribute('class', $class);
     }
     return parent::openTag($form);
 }
Esempio n. 13
0
 /**
  * Render a form from the provided $form,
  *
  * @param FormInterface $form
  * @param string $layout
  * @param array $parameter
  * @return string
  */
 public function renderBare(FormInterface $form, $layout = self::LAYOUT_HORIZONTAL, $parameter = array())
 {
     /* @var $renderer \Zend\View\Renderer\PhpRenderer
      * @var $headscript \Zend\View\Helper\HeadScript
      * @var $basepath \Zend\View\Helper\BasePath */
     $renderer = $this->getView();
     $headscript = $renderer->plugin('headscript');
     $basepath = $renderer->plugin('basepath');
     $headscript->appendFile($basepath('Core/js/core.spinnerbutton.js'))->appendFile($basepath('js/select2.min.js'))->appendFile($basepath('Core/js/core.forms.js'));
     /* @noinspection PhpParamsInspection */
     $renderer->headLink()->appendStylesheet($basepath('css/select2.css'));
     if ($scripts = $form->getOption('headscript')) {
         if (!is_array($scripts)) {
             $scripts = array($scripts);
         }
         foreach ($scripts as $script) {
             $headscript->appendFile($basepath($script));
         }
     }
     $class = $form->getAttribute('class');
     $class = preg_replace('~\\bform-[^ ]+\\b~', '', $class);
     $class .= ' ' . $layout;
     $form->setAttribute('class', $class);
     $formId = $form->getAttribute('id') ?: $form->getName();
     $form->setAttribute('id', preg_replace(array('~[^A-Za-z0-9_-]~', '~--+~', '~^-|-$~'), array('-', '-', ''), $formId));
     if (method_exists($form, 'prepare')) {
         $form->prepare();
     }
     $formContent = '';
     if ($form instanceof ViewPartialProviderInterface) {
         return $renderer->partial($form->getViewPartial(), array('element' => $form));
     }
     /* @var $element \Zend\Form\ElementInterface */
     foreach ($form as $element) {
         $parameterPartial = $parameter;
         if (!$element->hasAttribute('id')) {
             $elementId = preg_replace(array('~[^A-Za-z0-9_-]~', '~--+~', '~^-|-$~'), array('-', '-', ''), $element->getName());
             $element->setAttribute('id', $elementId);
         }
         if ($element instanceof ExplicitParameterProviderInterface) {
             /* @var $element ExplicitParameterProviderInterface */
             $parameterPartial = array_merge($element->getParams(), $parameterPartial);
         }
         if ($element instanceof ViewPartialProviderInterface) {
             /* @var $element ViewPartialProviderInterface */
             $parameterPartial = array_merge(array('element' => $element, 'layout' => $layout), $parameterPartial);
             /** @noinspection PhpToStringImplementationInspection */
             $formContent .= $renderer->partial($element->getViewPartial(), $parameterPartial);
         } elseif ($element instanceof FieldsetInterface) {
             if ($element instanceof ViewHelperProviderInterface) {
                 /* @var $element ViewHelperProviderInterface */
                 $helper = $element->getViewHelper();
                 if (is_string($helper)) {
                     $helper = $renderer->plugin($helper);
                 }
                 $formContent .= $helper($element);
             } else {
                 $formContent .= $renderer->formCollection($element, true, $layout);
             }
         } elseif (false !== $element->getOption('use_formrow_helper')) {
             $formContent .= $renderer->formRow($element, null, null, $layout);
         } else {
             $formContent .= $renderer->formElement($element);
         }
     }
     return $this->openTag($form) . $formContent . $this->closeTag();
 }
Esempio n. 14
0
 /**
  * {@inheritDoc}
  */
 public function openTag(FormInterface $form = null)
 {
     if ($form) {
         $class = $form->getAttribute('class');
         if (strpos($class, $this->defaultClass) === false) {
             $class = trim("{$class} {$this->defaultClass}");
         }
         if ($object = $form->getObject()) {
             $className = str_replace(array_keys($this->classNameReplacements), array_values($this->classNameReplacements), get_class($object));
         } else {
             $className = get_class($form);
         }
         $parts = explode('\\', $className);
         foreach ($parts as $part) {
             $classes[] = strtolower($part);
             if (count($classes) > 1) {
                 $class .= ' ' . implode('-', $classes);
             }
         }
         $form->setAttribute('class', $class);
         $formAttributes = $form->getAttributes();
         if (!array_key_exists('id', $formAttributes) && $classes) {
             $form->setAttribute('id', implode('-', $classes));
         }
     }
     return parent::openTag($form);
 }