acp_prune::handle()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 27
c 3
b 0
f 0
dl 0
loc 45
rs 9.488
cc 3
nc 4
nop 0
1
<?php
2
/**
3
 *
4
 * User Notification Control [UNC]. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2020-forever, Dark❶, https://dark1.tech
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace dark1\usernotificationcontrol\controller;
12
13
/**
14
 * @ignore
15
 */
16
use phpbb\language\language;
17
use phpbb\log\log;
18
use phpbb\request\request;
19
use phpbb\template\template;
20
use phpbb\user;
21
use phpbb\config\config;
22
use phpbb\cron\manager as cron_manager;
23
use phpbb\db\driver\driver_interface as db_driver;
24
25
/**
26
 * User Notification Control [UNC] ACP controller Prune.
27
 */
28
class acp_prune extends acp_base
29
{
30
	/** @var language */
31
	protected $language;
32
33
	/** @var config */
34
	protected $config;
35
36
	/** @var cron_manager */
37
	protected $cron_manager;
38
39
	/** @var db_driver */
40
	protected $db;
41
42
	/** @var string phpBB root path */
43
	protected $phpbb_root_path;
44
45
	/** @var string phpBB adm relative path */
46
	protected $phpbb_adm_relative_path;
47
48
	/** @var string phpBB php ext */
49
	protected $php_ext;
50
51
	/** Time Format */
52
	const TIME_FORMAT	= 'Y-m-d h:i:s A P';
53
54
	/**
55
	 * Constructor.
56
	 *
57
	 * @param language			$language					Language object
58
	 * @param log				$log						Log object
59
	 * @param request			$request					Request object
60
	 * @param template			$template					Template object
61
	 * @param user				$user						User object
62
	 * @param config			$config						Config object
63
	 * @param cron_manager		$cron_manager				Cron manager
64
	 * @param db_driver			$db							Database object
65
	 * @param string			$phpbb_root_path			phpBB root path
66
	 * @param string			$phpbb_adm_relative_path	phpBB adm relative path
67
	 * @param string			$php_ext					phpBB php ext
68
	 */
69
	public function __construct(language $language, log $log, request $request, template $template, user $user, config $config, cron_manager $cron_manager, db_driver $db, $phpbb_root_path, $phpbb_adm_relative_path, $php_ext)
70
	{
71
		parent::__construct($language, $log, $request, $template, $user);
72
73
		$this->language					= $language;
74
		$this->config					= $config;
75
		$this->cron_manager				= $cron_manager;
76
		$this->db						= $db;
77
		$this->phpbb_root_path			= $phpbb_root_path;
78
		$this->phpbb_adm_relative_path	= $phpbb_adm_relative_path;
79
		$this->php_ext					= $php_ext;
80
	}
81
82
	/**
83
	 * Display the options a user can configure for Cron Mode.
84
	 *
85
	 * @return void
86
	 * @access public
87
	 */
88
	public function handle()
89
	{
90
		// Is the form being submitted to us?
91
		if ($this->request->is_set_post('submit'))
92
		{
93
			$this->check_form_on_submit();
94
95
			// Set the options the user configured
96
			$this->config->set('dark1_unc_all_notify_expire_days', $this->request->variable('dark1_unc_all_notify_expire_days', 0));
97
			$this->config->set('dark1_unc_auto_prune_notify_enable', $this->request->variable('dark1_unc_auto_prune_notify_enable', 0));
98
			$this->config->set('dark1_unc_auto_prune_notify_gc', ($this->request->variable('dark1_unc_auto_prune_notify_gc', 0)) * 86400);
99
100
			$this->success_form_on_submit();
101
		}
102
103
		// Run Cron Task
104
		if ($this->request->is_set_post('runcrontask'))
105
		{
106
			$this->check_form_on_submit();
107
108
			$cron_task = $this->cron_manager->find_task('dark1.usernotificationcontrol.cron.auto_prune_notify');
109
			$cron_task->run();
110
111
			$this->success_form_on_submit('ACP_UNC_LOG_CRON', '');
112
		}
113
114
		$this->language->add_lang('acp/board');
115
		$main_adm_path = $this->phpbb_root_path . $this->phpbb_adm_relative_path . 'index.' . $this->php_ext;
116
		$read_expire_link = append_sid($main_adm_path, 'i=acp_board&amp;mode=load').'#read_notification_expire_days';
117
118
		$curr_time = time();
119
		$days = $this->config['read_notification_expire_days'] + $this->config['dark1_unc_all_notify_expire_days'];
120
		$this->disp_stats_tbl($curr_time - ($days * 86400));
121
122
		// Set output variables for display in the template
123
		$this->template->assign_vars([
124
			'UNC_READ_EXPIRE'		=> $this->config['read_notification_expire_days'],
125
			'UNC_READ_EXPIRE_LINK'	=> $read_expire_link,
126
			'UNC_ALL_EXPIRE'		=> $this->config['dark1_unc_all_notify_expire_days'],
127
			'UNC_TOTAL_EXPIRE'		=> $days,
128
			'UNC_CURRENT_TIME'		=> $this->user->format_date($curr_time, self::TIME_FORMAT, true),
129
			'UNC_ENABLE_CRON'		=> $this->config['dark1_unc_auto_prune_notify_enable'],
130
			'UNC_CRON_INTERVAL'		=> ($this->config['dark1_unc_auto_prune_notify_gc'] / 86400),
131
			'UNC_CRON_LAST_RUN'		=> $this->user->format_date($this->config['dark1_unc_auto_prune_notify_last_gc'], self::TIME_FORMAT, true),
132
			'UNC_CRON_NEXT_RUN'		=> $this->user->format_date($this->config['dark1_unc_auto_prune_notify_last_gc'] + $this->config['dark1_unc_auto_prune_notify_gc'], self::TIME_FORMAT, true),
133
		]);
134
	}
135
136
	/**
137
	 * Display the Stats Table.
138
	 *
139
	 * @param int	$timestamp	Timestamp
140
	 * @return void
141
	 * @access private
142
	 */
143
	private function disp_stats_tbl($timestamp)
144
	{
145
		foreach (['all' => '> 0', 'exp' => '< '.$timestamp, 'rem' => '>= '.$timestamp] as $key => $value)
146
		{
147
			$sql = 'SELECT notification_read, COUNT(*) AS count' .
148
					' FROM ' . NOTIFICATIONS_TABLE .
149
					' WHERE notification_time ' . (string) $value .
150
					' GROUP BY notification_read';
151
			$result = $this->db->sql_query($sql);
152
			$rows = array_column($this->db->sql_fetchrowset($result), 'count', 'notification_read');
153
			$this->db->sql_freeresult($result);
154
155
			$this->template->assign_block_vars('stats', [
156
				'TYPE'		=> $this->language->lang('ACP_UNC_STAT_' . strtoupper($key)),
157
				'UNREAD'	=> (int) (isset($rows[0]) ? $rows[0] : 0),
158
				'READ'		=> (int) (isset($rows[1]) ? $rows[1] : 0),
159
			]);
160
		}
161
	}
162
}
163