|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\BaseModel; |
|
6
|
|
|
use Nette\Utils\Json; |
|
7
|
|
|
use Nette\Database\Context; |
|
8
|
|
|
use Tracy\Debugger; |
|
9
|
|
|
use \Exception; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Settings |
|
13
|
|
|
* |
|
14
|
|
|
* class for handling settings |
|
15
|
|
|
* |
|
16
|
|
|
* @created 2012-03-06 |
|
17
|
|
|
* @author Tomas Litera <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class SettingsModel extends BaseModel |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
protected $table = 'kk_settings'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param Context $database |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(Context $database) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->setDatabase($database); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return Nette\Database\Table\ActiveRow |
|
35
|
|
|
*/ |
|
36
|
|
|
public function allOrFail() |
|
37
|
|
|
{ |
|
38
|
|
|
$data = $this->all(); |
|
39
|
|
|
|
|
40
|
|
|
if(!$data) { |
|
|
|
|
|
|
41
|
|
|
throw new Exception('Settings: no data found!'); |
|
42
|
|
|
} else { |
|
43
|
|
|
return $data; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Modify mail subject and message |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $subject e-mail subject |
|
51
|
|
|
* @param string $message e-mail message |
|
52
|
|
|
* @return string error code |
|
53
|
|
|
*/ |
|
54
|
|
|
public function modifyMailJSON($type, $subject, $message) |
|
55
|
|
|
{ |
|
56
|
|
|
$mailData = [ |
|
57
|
|
|
'subject' => $subject, |
|
58
|
|
|
'message' => $message |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
$result = $this->updateByName($mailData, 'mail_' . $type); |
|
62
|
|
|
|
|
63
|
|
|
if(!$result) { |
|
|
|
|
|
|
64
|
|
|
throw new Exception('Mail modification failed!'); |
|
65
|
|
|
} else { |
|
66
|
|
|
return $result; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $type |
|
72
|
|
|
* @return object |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getMailJSON($type) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->findByName('mail_' . $type); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return ActiveRow |
|
81
|
|
|
*/ |
|
82
|
|
|
public function all() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->getDatabase() |
|
85
|
|
|
->table($this->getTable()) |
|
86
|
|
|
->order('name') |
|
87
|
|
|
->fetchAll(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $name |
|
92
|
|
|
* @return ActiveRow |
|
93
|
|
|
*/ |
|
94
|
|
|
public function findByName($name) |
|
95
|
|
|
{ |
|
96
|
|
|
$result = $this->getDatabase() |
|
97
|
|
|
->table($this->getTable()) |
|
98
|
|
|
->where('name', $name) |
|
99
|
|
|
->fetch(); |
|
100
|
|
|
|
|
101
|
|
|
return Json::decode($result->value); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param mixed $value |
|
106
|
|
|
* @param string $name |
|
107
|
|
|
* @return ActiveRow |
|
108
|
|
|
*/ |
|
109
|
|
|
public function updateByName($value, $name) |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->getDatabase() |
|
112
|
|
|
->table($this->getTable()) |
|
113
|
|
|
->where('name', $name) |
|
114
|
|
|
->update($this->encodeValue($value)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param mixed $data |
|
119
|
|
|
* @return Row |
|
120
|
|
|
*/ |
|
121
|
|
|
public function updateDebugRegime($data) |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->updateByName($data, 'debug'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
|
|
public function findDebugRegime() |
|
130
|
|
|
{ |
|
131
|
|
|
return (bool) $this->findByName('debug'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param mixed $value |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function encodeValue($value): array |
|
139
|
|
|
{ |
|
140
|
|
|
return [ |
|
141
|
|
|
'value' => Json::encode($value) |
|
142
|
|
|
]; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.