phpDocumentor ZNF
Actions
[ class tree: ZNF ] [ index: ZNF ] [ all elements ]

Source for file DispatchAction.php

Documentation is available at DispatchAction.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4.  * PHP version 5
  5.  *
  6.  * This source file is subject to version 2.1 of the GNU Lesser General Public
  7.  * License, that is bundled with this package in the file COPYING, available
  8.  * through the world wide web at the following URI:
  9.  * http://www.gnu.org/copyleft/lesser.html.
  10.  *
  11.  * @package    ZNF
  12.  * @subpackage Actions
  13.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  14.  * @author     Graziano Liberati <http://www.liberati.org>
  15.  * @copyright  2004-2007 The ZNF Development Team
  16.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  17.  * @version    SVN $Id: DispatchAction.php 43 2007-06-26 23:36:35Z aronnax $
  18.  * @since      Release 0.7.0
  19.  * @link       http://www.zeronotice.org
  20.  */
  21.  
  22. /**
  23.  * <i>ZNF_Actions_DispatchAction</i> is an abstract <i>ZNF_Action_Action</i> that dispatches to a public method that is named by the request parameter whose name is specified by the <i>parameter</i> property of the corresponding <i>ZNF_Action_ActionMapping</i>.
  24.  *
  25.  * This <i>action</i> is useful for developers who prefer to combine many
  26.  * similar actions into a single <i>ZNF_Actions_DispatchAction</i> class, in
  27.  * order to simplify their application design.
  28.  *
  29.  * There are several way the configure this action in your module configuration
  30.  * file.
  31.  *
  32.  * - Usage of the <i>parameter</i> attribute
  33.  *
  34.  * <i>
  35.  *   &lt;action path="customAction"
  36.  *     type="Package_Action_CustomDispatchAction"
  37.  *     parameter="method@location"/&gt;
  38.  * </i>
  39.  *
  40.  * This action will use the value of the request parameter named <i>method</i>
  41.  * to pick the appropriate <i>execute</i> method, which must have the same
  42.  * signature (other than method name) of the standard
  43.  * <i>ZNF_Actions_DispatchAction->execute()</i> method. For example, you might
  44.  * have the following three methods in the same action class:
  45.  * <ul>
  46.  *   <li>public function create($form, $mapping)</li>
  47.  *   <li>public function update($form, $mapping)</li>
  48.  *   <li>public function delete($form, $mapping)</li>
  49.  * </ul>
  50.  * And call one of them with a URL like this:
  51.  * <i>
  52.  *   http://&lt;yourhost&gt;/&lT;yourapp&gt;/znf/action?method=update
  53.  * </i>
  54.  *
  55.  * The second part of the parameter attribute (@location) is optional and it is
  56.  * used to specify where to find the requested parameter, possible values are:
  57.  * 'GET', 'POST', 'REQUEST' and 'SESSION'.
  58.  * Considering the example above it should be replaced by 'GET', anyway if not
  59.  * specified it will be assumed the 'GET' value anyway.
  60.  *
  61.  * - Usage of the <i>parameterValue</i> attribute
  62.  *
  63.  * <i>
  64.  *   &lt;action path="customAction"
  65.  *     type="Package_Action_CustomDispatchAction"
  66.  *     parameterValue="update"/&gt;
  67.  * </i>
  68.  *
  69.  * The <i>parameterValue</i> attribute is used to specify directly the name of
  70.  * the method that it will be called, considering the configuration above it
  71.  * will becalled the <i>update</i> method of the
  72.  * <i>ZNF_Actions_CustomDispatchAction</i> class.
  73.  *
  74.  * <p><strong>NOTE</strong> - <i>parameter</i> and <i>parameterValues</i>
  75.  * attribute are exclusive and if any of them is specified or the result value
  76.  * is <i>empty</i> or <i>execute</i>, a method named <i>unspecified</i> is called.
  77.  * The default action is to throw an exception. You can override the
  78.  * <i>unspecified</i> method in your subclass.
  79.  *
  80.  * @access     public
  81.  * @package    ZNF
  82.  * @subpackage Actions
  83.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  84.  * @author     Graziano Liberati <http://www.liberati.org>
  85.  * @copyright  2004-2007 The ZNF Development Team
  86.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  87.  * @version    SVN $Id: DispatchAction.php 43 2007-06-26 23:36:35Z aronnax $
  88.  * @since      Release 0.7.0
  89.  * @link       http://www.zeronotice.org
  90.  */
  91. {
  92.  
  93.     /**
  94.      * Constructs a new <i>ZNF_Actions_DispatchAction</i> object.
  95.      *
  96.      * @access public
  97.      */
  98.     public function __construct()
  99.     {
  100.     }
  101.  
  102.     /**
  103.      * Code to check and execute the action, subclasses must implement the method requested.
  104.      *
  105.      * If the method required is not implemented by the subclass the
  106.      * <i>unspecified</i> method is called.
  107.      *
  108.      * @access public
  109.      * @param ZNF_Action_ActionForm $form 
  110.      * @param ZNF_Action_ActionMapping $mapping 
  111.      * @return ZNF_Action_ActionForward 
  112.      */
  113.     public function execute($form$mapping)
  114.     {
  115.         $method false;
  116.  
  117.         $parameterValue $mapping->getParameterValue();
  118.  
  119.         if (!$parameterValue{
  120.             $parameter $mapping->getParameter();
  121.  
  122.             $location '';
  123.  
  124.             if (ereg('@'$parameter)) {
  125.                 $parameterArray explode('@'$parameter);
  126.                 $parameter $parameterArray[0];
  127.                 $location $parameterArray[1];
  128.             }
  129.  
  130.             switch ($location{
  131.                 case 'GET':
  132.                     if (isset($_GET[$parameter])) {
  133.                         $method $_GET[$parameter];
  134.                     }
  135.                 break;
  136.  
  137.                 case 'POST':
  138.                     if (isset($_POST[$parameter])) {
  139.                         $method $_POST[$parameter];
  140.                     }
  141.                 break;
  142.  
  143.                 case 'REQUEST':
  144.                     if (isset($_REQUEST[$parameter])) {
  145.                         $method $_REQUEST[$parameter];
  146.                     }
  147.                 break;
  148.  
  149.                 case 'SESSION':
  150.                     if (isset($_SESSION[$parameter])) {
  151.                         $method $_SESSION[$parameter];
  152.                     }
  153.                 break;
  154.  
  155.                 default:
  156.                     if (isset($_GET[$parameter])) {
  157.                         $method $_GET[$parameter];
  158.                     }
  159.                 break;
  160.             }
  161.         else {
  162.             $method $parameterValue;
  163.         }
  164.  
  165.         if (!$method || $method == "execute" || !method_exists($this$method)) {
  166.             return $this->unspecified($form$mapping);
  167.         }
  168.  
  169.         return $this->$method($form$mapping);
  170.     }
  171.  
  172.     /**
  173.      * Default method to execute if the requested method is not found or the <i>parameter</i> attibute in the configuration file is not specified.
  174.      *
  175.      * Subclasses can override this method to customize the error.
  176.      *
  177.      * @access public
  178.      * @param ZNF_Action_ActionForm $form 
  179.      * @param ZNF_Action_ActionMapping $mapping 
  180.      */
  181.     public function unspecified($form$mapping)
  182.     {
  183.         $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  184.         throw new ZNF_Actions_DispatchActionException($translation->get('errorMethodNotValid'));
  185.     }
  186.  
  187.     /**
  188.      * Destroys the <i>ZNF_Actions_DispatchAction</i> object.
  189.      *
  190.      * @access public
  191.      */
  192.     public function __destruct()
  193.     {
  194.     }
  195.  
  196. }
  197.  
  198. /**
  199.  * <i>ZNF_Actions_DispatchActionException</i> is the exception type for the <i>ZNF_Actions_DispatchAction</i> class.
  200.  *
  201.  * <i>ZNF_Actions_DispatchActionException</i> extends the <i>Exception</i> class of PHP5.
  202.  *
  203.  * @access     public
  204.  * @package    ZNF
  205.  * @subpackage Actions
  206.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  207.  * @author     Graziano Liberati <http://www.liberati.org>
  208.  * @copyright  2004-2007 The ZNF Development Team
  209.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  210.  * @version    SVN $Id: DispatchAction.php 43 2007-06-26 23:36:35Z aronnax $
  211.  * @since      Release 0.7.0
  212.  * @link       http://www.zeronotice.org
  213.  */
  214. class ZNF_Actions_DispatchActionException extends Exception
  215. {
  216. }
  217.  
  218. ?>

Documentation generated on Wed, 14 Nov 2007 23:47:36 +0100 by phpDocumentor 1.4.0