|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (C) 2016 Tony Murray <[email protected]> |
|
4
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
5
|
|
|
* it under the terms of the GNU General Public License as published by |
|
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
7
|
|
|
* (at your option) any later version. |
|
8
|
|
|
* |
|
9
|
|
|
* This program is distributed in the hope that it will be useful, |
|
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
|
12
|
|
|
* GNU General Public License for more details. |
|
13
|
|
|
* |
|
14
|
|
|
* You should have received a copy of the GNU General Public License |
|
15
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16
|
|
|
*/ |
|
17
|
|
|
/** |
|
18
|
|
|
* DatabaseRepository.php |
|
19
|
|
|
* |
|
20
|
|
|
* @package LibreNMS |
|
21
|
|
|
* @author Tony Murray <[email protected]> |
|
22
|
|
|
* @copyright 2016 Tony Murray |
|
23
|
|
|
* @license @license http://opensource.org/licenses/GPL-3.0 GNU Public License v3 or later |
|
24
|
|
|
*/ |
|
25
|
|
|
namespace App\Settings; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
use App\Models\DbConfig; |
|
29
|
|
|
use App\Models\Notification; |
|
30
|
|
|
|
|
31
|
|
|
class DatabaseRepository |
|
32
|
|
|
{ |
|
33
|
|
|
// 'config_name', 'config_value' |
|
34
|
|
|
|
|
35
|
|
|
public function has($key) |
|
36
|
|
|
{ |
|
37
|
|
|
return DbConfig::exactKey($key)->exists(); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function get($key, $default = null) |
|
41
|
|
|
{ |
|
42
|
|
|
$results = DbConfig::key($key)->get(['config_name', 'config_value']); |
|
43
|
|
|
if (count($results) > 1) { |
|
44
|
|
|
return $this->collectionToArray($results); |
|
45
|
|
|
} |
|
46
|
|
|
elseif (count($results) == 1) { |
|
47
|
|
|
$entry = $results->first(); |
|
48
|
|
|
if($entry->config_name != $key) { //FIXME: better test |
|
49
|
|
|
// trim the prefix |
|
50
|
|
|
$local_key = substr($entry->config_name, strlen($key) + 1); |
|
51
|
|
|
return [$local_key => $entry->config_value]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$value = $entry->config_value; |
|
55
|
|
|
return is_null($value) ? $default : $value; |
|
56
|
|
|
} |
|
57
|
|
|
else { |
|
58
|
|
|
return $default; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function collectionToArray($collection) |
|
63
|
|
|
{ |
|
64
|
|
|
$ret = array(); |
|
65
|
|
|
foreach ($collection as $item) { |
|
66
|
|
|
$ret[$item->config_name] = $item->config_value; |
|
67
|
|
|
} |
|
68
|
|
|
return $ret; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function set($key, $value = null) |
|
72
|
|
|
{ |
|
73
|
|
|
$config = new DbConfig; |
|
74
|
|
|
$config->config_name = $key; |
|
|
|
|
|
|
75
|
|
|
$config->config_value = $value; |
|
|
|
|
|
|
76
|
|
|
//FIXME: dummy data |
|
77
|
|
|
$config->config_default = ""; |
|
|
|
|
|
|
78
|
|
|
$config->config_descr = ""; |
|
|
|
|
|
|
79
|
|
|
$config->config_group = ""; |
|
|
|
|
|
|
80
|
|
|
$config->config_group_order = ""; |
|
|
|
|
|
|
81
|
|
|
$config->config_sub_group = ""; |
|
|
|
|
|
|
82
|
|
|
$config->config_sub_group_order = ""; |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
$config->save(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function reset($key) |
|
88
|
|
|
{ |
|
89
|
|
|
DbConfig::key($key)->delete(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function forget($key) |
|
93
|
|
|
{ |
|
94
|
|
|
// set to null to prevent falling back to Config |
|
95
|
|
|
DbConfig::key($key)->update(['config_value' => null]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function all() |
|
99
|
|
|
{ |
|
100
|
|
|
$settings = DbConfig::all(); |
|
101
|
|
|
return $this->objsToArray($settings); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function objsToArray($objs) |
|
105
|
|
|
{ |
|
106
|
|
|
$ret = array(); |
|
107
|
|
|
foreach ($objs as $obj) { |
|
108
|
|
|
$ret[$obj->key] = $obj->value; |
|
109
|
|
|
} |
|
110
|
|
|
return $ret; |
|
111
|
|
|
} |
|
112
|
|
|
} |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.