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