1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class works with site info |
5
|
|
|
* |
6
|
|
|
* @author kolia |
7
|
|
|
*/ |
8
|
|
|
class SiteInfo |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* If TRUE then setting will be saved and getted for each locale |
13
|
|
|
* @var boolean |
14
|
|
|
*/ |
15
|
|
|
public $useLocales = TRUE; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* For items witch are same for all locales |
19
|
|
|
* (only if $useLocales is TRUE) |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $nonLocaleKeys = [ |
23
|
|
|
'siteinfo_logo', |
24
|
|
|
'siteinfo_favicon', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Current locale |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
public $locale; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
public $locales = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $siteinfo; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Path to folder where images will be uploaded |
47
|
|
|
* ATTENTION! serves as url too!!! |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
public $imagesPath = '/uploads/images/'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
public static $siteInfoObject; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Setting class variables |
60
|
|
|
* @param string $locale locale to intiate class with |
61
|
|
|
*/ |
62
|
|
|
public function __construct($locale = NULL) { |
63
|
|
|
if ($this->useLocales === TRUE) { |
64
|
|
|
$this->locale = $locale !== null ? $locale : MY_Controller::getCurrentLocale(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!self::$siteInfoObject) { |
68
|
|
|
$locales_ = CI::$APP->cms_base->get_langs(); |
69
|
|
|
|
70
|
|
|
foreach ($locales_ as $row) { |
71
|
|
|
$this->locales[$row['id']] = $row['identif']; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// getting data from DB |
75
|
|
|
$result = CI::$APP->db->select('siteinfo')->get('settings')->row_array(); |
76
|
|
|
self::$siteInfoObject = unserialize($result['siteinfo']); |
77
|
|
|
if (is_array(self::$siteInfoObject)) { |
78
|
|
|
$this->siteinfo = self::$siteInfoObject; |
79
|
|
|
} |
80
|
|
|
} else { |
81
|
|
|
$this->siteinfo = self::$siteInfoObject; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Sets all params in one array (mostly on saving) |
87
|
|
|
* @param array $siteInfo |
88
|
|
|
*/ |
89
|
|
|
public function setSiteInfoData(array $siteInfo) { |
90
|
|
|
|
91
|
|
|
$languages = CI::$APP->db->get('languages')->result_array(); |
92
|
|
|
|
93
|
|
|
if ($this->useLocales === TRUE) { |
94
|
|
|
if (!array_key_exists($this->locale, $this->siteinfo)) { |
95
|
|
|
$this->siteinfo[$this->locale] = []; |
96
|
|
|
} |
97
|
|
|
foreach ($siteInfo as $key => $value) { |
98
|
|
|
if (in_array($key, $this->nonLocaleKeys)) { |
99
|
|
|
$this->siteinfo[$key] = $value; |
100
|
|
|
} else { |
101
|
|
|
foreach ($languages as $lang) { |
102
|
|
|
if ($lang['identif'] === $this->locale) { |
103
|
|
|
$this->siteinfo[$this->locale][$key] = $value; |
104
|
|
|
} elseif ($key == 'contacts') { |
105
|
|
|
$this->contactsKeys($key, $lang); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} else { |
111
|
|
|
$this->siteinfo = $siteInfo; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $key |
117
|
|
|
* @param string $lang |
118
|
|
|
*/ |
119
|
|
|
private function contactsKeys($key, $lang) { |
120
|
|
|
$siteinfoAll = $this->getSiteInfoData(FALSE); |
121
|
|
|
$locale = MY_Controller::getCurrentLocale(); |
122
|
|
|
$keysMain = []; |
123
|
|
|
|
124
|
|
|
foreach ($siteinfoAll[$locale][$key] as $name => $contacts) { |
125
|
|
|
$contacts = $contacts; |
|
|
|
|
126
|
|
|
$keysMain[] = $name; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
foreach ($keysMain as $contacts) { |
130
|
|
|
if (isset($siteinfoAll[$locale][$key][$contacts])) { |
131
|
|
|
$this->siteinfo[$lang['identif']][$key][$contacts] = $siteinfoAll[$lang['identif']][$key][$contacts]; |
132
|
|
|
} else { |
133
|
|
|
$this->siteinfo[$lang['identif']][$key][$contacts] = ''; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
foreach ($this->siteinfo as $language => $datas) { |
138
|
|
|
foreach ($datas['contacts'] as $k => $data) { |
139
|
|
|
$data = $data; |
|
|
|
|
140
|
|
|
if (!in_array($k, $keysMain)) { |
141
|
|
|
unset($this->siteinfo[$language][$key][$k]); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Saving data in DB |
149
|
|
|
*/ |
150
|
|
|
public function save() { |
151
|
|
|
$this->normalizeData(); |
152
|
|
|
$siteinfo = $this->getSiteInfoData(FALSE); |
153
|
|
|
$string = serialize($siteinfo); |
154
|
|
|
return CI::$APP->db->update('settings', ['siteinfo' => $string]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Setting one value of site informations |
159
|
|
|
* @param string $key |
160
|
|
|
* @param string $value |
161
|
|
|
* @param boolean $contacts (optional, default false) true if value need to be setted in contacts |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public function setSiteInfoValue($key, $value, $contacts = FALSE) { |
165
|
|
|
if (0 !== strpos($key, 'siteinfo_')) { |
166
|
|
|
$key = 'siteinfo_' . $key; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if ($this->useLocales != TRUE || in_array($key, $this->nonLocaleKeys)) { |
170
|
|
|
if ($contacts == TRUE) { |
171
|
|
|
$this->siteinfo['contacts'][$key] = $value; |
172
|
|
|
return TRUE; |
173
|
|
|
} else { |
174
|
|
|
if (array_key_exists($key, $this->siteinfo)) { |
175
|
|
|
$this->siteinfo[$key] = $value; |
176
|
|
|
return TRUE; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} else { |
180
|
|
|
if ($contacts == TRUE) { |
181
|
|
|
$this->siteinfo[$this->locale]['contacts'][$key] = $value; |
182
|
|
|
return TRUE; |
183
|
|
|
} else { |
184
|
|
|
if (array_key_exists($key, $this->siteinfo[$this->locale])) { |
185
|
|
|
$this->siteinfo[$this->locale][$key] = $value; |
186
|
|
|
return TRUE; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param string $key |
195
|
|
|
* @param bool|FALSE $contacts |
196
|
|
|
*/ |
197
|
|
|
public function deleteSiteInfoValue($key, $contacts = FALSE) { |
198
|
|
|
if (0 !== strpos($key, 'siteinfo_')) { |
199
|
|
|
$key = 'siteinfo_' . $key; |
200
|
|
|
} |
201
|
|
|
if ($this->useLocales != TRUE || in_array($key, $this->nonLocaleKeys)) { |
202
|
|
|
if ($contacts == TRUE) { |
203
|
|
|
unset($this->siteinfo['contacts'][$key]); |
204
|
|
|
} else { |
205
|
|
|
unset($this->siteinfo[$key]); |
206
|
|
|
} |
207
|
|
|
} else { |
208
|
|
|
if ($contacts == TRUE) { |
209
|
|
|
unset($this->siteinfo[$this->locale]['contacts'][$key]); |
210
|
|
|
} else { |
211
|
|
|
unset($this->siteinfo[$this->locale][$key]); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Returns all params in one array (for serialize) |
218
|
|
|
* @param boolean $byLocale if true then will be returned data by locale, else all dataS |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
public function getSiteInfoData($byLocale = FALSE) { |
222
|
|
|
if ($this->useLocales == TRUE & $byLocale !== FALSE) { |
|
|
|
|
223
|
|
|
if (is_string($byLocale) & in_array($byLocale, $this->locales)) { |
224
|
|
|
$locale = $byLocale; |
225
|
|
|
} else { |
226
|
|
|
$locale = $this->locale; |
227
|
|
|
} |
228
|
|
|
if (array_key_exists($locale, $this->siteinfo)) { |
229
|
|
|
$returnArray = $this->siteinfo[$locale]; |
230
|
|
|
|
231
|
|
|
$defaultContacts = $this->siteinfo[MY_Controller::defaultLocale()]['contacts']; |
232
|
|
|
foreach ($defaultContacts as $contactKey => $contactValue) { |
233
|
|
|
if (!$returnArray['contacts'][$contactKey]) { |
234
|
|
|
$returnArray['contacts'][$contactKey] = ''; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
foreach ($this->siteinfo as $key => $value) { |
239
|
|
|
if (in_array($key, $this->nonLocaleKeys)) { |
240
|
|
|
$returnArray[$key] = $value; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
return $returnArray; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return $this->siteinfo; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Returns siteinfo item by param |
252
|
|
|
* @param string $name name of param |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
|
|
public function getSiteInfo($name = NULL) { |
256
|
|
|
// simple checks just in case |
257
|
|
|
if (!is_string($name)) { |
258
|
|
|
return ''; |
259
|
|
|
} |
260
|
|
|
if (!(strlen($name) > 0)) { |
261
|
|
|
return ''; |
262
|
|
|
} |
263
|
|
|
if (!is_array($this->siteinfo)) { |
264
|
|
|
return ''; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
if ($this->useLocales == TRUE) { |
268
|
|
|
// if it is non locale field |
269
|
|
|
if (array_key_exists($name, $this->siteinfo)) { |
270
|
|
|
return $this->siteinfo[$name]; |
271
|
|
|
} elseif (isset($this->siteinfo['contacts'])) { |
272
|
|
|
$nameTemp = str_replace('siteinfo_', '', $name); |
273
|
|
|
if (array_key_exists($nameTemp, $this->siteinfo['contacts'])) { |
274
|
|
|
return $this->siteinfo['contacts'][$nameTemp]; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
if (array_key_exists($this->locale, $this->siteinfo)) { |
278
|
|
|
$siteinfo = $this->siteinfo[$this->locale]; |
279
|
|
|
} else { |
280
|
|
|
return ''; |
281
|
|
|
} |
282
|
|
|
} else { |
283
|
|
|
$siteinfo = $this->siteinfo; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
// if key exists value will be returned |
287
|
|
|
if (array_key_exists($name, $siteinfo)) { |
288
|
|
|
return $siteinfo[$name]; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
$name = str_replace('siteinfo_', '', $name); |
292
|
|
|
if (array_key_exists($name, $siteinfo['contacts'])) { |
293
|
|
|
return $siteinfo['contacts'][$name]; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return ''; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Changing array structure relatively to that locales are use or not |
301
|
|
|
*/ |
302
|
|
|
public function normalizeData() { |
303
|
|
|
if ($this->useLocales == TRUE) { |
304
|
|
|
// deleting non locale fields from data array (except those what are present $this->nonLocaleKeys) |
305
|
|
|
foreach ($this->siteinfo as $key => $value) { |
306
|
|
|
if (!in_array($key, $this->locales) & !in_array($key, $this->nonLocaleKeys)) { |
307
|
|
|
unset($this->siteinfo[$key]); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
// deleting non-locale fields from each locale data |
311
|
|
|
foreach ($this->siteinfo as $locale => $localeData) { |
312
|
|
|
foreach ($localeData as $key => $value) { |
313
|
|
|
if (in_array($key, $this->nonLocaleKeys)) { |
314
|
|
|
unset($this->siteinfo[$locale][$key]); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
} else { |
319
|
|
|
// deleting locales data |
320
|
|
|
foreach ($this->siteinfo as $key => $value) { |
321
|
|
|
if (in_array($key, $this->locales)) { |
322
|
|
|
unset($this->siteinfo[$key]); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
} |
This checks looks for cases where a variable has been assigned to itself.
This assignement can be removed without consequences.