Completed
Push — master ( 47b0f8...cbbce4 )
by Khoa
09:57
created

bbook_chapter::import()   B

Complexity

Conditions 3
Paths 3

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 25
Code Lines 9

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
/**
3
* This file is part of the VinaBB.vn package.
4
*
5
* @copyright (c) VinaBB <vinabb.vn>
6
* @license GNU General Public License, version 2 (GPL-2.0)
7
*/
8
9
namespace vinabb\web\entities;
10
11
use vinabb\web\entities\sub\chapter_text;
12
use vinabb\web\includes\constants;
13
14
/**
15
* Entity for a single chapter
16
*/
17
class bbook_chapter extends chapter_text implements bbook_chapter_interface
18
{
19
	/** @var \phpbb\db\driver\driver_interface $db */
20
	protected $db;
21
22
	/** @var \vinabb\web\entities\helper\helper_interface $entity_helper */
23
	protected $entity_helper;
24
25
	/** @var string $table_name */
26
	protected $table_name;
27
28
	/** @var array $data */
29
	protected $data;
30
31
	/**
32
	* Constructor
33
	*
34
	* @param \phpbb\db\driver\driver_interface				$db				Database object
35
	* @param \vinabb\web\entities\helper\helper_interface	$entity_helper	Entity helper
36
	* @param string											$table_name		Table name
37
	*/
38
	public function __construct(\phpbb\db\driver\driver_interface $db, \vinabb\web\entities\helper\helper_interface $entity_helper, $table_name)
39
	{
40
		$this->db = $db;
41
		$this->entity_helper = $entity_helper;
42
		$this->table_name = $table_name;
43
	}
44
45
	/**
46
	* Data for this entity
47
	*
48
	* @return array
49
	*/
50
	protected function prepare_data()
51
	{
52
		return [
53
			'chapter_id'		=> 'integer',
54
			'book_id'			=> 'integer',
55
			'parent_id'			=> 'integer',
56
			'left_id'			=> 'integer',
57
			'right_id'			=> 'integer',
58
			'chapter_parents'	=> 'string',
59
			'chapter_name'		=> 'string',
60
			'chapter_name_seo'	=> 'string',
61
			'chapter_views'		=> 'integer',
62
			'chapter_time'		=> 'integer',
63
64
			// Entity: vinabb\web\entities\sub\chapter_text
65
			'chapter_text'			=> 'string',
66
			'chapter_text_uid'		=> 'string',
67
			'chapter_text_bitfield'	=> 'string',
68
			'chapter_text_options'	=> 'integer'
69
		];
70
	}
71
72
	/**
73
	* Load the data from the database for an entity
74
	*
75
	* @param int						$id		Chapter ID
76
	* @return bbook_chapter_interface	$this	Object for chaining calls: load()->set()->save()
77
	* @throws \vinabb\web\exceptions\out_of_bounds
78
	*/
79
	public function load($id)
80
	{
81
		$sql = 'SELECT *
82
			FROM ' . $this->table_name . '
83
			WHERE chapter_id = ' . (int) $id;
84
		$result = $this->db->sql_query($sql);
85
		$this->data = $this->db->sql_fetchrow($result);
86
		$this->db->sql_freeresult($result);
87
88
		// The entity does not exist
89
		if ($this->data === false)
90
		{
91
			throw new \vinabb\web\exceptions\out_of_bounds('chapter_id');
92
		}
93
94
		return $this;
95
	}
96
97
	/**
98
	* Import data for an entity
99
	*
100
	* Used when the data is already loaded externally.
101
	* Any existing data on this entity is over-written.
102
	* All data is validated and an exception is thrown if any data is invalid.
103
	*
104
	* @param array						$data	Data array from the database
105
	* @return bbook_chapter_interface	$this	Object for chaining calls: load()->set()->save()
106
	* @throws \vinabb\web\exceptions\invalid_argument
107
	*/
108
	public function import($data)
109
	{
110
		// Clear out any saved data
111
		$this->data = [];
112
113
		// Go through the basic fields and set them to our data array
114
		foreach ($this->prepare_data() as $field => $type)
115
		{
116
			// The data wasn't sent to us
117
			if (!isset($data[$field]))
118
			{
119
				throw new \vinabb\web\exceptions\invalid_argument([$field, 'EMPTY']);
120
			}
121
122
			// settype() passes values by reference
123
			$value = $data[$field];
124
125
			// We're using settype() to enforce data types
126
			settype($value, $type);
127
128
			$this->data[$field] = $value;
129
		}
130
131
		return $this;
132
	}
133
134
	/**
135
	* Insert the entity for the first time
136
	*
137
	* Will throw an exception if the entity was already inserted (call save() instead)
138
	*
139
	* @return bbook_chapter_interface $this Object for chaining calls: load()->set()->save()
140
	* @throws \vinabb\web\exceptions\out_of_bounds
141
	*/
142
	public function insert()
143
	{
144
		// The entity already exists
145
		if (!empty($this->data['chapter_id']))
146
		{
147
			throw new \vinabb\web\exceptions\out_of_bounds('chapter_id');
148
		}
149
150
		// Make extra sure there is no ID set
151
		unset($this->data['chapter_id']);
152
153
		$sql = 'INSERT INTO ' . $this->table_name . ' ' . $this->db->sql_build_array('INSERT', $this->data);
154
		$this->db->sql_query($sql);
155
156
		// Set the ID using the ID created by the SQL INSERT
157
		$this->data['chapter_id'] = (int) $this->db->sql_nextid();
158
159
		return $this;
160
	}
161
162
	/**
163
	* Save the current settings to the database
164
	*
165
	* This must be called before closing or any changes will not be saved!
166
	* If adding an entity (saving for the first time), you must call insert() or an exception will be thrown
167
	*
168
	* @return bbook_chapter_interface $this Object for chaining calls: load()->set()->save()
169
	* @throws \vinabb\web\exceptions\out_of_bounds
170
	*/
171
	public function save()
172
	{
173
		// The entity does not exist
174
		if (empty($this->data['chapter_id']))
175
		{
176
			throw new \vinabb\web\exceptions\out_of_bounds('chapter_id');
177
		}
178
179
		// Copy the data array, filtering out the ID
180
		// so we do not attempt to update the row's identity column.
181
		$sql_array = array_diff_key($this->data, ['chapter_id' => null]);
182
183
		$sql = 'UPDATE ' . $this->table_name . '
184
			SET ' . $this->db->sql_build_array('UPDATE', $sql_array) . '
185
			WHERE chapter_id = ' . $this->get_id();
186
		$this->db->sql_query($sql);
187
188
		return $this;
189
	}
190
191
	/**
192
	* Get the book ID
193
	*
194
	* @return int
195
	*/
196
	public function get_id()
197
	{
198
		return isset($this->data['chapter_id']) ? (int) $this->data['chapter_id'] : 0;
199
	}
200
201
	/**
202
	* Get the book ID
203
	*
204
	* @return int
205
	*/
206
	public function get_book_id()
207
	{
208
		return isset($this->data['book_id']) ? (int) $this->data['book_id'] : 0;
209
	}
210
211
	/**
212
	* Set the book ID
213
	*
214
	* @param int						$id		Book ID
215
	* @return bbook_chapter_interface	$this	Object for chaining calls: load()->set()->save()
216
	* @throws \vinabb\web\exceptions\unexpected_value
217
	*/
218
	public function set_book_id($id)
219
	{
220
		$id = (int) $id;
221
222
		// This is a required field
223
		if (!$id)
224
		{
225
			throw new \vinabb\web\exceptions\unexpected_value(['book_id', 'EMPTY']);
226
		}
227
		else if (!$this->entity_helper->check_bbook_book_id($id))
228
		{
229
			throw new \vinabb\web\exceptions\unexpected_value(['book_id', 'NOT_EXISTS']);
230
		}
231
232
		// Set the value on our data array
233
		$this->data['book_id'] = $id;
234
235
		return $this;
236
	}
237
238
	/**
239
	* Get the parent chapter ID
240
	*
241
	* @return int
242
	*/
243
	public function get_parent_id()
244
	{
245
		return isset($this->data['parent_id']) ? (int) $this->data['parent_id'] : 0;
246
	}
247
248
	/**
249
	* Set the parent chapter ID
250
	*
251
	* @param int						$id		Parent ID
252
	* @return bbook_chapter_interface	$this	Object for chaining calls: load()->set()->save()
253
	* @throws \vinabb\web\exceptions\unexpected_value
254
	*/
255
	public function set_parent_id($id)
256
	{
257
		$id = (int) $id;
258
259
		// Check existing category
260
		if ($id && !$this->entity_helper->check_bbook_chapter_id($id))
261
		{
262
			throw new \vinabb\web\exceptions\unexpected_value(['parent_id', 'NOT_EXISTS']);
263
		}
264
265
		// Set the value on our data array
266
		$this->data['parent_id'] = $id;
267
268
		return $this;
269
	}
270
271
	/**
272
	* Get the left_id for the tree
273
	*
274
	* @return int
275
	*/
276
	public function get_left_id()
277
	{
278
		return isset($this->data['left_id']) ? (int) $this->data['left_id'] : 0;
279
	}
280
281
	/**
282
	* Get the right_id for the tree
283
	*
284
	* @return int
285
	*/
286
	public function get_right_id()
287
	{
288
		return isset($this->data['right_id']) ? (int) $this->data['right_id'] : 0;
289
	}
290
291
	/**
292
	* Get the chapter title
293
	*
294
	* @return string
295
	*/
296
	public function get_name()
297
	{
298
		return isset($this->data['chapter_name']) ? (string) $this->data['chapter_name'] : '';
299
	}
300
301
	/**
302
	* Set the chapter title
303
	*
304
	* @param string						$text	Chapter title
305
	* @return bbook_chapter_interface	$this	Object for chaining calls: load()->set()->save()
306
	* @throws \vinabb\web\exceptions\unexpected_value
307
	*/
308
	public function set_name($text)
309
	{
310
		$text = (string) $text;
311
312
		// This is a required field
313
		if ($text == '')
314
		{
315
			throw new \vinabb\web\exceptions\unexpected_value(['chapter_name', 'EMPTY']);
316
		}
317
318
		// Check the max length
319
		if (utf8_strlen($text) > constants::MAX_CONFIG_NAME)
320
		{
321
			throw new \vinabb\web\exceptions\unexpected_value(['chapter_name', 'TOO_LONG']);
322
		}
323
324
		// Set the value on our data array
325
		$this->data['chapter_name'] = $text;
326
327
		return $this;
328
	}
329
330
	/**
331
	* Get the chapter SEO title
332
	*
333
	* @return string
334
	*/
335
	public function get_name_seo()
336
	{
337
		return isset($this->data['chapter_name_seo']) ? (string) $this->data['chapter_name_seo'] : '';
338
	}
339
340
	/**
341
	* Set the chapter SEO title
342
	*
343
	* @param string						$text	Chapter SEO title
344
	* @return bbook_chapter_interface	$this	Object for chaining calls: load()->set()->save()
345
	* @throws \vinabb\web\exceptions\unexpected_value
346
	*/
347
	public function set_name_seo($text)
348
	{
349
		$text = (string) $text;
350
351
		// Check invalid characters
352
		if (!preg_match(constants::REGEX_SEO, $text))
353
		{
354
			throw new \vinabb\web\exceptions\unexpected_value(['chapter_name_seo', 'INVALID']);
355
		}
356
357
		// Set the value on our data array
358
		$this->data['chapter_name_seo'] = $text;
359
360
		return $this;
361
	}
362
363
	/**
364
	* Get the chapter language
365
	*
366
	* @return string
367
	*/
368
	public function get_lang()
369
	{
370
		return isset($this->data['chapter_lang']) ? (string) $this->data['chapter_lang'] : '';
371
	}
372
373
	/**
374
	* Set the chapter language
375
	*
376
	* @param string						$text	2-letter language ISO code
377
	* @return bbook_chapter_interface	$this	Object for chaining calls: load()->set()->save()
378
	* @throws \vinabb\web\exceptions\unexpected_value
379
	*/
380
	public function set_lang($text)
381
	{
382
		$text = (string) $text;
383
384
		// This is a required field
385
		if ($text == '')
386
		{
387
			throw new \vinabb\web\exceptions\unexpected_value(['lang', 'EMPTY']);
388
		}
389
		else if (!$this->entity_helper->check_lang_iso($text))
390
		{
391
			throw new \vinabb\web\exceptions\unexpected_value(['lang', 'NOT_EXISTS']);
392
		}
393
394
		// Set the value on our data array
395
		$this->data['chapter_lang'] = $text;
396
397
		return $this;
398
	}
399
400
	/**
401
	* Get the chapter views
402
	*
403
	* @return int
404
	*/
405
	public function get_views()
406
	{
407
		return isset($this->data['chapter_views']) ? (int) $this->data['chapter_views'] : 0;
408
	}
409
410
	/**
411
	* Get the editing time
412
	*
413
	* @return int
414
	*/
415
	public function get_time()
416
	{
417
		return isset($this->data['chapter_time']) ? (int) $this->data['chapter_time'] : 0;
418
	}
419
420
	/**
421
	* Set the editing time
422
	*
423
	* @return bbook_chapter_interface $this Object for chaining calls: load()->set()->save()
424
	*/
425
	public function set_time()
426
	{
427
		// Set the value on our data array
428
		$this->data['chapter_time'] = time();
429
430
		return $this;
431
	}
432
}
433