Subversion Repositories PHPX

Rev

Rev 51 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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