/
section.php
329 lines (293 loc) · 10.8 KB
/
section.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?
defined('C5_EXECUTE') or die("Access Denied.");
class MultilingualSection extends Page {
/**
* @var string
*/
public $msLocale;
/**
* @var string
*/
public $msIcon;
/**
* @var string
*/
public $msLanguage;
public static function assign($c, $language, $icon) {
$db = Loader::db();
$locale = $language . (strlen($icon)?'_'.$icon:'');
$db->Replace('MultilingualSections', array('cID' => $c->getCollectionID(), 'msLanguage' => $language, 'msIcon' => $icon, 'msLocale' => $locale), array('cID'), true);
}
public function unassign() {
$db = Loader::db();
$db->Execute('delete from MultilingualSections where cID = ?', array($this->getCollectionID()));
}
/**
* returns an instance of MultilingualSection for the given page ID
* @param int $cID
* @param int $cvID
* @return MultilingualSection|false
*/
public static function getByID($cID, $cvID = 'RECENT') {
$r = self::isMultilingualSection($cID);
if ($r) {
$obj = parent::getByID($cID, $cvID, 'MultilingualSection');
$obj->msLanguage = $r['msLanguage'];
$obj->msIcon = $r['msIcon'];
$obj->msLocale = $r['msLocale'];
return $obj;
}
return false;
}
/**
* @param string $language
* @return MultilingualSection|false
* @deprecated
*/
public static function getByLanguage($language) {
$db = Loader::db();
$r = $db->GetRow('select cID, msLanguage, msIcon from MultilingualSections where msLanguage = ?', array($language));
if ($r && is_array($r) && $r['msLanguage']) {
$obj = parent::getByID($r['cID'], 'RECENT', 'MultilingualSection');
$obj->msLanguage = $r['msLanguage'];
$obj->msIcon = $r['msIcon'];
return $obj;
}
return false;
}
/**
* @param string $language
* @return MultilingualSection|false
*/
public function getByLocale($locale) {
$db = Loader::db();
$r = $db->GetRow('select cID, msLanguage, msIcon, msLocale from MultilingualSections where msLocale = ?', array($locale));
if ($r && is_array($r) && $r['msLocale']) {
$obj = parent::getByID($r['cID'], 'RECENT', 'MultilingualSection');
$obj->msLanguage = $r['msLanguage'];
$obj->msIcon = $r['msIcon'];
$obj->msLocale = $r['msLocale'];
return $obj;
}
return false;
}
/**
* gets the MultilingualSection object for the current section of the site
* @return MultilingualSection
*/
public static function getCurrentSection() {
static $lang;
if (!isset($lang)) {
$c = Page::getCurrentPage();
if($c instanceof Page) {
$lang = self::getBySectionOfSite($c);
}
}
return $lang;
}
/**
* @param Page $page
* @return MultilingualSection
*/
public static function getBySectionOfSite($page) {
// looks at the page, traverses its parents until it finds the proper language
$nav = Loader::helper('navigation');
$pages = $nav->getTrailToCollection($page);
$pages[] = $page;
$ids = self::getIDList();
foreach($pages as $pc) {
if (in_array($pc->getCollectionID(), $ids)) {
return MultilingualSection::getByID($pc->getCollectionID());
}
}
}
public function getLanguage() {return $this->msLanguage;}
public function getLocale() { return $this->msLocale; }
public function getLanguageText($locale = ACTIVE_LOCALE) {
if (!class_exists('Zend_Locale')) {
Loader::library('3rdparty/Zend/Locale');
}
return Zend_Locale::getTranslation($this->msLanguage, 'language', $locale);
}
public function getIcon() {return $this->msIcon;}
public static function assignDelete($page) {
$db = Loader::db();
$db->Execute('delete from MultilingualSections where cID = ?', array($page->getCollectionID()));
$db->Execute('delete from MultilingualPageRelations where cID = ?', array($page->getCollectionID()));
}
public static function relatePage($oldPage, $newPage, $language) {
$db = Loader::db();
$mpRelationID = $db->GetOne('select mpRelationID from MultilingualPageRelations where cID = ?', array($oldPage->getCollectionID()));
if ($mpRelationID) {
$v = array($mpRelationID, $newPage->getCollectionID(), $language);
$db->Execute('delete from MultilingualPageRelations where mpRelationID = ? and mpLanguage = ?', array($mpRelationID, $language));
$db->Execute('delete from MultilingualPageRelations where cID = ?', array($newPage->getCollectionID()));
$db->Execute('insert into MultilingualPageRelations (mpRelationID, cID, mpLanguage) values (?, ?, ?)', $v);
Events::fire('on_multilingual_page_relate', $newPage, $language);
}
}
public static function isAssigned($page) {
$db = Loader::db();
$mpRelationID = $db->GetOne('select mpRelationID from MultilingualPageRelations where cID = ?', array($page->getCollectionID()));
return $mpRelationID > 0;
}
public static function assignDuplicate($newPage, $oldPage) {
$db = Loader::db();
$mpRelationID = $db->GetOne('select mpRelationID from MultilingualPageRelations where cID = ?', array($oldPage->getCollectionID()));
if (self::isMultilingualSection($newPage)) {
$ms = MultilingualSection::getByID($newPage->getCollectionID());
} else {
$ms = MultilingualSection::getBySectionOfSite($newPage);
}
if (self::isMultilingualSection($oldPage)) {
$msx = MultilingualSection::getByID($oldPage->getCollectionID());
} else {
$msx = MultilingualSection::getBySectionOfSite($oldPage);
}
if (is_object($ms)) {
if (!$mpRelationID) {
$mpRelationID = $db->GetOne('select max(mpRelationID) as mpRelationID from MultilingualPageRelations');
if (!$mpRelationID) {
$mpRelationID = 1;
} else {
$mpRelationID++;
}
$db->Execute('insert into MultilingualPageRelations (mpRelationID, cID, mpLanguage) values (?, ?, ?)', array(
$mpRelationID, $oldPage->getCollectionID(), $msx->getLanguage()
));
}
$v = array($mpRelationID, $newPage->getCollectionID(), $ms->getLanguage());
$cID = $db->GetOne('select cID from MultilingualPageRelations where mpRelationID = ? and mpLanguage = ?', array($mpRelationID, $ms->getLanguage()));
if ($cID < 1) {
$db->Execute('delete from MultilingualPageRelations where mpRelationID = ? and mpLanguage = ?', array($mpRelationID, $ms->getLanguage()));
}
$db->Execute('insert into MultilingualPageRelations (mpRelationID, cID, mpLanguage) values (?, ?, ?)', $v);
/**
* Grabs the multilingual section for the old page, and for the new page, and compares themes
* If they are different, the page gets the theme from the new section
*/
if (is_object($ms) && is_object($msx)) {
if ($ms->getCollectionThemeID() != $msx->getCollectionThemeID()) {
$pt = $ms->getCollectionThemeObject();
$newPage->setTheme($pt);
}
}
Events::fire('on_multilingual_page_relate', $newPage, $language);
}
}
// make sure there is a relations entry in the multilingual table
public static function assignAdd($page) {
$db = Loader::db();
$ms = MultilingualSection::getBySectionOfSite($page);
if (is_object($ms)) {
$mpRelationID = $db->GetOne('select max(mpRelationID) as mpRelationID from MultilingualPageRelations');
if (!$mpRelationID) {
$mpRelationID = 1;
} else {
$mpRelationID++;
}
$v = array($mpRelationID, $page->getCollectionID(), $ms->getLanguage());
$db->Execute('insert into MultilingualPageRelations (mpRelationID, cID, mpLanguage) values (?, ?, ?)', $v);
Events::fire('on_multilingual_page_relate', $page, $language);
}
}
public static function assignMove($page, $oldParent, $newParent) {
if (self::isMultilingualSection($newParent)) {
$ms = MultilingualSection::getByID($newParent->getCollectionID());
} else {
$ms = MultilingualSection::getBySectionOfSite($newParent);
}
if (self::isMultilingualSection($oldParent)) {
$msx = MultilingualSection::getByID($oldParent->getCollectionID());
} else {
$msx = MultilingualSection::getBySectionOfSite($oldParent);
}
$db = Loader::db();
if (is_object($ms)) {
$cID = $db->GetOne('select cID from MultilingualPageRelations where cID = ?', array($page->getCollectionID()));
if (!$cID) {
$mpRelationID = $db->GetOne('select max(mpRelationID) as mpRelationID from MultilingualPageRelations');
if (!$mpRelationID) {
$mpRelationID = 1;
} else {
$mpRelationID++;
}
$v = array($mpRelationID, $page->getCollectionID(), $ms->getLanguage());
$db->Execute('insert into MultilingualPageRelations (mpRelationID, cID, mpLanguage) values (?, ?, ?)', $v);
} else {
$db->Execute('update MultilingualPageRelations set mpLanguage = ? where cID = ?', array($ms->getLanguage(), $page->getCollectionID()));
}
// now we check to see if the new target section has a different theme
if (is_object($ms) && is_object($msx)) {
if ($ms->getCollectionThemeID() != $msx->getCollectionThemeID()) {
$pt = $ms->getCollectionThemeObject();
$page->setTheme($pt);
}
}
} else {
self::assignDelete($page);
}
}
public static function isMultilingualSection($cID) {
if (is_object($cID)) {
$cID = $cID->getCollectionID();
}
$db = Loader::db();
$r = $db->GetRow('select cID, msLanguage, msIcon, msLocale from MultilingualSections where cID = ?', array($cID));
if ($r && is_array($r) && $r['msLanguage']) {
return $r;
} else {
return false;
}
}
public static function ignorePageRelation($page, $language) {
$db = Loader::db();
$mpRelationID = $db->GetOne('select mpRelationID from MultilingualPageRelations where cID = ?', array($page->getCollectionID()));
if ($mpRelationID) {
$v = array($mpRelationID, 0, $language);
$db->Execute('insert into MultilingualPageRelations (mpRelationID, cID, mpLanguage) values (?, ?, ?)', $v);
Events::fire('on_multilingual_page_ignore', $page, $language);
}
}
public static function getIDLIst() {
static $ids;
if (isset($ids)) {
return $ids;
}
$db = Loader::db();
$ids = $db->GetCol('select MultilingualSections.cID from MultilingualSections inner join Pages on MultilingualSections.cID = Pages.cID order by cDisplayOrder asc');
if (!$ids) {
$ids = array();
}
return $ids;
}
public static function getList() {
$ids = self::getIDList();
$pages = array();
if ($ids && is_array($ids)) {
foreach($ids as $cID) {
$obj = self::getByID($cID);
if (is_object($obj)) {
$pages[] = $obj;
}
}
}
return $pages;
}
/**
* Receives a page in a different language tree, and tries to return the corresponding page in the current language tree
*/
public function getTranslatedPageID($page) {
$db = Loader::db();
$ids = MultilingualSection::getIDList();
if (in_array($page->getCollectionID(), $ids)) {
$cID = $db->GetOne('select cID from MultilingualSections where msLanguage = ?', array($this->getLanguage()));
return $cID;
}
$mpRelationID = $db->GetOne('select mpRelationID from MultilingualPageRelations where cID = ?', array($page->getCollectionID()));
if ($mpRelationID) {
$cID = $db->GetOne('select cID from MultilingualPageRelations where mpRelationID = ? and mpLanguage = ?', array($mpRelationID, $this->getLanguage()));
return $cID;
}
}
}