@@ -35,106 +35,111 @@ public function getConn()
35
35
return $ this ->conn ;
36
36
}
37
37
38
- //
39
- // requests table methods
40
- //
41
- public function addRequest ($ requestor , $ dest = self ::REQUEST_BECOME_PI )
38
+ private function search ($ table , $ filters )
42
39
{
43
- if ($ this ->requestExists ($ requestor , $ dest )) {
44
- return ;
45
- }
46
-
47
40
$ stmt = $ this ->conn ->prepare (
48
- "INSERT INTO " . self ::TABLE_REQS . " (uid, request_for) VALUES (:uid, :request_for) "
41
+ "SELECT * FROM $ table WHERE " .
42
+ implode (" and " , array_map (fn ($ x ) => "$ x=: $ x " , array_keys ($ filters )))
49
43
);
50
- $ stmt -> bindParam ( " :uid " , $ requestor );
51
- $ stmt ->bindParam (":request_for " , $ dest );
52
-
44
+ foreach ( $ filters as $ key => $ val ) {
45
+ $ stmt ->bindParam (": $ key " , $ val );
46
+ }
53
47
$ stmt ->execute ();
48
+ return $ stmt ->fetchAll ();
54
49
}
55
50
56
- public function removeRequest ( $ requestor , $ dest = self :: REQUEST_BECOME_PI )
51
+ private function delete ( $ table , $ filters )
57
52
{
58
- if (!$ this ->requestExists ($ requestor , $ dest )) {
59
- return ;
60
- }
61
-
62
53
$ stmt = $ this ->conn ->prepare (
63
- "DELETE FROM " . self ::TABLE_REQS . " WHERE uid=:uid and request_for=:request_for "
54
+ "DELETE FROM $ table WHERE " .
55
+ implode (" and " , array_map (fn ($ x ) => "$ x=: $ x " , array_keys ($ filters )))
64
56
);
65
- $ stmt -> bindParam ( " :uid " , $ requestor );
66
- $ stmt ->bindParam (":request_for " , $ dest );
67
-
57
+ foreach ( $ filters as $ key => $ val ) {
58
+ $ stmt ->bindParam (": $ key " , $ val );
59
+ }
68
60
$ stmt ->execute ();
69
61
}
70
62
71
- public function removeRequests ( $ dest = self :: REQUEST_BECOME_PI )
63
+ private function insert ( $ table , $ data )
72
64
{
73
65
$ stmt = $ this ->conn ->prepare (
74
- "DELETE FROM " . self ::TABLE_REQS . " WHERE request_for=:request_for "
66
+ "INSERT INTO $ table " .
67
+ "( " . implode (", " , array_keys ($ data )) . ") " .
68
+ "VALUES " .
69
+ "( " . implode (", " , array_map (fn ($ x ) => ": $ x " , array_keys ($ data ))) . ") "
75
70
);
76
- $ stmt ->bindParam (":request_for " , $ dest );
77
-
71
+ foreach ($ data as $ key => $ val ) {
72
+ $ stmt ->bindParam (": $ key " , $ val );
73
+ }
78
74
$ stmt ->execute ();
79
75
}
80
76
81
- public function requestExists ( $ requestor , $ dest = self :: REQUEST_BECOME_PI )
77
+ private function update ( $ table , $ filters , $ data )
82
78
{
79
+ // "UPDATE " . self::TABLE_NOTICES . " SET date=:date, title=:title, message=:message WHERE id=:id"
83
80
$ stmt = $ this ->conn ->prepare (
84
- "SELECT * FROM " . self ::TABLE_REQS . " WHERE uid=:uid and request_for=:request_for "
81
+ "UPDATE $ table SET " .
82
+ implode (", " , array_map (fn ($ x ) => "$ x=: $ x " , array_keys ($ filters ))) .
83
+ "WHERE " .
84
+ implode (" and " , array_map (fn ($ x ) => "$ x=: $ x " , array_keys ($ filters )))
85
85
);
86
- $ stmt ->bindParam (":uid " , $ requestor );
87
- $ stmt ->bindParam (":request_for " , $ dest );
88
-
86
+ foreach ($ filters as $ key => $ val ) {
87
+ $ stmt ->bindParam (": $ key " , $ val );
88
+ }
89
+ foreach ($ data as $ key => $ val ) {
90
+ $ stmt ->bindParam (": $ key " , $ val );
91
+ }
89
92
$ stmt ->execute ();
90
-
91
- return count ($ stmt ->fetchAll ()) > 0 ;
92
93
}
93
94
94
- public function getRequests ($ dest = self ::REQUEST_BECOME_PI )
95
+ //
96
+ // requests table methods
97
+ //
98
+ public function addRequest ($ requestor , $ dest = self ::REQUEST_BECOME_PI )
95
99
{
96
- $ stmt = $ this ->conn ->prepare (
97
- "SELECT * FROM " . self ::TABLE_REQS . " WHERE request_for=:request_for "
98
- );
99
- $ stmt ->bindParam (":request_for " , $ dest );
100
+ if ($ this ->requestExists ($ requestor , $ dest )) {
101
+ return ;
102
+ }
103
+ $ this ->insert (self ::TABLE_REQS , ["uid " => $ requestor , "request_for " => $ dest ]);
104
+ }
100
105
101
- $ stmt ->execute ();
106
+ public function removeRequest ($ requestor , $ dest = self ::REQUEST_BECOME_PI )
107
+ {
108
+ if (!$ this ->requestExists ($ requestor , $ dest )) {
109
+ return ;
110
+ }
111
+ $ this ->delete (self ::TABLE_REQS , ["uid " => $ uid , "request_for " => $ dest ]);
112
+ }
102
113
103
- return $ stmt ->fetchAll ();
114
+ public function removeRequests ($ dest = self ::REQUEST_BECOME_PI )
115
+ {
116
+ $ this ->delete (self ::TABLE_REQS , ["request_for " => $ dest ]);
104
117
}
105
118
106
- public function getRequestsByUser ( $ user )
119
+ public function requestExists ( $ requestor , $ dest = self :: REQUEST_BECOME_PI )
107
120
{
108
- $ stmt = $ this ->conn ->prepare (
109
- "SELECT * FROM " . self ::TABLE_REQS . " WHERE uid=:uid "
110
- );
111
- $ stmt ->bindParam (":uid " , $ user );
121
+ $ results = $ this ->search (self ::TABLE_REQS , ["request_for " => $ dest ]);
122
+ return count ($ results ) > 0 ;
123
+ }
112
124
113
- $ stmt ->execute ();
125
+ public function getRequests ($ dest = self ::REQUEST_BECOME_PI )
126
+ {
127
+ return $ this ->search (self ::TABLE_REQS , ["request_for " => $ dest ]);
128
+ }
114
129
115
- return $ stmt ->fetchAll ();
130
+ public function getRequestsByUser ($ uid )
131
+ {
132
+ return $ this ->search (self ::TABLE_REQS , ["uid " => $ uid ]);
116
133
}
117
134
118
135
public function deleteRequestsByUser ($ user )
119
136
{
120
- $ stmt = $ this ->conn ->prepare (
121
- "DELETE FROM " . self ::TABLE_REQS . " WHERE uid=:uid "
122
- );
123
- $ stmt ->bindParam (":uid " , $ user );
124
-
125
- $ stmt ->execute ();
137
+ $ this ->delete (self ::TABLE_REQS , ["uid " => $ uid ]);
126
138
}
127
139
128
140
public function addNotice ($ title , $ date , $ content , $ operator )
129
141
{
130
- $ stmt = $ this ->conn ->prepare (
131
- "INSERT INTO " . self ::TABLE_NOTICES . " (date, title, message) VALUES (:date, :title, :message) "
132
- );
133
- $ stmt ->bindParam (":date " , $ date );
134
- $ stmt ->bindParam (":title " , $ title );
135
- $ stmt ->bindParam (":message " , $ content );
136
-
137
- $ stmt ->execute ();
142
+ $ this ->insert (self ::TABLE_NOTICES , ["date " => $ date , "title " => $ title , "message " => $ content ]);
138
143
139
144
$ operator = $ operator ->getUID ();
140
145
@@ -161,56 +166,27 @@ public function editNotice($id, $title, $date, $content)
161
166
162
167
public function deleteNotice ($ id )
163
168
{
164
- $ stmt = $ this ->conn ->prepare (
165
- "DELETE FROM " . self ::TABLE_NOTICES . " WHERE id=:id "
166
- );
167
- $ stmt ->bindParam (":id " , $ id );
168
-
169
- $ stmt ->execute ();
169
+ $ this ->delete (self ::TABLE_NOTICES , ["id " => $ id ]);
170
170
}
171
171
172
172
public function getNotice ($ id )
173
173
{
174
- $ stmt = $ this ->conn ->prepare (
175
- "SELECT * FROM " . self ::TABLE_NOTICES . " WHERE id=:id "
176
- );
177
- $ stmt ->bindParam (":id " , $ id );
178
-
179
- $ stmt ->execute ();
180
-
181
- return $ stmt ->fetchAll ()[0 ];
174
+ return $ this ->search (self ::TABLE_NOTICES , ["id " => $ id ]);
182
175
}
183
176
184
177
public function getNotices ()
185
178
{
186
- $ stmt = $ this ->conn ->prepare (
187
- "SELECT * FROM " . self ::TABLE_NOTICES . " ORDER BY date DESC "
188
- );
189
- $ stmt ->execute ();
190
-
191
- return $ stmt ->fetchAll ();
179
+ return $ this ->search (self ::TABLE_NOTICES , []);
192
180
}
193
181
194
182
public function getPages ()
195
183
{
196
- $ stmt = $ this ->conn ->prepare (
197
- "SELECT * FROM " . self ::TABLE_PAGES
198
- );
199
- $ stmt ->execute ();
200
-
201
- return $ stmt ->fetchAll ();
184
+ return $ this ->search (self ::TABLE_PAGES , []);
202
185
}
203
186
204
187
public function getPage ($ id )
205
188
{
206
- $ stmt = $ this ->conn ->prepare (
207
- "SELECT * FROM " . self ::TABLE_PAGES . " WHERE page=:id "
208
- );
209
- $ stmt ->bindParam (":id " , $ id );
210
-
211
- $ stmt ->execute ();
212
-
213
- return $ stmt ->fetchAll ()[0 ];
189
+ return $ this ->search (self ::TABLE_PAGES , ["page " => $ id ]);
214
190
}
215
191
216
192
public function editPage ($ id , $ content , $ operator )
@@ -236,73 +212,44 @@ public function editPage($id, $content, $operator)
236
212
// audit log table methods
237
213
public function addLog ($ operator , $ operator_ip , $ action_type , $ recipient )
238
214
{
239
- $ stmt = $ this ->conn ->prepare (
240
- "INSERT INTO " . self ::TABLE_AUDIT_LOG . " (operator, operator_ip, action_type, recipient)
241
- VALUE (:operator, :operator_ip, :action_type, :recipient) "
215
+ $ this ->insert (
216
+ self ::TABLE_REQS ,
217
+ [
218
+ "operator " => $ operator ,
219
+ "operator_ip " => $ operator_ip ,
220
+ "action_type " => $ action_type ,
221
+ "recipient " => $ recipient
222
+ ]
242
223
);
243
- $ stmt ->bindParam (":operator " , $ operator );
244
- $ stmt ->bindParam (":operator_ip " , $ operator_ip );
245
- $ stmt ->bindParam (":action_type " , $ action_type );
246
- $ stmt ->bindParam (":recipient " , $ recipient );
247
-
248
- $ stmt ->execute ();
249
224
}
250
225
251
226
public function addAccountDeletionRequest ($ uid )
252
227
{
253
- $ stmt = $ this ->conn ->prepare (
254
- "INSERT INTO " . self ::TABLE_ACCOUNT_DELETION_REQUESTS . " (uid) VALUE (:uid) "
255
- );
256
- $ stmt ->bindParam (":uid " , $ uid );
257
-
258
- $ stmt ->execute ();
228
+ $ this ->insert (self ::TABLE_ACCOUNT_DELETION_REQUESTS , ["uid " => $ uid ]);
259
229
}
260
230
261
231
public function accDeletionRequestExists ($ uid )
262
232
{
263
- $ stmt = $ this ->conn ->prepare (
264
- "SELECT * FROM " . self ::TABLE_ACCOUNT_DELETION_REQUESTS . " WHERE uid=:uid "
265
- );
266
- $ stmt ->bindParam (":uid " , $ uid );
267
-
268
- $ stmt ->execute ();
269
-
270
- return count ($ stmt ->fetchAll ()) > 0 ;
233
+ $ results = $ this ->search (self ::TABLE_ACCOUNT_DELETION_REQUESTS , ["uid " => $ uid ]);
234
+ return count ($ results ) > 0 ;
271
235
}
272
236
273
237
public function deleteAccountDeletionRequest ($ uid )
274
238
{
275
239
if (!$ this ->accDeletionRequestExists ($ uid )) {
276
240
return ;
277
241
}
278
- $ stmt = $ this ->conn ->prepare (
279
- "DELETE FROM " . self ::TABLE_ACCOUNT_DELETION_REQUESTS . " WHERE uid=:uid "
280
- );
281
- $ stmt ->bindParam (":uid " , $ uid );
282
- $ stmt ->execute ();
242
+ $ this ->delete (self ::TABLE_ACCOUNT_DELETION_REQUESTS , ["uid " => $ uid ]);
283
243
}
284
244
285
245
public function getSiteVar ($ name )
286
246
{
287
- $ stmt = $ this ->conn ->prepare (
288
- "SELECT * FROM " . self ::TABLE_SITEVARS . " WHERE name=:name "
289
- );
290
- $ stmt ->bindParam (":name " , $ name );
291
-
292
- $ stmt ->execute ();
293
-
294
- return $ stmt ->fetchAll ()[0 ]['value ' ];
247
+ return $ this ->search (self ::TABLE_SITEVARS , ["name " => $ name ]);
295
248
}
296
249
297
250
public function updateSiteVar ($ name , $ value )
298
251
{
299
- $ stmt = $ this ->conn ->prepare (
300
- "UPDATE " . self ::TABLE_SITEVARS . " SET value=:value WHERE name=:name "
301
- );
302
- $ stmt ->bindParam (":name " , $ name );
303
- $ stmt ->bindParam (":value " , $ value );
304
-
305
- $ stmt ->execute ();
252
+ $ this ->update (self ::TABLE_SITEVARS , ["value " => $ value , "name " => $ name ]);
306
253
}
307
254
308
255
public function getRole ($ uid , $ group )
0 commit comments