1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\System\Module;
9:
10: use Aurora\System\Api;
11:
12: /**
13: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
14: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
15: * @copyright Copyright (c) 2019, Afterlogic Corp.
16: *
17: * @package Api
18: */
19: class Settings extends \Aurora\System\AbstractSettings
20: {
21: /**
22: * @var \Aurora\System\Module\TenantSettings[]
23: */
24: protected $aTenantSettings = [];
25:
26: /**
27: * @var string
28: */
29: public $ModuleName;
30:
31: /**
32: * @param string $sModuleName
33: */
34: public function __construct($sModuleName)
35: {
36: $this->ModuleName = $sModuleName;
37:
38: parent::__construct(
39: Api::GetModuleManager()->GetModulesSettingsPath() . $sModuleName . '.config.json'
40: );
41:
42: $this->initDefaults();
43: }
44:
45: protected function initDefaults()
46: {
47: $this->aContainer = [];
48: }
49:
50: /**
51: * @param string $sTenantName
52: *
53: * @return \Aurora\System\Module\TenantSettings
54: */
55: public function GetTenantSetttings($sTenantName)
56: {
57: if (!isset($this->aTenantSettings[$sTenantName])) {
58: $oTenantSettings = new TenantSettings(
59: $this->ModuleName,
60: $sTenantName
61: );
62: $oTenantSettings->Load();
63: $this->aTenantSettings[$sTenantName] = $oTenantSettings;
64: }
65:
66: return isset($this->aTenantSettings[$sTenantName]) ? $this->aTenantSettings[$sTenantName] : null;
67: }
68:
69: /**
70: * @param string $sName
71: * @param mixed $sDefaultValue
72: *
73: * @return mixed
74: */
75: public function GetValue($sName, $sDefaultValue = null)
76: {
77: return $this->GetTenantValue(Api::getTenantName(), $sName, $sDefaultValue);
78: }
79:
80: /**
81: * @param string $sTenantName
82: * @param string $sName
83: * @param mixed $sDefaultValue
84: *
85: * @return mixed
86: */
87: public function GetTenantValue($sTenantName, $sName, $sDefaultValue = null)
88: {
89: $mResult = parent::GetValue($sName, $sDefaultValue);
90:
91: if ($this->IsTenantSettingsExists($sTenantName)) {
92: $oTenantSettings = $this->GetTenantSetttings($sTenantName);
93: if ($oTenantSettings !== null && isset($oTenantSettings->{$sName})) {
94: $mResult = $oTenantSettings->GetValue($sName, $sDefaultValue);
95: }
96: }
97:
98: return $mResult;
99: }
100:
101: public function SaveTenantSettings($sTenantName, $aValues = [])
102: {
103: $mResult = false;
104: $oTenantSettings = $this->GetTenantSetttings($sTenantName);
105: foreach ($aValues as $key => $value) {
106: if (!isset($oTenantSettings->{$key}) && isset($this->aContainer[$key])) {
107: $oTenantSettings->SetProperty($key, $this->aContainer[$key]);
108: }
109: $oTenantSettings->SetValue($key, $value);
110: }
111: if ($oTenantSettings !== null) {
112: $mResult = $oTenantSettings->Save();
113: }
114:
115: return $mResult;
116: }
117:
118: public function IsTenantSettingsExists($sTenantName)
119: {
120: return \file_exists(Api::GetModuleManager()->GetModulesSettingsPath() . 'tenants/' . $sTenantName . '/' . $this->ModuleName . '.config.json');
121: }
122: }
123: