1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | namespace Aurora\System\Module; |
9: | |
10: | use Aurora\System\Api; |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | class Settings extends \Aurora\System\AbstractSettings |
20: | { |
21: | |
22: | |
23: | |
24: | protected $aTenantSettings = []; |
25: | |
26: | |
27: | |
28: | |
29: | public $ModuleName; |
30: | |
31: | |
32: | |
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: | |
52: | |
53: | |
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: | |
71: | |
72: | |
73: | |
74: | |
75: | public function GetValue($sName, $sDefaultValue = null) |
76: | { |
77: | return $this->GetTenantValue(Api::getTenantName(), $sName, $sDefaultValue); |
78: | } |
79: | |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | |
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: | |