-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-settings.php
183 lines (157 loc) · 5.8 KB
/
user-settings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace Hexapla;
require_once "sql-functions.php";
require_once "cookie-functions.php";
$db = $db ?? null;
class UserSettings
{
const USE_SAVED_TRANSLATIONS = 'saved';
const USE_LAST_TRANSLATIONS = 'last';
const SETTINGS_SETUP = [HexaplaSettings::DEFAULT_LOAD => 'tlSetting',
HexaplaSettings::SAVED_TLS => 'tlList',
HexaplaSettings::DIFF_BY_WORD => 'diffByWord',
HexaplaSettings::CASE_SENS_DIFF => 'caseSensitiveDiff',
HexaplaSettings::SCROLL_TOGETHER => 'scrollTogether',
HexaplaSettings::PIN_SIDEBAR => 'pinSidebar'];
const SETTINGS_DEFAULTS = [HexaplaSettings::DEFAULT_LOAD => self::USE_LAST_TRANSLATIONS,
HexaplaSettings::SAVED_TLS => 1,
HexaplaSettings::DIFF_BY_WORD => true,
HexaplaSettings::CASE_SENS_DIFF => false,
HexaplaSettings::SCROLL_TOGETHER => false,
HexaplaSettings::PIN_SIDEBAR => false];
private string $tlSetting;
private string $tlList;
private int $allowsBehavior;
private int $id;
private bool $diffByWord;
private bool $caseSensitiveDiff;
private bool $scrollTogether;
private bool $pinSidebar;
public function __construct($userId, $settingsList = [], $permissions = -1) {
$this->id = $userId;
if ($permissions === -1) {
$permResult = getData($db,
HexaplaTables::USER_GROUP,
[HexaplaUserGroup::ALLOWS_ACTIONS],
[HexaplaTables::USER . "." . HexaplaUser::ID => $userId], [],
[new HexaplaJoin(HexaplaTables::USER,
HexaplaTables::USER_GROUP,HexaplaUserGroup::ID,
HexaplaTables::USER, HexaplaUser::GROUP_ID)]);
$permData = pg_fetch_assoc($permResult);
$permissions = bindec($permData[HexaplaUserGroup::ALLOWS_ACTIONS]);
}
$this->allowsBehavior = $permissions;
if (count($settingsList) === 0) {
$setResult = getData($db, HexaplaTables::USER_SETTINGS, [], [HexaplaUserSettings::USER_ID => $userId]);
while (($row = pg_fetch_assoc($setResult)) !== false) {
$settingsList[$row[HexaplaUserSettings::SETTING]] = $row[HexaplaUserSettings::VALUE];
}
}
foreach(self::SETTINGS_SETUP as $sqlSetting => $objSetting) {
if (!isset($settingsList[$sqlSetting])) $settingsList[$sqlSetting] = self::SETTINGS_DEFAULTS[$sqlSetting];
if (in_array($settingsList[$sqlSetting], ['false', 'f'])) $settingsList[$sqlSetting] = false;
$value = $settingsList[$sqlSetting];
$this->$objSetting = $value;
$cookie = HexaplaCookies::cookieFromSetting($sqlSetting);
if ($cookie !== '') {
setHexCookie($cookie, $value);
}
}
}
public function set_tlSetting($setting) {
$this->tlSetting = $setting;
$this->save();
}
public function set_tlList($list) {
$this->tlList = $list;
$this->save();
}
public function useSavedTl(): bool {
return $this->tlSetting === self::USE_SAVED_TRANSLATIONS;
}
public function useLastTl(): bool {
return $this->tlSetting === self::USE_LAST_TRANSLATIONS;
}
public function pinSidebar() {
return $this->pinSidebar;
}
public function set_pinSidebar($pin) {
$this->pinSidebar = $pin;
$this->save();
}
public function scrollsTogether() {
return $this->scrollTogether;
}
public function set_scroll($together) {
$this->scrollTogether = $together;
$this->save();
}
public function savedTls(): string {
return $this->tlList;
}
public function canWriteNotes(): bool {
return $this->allowsBehavior & HexaplaPermissions::NOTE;
}
public function id(): int {
return $this->id;
}
public function diffsByWord(): bool {
return $this->diffByWord;
}
public function diffsCaseSens(): bool {
return $this->caseSensitiveDiff;
}
public function set_diffsByWord($value) {
$this->diffByWord = $value;
$this->save();
}
public function set_diffsCaseSens($value) {
$this->caseSensitiveDiff = $value;
$this->save();
}
private function save() {
$insertArray = [];
foreach(self::SETTINGS_SETUP as $sqlSetting => $objSetting) {
$insertArray[] = [HexaplaUserSettings::USER_ID => $this->id,
HexaplaUserSettings::SETTING => $sqlSetting,
HexaplaUserSettings::VALUE => $this->$objSetting];
}
putData($db, HexaplaTables::USER_SETTINGS, $insertArray);
}
public function set($setting, $value) {
switch($setting) {
case HexaplaSettings::DEFAULT_LOAD:
$this->set_tlSetting($value);
break;
case HexaplaSettings::PIN_SIDEBAR:
$this->set_pinSidebar($value);
break;
case HexaplaSettings::SCROLL_TOGETHER:
$this->set_scroll($value);
break;
case HexaplaSettings::CASE_SENS_DIFF:
$this->set_diffsCaseSens($value);
break;
case HexaplaSettings::DIFF_BY_WORD:
$this->set_diffsByWord($value);
break;
case HexaplaSettings::SAVED_TLS:
$this->set_tlList($value);
break;
default:
// FIXME: error
}
}
}
class HexaplaThemes {
const PARCHMENT = 'parchment';
const LEATHER_BOUND = 'leather-bound';
const JONAH = 'jonah';
const LITURGICAL = 'liturgical';
const ALL = [self::PARCHMENT, self::LEATHER_BOUND, self::JONAH, self::LITURGICAL];
}
class HexaplaShades {
const LIGHT = 'light';
const DARK = 'dark';
const ALL = [self::LIGHT, self::DARK];
}