Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
option.php
1<?php
2
10namespace Bitrix\Main\Config;
11
12use Bitrix\Main;
13
14class Option
15{
16 protected const CACHE_DIR = "b_option";
17
18 protected static $options = [];
19 protected static $loading = [];
20
30 public static function get($moduleId, $name, $default = "", $siteId = false)
31 {
32 $value = static::getRealValue($moduleId, $name, $siteId);
33
34 if ($value !== null)
35 {
36 return $value;
37 }
38
39 if (isset(self::$options[$moduleId]["-"][$name]))
40 {
41 return self::$options[$moduleId]["-"][$name];
42 }
43
44 if ($default == "")
45 {
46 $moduleDefaults = static::getDefaults($moduleId);
47 if (isset($moduleDefaults[$name]))
48 {
49 return $moduleDefaults[$name];
50 }
51 }
52
53 return $default;
54 }
55
65 public static function getRealValue($moduleId, $name, $siteId = false)
66 {
67 if ($moduleId == '')
68 {
69 throw new Main\ArgumentNullException("moduleId");
70 }
71 if ($name == '')
72 {
73 throw new Main\ArgumentNullException("name");
74 }
75
76 if (isset(self::$loading[$moduleId]))
77 {
78 trigger_error("Options are already in the process of loading for the module {$moduleId}. Default value will be used for the option {$name}.", E_USER_WARNING);
79 }
80
81 if (!isset(self::$options[$moduleId]))
82 {
83 static::load($moduleId);
84 }
85
86 if ($siteId === false)
87 {
88 $siteId = static::getDefaultSite();
89 }
90
91 $siteKey = ($siteId == ""? "-" : $siteId);
92
93 if (isset(self::$options[$moduleId][$siteKey][$name]))
94 {
95 return self::$options[$moduleId][$siteKey][$name];
96 }
97
98 return null;
99 }
100
108 public static function getDefaults($moduleId)
109 {
110 static $defaultsCache = [];
111
112 if (isset($defaultsCache[$moduleId]))
113 {
114 return $defaultsCache[$moduleId];
115 }
116
117 if (preg_match("#[^a-zA-Z0-9._]#", $moduleId))
118 {
119 throw new Main\ArgumentOutOfRangeException("moduleId");
120 }
121
122 $path = Main\Loader::getLocal("modules/".$moduleId."/default_option.php");
123 if ($path === false)
124 {
125 $defaultsCache[$moduleId] = [];
126 return $defaultsCache[$moduleId];
127 }
128
129 include($path);
130
131 $varName = str_replace(".", "_", $moduleId)."_default_option";
132 if (isset(${$varName}) && is_array(${$varName}))
133 {
134 $defaultsCache[$moduleId] = ${$varName};
135 return $defaultsCache[$moduleId];
136 }
137
138 $defaultsCache[$moduleId] = [];
139 return $defaultsCache[$moduleId];
140 }
141
150 public static function getForModule($moduleId, $siteId = false)
151 {
152 if ($moduleId == '')
153 {
154 throw new Main\ArgumentNullException("moduleId");
155 }
156
157 if (!isset(self::$options[$moduleId]))
158 {
159 static::load($moduleId);
160 }
161
162 if ($siteId === false)
163 {
164 $siteId = static::getDefaultSite();
165 }
166
167 $result = self::$options[$moduleId]["-"];
168
169 if($siteId <> "" && !empty(self::$options[$moduleId][$siteId]))
170 {
171 //options for the site override general ones
172 $result = array_replace($result, self::$options[$moduleId][$siteId]);
173 }
174
175 return $result;
176 }
177
178 protected static function load($moduleId)
179 {
180 $cache = Main\Application::getInstance()->getManagedCache();
181 $cacheTtl = static::getCacheTtl();
182 $loadFromDb = true;
183
184 if ($cacheTtl !== false)
185 {
186 if($cache->read($cacheTtl, "b_option:{$moduleId}", self::CACHE_DIR))
187 {
188 self::$options[$moduleId] = $cache->get("b_option:{$moduleId}");
189 $loadFromDb = false;
190 }
191 }
192
193 if($loadFromDb)
194 {
195 self::$loading[$moduleId] = true;
196
197 $con = Main\Application::getConnection();
198 $sqlHelper = $con->getSqlHelper();
199
200 // prevents recursion and cache miss
201 self::$options[$moduleId] = ["-" => []];
202
203 $query = "
204 SELECT NAME, VALUE
205 FROM b_option
206 WHERE MODULE_ID = '{$sqlHelper->forSql($moduleId)}'
207 ";
208
209 $res = $con->query($query);
210 while ($ar = $res->fetch())
211 {
212 self::$options[$moduleId]["-"][$ar["NAME"]] = $ar["VALUE"];
213 }
214
215 try
216 {
217 //b_option_site possibly doesn't exist
218
219 $query = "
220 SELECT SITE_ID, NAME, VALUE
221 FROM b_option_site
222 WHERE MODULE_ID = '{$sqlHelper->forSql($moduleId)}'
223 ";
224
225 $res = $con->query($query);
226 while ($ar = $res->fetch())
227 {
228 self::$options[$moduleId][$ar["SITE_ID"]][$ar["NAME"]] = $ar["VALUE"];
229 }
230 }
231 catch(Main\DB\SqlQueryException $e){}
232
233 if($cacheTtl !== false)
234 {
235 $cache->setImmediate("b_option:{$moduleId}", self::$options[$moduleId]);
236 }
237
238 unset(self::$loading[$moduleId]);
239 }
240
241
242 }
243
253 public static function set($moduleId, $name, $value = "", $siteId = "")
254 {
255 if ($moduleId == '')
256 {
257 throw new Main\ArgumentNullException("moduleId");
258 }
259 if ($name == '')
260 {
261 throw new Main\ArgumentNullException("name");
262 }
263
264 if (mb_strlen($name) > 100)
265 {
266 trigger_error("Option name {$name} will be truncated on saving.", E_USER_WARNING);
267 }
268
269 if ($siteId === false)
270 {
271 $siteId = static::getDefaultSite();
272 }
273
274 $con = Main\Application::getConnection();
275 $sqlHelper = $con->getSqlHelper();
276
277 $updateFields = [
278 "VALUE" => $value,
279 ];
280
281 if($siteId == "")
282 {
283 $insertFields = [
284 "MODULE_ID" => $moduleId,
285 "NAME" => $name,
286 "VALUE" => $value,
287 ];
288
289 $keyFields = ["MODULE_ID", "NAME"];
290
291 $sql = $sqlHelper->prepareMerge("b_option", $keyFields, $insertFields, $updateFields);
292 }
293 else
294 {
295 $insertFields = [
296 "MODULE_ID" => $moduleId,
297 "NAME" => $name,
298 "SITE_ID" => $siteId,
299 "VALUE" => $value,
300 ];
301
302 $keyFields = ["MODULE_ID", "NAME", "SITE_ID"];
303
304 $sql = $sqlHelper->prepareMerge("b_option_site", $keyFields, $insertFields, $updateFields);
305 }
306
307 $con->queryExecute(current($sql));
308
309 static::clearCache($moduleId);
310
311 static::loadTriggers($moduleId);
312
313 $event = new Main\Event(
314 "main",
315 "OnAfterSetOption_".$name,
316 array("value" => $value)
317 );
318 $event->send();
319
320 $event = new Main\Event(
321 "main",
322 "OnAfterSetOption",
323 array(
324 "moduleId" => $moduleId,
325 "name" => $name,
326 "value" => $value,
327 "siteId" => $siteId,
328 )
329 );
330 $event->send();
331 }
332
333 protected static function loadTriggers($moduleId)
334 {
335 static $triggersCache = [];
336
337 if (isset($triggersCache[$moduleId]))
338 {
339 return;
340 }
341
342 if (preg_match("#[^a-zA-Z0-9._]#", $moduleId))
343 {
344 throw new Main\ArgumentOutOfRangeException("moduleId");
345 }
346
347 $triggersCache[$moduleId] = true;
348
349 $path = Main\Loader::getLocal("modules/".$moduleId."/option_triggers.php");
350 if ($path === false)
351 {
352 return;
353 }
354
355 include($path);
356 }
357
358 protected static function getCacheTtl()
359 {
360 static $cacheTtl = null;
361
362 if($cacheTtl === null)
363 {
364 $cacheFlags = Configuration::getValue("cache_flags");
365 $cacheTtl = $cacheFlags["config_options"] ?? 3600;
366 }
367 return $cacheTtl;
368 }
369
379 public static function delete($moduleId, array $filter = array())
380 {
381 if ($moduleId == '')
382 {
383 throw new Main\ArgumentNullException("moduleId");
384 }
385
386 $con = Main\Application::getConnection();
387 $sqlHelper = $con->getSqlHelper();
388
389 $deleteForSites = true;
390 $sqlWhere = $sqlWhereSite = "";
391
392 if (isset($filter["name"]))
393 {
394 if ($filter["name"] == '')
395 {
396 throw new Main\ArgumentNullException("filter[name]");
397 }
398 $sqlWhere .= " AND NAME = '{$sqlHelper->forSql($filter["name"])}'";
399 }
400 if (isset($filter["site_id"]))
401 {
402 if($filter["site_id"] <> "")
403 {
404 $sqlWhereSite = " AND SITE_ID = '{$sqlHelper->forSql($filter["site_id"], 2)}'";
405 }
406 else
407 {
408 $deleteForSites = false;
409 }
410 }
411 if($moduleId == 'main')
412 {
413 $sqlWhere .= "
414 AND NAME NOT LIKE '~%'
415 AND NAME NOT IN ('crc_code', 'admin_passwordh', 'server_uniq_id','PARAM_MAX_SITES', 'PARAM_MAX_USERS')
416 ";
417 }
418 else
419 {
420 $sqlWhere .= " AND NAME <> '~bsm_stop_date'";
421 }
422
423 if($sqlWhereSite == '')
424 {
425 $con->queryExecute("
426 DELETE FROM b_option
427 WHERE MODULE_ID = '{$sqlHelper->forSql($moduleId)}'
428 {$sqlWhere}
429 ");
430 }
431
432 if($deleteForSites)
433 {
434 $con->queryExecute("
435 DELETE FROM b_option_site
436 WHERE MODULE_ID = '{$sqlHelper->forSql($moduleId)}'
437 {$sqlWhere}
438 {$sqlWhereSite}
439 ");
440 }
441
442 static::clearCache($moduleId);
443 }
444
445 protected static function clearCache($moduleId)
446 {
447 unset(self::$options[$moduleId]);
448
449 if (static::getCacheTtl() !== false)
450 {
451 $cache = Main\Application::getInstance()->getManagedCache();
452 $cache->clean("b_option:{$moduleId}", self::CACHE_DIR);
453 }
454 }
455
456 protected static function getDefaultSite()
457 {
458 static $defaultSite;
459
460 if ($defaultSite === null)
461 {
462 $context = Main\Application::getInstance()->getContext();
463 if ($context != null)
464 {
465 $defaultSite = $context->getSite();
466 }
467 }
468 return $defaultSite;
469 }
470}
static loadTriggers($moduleId)
Definition option.php:333
static getForModule($moduleId, $siteId=false)
Definition option.php:150
static getDefaults($moduleId)
Definition option.php:108
static clearCache($moduleId)
Definition option.php:445
static load($moduleId)
Definition option.php:178
static getRealValue($moduleId, $name, $siteId=false)
Definition option.php:65