Passed
Push — develop-3.3.x ( 4537cc...629cce )
by Mario
02:54
created

ppde_module   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 73
c 4
b 0
f 0
dl 0
loc 178
rs 10
wmc 22

3 Methods

Rating   Name   Duplication   Size   Complexity  
B switch_mode() 0 40 7
C do_action() 0 48 12
A main() 0 35 3
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2020 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\acp;
12
13
class ppde_module
14
{
15
	/** @var array */
16
	private static $available_mode = [
17
		['module_name' => 'currency', 'lang_key_prefix' => 'PPDE_DC_', 'id_prefix_name' => 'currency'],
18
		['module_name' => 'donation_pages', 'lang_key_prefix' => 'PPDE_DP_', 'id_prefix_name' => 'page'],
19
		['module_name' => 'overview'],
20
		['module_name' => 'paypal_features'],
21
		['module_name' => 'settings'],
22
		['module_name' => 'transactions', 'lang_key_prefix' => 'PPDE_DT_', 'id_prefix_name' => 'transaction'],
23
	];
24
	/** @var string */
25
	public $u_action;
26
	/** @var string */
27
	public $page_title;
28
	/** @var string */
29
	public $tpl_name;
30
	/** @var array */
31
	private $module_info;
32
33
	/**
34
	 * @param string $id
35
	 * @param string $mode
36
	 *
37
	 * @return void
38
	 * @throws \Exception
39
	 * @access public
40
	 */
41
	public function main($id, $mode)
42
	{
43
		global $phpbb_container;
44
45
		/** @type \phpbb\language\language $language Language object */
46
		$language = $phpbb_container->get('language');
47
48
		$is_in_array_field = in_array($mode, array_column(self::$available_mode, 'module_name'), true);
49
		if ($is_in_array_field)
50
		{
51
			$this->module_info = current(array_filter(self::$available_mode, function ($item) use ($mode) {
52
				return $item['module_name'] === $mode;
53
			})) ?: [];
54
55
			// Load the module language file currently in use
56
			$language->add_lang('acp_' . $mode, 'skouat/ppde');
57
58
			// Get an instance of the admin controller
59
			/** @type \skouat\ppde\controller\admin\admin_main $admin_controller */
60
			$admin_controller = $phpbb_container->get('skouat.ppde.controller.admin.' . $mode);
61
62
			// Make the $u_action url available in the admin controller
63
			$admin_controller->set_page_url($this->u_action);
64
65
			// Set the page title for our ACP page
66
			$this->page_title = 'PPDE_ACP_' . strtoupper($mode);
67
68
			// Load a template from adm/style for our ACP page
69
			$this->tpl_name = 'ppde_' . strtolower($mode);
70
71
			$this->switch_mode($id, $mode, $admin_controller);
72
		}
73
		else
74
		{
75
			trigger_error('NO_MODE', E_USER_ERROR);
76
		}
77
	}
78
79
	/**
80
	 * Switches the mode of the admin controller based on the given parameters.
81
	 *
82
	 * @param int                                      $id
83
	 * @param string                                   $mode
84
	 * @param \skouat\ppde\controller\admin\admin_main $admin_controller
85
	 *
86
	 * @return void
87
	 * @throws \Exception
88
	 * @access private
89
	 */
90
	private function switch_mode($id, $mode, $admin_controller): void
91
	{
92
		global $phpbb_container;
93
94
		/** @type \phpbb\request\request $request Request object */
95
		$request = $phpbb_container->get('request');
96
97
		// Requests vars
98
		$action = $request->variable('action', '');
99
100
		switch ($mode)
101
		{
102
			case 'currency':
103
			case 'donation_pages':
104
				// Get an instance of the entity
105
				$entity = $phpbb_container->get('skouat.ppde.entity.' . $mode);
106
107
				// Make the $u_action url available in entity
108
				$entity->set_page_url($this->u_action);
109
			// no break;
110
			case 'transactions':
111
				// Request the item ID
112
				$admin_controller->set_item_id($request->variable($this->module_info['id_prefix_name'] . '_id', 0));
113
114
				// Send module IDs to the controller
115
				$admin_controller->set_hidden_fields($id, $mode, $action);
116
117
				$this->do_action($admin_controller->get_action(), $admin_controller);
118
			break;
119
			case 'paypal_features':
120
			case 'settings':
121
				// Load the display handle in the admin controller
122
				/** @type \skouat\ppde\controller\admin\settings_controller|\skouat\ppde\controller\admin\paypal_features_controller $admin_controller */
123
				$admin_controller->display_settings();
124
			break;
125
			case 'overview':
126
				// Load the display overview handle in the admin controller
127
				/** @type \skouat\ppde\controller\admin\overview_controller $admin_controller */
128
				$admin_controller->display_overview($action);
129
			break;
130
		}
131
	}
132
133
	/**
134
	 * Performs action requested by the module
135
	 *
136
	 * @param string                                   $action
137
	 * @param \skouat\ppde\controller\admin\admin_main $controller
138
	 *
139
	 * @return void
140
	 * @throws \Exception
141
	 * @access private
142
	 */
143
	private function do_action($action, $controller): void
144
	{
145
		global $phpbb_container;
146
147
		/** @type \phpbb\language\language $language Language object */
148
		$language = $phpbb_container->get('language');
149
150
		switch ($action)
151
		{
152
			case 'add':
153
			case 'change':
154
			case 'edit':
155
			case 'view':
156
				// Set the page title for our ACP page
157
				$this->page_title = $this->module_info['lang_key_prefix'] . 'CONFIG';
158
159
				// Call the method in the admin controller based on the $action value
160
				$controller->$action();
161
162
				// Return to stop execution of this script
163
				return;
164
			case 'move_down':
165
			case 'move_up':
166
				$controller->move();
167
			break;
168
			case 'activate':
169
			case 'deactivate':
170
				$controller->enable();
171
			break;
172
			case 'approve':
173
			case 'delete':
174
				// Use a confirm box routine when approving/deleting an item
175
				if (confirm_box(true))
176
				{
177
					$controller->$action();
178
					break;
179
				}
180
181
				// Request confirmation from the user to perform the action for selected item
182
				confirm_box(false, $language->lang($this->module_info['lang_key_prefix'] . 'CONFIRM_OPERATION'), build_hidden_fields($controller->get_hidden_fields()));
183
184
				// Clear $action status
185
				$controller->set_action($action);
186
			break;
187
		}
188
189
		// Load the display handle in the admin controller
190
		$controller->display();
191
	}
192
}
193