Rev 29 | Rev 43 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 29 | Rev 30 | ||
|---|---|---|---|
| 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 | * The {@link View} used by this controller
|
37 | * If an array, maps an action name to an action method of the controller.
|
| - | 38 | *
|
|
| - | 39 | * Fallback for IE 7 where the content of a <code>button</code> element
|
|
| - | 40 | * is submitted as value.
|
|
| 38 | *
|
41 | *
|
| - | 42 | * @var array
|
|
| - | 43 | */
|
|
| - | 44 | protected $_actionMap = null; |
|
| - | 45 | ||
| - | 46 | /**
|
|
| - | 47 | * The {@link View} used by this controller
|
|
| 39 | * @var View
|
48 | * @var View
|
| 40 | */
|
49 | */
|
| 41 | protected $_view = null; |
50 | protected $_view = null; |
| 42 | 51 | ||
| 43 | /**
|
52 | /**
|
| 44 | * Constructs a controller, initializes the related view,
|
53 | * Constructs a controller, initializes the related view,
|
| 45 | * and calls the controller's URI-indicated action method.
|
54 | * and calls the controller's URI-indicated action method.
|
| 46 | *
|
55 | *
|
| 47 | * @param string $viewClass
|
56 | * @param string $viewClass
|
| 48 | * View class. The default is <code>'View'</code>.
|
57 | * View class. The default is <code>'View'</code>.
|
| 49 | * @param string $template
|
58 | * @param string $template
|
| 50 | * Resource path of the template for the view. The default
|
59 | * Resource path of the template for the view. The default
|
| 51 | * is the empty string.
|
60 | * is the empty string.
|
| 52 | */
|
61 | */
|
| 53 | protected function __construct($viewClass = 'View', |
62 | protected function __construct($viewClass = 'View', $template = null) |
| 54 | $template = null) |
- | |
| 55 | {
|
63 | {
|
| 56 | $this->_view = new $viewClass($template); |
64 | $this->_view = new $viewClass($template); |
| 57 | 65 | ||
| 58 | Application::getInstance()->setCurrentController($this); |
66 | Application::getInstance()->setCurrentController($this); |
| 59 | 67 | ||
| 60 | $action = Application::getParam('action', $_REQUEST); |
68 | $action = Application::getParam('action', $_REQUEST); |
| 61 | 69 | ||
| 62 | /* NOTE: No `==='; treat empty action like no specific action */
|
70 | /* NOTE: No `==='; treat empty action like no specific action */
|
| 63 | if ($action == null) |
71 | if ($action == null) |
| 64 | {
|
72 | {
|
| 65 | $action = $this->_defaultAction; |
73 | $action = $this->_defaultAction; |
| 66 | }
|
74 | }
|
| 67 | 75 | ||
| 68 | $this->{lcfirst($action) . 'Action'}(); |
76 | $actionMethod = lcfirst($action) . 'Action'; |
| - | 77 | ||
| - | 78 | /* Fallback for IE 7 where the content of a `button' element is submitted as value */
|
|
| - | 79 | if (!method_exists($this, $actionMethod)) |
|
| - | 80 | {
|
|
| - | 81 | if (is_array($this->_actionMap) && array_key_exists($action, $this->_actionMap)) |
|
| - | 82 | {
|
|
| - | 83 | $actionMethod = $this->_actionMap[$action]; |
|
| - | 84 | }
|
|
| - | 85 | }
|
|
| - | 86 | ||
| - | 87 | $this->$actionMethod(); |
|
| 69 | }
|
88 | }
|
| 70 | 89 | ||
| 71 | /**
|
90 | /**
|
| 72 | * Assigns a value to a template variable (after this,
|
91 | * Assigns a value to a template variable (after this,
|
| 73 | * <var>$value</var> is available through
|
92 | * <var>$value</var> is available through
|
| 74 | * <code>$this-><var>$name</var></code> in the view's template).
|
93 | * <code>$this-><var>$name</var></code> in the view's template).
|
| 75 | * <code>Controller</code>s should call this method instead of
|
94 | * <code>Controller</code>s should call this method instead of
|
| 76 | * {@link View::assign()}.
|
95 | * {@link View::assign()}.
|
| 77 | *
|
96 | *
|
| 78 | * @param string $name
|
97 | * @param string $name
|
| 79 | * Variable name
|
98 | * Variable name
|
| 80 | * @param mixed $value
|
99 | * @param mixed $value
|
| 81 | * Variable value
|
100 | * Variable value
|
| 82 | * @param bool $encodeHTML
|
101 | * @param bool $encodeHTML
|
| 83 | * If <code>true</code>, replace all potentially conflicting
|
102 | * If <code>true</code>, replace all potentially conflicting
|
| 84 | * characters in <var>$value</var> with their HTML entity
|
103 | * characters in <var>$value</var> with their HTML entity
|
| 85 | * references. The default is <code>false</code>.
|
104 | * references. The default is <code>false</code>.
|
| 86 | * @return mixed The assigned value (after possible HTML encoding)
|
105 | * @return mixed The assigned value (after possible HTML encoding)
|
| 87 | * @see View::encodeHTML()
|
106 | * @see View::encodeHTML()
|
| 88 | */
|
107 | */
|
| 89 | protected function assign($name, $value, $encodeHTML = false) |
108 | protected function assign($name, $value, $encodeHTML = false) |
| 90 | {
|
109 | {
|
| 91 | return $this->_view->assign($name, $value, $encodeHTML); |
110 | return $this->_view->assign($name, $value, $encodeHTML); |
| 92 | }
|
111 | }
|
| 93 | 112 | ||
| 94 | /**
|
113 | /**
|
| 95 | * Renders the {@link View} associated with this controller
|
114 | * Renders the {@link View} associated with this controller
|
| 96 | * by including the <code>View</code>'s template.
|
115 | * by including the <code>View</code>'s template.
|
| 97 | * <code>Controller</code>s should call this method instead of
|
116 | * <code>Controller</code>s should call this method instead of
|
| 98 | * <code>View::render()</code>.
|
117 | * <code>View::render()</code>.
|
| 99 | *
|
118 | *
|
| 100 | * @param string $template
|
119 | * @param string $template
|
| 101 | * Optional alternative template resource path.
|
120 | * Optional alternative template resource path.
|
| 102 | * If not provided, the default template (the
|
121 | * If not provided, the default template (the
|
| 103 | * <code>View</code>'s <code>$template</code> property)
|
122 | * <code>View</code>'s <code>$template</code> property)
|
| 104 | * will be used.
|
123 | * will be used.
|
| 105 | */
|
124 | */
|
| 106 | public function render($template = null, $content = null) |
125 | public function render($template = null, $content = null) |
| 107 | {
|
126 | {
|
| 108 | $this->_view->render($template, $content); |
127 | $this->_view->render($template, $content); |
| 109 | }
|
128 | }
|
| 110 | }
|
129 | }
|
| 111 | 130 | ||
| 112 | ?>
|
131 | ?>
|