Subversion Repositories PHPX

Rev

Rev 52 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

1
<?php

namespace PointedEars\PHPX;

/**
 * A general controller that can handle views according to
 * the MVC pattern
 *
 * @author tlahn
 */

abstract class Controller
{
  /**
   * Default action of the controller
   * @var string
   */

  protected $_defaultAction = 'index';

  /**
   * If an array, maps an action name to an action method of the controller.
   *
   * Fallback for IE 7 where the content of a <code>button</code> element
   * is submitted as value.
   *
   * @var array
   */

  protected $_actionMap = null;

  /**
   * The {@link View} used by this controller
   * @var View
   */

  protected $_view = null;

  /**
   * Constructs a controller, initializes the related view,
   * and calls the controller's URI-indicated action method.
   *
   * @param string $viewClass
   *   View class.  The default is <code>'View'</code>.
   * @param string $template
   *   Resource path of the template for the view.  The default
   *   is to <code>null</code> (no template).
   *
   * @see View::__construct()
   */

  protected function __construct ($viewClass = 'View', $template = null)
  {
    $this->_view = new $viewClass($template);

    Application::getInstance()->setCurrentController($this);

    $action = Application::getParam('action', $_REQUEST);

    /* NOTE: No `==='; treat empty action like no specific action */
    if ($action == null)
    {
      $action = $this->_defaultAction;
    }

    $actionMethod = lcfirst($action) . 'Action';

    /* Fallback for IE 7 where the content of a `button' element is submitted as value */
    if (!method_exists($this, $actionMethod))
    {
      if (is_array($this->_actionMap) && array_key_exists($action, $this->_actionMap))
      {
        $actionMethod = $this->_actionMap[$action];
      }
    }

    $this->$actionMethod();
  }

  /**
   * (non-PHPDoc)
   * @see View::assign()
   */

  protected function assign ($name, $value, $encodeHTML = false)
  {
    return $this->_view->assign($name, $value, $encodeHTML);
  }

  /**
   * (non-PHPDoc)
   * @see View::addStylesheet()
   */

  protected function addStylesheet ($uri, $key = null)
  {
    return $this->_view->addStylesheet($uri, $key);
  }

  /**
   * (non-PHPDoc)
   * @see View::addSscript()
   */

  protected function addScript ($uri, $key = null)
  {
    return $this->_view->addScript ($uri, $key);
  }

  /**
   * Renders the {@link View} associated with this controller
   * by including the <code>View</code>'s template.
   * <code>Controller</code>s should call this method instead of
   * <code>View::render()</code>.
   *
   * @param string $template
   *   Optional alternative template resource path.
   *   If not provided, the default template (the
   *   <code>View</code>'s <code>$template</code> property)
   *   will be used.
   */

  public function render ($template = null, $content = null)
  {
    $this->_view->render($template, $content);
  }
}

?>