[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/web_app/src/controller/ -> lmbController.class.php (source)

   1  <?php
   2  /*

   3   * Limb PHP Framework

   4   *

   5   * @link http://limb-project.com

   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)

   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html

   8   */
   9  lmb_require('limb/web_app/src/controller/lmbAbstractController.class.php');
  10  lmb_require('limb/validation/src/lmbErrorList.class.php');
  11  lmb_require('limb/validation/src/lmbValidator.class.php');
  12  
  13  /**

  14   * class lmbController.

  15   *

  16   * @package web_app

  17   * @version $Id: lmbController.class.php 5990 2007-06-13 13:57:19Z tony $

  18   */
  19  class lmbController extends lmbAbstractController
  20  {
  21    protected $toolkit;
  22    protected $request;
  23    protected $response;
  24    protected $session;
  25    protected $view;
  26    protected $error_list;
  27    protected $validator;
  28    protected $form_id;
  29    protected $in_popup = true;
  30  
  31    function __construct()
  32    {
  33      parent :: __construct();
  34  
  35      $this->toolkit = lmbToolkit :: instance();
  36      $this->request = $this->toolkit->getRequest();
  37      $this->response = $this->toolkit->getResponse();
  38      $this->session = $this->toolkit->getSession();
  39      $this->view = $this->toolkit->getView();
  40      $this->error_list = new lmbErrorList();
  41      $this->validator = new lmbValidator($this->error_list);
  42    }
  43  
  44    function validate($dataspace)
  45    {
  46      $this->validator->validate($dataspace);
  47      return $this->validator->isValid();
  48    }
  49  
  50    function actionExists($action)
  51    {
  52      if(method_exists($this, $this->_mapActionToMethod($action)))
  53        return true;
  54  
  55      if($this->_findTemplateForAction($action))
  56        return true;
  57  
  58      return false;
  59    }
  60  
  61    function performAction()
  62    {
  63      if(method_exists($this, $this->_mapCurrentActionToMethod()))
  64      {
  65        if($template_path = $this->_findTemplateForAction($this->current_action))
  66          $this->setTemplate($template_path);
  67  
  68        $method = $this->_mapCurrentActionToMethod($this->_mapCurrentActionToMethod());
  69        $res = $this->$method();
  70  
  71        $this->_passLocalAttributesToView();
  72  
  73        if(is_string($res))
  74          $this->response->write($res);
  75        elseif($this->response->isEmpty() && !$this->view->getTemplate())
  76          $this->response->write('Default empty output for controller "' .
  77                                 get_class($this) . '" action "' . $this->current_action . '"');
  78  
  79        return $res;
  80      }
  81      elseif($template_path = $this->_findTemplateForAction($this->current_action))
  82      {
  83        $this->setTemplate($template_path);
  84        $this->_passLocalAttributesToView();
  85        return;
  86      }
  87  
  88      throw new lmbException('No method defined in controller "' .
  89                             $this->getName(). '" for action "' . $this->current_action . '" ' .
  90                             'and no appropriate template found');
  91    }
  92  
  93    function useForm($form_id)
  94    {
  95      $this->form_id = $form_id;
  96      $this->view->setFormErrors($form_id, $this->error_list);
  97    }
  98  
  99    function setTemplate($template_path)
 100    {
 101      $this->view->setTemplate($template_path);
 102    }
 103  
 104    protected function _passLocalAttributesToView()
 105    {
 106      foreach(get_object_vars($this) as $name => $value)
 107      {
 108        if($name{0} == '_')
 109          continue;
 110        $this->view->set($name, $value);
 111      }
 112    }
 113  
 114    function passToView($var, $value)
 115    {
 116      $this->view->set($var, $value);
 117    }
 118  
 119    function resetView()
 120    {
 121      $this->view->reset();
 122    }
 123  
 124    function setFormDatasource($datasource, $form_id = null)
 125    {
 126      if(!$form_id && !$this->form_id)
 127        throw new lmbException('There is no form id specified');
 128  
 129      if(!$form_id)
 130        $form_id = $this->form_id;
 131  
 132      $this->view->setFormDatasource($form_id, $datasource);
 133    }
 134  
 135    /**

 136     * @deprecated

 137     */
 138    function setViewFormDatasource($datasource, $form_id = null)
 139    {
 140      $this->setFormDatasource($datasource, $form_id);
 141    }
 142  
 143    function redirect($params_or_url = array(), $route_url = null)
 144    {
 145      $this->toolkit->redirect($params_or_url, $route_url);
 146    }
 147  
 148    function forward($controller_name, $action)
 149    {
 150      $controller = $this->toolkit->createController($controller_name);
 151      $controller->setCurrentAction($action);
 152      return $controller->performAction();
 153    }
 154  
 155    function forwardTo404()
 156    {
 157      return $this->forward('not_found', 'display');
 158    }
 159  
 160    function forwardTo500()
 161    {
 162      return $this->forward('server_error', 'display');
 163    }
 164  
 165    function flashError($message)
 166    {
 167      $this->toolkit->flashError($message);
 168    }
 169  
 170    function flashMessage($message)
 171    {
 172      $this->toolkit->flashMessage($message);
 173    }
 174  
 175    function flash($message)
 176    {
 177      $this->flashMessage($message);
 178    }
 179  
 180    function addError($message, $fields = array(), $values = array())
 181    {
 182      $this->error_list->addError($message, $fields, $values);
 183    }
 184  
 185    function closePopup()
 186    {
 187      if(!$this->in_popup)
 188        return;
 189  
 190      $this->response->write('<html><script>if(window.opener){window.opener.focus();window.opener.location.reload();window.close();}</script></html>');
 191    }
 192  
 193    protected function _mapCurrentActionToMethod()
 194    {
 195      return $this->_mapActionToMethod($this->current_action);
 196    }
 197  
 198    protected function _mapActionToMethod($action)
 199    {
 200      return lmb_camel_case('do_' . $action);
 201    }
 202  }
 203  
 204  ?>


Generated: Sat Sep 6 04:46:52 2008 Cross-referenced by PHPXref 0.7