Completed
Push — master ( 7659ff...13b4bf )
by Khoa
10:41
created

bbook_chapters::save_data()   B

Complexity

Conditions 4
Paths 4

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 36
Code Lines 15

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
rs 8.5806
cc 4
eloc 15
nc 4
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\controllers\acp;
10
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
/**
14
* Controller for the bbook_chapters_module
15
*/
16
class bbook_chapters implements bbook_chapters_interface
17
{
18
	/** @var \vinabb\web\controllers\cache\service_interface $cache */
19
	protected $cache;
20
21
	/** @var ContainerInterface $container */
22
	protected $container;
23
24
	/** @var \phpbb\language\language $language */
25
	protected $language;
26
27
	/** @var \phpbb\log\log $log */
28
	protected $log;
29
30
	/** @var \vinabb\web\operators\bbook_chapter_interface $operator */
31
	protected $operator;
32
33
	/** @var \phpbb\request\request $request */
34
	protected $request;
35
36
	/** @var \phpbb\template\template $template */
37
	protected $template;
38
39
	/** @var \phpbb\user $user */
40
	protected $user;
41
42
	/** @var \vinabb\web\controllers\helper\helper_interface $ext_helper */
43
	protected $ext_helper;
44
45
	/** @var string $root_path */
46
	protected $root_path;
47
48
	/** @var string $php_ext */
49
	protected $php_ext;
50
51
	/** @var string $u_action */
52
	protected $u_action;
53
54
	/** @var array $data */
55
	protected $data;
56
57
	/** @var array $errors */
58
	protected $errors;
59
60
	/**
61
	* Constructor
62
	*
63
	* @param \vinabb\web\controllers\cache\service_interface	$cache			Cache service
64
	* @param ContainerInterface									$container		Container object
65
	* @param \phpbb\language\language							$language		Language object
66
	* @param \phpbb\log\log										$log			Log object
67
	* @param \vinabb\web\operators\bbook_chapter_interface		$operator		Chapter operators
68
	* @param \phpbb\request\request								$request		Request object
69
	* @param \phpbb\template\template							$template		Template object
70
	* @param \phpbb\user										$user			User object
71
	* @param \vinabb\web\controllers\helper\helper_interface	$ext_helper		Extension helper
72
	* @param string												$root_path		phpBB root path
73
	* @param string												$php_ext		PHP file extension
74
	*/
75
	public function __construct(
76
		\vinabb\web\controllers\cache\service_interface $cache,
77
		ContainerInterface $container,
78
		\phpbb\language\language $language,
79
		\phpbb\log\log $log,
80
		\vinabb\web\operators\bbook_chapter_interface $operator,
81
		\phpbb\request\request $request,
82
		\phpbb\template\template $template,
83
		\phpbb\user $user,
84
		\vinabb\web\controllers\helper\helper_interface $ext_helper,
85
		$root_path,
86
		$php_ext
87
	)
88
	{
89
		$this->cache = $cache;
90
		$this->container = $container;
91
		$this->language = $language;
92
		$this->log = $log;
93
		$this->operator = $operator;
94
		$this->request = $request;
95
		$this->template = $template;
96
		$this->user = $user;
97
		$this->ext_helper = $ext_helper;
98
		$this->root_path = $root_path;
99
		$this->php_ext = $php_ext;
100
	}
101
102
	/**
103
	* Set form action URL
104
	*
105
	* @param string $u_action Form action
106
	*/
107
	public function set_form_action($u_action)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
108
	{
109
		$this->u_action = $u_action;
110
	}
111
112
	/**
113
	* Display chapters
114
	*
115
	* @param int $parent_id Parent ID
116
	*/
117
	public function display_chapters($parent_id = 0)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
118
	{
119
		// Grab all from database
120
		$entities = $this->operator->get_chapters($parent_id);
121
122
		// Initialize a variable to hold the right_id value
123
		$last_right_id = 0;
124
125
		/** @var \vinabb\web\entities\bbook_chapter_interface $entity */
126
		foreach ($entities as $entity)
127
		{
128
			// The current entity is a child of a previous entity, do not display it
129
			if ($entity->get_left_id() < $last_right_id)
130
			{
131
				continue;
132
			}
133
134
			$this->template->assign_block_vars('chapters', [
135
				'URL'		=> "{$this->u_action}&parent_id={$entity->get_id()}",
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $entity instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
136
				'NAME'		=> $entity->get_name(),
137
				'TIME'		=> $this->user->format_date($entity->get_time()),
138
139
				'S_IS_CAT'	=> $entity->get_right_id() - $entity->get_left_id() > 1,
140
141
				'U_EDIT'		=> "{$this->u_action}&action=edit&id={$entity->get_id()}",
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $entity instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
142
				'U_MOVE_DOWN'	=> "{$this->u_action}&action=move_down&id={$entity->get_id()}&hash=" . generate_link_hash('down' . $entity->get_id()),
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $entity instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
143
				'U_MOVE_UP'		=> "{$this->u_action}&action=move_up&id={$entity->get_id()}&hash=" . generate_link_hash('up' . $entity->get_id()),
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $entity instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
144
				'U_DELETE'		=> "{$this->u_action}&action=delete&id={$entity->get_id()}"
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $entity instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
145
			]);
146
147
			// Store the current right_id value
148
			$last_right_id = $entity->get_right_id();
149
		}
150
151
		// Prepare rule breadcrumb path navigation
152
		$entities = $this->operator->get_parents($parent_id);
153
154
		// Process each entity for breadcrumb
155
		/** @var \vinabb\web\entities\bbook_chapter_interface $entity */
156
		foreach ($entities as $entity)
157
		{
158
			$this->template->assign_block_vars('breadcrumb', [
159
				'NAME'	=> $entity->get_name(),
160
				'URL'	=> "{$this->u_action}&parent_id={$entity->get_id()}",
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $entity instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
161
162
				'S_CURRENT'	=> $entity->get_id() == $parent_id
163
			]);
164
		}
165
166
		$this->template->assign_vars([
167
			'U_ACTION'	=> "{$this->u_action}&action=add&parent_id={$parent_id}",
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $parent_id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
168
			'U_ROOT'	=> "{$this->u_action}&parent_id=0"
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
169
		]);
170
	}
171
172
	/**
173
	* Add a chapter
174
	*
175
	* @param int $parent_id Parent ID
176
	*/
177
	public function add_chapter($parent_id = 0)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
178
	{
179
		// Initiate an entity
180
		/** @var \vinabb\web\entities\bbook_chapter_interface $entity */
181
		$entity = $this->container->get('vinabb.web.entities.bbook_chapter');
182
183
		// Process the new entity
184
		$this->add_edit_data($entity);
185
186
		// Build the parent selection
187
		$this->build_parent_options($entity, $parent_id, 'add');
188
189
		$this->template->assign_vars([
190
			'S_ADD'	=> true,
191
192
			'U_ACTION'	=> "{$this->u_action}&action=add&parent_id={$parent_id}",
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $parent_id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
193
			'U_BACK'	=> "{$this->u_action}&parent_id={$parent_id}"
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $parent_id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
194
		]);
195
	}
196
197
	/**
198
	* Edit a chapter
199
	*
200
	* @param int $chapter_id Chapter ID
201
	*/
202
	public function edit_chapter($chapter_id)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
203
	{
204
		// Initiate and load the entity
205
		/** @var \vinabb\web\entities\bbook_chapter_interface $entity */
206
		$entity = $this->container->get('vinabb.web.entities.bbook_chapter')->load($chapter_id);
207
208
		// Process the edited entity
209
		$this->add_edit_data($entity);
210
211
		// Build the parent selection
212
		$this->build_parent_options($entity);
213
214
		$this->template->assign_vars([
215
			'S_EDIT'	=> true,
216
			'S_IS_CAT'	=> ($entity->get_right_id() - $entity->get_left_id()) > 1,
217
218
			'U_ACTION'	=> "{$this->u_action}&action=edit&id={$chapter_id}",
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $chapter_id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
219
			'U_BACK'	=> "{$this->u_action}&parent_id={$entity->get_parent_id()}"
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $entity instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
220
		]);
221
	}
222
223
	/**
224
	* Process data to be added or edited
225
	*
226
	* @param \vinabb\web\entities\bbook_chapter_interface $entity Chapter entity
227
	*/
228
	public function add_edit_data(\vinabb\web\entities\bbook_chapter_interface $entity)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
229
	{
230
		$submit = $this->request->is_set_post('submit');
231
232
		// Create a form key for preventing CSRF attacks
233
		add_form_key('acp_bbook_chapters');
234
235
		// Get form data
236
		$this->request_data();
237
238
		// Set the parse options to the entity
239
		$this->set_bbcode_options($entity, $submit);
240
241
		if ($submit)
242
		{
243
			// Test if the submitted form is valid
244
			if (!check_form_key('acp_bbook_chapters'))
245
			{
246
				$this->errors[] = $this->language->lang('FORM_INVALID');
247
			}
248
249
			// Map and set data to the entity
250
			$this->map_set_data($entity);
251
252
			// Insert or update
253
			if (empty($this->errors))
254
			{
255
				$this->save_data($entity);
256
			}
257
		}
258
259
		// Output
260
		$this->data_to_tpl($entity);
261
262
		$this->template->assign_vars([
263
			'ERRORS'	=> !empty($this->errors) ? implode('<br>', $this->errors) : ''
264
		]);
265
266
		// Custom BBCode
267
		include_once "{$this->root_path}includes/functions_display.{$this->php_ext}";
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
268
		display_custom_bbcodes();
269
	}
270
271
	/**
272
	* Request data from the form
273
	*/
274
	protected function request_data()
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
275
	{
276
		$this->data = [
277
			'parent_id'		=> $this->request->variable('parent_id', 0),
278
			'chapter_name'	=> $this->request->variable('chapter_name', '', true),
279
			'chapter_text'	=> $this->request->variable('chapter_text', '', true),
280
			'chapter_time'	=> null
281
		];
282
	}
283
284
	/**
285
	* Grab the form data's parsing options and set them to the entity
286
	*
287
	* If submit, use data from the form
288
	* In edit mode, use data stored in the entity
289
	* In add mode, use default values
290
	*
291
	* @param \vinabb\web\entities\bbook_chapter_interface $entity Chapter entity
292
	*/
293
	protected function set_bbcode_options(\vinabb\web\entities\bbook_chapter_interface $entity, $submit)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
294
	{
295
		$entity->text_enable_bbcode($submit ? $this->request->is_set_post('text_bbcode') : ($entity->get_id() ? $entity->text_bbcode_enabled() : true));
296
		$entity->text_enable_urls($submit ? $this->request->is_set_post('text_urls') : ($entity->get_id() ? $entity->text_urls_enabled() : true));
297
		$entity->text_enable_smilies($submit ? $this->request->is_set_post('text_smilies') : ($entity->get_id() ? $entity->text_smilies_enabled() : true));
298
	}
299
300
	/**
301
	* Map the form data fields to setters and set them to the entity
302
	*
303
	* @param \vinabb\web\entities\bbook_chapter_interface $entity Chapter entity
304
	*/
305
	protected function map_set_data(\vinabb\web\entities\bbook_chapter_interface $entity)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
306
	{
307
		$map_fields = [
308
			'set_parent_id'	=> $this->data['parent_id'],
309
			'set_name'		=> $this->data['chapter_name'],
310
			'set_name_seo'	=> $this->ext_helper->clean_url($this->data['chapter_name']),
311
			'set_text'		=> $this->data['chapter_text'],
312
			'set_time'		=> null
313
		];
314
315
		// Set the mapped data in the entity
316
		foreach ($map_fields as $entity_function => $chapter_data)
317
		{
318
			try
319
			{
320
				// Calling the $entity_function on the entity and passing it $chapter_data
321
				$entity->$entity_function($chapter_data);
322
			}
323
			catch (\vinabb\web\exceptions\base $e)
324
			{
325
				$this->errors[] = $e->get_friendly_message($this->language);
326
			}
327
		}
328
329
		unset($map_fields);
330
	}
331
332
	/**
333
	* Insert or update data, then log actions and clear cache if needed
334
	*
335
	* @param \vinabb\web\entities\bbook_chapter_interface $entity Chapter entity
336
	*/
337
	protected function save_data(\vinabb\web\entities\bbook_chapter_interface $entity)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
338
	{
339
		if ($entity->get_id())
340
		{
341
			// Save the edited entity to the database
342
			$entity->save();
343
344
			// Change the parent
345
			if ($this->data['parent_id'] != $entity->get_parent_id())
346
			{
347
				try
348
				{
349
					$this->operator->change_parent($entity->get_id(), $this->data['parent_id']);
350
				}
351
				catch (\vinabb\web\exceptions\base $e)
352
				{
353
					trigger_error($this->language->lang('ERROR_CHAPTER_CHANGE_PARENT', $e->get_message($this->language)) . adm_back_link($this->u_action), E_USER_WARNING);
354
				}
355
			}
356
357
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_BBOOK_CHAPTER_EDIT', time(), [$entity->get_name()]);
358
359
			$message = 'MESSAGE_CHAPTER_EDIT';
360
		}
361
		else
362
		{
363
			// Add the new entity to the database
364
			$entity = $this->operator->add_chapter($entity, $this->data['parent_id']);
365
366
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_BBOOK_CHAPTER_ADD', time(), [$entity->get_name()]);
367
368
			$message = 'MESSAGE_CHAPTER_ADD';
369
		}
370
371
		trigger_error($this->language->lang($message) . adm_back_link("{$this->u_action}&parent_id={$this->data['parent_id']}"));
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
372
	}
373
374
	/**
375
	* Output entity data to template variables
376
	*
377
	* @param \vinabb\web\entities\bbook_chapter_interface $entity Chapter entity
378
	*/
379
	protected function data_to_tpl(\vinabb\web\entities\bbook_chapter_interface $entity)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
380
	{
381
		$this->template->assign_vars([
382
			'CHAPTER_NAME'	=> $entity->get_name(),
383
			'CHAPTER_TEXT'	=> $entity->get_text_for_edit()
384
		]);
385
	}
386
387
	/**
388
	* Move a rule up/down
389
	*
390
	* @param int	$chapter_id	Chapter ID
391
	* @param string	$direction	The direction (up|down)
392
	* @param int	$amount		The number of places to move
393
	*/
394
	public function move_chapter($chapter_id, $direction, $amount = 1)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
395
	{
396
		// Check the valid link hash
397
		if (!check_link_hash($this->request->variable('hash', ''), $direction . $chapter_id))
398
		{
399
			trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
400
		}
401
402
		try
403
		{
404
			$this->operator->move_chapter($chapter_id, $direction, $amount);
405
		}
406
		catch (\vinabb\web\exceptions\base $e)
407
		{
408
			trigger_error($this->language->lang('ERROR_CHAPTER_MOVE', $e->get_message($this->language)) . adm_back_link($this->u_action), E_USER_WARNING);
409
		}
410
411
		// If AJAX was used, show user a result message
412
		if ($this->request->is_ajax())
413
		{
414
			$json_response = new \phpbb\json_response;
415
			$json_response->send(['success' => true]);
416
		}
417
418
		// Initiate and load the entity for no AJAX request
419
		/** @var \vinabb\web\entities\bbook_chapter_interface $entity */
420
		$entity = $this->container->get('vinabb.web.entities.bbook_chapter')->load($chapter_id);
421
422
		// Reload the current page
423
		redirect("{$this->u_action}&parent_id={$entity->get_parent_id()}");
0 ignored issues
show
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by Khoa Nguyen
As per coding-style, please use concatenation or sprintf for the variable $entity instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
424
	}
425
426
	/**
427
	* Delete a chapter
428
	*
429
	* @param int $chapter_id Chapter ID
430
	*/
431
	public function delete_chapter($chapter_id)
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
432
	{
433
		/** @var \vinabb\web\entities\bbook_chapter_interface $entity */
434
		$entity = $this->container->get('vinabb.web.entities.bbook_chapter')->load($chapter_id);
435
436
		try
437
		{
438
			$this->operator->delete_chapter($chapter_id);
439
		}
440
		catch (\vinabb\web\exceptions\base $e)
441
		{
442
			trigger_error($this->language->lang('ERROR_CHAPTER_DELETE', $e->get_message($this->language)) . adm_back_link($this->u_action), E_USER_WARNING);
443
		}
444
445
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_BBOOK_CHAPTER_DELETE', time(), [$entity->get_name()]);
446
447
		// If AJAX was used, show user a result message
448
		if ($this->request->is_ajax())
449
		{
450
			$json_response = new \phpbb\json_response;
451
			$json_response->send([
452
				'MESSAGE_TITLE'	=> $this->language->lang('INFORMATION'),
453
				'MESSAGE_TEXT'	=> $this->language->lang('MESSAGE_CHAPTER_DELETE'),
454
				'REFRESH_DATA'	=> ['time'	=> 3]
455
			]);
456
		}
457
	}
458
459
	/**
460
	* Generate options of available parents
461
	*
462
	* @param \vinabb\web\entities\bbook_chapter_interface	$entity		Chapter entity
463
	* @param int											$parent_id	Parent ID
464
	* @param string											$mode		Add or edit mode?
465
	*/
466
	protected function build_parent_options(\vinabb\web\entities\bbook_chapter_interface $entity, $parent_id = 0, $mode = 'edit')
0 ignored issues
show
Coding Style introduced by Khoa Nguyen
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
467
	{
468
		$options = $this->operator->get_chapters();
469
		$parent_id = ($mode == 'edit') ? $entity->get_parent_id() : $parent_id;
470
471
		$padding = '';
472
		$padding_store = [];
473
		$right = 0;
474
475
		/** @var \vinabb\web\entities\bbook_chapter_interface $option */
476
		foreach ($options as $option)
477
		{
478
			// Update padding for the current tree level
479
			$this->ext_helper->build_parent_padding($padding, $padding_store, $right, $option->get_parent_id(), $option->get_left_id(), $option->get_right_id());
480
481
			$this->template->assign_block_vars('parent_options', [
482
				'ID'		=> $option->get_id(),
483
				'NAME'		=> $padding . $option->get_name(),
484
485
				'S_DISABLED'	=> $mode == 'edit' && (($option->get_left_id() > $entity->get_left_id()) && ($option->get_right_id() < $entity->get_right_id()) || ($option->get_id() == $entity->get_id())),
486
				'S_SELECTED'	=> $option->get_id() == $parent_id
487
			]);
488
		}
489
	}
490
}
491