tangra logo
   
[ class tree: tangra_lib ] [ index: tangra_lib ] [ all elements ]
 

Source for file gform_ctrl_with_obj.class.php

Documentation is available at gform_ctrl_with_obj.class.php

  1. <?php
  2.  
  3.  
  4. /**
  5.  * Contains class GForm_Ctrl_With_Object
  6.  *
  7.  * @package  tangra_lib
  8.  * @subpackage  form
  9.  */
  10.  
  11.  
  12. /**
  13.  *
  14.  */
  15. require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');
  16.  
  17. /**
  18.  *
  19.  */
  20. require_once(TANGRA_MAIN_DIR.'form/gform_ctrl.class.php');
  21.  
  22. /**
  23.  *
  24.  */
  25. require_once(TANGRA_MAIN_DIR.'exceptions/te_gform_ctrl_wo_record_not_exists.class.php');
  26.  
  27.  
  28. /**
  29.  * Controller for Guarded_Form with I_DB_Storable Object
  30.  *
  31.  * Abstract class that provides basic infrastructure for controllers that will use Guarded_Form and
  32.  * I_DB_Storable objects.
  33.  *
  34.  * @package  tangra_lib
  35.  * @subpackage  form
  36.  */
  37. abstract class GForm_Ctrl_With_Object extends GForm_Ctrl {
  38.     /**
  39.      * array that holds Object to form_ids links
  40.      *
  41.      * @var array 
  42.      * @internal
  43.      */
  44.     protected $object_to_form_id_links;
  45.  
  46.     /**
  47.      * This string will be searched in $_GET to capture object id
  48.      *
  49.      * @var string 
  50.      * @internal
  51.      */
  52.     private $object_id_capture;
  53.  
  54.     /**
  55.      * Object that will be controlled. It must be instance of I_DB_Storable
  56.      *
  57.      * @var unknown_type 
  58.      * @internal
  59.      */
  60.     private $object;
  61.  
  62.     /**
  63.      * DB_Connection object
  64.      *
  65.      * @var unknown_type 
  66.      * @internal
  67.      */
  68.     private $dbc;
  69.  
  70.  
  71.     /**
  72.      * Conctructor
  73.      *
  74.      * @param string $system_name Controller's name
  75.      * @param Vars_Manager $vm Permanent Vars_Manager that will be used. Session of Thread VM
  76.      * @param DB_Connection $dbc Database connection
  77.      * @param string $object_id_capture This string will be searched in $_GET to capture object id
  78.      */
  79.     function __construct($system_nameVars_Manager $vmDB_Connection $dbc$object_id_capture 'obj_id'{
  80.         $this->dbc = $dbc;
  81.         parent::__construct($system_name$vm);
  82.  
  83.         $this->object_id_capture = $object_id_capture;
  84.         $this->init_object_to_form_id_links();
  85.         $this->_create_object();
  86.     }
  87.  
  88.  
  89.     /**
  90.      * Returns the event for receiving object id
  91.      *
  92.      * @return Web_Event_Simple 
  93.      */
  94.     public function get_object_id_event({
  95.         $object_id_event new Web_Event_Simple($this->compose_object_id_event_name(),
  96.                                                  Tangra_Form_Submit_Method::GET,
  97.                                                  $this->form->get_name().'_'.$this->query_object_id_capture());
  98.  
  99.         return $object_id_event;
  100.     }
  101.  
  102.  
  103.     /**
  104.      * Returns the event for adding new record
  105.      *
  106.      * @return Web_Event_Simple 
  107.      */
  108.     public function get_add_new_event({
  109.         $add_new_event new Web_Event_Simple($this->compose_add_new_event_name(),
  110.                                                  Tangra_Form_Submit_Method::GET,
  111.                                                  $this->form->get_name().'_add_new');
  112.  
  113.         return $add_new_event;
  114.     }
  115.  
  116.  
  117.     /**
  118.      * Loads object from the DB, transfers data to form, prepares for show
  119.      *
  120.      * @param integer $object_id - ID of the object
  121.      * @throws  Tangra_Exception
  122.      * @return  boolean Returns true if record is loaded, false if not found
  123.      */
  124.     public function process_object_id($object_id{
  125.         $dbc $this->get_dbc();
  126.         tangra_if_not_int_throw_e($object_id);
  127.  
  128.         $object $this->get_object();
  129.  
  130.         if ($object->load_by_id($dbc$object_id)) {
  131.             $form $this->get_form();
  132.  
  133.             $this->transfer_obj2form();
  134.             $this->prepare_for_show();
  135.  
  136.             $this->export($this->form->get_name().'_obj_id'$object->get_id());
  137.  
  138.             $ret true;
  139.         else {
  140.             $ret false;
  141.         }
  142.  
  143.         return $ret;
  144.     }
  145.  
  146.  
  147.     /**
  148.      * Called when receives command to show "empty" form that will be used to add new record
  149.      *
  150.      * Transfers data to form, prepares for show
  151.      *
  152.      */
  153.     public function process_add_new({
  154.         $this->transfer_obj2form();
  155.         $this->prepare_for_show();
  156.         $this->export($this->form->get_name().'_add_new'1);
  157.     }
  158.  
  159.  
  160.     /**
  161.      * Returns string for capture of the object id
  162.      *
  163.      * @return string 
  164.      */
  165.     public function query_object_id_capture({
  166.         return $this->object_id_capture;
  167.     }
  168.  
  169.  
  170.     /**
  171.      * Returns I_DB_Storable object
  172.      *
  173.      * @return I_DB_Storable 
  174.      */
  175.     public function get_object({
  176.         return $this->object;
  177.     }
  178.  
  179.  
  180.     /**
  181.      * Prepares form for show
  182.      *
  183.      */
  184.     public function prepare_for_show({
  185.         parent::prepare_for_show();
  186.  
  187.         $form_id $this->form->get_field_value('form_id');
  188.         $this->object_to_form_id_links->register_var($form_id);
  189.  
  190.         $tmp $this->object->get_id(?
  191.                                         $this->object->get_id()
  192.                                         :  0;
  193.         $this->object_to_form_id_links->set_var($form_id$tmp);
  194.     }
  195.  
  196.  
  197.     /**
  198.      *
  199.      * @return DB_Connection 
  200.      */
  201.     public function get_dbc({
  202.         return $this->dbc;
  203.     }
  204.  
  205.  
  206.     /**
  207.      * Transfers data from form to object. Saves the object into DB
  208.      *
  209.      */
  210.     public function on_process_submit({
  211.         $object $this->get_object();
  212.         $object_id $object->get_id();
  213.         if ($object_id{
  214.             $dbc $this->get_dbc();
  215.             if (!$object->load_by_id($dbc,$object_id)) {
  216.                 throw new TE_Gform_Ctrl_WO_Record_Not_Exists('Record with that id no longer exists.');
  217.             }
  218.         }
  219.  
  220.         $this->transfer_form2obj();
  221.         $this->save_object();
  222.         $this->after_save();
  223.     }
  224.  
  225.  
  226.     /**
  227.      * Save the object into DB
  228.      *
  229.      * Please note that saving is in DB transaction.
  230.      *
  231.      */
  232.     protected function save_object({
  233.         $dbc $this->get_dbc();
  234.  
  235.         $object $this->get_object();
  236.         $dbc->start_trans();
  237.         try {
  238.             $this->on_trans_started($dbc);
  239.             $this->_save_object($dbc$object);
  240.             $this->on_trans_ending($dbc);
  241.         catch (Exception $e{
  242.             $dbc->fail_trans();
  243.             $dbc->complete_trans();
  244.  
  245.             throw $e;
  246.         }
  247.         $dbc->complete_trans();
  248.     }
  249.  
  250.  
  251.     /**
  252.      * Actual save is called here.
  253.      *
  254.      * You can overload this method in order to provide additional parameters to save(), e.g. user id.
  255.      *
  256.      * @param DB_Connection $dbc 
  257.      */
  258.     protected function _save_object(DB_Connection $dbcI_DB_Storable $object{
  259.         $object->save($dbc);
  260.     }
  261.  
  262.  
  263.     /**
  264.      * This method will be called just before saving the object and after db transaction is stared
  265.      *
  266.      * 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.
  267.      *
  268.      * @param DB_Connection $dbc 
  269.      */
  270.     protected function on_trans_started(DB_Connection $dbc{
  271.  
  272.     }
  273.  
  274.  
  275.     /**
  276.      * This method will be called just after saving the object and before db transaction end
  277.      *
  278.      * 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)
  279.      *
  280.      * @param DB_Connection $dbc 
  281.      */
  282.     protected function on_trans_ending(DB_Connection $dbc{
  283.  
  284.     }
  285.  
  286.  
  287.     /**
  288.      * Initializes array that will hold object to form_id links
  289.      *
  290.      */
  291.     protected function init_object_to_form_id_links({
  292.         $vm $this->get_vm();
  293.  
  294.         if (!$vm->is_var_registered($this->get_system_name().'_object_to_form_id_links')) {
  295.             $vm->register_var($this->get_system_name().'_object_to_form_id_links');
  296.             $vm->set_var($this->get_system_name().'_object_to_form_id_links'new Vars_Manager());
  297.         }
  298.  
  299.         $this->object_to_form_id_links = &$vm->get_var($this->get_system_name().'_object_to_form_id_links');
  300.     }
  301.  
  302.  
  303.     /**
  304.      * @see GForm_Ctrl::on_accepted_submit()
  305.      *
  306.      * @throws Tangra_Exception
  307.      */
  308.     protected function on_accepted_submit({
  309.         $form_id $this->form->get_field_value('form_id');
  310.  
  311.         if ($this->object_to_form_id_links->is_var_registered($form_id)) {
  312.             $this->object->set_id($this->object_to_form_id_links->get_var($form_id));
  313.         else {
  314.             throw new Tangra_Exception('form_id not found in object_to_form_id_links.');
  315.         }
  316.     }
  317.  
  318.  
  319.     /**
  320.      * Ensures that create_object() returns object that is instance of I_DB_Storable
  321.      *
  322.      * @throws Tangra_Exception
  323.      */
  324.     private function _create_object({
  325.         $object $this->create_object();
  326.         if ($object instanceof I_DB_Storable{
  327.             $this->object = $object;
  328.         else {
  329.             throw new Tangra_Exception('$object is not instance of I_DB_Storable.');
  330.         }
  331.     }
  332.  
  333.  
  334.     /**
  335.      * Creates and returns I_DB_Storable object.
  336.      *
  337.      * This method have to be implemented by the user. At the end I_DB_Storable object have to be returned.
  338.      *
  339.      */
  340.     abstract protected function create_object();
  341.  
  342.     /**
  343.      * Transfers date from object to form
  344.      *
  345.      * User have to implement this method and to transfer each field from object to form
  346.      *
  347.       * Example:
  348.      * <code>
  349.      * $object = $this->get_object();
  350.      * $form = $this->get_form();
  351.      *
  352.      * $form->set_field_value('name', $object->get_name());
  353.      * </code>
  354.      *
  355.      */
  356.     abstract protected function transfer_obj2form();
  357.  
  358.  
  359.     /**
  360.      * Transfers data from object to form
  361.      *
  362.      * User have to implement this method and to transfer each field from form to object
  363.      * Example:
  364.      * <code>
  365.      * $object = $this->get_object();
  366.      * $form = $this->get_form();
  367.      *
  368.      * $object->set_name($form->get_field_value('name'));
  369.      * </code>
  370.      *
  371.      */
  372.     abstract protected function transfer_form2obj();
  373.  
  374.     /**
  375.      * Called right after object is saved.
  376.      *
  377.      * Useful for some post processing. For example: notifying other controllers, etc.
  378.      *
  379.      */
  380.     protected function after_save({
  381.  
  382.     }
  383.  
  384.  
  385.     /**
  386.      * Composes name for receiving object id event
  387.      *
  388.      * @return string 
  389.      */
  390.     public function compose_object_id_event_name({
  391.         return $this->form->get_name().'_object_id';
  392.     }
  393.  
  394.  
  395.     /**
  396.      * Composes name for add new event
  397.      *
  398.      * @return string 
  399.      */
  400.     public function compose_add_new_event_name({
  401.         return $this->form->get_name().'_add_new';
  402.     }
  403.  
  404.  
  405.     protected function on_bad_submit({
  406.         $object $this->get_object();
  407.         if ($object->get_id()) {
  408.             $this->export($this->form->get_name().'_obj_id'$object->get_id());
  409.         else {
  410.             $this->export($this->form->get_name().'_add_new'1);
  411.         }
  412.     }
  413. }