Completed
Push — master ( 561264...fd44b1 )
by Khoa
14:56
created

bbook_chapters::set_form_data()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 5
Code Lines 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
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 int $book_id */
55
	protected $book_id;
56
57
	/** @var array $data */
58
	protected $data;
59
60
	/** @var array $errors */
61
	protected $errors;
62
63
	/**
64
	* Constructor
65
	*
66
	* @param \vinabb\web\controllers\cache\service_interface	$cache			Cache service
67
	* @param ContainerInterface									$container		Container object
68
	* @param \phpbb\language\language							$language		Language object
69
	* @param \phpbb\log\log										$log			Log object
70
	* @param \vinabb\web\operators\bbook_chapter_interface		$operator		Chapter operators
71
	* @param \phpbb\request\request								$request		Request object
72
	* @param \phpbb\template\template							$template		Template object
73
	* @param \phpbb\user										$user			User object
74
	* @param \vinabb\web\controllers\helper\helper_interface	$ext_helper		Extension helper
75
	* @param string												$root_path		phpBB root path
76
	* @param string												$php_ext		PHP file extension
77
	*/
78
	public function __construct(
79
		\vinabb\web\controllers\cache\service_interface $cache,
80
		ContainerInterface $container,
81
		\phpbb\language\language $language,
82
		\phpbb\log\log $log,
83
		\vinabb\web\operators\bbook_chapter_interface $operator,
84
		\phpbb\request\request $request,
85
		\phpbb\template\template $template,
86
		\phpbb\user $user,
87
		\vinabb\web\controllers\helper\helper_interface $ext_helper,
88
		$root_path,
89
		$php_ext
90
	)
91
	{
92
		$this->cache = $cache;
93
		$this->container = $container;
94
		$this->language = $language;
95
		$this->log = $log;
96
		$this->operator = $operator;
97
		$this->request = $request;
98
		$this->template = $template;
99
		$this->user = $user;
100
		$this->ext_helper = $ext_helper;
101
		$this->root_path = $root_path;
102
		$this->php_ext = $php_ext;
103
	}
104
105
	/**
106
	* Set form data
107
	*
108
	* @param array $data Form data
109
	*/
110
	public function set_form_data($data)
111
	{
112
		$this->book_id = $data['book_id'];
113
		$this->u_action = $data['u_action'];
114
	}
115
116
	/**
117
	* Display chapters
118
	*
119
	* @param int $parent_id Parent ID
120
	*/
121
	public function display_chapters($parent_id = 0)
122
	{
123
		// Grab all from database
124
		$entities = $this->operator->get_chapters($this->book_id, $parent_id);
125
126
		// Initialize a variable to hold the right_id value
127
		$last_right_id = 0;
128
129
		/** @var \vinabb\web\entities\bbook_chapter_interface $entity */
130
		foreach ($entities as $entity)
131
		{
132
			// The current entity is a child of a previous entity, do not display it
133
			if ($entity->get_left_id() < $last_right_id)
134
			{
135
				continue;
136
			}
137
138
			$this->template->assign_block_vars('chapters', [
139
				'URL'		=> "{$this->u_action}&parent_id={$entity->get_id()}",
140
				'NAME'		=> $entity->get_name(),
141
				'TIME'		=> $this->user->format_date($entity->get_time()),
142
143
				'S_IS_CAT'	=> $entity->get_right_id() - $entity->get_left_id() > 1,
144
145
				'U_EDIT'		=> "{$this->u_action}&action=edit&id={$entity->get_id()}",
146
				'U_MOVE_DOWN'	=> "{$this->u_action}&action=move_down&id={$entity->get_id()}&hash=" . generate_link_hash('down' . $entity->get_id()),
147
				'U_MOVE_UP'		=> "{$this->u_action}&action=move_up&id={$entity->get_id()}&hash=" . generate_link_hash('up' . $entity->get_id()),
148
				'U_DELETE'		=> "{$this->u_action}&action=delete&id={$entity->get_id()}"
149
			]);
150
151
			// Store the current right_id value
152
			$last_right_id = $entity->get_right_id();
153
		}
154
155
		// Prepare rule breadcrumb path navigation
156
		$entities = $this->operator->get_parents($parent_id);
157
158
		// Process each entity for breadcrumb
159
		/** @var \vinabb\web\entities\bbook_chapter_interface $entity */
160
		foreach ($entities as $entity)
161
		{
162
			$this->template->assign_block_vars('breadcrumb', [
163
				'NAME'	=> $entity->get_name(),
164
				'URL'	=> "{$this->u_action}&parent_id={$entity->get_id()}",
165
166
				'S_CURRENT'	=> $entity->get_id() == $parent_id
167
			]);
168
		}
169
170
		$this->template->assign_vars([
171
			'U_ACTION'	=> "{$this->u_action}&action=add&parent_id={$parent_id}",
172
			'U_ROOT'	=> "{$this->u_action}&parent_id=0"
173
		]);
174
	}
175
176
	/**
177
	* Add a chapter
178
	*
179
	* @param int $parent_id Parent ID
180
	*/
181
	public function add_chapter($parent_id = 0)
182
	{
183
		// Initiate an entity
184
		/** @var \vinabb\web\entities\bbook_chapter_interface $entity */
185
		$entity = $this->container->get('vinabb.web.entities.bbook_chapter');
186
187
		// Process the new entity
188
		$this->add_edit_data($entity);
189
190
		// Build the parent selection
191
		$this->build_parent_options($entity, $parent_id, 'add');
192
193
		$this->template->assign_vars([
194
			'S_ADD'	=> true,
195
196
			'U_ACTION'	=> "{$this->u_action}&action=add&parent_id={$parent_id}",
197
			'U_BACK'	=> "{$this->u_action}&parent_id={$parent_id}"
198
		]);
199
	}
200
201
	/**
202
	* Edit a chapter
203
	*
204
	* @param int $chapter_id Chapter ID
205
	*/
206
	public function edit_chapter($chapter_id)
207
	{
208
		// Initiate and load the entity
209
		/** @var \vinabb\web\entities\bbook_chapter_interface $entity */
210
		$entity = $this->container->get('vinabb.web.entities.bbook_chapter')->load($chapter_id);
211
212
		// Process the edited entity
213
		$this->add_edit_data($entity);
214
215
		// Build the parent selection
216
		$this->build_parent_options($entity);
217
218
		$this->template->assign_vars([
219
			'S_EDIT'	=> true,
220
			'S_IS_CAT'	=> ($entity->get_right_id() - $entity->get_left_id()) > 1,
221
222
			'U_ACTION'	=> "{$this->u_action}&action=edit&id={$chapter_id}",
223
			'U_BACK'	=> "{$this->u_action}&parent_id={$entity->get_parent_id()}"
224
		]);
225
	}
226
227
	/**
228
	* Process data to be added or edited
229
	*
230
	* @param \vinabb\web\entities\bbook_chapter_interface $entity Chapter entity
231
	*/
232
	public function add_edit_data(\vinabb\web\entities\bbook_chapter_interface $entity)
233
	{
234
		$submit = $this->request->is_set_post('submit');
235
236
		// Create a form key for preventing CSRF attacks
237
		add_form_key('acp_bbook_chapters');
238
239
		// Get form data
240
		$this->request_data();
241
242
		// Set the parse options to the entity
243
		$this->set_bbcode_options($entity, $submit);
244
245
		if ($submit)
246
		{
247
			// Test if the submitted form is valid
248
			if (!check_form_key('acp_bbook_chapters'))
249
			{
250
				$this->errors[] = $this->language->lang('FORM_INVALID');
251
			}
252
253
			// Map and set data to the entity
254
			$this->map_set_data($entity);
255
256
			// Insert or update
257
			if (empty($this->errors))
258
			{
259
				$this->save_data($entity);
260
			}
261
		}
262
263
		// Output
264
		$this->data_to_tpl($entity);
265
266
		$this->template->assign_vars([
267
			'ERRORS'	=> !empty($this->errors) ? implode('<br>', $this->errors) : ''
268
		]);
269
270
		// Custom BBCode
271
		include_once "{$this->root_path}includes/functions_display.{$this->php_ext}";
272
		display_custom_bbcodes();
273
	}
274
275
	/**
276
	* Request data from the form
277
	*/
278
	protected function request_data()
279
	{
280
		$this->data = [
281
			'parent_id'		=> $this->request->variable('parent_id', 0),
282
			'chapter_name'	=> $this->request->variable('chapter_name', '', true),
283
			'chapter_text'	=> $this->request->variable('chapter_text', '', true),
284
			'chapter_time'	=> null
285
		];
286
	}
287
288
	/**
289
	* Grab the form data's parsing options and set them to the entity
290
	*
291
	* If submit, use data from the form
292
	* In edit mode, use data stored in the entity
293
	* In add mode, use default values
294
	*
295
	* @param \vinabb\web\entities\bbook_chapter_interface $entity Chapter entity
296
	*/
297
	protected function set_bbcode_options(\vinabb\web\entities\bbook_chapter_interface $entity, $submit)
298
	{
299
		$entity->text_enable_bbcode($submit ? $this->request->is_set_post('text_bbcode') : ($entity->get_id() ? $entity->text_bbcode_enabled() : true));
300
		$entity->text_enable_urls($submit ? $this->request->is_set_post('text_urls') : ($entity->get_id() ? $entity->text_urls_enabled() : true));
301
		$entity->text_enable_smilies($submit ? $this->request->is_set_post('text_smilies') : ($entity->get_id() ? $entity->text_smilies_enabled() : true));
302
	}
303
304
	/**
305
	* Map the form data fields to setters and set them to the entity
306
	*
307
	* @param \vinabb\web\entities\bbook_chapter_interface $entity Chapter entity
308
	*/
309
	protected function map_set_data(\vinabb\web\entities\bbook_chapter_interface $entity)
310
	{
311
		$map_fields = [
312
			'set_parent_id'	=> $this->data['parent_id'],
313
			'set_name'		=> $this->data['chapter_name'],
314
			'set_name_seo'	=> $this->ext_helper->clean_url($this->data['chapter_name']),
315
			'set_text'		=> $this->data['chapter_text'],
316
			'set_time'		=> null
317
		];
318
319
		// Set the mapped data in the entity
320
		foreach ($map_fields as $entity_function => $chapter_data)
321
		{
322
			try
323
			{
324
				// Calling the $entity_function on the entity and passing it $chapter_data
325
				$entity->$entity_function($chapter_data);
326
			}
327
			catch (\vinabb\web\exceptions\base $e)
328
			{
329
				$this->errors[] = $e->get_friendly_message($this->language);
330
			}
331
		}
332
333
		unset($map_fields);
334
	}
335
336
	/**
337
	* Insert or update data, then log actions and clear cache if needed
338
	*
339
	* @param \vinabb\web\entities\bbook_chapter_interface $entity Chapter entity
340
	*/
341
	protected function save_data(\vinabb\web\entities\bbook_chapter_interface $entity)
342
	{
343
		if ($entity->get_id())
344
		{
345
			// Save the edited entity to the database
346
			$entity->save();
347
348
			// Change the parent
349
			if ($this->data['parent_id'] != $entity->get_parent_id())
350
			{
351
				try
352
				{
353
					$this->operator->change_parent($entity->get_id(), $this->data['parent_id']);
354
				}
355
				catch (\vinabb\web\exceptions\base $e)
356
				{
357
					trigger_error($this->language->lang('ERROR_CHAPTER_CHANGE_PARENT', $e->get_message($this->language)) . adm_back_link($this->u_action), E_USER_WARNING);
358
				}
359
			}
360
361
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_BBOOK_CHAPTER_EDIT', time(), [$entity->get_name()]);
362
363
			$message = 'MESSAGE_CHAPTER_EDIT';
364
		}
365
		else
366
		{
367
			// Add the new entity to the database
368
			$entity = $this->operator->add_chapter($entity, $this->data['parent_id']);
369
370
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_BBOOK_CHAPTER_ADD', time(), [$entity->get_name()]);
371
372
			$message = 'MESSAGE_CHAPTER_ADD';
373
		}
374
375
		trigger_error($this->language->lang($message) . adm_back_link("{$this->u_action}&parent_id={$this->data['parent_id']}"));
376
	}
377
378
	/**
379
	* Output entity data to template variables
380
	*
381
	* @param \vinabb\web\entities\bbook_chapter_interface $entity Chapter entity
382
	*/
383
	protected function data_to_tpl(\vinabb\web\entities\bbook_chapter_interface $entity)
384
	{
385
		$this->template->assign_vars([
386
			'CHAPTER_NAME'	=> $entity->get_name(),
387
			'CHAPTER_TEXT'	=> $entity->get_text_for_edit()
388
		]);
389
	}
390
391
	/**
392
	* Move a rule up/down
393
	*
394
	* @param int	$chapter_id	Chapter ID
395
	* @param string	$direction	The direction (up|down)
396
	* @param int	$amount		The number of places to move
397
	*/
398
	public function move_chapter($chapter_id, $direction, $amount = 1)
399
	{
400
		// Check the valid link hash
401
		if (!check_link_hash($this->request->variable('hash', ''), $direction . $chapter_id))
402
		{
403
			trigger_error($this->language->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
404
		}
405
406
		try
407
		{
408
			$this->operator->move_chapter($chapter_id, $direction, $amount);
409
		}
410
		catch (\vinabb\web\exceptions\base $e)
411
		{
412
			trigger_error($this->language->lang('ERROR_CHAPTER_MOVE', $e->get_message($this->language)) . adm_back_link($this->u_action), E_USER_WARNING);
413
		}
414
415
		// If AJAX was used, show user a result message
416
		if ($this->request->is_ajax())
417
		{
418
			$json_response = new \phpbb\json_response;
419
			$json_response->send(['success' => true]);
420
		}
421
422
		// Initiate and load the entity for no AJAX request
423
		/** @var \vinabb\web\entities\bbook_chapter_interface $entity */
424
		$entity = $this->container->get('vinabb.web.entities.bbook_chapter')->load($chapter_id);
425
426
		// Reload the current page
427
		redirect("{$this->u_action}&parent_id={$entity->get_parent_id()}");
428
	}
429
430
	/**
431
	* Delete a chapter
432
	*
433
	* @param int $chapter_id Chapter ID
434
	*/
435
	public function delete_chapter($chapter_id)
436
	{
437
		/** @var \vinabb\web\entities\bbook_chapter_interface $entity */
438
		$entity = $this->container->get('vinabb.web.entities.bbook_chapter')->load($chapter_id);
439
440
		try
441
		{
442
			$this->operator->delete_chapter($chapter_id);
443
		}
444
		catch (\vinabb\web\exceptions\base $e)
445
		{
446
			trigger_error($this->language->lang('ERROR_CHAPTER_DELETE', $e->get_message($this->language)) . adm_back_link($this->u_action), E_USER_WARNING);
447
		}
448
449
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_BBOOK_CHAPTER_DELETE', time(), [$entity->get_name()]);
450
451
		// If AJAX was used, show user a result message
452
		if ($this->request->is_ajax())
453
		{
454
			$json_response = new \phpbb\json_response;
455
			$json_response->send([
456
				'MESSAGE_TITLE'	=> $this->language->lang('INFORMATION'),
457
				'MESSAGE_TEXT'	=> $this->language->lang('MESSAGE_CHAPTER_DELETE'),
458
				'REFRESH_DATA'	=> ['time'	=> 3]
459
			]);
460
		}
461
	}
462
463
	/**
464
	* Generate options of available parents
465
	*
466
	* @param \vinabb\web\entities\bbook_chapter_interface	$entity		Chapter entity
467
	* @param int											$parent_id	Parent ID
468
	* @param string											$mode		Add or edit mode?
469
	*/
470
	protected function build_parent_options(\vinabb\web\entities\bbook_chapter_interface $entity, $parent_id = 0, $mode = 'edit')
471
	{
472
		$options = $this->operator->get_chapters($this->book_id);
473
		$parent_id = ($mode == 'edit') ? $entity->get_parent_id() : $parent_id;
474
475
		$padding = '';
476
		$padding_store = [];
477
		$right = 0;
478
479
		/** @var \vinabb\web\entities\bbook_chapter_interface $option */
480
		foreach ($options as $option)
481
		{
482
			// Update padding for the current tree level
483
			$this->ext_helper->build_parent_padding($padding, $padding_store, $right, $option->get_parent_id(), $option->get_left_id(), $option->get_right_id());
484
485
			$this->template->assign_block_vars('parent_options', [
486
				'ID'		=> $option->get_id(),
487
				'NAME'		=> $padding . $option->get_name(),
488
489
				'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())),
490
				'S_SELECTED'	=> $option->get_id() == $parent_id
491
			]);
492
		}
493
	}
494
}
495