Subversion Repositories PHPX

Rev

Rev 51 | Rev 70 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
27 PointedEar 1
<?php
2
 
51 PointedEar 3
namespace PointedEars\PHPX;
4
 
27 PointedEar 5
/**
6
 * A general controller that can handle views according to
7
 * the MVC pattern
8
 *
9
 * @author tlahn
10
 */
11
abstract class Controller
12
{
13
  /**
14
   * Default action of the controller
15
   * @var string
16
   */
17
  protected $_defaultAction = 'index';
43 PointedEar 18
 
27 PointedEar 19
  /**
30 PointedEar 20
   * If an array, maps an action name to an action method of the controller.
21
   *
22
   * Fallback for IE 7 where the content of a <code>button</code> element
23
   * is submitted as value.
24
   *
25
   * @var array
26
   */
27
  protected $_actionMap = null;
43 PointedEar 28
 
30 PointedEar 29
  /**
27 PointedEar 30
   * The {@link View} used by this controller
31
   * @var View
32
   */
33
  protected $_view = null;
43 PointedEar 34
 
27 PointedEar 35
  /**
36
   * Constructs a controller, initializes the related view,
37
   * and calls the controller's URI-indicated action method.
38
   *
39
   * @param string $viewClass
40
   *   View class.  The default is <code>'View'</code>.
41
   * @param string $template
42
   *   Resource path of the template for the view.  The default
43 PointedEar 43
   *   is to <code>null</code> (no template).
44
   *
45
   * @see View::__construct()
27 PointedEar 46
   */
30 PointedEar 47
  protected function __construct($viewClass = 'View', $template = null)
27 PointedEar 48
  {
49
    $this->_view = new $viewClass($template);
50
 
51
    Application::getInstance()->setCurrentController($this);
43 PointedEar 52
 
27 PointedEar 53
    $action = Application::getParam('action', $_REQUEST);
43 PointedEar 54
 
27 PointedEar 55
    /* NOTE: No `==='; treat empty action like no specific action */
56
    if ($action == null)
57
    {
58
      $action = $this->_defaultAction;
59
    }
60
 
30 PointedEar 61
    $actionMethod = lcfirst($action) . 'Action';
43 PointedEar 62
 
30 PointedEar 63
    /* Fallback for IE 7 where the content of a `button' element is submitted as value */
64
    if (!method_exists($this, $actionMethod))
65
    {
66
      if (is_array($this->_actionMap) && array_key_exists($action, $this->_actionMap))
67
      {
68
        $actionMethod = $this->_actionMap[$action];
69
      }
70
    }
43 PointedEar 71
 
30 PointedEar 72
    $this->$actionMethod();
27 PointedEar 73
  }
43 PointedEar 74
 
27 PointedEar 75
  /**
76
   * Assigns a value to a template variable (after this,
77
   * <var>$value</var> is available through
78
   * <code>$this-><var>$name</var></code> in the view's template).
79
   * <code>Controller</code>s should call this method instead of
80
   * {@link View::assign()}.
81
   *
82
   * @param string $name
83
   *   Variable name
84
   * @param mixed $value
85
   *   Variable value
86
   * @param bool $encodeHTML
87
   *   If <code>true</code>, replace all potentially conflicting
88
   *   characters in <var>$value</var> with their HTML entity
89
   *   references.  The default is <code>false</code>.
90
   * @return mixed The assigned value (after possible HTML encoding)
91
   * @see View::encodeHTML()
92
   */
93
  protected function assign($name, $value, $encodeHTML = false)
94
  {
95
    return $this->_view->assign($name, $value, $encodeHTML);
96
  }
43 PointedEar 97
 
27 PointedEar 98
  /**
99
   * Renders the {@link View} associated with this controller
100
   * by including the <code>View</code>'s template.
101
   * <code>Controller</code>s should call this method instead of
102
   * <code>View::render()</code>.
103
   *
104
   * @param string $template
105
   *   Optional alternative template resource path.
106
   *   If not provided, the default template (the
107
   *   <code>View</code>'s <code>$template</code> property)
108
   *   will be used.
109
   */
110
  public function render($template = null, $content = null)
111
  {
112
    $this->_view->render($template, $content);
113
  }
114
}
115
 
116
?>