Subversion Repositories PHPX

Rev

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

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