Passed
Push — master ( 146fdc...6e4ec6 )
by Dark❶
02:30
created

acp_forum::print_forums()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 25
c 1
b 0
f 0
nc 13
nop 0
dl 0
loc 46
rs 8.5866
1
<?php
2
/**
3
 *
4
 * Reduce Search Index [RSI]. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2020, Dark❶, https://dark1.tech
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace dark1\reducesearchindex\controller;
12
13
/**
14
 * @ignore
15
 */
16
use phpbb\db\driver\driver_interface;
17
use phpbb\template\template;
18
use phpbb\user;
19
use phpbb\language\language;
20
use phpbb\log\log;
21
use phpbb\request\request;
22
23
/**
24
 * Reduce Search Index [RSI] ACP controller Forum.
25
 */
26
class acp_forum extends acp_base
27
{
28
	/** @var \phpbb\db\driver\driver_interface */
29
	protected $db;
30
31
	/**
32
	 * Constructor.
33
	 *
34
	 * @param \phpbb\config\config					$config			Config object
35
	 * @param \phpbb\language\language				$language		Language object
36
	 * @param \phpbb\log\log						$log			Log object
37
	 * @param \phpbb\request\request				$request		Request object
38
	 * @param \phpbb\template\template				$template		Template object
39
	 * @param \phpbb\user							$user			User object
40
	 * @param \phpbb\db\driver\driver_interface		$db				Database object
41
	 */
42
	public function __construct(language $language, log $log, request $request, template $template, user $user, driver_interface $db)
43
	{
44
		parent::__construct($language, $log, $request, $template, $user);
45
46
		$this->db	= $db;
47
	}
48
49
	/**
50
	 * Display the options a user can configure for Forum Mode.
51
	 *
52
	 * @return void
53
	 */
54
	public function handle()
55
	{
56
		// Is the form being submitted to us?
57
		if ($this->request->is_set_post('submit'))
58
		{
59
			$this->check_form_on_submit();
60
			$this->submit_forums();
61
			$this->success_form_on_submit();
62
		}
63
64
		// Set output variables for display in the template
65
		$this->print_forums();
66
	}
67
68
	/**
69
	 * Display the Forum options.
70
	 *
71
	 * @return void
72
	 */
73
	private function print_forums()
74
	{
75
		$sql = 'SELECT forum_id, forum_type, forum_name, parent_id, left_id, right_id, dark1_rsi_f_enable FROM ' . FORUMS_TABLE . ' ORDER BY left_id ASC';
76
		$result = $this->db->sql_query($sql);
77
78
		$right = 0;
79
		$padding_store = array('0' => '');
80
		$padding = '';
81
82
		while ($row = $this->db->sql_fetchrow($result))
83
		{
84
			if ($row['left_id'] < $right)
85
			{
86
				$padding .= '&nbsp; &nbsp; &nbsp;';
87
				$padding_store[$row['parent_id']] = $padding;
88
			}
89
			else if ($row['left_id'] > $right + 1)
90
			{
91
				$padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
92
			}
93
			$right = $row['right_id'];
94
95
			// Category forums are displayed for organizational purposes, but have no configuration
96
			if ($row['forum_type'] == FORUM_CAT)
97
			{
98
				$tpl_row = array(
99
					'S_IS_CAT'		=> true,
100
					'FORUM_NAME'	=> $padding . '&nbsp; &#8627; &nbsp;' . $row['forum_name'],
101
				);
102
				$this->template->assign_block_vars('forumrow', $tpl_row);
103
			}
104
			// Normal forums have a radio input with the value selected based on the value of the setting
105
			else if ($row['forum_type'] == FORUM_POST)
106
			{
107
				// The labels for all the inputs are constructed based on the forum IDs to make it easy to know which
108
				$tpl_row = array(
109
					'S_IS_CAT'		=> false,
110
					'FORUM_NAME'	=> $padding . '&nbsp; &#8627; &nbsp;' . $row['forum_name'],
111
					'FORUM_ID'		=> $row['forum_id'],
112
					'ENABLE'		=> $row['dark1_rsi_f_enable'],
113
				);
114
				$this->template->assign_block_vars('forumrow', $tpl_row);
115
			}
116
			// Other forum types (links) are ignored
117
		}
118
		$this->db->sql_freeresult($result);
119
	}
120
121
	/**
122
	 * Submit the Forum options.
123
	 *
124
	 * @return void
125
	 */
126
	private function submit_forums()
127
	{
128
		// Set the options the user configured
129
		$sql = 'SELECT forum_id, forum_type FROM ' . FORUMS_TABLE . ' ORDER BY left_id ASC';
130
		$result = $this->db->sql_query($sql);
131
		$forum_id_set = array();
132
		while ($row = $this->db->sql_fetchrow($result))
133
		{
134
			if ($row['forum_type'] == FORUM_POST)
135
			{
136
				$forum_id_set[$row['forum_id']] =  $this->request->variable('forum_' . $row['forum_id'] . '_enable', 0);
137
			}
138
		}
139
		$this->db->sql_freeresult($result);
140
		foreach ($forum_id_set as $id => $input)
141
		{
142
			$sql = 'UPDATE ' . FORUMS_TABLE . ' SET dark1_rsi_f_enable = ' . (int) $input . ' WHERE forum_id = ' . (int) $id;
143
			$this->db->sql_query($sql);
144
		}
145
	}
146
}
147