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