Subversion Repositories PHPX

Rev

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

Rev 52 Rev 70
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
   * Assigns a value to a template variable (after this,
-
 
77
   * <var>$value</var> is available through
-
 
78
   * <code>$this-><var>$name</var></code> in the view's template).
-
 
79
   * <code>Controller</code>s should call this method instead of
-
 
80
   * {@link View::assign()}.
-
 
81
   *
-
 
82
   * @param string $name
-
 
83
   *   Variable name
76
   * (non-PHPDoc)
84
   * @param mixed $value
-
 
85
   *   Variable value
-
 
86
   * @param bool $encodeHTML
-
 
87
   *   If <code>true</code>, replace all potentially conflicting
-
 
88
   *   characters in <var>$value</var> with their HTML entity
-
 
89
   *   references.  The default is <code>false</code>.
-
 
90
   * @return mixed The assigned value (after possible HTML encoding)
-
 
91
   * @see View::encodeHTML()
77
   * @see View::assign()
92
   */
78
   */
93
  protected function assign($name, $value, $encodeHTML = false)
79
  protected function assign ($name, $value, $encodeHTML = false)
94
  {
80
  {
95
    return $this->_view->assign($name, $value, $encodeHTML);
81
    return $this->_view->assign($name, $value, $encodeHTML);
96
  }
82
  }
97
83
98
  /**
84
  /**
-
 
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)
-
 
95
   * @see View::addSscript()
-
 
96
   */
-
 
97
  protected function addScript ($uri, $key = null)
-
 
98
  {
-
 
99
    return $this->_view->addScript ($uri, $key);
-
 
100
  }
-
 
101
-
 
102
  /**
99
   * Renders the {@link View} associated with this controller
103
   * Renders the {@link View} associated with this controller
100
   * by including the <code>View</code>'s template.
104
   * by including the <code>View</code>'s template.
101
   * <code>Controller</code>s should call this method instead of
105
   * <code>Controller</code>s should call this method instead of
102
   * <code>View::render()</code>.
106
   * <code>View::render()</code>.
103
   *
107
   *
104
   * @param string $template
108
   * @param string $template
105
   *   Optional alternative template resource path.
109
   *   Optional alternative template resource path.
106
   *   If not provided, the default template (the
110
   *   If not provided, the default template (the
107
   *   <code>View</code>'s <code>$template</code> property)
111
   *   <code>View</code>'s <code>$template</code> property)
108
   *   will be used.
112
   *   will be used.
109
   */
113
   */
110
  public function render($template = null, $content = null)
114
  public function render ($template = null, $content = null)
111
  {
115
  {
112
    $this->_view->render($template, $content);
116
    $this->_view->render($template, $content);
113
  }
117
  }
114
}
118
}
115
119
116
?>
120
?>