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