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

bbook_books   B

Coupling/Cohesion

Components 1
Dependencies 6

Complexity

Total Complexity 38

Size/Duplication

Total Lines 459
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 38
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 459
rs 8.3999

14 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 40 1
A set_form_data() 0 5 1
A select_lang() 0 20 4
B display_books() 0 24 2
A add_book() 0 16 1
A edit_book() 0 16 1
B add_edit_data() 0 46 5
A request_data() 0 11 1
B set_bbcode_options() 0 6 7
B map_set_data() 0 25 3
B upload_data() 0 14 5
B save_data() 0 24 2
A data_to_tpl() 0 11 1
B delete_book() 0 35 4
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
use vinabb\web\includes\constants;
13
14
/**
15
* Controller for the bbook_books_module
16
*/
17
class bbook_books implements bbook_books_interface
18
{
19
	/** @var \vinabb\web\controllers\cache\service_interface $cache */
20
	protected $cache;
21
22
	/** @var \vinabb\web\operators\bbook_chapter_interface $chapter_operator */
23
	protected $chapter_operator;
24
25
	/** @var \phpbb\config\config $config */
26
	protected $config;
27
28
	/** @var ContainerInterface $container */
29
	protected $container;
30
31
	/** @var \phpbb\extension\manager $ext_manager */
32
	protected $ext_manager;
33
34
	/** @var \phpbb\filesystem\filesystem_interface $filesystem */
35
	protected $filesystem;
36
37
	/** @var \phpbb\language\language $language */
38
	protected $language;
39
40
	/** @var \phpbb\log\log $log */
41
	protected $log;
42
43
	/** @var \vinabb\web\operators\bbook_book_interface $operator */
44
	protected $operator;
45
46
	/** @var \phpbb\request\request $request */
47
	protected $request;
48
49
	/** @var \phpbb\template\template $template */
50
	protected $template;
51
52
	/** @var \phpbb\files\upload $upload */
53
	protected $upload;
54
55
	/** @var \phpbb\user $user */
56
	protected $user;
57
58
	/** @var \vinabb\web\controllers\helper\helper_interface $ext_helper */
59
	protected $ext_helper;
60
61
	/** @var string $root_path */
62
	protected $root_path;
63
64
	/** @var string $php_ext */
65
	protected $php_ext;
66
67
	/** @var string $u_action */
68
	protected $u_action;
69
70
	/** @var string $book_lang */
71
	protected $book_lang;
72
73
	/** @var array $data */
74
	protected $data;
75
76
	/** @var array $errors */
77
	protected $errors;
78
79
	/** @var string $ext_root_path */
80
	protected $ext_root_path;
81
82
	/** @var string $upload_dir_path */
83
	protected $upload_dir_path;
84
85
	/** @var array $lang_data */
86
	protected $lang_data;
87
88
	/**
89
	* Constructor
90
	*
91
	* @param \vinabb\web\controllers\cache\service_interface	$cache				Cache service
92
	* @param \vinabb\web\operators\bbook_chapter_interface		$chapter_operator	Chapter operators
93
	* @param \phpbb\config\config								$config				Config object
94
	* @param ContainerInterface									$container			Container object
95
	* @param \phpbb\extension\manager							$ext_manager		Extension manager
96
	* @param \phpbb\filesystem\filesystem_interface				$filesystem			Filesystem object
97
	* @param \phpbb\language\language							$language			Language object
98
	* @param \phpbb\log\log										$log				Log object
99
	* @param \vinabb\web\operators\bbook_book_interface			$operator			Book operators
100
	* @param \phpbb\request\request								$request			Request object
101
	* @param \phpbb\template\template							$template			Template object
102
	* @param \phpbb\files\upload								$upload				Upload object
103
	* @param \phpbb\user										$user				User object
104
	* @param \vinabb\web\controllers\helper\helper_interface	$ext_helper			Extension helper
105
	* @param string												$root_path			phpBB root path
106
	* @param string												$php_ext			PHP file extension
107
	*/
108
	public function __construct(
109
		\vinabb\web\controllers\cache\service_interface $cache,
110
		\vinabb\web\operators\bbook_chapter_interface $chapter_operator,
111
		\phpbb\config\config $config,
112
		ContainerInterface $container,
113
		\phpbb\extension\manager $ext_manager,
114
		\phpbb\filesystem\filesystem_interface $filesystem,
115
		\phpbb\language\language $language,
116
		\phpbb\log\log $log,
117
		\vinabb\web\operators\bbook_book_interface $operator,
118
		\phpbb\request\request $request,
119
		\phpbb\template\template $template,
120
		\phpbb\files\upload $upload,
121
		\phpbb\user $user,
122
		\vinabb\web\controllers\helper\helper_interface $ext_helper,
123
		$root_path,
124
		$php_ext
125
	)
126
	{
127
		$this->cache = $cache;
128
		$this->chapter_operator = $chapter_operator;
129
		$this->config = $config;
130
		$this->container = $container;
131
		$this->ext_manager = $ext_manager;
132
		$this->filesystem = $filesystem;
133
		$this->language = $language;
134
		$this->log = $log;
135
		$this->operator = $operator;
136
		$this->request = $request;
137
		$this->template = $template;
138
		$this->upload = $upload;
139
		$this->user = $user;
140
		$this->ext_helper = $ext_helper;
141
		$this->root_path = $root_path;
142
		$this->php_ext = $php_ext;
143
144
		$this->ext_root_path = $this->ext_manager->get_extension_path('vinabb/web', true);
145
		$this->upload_dir_path = $this->ext_root_path . constants::DIR_BBOOK_FILES . 'cover_images/';
146
		$this->lang_data = $this->cache->get_lang_data();
147
	}
148
149
	/**
150
	* Set form data
151
	*
152
	* @param array $data Form data
153
	*/
154
	public function set_form_data($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...
155
	{
156
		$this->book_lang = $data['book_lang'];
157
		$this->u_action = $data['u_action'] . "&lang={$this->book_lang}";
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...
158
	}
159
160
	/**
161
	* Language selection
162
	*/
163
	public function select_lang()
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...
164
	{
165
		if (count($this->lang_data) > 1)
166
		{
167
			foreach ($this->lang_data as $lang_iso => $lang_data)
168
			{
169
				$this->template->assign_block_vars('lang_options', [
170
					'VALUE'	=> $lang_iso,
171
					'NAME'	=> ($lang_data['english_name'] == $lang_data['local_name']) ? $lang_data['english_name'] : $lang_data['english_name'] . ' (' . $lang_data['local_name'] . ')'
172
				]);
173
			}
174
175
			$this->template->assign_var('U_ACTION', $this->u_action);
176
		}
177
		// If there is only one available language, we do not need the selection list
178
		else
179
		{
180
			$this->display_books();
181
		}
182
	}
183
184
	/**
185
	* Display books
186
	*/
187
	public function display_books()
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...
188
	{
189
		// Grab all from database
190
		$entities = $this->operator->get_books($this->book_lang);
191
192
		/** @var \vinabb\web\entities\bbook_book_interface $entity */
193
		foreach ($entities as $entity)
194
		{
195
			$this->template->assign_block_vars('books', [
196
				'NAME'	=> $entity->get_name(),
197
				'IMG'	=> $entity->get_img(),
198
				'DESC'	=> $entity->get_desc(),
199
200
				'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...
201
				'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...
202
			]);
203
		}
204
205
		$this->template->assign_vars([
206
			'LANG_NAME'	=> $this->lang_data[$this->book_lang]['local_name'],
207
208
			'U_ACTION'	=> "{$this->u_action}&action=add"
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...
209
		]);
210
	}
211
212
	/**
213
	* Add a book
214
	*/
215
	public function add_book()
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...
216
	{
217
		// Initiate an entity
218
		/** @var \vinabb\web\entities\bbook_book_interface $entity */
219
		$entity = $this->container->get('vinabb.web.entities.bbook_books');
220
221
		// Process the new entity
222
		$this->add_edit_data($entity);
223
224
		$this->template->assign_vars([
225
			'S_ADD'		=> true,
226
227
			'U_ACTION'	=> "{$this->u_action}&action=add",
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...
228
			'U_BACK'	=> $this->u_action
229
		]);
230
	}
231
232
	/**
233
	* Edit a book
234
	*
235
	* @param int $book_id Book ID
236
	*/
237
	public function edit_book($book_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...
238
	{
239
		// Initiate and load the entity
240
		/** @var \vinabb\web\entities\bbook_book_interface $entity */
241
		$entity = $this->container->get('vinabb.web.entities.bbook_book')->load($book_id);
242
243
		// Process the edited entity
244
		$this->add_edit_data($entity);
245
246
		$this->template->assign_vars([
247
			'S_EDIT'	=> true,
248
249
			'U_ACTION'	=> "{$this->u_action}&action=edit&id={$book_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 $book_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...
250
			'U_BACK'	=> $this->u_action
251
		]);
252
	}
253
254
	/**
255
	* Process data to be added or edited
256
	*
257
	* @param \vinabb\web\entities\bbook_book_interface $entity Book entity
258
	*/
259
	public function add_edit_data(\vinabb\web\entities\bbook_book_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...
260
	{
261
		$submit = $this->request->is_set_post('submit');
262
263
		// Create a form key for preventing CSRF attacks
264
		add_form_key('acp_bbook_books');
265
266
		// Get form data
267
		$this->request_data();
268
269
		// Set the parse options to the entity
270
		$this->set_bbcode_options($entity, $submit);
271
272
		if ($submit)
273
		{
274
			// Test if the submitted form is valid
275
			if (!check_form_key('acp_bbook_books'))
276
			{
277
				$this->errors[] = $this->language->lang('FORM_INVALID');
278
			}
279
280
			// Map and set data to the entity
281
			$this->map_set_data($entity);
282
283
			// Upload files
284
			$this->upload_data($entity);
285
286
			// Insert or update
287
			if (empty($this->errors))
288
			{
289
				$this->save_data($entity);
290
			}
291
		}
292
293
		// Output
294
		$this->data_to_tpl($entity);
295
296
		$this->template->assign_vars([
297
			'LANG_NAME'	=> $this->lang_data[$this->book_lang]['local_name'],
298
			'ERRORS'	=> !empty($this->errors) ? implode('<br>', $this->errors) : ''
299
		]);
300
301
		// Custom BBCode
302
		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...
303
		display_custom_bbcodes();
304
	}
305
306
	/**
307
	* Request data from the form
308
	*/
309
	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...
310
	{
311
		$this->data = [
312
			'book_name'		=> $this->request->variable('book_name', '', true),
313
			'book_img'		=> $this->request->file('book_img'),
314
			'book_desc'		=> $this->request->variable('book_desc', '', true),
315
			'desc_bbcode'	=> $this->request->variable('desc_bbcode', true),
316
			'desc_urls'		=> $this->request->variable('desc_urls', true),
317
			'desc_smilies'	=> $this->request->variable('desc_smilies', true)
318
		];
319
	}
320
321
	/**
322
	* Grab the form data's parsing options and set them to the entity
323
	*
324
	* If submit, use data from the form
325
	* In edit mode, use data stored in the entity
326
	* In add mode, use default values
327
	*
328
	* @param \vinabb\web\entities\bbook_book_interface $entity Book entity
329
	*/
330
	protected function set_bbcode_options(\vinabb\web\entities\bbook_book_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...
331
	{
332
		$entity->desc_enable_bbcode($submit ? $this->request->is_set_post('desc_bbcode') : ($entity->get_id() ? $entity->desc_bbcode_enabled() : true));
333
		$entity->desc_enable_urls($submit ? $this->request->is_set_post('desc_urls') : ($entity->get_id() ? $entity->desc_urls_enabled() : true));
334
		$entity->desc_enable_smilies($submit ? $this->request->is_set_post('desc_smilies') : ($entity->get_id() ? $entity->desc_smilies_enabled() : true));
335
	}
336
337
	/**
338
	* Map the form data fields to setters and set them to the entity
339
	*
340
	* @param \vinabb\web\entities\bbook_book_interface $entity Book entity
341
	*/
342
	protected function map_set_data(\vinabb\web\entities\bbook_book_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...
343
	{
344
		$map_fields = [
345
			'set_name'		=> $this->data['book_name'],
346
			'set_name_seo'	=> $this->ext_helper->clean_url($this->data['book_name']),
347
			'set_lang'		=> $this->book_lang,
348
			'set_desc'		=> $this->data['book_desc']
349
		];
350
351
		// Set the mapped data in the entity
352
		foreach ($map_fields as $entity_function => $book_data)
353
		{
354
			try
355
			{
356
				// Calling the $entity_function on the entity and passing it $book_data
357
				$entity->$entity_function($book_data);
358
			}
359
			catch (\vinabb\web\exceptions\base $e)
360
			{
361
				$this->errors[] = $e->get_friendly_message($this->language);
362
			}
363
		}
364
365
		unset($map_fields);
366
	}
367
368
	/**
369
	* Upload files and return their filenames to the form data
370
	*
371
	* @param \vinabb\web\entities\bbook_book_interface $entity Book entity
372
	*/
373
	protected function upload_data(\vinabb\web\entities\bbook_book_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...
374
	{
375
		// If there are not any input errors, then begin to upload file
376
		if ($this->ext_helper->can_upload($this->upload_dir_path) && $this->data['book_img']['name'] != '' && empty($this->errors))
377
		{
378
			// Delete the old file if uploaded a new one
379
			if ($this->data['book_img']['name'] != $entity->get_img(true, false))
380
			{
381
				$this->filesystem->remove($entity->get_img(true));
382
			}
383
384
			$entity->set_img($this->ext_helper->upload_file('book_img', $this->upload_dir_path, constants::FILE_EXTENSION_IMAGES, $this->book_lang . '_', $this->ext_helper->clean_url($this->data['book_name']), $this->errors));
385
		}
386
	}
387
388
	/**
389
	* Insert or update data, then log actions and clear cache if needed
390
	*
391
	* @param \vinabb\web\entities\bbook_book_interface $entity Book entity
392
	*/
393
	protected function save_data(\vinabb\web\entities\bbook_book_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...
394
	{
395
		if ($entity->get_id())
396
		{
397
			// Save the edited entity to the database
398
			$entity->save();
399
400
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_BBOOK_BOOK_EDIT', time(), [$entity->get_name()]);
401
402
			$message = 'MESSAGE_BOOK_EDIT';
403
		}
404
		else
405
		{
406
			// Add the new entity to the database
407
			$entity = $this->operator->add_book($entity);
408
409
			$this->config->increment('vinabb_web_total_books', 1, false);
410
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_BBOOK_BOOK_ADD', time(), [$entity->get_name()]);
411
412
			$message = 'MESSAGE_BOOK_ADD';
413
		}
414
415
		trigger_error($this->language->lang($message) . adm_back_link($this->u_action));
416
	}
417
418
	/**
419
	* Output entity data to template variables
420
	*
421
	* @param \vinabb\web\entities\bbook_book_interface $entity Book entity
422
	*/
423
	protected function data_to_tpl(\vinabb\web\entities\bbook_book_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...
424
	{
425
		$this->template->assign_vars([
426
			'BOOK_NAME'			=> $entity->get_name(),
427
			'BOOK_IMG'			=> $entity->get_img(),
428
			'BOOK_DESC'			=> $entity->get_desc_for_edit(),
429
			'BOOK_DESC_BBCODE'	=> $entity->desc_bbcode_enabled(),
430
			'BOOK_DESC_URLS'	=> $entity->desc_urls_enabled(),
431
			'BOOK_DESC_SMILIES'	=> $entity->desc_smilies_enabled()
432
		]);
433
	}
434
435
	/**
436
	* Delete a book
437
	*
438
	* @param int $book_id Book ID
439
	*/
440
	public function delete_book($book_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...
441
	{
442
		// Do not delete if the book has assigned chapters
443
		if ($this->chapter_operator->count_chapters($book_id) > 0)
444
		{
445
			trigger_error($this->language->lang('ERROR_BOOK_DELETE_IN_USE') . adm_back_link($this->u_action), E_USER_WARNING);
446
		}
447
448
		/** @var \vinabb\web\entities\bbook_book_interface $entity */
449
		$entity = $this->container->get('vinabb.web.entities.bbook_book')->load($book_id);
450
451
		try
452
		{
453
			$this->operator->delete_book($book_id);
454
			$this->filesystem->remove($entity->get_img(true));
455
		}
456
		catch (\vinabb\web\exceptions\base $e)
457
		{
458
			trigger_error($this->language->lang('ERROR_BOOK_DELETE', $e->get_message($this->language)) . adm_back_link($this->u_action), E_USER_WARNING);
459
		}
460
461
		$this->config->increment('vinabb_web_total_books', -1, false);
462
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_BBOOK_BOOK_DELETE', time(), [$entity->get_name()]);
463
464
		// If AJAX was used, show user a result message
465
		if ($this->request->is_ajax())
466
		{
467
			$json_response = new \phpbb\json_response;
468
			$json_response->send([
469
				'MESSAGE_TITLE'	=> $this->language->lang('INFORMATION'),
470
				'MESSAGE_TEXT'	=> $this->language->lang('MESSAGE_BOOK_DELETE'),
471
				'REFRESH_DATA'	=> ['time'	=> 3]
472
			]);
473
		}
474
	}
475
}
476