1 | <?php |
---|
2 | |
---|
3 | |
---|
4 | |
---|
5 | |
---|
6 | |
---|
7 | |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | class JournalSettingsDAO extends DAO { |
---|
19 | function &_getCache($journalId) { |
---|
20 | static $settingCache; |
---|
21 | if (!isset($settingCache)) { |
---|
22 | $settingCache = array(); |
---|
23 | } |
---|
24 | if (!isset($settingCache[$journalId])) { |
---|
25 | import('cache.CacheManager'); |
---|
26 | $cacheManager =& CacheManager::getManager(); |
---|
27 | $settingCache[$journalId] = $cacheManager->getCache( |
---|
28 | 'journalSettings', $journalId, |
---|
29 | array($this, '_cacheMiss') |
---|
30 | ); |
---|
31 | } |
---|
32 | return $settingCache[$journalId]; |
---|
33 | } |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | function &getSetting($journalId, $name, $locale = null) { |
---|
43 | $cache =& $this->_getCache($journalId); |
---|
44 | $returner = $cache->get($name); |
---|
45 | |
---|
46 | if ($name=='commentsInstructionsText') { |
---|
47 | |
---|
48 | |
---|
49 | } |
---|
50 | if ($locale !== null) { |
---|
51 | if (!isset($returner[$locale]) || !is_array($returner)) { |
---|
52 | unset($returner); |
---|
53 | $returner = null; |
---|
54 | return $returner; |
---|
55 | } |
---|
56 | return $returner[$locale]; |
---|
57 | } |
---|
58 | return $returner; |
---|
59 | } |
---|
60 | |
---|
61 | function _cacheMiss(&$cache, $id) { |
---|
62 | $settings =& $this->getJournalSettings($cache->getCacheId()); |
---|
63 | if (!isset($settings[$id])) { |
---|
64 | |
---|
65 | $cache->setCache($id, null); |
---|
66 | return null; |
---|
67 | } |
---|
68 | return $settings[$id]; |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | function &getJournalSettings($journalId) { |
---|
77 | $journalSettings = array(); |
---|
78 | |
---|
79 | $result = &$this->retrieve( |
---|
80 | 'SELECT setting_name, setting_value, setting_type, locale FROM journal_settings WHERE journal_id = ?', $journalId |
---|
81 | ); |
---|
82 | |
---|
83 | if ($result->RecordCount() == 0) { |
---|
84 | $returner = null; |
---|
85 | $result->Close(); |
---|
86 | return $returner; |
---|
87 | |
---|
88 | } else { |
---|
89 | while (!$result->EOF) { |
---|
90 | $row = &$result->getRowAssoc(false); |
---|
91 | $value = $this->convertFromDB($row['setting_value'], $row['setting_type']); |
---|
92 | if ($row['locale'] == '') $journalSettings[$row['setting_name']] = $value; |
---|
93 | else $journalSettings[$row['setting_name']][$row['locale']] = $value; |
---|
94 | $result->MoveNext(); |
---|
95 | } |
---|
96 | $result->close(); |
---|
97 | unset($result); |
---|
98 | |
---|
99 | $cache =& $this->_getCache($journalId); |
---|
100 | $cache->setEntireCache($journalSettings); |
---|
101 | |
---|
102 | return $journalSettings; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | function updateSetting($journalId, $name, $value, $type = null, $isLocalized = false) { |
---|
115 | |
---|
116 | $cache =& $this->_getCache($journalId); |
---|
117 | $cache->setCache($name, $value); |
---|
118 | |
---|
119 | $keyFields = array('setting_name', 'locale', 'journal_id'); |
---|
120 | |
---|
121 | if (!$isLocalized) { |
---|
122 | $value = $this->convertToDB($value, $type); |
---|
123 | $this->replace('journal_settings', |
---|
124 | array( |
---|
125 | 'journal_id' => $journalId, |
---|
126 | 'setting_name' => $name, |
---|
127 | 'setting_value' => $value, |
---|
128 | 'setting_type' => $type, |
---|
129 | 'locale' => '' |
---|
130 | ), |
---|
131 | $keyFields |
---|
132 | ); |
---|
133 | } else { |
---|
134 | if (is_array($value)) foreach ($value as $locale => $localeValue) { |
---|
135 | $this->update('DELETE FROM journal_settings WHERE journal_id = ? AND setting_name = ? AND locale = ?', array($journalId, $name, $locale)); |
---|
136 | if (empty($localeValue)) continue; |
---|
137 | $type = null; |
---|
138 | $this->update('INSERT INTO journal_settings |
---|
139 | (journal_id, setting_name, setting_value, setting_type, locale) |
---|
140 | VALUES (?, ?, ?, ?, ?)', |
---|
141 | array( |
---|
142 | $journalId, $name, $this->convertToDB($localeValue, $type), $type, $locale |
---|
143 | ) |
---|
144 | ); |
---|
145 | } |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | function deleteSetting($journalId, $name, $locale = null) { |
---|
155 | $cache =& $this->_getCache($journalId); |
---|
156 | $cache->setCache($name, null); |
---|
157 | |
---|
158 | $params = array($journalId, $name); |
---|
159 | $sql = 'DELETE FROM journal_settings WHERE journal_id = ? AND setting_name = ?'; |
---|
160 | if ($locale !== null) { |
---|
161 | $params[] = $locale; |
---|
162 | $sql .= ' AND locale = ?'; |
---|
163 | } |
---|
164 | |
---|
165 | return $this->update($sql, $params); |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | function deleteSettingsByJournal($journalId) { |
---|
173 | $cache =& $this->_getCache($journalId); |
---|
174 | $cache->flush(); |
---|
175 | |
---|
176 | return $this->update( |
---|
177 | 'DELETE FROM journal_settings WHERE journal_id = ?', $journalId |
---|
178 | ); |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | function _performReplacement($rawInput, $paramArray = array()) { |
---|
188 | $value = preg_replace_callback('{{translate key="([^"]+)"}}', '_installer_regexp_callback', $rawInput); |
---|
189 | foreach ($paramArray as $pKey => $pValue) { |
---|
190 | $value = str_replace('{$' . $pKey . '}', $pValue, $value); |
---|
191 | } |
---|
192 | return $value; |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | |
---|
201 | function &_buildObject (&$node, $paramArray = array()) { |
---|
202 | $value = array(); |
---|
203 | foreach ($node->getChildren() as $element) { |
---|
204 | $key = $element->getAttribute('key'); |
---|
205 | $childArray = &$element->getChildByName('array'); |
---|
206 | if (isset($childArray)) { |
---|
207 | $content = $this->_buildObject($childArray, $paramArray); |
---|
208 | } else { |
---|
209 | $content = $this->_performReplacement($element->getValue(), $paramArray); |
---|
210 | } |
---|
211 | if (!empty($key)) { |
---|
212 | $key = $this->_performReplacement($key, $paramArray); |
---|
213 | $value[$key] = $content; |
---|
214 | } else $value[] = $content; |
---|
215 | } |
---|
216 | return $value; |
---|
217 | } |
---|
218 | |
---|
219 | |
---|
220 | |
---|
221 | |
---|
222 | |
---|
223 | |
---|
224 | |
---|
225 | function installSettings($journalId, $filename, $paramArray = array()) { |
---|
226 | $xmlParser = &new XMLParser(); |
---|
227 | $tree = $xmlParser->parse($filename); |
---|
228 | |
---|
229 | if (!$tree) { |
---|
230 | $xmlParser->destroy(); |
---|
231 | return false; |
---|
232 | } |
---|
233 | |
---|
234 | foreach ($tree->getChildren() as $setting) { |
---|
235 | $nameNode = &$setting->getChildByName('name'); |
---|
236 | $valueNode = &$setting->getChildByName('value'); |
---|
237 | |
---|
238 | if (isset($nameNode) && isset($valueNode)) { |
---|
239 | $type = $setting->getAttribute('type'); |
---|
240 | $isLocaleField = $setting->getAttribute('locale'); |
---|
241 | $name = &$nameNode->getValue(); |
---|
242 | |
---|
243 | if ($type == 'object') { |
---|
244 | $arrayNode = &$valueNode->getChildByName('array'); |
---|
245 | $value = $this->_buildObject($arrayNode, $paramArray); |
---|
246 | } else { |
---|
247 | $value = $this->_performReplacement($valueNode->getValue(), $paramArray); |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | $this->updateSetting( |
---|
252 | $journalId, |
---|
253 | $name, |
---|
254 | $isLocaleField?array(Locale::getLocale() => $value):$value, |
---|
255 | $type, |
---|
256 | $isLocaleField |
---|
257 | ); |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | $xmlParser->destroy(); |
---|
262 | |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | |
---|
267 | |
---|
268 | |
---|
269 | |
---|
270 | |
---|
271 | |
---|
272 | function _performLocalizedReplacement($rawInput, $paramArray = array(), $locale = null) { |
---|
273 | $value = preg_replace_callback('{{translate key="([^"]+)"}}', |
---|
274 | |
---|
275 | create_function('$matches', |
---|
276 | '$locale = "' . $locale . '";'. |
---|
277 | '$localeFileName = Locale::getMainLocaleFilename($locale);'. |
---|
278 | '$localeFile =& new LocaleFile($locale, $localeFileName);'. |
---|
279 | 'return $localeFile->translate($matches[1]);' |
---|
280 | ), |
---|
281 | $rawInput); |
---|
282 | foreach ($paramArray as $pKey => $pValue) { |
---|
283 | $value = str_replace('{$' . $pKey . '}', $pValue, $value); |
---|
284 | } |
---|
285 | return $value; |
---|
286 | } |
---|
287 | |
---|
288 | |
---|
289 | |
---|
290 | |
---|
291 | |
---|
292 | |
---|
293 | |
---|
294 | |
---|
295 | function &_buildLocalizedObject (&$node, $paramArray = array(), $locale = null) { |
---|
296 | $value = array(); |
---|
297 | foreach ($node->getChildren() as $element) { |
---|
298 | $key = $element->getAttribute('key'); |
---|
299 | $childArray = &$element->getChildByName('array'); |
---|
300 | if (isset($childArray)) { |
---|
301 | $content = $this->_buildLocalizedObject($childArray, $paramArray, $locale); |
---|
302 | } else { |
---|
303 | $content = $this->_performLocalizedReplacement($element->getValue(), $paramArray, $locale); |
---|
304 | } |
---|
305 | if (!empty($key)) { |
---|
306 | $key = $this->_performLocalizedReplacement($key, $paramArray, $locale); |
---|
307 | $value[$key] = $content; |
---|
308 | } else $value[] = $content; |
---|
309 | } |
---|
310 | return $value; |
---|
311 | } |
---|
312 | |
---|
313 | |
---|
314 | |
---|
315 | |
---|
316 | |
---|
317 | |
---|
318 | |
---|
319 | |
---|
320 | function reloadLocalizedDefaultSettings($journalId, $filename, $paramArray, $locale) { |
---|
321 | $xmlParser = &new XMLParser(); |
---|
322 | $tree = $xmlParser->parse($filename); |
---|
323 | |
---|
324 | if (!$tree) { |
---|
325 | $xmlParser->destroy(); |
---|
326 | return false; |
---|
327 | } |
---|
328 | |
---|
329 | foreach ($tree->getChildren() as $setting) { |
---|
330 | $nameNode = &$setting->getChildByName('name'); |
---|
331 | $valueNode = &$setting->getChildByName('value'); |
---|
332 | |
---|
333 | if (isset($nameNode) && isset($valueNode)) { |
---|
334 | $type = $setting->getAttribute('type'); |
---|
335 | $isLocaleField = $setting->getAttribute('locale'); |
---|
336 | $name = &$nameNode->getValue(); |
---|
337 | |
---|
338 | |
---|
339 | if (!$isLocaleField) continue; |
---|
340 | |
---|
341 | if ($type == 'object') { |
---|
342 | $arrayNode = &$valueNode->getChildByName('array'); |
---|
343 | $value = $this->_buildLocalizedObject($arrayNode, $paramArray, $locale); |
---|
344 | } else { |
---|
345 | $value = $this->_performLocalizedReplacement($valueNode->getValue(), $paramArray, $locale); |
---|
346 | } |
---|
347 | |
---|
348 | |
---|
349 | $this->updateSetting( |
---|
350 | $journalId, |
---|
351 | $name, |
---|
352 | array($locale => $value), |
---|
353 | $type, |
---|
354 | true |
---|
355 | ); |
---|
356 | } |
---|
357 | } |
---|
358 | |
---|
359 | $xmlParser->destroy(); |
---|
360 | |
---|
361 | } |
---|
362 | } |
---|
363 | |
---|
364 | |
---|
365 | |
---|
366 | |
---|
367 | function _installer_regexp_callback($matches) { |
---|
368 | return Locale::translate($matches[1]); |
---|
369 | } |
---|
370 | |
---|
371 | ?> |
---|