Updater Controller

From Rhypedigital

Jump to: navigation, search

The form submit is rendered by the Controller/View rather than the Component because at design time the Component does not know the path to the controller. We would have to pass this into the component at runtime in order for it to do this. It is simpler to let the Controller take care of it.


<?php
 
#
#  File     : Ctrl/Test.php
#  Copyright: 2009
#           : Rhype Digital Ltd
#  License  : This file is released under the terms of the Rhype Digital Software Licence.
#           : Use of this software is prohibited without express permission of Rhype
#           : Digital Ltd.
#  EMail    : copyright@rhypedigital.com
#
 
require_once   ('Lib/View/Test.php'          ) ;
require_once   ('Lib/Model/AR/AR_test.php'   ) ;
require_once   ('Lib/Component/Insert.php'   ) ;
require_once   ('Lib/View/Updater.php'       ) ;
require_once   ('RiPHPLib/Support/Class.php' ) ;
require_once   ('RiPHPLib/MVC/View.php'      ) ;
 
#  This is the controller class. By convention the class is named Ctrl_name
#  where name is the controller name (in this case Updater).
#
class Ctrl_Updater extends RPL_Support_Class
{
   protected   $m_dbh         ;
   protected   $m_cgi         ;
 
   #  Class constructor, typically stores the database and CGI objects for
   #  later use.
   #
   function __construct ($dbh, $cgi)
   {
      $this->m_dbh         = $dbh   ;
      $this->m_cgi         = $cgi   ;
   }
 
   #  Use a common display method which is called by both the init and the
   #  insert actions.
   #
   function display ()
   {
      #  Create the insert component with an empty active record. The
      #  name 'insert' is passed as the component name; this is the name
      #  used in Updater.skel to embed the component.
      #
      $arEmpty = new AR_test ($this->m_dbh) ;
      $comp    = new Lib_Component_Insert ('insert', $arEmpty) ;
 
      #  Create an active record for the test table, and retrieve all
      #  records.
      #
      $arTest  = new AR_test ($this->m_dbh) ;
      $arTest->setMultiple   (true) ;
      $arTest->find () ;
 
      #  Create the view, passing is the active record. Also pass it the
      #  component which will be rendered as part of the view.
      #
      $view    = new Lib_View_Updater ($arTest) ;
      $view->addControl ($comp) ;
      return   $view->render () ;
   }
   #  There should be a method for each action, with the same name as the
   #  action. Typically, init is the default action.
   #
   function init ()
   {
      return   $this->display() ;
   }
 
   #  This is the method for the insert action.
   #
   function insert ()
   {
      #  Create an active record on the test table, and a instance of
      #  the insert component. The call to "loadActiveRecord" will load
      #  CGI parameter values into the active record.
      #
      $arIns   = new AR_test ($this->m_dbh) ;
      $comp    = new Lib_Component_Insert ('insert', $arIns) ;
      $comp->loadActiveRecord ($arIns) ;
      #  If the user has entered both a name and a date then we can
      #  save the record (which will become an SQL insert). Note that
      #  there is no validity checking.
      #
      if (($arIns->test_name != '') && ($arIns->test_date != ''))
         $arIns->save () ;
 
      return   $this->display() ;
   }
}
#  A controller should have a function named name_Factory where name is the
#  controller name (in this case Updater). This is called from the RPL_MVC_Controller
#  instance created in the main "index.php" script, and passes through the database
#  and CGI objects. The function creates an instance of the controller class.
#
function Updater_factory ($dbh, $cgi)
{
   return   new Ctrl_Updater ($dbh, $cgi) ;
}
?>
Personal tools