Subversion Repositories PHPX

Rev

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