Rev 70 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 70 | Rev 71 | ||
|---|---|---|---|
| 1 | <?php
|
1 | <?php
|
| 2 | 2 | ||
| 3 | namespace PointedEars\PHPX; |
3 | namespace PointedEars\PHPX; |
| 4 | 4 | ||
| 5 | /**
|
5 | /**
|
| 6 | * A general controller that can handle views according to
|
6 | * A general controller that can handle views according to
|
| 7 | * the MVC pattern
|
7 | * the MVC pattern
|
| 8 | *
|
8 | *
|
| 9 | * @author tlahn
|
9 | * @author tlahn
|
| 10 | */
|
10 | */
|
| 11 | abstract class Controller |
11 | abstract class Controller |
| 12 | {
|
12 | {
|
| 13 | /**
|
13 | /**
|
| 14 | * Default action of the controller
|
14 | * Default action of the controller
|
| 15 | * @var string
|
15 | * @var string
|
| 16 | */
|
16 | */
|
| 17 | protected $_defaultAction = 'index'; |
17 | protected $_defaultAction = 'index'; |
| 18 | 18 | ||
| 19 | /**
|
19 | /**
|
| 20 | * 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.
|
| 21 | *
|
21 | *
|
| 22 | * 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
|
| 23 | * is submitted as value.
|
23 | * is submitted as value.
|
| 24 | *
|
24 | *
|
| 25 | * @var array
|
25 | * @var array
|
| 26 | */
|
26 | */
|
| 27 | protected $_actionMap = null; |
27 | protected $_actionMap = null; |
| 28 | 28 | ||
| 29 | /**
|
29 | /**
|
| 30 | * The {@link View} used by this controller
|
30 | * The {@link View} used by this controller
|
| 31 | * @var View
|
31 | * @var View
|
| 32 | */
|
32 | */
|
| 33 | protected $_view = null; |
33 | protected $_view = null; |
| 34 | 34 | ||
| 35 | /**
|
35 | /**
|
| 36 | * Constructs a controller, initializes the related view,
|
36 | * Constructs a controller, initializes the related view,
|
| 37 | * and calls the controller's URI-indicated action method.
|
37 | * and calls the controller's URI-indicated action method.
|
| 38 | *
|
38 | *
|
| 39 | * @param string $viewClass
|
39 | * @param string $viewClass
|
| 40 | * View class. The default is <code>'View'</code>.
|
40 | * View class. The default is <code>'View'</code>.
|
| 41 | * @param string $template
|
41 | * @param string $template
|
| 42 | * Resource path of the template for the view. The default
|
42 | * Resource path of the template for the view. The default
|
| 43 | * is to <code>null</code> (no template).
|
43 | * is to <code>null</code> (no template).
|
| 44 | *
|
44 | *
|
| 45 | * @see View::__construct()
|
45 | * @see View::__construct()
|
| 46 | */
|
46 | */
|
| 47 | protected function __construct ($viewClass = 'View', $template = null) |
47 | protected function __construct ($viewClass = 'View', $template = null) |
| 48 | {
|
48 | {
|
| 49 | $this->_view = new $viewClass($template); |
49 | $this->_view = new $viewClass($template); |
| 50 | 50 | ||
| 51 | Application::getInstance()->setCurrentController($this); |
51 | Application::getInstance()->setCurrentController($this); |
| 52 | 52 | ||
| 53 | $action = Application::getParam('action', $_REQUEST); |
53 | $action = Application::getParam('action', $_REQUEST); |
| 54 | 54 | ||
| 55 | /* NOTE: No `==='; treat empty action like no specific action */
|
55 | /* NOTE: No `==='; treat empty action like no specific action */
|
| 56 | if ($action == null) |
56 | if ($action == null) |
| 57 | {
|
57 | {
|
| 58 | $action = $this->_defaultAction; |
58 | $action = $this->_defaultAction; |
| 59 | }
|
59 | }
|
| 60 | 60 | ||
| 61 | $actionMethod = lcfirst($action) . 'Action'; |
61 | $actionMethod = lcfirst($action) . 'Action'; |
| 62 | 62 | ||
| 63 | /* 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 */
|
| 64 | if (!method_exists($this, $actionMethod)) |
64 | if (!method_exists($this, $actionMethod)) |
| 65 | {
|
65 | {
|
| 66 | if (is_array($this->_actionMap) && array_key_exists($action, $this->_actionMap)) |
66 | if (is_array($this->_actionMap) && array_key_exists($action, $this->_actionMap)) |
| 67 | {
|
67 | {
|
| 68 | $actionMethod = $this->_actionMap[$action]; |
68 | $actionMethod = $this->_actionMap[$action]; |
| 69 | }
|
69 | }
|
| 70 | }
|
70 | }
|
| 71 | 71 | ||
| 72 | $this->$actionMethod(); |
72 | $this->$actionMethod(); |
| 73 | }
|
73 | }
|
| 74 | 74 | ||
| 75 | /**
|
75 | /**
|
| 76 | * (non-PHPDoc)
|
76 | * (non-PHPDoc)
|
| 77 | * @see View::assign()
|
77 | * @see View::assign()
|
| 78 | */
|
78 | */
|
| 79 | protected function assign ($name, $value, $encodeHTML = false) |
79 | protected function assign ($name, $value, $encodeHTML = false) |
| 80 | {
|
80 | {
|
| 81 | return $this->_view->assign($name, $value, $encodeHTML); |
81 | return $this->_view->assign($name, $value, $encodeHTML); |
| 82 | }
|
82 | }
|
| 83 | 83 | ||
| 84 | /**
|
84 | /**
|
| 85 | * (non-PHPDoc)
|
85 | * (non-PHPDoc)
|
| 86 | * @see View::addStylesheet()
|
86 | * @see View::addStylesheet()
|
| 87 | */
|
87 | */
|
| 88 | protected function addStylesheet ($uri, $key = null) |
88 | protected function addStylesheet ($uri, $key = null) |
| 89 | {
|
89 | {
|
| 90 | return $this->_view->addStylesheet($uri, $key); |
90 | return $this->_view->addStylesheet($uri, $key); |
| 91 | }
|
91 | }
|
| 92 | 92 | ||
| 93 | /**
|
93 | /**
|
| 94 | * (non-PHPDoc)
|
94 | * (non-PHPDoc)
|
| - | 95 | * @see View::addStylesheets()
|
|
| - | 96 | */
|
|
| - | 97 | protected function addStylesheets (array $uris) |
|
| - | 98 | {
|
|
| - | 99 | return $this->_view->addStylesheets($uris); |
|
| - | 100 | }
|
|
| - | 101 | ||
| - | 102 | /**
|
|
| - | 103 | * (non-PHPDoc)
|
|
| 95 | * @see View::addSscript()
|
104 | * @see View::addSscript()
|
| 96 | */
|
105 | */
|
| 97 | protected function addScript ($uri, $key = null) |
106 | protected function addScript ($uri, $key = null) |
| 98 | {
|
107 | {
|
| 99 | return $this->_view->addScript ($uri, $key); |
108 | return $this->_view->addScript($uri, $key); |
| - | 109 | }
|
|
| - | 110 | ||
| - | 111 | /**
|
|
| - | 112 | * (non-PHPDoc)
|
|
| - | 113 | * @see View::addSscripts()
|
|
| - | 114 | */
|
|
| - | 115 | protected function addScripts (array $uris) |
|
| - | 116 | {
|
|
| - | 117 | return $this->_view->addScripts($uris); |
|
| 100 | }
|
118 | }
|
| 101 | 119 | ||
| 102 | /**
|
120 | /**
|
| 103 | * Renders the {@link View} associated with this controller
|
121 | * Renders the {@link View} associated with this controller
|
| 104 | * by including the <code>View</code>'s template.
|
122 | * by including the <code>View</code>'s template.
|
| 105 | * <code>Controller</code>s should call this method instead of
|
123 | * <code>Controller</code>s should call this method instead of
|
| 106 | * <code>View::render()</code>.
|
124 | * <code>View::render()</code>.
|
| 107 | *
|
125 | *
|
| 108 | * @param string $template
|
126 | * @param string $template
|
| 109 | * Optional alternative template resource path.
|
127 | * Optional alternative template resource path.
|
| 110 | * If not provided, the default template (the
|
128 | * If not provided, the default template (the
|
| 111 | * <code>View</code>'s <code>$template</code> property)
|
129 | * <code>View</code>'s <code>$template</code> property)
|
| 112 | * will be used.
|
130 | * will be used.
|
| 113 | */
|
131 | */
|
| 114 | public function render ($template = null, $content = null) |
132 | public function render ($template = null, $content = null) |
| 115 | {
|
133 | {
|
| 116 | $this->_view->render($template, $content); |
134 | $this->_view->render($template, $content); |
| 117 | }
|
135 | }
|
| 118 | }
|
136 | }
|
| 119 | 137 | ||
| 120 | ?>
|
138 | ?>
|