Source for file gform_ctrl_with_obj.class.php
Documentation is available at gform_ctrl_with_obj.class.php
* Contains class GForm_Ctrl_With_Object
require_once(TANGRA_MAIN_DIR. 'interfaces/i_db_storable.class.php');
require_once(TANGRA_MAIN_DIR. 'form/gform_ctrl.class.php');
require_once(TANGRA_MAIN_DIR. 'exceptions/te_gform_ctrl_wo_record_not_exists.class.php');
* Controller for Guarded_Form with I_DB_Storable Object
* Abstract class that provides basic infrastructure for controllers that will use Guarded_Form and
* array that holds Object to form_ids links
* This string will be searched in $_GET to capture object id
* Object that will be controlled. It must be instance of I_DB_Storable
* @param string $system_name Controller's name
* @param Vars_Manager $vm Permanent Vars_Manager that will be used. Session of Thread VM
* @param DB_Connection $dbc Database connection
* @param string $object_id_capture This string will be searched in $_GET to capture object id
function __construct($system_name, Vars_Manager $vm, DB_Connection $dbc, $object_id_capture = 'obj_id') {
* Returns the event for receiving object id
* @return Web_Event_Simple
* Returns the event for adding new record
* @return Web_Event_Simple
$this->form->get_name(). '_add_new');
* Loads object from the DB, transfers data to form, prepares for show
* @param integer $object_id - ID of the object
* @throws Tangra_Exception
* @return boolean Returns true if record is loaded, false if not found
if ($object->load_by_id($dbc, $object_id)) {
$this->export($this->form->get_name(). '_obj_id', $object->get_id());
* Called when receives command to show "empty" form that will be used to add new record
* Transfers data to form, prepares for show
$this->export($this->form->get_name(). '_add_new', 1);
* Returns string for capture of the object id
* Returns I_DB_Storable object
$form_id = $this->form->get_field_value('form_id');
$tmp = $this->object->get_id() ?
* Transfers data from form to object. Saves the object into DB
$object_id = $object->get_id();
if (!$object->load_by_id($dbc,$object_id)) {
throw new TE_Gform_Ctrl_WO_Record_Not_Exists('Record with that id no longer exists.');
* Save the object into DB
* Please note that saving is in DB transaction.
* Actual save is called here.
* You can overload this method in order to provide additional parameters to save(), e.g. user id.
* @param DB_Connection $dbc
protected function _save_object(DB_Connection $dbc, I_DB_Storable $object) {
* This method will be called just before saving the object and after db transaction is stared
* Feel free to overload this method if you need some processing just before save. For example: inserting other records and getting their ids that are needed for current object.
* @param DB_Connection $dbc
* This method will be called just after saving the object and before db transaction end
* Feel free to overload this method if you need some postprocessing just after save. For example: creating additional records that depends on result of object saving (its id)
* @param DB_Connection $dbc
* Initializes array that will hold object to form_id links
if (!$vm->is_var_registered($this->get_system_name(). '_object_to_form_id_links')) {
* @see GForm_Ctrl::on_accepted_submit()
* @throws Tangra_Exception
$form_id = $this->form->get_field_value('form_id');
throw new Tangra_Exception('form_id not found in object_to_form_id_links.');
* Ensures that create_object() returns object that is instance of I_DB_Storable
* @throws Tangra_Exception
throw new Tangra_Exception('$object is not instance of I_DB_Storable.');
* Creates and returns I_DB_Storable object.
* This method have to be implemented by the user. At the end I_DB_Storable object have to be returned.
* Transfers date from object to form
* User have to implement this method and to transfer each field from object to form
* $object = $this->get_object();
* $form = $this->get_form();
* $form->set_field_value('name', $object->get_name());
* Transfers data from object to form
* User have to implement this method and to transfer each field from form to object
* $object = $this->get_object();
* $form = $this->get_form();
* $object->set_name($form->get_field_value('name'));
* Called right after object is saved.
* Useful for some post processing. For example: notifying other controllers, etc.
* Composes name for receiving object id event
return $this->form->get_name(). '_object_id';
* Composes name for add new event
return $this->form->get_name(). '_add_new';
$this->export($this->form->get_name(). '_obj_id', $object->get_id());
$this->export($this->form->get_name(). '_add_new', 1);
|