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: /**
11: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
12: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
13: * @copyright Copyright (c) 2019, Afterlogic Corp.
14: *
15: * @package Api
16: */
17: class Settings extends \Aurora\System\AbstractSettings
18: {
19: /**
20: * @var \Aurora\System\Module\TenantSettings[]
21: */
22: protected $aTenantSettings = [];
23:
24: /**
25: * @var string
26: */
27: public $ModuleName;
28:
29: /**
30: * @param string $sModuleName
31: */
32: public function __construct($sModuleName)
33: {
34: $this->ModuleName = $sModuleName;
35:
36: parent::__construct(
37: \Aurora\System\Api::GetModuleManager()->GetModulesSettingsPath() . $sModuleName . '.config.json'
38: );
39: }
40:
41: public function GetDefaultConfigFilePath()
42: {
43: return \Aurora\System\Api::GetModuleManager()->GetModulesRootPath() . $this->ModuleName . '/config.json';
44: }
45:
46: public function InitDefaultConfiguration()
47: {
48: if (\file_exists($this->GetDefaultConfigFilePath())) {
49: $sModulesSettingsPath = \Aurora\System\Api::GetModuleManager()->GetModulesSettingsPath();
50: if (!\file_exists($sModulesSettingsPath)) {
51: \set_error_handler(function () {
52: });
53: \mkdir($sModulesSettingsPath, 0777);
54: \restore_error_handler();
55: if (!\file_exists($sModulesSettingsPath)) {
56: return;
57: }
58: }
59: \copy($this->GetDefaultConfigFilePath(), $this->sPath);
60: }
61: }
62:
63: public function Load($bForceLoad = false)
64: {
65: $bResult = false;
66: if ($this->bIsLoaded && !$bForceLoad) {
67: $bResult = true;
68: } else {
69: if (!\file_exists($this->sPath)) {
70: $mData = $this->LoadDataFromFile($this->GetDefaultConfigFilePath());
71: } else {
72: $mData = $this->LoadDataFromFile($this->sPath);
73: }
74:
75: if (!$mData) {
76: $mData = $this->LoadDataFromFile($this->sPath.'.bak');
77: if ($mData) {
78: \copy($this->sPath.'.bak', $this->sPath);
79: } else {
80: $this->Save();
81: }
82: }
83:
84: if ($mData !== false) {
85: $this->aContainer = $this->ParseData($mData);
86: $this->bIsLoaded = true;
87: $bResult = true;
88: }
89: }
90:
91: return $bResult;
92: }
93:
94: /**
95: * @param string $sTenantName
96: *
97: * @return \Aurora\System\Module\TenantSettings
98: */
99: public function GetTenantSetttings($sTenantName)
100: {
101: if (!isset($this->aTenantSettings[$sTenantName]) && $this->IsTenantSettingsExists($sTenantName)) {
102: $this->aTenantSettings[$sTenantName] = new TenantSettings(
103: $this->ModuleName,
104: $sTenantName
105: );
106: }
107:
108: return isset($this->aTenantSettings[$sTenantName]) ? $this->aTenantSettings[$sTenantName] : null;
109: }
110:
111: /**
112: * @param string $sName
113: * @param mixed $sDefaultValue
114: *
115: * @return mixed
116: */
117: public function GetValue($sName, $sDefaultValue = null)
118: {
119: return $this->GetTenantValue(\Aurora\System\Api::getTenantName(), $sName, $sDefaultValue);
120: }
121:
122: /**
123: * @param string $sName
124: * @param mixed $sDefaultValue
125: *
126: * @return mixed
127: */
128: public function GetTenantValue($sTenantName, $sName, $sDefaultValue = null)
129: {
130: $mResult = $sDefaultValue;
131:
132: $oTenantSettings = $this->GetTenantSetttings(
133: $sTenantName
134: );
135:
136: if (isset($oTenantSettings) && isset($oTenantSettings->{$sName})) {
137: $mResult = $oTenantSettings->GetValue($sName, $sDefaultValue);
138: } else {
139: $mResult = parent::GetValue($sName, $sDefaultValue);
140: }
141:
142:
143: return $mResult;
144: }
145:
146: /**
147: * @param string $sTenantName
148: * @param string $sName
149: * @param string $sValue
150: */
151: public function SetTenantValue($sTenantName, $sName, $sValue = null)
152: {
153: if (isset($this->{$sName})) {
154: $oTenantSettings = null;
155: if (!isset($this->aTenantSettings[$sTenantName])) {
156: $oTenantSettings = new TenantSettings(
157: $this->ModuleName,
158: $sTenantName
159: );
160: } else {
161: $oTenantSettings = $this->aTenantSettings[$sTenantName];
162: }
163:
164: if (isset($oTenantSettings)) {
165: if (!isset($oTenantSettings->{$sName}) && isset($this->aContainer[$sName])) {
166: $oTenantSettings->SetProperty($this->aContainer[$sName]);
167: }
168: $oTenantSettings->SetValue($sName, $sValue);
169: $this->aTenantSettings[$sTenantName] = $oTenantSettings;
170: } else {
171: throw new \Aurora\System\Exceptions\SettingsException();
172: }
173: } else {
174: throw new \Aurora\System\Exceptions\SettingsException();
175: }
176: }
177:
178: public function SaveTenantSettings($sTenantName)
179: {
180: $mResult = false;
181: $oTenantSettings = $this->GetTenantSetttings($sTenantName);
182: if (isset($oTenantSettings)) {
183: $mResult = $oTenantSettings->Save();
184: }
185:
186: return $mResult;
187: }
188:
189: public function GetDefaultConfigValues()
190: {
191: $oDefaultSettings = new DefaultSettings($this->GetDefaultConfigFilePath());
192: $oDefaultSettings->Load();
193: return $oDefaultSettings->GetValues();
194: }
195:
196: public function IsTenantSettingsExists($sTenantName)
197: {
198: return \file_exists(\Aurora\System\Api::GetModuleManager()->GetModulesSettingsPath() . 'tenants/' . $sTenantName . '/' . $this->ModuleName . '.config.json');
199: }
200: }
201: