acp_base   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 136
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A get_data() 0 5 1
A set_data() 0 5 1
A setup() 0 17 1
A check_form_on_submit() 0 6 2
A success_form_on_submit() 0 10 2
1
<?php
2
/**
3
 *
4
 * Reduce Search Index [RSI]. 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\reducesearchindex\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
22
/**
23
 * Reduce Search Index [RSI] ACP controller Base.
24
 */
25
class acp_base
26
{
27
	/** @var language */
28
	protected $language;
29
30
	/** @var log */
31
	protected $log;
32
33
	/** @var request */
34
	protected $request;
35
36
	/** @var template */
37
	protected $template;
38
39
	/** @var user */
40
	protected $user;
41
42
	/** @var string The module ID */
43
	protected $id;
44
45
	/** @var string The module mode */
46
	protected $mode;
47
48
	/** @var string Custom form action */
49
	protected $u_action;
50
51
	/**
52
	 * Constructor.
53
	 *
54
	 * @param language		$language		Language object
55
	 * @param log			$log			Log object
56
	 * @param request		$request		Request object
57
	 * @param template		$template		Template object
58
	 * @param user			$user			User object
59
	 */
60
	public function __construct(language $language, log $log, request $request, template $template, user $user)
61
	{
62
		$this->language		= $language;
63
		$this->log			= $log;
64
		$this->request		= $request;
65
		$this->template		= $template;
66
		$this->user			= $user;
67
	}
68
69
	/**
70
	 * Set Data form.
71
	 *
72
	 * @param int		$id			The module ID
73
	 * @param string	$mode		The module mode
74
	 * @param string	$u_action	Custom form action
75
	 *
76
	 * @return void
77
	 * @access public
78
	 */
79
	public function set_data($id, $mode, $u_action)
80
	{
81
		$this->id = $id;
82
		$this->mode = $mode;
83
		$this->u_action = $u_action;
84
	}
85
86
	/**
87
	 * Get Data form.
88
	 *
89
	 * @return array Having keys 'tpl_name' & 'page_title'
90
	 * @access public
91
	 */
92
	public function get_data()
93
	{
94
		return [
95
			'tpl_name' => 'dark1_rsi_acp_' . $this->mode,
96
			'page_title' => $this->language->lang('ACP_RSI_TITLE') . ' - ' . $this->language->lang('ACP_RSI_' . strtoupper($this->mode)),
97
		];
98
	}
99
100
	/**
101
	 * Set Display form.
102
	 *
103
	 * @return void
104
	 * @access public
105
	 */
106
	public function setup()
107
	{
108
		$ext_name_rsi = 'Reduce Search Index [RSI]';
109
		$ext_by_dark1 = 'Dark❶ [dark1]';
110
111
		// Add our common language file
112
		$this->language->add_lang('lang_acp_rsi', 'dark1/reducesearchindex');
113
114
		// Create a form key for preventing CSRF attacks
115
		add_form_key('dark1_rsi_acp_' . $this->mode);
116
117
		// Set u_action in the template
118
		$this->template->assign_vars([
119
			'U_ACTION'		=> $this->u_action,
120
			'RSI_EXT_MODE'	=> $this->language->lang('ACP_RSI_' . strtoupper($this->mode)),
121
			'RSI_EXT_NAME'	=> $ext_name_rsi,
122
			'RSI_EXT_DEV'	=> $ext_by_dark1,
123
		]);
124
	}
125
126
	/**
127
	 * Check Form On Submit .
128
	 *
129
	 * @return void
130
	 * @access protected
131
	 */
132
	protected function check_form_on_submit()
133
	{
134
		// Test if the submitted form is valid
135
		if (!check_form_key('dark1_rsi_acp_' . $this->mode))
136
		{
137
			trigger_error('FORM_INVALID', E_USER_WARNING);
138
		}
139
	}
140
141
	/**
142
	 * Success Form On Submit.
143
	 * Used to Log & Trigger Success Err0r.
144
	 *
145
	 * @param string	$lang_key	Lang Key
146
	 * @param string	$lang_str	Lang String
147
	 *
148
	 * @return void
149
	 * @access protected
150
	 */
151
	protected function success_form_on_submit($lang_key = 'ACP_RSI_LOG_SET_SAV', $lang_str = null)
152
	{
153
		$lang_str = !isset($lang_str) ? $this->language->lang('ACP_RSI_' . strtoupper($this->mode)) : $lang_str;
154
155
		// Add option settings change action to the admin log
156
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $lang_key, time(), [$lang_str]);
157
158
		// Option settings have been updated and logged
159
		// Confirm this to the user and provide link back to previous page
160
		trigger_error($this->language->lang($lang_key, $lang_str) . adm_back_link($this->u_action), E_USER_NOTICE);
161
	}
162
}
163