Passed
Pull Request — master (#1)
by Dark❶
03:30
created

acp_controller::mode_cron()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 17
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 32
rs 9.7
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\config\config;
17
use phpbb\db\driver\driver_interface;
18
use phpbb\template\template;
19
use phpbb\user;
20
use phpbb\language\language;
21
use phpbb\log\log;
22
use phpbb\request\request;
23
use phpbb\cron\manager as cron_manager;
24
25
/**
26
 * Reduce Search Index [RSI] ACP controller.
27
 */
28
class acp_controller
29
{
30
	/** @var \phpbb\config\config */
31
	protected $config;
32
33
	/** @var \phpbb\language\language */
34
	protected $language;
35
36
	/** @var \phpbb\log\log */
37
	protected $log;
38
39
	/** @var \phpbb\request\request */
40
	protected $request;
41
42
	/** @var \phpbb\template\template */
43
	protected $template;
44
45
	/** @var \phpbb\user */
46
	protected $user;
47
48
	/** @var \phpbb\db\driver\driver_interface */
49
	protected $db;
50
51
	/** @var \phpbb\cron\manager */
52
	protected $cron_manager;
53
54
	/** @var string The module ID */
55
	protected $id;
56
57
	/** @var string The module mode */
58
	protected $mode;
59
60
	/** @var string Custom form action */
61
	protected $u_action;
62
63
	/**
64
	 * Constructor.
65
	 *
66
	 * @param \phpbb\config\config					$config			Config object
67
	 * @param \phpbb\language\language				$language		Language object
68
	 * @param \phpbb\log\log						$log			Log object
69
	 * @param \phpbb\request\request				$request		Request object
70
	 * @param \phpbb\template\template				$template		Template object
71
	 * @param \phpbb\user							$user			User object
72
	 * @param \phpbb\db\driver\driver_interface		$db				Database object
73
	 * @param \phpbb\cron\manager					$cron_manager	Cron manager
74
	 */
75
	public function __construct(config $config, language $language, log $log, request $request, template $template, user $user, driver_interface $db, cron_manager $cron_manager)
76
	{
77
		$this->config		= $config;
78
		$this->language		= $language;
79
		$this->log			= $log;
80
		$this->request		= $request;
81
		$this->template		= $template;
82
		$this->user			= $user;
83
		$this->db			= $db;
84
		$this->cron_manager	= $cron_manager;
85
	}
86
87
	/**
88
	 * Set Data form.
89
	 *
90
	 * @param int		$id			The module ID
91
	 * @param string	$mode		The module mode
92
	 * @param string	$u_action	Custom form action
93
	 *
94
	 * @return void
95
	 */
96
	public function set_data($id, $mode, $u_action)
97
	{
98
		$this->id = $id;
99
		$this->mode = strtolower($mode);
100
		$this->u_action = $u_action;
101
	}
102
103
	/**
104
	 * Get Data form.
105
	 *
106
	 * @return array Having keys 'tpl_name' & 'page_title'
107
	 */
108
	public function get_data()
109
	{
110
		return [
111
			'tpl_name' => 'dark1_rsi_acp_' . $this->mode,
112
			'page_title' => $this->language->lang('ACP_RSI_TITLE') . ' - ' . $this->language->lang('ACP_RSI_' . strtoupper($this->mode)),
113
		];
114
	}
115
116
	/**
117
	 * Set Display form.
118
	 *
119
	 * @return void
120
	 */
121
	public function display()
122
	{
123
		$ext_name_rsi = 'Reduce Search Index [RSI]';
124
		$ext_by_dark1 = 'Dark❶ [dark1]';
125
126
		// Add our common language file
127
		$this->language->add_lang('lang_rsi', 'dark1/reducesearchindex');
128
129
		// Create a form key for preventing CSRF attacks
130
		add_form_key('dark1_rsi_acp_' . $this->mode);
131
132
		// Set u_action in the template
133
		$this->template->assign_vars([
134
			'U_ACTION'		=> $this->u_action,
135
			'RSI_MODE'		=> $this->mode,
136
			'RSI_EXT_MODE'	=> $this->language->lang('ACP_RSI_' . strtoupper($this->mode)),
137
			'RSI_EXT_NAME'	=> $ext_name_rsi,
138
			'RSI_EXT_DEV'	=> $ext_by_dark1,
139
		]);
140
141
		$mode_display = 'mode_' . $this->mode;
142
143
		if (!method_exists($this, $mode_display))
144
		{
145
			trigger_error('FORM_INVALID', E_USER_WARNING);
146
		}
147
148
		// Trigger the Mode
149
		$this->$mode_display();
150
	}
151
152
	/**
153
	 * Check Form On Submit .
154
	 *
155
	 * @return void
156
	 */
157
	private function check_form_on_submit()
158
	{
159
		// Test if the submitted form is valid
160
		if (!check_form_key('dark1_rsi_acp_' . $this->mode))
161
		{
162
			trigger_error('FORM_INVALID', E_USER_WARNING);
163
		}
164
	}
165
166
	/**
167
	 * Success Form On Submit.
168
	 * Used to Log & Trigger Success Err0r.
169
	 *
170
	 * @return void
171
	 */
172
	private function success_form_on_submit()
173
	{
174
		// Add option settings change action to the admin log
175
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_RSI_LOG_SET_SAV', time(), array($this->language->lang('ACP_RSI_' . strtoupper($this->mode))));
176
177
		// Option settings have been updated and logged
178
		// Confirm this to the user and provide link back to previous page
179
		trigger_error($this->language->lang('ACP_RSI_LOG_SET_SAV', $this->language->lang('ACP_RSI_' . strtoupper($this->mode))) . adm_back_link($this->u_action), E_USER_NOTICE);
180
	}
181
182
	/**
183
	 * Display the options a user can configure for Main Mode.
184
	 *
185
	 * @return void
186
	 */
187
	private function mode_main()
188
	{
189
		// Is the form being submitted to us?
190
		if ($this->request->is_set_post('submit'))
191
		{
192
			$this->check_form_on_submit();
193
194
			// Set the options the user configured
195
			$this->config->set('dark1_rsi_enable', $this->request->variable('dark1_rsi_enable', 0));
196
			$this->config->set('dark1_rsi_time', strtotime($this->request->variable('dark1_rsi_time', '0', true)));
197
			$this->config->set('dark1_rsi_interval', ($this->request->variable('dark1_rsi_interval', 0)) * 86400);
198
199
			$this->success_form_on_submit();
200
		}
201
202
		// Set output variables for display in the template
203
		$this->template->assign_vars([
204
			'RSI_ENABLE'		=> $this->config['dark1_rsi_enable'],
205
			'RSI_INTERVAL'		=> ($this->config['dark1_rsi_interval'] / 86400),
206
			'RSI_TIME'			=> $this->user->format_date($this->config['dark1_rsi_time'], 'Y-m-d h:i:s A P', true),
207
			'RSI_CURR_TIME'		=> $this->user->format_date(time(), 'Y-m-d h:i:s A P', true),
208
		]);
209
	}
210
211
	/**
212
	 * Display the options a user can configure for Forum Mode.
213
	 *
214
	 * @return void
215
	 */
216
	private function mode_forum()
217
	{
218
		// Is the form being submitted to us?
219
		if ($this->request->is_set_post('submit'))
220
		{
221
			$this->check_form_on_submit();
222
223
			// Set the options the user configured
224
			$sql = 'SELECT forum_id, forum_type FROM ' . FORUMS_TABLE . ' ORDER BY left_id ASC';
225
			$result = $this->db->sql_query($sql);
226
			$forum_id_set = array();
227
			while ($row = $this->db->sql_fetchrow($result))
228
			{
229
				if ($row['forum_type'] == FORUM_POST)
230
				{
231
					$forum_id_set[$row['forum_id']] =  $this->request->variable('forum_' . $row['forum_id'] . '_enable', 0);
232
				}
233
			}
234
			$this->db->sql_freeresult($result);
235
			foreach ($forum_id_set as $id => $input)
236
			{
237
				$sql = 'UPDATE ' . FORUMS_TABLE . ' SET dark1_rsi_f_enable = ' . (int) $input . ' WHERE forum_id = ' . (int) $id;
238
				$this->db->sql_query($sql);
239
			}
240
241
			$this->success_form_on_submit();
242
		}
243
244
		// Set output variables for display in the template
245
		$forum_rows = $this->print_forums();
246
		foreach ($forum_rows as $key => $tpl_row)
247
		{
248
			$this->template->assign_block_vars('forumrow', $tpl_row);
249
		}
250
	}
251
252
	/**
253
	 * Display the Forum options.
254
	 *
255
	 * @return array
256
	 */
257
	private function print_forums()
258
	{
259
		$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';
260
		$result = $this->db->sql_query($sql);
261
262
		$right = 0;
263
		$padding_store = array('0' => '');
264
		$padding = '';
265
		$forum_rows = array();
266
267
		while ($row = $this->db->sql_fetchrow($result))
268
		{
269
			if ($row['left_id'] < $right)
270
			{
271
				$padding .= '&nbsp; &nbsp; &nbsp;';
272
				$padding_store[$row['parent_id']] = $padding;
273
			}
274
			else if ($row['left_id'] > $right + 1)
275
			{
276
				$padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
277
			}
278
			$right = $row['right_id'];
279
280
			// Category forums are displayed for organizational purposes, but have no configuration
281
			if ($row['forum_type'] == FORUM_CAT)
282
			{
283
				$tpl_row = array(
284
					'S_IS_CAT'		=> true,
285
					'FORUM_NAME'	=> $padding . '&nbsp; &#8627; &nbsp;' . $row['forum_name'],
286
				);
287
				$forum_rows[] = $tpl_row;
288
			}
289
			// Normal forums have a radio input with the value selected based on the value of the discord_notifications_enabled setting
290
			else if ($row['forum_type'] == FORUM_POST)
291
			{
292
				// The labels for all the inputs are constructed based on the forum IDs to make it easy to know which
293
				$tpl_row = array(
294
					'S_IS_CAT'		=> false,
295
					'FORUM_NAME'	=> $padding . '&nbsp; &#8627; &nbsp;' . $row['forum_name'],
296
					'FORUM_ID'		=> $row['forum_id'],
297
					'ENABLE'		=> $row['dark1_rsi_f_enable'],
298
				);
299
				$forum_rows[] = $tpl_row;
300
			}
301
			// Other forum types (links) are ignored
302
		}
303
		$this->db->sql_freeresult($result);
304
305
		return $forum_rows;
306
	}
307
308
	/**
309
	 * Display the options a user can configure for Cron Mode.
310
	 *
311
	 * @return void
312
	 */
313
	private function mode_cron()
314
	{
315
		// Is the form being submitted to us?
316
		if ($this->request->is_set_post('submit'))
317
		{
318
			$this->check_form_on_submit();
319
320
			// Set the options the user configured
321
			$this->config->set('dark1_rsi_auto_reduce_sync_enable', $this->request->variable('dark1_rsi_auto_reduce_sync_enable', 0));
322
			$this->config->set('dark1_rsi_auto_reduce_sync_gc', ($this->request->variable('dark1_rsi_auto_reduce_sync_gc', 0)) * 86400);
323
			$this->config->set('dark1_rsi_auto_reduce_sync_last_gc', strtotime($this->request->variable('dark1_rsi_auto_reduce_sync_last_gc', '0', true)), false);
324
325
			$this->success_form_on_submit();
326
		}
327
328
		// Run Cron Task
329
		if ($this->request->is_set_post('runcrontask'))
330
		{
331
			$this->check_form_on_submit();
332
333
			$cron_task = $this->cron_manager->find_task('dark1.reducesearchindex.cron.auto_reduce_sync');
334
			$cron_task->run();
335
			$this->template->assign_var('DONE_RUN_LS_CRON', (string) true);
336
		}
337
338
		// Set output variables for display in the template
339
		$this->template->assign_vars([
340
			'ENABLE_CRON'		=> $this->config['dark1_rsi_auto_reduce_sync_enable'],
341
			'CRON_INTERVAL'		=> ($this->config['dark1_rsi_auto_reduce_sync_gc'] / 86400),
342
			'CRON_LAST_RUN'		=> $this->user->format_date($this->config['dark1_rsi_auto_reduce_sync_last_gc'], 'Y-m-d h:i:s A P', true),
343
			'CRON_NEXT_RUN'		=> $this->user->format_date($this->config['dark1_rsi_auto_reduce_sync_last_gc'] + $this->config['dark1_rsi_auto_reduce_sync_gc'], 'Y-m-d h:i:s A P', true),
344
			'CRON_PREV_RUN'		=> $this->user->format_date($this->config['dark1_rsi_auto_reduce_sync_last_gc'] - $this->config['dark1_rsi_auto_reduce_sync_gc'], 'Y-m-d h:i:s A P', true),
345
		]);
346
	}
347
}
348