30 public static function get($moduleId, $name, $default =
"", $siteId =
false)
32 $value = static::getRealValue($moduleId, $name, $siteId);
39 if (isset(self::$options[$moduleId][
"-"][$name]))
41 return self::$options[$moduleId][
"-"][$name];
46 $moduleDefaults = static::getDefaults($moduleId);
47 if (isset($moduleDefaults[$name]))
49 return $moduleDefaults[$name];
65 public static function getRealValue($moduleId, $name, $siteId =
false)
76 if (isset(self::$loading[$moduleId]))
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);
81 if (!isset(self::$options[$moduleId]))
83 static::load($moduleId);
86 if ($siteId ===
false)
88 $siteId = static::getDefaultSite();
91 $siteKey = ($siteId ==
""?
"-" : $siteId);
93 if (isset(self::$options[$moduleId][$siteKey][$name]))
95 return self::$options[$moduleId][$siteKey][$name];
110 static $defaultsCache = [];
112 if (isset($defaultsCache[$moduleId]))
114 return $defaultsCache[$moduleId];
117 if (preg_match(
"#[^a-zA-Z0-9._]#", $moduleId))
122 $path = Main\Loader::getLocal(
"modules/".$moduleId.
"/default_option.php");
125 $defaultsCache[$moduleId] = [];
126 return $defaultsCache[$moduleId];
131 $varName = str_replace(
".",
"_", $moduleId).
"_default_option";
132 if (isset(${$varName}) && is_array(${$varName}))
134 $defaultsCache[$moduleId] = ${$varName};
135 return $defaultsCache[$moduleId];
138 $defaultsCache[$moduleId] = [];
139 return $defaultsCache[$moduleId];
157 if (!isset(self::$options[$moduleId]))
159 static::load($moduleId);
162 if ($siteId ===
false)
164 $siteId = static::getDefaultSite();
167 $result = self::$options[$moduleId][
"-"];
169 if($siteId <>
"" && !empty(self::$options[$moduleId][$siteId]))
172 $result = array_replace($result, self::$options[$moduleId][$siteId]);
178 protected static function load($moduleId)
180 $cache = Main\Application::getInstance()->getManagedCache();
181 $cacheTtl = static::getCacheTtl();
184 if ($cacheTtl !==
false)
186 if($cache->read($cacheTtl,
"b_option:{$moduleId}", self::CACHE_DIR))
188 self::$options[$moduleId] = $cache->get(
"b_option:{$moduleId}");
195 self::$loading[$moduleId] =
true;
197 $con = Main\Application::getConnection();
198 $sqlHelper = $con->getSqlHelper();
201 self::$options[$moduleId] = [
"-" => []];
206 WHERE MODULE_ID = '{$sqlHelper->forSql($moduleId)}'
209 $res = $con->query($query);
210 while ($ar = $res->fetch())
212 self::$options[$moduleId][
"-"][$ar[
"NAME"]] = $ar[
"VALUE"];
220 SELECT SITE_ID, NAME, VALUE
222 WHERE MODULE_ID = '{$sqlHelper->forSql($moduleId)}'
225 $res = $con->query($query);
226 while ($ar = $res->fetch())
228 self::$options[$moduleId][$ar[
"SITE_ID"]][$ar[
"NAME"]] = $ar[
"VALUE"];
231 catch(Main\DB\SqlQueryException $e){}
233 if($cacheTtl !==
false)
235 $cache->setImmediate(
"b_option:{$moduleId}", self::$options[$moduleId]);
238 unset(self::$loading[$moduleId]);
253 public static function set($moduleId, $name, $value =
"", $siteId =
"")
264 if (mb_strlen($name) > 100)
266 trigger_error(
"Option name {$name} will be truncated on saving.", E_USER_WARNING);
269 if ($siteId ===
false)
271 $siteId = static::getDefaultSite();
274 $con = Main\Application::getConnection();
275 $sqlHelper = $con->getSqlHelper();
284 "MODULE_ID" => $moduleId,
289 $keyFields = [
"MODULE_ID",
"NAME"];
291 $sql = $sqlHelper->prepareMerge(
"b_option", $keyFields, $insertFields, $updateFields);
296 "MODULE_ID" => $moduleId,
298 "SITE_ID" => $siteId,
302 $keyFields = [
"MODULE_ID",
"NAME",
"SITE_ID"];
304 $sql = $sqlHelper->prepareMerge(
"b_option_site", $keyFields, $insertFields, $updateFields);
307 $con->queryExecute(current($sql));
309 static::clearCache($moduleId);
311 static::loadTriggers($moduleId);
315 "OnAfterSetOption_".$name,
316 array(
"value" => $value)
324 "moduleId" => $moduleId,
379 public static function delete($moduleId, array $filter = array())
386 $con = Main\Application::getConnection();
387 $sqlHelper = $con->getSqlHelper();
389 $deleteForSites =
true;
390 $sqlWhere = $sqlWhereSite =
"";
392 if (isset($filter[
"name"]))
394 if ($filter[
"name"] ==
'')
398 $sqlWhere .=
" AND NAME = '{$sqlHelper->forSql($filter["name
"])}'";
400 if (isset($filter[
"site_id"]))
402 if($filter[
"site_id"] <>
"")
404 $sqlWhereSite =
" AND SITE_ID = '{$sqlHelper->forSql($filter["site_id
"], 2)}'";
408 $deleteForSites =
false;
411 if($moduleId ==
'main')
414 AND NAME NOT LIKE '~%'
415 AND NAME NOT IN ('crc_code', 'admin_passwordh', 'server_uniq_id','PARAM_MAX_SITES', 'PARAM_MAX_USERS')
420 $sqlWhere .=
" AND NAME <> '~bsm_stop_date'";
423 if($sqlWhereSite ==
'')
427 WHERE MODULE_ID = '{$sqlHelper->forSql($moduleId)}'
435 DELETE FROM b_option_site
436 WHERE MODULE_ID = '{$sqlHelper->forSql($moduleId)}'
442 static::clearCache($moduleId);