|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package CleverStyle CMS |
|
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
5
|
|
|
* @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi |
|
6
|
|
|
* @license MIT License, see license.txt |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace cs\Config; |
|
9
|
|
|
use |
|
10
|
|
|
cs\Config; |
|
11
|
|
|
/** |
|
12
|
|
|
* Class for getting of db and storage configuration of module |
|
13
|
|
|
*/ |
|
14
|
|
|
class Module_Properties { |
|
15
|
|
|
const ENABLED = 1; |
|
16
|
|
|
const DISABLED = 0; |
|
17
|
|
|
const UNINSTALLED = -1; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $module_data = []; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $module; |
|
26
|
|
|
/** |
|
27
|
|
|
* Creating of object and saving module data inside |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $module_data |
|
30
|
|
|
* @param string $module |
|
31
|
|
|
*/ |
|
32
|
|
|
function __construct ($module_data, $module) { |
|
33
|
|
|
$this->module_data = $module_data; |
|
34
|
|
|
$this->module = $module; |
|
35
|
|
|
} |
|
36
|
|
|
/** |
|
37
|
|
|
* Whether module is enabled |
|
38
|
|
|
* |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
|
|
function enabled () { |
|
42
|
|
|
return $this->module_data['active'] == self::ENABLED; |
|
43
|
|
|
} |
|
44
|
|
|
/** |
|
45
|
|
|
* Whether module is disabled |
|
46
|
|
|
* |
|
47
|
|
|
* @return bool |
|
48
|
|
|
*/ |
|
49
|
|
|
function disabled () { |
|
50
|
|
|
return $this->module_data['active'] == self::DISABLED; |
|
51
|
|
|
} |
|
52
|
|
|
/** |
|
53
|
|
|
* Whether module is installed |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
function installed () { |
|
58
|
|
|
return $this->module_data['active'] != self::UNINSTALLED; |
|
59
|
|
|
} |
|
60
|
|
|
/** |
|
61
|
|
|
* Whether module is uninstalled |
|
62
|
|
|
* |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
|
|
function uninstalled () { |
|
66
|
|
|
return $this->module_data['active'] == self::UNINSTALLED; |
|
67
|
|
|
} |
|
68
|
|
|
/** |
|
69
|
|
|
* Get db id by name |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $db_name |
|
72
|
|
|
* |
|
73
|
|
|
* @return int |
|
74
|
|
|
*/ |
|
75
|
|
|
function db ($db_name) { |
|
76
|
|
|
return $this->module_data['db'][$db_name]; |
|
77
|
|
|
} |
|
78
|
|
|
/** |
|
79
|
|
|
* Get storage id by name |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $storage_name |
|
82
|
|
|
* |
|
83
|
|
|
* @return int |
|
84
|
|
|
*/ |
|
85
|
|
|
function storage ($storage_name) { |
|
86
|
|
|
return $this->module_data['storage'][$storage_name]; |
|
87
|
|
|
} |
|
88
|
|
|
/** |
|
89
|
|
|
* Get data item of module configuration |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $item |
|
92
|
|
|
* |
|
93
|
|
|
* @return false|mixed |
|
94
|
|
|
*/ |
|
95
|
|
|
function __get ($item) { |
|
96
|
|
|
return $this->get($item); |
|
97
|
|
|
} |
|
98
|
|
|
/** |
|
99
|
|
|
* Set data item of module configuration (only for admin) |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $item |
|
102
|
|
|
* @param mixed $value |
|
103
|
|
|
*/ |
|
104
|
|
|
function __set ($item, $value) { |
|
105
|
|
|
$this->set_internal($item, $value); |
|
106
|
|
|
} |
|
107
|
|
|
/** |
|
108
|
|
|
* Get data item (or array of items) of module configuration |
|
109
|
|
|
* |
|
110
|
|
|
* @param string|string[] $item |
|
111
|
|
|
* |
|
112
|
|
|
* @return false|mixed|mixed[] |
|
113
|
|
|
*/ |
|
114
|
|
|
function get ($item) { |
|
115
|
|
|
if (is_array($item)) { |
|
116
|
|
|
$result = []; |
|
117
|
|
|
foreach ($item as $i) { |
|
118
|
|
|
$result[$i] = $this->get($i); |
|
119
|
|
|
} |
|
120
|
|
|
return $result; |
|
121
|
|
|
} elseif (isset($this->module_data['data'], $this->module_data['data'][$item])) { |
|
122
|
|
|
return $this->module_data['data'][$item]; |
|
123
|
|
|
} else { |
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
/** |
|
128
|
|
|
* Set data item (or array of items) of module configuration (only for admin) |
|
129
|
|
|
* |
|
130
|
|
|
* @param array|string $item |
|
131
|
|
|
* @param mixed|null $value |
|
132
|
|
|
* |
|
133
|
|
|
* @return bool |
|
134
|
|
|
* |
|
135
|
|
|
* @throws \cs\ExitException |
|
136
|
|
|
*/ |
|
137
|
|
|
function set ($item, $value = null) { |
|
138
|
|
|
if (is_array($item)) { |
|
139
|
|
|
foreach ($item as $i => $value) { |
|
140
|
|
|
$this->set_internal($i, $value, false); |
|
141
|
|
|
} |
|
142
|
|
|
return Config::instance()->save(); |
|
143
|
|
|
} else { |
|
144
|
|
|
return $this->set_internal($item, $value); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
protected function set_internal ($item, $value, $save = true) { |
|
148
|
|
|
$Config = Config::instance(); |
|
149
|
|
|
$module_data = &$Config->components['modules'][$this->module]; |
|
150
|
|
|
if (!isset($module_data['data'])) { |
|
151
|
|
|
$module_data['data'] = []; |
|
152
|
|
|
} |
|
153
|
|
|
$module_data['data'][$item] = $value; |
|
154
|
|
|
$this->module_data = $module_data; |
|
155
|
|
|
if ($save) { |
|
156
|
|
|
return $Config->save(); |
|
157
|
|
|
} |
|
158
|
|
|
return true; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|