1 : <?php
2 :
3 : /**
4 : * Dictionaries of Old French and Latin
5 : *
6 : * PHP 5
7 : *
8 : * @category Application
9 : * @package DicFro
10 : * @author Michel Corne <mcorne@yahoo.com>
11 : * @copyright 2008-2010 Michel Corne
12 : * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
13 : * @link http://www.micmap.org/dicfro
14 : */
15 :
16 : require_once 'ViewHelper/Dictionaries.php';
17 : require_once 'ViewHelper/Images.php';
18 : require_once 'ViewHelper/Verbs.php';
19 : require_once 'ViewHelper/Words.php';
20 :
21 : /**
22 : * View engine
23 : *
24 : * @category Application
25 : * @package DicFro
26 : * @author Michel Corne <mcorne@yahoo.com>
27 : * @copyright 2008-2010 Michel Corne
28 : * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
29 : */
30 :
31 : class Engine_View
32 : {
33 :
34 : /**
35 : * Base URL, ex. "http://micmap.org/dicfro"
36 : * @var string
37 : */
38 : public $baseUrl;
39 :
40 : /**
41 : * Configuration directives
42 : * @var array
43 : */
44 : public $config;
45 :
46 : /**
47 : * Dictionaries view helper
48 : * @var object
49 : */
50 : public $dictionariesHelper;
51 :
52 : /**
53 : * Images view helper
54 : * @var object
55 : */
56 : public $imagesHelper;
57 :
58 : /**
59 : * Verbs view helper
60 : * @var object
61 : */
62 : public $verbsHelper;
63 :
64 : /**
65 : * Application version number
66 : * @var string
67 : */
68 : public $version;
69 :
70 : /**
71 : * Directory containing the views
72 : * @var string
73 : */
74 : public $viewsDir;
75 :
76 : /**
77 : * View helper for identified words
78 : * @var object
79 : */
80 : public $wordsHelper;
81 :
82 : /**
83 : * Constructor
84 : *
85 : * @param string $config the configuration directives
86 : * @return void
87 : */
88 : public function __construct($config, $init = true)
89 : {
90 63 : $this->config = $config;
91 63 : $init and $this->init();
92 63 : }
93 :
94 : /**
95 : * Property overloading getter
96 : *
97 : * @param string $property the name of the property to get
98 : * @return null
99 : */
100 : public function __get($property)
101 : {
102 9 : return null;
103 : }
104 :
105 : /**
106 : * Sets resources used by the view
107 : *
108 : * @return void
109 : */
110 : public function init()
111 : {
112 1 : $this->viewsDir = $this->config['views-dir'];
113 1 : $this->version = $this->config['version'];
114 :
115 1 : $this->dictionariesHelper = new ViewHelper_Dictionaries($this);
116 1 : $this->imagesHelper = new ViewHelper_Images($this);
117 1 : $this->verbsHelper = new ViewHelper_Verbs($this);
118 1 : $this->wordsHelper = new ViewHelper_Words($this);
119 :
120 1 : $this->setBaseUrl();
121 1 : }
122 :
123 : /**
124 : * Renders a view
125 : *
126 : * @param string $viewName the name of the view
127 : * @return void
128 : */
129 : public function render($viewName = 'layout.phtml')
130 : {
131 1 : include "{$this->viewsDir}/$viewName";
132 1 : }
133 :
134 : /**
135 : * Sets an action URL
136 : *
137 : * @param string $action the name of the action
138 : * @return string the action URL
139 : */
140 : public function setActionUrl($action = null)
141 : {
142 1 : $actionUrl = $this->baseUrl . '/';
143 1 : empty($this->config['environment']) or $actionUrl .= $this->config['environment'] . '/';
144 1 : $action and $actionUrl .= $action;
145 :
146 1 : return $actionUrl;
147 : }
148 :
149 : /**
150 : * Sets the base URL
151 : *
152 : * @return void
153 : */
154 : public function setBaseUrl()
155 : {
156 2 : $this->baseUrl = 'http://' . $_SERVER['HTTP_HOST'];
157 2 : empty($this->config['base-url']) or $this->baseUrl .= '/' . $this->config['base-url'];
158 2 : }
159 :
160 : /**
161 : * Sets a link URL
162 : *
163 : * @param string $link the name of the link
164 : * @param boolean $withVersionSubDir flag to add the version number or not
165 : * @return string the link URL
166 : */
167 : public function setLinkUrl($link, $withVersionSubDir = true)
168 : {
169 2 : $url = $this->baseUrl;
170 2 : $withVersionSubDir and $url .= '/' . $this->config['version'];
171 2 : $url .= '/' . $link;
172 :
173 2 : return $url;
174 : }
175 :
176 : /**
177 : * Returns an array of properties set dynamically
178 : *
179 : * @param boolean $filter flag to filter out empty properties or not
180 : * @param array $ignore list of properties to ignore
181 : * @return array the list of properties and their values
182 : */
183 : public function toArray($filter = false, $ignore = array())
184 : {
185 17 : settype($ignore, 'array');
186 :
187 : // extracts the object properties excluding the class default properties
188 17 : $dynamicProperties = array_diff_key(get_object_vars($this), get_class_vars(__CLASS__));
189 :
190 17 : foreach($dynamicProperties as $key => $value) {
191 17 : if (in_array($key, $ignore)) {
192 : // ignores the property
193 5 : unset($dynamicProperties[$key]);
194 5 : }
195 17 : }
196 :
197 : // filters out empty properties
198 17 : $filter and $dynamicProperties = array_filter($dynamicProperties);
199 :
200 17 : ksort($dynamicProperties);
201 :
202 17 : return $dynamicProperties;
203 : }
|