/** * renders the form * @param string $override_output_type output type * @return string the form html */ public function render($override_output_type = NULL) { $output = ''; $errors = ''; $highlights = ''; $fields_html = ''; // render needs the form to be processed if (!$this->processed) { $this->process(); } if (!is_string($override_output_type)) { $override_output_type = NULL; } $output_type = !empty($override_output_type) ? $override_output_type : $this->get_output_type(); $output_type = trim(strtolower($output_type)); if ($output_type == 'json' && empty($this->ajax_submit_url)) { $output_type = 'html'; } if ($this->valid() === FALSE) { $errors = $this->show_errors(); $this->set_attribute('class', trim($this->get_attribute('class') . ' with-errors')); if (!$this->errors_inline()) { foreach ($this->get_fields($this->current_step) as $field) { $errors .= $field->show_errors(); } } if (trim($errors) != '') { $errors = sprintf(FORMS_ERRORS_TEMPLATE, $errors); } } if ($this->has_highlights()) { $highlights = $this->show_highlights(); if (trim($highlights) != '') { $highlights = sprintf(FORMS_HIGHLIGHTS_TEMPLATE, $highlights); } } $insertorder = array_flip($this->insert_field_order); $weights = array(); foreach ($this->get_fields($this->current_step) as $key => $elem) { $weights[$key] = $elem->get_weight(); $order[$key] = $insertorder[$key]; } if (count($this->get_fields($this->current_step)) > 0) { array_multisort($weights, SORT_ASC, $order, SORT_ASC, $this->get_fields($this->current_step)); } foreach ($this->get_fields($this->current_step) as $name => $field) { if (is_object($field) && method_exists($field, 'render')) { $fields_html .= $field->render($this); } } $attributes = $this->get_attributes(array('action', 'method', 'id')); $js = $this->generate_js(); if (cs_form::is_partial()) { // ajax request - form item event $jsondata = json_decode($_REQUEST['jsondata']); $callback = $jsondata->callback; if (is_callable($callback)) { $target_elem = $callback($this); $html = $target_elem->render($this); if (count($target_elem->get_css()) > 0) { $html .= '<style>' . implode("\n", $target_elem->get_css()) . "</style>"; } $js = ''; if (count($target_elem->get_js()) > 0) { $js = "(function(\$){\n" . "\t\$(document).ready(function(){\n" . "\t\t" . implode(";\n\t\t", $target_elem->get_js()) . ";\n" . "\t});\n" . "})(jQuery);"; } return json_encode(array('html' => $html, 'js' => $js)); } return FALSE; } if (!empty($this->ajax_submit_url) && $this->get_output_type() == 'json' && $output_type == 'html') { // print initial js for ajax form $output = "<script type=\"text/javascript\">" . preg_replace("/\\s+/", " ", str_replace("\n", "", "(function(\$){\n \$(document).ready(function(){\n var {$this->get_id()}_attachFormBehaviours = function (){\n \$('#{$this->get_id()}').submit(function(evt){\n evt.preventDefault();\n \$.post( \"{$this->get_ajax_url()}\", \$('#{$this->get_id()}').serialize(), function( data ) {\n var response;\n if(typeof data =='object') response = data;\n else response = \$.parseJSON(data);\n \$('#{$this->get_id()}-formcontainer').html('');\n \$(response.html).appendTo( \$('#{$this->get_id()}-formcontainer') );\n if( \$.trim(response.js) != '' ){\n eval( response.js );\n };\n {$this->get_id()}_attachFormBehaviours();\n });\n return false;\n });\n };\n \$.getJSON('{$this->get_ajax_url()}',function(response){\n \$(response.html).appendTo( \$('#{$this->get_id()}-formcontainer') );\n if( \$.trim(response.js) != '' ){\n eval( response.js );\n };\n {$this->get_id()}_attachFormBehaviours();\n });\n });\n })(jQuery);")) . "</script>\n" . "<div id=\"{$this->get_id()}-formcontainer\"></div>"; } else { switch ($output_type) { case 'json': $output = array('html' => '', 'js' => '', 'is_submitted' => $this->is_submitted()); $output['html'] = $this->get_element_prefix(); $output['html'] .= $this->get_prefix(); $output['html'] .= $highlights; $output['html'] .= $errors; $output['html'] .= "<form action=\"{$this->action}\" id=\"{$this->form_id}\" method=\"{$this->method}\"{$attributes}>\n"; $output['html'] .= $fields_html; $output['html'] .= "<input type=\"hidden\" name=\"form_id\" value=\"{$this->form_id}\" />\n"; if (!$this->no_token) { $output['html'] .= "<input type=\"hidden\" name=\"form_token\" value=\"{$this->form_token}\" />\n"; } if ($this->get_num_steps() > 1) { $output['html'] .= "<input type=\"hidden\" name=\"current_step\" value=\"{$this->current_step}\" />\n"; } $output['html'] .= "</form>\n"; $output['html'] .= $this->get_suffix(); $output['html'] .= $this->get_element_suffix(); if (count($this->get_css()) > 0) { $output['html'] .= "<style>" . implode("\n", $this->get_css()) . "</style>"; } if (!empty($js)) { $output['js'] = $js; } $output = json_encode($output); break; case 'html': default: $output = $this->get_element_prefix(); $output .= $this->get_prefix(); $output .= $highlights; $output .= $errors; $output .= "<form action=\"{$this->action}\" id=\"{$this->form_id}\" method=\"{$this->method}\"{$attributes}>\n"; $output .= $fields_html; $output .= "<input type=\"hidden\" name=\"form_id\" value=\"{$this->form_id}\" />\n"; if (!$this->no_token) { $output .= "<input type=\"hidden\" name=\"form_token\" value=\"{$this->form_token}\" />\n"; } if ($this->get_num_steps() > 1) { $output .= "<input type=\"hidden\" name=\"current_step\" value=\"{$this->current_step}\" />\n"; } $output .= "</form>\n"; if (count($this->get_css()) > 0) { $output .= "<style>" . implode("\n", $this->get_css()) . "</style>"; } if (!empty($js)) { $output .= "\n<script type=\"text/javascript\">\n" . $js . "\n</script>\n"; } $output .= $this->get_suffix(); $output .= $this->get_element_suffix(); break; } } return $output; }
function batchoperationsform(cs_form $form, &$form_state) { $step = 0; $form->set_action($_SERVER['PHP_SELF']); $form->add_field('progressnum', array('type' => 'value', 'value' => isset($form_state['input_form_definition']['fields'][$step]['progressnum']['value']) ? $form_state['input_form_definition']['fields'][$step]['progressnum']['value'] + 20 : 0)); $fieldset = $form->add_field('fieldset', array('type' => 'tag_container')); if (cs_form::is_partial()) { $jsondata = json_decode($form_state['input_values']['jsondata']); $callback = $jsondata->callback; if (isset($form_state['input_form_definition']['fields'][$step]['progressnum']['value']) && $form_state['input_form_definition']['fields'][$step]['progressnum']['value'] >= 100) { $fieldset->add_field('done', array('type' => 'markup', 'default_value' => 'finito!')); } else { if (is_callable($callback)) { $fieldset->add_js("setTimeout(function(){ \$('#progress','#{$form->get_id()}').trigger('click') },1000);"); } $fieldset->add_field('progress', array('type' => 'progressbar', 'default_value' => $form->get_field('progressnum')->get_value(), 'show_label' => TRUE, 'ajax_url' => $_SERVER['PHP_SELF'], 'event' => array(array('event' => 'click', 'callback' => 'batch_operations_form_callback', 'target' => 'batchoperationsform', 'effect' => '', 'method' => 'replace')))); } } // must be outside of the fieldset in order to be processed $form->add_field('file', array('type' => 'file', 'ajax_url' => $_SERVER['PHP_SELF'], 'destination' => dirname(__FILE__), 'event' => array(array('event' => 'change', 'callback' => 'batch_operations_form_callback', 'target' => 'batchoperationsform', 'effect' => 'fade', 'method' => 'replace')))); /* $fieldset->add_field('submit', array( 'type' => 'submit', )); */ return $form; }