1: <?php
2:
3: namespace Budabot\Core;
4:
5: use stdClass;
6:
7: 8: 9:
10: class SettingManager {
11:
12: public $db;
13:
14:
15: public $chatBot;
16:
17:
18: public $util;
19:
20:
21: public $helpManager;
22:
23:
24: public $accessManager;
25:
26:
27: public $logger;
28:
29: public $settings = array();
30:
31: private $changeListeners = array();
32:
33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:
46: public function add($module, $name, $description, $mode, $type, $value, $options = '', $intoptions = '', $accessLevel = 'mod', $help = '') {
47: $name = strtolower($name);
48: $type = strtolower($type);
49:
50: if ($accessLevel == '') {
51: $accessLevel = 'mod';
52: }
53: $accessLevel = $this->accessManager->getAccessLevel($accessLevel);
54:
55: if (!in_array($type, array('color', 'number', 'text', 'options', 'time'))) {
56: $this->logger->log('ERROR', "Error in registering Setting $module:setting($name). Type should be one of: 'color', 'number', 'text', 'options', 'time'. Actual: '$type'.");
57: }
58:
59: if ($type == 'time') {
60: $oldvalue = $value;
61: $value = $this->util->parseTime($value);
62: if ($value < 1) {
63: $this->logger->log('ERROR', "Error in registering Setting $module:setting($name). Invalid time: '{$oldvalue}'.");
64: return;
65: }
66: }
67:
68: if (!empty($help)) {
69: $help = $this->helpManager->checkForHelpFile($module, $help);
70: }
71:
72: try {
73: if (array_key_exists($name, $this->chatBot->existing_settings) || array_key_exists($name, $this->settings)) {
74: $sql = "UPDATE settings_<myname> SET `module` = ?, `type` = ?, `mode` = ?, `options` = ?, `intoptions` = ?, `description` = ?, `admin` = ?, `verify` = 1, `help` = ? WHERE `name` = ?";
75: $this->db->exec($sql, $module, $type, $mode, $options, $intoptions, $description, $accessLevel, $help, $name);
76: } else {
77: $sql = "INSERT INTO settings_<myname> (`name`, `module`, `type`, `mode`, `value`, `options`, `intoptions`, `description`, `source`, `admin`, `verify`, `help`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
78: $this->db->exec($sql, $name, $module, $type, $mode, $value, $options, $intoptions, $description, 'db', $accessLevel, '1', $help);
79: $this->settings[$name] = $value;
80: }
81: } catch (SQLException $e) {
82: $this->logger->log('ERROR', "Error in registering Setting $module:setting($name): " . $e->getMessage());
83: }
84: }
85:
86: 87: 88: 89: 90:
91: public function exists($name) {
92: return array_key_exists($name, $this->settings);
93: }
94:
95: 96: 97: 98: 99:
100: public function get($name) {
101: $name = strtolower($name);
102: if (array_key_exists($name, $this->settings)) {
103: return $this->settings[$name];
104: } else {
105: $this->logger->log("ERROR", "Could not retrieve value for setting '$name' because setting does not exist");
106: return false;
107: }
108: }
109:
110: 111: 112: 113: 114: 115: 116:
117: public function save($name, $value) {
118: $name = strtolower($name);
119:
120: if (array_key_exists($name, $this->settings)) {
121: if ($this->settings[$name] !== $value) {
122:
123: if (isset($this->changeListeners[$name])) {
124: forEach ($this->changeListeners[$name] as $listener) {
125: call_user_func($listener->callback, $name, $this->settings[$name], $value, $listener->data);
126: }
127: }
128: $this->settings[$name] = $value;
129: $this->db->exec("UPDATE settings_<myname> SET `verify` = 1, `value` = ? WHERE `name` = ?", $value, $name);
130: }
131: return true;
132: } else {
133: $this->logger->log("ERROR", "Could not save value '$value' for setting '$name' because setting does not exist");
134: return false;
135: }
136: }
137:
138: public function upload() {
139: $this->settings = array();
140:
141:
142: $data = $this->db->query("SELECT * FROM settings_<myname>");
143: forEach ($data as $row) {
144: $this->settings[$row->name] = $row->value;
145: }
146: }
147:
148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168:
169: public function registerChangeListener($settingName, $callback, $data = null) {
170: if (!is_callable($callback)) {
171: $this->logger->log('ERROR', 'Given callback is not valid.');
172: return;
173: }
174: $settingName = strtolower($settingName);
175:
176: $listener = new StdClass();
177: $listener->callback = $callback;
178: $listener->data = $data;
179: $this->changeListeners[$settingName] []= $listener;
180: }
181:
182: public function getSettingHandler($row) {
183: $handler = null;
184: switch ($row->type) {
185: case 'color':
186: $handler = new ColorSettingHandler($row);
187: break;
188: case 'text':
189: $handler = new TextSettingHandler($row);
190: break;
191: case 'number':
192: $handler = new NumberSettingHandler($row);
193: break;
194: case 'options':
195: $handler = new OptionsSettingHandler($row);
196: break;
197: case 'time':
198: $handler = new TimeSettingHandler($row);
199: break;
200: default:
201: $this->loggger->log('ERROR', "Could not find setting handler for setting type: '$row->type'");
202: }
203: Registry::injectDependencies($handler);
204: return $handler;
205: }
206: }
207: