Source for file gform_ctrl.class.php
Documentation is available at gform_ctrl.class.php
// *** Tangra (Application Framework and Tools for PHP)
* Contains class GForm_Ctrl
require_once(TANGRA_MAIN_DIR. 'core/vars_manager.class.php');
require_once(TANGRA_MAIN_DIR. 'form/guardable_form.class.php');
require_once(TANGRA_MAIN_DIR. 'form/form_ctrl.class.php');
* Controller for Guarder_Form
* Holds Form_Guard object
* Holds Vars_Manager object
* Array of processed states ids
* Array containing guarded_pairs
* @param stying $<ystem_name Name of the controller
* @param Vars_Manager $vm Permanent Vars_Manager that will be used. Session of Thread VM
* @param Web_Context $context
* @return integer Returned value will be Form_Ctrl::SUBMIT_RESULT_ERROR or Form_Ctrl::SUBMIT_RESULT_OK
$this->form->capture_submit($context);
$form_id = $this->form->get_field_html_value('form_id');
* Initialization of guard pairs array and permanent variable
if (!$vm->is_var_registered($this->get_system_name(). '_form_guarded_pairs')) {
if (!$vm->is_var_registered($this->get_system_name(). '_processed_states')) {
* Called right after submit is accepted.
* After a form is submited and basic_check is passed, Form->accept_submit() method is called
* to transfer values from $_POST to fields' values. Immediately after that
* $this->on_accepted_submit() is called in order to give the user a chance to manipulate
* fields values if there is such need (which happens vary rare, only in low level code. If required
* in high level code that may indicate that there is some design flaw).
* Place for your own processing of the validated form data.
* <p>Called after form is passed successfully basic_check and also on_good_submit returned true.</p>
* <p>For the sake of example - this method is used in form controllers with object to save the object in the DB.
* Normally is not used in high level code.</p>
* Sets Vars_Manager that will be used
* @param Vars_Manager $vm
protected function set_vm(Vars_Manager $vm) {
* Returns reference to used Vars_Mananger
* Returns copy of used Vars_Mananger
* Handles fresh submit of the form.
* GForm_Ctrl guarantees that <b>only one</b> submit of the form (with same data, for
* example when visitor to your site clicks multiple times on submit button) will be processed.
* That means, for example, that you will not get multiple rows in your database with same data,
* just because visitor was impatient and pumped same form again and again.
* @param unknown_type $form_id
* @see GForm_Ctrl::handle_repeated_submit()
//it is first submit of this form
$this->form->basic_check();
$this->form->accept_submit();
if ($this->form->is_form_ok()) {
* Handles repeated submit.
* <p>A submit is considered repeated when form_guard detects that same data is submited more than once.
* In this case "same data" means not just the casual form data but also form_id and guard_value
* hidden fields. handle_repeated_submit is smart enough to detect state of the last fresh submit
* (@see GForm_Ctrl::handle_fresh_submit) and to return it.</p>
* <p>Usualy repeated submits happens when impatient visitor to your site clicks submit button on a
* form multile times. Other case is when page that has received submit is refreshed with F5 or
* "Refresh" button of the browser.</p>
* <p>From application point of view repeated submits are just "skipped", i.e. there are no calls of
*<p>From visitor's point of view repeated submit is 100% transparent - he/she does not notice any
* @param unknown_type $form_id
//second+ submit. Cause: double click on submit button, or refreshed page
//we have already processed this event, just show them last result
$this->form->basic_check();
$this->form->accept_submit();
if ($this->form->is_form_ok()) {
// we never generated such guarded pair, so we assume
// that this is hackers activity and we are closing application immediatly.
// exception can be changed to more concrete class if you intend to intercept it
throw new Tangra_Exception('Form protection detected abnormal condition.');
* Checks if this submit is already processed
* Adds new processed state
* Returns processed state for submit identified with <var>$form_id</var>
* Stores external parameters in vars manager.
if (!$vm->is_var_registered($external_params_var_name)) {
$vm->register_var($external_params_var_name);
$vm->set_var($external_params_var_name, $tmp);
* Retrieves stored external parameters
if ($vm->is_var_registered($external_params_var_name)) {
$arr = $vm->get_var($external_params_var_name);
foreach($arr as $form_id_key => $ep_arr) {
if ($form_id_key == $form_id) {
* Composes string with external parameters vars name.
* Sets parameter that will be stored in vars manager and automatically retrieved after each submit
* @param string $param_name Parameter name
* @param mixed $param_value Parameter actual value
* Returns value of previosly set external parameter
* @param string $param_name Parameter name
* @param boolean $forgiving If true this function will return false if there is no parameter with name $param_name. Otherwise will throw exception. Default is false.
throw new Tangra_Exception('Such param does not exists: '. $param_name);
* Gets TPLE_Exports for the form
$this->export($this->form->get_name(). '_pep_'. $key, $par['value']);
|