Subversion Repositories PHPX

Rev

Rev 29 | 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__ . '/AbstractModel.php';
27 PointedEar 7
 
8
/**
9
 * A general view handled by a controller according to the MVC pattern
10
 *
11
 * @author tlahn
12
 */
13
class View
14
{
15
  /**
16
   * Default template resource path
17
   *
18
   * @var string
19
   */
20
  protected $_template = '';
51 PointedEar 21
 
27 PointedEar 22
  /**
23
   * Content that can be inserted in the template
24
   *
25
   * @var string
26
   */
27
  protected $_content = '';
51 PointedEar 28
 
27 PointedEar 29
  /**
30
   * Template variables.  The variable name serves as item key, the item's value
31
   * is the variable value.
32
   *
33
   * @var array
34
   */
35
  protected $_template_vars = array();
51 PointedEar 36
 
27 PointedEar 37
  /**
38
   * Creates a new view
39
   *
40
   * @param string $template
41
   *   Template resource path
42
   */
43
  public function __construct($template)
44
  {
45
    $this->_template = $template;
46
  }
51 PointedEar 47
 
27 PointedEar 48
  /**
49
   * Magic setter method used for defining template variables
50
   *
51
   * @param string $name
52
   *   Variable name
53
   * @param mixed $value
54
   *   Variable value
55
   */
56
  public function __set($name, $value)
57
  {
58
    $this->_template_vars[$name] = $value;
59
  }
60
 
61
  /**
62
   * Magic getter method used for retrieving values of template variables
63
   *
64
   * @param string $name
65
   *   Variable name
66
   */
67
  public function __get($name)
68
  {
69
    return $this->_template_vars[$name];
70
  }
51 PointedEar 71
 
27 PointedEar 72
  /**
73
   * Returns <var>$v</var> with occurences of '&' (ampersand), '"' (double quote),
74
   * "'" (single quote), '<' (less than), and '>' (greater than) replaced by their
75
   * HTML character entity references, if any, or their numeric HTML character
76
   * reference (as required primarily in HTML for attribute values and element
77
   * content).
78
   *
79
   * @param mixed $value
80
   */
81
  public function escape($value)
82
  {
83
    if (is_array($value))
84
    {
85
      return array_map(array('self', 'escape'), $value);
86
    }
87
    else if (is_object($value))
88
    {
89
      if ($value instanceof AbstractModel)
90
      {
91
        foreach ($value->getPropertyVars() as $varName)
92
        {
93
          $value->$varName = self::escape($value->$varName);
94
        }
95
      }
51 PointedEar 96
 
27 PointedEar 97
      return $value;
98
    }
99
    else
100
    {
101
      if (is_string($value))
102
      {
103
        $encoding = mb_detect_encoding($value);
104
        if ($encoding === 'ASCII')
105
        {
106
          $encoding = 'ISO-8859-1';
107
        }
108
        return htmlspecialchars($value, ENT_QUOTES, $encoding);
109
      }
51 PointedEar 110
 
27 PointedEar 111
      return $value;
112
    }
113
  }
51 PointedEar 114
 
27 PointedEar 115
  /**
116
   * Assigns a value to a template variable
117
   *
118
   * @param string $name
119
   *   Variable name
120
   * @param mixed $value
121
   *   Variable value
122
   * @param bool $escape
123
   *   If <code>true</code>, replace all potentially conflicting characters
124
   *   in <var>$value</var> with their HTML entity references.  The default is
125
   *   <code>false</code>.
126
   * @return mixed The assigned value (after possible HTML encoding)
127
   * @see View::escape()
128
   */
129
  public function assign($name, $value, $escape = false)
130
  {
131
    if ($escape)
132
    {
133
      $value = $this->escape($value);
134
    }
135
 
136
    $this->$name = $value;
137
    return $value;
138
  }
51 PointedEar 139
 
27 PointedEar 140
  /**
141
   * Renders the view by including a template
142
   *
143
   * @param string $template
144
   *   Optional alternative template resource path.
145
   *   If not provided, the default template ($template property) will be used.
146
   * @throws Exception if no template has been defined before
147
   */
148
  public function render($template = null, $content = null)
149
  {
51 PointedEar 150
        if (!is_null($content))
27 PointedEar 151
    {
152
      ob_start();
153
        require_once $content;
154
        $this->_content = ob_get_contents();
155
      ob_end_clean();
156
    }
51 PointedEar 157
 
27 PointedEar 158
        if (!is_null($template))
159
        {
160
                require $template;
161
        }
162
    elseif ($this->_template)
163
    {
164
      require $this->_template;
165
    }
166
    else
167
    {
168
      throw new Exception('No template defined');
169
    }
170
  }
51 PointedEar 171
 
27 PointedEar 172
  /**
173
   * Returns the content for insertion into the template
174
   */
175
  public function getContent()
176
  {
177
    return $this->_content;
178
  }
51 PointedEar 179
 
27 PointedEar 180
  /**
181
   * @param string[optional] $controller
182
   * @param string[optional] $action
183
   * @param int[optional] $id
184
   * @see Application::getURL()
185
   */
186
  public function getURL($controller = null, $action = null, $id = null)
187
  {
188
    return Application::getInstance()->getURL($controller, $action, $id);
189
  }
190
}
191
 
192
?>