/** * Generate a generic form * @param objForm $form * @param \Zend\Mvc\View\View $view * @param array $arr_options - Optional * @return string */ public function __invoke($form, $view, $arr_options = array("appendJavaScriptUtils" => TRUE)) { //assign view as class variable $this->view = $view; //set form class if ($form->getAttribute("class") == "") { $form->setAttribute("class", "form-vertical"); } //end class if (strtolower($form->getAttribute("id")) == "form") { $form->setAttribute("id", "form-id"); } //end if //analyse options if (is_array($arr_options)) { //remove specified elements if (array_key_exists("arr_remove_elements", $arr_options)) { foreach ($arr_options["arr_remove_elements"] as $field) { if ($form->has($field) === TRUE) { $form->remove($field); } //end if } //end foreach } //end if if ($arr_options["generate_field_groups"] === TRUE) { $form_html = $this->generateFormWithFieldGroups($form, $view, $arr_options); //check if form has been generated if (!$form_html) { //generate normal form, grouped form could not be created } else { return $form_html; } //end if } //end if } //end if if ($form->hasAttribute("arr_field_groups") === TRUE) { $form->removeAttribute("arr_field_groups"); $form->remove("arr_field_groups"); } //end if $html = "<div class=\"col-md-6\">"; //open the form tag $html .= $view->form()->openTag($form); //add help toggle button if ($arr_options["disable_help_button"] !== TRUE) { $html .= "<span id=\"frm-toggle-help\" class=\"btn btn-warning\" title=\"Toggle Help\" data-toggle=\"tooltip\">"; $html .= "<span class=\"glyphicon glyphicon-question-sign\"></span>"; $html .= "</span>"; } //end if //custom form element error structure $view->formElementErrors()->setMessageOpenFormat('<div class="bg-danger help-inline" style="padding: 10px; margin-top: 5px; margin-bottom: 5px;">')->setMessageSeparatorString('</div><div>')->setMessageCloseString('</div>'); foreach ($form as $form_element) { $html .= $this->generateFieldHtml($form, $form_element->getName(), $arr_options); } //end foreach //add submit button if ($form->has("submit")) { if ($form->get("submit")->getValue() == "") { //set display ttitle $form->get("submit")->setAttribute("value", "Save"); } else { ////overwrite button value received from API $form->get("submit")->setAttribute("value", "Save"); } //end if $form->get("submit")->setAttribute("class", "btn btn-primary"); $html .= $view->formSubmit($form->get("submit")); } //end if //close the form $html .= $view->form()->closeTag(); $html .= "</div>"; //append js utils if (isset($arr_options["appendJavaScriptUtils"]) && $arr_options["appendJavaScriptUtils"] === TRUE) { $html .= $this->appendJavaScriptUtils($arr_options); } //end if return $html; }
/** * Generate a form with field Groups * @param objForm $form * @param \Zend\Mvc\View\View $view * @param array $arr_options - Optional * @return mixed */ private function generateFormWithFieldGroups($form, $view, $arr_options = NULL) { //extract field groups $arr_field_groups = (array) $form->getAttribute("arr_field_groups"); $form->remove("arr_field_groups"); if (!is_array($arr_field_groups)) { return FALSE; } //end if //set unique id $java_id = str_replace(".", "", microtime(TRUE)); //open html string $html = ""; //check if accordion must be enabled if ($arr_options["enable_accordion"] === TRUE) { //add javascript to load accodian plugin $html .= "<script type=\"text/javascript\">"; $html .= "jQuery(document).ready(function () {\n\t\t\t\t\t\t\tjQuery(\".{$java_id}\").mj_accordion();\n\t\t\t\t\t\t });"; $html .= "</script>"; } //end if //set form class attribute $form->setAttribute("class", "form"); //open the form tag $html .= $view->form()->openTag($form); //add accordion div element $html .= "<div class=\"{$java_id} style-js-accordion\">"; //custom form element error structure $view->formElementErrors()->setMessageOpenFormat('<div class="bg-danger help-inline" style="padding: 10px; margin-top: 5px; margin-bottom: 5px;">')->setMessageSeparatorString('</div><div>')->setMessageCloseString('</div>'); foreach ($arr_field_groups as $key => $arr_fields) { //make sure that this groups does contain fields if (count($arr_fields) == 0) { continue; } //end if $html .= "<h3>{$key}</h3>"; $html .= "<div class=\"accordion-section\">"; foreach ($arr_fields as $section_internal_group => $field) { //check for grouped fields within section if (is_array($field)) { //create fieldset $html .= "<fieldset><legend>{$section_internal_group}</legend>"; foreach ($field as $group => $grouped_field) { $html .= $this->generateFieldHtml($form, $grouped_field); } //end foreach //close fieldset $html .= "</fieldset>"; } else { $html .= $this->generateFieldHtml($form, $field); } //end if } //end foreach //$html .= "</div>"; Commented out by Lodi } //end foreach //collapse field group array foreach ($arr_field_groups as $key => $arr_fields) { foreach ($arr_fields as $field) { if (is_array($field)) { foreach ($field as $grouped_field) { $arr_collapsed_field_groups[] = $grouped_field; } //end foreach } else { $arr_collapsed_field_groups[] = $field; } //end if } //end foreach } //end foreach //make sure that all elements have been rendered $objElements = $form->getElements(); foreach ($objElements as $field => $objElement) { if (!in_array($field, $arr_collapsed_field_groups)) { $html .= $this->generateFieldHtml($form, $field); continue; } //end if } //end foreach //close accordion div $html .= "</div><br/>"; if ($form->has("submit")) { if ($form->get("submit")->getValue() == "") { $form->get("submit")->setAttribute("class", "submit"); $form->get("submit")->setValue("Submit"); } //end if $html .= $view->formSubmit($form->get("submit")); } //end if //close the form $html .= $view->form()->closeTag(); return $html; }