Subversion Repositories PHPX

Rev

Rev 29 | 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';
35
 
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;
45
 
46
  /**
27 PointedEar 47
   * The {@link View} used by this controller
48
   * @var View
49
   */
50
  protected $_view = null;
51
 
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
60
   *   is the empty string.
61
   */
30 PointedEar 62
  protected function __construct($viewClass = 'View', $template = null)
27 PointedEar 63
  {
64
    $this->_view = new $viewClass($template);
65
 
66
    Application::getInstance()->setCurrentController($this);
67
 
68
    $action = Application::getParam('action', $_REQUEST);
69
 
70
    /* NOTE: No `==='; treat empty action like no specific action */
71
    if ($action == null)
72
    {
73
      $action = $this->_defaultAction;
74
    }
75
 
30 PointedEar 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();
27 PointedEar 88
  }
89
 
90
  /**
91
   * Assigns a value to a template variable (after this,
92
   * <var>$value</var> is available through
93
   * <code>$this-><var>$name</var></code> in the view's template).
94
   * <code>Controller</code>s should call this method instead of
95
   * {@link View::assign()}.
96
   *
97
   * @param string $name
98
   *   Variable name
99
   * @param mixed $value
100
   *   Variable value
101
   * @param bool $encodeHTML
102
   *   If <code>true</code>, replace all potentially conflicting
103
   *   characters in <var>$value</var> with their HTML entity
104
   *   references.  The default is <code>false</code>.
105
   * @return mixed The assigned value (after possible HTML encoding)
106
   * @see View::encodeHTML()
107
   */
108
  protected function assign($name, $value, $encodeHTML = false)
109
  {
110
    return $this->_view->assign($name, $value, $encodeHTML);
111
  }
112
 
113
  /**
114
   * Renders the {@link View} associated with this controller
115
   * by including the <code>View</code>'s template.
116
   * <code>Controller</code>s should call this method instead of
117
   * <code>View::render()</code>.
118
   *
119
   * @param string $template
120
   *   Optional alternative template resource path.
121
   *   If not provided, the default template (the
122
   *   <code>View</code>'s <code>$template</code> property)
123
   *   will be used.
124
   */
125
  public function render($template = null, $content = null)
126
  {
127
    $this->_view->render($template, $content);
128
  }
129
}
130
 
131
?>