-
Notifications
You must be signed in to change notification settings - Fork 5
/
functions.php
393 lines (339 loc) · 9.59 KB
/
functions.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
/**
*
* Knowledge Base extension for the phpBB Forum Software package
*
* @copyright (c) 2017, kinerity, https://www.acsyste.com
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
namespace kinerity\knowledgebase\entity;
/**
* Functions for a single entity
*/
class functions implements functions_interface
{
/** @var array */
protected $data;
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var string */
protected $kb_categories_table;
/**
* Constructor
*
* @param \phpbb\db\driver\driver_interface $db
* @param string $kb_categories_table
* @access public
*/
public function __construct(\phpbb\db\driver\driver_interface $db, $kb_categories_table)
{
$this->db = $db;
$this->kb_categories_table = $kb_categories_table;
}
/**
* Load the data from the database for this entity
*
* @param int
* @return $this object for chaining calls; load()->set()->save()
* @access public
* @throws \kinerity\knowledgebase\exception\out_of_bounds
*/
public function load($id)
{
$sql = 'SELECT *
FROM ' . $this->kb_categories_table . '
WHERE category_id = ' . (int) $id;
$result = $this->db->sql_query($sql);
$this->data = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if ($this->data === false)
{
// A entity does not exist
throw new \kinerity\knowledgebase\exception\out_of_bounds('category_id');
}
return $this;
}
/**
* Insert the entity for the first time
*
* Will throw an exception if the entity was already inserted (call save() instead)
*
* @return $this object for chaining calls; load()->set()->save()
* @access public
* @throws \kinerity\knowledgebase\exception\out_of_bounds
*/
public function insert()
{
if (!empty($this->data['category_id']))
{
// The entity already exists
throw new \kinerity\knowledgebase\exception\out_of_bounds('category_id');
}
$sql = 'SELECT MAX(right_id) AS right_id
FROM ' . $this->kb_categories_table;
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
// Sets values required for the nested set system
$this->data['left_id'] = $row['right_id'] + 1;
$this->data['right_id'] = $row['right_id'] + 2;
// Make extra sure there is no category_id set
unset($this->data['category_id']);
// Insert the entity data to the database
$sql = 'INSERT INTO ' . $this->kb_categories_table . ' ' . $this->db->sql_build_array('INSERT', $this->data);
$this->db->sql_query($sql);
// Set the category_id using the id created by the SQL insert
$this->data['category_id'] = (int) $this->db->sql_nextid();
return $this;
}
/**
* Save the current settings to the database
*
* This must be called before closing or any changes will not be saved!
* If adding a entity (saving for the first time), you must call insert() or an exception will be thrown
*
* @return $this object for chaining calls; load()->set()->save()
* @access public
* @throws \kinerity\knowledgebase\exception\out_of_bounds
*/
public function save()
{
if (empty($this->data['category_id']))
{
// The entity does not exist
throw new \kinerity\knowledgebase\exception\out_of_bounds('category_id');
}
// Copy the data array, filtering out the category_id identifier
// so we do not attempt to update the row's identity column.
$sql_array = array_diff_key($this->data, array('category_id' => null));
// Update the page data in the database
$sql = 'UPDATE ' . $this->kb_categories_table . '
SET ' . $this->db->sql_build_array('UPDATE', $sql_array) . '
WHERE category_id = ' . (int) $this->get_id();
$this->db->sql_query($sql);
return $this;
}
/**
* Get id
*
* @return integer
* @access public
*/
public function get_id()
{
return isset($this->data['category_id']) ? (int) $this->data['category_id'] : 0;
}
/**
* Get title
*
* @return string
* @access public
*/
public function get_title()
{
return isset($this->data['category_name']) ? (string) $this->data['category_name'] : '';
}
/**
* Set title
*
* @param string
* @return $this
* @access public
* @throws \kinerity\knowledgebase\exception\unexpected_value
*/
public function set_title($title)
{
// Enforce a string
$title = (string) $title;
// We limit the title length to 200 characters
if (truncate_string($title, 200) != $title)
{
throw new \kinerity\knowledgebase\exception\unexpected_value(array('title', 'TOO_LONG'));
}
// Set the title on our data array
$this->data['category_name'] = $title;
return $this;
}
/**
* Get description for edit
*
* @return string
* @access public
*/
public function get_description_for_edit()
{
// Use defaults if these haven't been set yet
$description = isset($this->data['category_description']) ? $this->data['category_description'] : '';
$uid = isset($this->data['bbcode_uid']) ? $this->data['bbcode_uid'] : '';
$options = isset($this->data['bbcode_options']) ? (int) $this->data['bbcode_options'] : 0;
// Generate for edit
$description_data = generate_text_for_edit($description, $uid, $options);
return $description_data['text'];
}
/**
* Get description for display
*
* @param bool
* @return mixed|string
* @access public
*/
public function get_description_for_display($censor_text = true)
{
// If these haven't been set yet; use defaults
$description = isset($this->data['category_description']) ? $this->data['category_description'] : '';
$uid = isset($this->data['bbcode_uid']) ? $this->data['bbcode_uid'] : '';
$bitfield = isset($this->data['bbcode_bitfield']) ? $this->data['bbcode_bitfield'] : '';
$options = isset($this->data['bbcode_options']) ? (int) $this->data['bbcode_options'] : 0;
// Generate for display
return generate_text_for_display($description, $uid, $bitfield, $options, $censor_text);
}
/**
* Set description
*
* @param string
* @return $this
* @access public
*/
public function set_description($description)
{
// Prepare the text for storage
$uid = $bitfield = $flags = '';
generate_text_for_storage($description, $uid, $bitfield, $flags, $this->description_bbcode_enabled(), $this->description_magic_url_enabled(), $this->description_smilies_enabled());
// Set the description to our data array
$this->data['category_description'] = $description;
$this->data['bbcode_uid'] = $uid;
$this->data['bbcode_bitfield'] = $bitfield;
// Flags are already set
return $this;
}
/**
* Check if bbcode is enabled on the description
*
* @return integer
* @access public
*/
public function description_bbcode_enabled()
{
$this->data['bbcode_options'] = $this->data['bbcode_options'] ?? 0;
return ($this->data['bbcode_options'] & OPTION_FLAG_BBCODE);
}
/**
* Enable bbcode on the description
*
* @return $this
* @access public
*/
public function description_enable_bbcode()
{
$this->set_description_option(OPTION_FLAG_BBCODE);
return $this;
}
/**
* Disable bbcode on the description
*
* @return $this
* @access public
*/
public function description_disable_bbcode()
{
$this->set_description_option(OPTION_FLAG_BBCODE, true);
return $this;
}
/**
* Check if magic_url is enabled on the description
*
* @return integer
* @access public
*/
public function description_magic_url_enabled()
{
$this->data['bbcode_options'] = $this->data['bbcode_options'] ?? 0;
return ($this->data['bbcode_options'] & OPTION_FLAG_LINKS);
}
/**
* Enable magic url on the description
*
* @return $this
* @access public
*/
public function description_enable_magic_url()
{
$this->set_description_option(OPTION_FLAG_LINKS);
return $this;
}
/**
* Disable magic url on the description
*
* @return $this
* @access public
*/
public function description_disable_magic_url()
{
$this->set_description_option(OPTION_FLAG_LINKS, true);
return $this;
}
/**
* Check if smilies are enabled on the description
*
* @return integer
* @access public
*/
public function description_smilies_enabled()
{
$this->data['bbcode_options'] = $this->data['bbcode_options'] ?? 0;
return ($this->data['bbcode_options'] & OPTION_FLAG_SMILIES);
}
/**
* Enable smilies on the description
*
* @return $this
* @access public
*/
public function description_enable_smilies()
{
$this->set_description_option(OPTION_FLAG_SMILIES);
return $this;
}
/**
* Disable smilies on the description
*
* @return $this
* @access public
*/
public function description_disable_smilies()
{
$this->set_description_option(OPTION_FLAG_SMILIES, true);
return $this;
}
/**
* Set option helper
*
* @return void
* @access protected
*/
protected function set_description_option($option_value, $negate = false, $reparse_description = true)
{
// Set bbcode_options to 0 if it does not yet exist
$this->data['bbcode_options'] = $this->data['bbcode_options'] ?? 0;
// If we're setting the option and the option is not already set
if (!$negate && !($this->data['bbcode_options'] & $option_value))
{
// Add the option to the options
$this->data['bbcode_options'] += $option_value;
}
// If we're un-setting the option and the option is already set
if ($negate && $this->data['bbcode_options'] & $option_value)
{
// Subtract the option from the options
$this->data['bbcode_options'] -= $option_value;
}
// Re-parse the description
if ($reparse_description && !empty($this->data['category_description']))
{
$description = $this->data['category_description'];
decode_message($description, $this->data['bbcode_uid']);
$this->set_description($description);
}
}
}