Example #1
0
	/**
	 * Produce a form with the contained fields.
	 *
	 * @param boolean $process_for_success Set to true to display the form as it would look if the submission succeeded, but do not execute success methods.
	 * @return string HTML form generated from all controls assigned to this form
	 */
	public function get( $use_theme = null, $process_for_success = true )
	{
		$forvalidation = false;

		Plugins::act( 'modify_form_' . Utils::slugify( $this->formtype, '_' ), $this );
		Plugins::act( 'modify_form', $this );

		if ( isset( $use_theme ) ) {
			$theme = $use_theme;
		}
		else {
			$theme = $this->get_theme( $forvalidation, $this );
		}
		$theme->start_buffer();
		$theme->success = false;
		$this->success = false;
		$this->submitted = false;

		// Should we be validating?
		if ( isset( $_POST['FormUI'] ) && $_POST['FormUI'] == $this->salted_name() ) {
			$this->submitted = true;
			$validate = $this->validate();
			if ( count( $validate ) == 0 ) {
				if ( $process_for_success ) {
					$result = $this->success();
					if ( $result ) {
						return $result;
					}
				}
				$theme->success = true;
				$this->success = true;
				$theme->message = $this->options['success_message'];
			}
			else {
				$forvalidation = true;
				if ( !isset( $_SESSION['forms'][$this->salted_name()]['url'] ) ) {
					$_SESSION['forms'][$this->salted_name()]['url'] = Site::get_url( 'habari', true ) . Controller::get_stub() . ( isset( $_SERVER['QUERY_STRING'] ) ? '?' . $_SERVER['QUERY_STRING'] : '' );
				}
			}
		}
		else {
			$_SESSION['forms'][$this->salted_name()]['url'] = Site::get_url( 'habari', true ) . Controller::get_stub() . ( isset( $_SERVER['QUERY_STRING'] ) ? '?' . $_SERVER['QUERY_STRING'] : '' );
		}
		if ( isset( $_SESSION['forms'][$this->salted_name()]['error_data'] ) ) {
			foreach ( $_SESSION['forms'][$this->salted_name()]['error_data'] as $key => $value ) {
				$_POST[$key] = $value;
			}
			unset( $_SESSION['forms'][$this->salted_name()]['error_data'] );
			$forvalidation = true;
		}

		$out = '';

		$theme->controls = $this->output_controls( $forvalidation );
		$theme->form = $this;

		foreach ( $this->properties as $prop => $value ) {
			$theme->$prop = $value;
		}

		$theme->id = Utils::slugify( $this->name );
		$theme->class = implode( " ", (array) $this->class );
		$theme->action = $this->options['form_action'];
		$theme->onsubmit = ($this->properties['onsubmit'] == '') ? '' : "onsubmit=\"{$this->properties['onsubmit']}\"";
		$theme->salted_name = $this->salted_name();
		$theme->pre_out = $this->pre_out_controls();

		$out = $theme->display_fallback( $this->options['template'], 'fetch' );
		$theme->end_buffer();

		return $out;
	}
Example #2
0
 /**
  * Produce a form with the contained fields.
  *
  * @param Theme $theme The theme to render the controls into
  * @return string HTML form generated from all controls assigned to this form
  */
 public function get(Theme $theme = null)
 {
     // Allow plugins to modify the form
     Plugins::act('modify_form_' . Utils::slugify($this->formtype, '_'), $this);
     Plugins::act('modify_form', $this);
     // Get the theme used to render the form
     if (is_null($theme)) {
         $theme = $this->get_theme();
     }
     $theme->start_buffer();
     $theme->success = false;
     $this->success = false;
     $this->submitted = false;
     $this->success_render = false;
     // Set the ID of the control explicitly if it's not already set
     $this->get_id();
     // If the form template wasn't explicitly set, set it, because this class' name can't be used to determine its template
     if (!isset($this->settings['template'])) {
         $this->set_template('control.form');
     }
     // If the action of this form wasn't explicitly set, unset it to avoid validation errors in output
     if (empty($this->properties['action'])) {
         unset($this->properties['action']);
     }
     // Add the control ID to the template output for the form
     $this->vars['_control_id'] = $this->control_id();
     // Load all of the initial values of controls from their storage locations
     $this->load();
     // If there was an error submitting this form before, set the values of the controls to the old ones to retry
     $this->set_from_error_values();
     // Is this form not a same-page duplicate?
     if (!$this->get_setting('is_dupe', false)) {
         // Was the form submitted?
         if (isset($_POST['_form_id']) && $_POST['_form_id'] == $this->control_id()) {
             $this->submitted = true;
             // Process all of the submitted values into the controls
             $this->process();
             // Do any of the controls fail validation?  This call alters the wrap
             $validation_errors = $this->validate();
             if (count($validation_errors) == 0) {
                 // All of the controls validate
                 $this->success = true;
                 // If do_success() returns anything, it should be output instead of the form.
                 $this->success_render = $this->do_success($this);
             } else {
                 if (isset($this->settings['use_session_errors']) && $this->settings['use_session_errors']) {
                     $this->each(function ($control) {
                         $control->errors = array();
                     });
                     foreach ($validation_errors as $error) {
                         Session::error($error);
                     }
                 }
             }
         } else {
             // Store the location at which this form was loaded, so we can potentially redirect to it later
             if (!$this->has_session_data() || !isset($_SESSION['forms'][$this->control_id()]['url'])) {
                 $_SESSION['forms'][$this->control_id()]['url'] = Site::get_url('habari', true) . Controller::get_stub() . '#' . $this->get_id(false);
             }
         }
         $output = $this->pre_out_controls();
         if ($this->success_render) {
             $output .= $this->success_render;
         } else {
             $output .= parent::get($theme);
         }
         if ($this->success && isset($this->settings['success_message'])) {
             $output .= $this->settings['success_message'];
         }
     } else {
         $output = parent::get($theme);
     }
     if (class_exists('\\tidy')) {
         $t = new \tidy();
         $t->parseString($output, array('indent' => true, 'wrap' => 80, 'show-body-only' => true));
         //$output = (string) $t;
     }
     return $output;
 }