Subversion Repositories PHPX

Rev

Rev 43 | Rev 52 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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