Completed
Push — master ( a821fd...7659ff )
by Khoa
07:38 queued 30s
created

bbook_chapter::get_parents()   A

Complexity

Conditions 2
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 14
Code Lines 6

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
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\operators;
10
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
/**
14
* Operator for a set of chapters
15
*/
16
class bbook_chapter implements bbook_chapter_interface
17
{
18
	/** @var ContainerInterface $container */
19
	protected $container;
20
21
	/** @var \phpbb\db\driver\driver_interface $db */
22
	protected $db;
23
24
	/** @var \vinabb\web\operators\nestedset_bbook_chapters $nestedset */
25
	protected $nestedset;
26
27
	/** @var string $table_name */
28
	protected $table_name;
29
30
	/**
31
	* Constructor
32
	*
33
	* @param ContainerInterface								$container	Container object
34
	* @param \vinabb\web\operators\nestedset_bbook_chapters	$nestedset	Nestedset object for tree functionality
35
	* @param \phpbb\db\driver\driver_interface				$db			Database object
36
	* @param string											$table_name	Table name
37
	*/
38
	public function __construct(
39
		ContainerInterface $container,
40
		\phpbb\db\driver\driver_interface $db,
41
		\vinabb\web\operators\nestedset_bbook_chapters $nestedset,
42
		$table_name
43
	)
44
	{
45
		$this->container = $container;
46
		$this->db = $db;
47
		$this->nestedset = $nestedset;
48
		$this->table_name = $table_name;
49
	}
50
51
	/**
52
	* Get number of chapters
53
	*
54
	* @param int $book_id Book ID
55
	* @return int
56
	*/
57
	public function count_chapters($book_id = 0)
58
	{
59
		$sql_where = $book_id ? 'WHERE book_id = ' . (int) $book_id : 'WHERE book_id > 0';
60
61
		$sql = 'SELECT COUNT(chapter_id) AS counter
62
			FROM ' . $this->table_name . "
63
			$sql_where";
64
		$result = $this->db->sql_query($sql);
65
		$counter = (int) $this->db->sql_fetchfield('counter');
66
		$this->db->sql_freeresult($result);
67
68
		return $counter;
69
	}
70
71
	/**
72
	* Get all chapters
73
	*
74
	* @param int $parent_id Parent ID
75
	* @return array
76
	*/
77
	public function get_chapters($parent_id = 0)
78
	{
79
		$entities = [];
80
81
		// Load all entity data from the database into an array
82
		$rowset = $this->nestedset->get_chapter_data($parent_id);
83
84
		foreach ($rowset as $row)
85
		{
86
			$entities[] = $this->container->get('vinabb.web.entities.bbook_chapter')->import($row);
87
		}
88
89
		return $entities;
90
	}
91
92
	/**
93
	* Add a chapter
94
	*
95
	* @param \vinabb\web\entities\bbook_chapter_interface	$entity		Chapter entity
96
	* @param int											$parent_id	Parent ID
97
	* @return \vinabb\web\entities\bbook_chapter_interface
98
	*/
99
	public function add_chapter(\vinabb\web\entities\bbook_chapter_interface $entity, $parent_id = 0)
100
	{
101
		// Insert the entity to the database
102
		$entity->insert();
103
104
		// Get the newly inserted entity ID
105
		$id = $entity->get_id();
106
107
		// Update the tree for the entity in the database
108
		$this->nestedset->add_to_nestedset($id);
109
110
		// If a parent ID was supplied, update the parent ID and tree IDs
111
		if ($parent_id)
112
		{
113
			$this->nestedset->change_parent($id, $parent_id);
114
		}
115
116
		// Reload the data to return a fresh entity
117
		return $entity->load($id);
118
	}
119
120
	/**
121
	* Move a chapter up/down
122
	*
123
	* @param int	$id			Chapter ID
124
	* @param string	$direction	The direction: up|down
125
	* @param int	$amount		The number of places to move
126
	* @throws \vinabb\web\exceptions\out_of_bounds
127
	*/
128
	public function move_chapter($id, $direction = 'up', $amount = 1)
129
	{
130
		$id = (int) $id;
131
		$amount = (int) $amount;
132
133
		try
134
		{
135
			$this->nestedset->move($id, (($direction !== 'up') ? -$amount : $amount));
136
		}
137
		catch (\OutOfBoundsException $e)
138
		{
139
			throw new \vinabb\web\exceptions\out_of_bounds('chapter_id');
140
		}
141
	}
142
143
	/**
144
	* Change the parent
145
	*
146
	* @param int	$id				Chapter ID
147
	* @param int	$new_parent_id	New parent ID
148
	* @throws \vinabb\web\exceptions\out_of_bounds
149
	*/
150
	public function change_parent($id, $new_parent_id)
151
	{
152
		$id = (int) $id;
153
		$new_parent_id = (int) $new_parent_id;
154
155
		try
156
		{
157
			$this->nestedset->change_parent($id, $new_parent_id);
158
		}
159
		catch (\OutOfBoundsException $e)
160
		{
161
			$field = (strpos($e->getMessage(), 'INVALID_ITEM') !== false) ? 'chapter_id' : 'new_parent_id';
162
			throw new \vinabb\web\exceptions\out_of_bounds($field);
163
		}
164
	}
165
166
	/**
167
	* Get a chapter's parent chapters (for use in breadcrumbs)
168
	*
169
	* @param int $parent_id Parent ID
170
	* @return array
171
	*/
172
	public function get_parents($parent_id)
173
	{
174
		$entities = [];
175
176
		// Load all parent data from the database into an array
177
		$rows = $this->nestedset->get_path_data($parent_id);
178
179
		foreach ($rows as $row)
180
		{
181
			$entities[] = $this->container->get('vinabb.web.entities.bbook_chapter')->import($row);
182
		}
183
184
		return $entities;
185
	}
186
187
	/**
188
	* Update the view counter
189
	*
190
	* @param int $id Chapter ID
191
	*/
192
	public function increase_views($id)
193
	{
194
		$sql = 'UPDATE ' . $this->table_name . '
195
			SET chapter_views = chapter_views + 1
196
			WHERE chapter_id = ' . (int) $id;
197
		$this->db->sql_query($sql);
198
	}
199
200
	/**
201
	* Delete a chapter
202
	*
203
	* @param int $id Chapter ID
204
	* @throws \vinabb\web\exceptions\out_of_bounds
205
	*/
206
	public function delete_chapter($id)
207
	{
208
		$id = (int) $id;
209
210
		try
211
		{
212
			$this->nestedset->delete($id);
213
		}
214
		catch (\OutOfBoundsException $e)
215
		{
216
			throw new \vinabb\web\exceptions\out_of_bounds('chapter_id');
217
		}
218
	}
219
}
220