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