Completed
Push — 3.3.x ( 37d736...41194d )
by Erwan
02:32
created

cat::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 17
nc 2
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
*
4
* phpBB Directory extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 ErnadoO <http://www.phpbb-services.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace ernadoo\phpbbdirectory\controller\acp;
12
13
use \ernadoo\phpbbdirectory\core\helper;
14
15
class cat extends helper
16
{
17
	/** @var \phpbb\cache\service */
18
	protected $cache;
19
20
	/** @var \phpbb\db\driver\driver_interface */
21
	protected $db;
22
23
	/** @var \phpbb\controller\helper */
24
	protected $helper;
25
26
	/** @var \phpbb\language\language */
27
	protected $language;
28
29
	/** @var \phpbb\log\log */
30
	protected $phpbb_log;
31
32
	/** @var \phpbb\request\request */
33
	protected $request;
34
35
	/** @var \phpbb\template\template */
36
	protected $template;
37
38
	/** @var \phpbb\user */
39
	protected $user;
40
41
	/** @var \ernadoo\phpbbdirectory\core\categorie */
42
	protected $categorie;
43
44
	/** @var \ernadoo\phpbbdirectory\core\nestedset_category */
45
	protected $nestedset_category;
46
47
	/** @var string Custom form action */
48
	protected $u_action;
49
50
	/** @var string */
51
	private $action;
52
53
	/** @var array */
54
	private $cat_data = array();
55
56
	/** @var int */
57
	private $cat_id;
58
59
	/** @var array */
60
	private $errors;
61
62
	/** @var string */
63
	private $form_key;
64
65
	/** @var int */
66
	private $parent_id;
67
68
	/** @var bool */
69
	private $update;
70
71
	/**
72
	* Constructor
73
	*
74
	* @param \phpbb\cache\service								$cache				Cache object
75
	* @param \phpbb\db\driver\driver_interface 					$db					Database object
76
	* @param \phpbb\controller\helper							$helper				Helper object
77
	* @param \phpbb\language\language							$language			Language object
78
	* @param \phpbb\log\log										$log				Log object
79
	* @param \phpbb\request\request								$request			Request object
80
	* @param \phpbb\template\template							$template			Template object
81
	* @param \phpbb\user										$user				User object
82
	* @param \ernadoo\phpbbdirectory\core\categorie				$categorie			PhpBB Directory extension categorie object
83
	* @param \ernadoo\phpbbdirectory\core\nestedset_category	$nestedset_category	PhpBB Directory extension nestedset object
84
	*/
85
	public function __construct(\phpbb\cache\service $cache, \phpbb\db\driver\driver_interface $db, \phpbb\controller\helper $helper, \phpbb\language\language $language, \phpbb\log\log $log, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, \ernadoo\phpbbdirectory\core\categorie $categorie, \ernadoo\phpbbdirectory\core\nestedset_category $nestedset_category)
86
	{
87
		$this->cache				= $cache;
88
		$this->db					= $db;
89
		$this->helper				= $helper;
90
		$this->language				= $language;
91
		$this->phpbb_log			= $log;
92
		$this->request				= $request;
93
		$this->template				= $template;
94
		$this->user					= $user;
95
		$this->categorie			= $categorie;
96
		$this->nestedset_category	= $nestedset_category;
97
98
		$this->form_key = 'acp_dir_cat';
99
		add_form_key($this->form_key);
100
101
		$this->action		= $this->request->variable('action', '');
102
		$this->cat_id		= $request->variable('c', 0);
103
		$this->parent_id	= $request->variable('parent_id', 0);
104
		$this->update		= ($this->request->is_set_post('update')) ? true : false;
105
	}
106
107
	/**
108
	* Initialize defaults data for add page
109
	*
110
	* @return null
111
	*/
112
	public function action_add()
113
	{
114
		$this->cat_id = $this->parent_id;
115
		$parents_list = $this->categorie->make_cat_select($this->parent_id);
116
117
		// Fill categorie data with default values
118
		if (!$this->update)
119
		{
120
			$this->cat_data = array(
121
				'parent_id'				=> $this->parent_id,
122
				'cat_name'				=> $this->request->variable('cat_name', '', true),
123
				'cat_route'				=> '',
124
				'cat_desc'				=> '',
125
				'cat_icon'				=> '',
126
				'cat_allow_comments'	=> true,
127
				'cat_allow_votes'		=> true,
128
				'cat_must_describe'		=> true,
129
				'cat_count_all'			=> false,
130
				'cat_validate'			=> false,
131
				'enable_icons'			=> false,
132
133
				'display_subcat_list'	=> true,
134
135
				'cat_link_back'			=> false,
136
				'cat_cron_enable'		=> false,
137
				'cat_cron_freq'			=> 7,
138
				'cat_cron_nb_check'		=> 1,
139
			);
140
		}
141
142
		$this->_display_cat_form($parents_list);
143
	}
144
145
	/**
146
	* Display deleting page
147
	*
148
	* @return null
149
	*/
150
	public function action_delete()
151
	{
152 View Code Duplication
		if (!$this->cat_id)
153
		{
154
			trigger_error($this->language->lang('DIR_NO_CAT') . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
155
		}
156
157
		$this->cat_data = $this->_get_cat_info($this->cat_id);
158
159
		$subcats_id = array();
160
		$subcats = $this->nestedset_category->get_subtree_data($this->cat_id);
161
162
		foreach ($subcats as $row)
163
		{
164
			$subcats_id[] = $row['cat_id'];
165
		}
166
167
		$cat_list = $this->categorie->make_cat_select((int) $this->cat_data['parent_id'], $subcats_id);
168
169
		$sql = 'SELECT cat_id
170
			FROM ' . $this->categories_table . '
171
			WHERE cat_id <> ' . (int) $this->cat_id;
172
		$result = $this->db->sql_query_limit($sql, 1);
173
174
		if ($this->db->sql_fetchrow($result))
175
		{
176
			$this->template->assign_vars(array(
177
				'S_MOVE_DIR_CAT_OPTIONS'	=> $this->categorie->make_cat_select((int) $this->cat_data['parent_id'], $subcats_id))
178
			);
179
		}
180
		$this->db->sql_freeresult($result);
181
182
		$parent_id = ($this->parent_id == $this->cat_id) ? 0 : $this->parent_id;
183
184
		$this->template->assign_vars(array(
185
			'S_DELETE_DIR_CAT'		=> true,
186
			'U_ACTION'				=> $this->u_action . "&amp;parent_id={$parent_id}&amp;action=delete&amp;c=$this->cat_id",
187
			'U_BACK'				=> $this->u_action . '&amp;parent_id=' . $this->parent_id,
188
189
			'DIR_CAT_NAME'			=> $this->cat_data['cat_name'],
190
			'S_HAS_SUBCATS'			=> ($this->cat_data['right_id'] - $this->cat_data['left_id'] > 1) ? true : false,
191
			'S_CATS_LIST'			=> $cat_list,
192
			'S_ERROR'				=> (sizeof($this->errors)) ? true : false,
193
			'ERROR_MSG'				=> (sizeof($this->errors)) ? implode('<br />', $this->errors) : '')
194
		);
195
196
		return;
197
	}
198
199
	/**
200
	* Initialize data for edit page
201
	*
202
	* @return null
203
	*/
204
	public function action_edit()
205
	{
206
		$row = $this->_get_cat_info($this->cat_id);
207
208
		if (!$this->update)
209
		{
210
			$this->cat_data = $row;
211
		}
212
		else
213
		{
214
			$this->cat_data['left_id'] = $row['left_id'];
215
			$this->cat_data['right_id'] = $row['right_id'];
216
		}
217
218
		// Make sure no direct child categories are able to be selected as parents.
219
		$exclude_cats = array();
220
		foreach ($this->nestedset_category->get_subtree_data($this->cat_id) as $row2)
221
		{
222
			$exclude_cats[] = $row2['cat_id'];
223
		}
224
		$parents_list = $this->categorie->make_cat_select((int) $this->cat_data['parent_id'], $exclude_cats);
225
226
		$this->_display_cat_form($parents_list);
227
	}
228
229
	/**
230
	* Move order categories
231
	*
232
	* @return null
233
	*/
234
	public function action_move()
235
	{
236 View Code Duplication
		if (!$this->cat_id)
237
		{
238
			trigger_error($this->language->lang('DIR_NO_CAT') . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
239
		}
240
241
		$sql = 'SELECT cat_id, cat_name, parent_id, left_id, right_id
242
			FROM ' . $this->categories_table . '
243
			WHERE cat_id = ' . (int) $this->cat_id;
244
		$result = $this->db->sql_query($sql);
245
		$row = $this->db->sql_fetchrow($result);
246
		$this->db->sql_freeresult($result);
247
248 View Code Duplication
		if (!$row)
249
		{
250
			trigger_error($this->language->lang('DIR_NO_CAT') . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
251
		}
252
253
		try
254
		{
255
			$move_cat_name = $this->nestedset_category->{$this->action}($this->cat_id);
256
		}
257
		catch (\Exception $e)
258
		{
259
			trigger_error($e->getMessage(), E_USER_WARNING);
260
		}
261
262
		if ($move_cat_name !== false)
263
		{
264
			$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_CAT_' . strtoupper($this->action), time(), array($row['cat_name'], $move_cat_name));
265
			$this->cache->destroy('sql', $this->categories_table);
266
		}
267
268
		if ($this->request->is_ajax())
269
		{
270
			$json_response = new \phpbb\json_response;
271
			$json_response->send(array('success' => ($move_cat_name !== false)));
272
		}
273
	}
274
275
	/**
276
	* Display progress bar for syncinc categories
277
	*
278
	* @return null
279
	*/
280
	public function action_progress_bar()
281
	{
282
		$start = $this->request->variable('start', 0);
283
		$total = $this->request->variable('total', 0);
284
285
		adm_page_header($this->language->lang('SYNC_IN_PROGRESS'));
286
287
		$this->template->set_filenames(array(
288
			'body'	=> 'progress_bar.html')
289
		);
290
291
		$this->template->assign_vars(array(
292
			'L_PROGRESS'			=> $this->language->lang('SYNC_IN_PROGRESS'),
293
			'L_PROGRESS_EXPLAIN'	=> ($start && $total) ? $this->language->lang('SYNC_IN_PROGRESS_EXPLAIN', $start, $total) : $this->language->lang('SYNC_IN_PROGRESS'))
294
		);
295
296
		adm_page_footer();
297
	}
298
299
	/**
300
	* Get link's ID interval for _sync_dir_links()
301
	*
302
	* @return null
303
	*/
304
	public function action_sync()
305
	{
306 View Code Duplication
		if (!$this->cat_id)
307
		{
308
			trigger_error($this->language->lang('DIR_NO_CAT') . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
309
		}
310
311
		@set_time_limit(0);
312
313
		$sql = 'SELECT cat_name, cat_links
314
			FROM ' . $this->categories_table . '
315
			WHERE cat_id = ' . (int) $this->cat_id;
316
		$result = $this->db->sql_query($sql);
317
		$row = $this->db->sql_fetchrow($result);
318
		$this->db->sql_freeresult($result);
319
320 View Code Duplication
		if (!$row)
321
		{
322
			trigger_error($this->language->lang('DIR_NO_CAT') . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
323
		}
324
325
		$sql = 'SELECT MIN(link_id) as min_link_id, MAX(link_id) as max_link_id
326
			FROM ' . $this->links_table . '
327
			WHERE link_cat = ' . (int) $this->cat_id . '
328
				AND link_active = 1';
329
		$result = $this->db->sql_query($sql);
330
		$row2 = $this->db->sql_fetchrow($result);
331
		$this->db->sql_freeresult($result);
332
333
		// Typecast to int if there is no data available
334
		$row2['min_link_id'] = (int) $row2['min_link_id'];
335
		$row2['max_link_id'] = (int) $row2['max_link_id'];
336
337
		$start = $this->request->variable('start', $row2['min_link_id']);
338
339
		$batch_size = 200;
340
		$end = $start + $batch_size;
341
342
		// Sync all links in batch mode...
343
		$this->_sync_dir_links($start, $end);
344
345
		if ($end < $row2['max_link_id'])
346
		{
347
			// We really need to find a way of showing statistics... no progress here
348
			$sql = 'SELECT COUNT(link_id) as num_links
349
				FROM ' . $this->links_table . '
350
				WHERE link_cat = ' . (int) $this->cat_id . '
351
						AND link_active = 1
352
						AND link_id BETWEEN ' . $start . ' AND ' . $end;
353
			$result = $this->db->sql_query($sql);
354
			$links_done = $this->request->variable('links_done', 0) + (int) $this->db->sql_fetchfield('num_links');
355
			$this->db->sql_freeresult($result);
356
357
			$start += $batch_size;
358
359
			$url = $this->u_action . "&amp;parent_id={$this->parent_id}&amp;c=$this->cat_id&amp;action=sync&amp;start=$start&amp;links_done=$links_done&amp;total={$row['cat_links']}";
360
361
			meta_refresh(0, $url);
362
363
			$this->template->assign_vars(array(
364
				'UA_PROGRESS_BAR'		=> $this->u_action . "&amp;action=progress_bar&amp;start=$links_done&amp;total={$row['cat_links']}",
365
				'S_CONTINUE_SYNC'		=> true,
366
				'L_PROGRESS_EXPLAIN'	=> $this->language->lang('SYNC_IN_PROGRESS_EXPLAIN', $links_done, $row['cat_links']))
367
			);
368
369
			return;
370
		}
371
372
		$url = $this->u_action . "&amp;parent_id={$this->parent_id}&amp;c=$this->cat_id&amp;action=sync_cat";
373
		meta_refresh(0, $url);
374
375
		$this->template->assign_vars(array(
376
			'UA_PROGRESS_BAR'		=> $this->u_action . '&amp;action=progress_bar',
377
			'S_CONTINUE_SYNC'		=> true,
378
			'L_PROGRESS_EXPLAIN'	=> $this->language->lang('SYNC_IN_PROGRESS_EXPLAIN', 0, $row['cat_links']))
379
		);
380
381
		return;
382
	}
383
384
	/**
385
	* Sync category data
386
	*
387
	* @return null
388
	*/
389
	public function action_sync_cat()
390
	{
391
		$sql = 'SELECT cat_name
392
			FROM ' . $this->categories_table . '
393
			WHERE cat_id = ' . (int) $this->cat_id;
394
		$result = $this->db->sql_query($sql);
395
		$row = $this->db->sql_fetchrow($result);
396
		$this->db->sql_freeresult($result);
397
398 View Code Duplication
		if (!$row)
399
		{
400
			trigger_error($this->language->lang('DIR_NO_CAT') . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id), E_USER_WARNING);
401
		}
402
403
		$this->_sync_dir_cat($this->cat_id);
404
405
		$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_CAT_SYNC', time(), array($row['cat_name']));
406
		$this->cache->destroy('sql', $this->categories_table);
407
408
		$this->template->assign_var('L_DIR_CAT_RESYNCED', $this->language->lang('DIR_CAT_RESYNCED', $row['cat_name']));
409
	}
410
411
	/**
412
	* Display categories page
413
	*
414
	* @return null
415
	*/
416
	public function display_cats()
417
	{
418
		// Default management page
419
		if (!$this->parent_id)
420
		{
421
			$navigation = $this->language->lang('DIR_INDEX');
422
		}
423
		else
424
		{
425
			$navigation = '<a href="' . $this->u_action . '">' . $this->language->lang('DIR_INDEX') . '</a>';
426
427
			$cats_nav = $this->nestedset_category->get_path_data($this->parent_id);
428
429
			foreach ($cats_nav as $row)
430
			{
431
				if ($row['cat_id'] == $this->parent_id)
432
				{
433
					$navigation .= ' -&gt; ' . $row['cat_name'];
434
				}
435
				else
436
				{
437
					$navigation .= ' -&gt; <a href="' . $this->u_action . '&amp;parent_id=' . $row['cat_id'] . '">' . $row['cat_name'] . '</a>';
438
				}
439
			}
440
		}
441
442
		// Jumpbox
443
		$cat_box = $this->categorie->make_cat_select($this->parent_id);
444
445
		if ($this->action == 'sync' || $this->action == 'sync_cat')
446
		{
447
			$this->template->assign_var('S_RESYNCED', true);
448
		}
449
450
		$sql = 'SELECT cat_id, parent_id, right_id, left_id, cat_name, cat_icon, cat_desc_uid, cat_desc_bitfield, cat_desc, cat_desc_options, cat_links
451
			FROM ' . $this->categories_table . '
452
			WHERE parent_id = ' . (int) $this->parent_id . '
453
			ORDER BY left_id';
454
		$result = $this->db->sql_query($sql);
455
456
		if ($row = $this->db->sql_fetchrow($result))
457
		{
458
			do
459
			{
460
				$folder_image = ($row['left_id'] + 1 != $row['right_id']) ? '<img src="images/icon_subfolder.gif" alt="' . $this->language->lang('DIR_SUBCAT') . '" />' : '<img src="images/icon_folder.gif" alt="' . $this->language->lang('FOLDER') . '" />';
461
462
				$url = $this->u_action . "&amp;parent_id=$this->parent_id&amp;c={$row['cat_id']}";
463
464
				$this->template->assign_block_vars('cats', array(
465
					'FOLDER_IMAGE'		=> $folder_image,
466
					'CAT_IMAGE'			=> ($row['cat_icon']) ? '<img src="' . $this->get_img_path('icons', $row['cat_icon']) . '" alt="" />' : '',
467
					'CAT_NAME'			=> $row['cat_name'],
468
					'CAT_DESCRIPTION'	=> generate_text_for_display($row['cat_desc'], $row['cat_desc_uid'], $row['cat_desc_bitfield'], $row['cat_desc_options']),
469
					'CAT_LINKS'			=> $row['cat_links'],
470
471
					'U_CAT'				=> $this->u_action . '&amp;parent_id=' . $row['cat_id'],
472
					'U_MOVE_UP'			=> $url . '&amp;action=move_up',
473
					'U_MOVE_DOWN'		=> $url . '&amp;action=move_down',
474
					'U_EDIT'			=> $url . '&amp;action=edit',
475
					'U_DELETE'			=> $url . '&amp;action=delete',
476
					'U_SYNC'			=> $url . '&amp;action=sync')
477
				);
478
			}
479
			while ($row = $this->db->sql_fetchrow($result));
480
		}
481
		else if ($this->parent_id)
482
		{
483
			$row = $this->_get_cat_info($this->parent_id);
484
485
			$url = $this->u_action . '&amp;parent_id=' . $this->parent_id . '&amp;c=' . $row['cat_id'];
486
487
			$this->template->assign_vars(array(
488
				'S_NO_CATS'			=> true,
489
490
				'U_EDIT'			=> $url . '&amp;action=edit',
491
				'U_DELETE'			=> $url . '&amp;action=delete',
492
				'U_SYNC'			=> $url . '&amp;action=sync')
493
			);
494
		}
495
		$this->db->sql_freeresult($result);
496
497
		$this->template->assign_vars(array(
498
			'ERROR_MSG'		=> (sizeof($this->errors)) ? implode('<br />', $this->errors) : '',
499
			'NAVIGATION'	=> $navigation,
500
			'CAT_BOX'		=> $cat_box,
501
			'U_SEL_ACTION'	=> $this->u_action,
502
			'U_ACTION'		=> $this->u_action . '&amp;parent_id=' . $this->parent_id,
503
504
			'UA_PROGRESS_BAR'	=> $this->u_action . '&amp;action=progress_bar',
505
		));
506
	}
507
508
	/**
509
	* Set page url
510
	*
511
	* @param	string $u_action Custom form action
512
	* @return	null
513
	* @access	public
514
	*/
515
	public function set_page_url($u_action)
516
	{
517
		$this->u_action = $u_action;
518
	}
519
520
	/**
521
	* Update cat table
522
	*
523
	* @return null
524
	*/
525
	public function update()
526
	{
527
		if (!check_form_key($this->form_key))
528
		{
529
			$this->update = false;
530
			$this->errors[] = $this->language->lang('FORM_INVALID');
531
		}
532
533
		switch ($this->action)
534
		{
535
			case 'delete':
536
				$action_subcats	= $this->request->variable('action_subcats', '');
537
				$subcats_to_id	= $this->request->variable('subcats_to_id', 0);
538
				$action_links	= $this->request->variable('action_links', '');
539
				$links_to_id	= $this->request->variable('links_to_id', 0);
540
541
				try
542
				{
543
					$this->errors = $this->_delete_cat($action_links, $action_subcats, $links_to_id, $subcats_to_id);
544
				}
545
				catch (\Exception $e)
546
				{
547
					trigger_error($e->getMessage(), E_USER_WARNING);
548
				}
549
550
				if (sizeof($this->errors))
551
				{
552
					break;
553
				}
554
555
				$this->cache->destroy('sql', $this->categories_table);
556
557
				trigger_error($this->language->lang('DIR_CAT_DELETED') . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id));
558
559
				break;
560
561
			case 'edit':
562
				$this->cat_data = array(
563
					'cat_id'		=>	$this->cat_id
564
				);
565
				// No break here
566
			case 'add':
567
568
				$this->cat_data += array(
569
					'parent_id'				=> $this->request->variable('cat_parent_id', (int) $this->parent_id),
570
					'cat_parents'			=> '',
571
					'cat_name'				=> $this->request->variable('cat_name', '', true),
572
					'cat_route'				=> $this->request->variable('cat_route', ''),
573
					'cat_desc'				=> $this->request->variable('cat_desc', '', true),
574
					'cat_desc_uid'			=> '',
575
					'cat_desc_options'		=> 7,
576
					'cat_desc_bitfield'		=> '',
577
					'cat_icon'				=> $this->request->variable('cat_icon', ''),
578
					'display_subcat_list'	=> $this->request->variable('display_on_index', false),
579
					'cat_allow_comments'	=> $this->request->variable('allow_comments', 1),
580
					'cat_allow_votes'		=> $this->request->variable('allow_votes', 1),
581
					'cat_must_describe'		=> $this->request->variable('must_describe', 1),
582
					'cat_count_all'			=> $this->request->variable('count_all', 0),
583
					'cat_validate'			=> $this->request->variable('validate', 0),
584
					'cat_link_back'			=> $this->request->variable('link_back', 0),
585
					'cat_cron_enable'		=> $this->request->variable('cron_enable', 0),
586
					'cat_cron_freq'			=> $this->request->variable('cron_every', 7),
587
					'cat_cron_nb_check'		=> $this->request->variable('nb_check', 1),
588
				);
589
590
				// Get data for cat description if specified
591
				if ($this->cat_data['cat_desc'])
592
				{
593
					generate_text_for_storage($this->cat_data['cat_desc'], $this->cat_data['cat_desc_uid'], $this->cat_data['cat_desc_bitfield'], $this->cat_data['cat_desc_options'], $this->request->variable('desc_parse_bbcode', false), $this->request->variable('desc_parse_urls', false), $this->request->variable('desc_parse_smilies', false));
594
				}
595
596
				try
597
				{
598
					$this->errors = $this->_update_cat_data();
599
				}
600
				catch (\Exception $e)
601
				{
602
					trigger_error($e->getMessage(), E_USER_WARNING);
603
				}
604
605
				if (!sizeof($this->errors))
606
				{
607
					$this->cache->destroy('sql', $this->categories_table);
608
609
					$message = ($this->action == 'add') ? $this->language->lang('DIR_CAT_CREATED') : $this->language->lang('DIR_CAT_UPDATED');
610
611
					trigger_error($message . adm_back_link($this->u_action . '&amp;parent_id=' . $this->parent_id));
612
				}
613
614
			break;
615
		}
616
617
		// Purge the cache to refresh route collections
618
		$this->cache->purge();
619
	}
620
621
	/**
622
	* Check route
623
	*
624
	* @param string $route Route text
625
	* @return null
626
	* @access public
627
	* @throws \phpbb\pages\exception\unexpected_value
628
	*/
629
	private function _check_route($route)
630
	{
631
		// Route is a required field
632
		if (empty($route))
633
		{
634
			$this->errors[] = $this->language->lang('DIR_CAT_ROUTE_EMPTY');
635
			return;
636
		}
637
638
		// Route should not contain any unexpected special characters
639
		if (!preg_match('/^[^!"#$%&*\'()+,.\/\\\\:;<=>?@\\[\\]^`{|}~ ]*$/', $route))
640
		{
641
			$this->errors[] = $this->language->lang('DIR_CAT_ROUTE_ILLEGAL_CHARACTERS');
642
		}
643
644
		$sql = 'SELECT cat_route
645
			FROM ' . $this->categories_table  . "
646
			WHERE cat_route = '" . $this->db->sql_escape($route) . "'
647
				AND cat_id <> " . $this->cat_id;
648
		$result = $this->db->sql_query_limit($sql, 1);
649
		$row = $this->db->sql_fetchrow($result);
650
		$this->db->sql_freeresult($result);
651
652
		if ($row)
653
		{
654
			$this->errors[] = $this->language->lang('DIR_CAT_ROUTE_NOT_UNIQUE');
655
		}
656
	}
657
658
	/**
659
	* Display form
660
	*
661
	* @param	string $parents_list	Drop-down list
662
	* @return	null
663
	*/
664
	private function _display_cat_form($parents_list)
665
	{
666
		$dir_cat_desc_data = array(
667
			'text'			=> $this->cat_data['cat_desc'],
668
			'allow_bbcode'	=> true,
669
			'allow_smilies'	=> true,
670
			'allow_urls'	=> true
671
		);
672
673
		// Parse desciption if specified
674
		if ($this->cat_data['cat_desc'])
675
		{
676
			if (!isset($this->cat_data['cat_desc_uid']))
677
			{
678
				// Before we are able to display the preview and plane text, we need to parse our $request->variable()'d value...
679
				$this->cat_data['cat_desc_uid'] = '';
680
				$this->cat_data['cat_desc_bitfield'] = '';
681
				$this->cat_data['cat_desc_options'] = 0;
682
683
				generate_text_for_storage($this->cat_data['cat_desc'], $this->cat_data['cat_desc_uid'], $this->cat_data['cat_desc_bitfield'], $this->cat_data['cat_desc_options'], $this->request->variable('desc_allow_bbcode', false), $this->request->variable('desc_allow_urls', false), $this->request->variable('desc_allow_smilies', false));
684
			}
685
686
			// decode...
687
			$dir_cat_desc_data = generate_text_for_edit($this->cat_data['cat_desc'], $this->cat_data['cat_desc_uid'], $this->cat_data['cat_desc_options']);
688
		}
689
690
		$this->template->assign_vars(array(
691
			'S_EDIT_CAT'		=> true,
692
			'S_ERROR'			=> (sizeof($this->errors)) ? true : false,
693
			'S_CAT_PARENT_ID'	=> $this->cat_data['parent_id'],
694
			'S_ADD_ACTION'		=> ($this->action == 'add') ? true : false,
695
696
			'U_BACK'			=> $this->u_action . '&amp;parent_id=' . $this->parent_id,
697
			'U_EDIT_ACTION'		=> $this->u_action . "&amp;parent_id={$this->parent_id}&amp;action=$this->action&amp;c=$this->cat_id",
698
699
			'L_TITLE'					=> $this->language->lang('DIR_' . strtoupper($this->action) . '_CAT'),
700
			'ERROR_MSG'					=> (sizeof($this->errors)) ? implode('<br />', $this->errors) : '',
701
			'ICON_IMAGE'				=> ($this->cat_data['cat_icon']) ? $this->get_img_path('icons', $this->cat_data['cat_icon']) : 'images/spacer.gif',
702
703
			'DIR_ICON_PATH'				=> $this->get_img_path('icons'),
704
			'DIR_CAT_NAME'				=> $this->cat_data['cat_name'],
705
			'DIR_CAT_ROUTE'				=> $this->cat_data['cat_route'],
706
			'DIR_CAT_DESC'				=> $dir_cat_desc_data['text'],
707
708
			'S_DESC_BBCODE_CHECKED'		=> ($dir_cat_desc_data['allow_bbcode']) ? true : false,
709
			'S_DESC_SMILIES_CHECKED'	=> ($dir_cat_desc_data['allow_smilies']) ? true : false,
710
			'S_DESC_URLS_CHECKED'		=> ($dir_cat_desc_data['allow_urls']) ? true : false,
711
			'S_DISPLAY_SUBCAT_LIST'		=> ($this->cat_data['display_subcat_list']) ? true : false,
712
			'S_PARENT_OPTIONS'			=> $parents_list,
713
			'S_ICON_OPTIONS'			=> $this->_get_dir_icon_list($this->get_img_path('icons'), $this->cat_data['cat_icon']),
714
			'S_ALLOW_COMMENTS'			=> ($this->cat_data['cat_allow_comments']) ? true : false,
715
			'S_ALLOW_VOTES'				=> ($this->cat_data['cat_allow_votes']) ? true : false,
716
			'S_MUST_DESCRIBE'			=> ($this->cat_data['cat_must_describe']) ? true : false,
717
			'S_COUNT_ALL'				=> ($this->cat_data['cat_count_all']) ? true : false,
718
			'S_VALIDATE'				=> ($this->cat_data['cat_validate']) ? true : false,
719
720
			'DIR_CRON_EVERY'			=> $this->cat_data['cat_cron_freq'],
721
			'DIR_NEXT_CRON_ACTION'		=> !empty($this->cat_data['cat_cron_next']) ? $this->user->format_date($this->cat_data['cat_cron_next']) : '-',
722
			'DIR_CRON_NB_CHECK'			=> $this->cat_data['cat_cron_nb_check'],
723
724
			'S_LINK_BACK'				=> ($this->cat_data['cat_link_back']) ? true : false,
725
			'S_CRON_ENABLE'				=> ($this->cat_data['cat_cron_enable']) ? true : false,
726
727
			'U_DATE'					=> $this->helper->route('ernadoo_phpbbdirectory_ajax_date_controller'),
728
			'U_SLUG'					=> $this->helper->route('ernadoo_phpbbdirectory_ajax_slug_controller'),
729
		));
730
731
		return;
732
	}
733
734
	/**
735
	* Get category details
736
	*
737
	* @param	int		$cat_id	The category ID
738
	* @return 	array
739
	*/
740 View Code Duplication
	private function _get_cat_info($cat_id)
741
	{
742
		$sql = 'SELECT cat_id, parent_id, right_id, left_id, cat_desc, cat_desc_uid, cat_desc_options, cat_icon, cat_name, cat_route, display_subcat_list, cat_allow_comments, cat_allow_votes, cat_must_describe, cat_count_all, cat_validate, cat_cron_freq, cat_cron_nb_check, cat_link_back, cat_cron_enable, cat_cron_next
743
			FROM ' . $this->categories_table . '
744
			WHERE cat_id = ' . (int) $cat_id;
745
		$result = $this->db->sql_query($sql);
746
		$row = $this->db->sql_fetchrow($result);
747
		$this->db->sql_freeresult($result);
748
749
		if (!$row)
750
		{
751
			trigger_error('DIR_ERROR_NO_CATS', E_USER_ERROR);
752
		}
753
754
		return $row;
755
	}
756
757
	/**
758
	* Update category data
759
	*
760
	* @return array
761
	*/
762
	private function _update_cat_data()
763
	{
764
		if (!$this->cat_data['cat_name'])
765
		{
766
			$this->errors[] = $this->language->lang('DIR_CAT_NAME_EMPTY');
767
		}
768
769
		$this->_check_route($this->cat_data['cat_route']);
770
771
		if (utf8_strlen($this->cat_data['cat_desc']) > 4000)
772
		{
773
			$this->errors[] = $this->language->lang('DIR_CAT_DESC_TOO_LONG');
774
		}
775
776
		if (($this->cat_data['cat_cron_enable'] && $this->cat_data['cat_cron_freq'] <= 0) || $this->cat_data['cat_cron_nb_check'] < 0)
777
		{
778
			$this->errors[] = $this->language->lang('DIR_CAT_DATA_NEGATIVE');
779
		}
780
781
		// Unset data that are not database fields
782
		$cat_data_sql = $this->cat_data;
783
784
		// What are we going to do tonight Brain? The same thing we do everynight,
785
		// try to take over the world ... or decide whether to continue update
786
		// and if so, whether it's a new cat/link or an existing one
787
		if (sizeof($this->errors))
788
		{
789
			return $this->errors;
790
		}
791
792
		if (!$cat_data_sql['cat_link_back'])
793
		{
794
			$cat_data_sql['cat_cron_enable'] = 0;
795
			$cat_data_sql['cat_cron_next'] = 0;
796
		}
797
798
		if (!$cat_data_sql['parent_id'])
799
		{
800
			$cat_data_sql['display_subcat_list'] = 0;
801
		}
802
803
		// no cat_id means we're creating a new categorie
804
		if (!isset($cat_data_sql['cat_id']))
805
		{
806
			if ($cat_data_sql['cat_cron_enable'])
807
			{
808
				$cat_data_sql['cat_cron_next'] = time() + $cat_data_sql['cat_cron_freq']*86400;
809
			}
810
811
			$this->cat_data = $this->nestedset_category->insert($cat_data_sql);
812
813
			if ($cat_data_sql['parent_id'])
814
			{
815
				$this->nestedset_category->change_parent($this->cat_data['cat_id'], $cat_data_sql['parent_id']);
816
			}
817
818
			$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_CAT_ADD', time(), array($this->cat_data['cat_name']));
819
		}
820
		else
821
		{
822
			$row = $this->_get_cat_info($cat_data_sql['cat_id']);
823
824
			if ($row['parent_id'] != $cat_data_sql['parent_id'])
825
			{
826
				$this->nestedset_category->change_parent($cat_data_sql['cat_id'], $cat_data_sql['parent_id']);
827
			}
828
829
			if ($cat_data_sql['cat_cron_enable'] && ($row['cat_cron_freq'] != $cat_data_sql['cat_cron_freq'] || !$row['cat_cron_enable']))
830
			{
831
				$cat_data_sql['cat_cron_next'] = time() + $cat_data_sql['cat_cron_freq']*86400;
832
			}
833
834
			if ($row['cat_name'] != $cat_data_sql['cat_name'])
835
			{
836
				// the cat name has changed, clear the parents list of all categories (for safety)
837
				$sql = 'UPDATE ' . $this->categories_table . "
838
					SET cat_parents = ''";
839
				$this->db->sql_query($sql);
840
			}
841
842
			// Setting the cat id to the categorie id is not really received well by some dbs. ;)
843
			unset($cat_data_sql['cat_id']);
844
845
			$sql = 'UPDATE ' . $this->categories_table . '
846
				SET ' . $this->db->sql_build_array('UPDATE', $cat_data_sql) . '
847
				WHERE cat_id = ' . (int) $this->cat_id;
848
			$this->db->sql_query($sql);
849
850
			$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_CAT_EDIT', time(), array($this->cat_data['cat_name']));
851
		}
852
853
		return $this->errors;
854
	}
855
856
	/**
857
	* Remove complete category
858
	*
859
	* @param	string	$action_links	Action for categories links
860
	* @param	string	$action_subcats	Action for sub-categories
861
	* @param	int		$links_to_id	New category ID for links
862
	* @param	int		$subcats_to_id	New category ID for sub-categories
863
	* @return 	array
864
	*/
865
	private function _delete_cat($action_links = 'delete', $action_subcats = 'delete', $links_to_id = 0, $subcats_to_id = 0)
866
	{
867
		$this->cat_data = $this->_get_cat_info($this->cat_id);
868
869
		$log_action_links = $log_action_cats = $links_to_name = $subcats_to_name = '';
870
871
		if ($action_links == 'delete')
872
		{
873
			$log_action_links = 'LINKS';
874
			$this->errors = array_merge($this->errors, $this->_delete_cat_content());
875
		}
876
		else if ($action_links == 'move')
877
		{
878
			if (!$links_to_id)
879
			{
880
				$this->errors[] = $this->language->lang('DIR_NO_DESTINATION_CAT');
881
			}
882
			else
883
			{
884
				$log_action_links = 'MOVE_LINKS';
885
886
				$sql = 'SELECT cat_name
887
					FROM ' . $this->categories_table . '
888
					WHERE cat_id = ' . (int) $links_to_id;
889
				$result = $this->db->sql_query($sql);
890
				$row = $this->db->sql_fetchrow($result);
891
				$this->db->sql_freeresult($result);
892
893
				if (!$row)
894
				{
895
					throw new \OutOfBoundsException('DIR_NO_CAT');
896
				}
897
				else
898
				{
899
					$links_to_name = $row['cat_name'];
900
					$this->_move_cat_content($this->cat_id, $links_to_id);
901
				}
902
			}
903
		}
904
905
		if (sizeof($this->errors))
906
		{
907
			return $this->errors;
908
		}
909
910
		if ($action_subcats == 'delete')
911
		{
912
			$log_action_cats = 'CATS';
913
		}
914
		else if ($action_subcats == 'move')
915
		{
916
			if (!$subcats_to_id)
917
			{
918
				$this->errors[] = $this->language->lang('DIR_NO_DESTINATION_CAT');
919
			}
920
			else
921
			{
922
				$log_action_cats = 'MOVE_CATS';
923
924
				$subcats_to_name = $row['cat_name'];
925
				$this->nestedset_category->move_children($this->cat_id, $subcats_to_id);
926
			}
927
		}
928
929
		if (sizeof($this->errors))
930
		{
931
			return $this->errors;
932
		}
933
934
		$this->nestedset_category->delete($this->cat_id);
935
936
		$log_action = implode('_', array($log_action_links, $log_action_cats));
937
938
		switch ($log_action)
939
		{
940 View Code Duplication
			case 'MOVE_LINKS_MOVE_CATS':
941
				$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_CAT_DEL_MOVE_LINKS_MOVE_CATS', time(), array($links_to_name, $subcats_to_name, $this->cat_data['cat_name']));
942
			break;
943
944 View Code Duplication
			case 'MOVE_LINKS_CATS':
945
				$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_CAT_DEL_MOVE_LINKS_CATS', time(), array($links_to_name, $this->cat_data['cat_name']));
946
			break;
947
948 View Code Duplication
			case 'LINKS_MOVE_CATS':
949
				$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_CAT_DEL_LINKS_MOVE_CATS', time(), array($subcats_to_name, $this->cat_data['cat_name']));
950
			break;
951
952 View Code Duplication
			case '_MOVE_CATS':
953
				$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_CAT_DEL_MOVE_CATS', time(), array($subcats_to_name, $this->cat_data['cat_name']));
954
			break;
955
956 View Code Duplication
			case 'MOVE_LINKS_':
957
				$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_CAT_DEL_MOVE_LINKS', time(), array($links_to_name, $this->cat_data['cat_name']));
958
			break;
959
960 View Code Duplication
			case 'LINKS_CATS':
961
				$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_CAT_DEL_LINKS_CATS', time(), array($this->cat_data['cat_name']));
962
			break;
963
964 View Code Duplication
			case '_CATS':
965
				$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_CAT_DEL_CATS', time(), array($this->cat_data['cat_name']));
966
			break;
967
968 View Code Duplication
			case 'LINKS_':
969
				$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_CAT_DEL_LINKS', time(), array($this->cat_data['cat_name']));
970
			break;
971
972 View Code Duplication
			default:
973
				$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_CAT_DEL_CAT', time(), array($this->cat_data['cat_name']));
974
			break;
975
		}
976
977
		return $this->errors;
978
	}
979
980
	/**
981
	* Move category content from one to another forum
982
	*
983
	* @param	int	$from_id
984
	* @param	int	$to_id
985
	* @return	null
986
	*/
987
	private function _move_cat_content($from_id, $to_id)
988
	{
989
		$sql = 'UPDATE ' . $this->links_table . '
990
			SET link_cat = ' . (int) $to_id . '
991
			WHERE link_cat = ' . (int) $from_id;
992
		$this->db->sql_query($sql);
993
994
		$sql = 'DELETE FROM ' . $this->watch_table . '
995
			WHERE cat_id = ' . (int) $from_id;
996
		$this->db->sql_query($sql);
997
998
		$this->_sync_dir_cat($to_id);
999
	}
1000
1001
	/**
1002
	* Delete category content
1003
	*
1004
	* @return array
1005
	*/
1006
	private function _delete_cat_content()
1007
	{
1008
		$this->db->sql_transaction('begin');
1009
1010
		// Before we remove anything we make sure we are able to adjust the post counts later. ;)
1011
		$sql = 'SELECT link_id, link_banner
1012
			FROM ' . $this->links_table . '
1013
			WHERE link_cat = ' . (int) $this->cat_id;
1014
		$result = $this->db->sql_query($sql);
1015
1016
		$link_ids = array();
1017
		while ($row = $this->db->sql_fetchrow($result))
1018
		{
1019
			$link_ids[] = $row['link_id'];
1020
1021 View Code Duplication
			if ($row['link_banner'] && !preg_match('/^(http:\/\/|https:\/\/|ftp:\/\/|ftps:\/\/|www\.).+/si', $row['link_banner']))
1022
			{
1023
				$banner_img = $this->get_banner_path(basename($row['link_banner']));
1024
1025
				if (file_exists($banner_img))
1026
				{
1027
					@unlink($banner_img);
1028
				}
1029
			}
1030
		}
1031
		$this->db->sql_freeresult($result);
1032
1033
		if (sizeof($link_ids))
1034
		{
1035
			// Delete links datas
1036
			$link_datas_ary = array(
1037
				$this->comments_table	=> 'comment_link_id',
1038
				$this->votes_table		=> 'vote_link_id',
1039
			);
1040
1041
			foreach ($link_datas_ary as $table => $field)
1042
			{
1043
				$this->db->sql_query("DELETE FROM $table WHERE " . $this->db->sql_in_set($field, $link_ids));
1044
			}
1045
		}
1046
1047
		// Delete cats datas
1048
		$cat_datas_ary = array(
1049
			$this->links_table	=> 'link_cat',
1050
			$this->watch_table	=> 'cat_id',
1051
		);
1052
1053
		foreach ($cat_datas_ary as $table => $field)
1054
		{
1055
			$this->db->sql_query("DELETE FROM $table WHERE $field = " . (int) $this->cat_id);
1056
		}
1057
1058
		$this->db->sql_transaction('commit');
1059
1060
		return array();
1061
	}
1062
1063
	/**
1064
	* Update links counter
1065
	*
1066
	* @param	int $cat_id	The category ID
1067
	* @return	null
1068
	*/
1069
	private function _sync_dir_cat($cat_id)
1070
	{
1071
		$sql = 'SELECT COUNT(link_id) AS num_links
1072
			FROM ' . $this->links_table . '
1073
			WHERE link_cat = ' . (int) $cat_id . '
1074
				AND link_active = 1';
1075
		$result = $this->db->sql_query($sql);
1076
		$total_links = (int) $this->db->sql_fetchfield('num_links');
1077
		$this->db->sql_freeresult($result);
1078
1079
		$sql = 'UPDATE ' . $this->categories_table . '
1080
			SET cat_links = ' . $total_links . '
1081
			WHERE cat_id = ' . (int) $cat_id;
1082
		$this->db->sql_query($sql);
1083
	}
1084
1085
	/**
1086
	* Update link data (note, vote, comment)
1087
	*
1088
	* @param	int	$start
1089
	* @param	int	$stop
1090
	* @return	null
1091
	*/
1092
	private function _sync_dir_links($start, $stop)
1093
	{
1094
		$sql_ary = array(
1095
			'link_comment'	=> 0,
1096
			'link_note'		=> 0,
1097
			'link_vote'		=> 0,
1098
		);
1099
1100
		$sql = 'UPDATE ' . $this->links_table . '
1101
			SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . '
1102
			WHERE link_id BETWEEN ' . (int) $start . ' AND ' . (int) $stop;
1103
		$this->db->sql_query($sql);
1104
1105
		$sql = 'SELECT vote_link_id, COUNT(vote_note) AS nb_vote, SUM(vote_note) AS total FROM ' . $this->votes_table . '
1106
			WHERE vote_link_id BETWEEN ' . (int) $start . ' AND ' . (int) $stop . '
1107
			GROUP BY vote_link_id';
1108
		$result = $this->db->sql_query($sql);
1109
		while ($tmp = $this->db->sql_fetchrow($result))
1110
		{
1111
			$sql = 'UPDATE ' . $this->links_table . '
1112
				SET link_note = ' . (int) $tmp['total'] . ', link_vote = ' . (int) $tmp['nb_vote'] . '
1113
				WHERE link_id = ' . (int) $tmp['vote_link_id'];
1114
			$this->db->sql_query($sql);
1115
		}
1116
		$this->db->sql_freeresult($result);
1117
1118
		$sql = 'SELECT 	comment_link_id, COUNT(comment_id) AS nb_comment
1119
			FROM ' . $this->comments_table . '
1120
			WHERE comment_link_id BETWEEN ' . (int) $start . ' AND ' . (int) $stop . '
1121
			GROUP BY comment_link_id';
1122
		$result = $this->db->sql_query($sql);
1123
		while ($tmp = $this->db->sql_fetchrow($result))
1124
		{
1125
			$sql = 'UPDATE ' . $this->links_table . '
1126
				SET link_comment = ' . (int) $tmp['nb_comment'] . '
1127
				WHERE link_id = ' . (int) $tmp['comment_link_id'];
1128
			$this->db->sql_query($sql);
1129
		}
1130
		$this->db->sql_freeresult($result);
1131
	}
1132
1133
	/**
1134
	* Display icons drop-down list
1135
	*
1136
	* @param	string	$icons_path
1137
	* @param	string	$img_selected
1138
	* @return	string
1139
	*/
1140
	private function _get_dir_icon_list($icons_path, $img_selected)
1141
	{
1142
		$imglist = filelist($icons_path, '');
1143
		$filename_list = '<option value="">----------</option>';
1144
1145
		foreach ($imglist as $path => $img_ary)
1146
		{
1147
			sort($img_ary);
1148
1149
			foreach ($img_ary as $img)
1150
			{
1151
				$img = $path . $img;
1152
				$selected = '';
1153
1154
				if (strlen($img) > 255)
1155
				{
1156
					continue;
1157
				}
1158
1159
				if ($img == $img_selected)
1160
				{
1161
					$selected = ' selected="selected"';
1162
				}
1163
1164
				$filename_list .= '<option value="' . htmlspecialchars($img) . '"' . $selected . '>' . $img . '</option>';
1165
			}
1166
		}
1167
1168
		return ($filename_list);
1169
	}
1170
}
1171