Completed
Pull Request — master (#21)
by Mario
05:14 queued 02:39
created

admin_overview_controller::display_confirm()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 8.439
cc 6
eloc 21
nc 10
nop 1
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\controller;
12
13
/**
14
 * @property \phpbb\log\log             log          The phpBB log system
15
 * @property \phpbb\request\request     request      Request object
16
 * @property \phpbb\template\template   $template    Template object
17
 * @property string                     u_action     Action URL
18
 * @property \phpbb\user                user         User object
19
 */
20
21
class admin_overview_controller extends admin_main
22
{
23
	protected $auth;
24
	protected $db;
25
	protected $cache;
26
	protected $config;
27
	protected $ppde_controller_main;
28
	protected $php_ext;
29
	protected $table_ppde_transactions;
30
31
	protected $ext_name;
32
	protected $ext_meta = array();
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param \phpbb\auth\auth                        $auth                    Authentication object
38
	 * @param \phpbb\db\driver\driver_interface       $db                      Database object
39
	 * @param \phpbb\cache\service                    $cache                   Cache object
40
	 * @param \phpbb\config\config                    $config                  Config object
41
	 * @param \phpbb\log\log                          $log                     The phpBB log system
42
	 * @param \skouat\ppde\controller\main_controller $ppde_controller_main    Main controller object
43
	 * @param \phpbb\request\request                  $request                 Request object
44
	 * @param \phpbb\template\template                $template                Template object
45
	 * @param \phpbb\user                             $user                    User object
46
	 * @param string                                  $php_ext                 phpEx
47
	 * @param string                                  $table_ppde_transactions Name of the table used to store data
48
	 *
49
	 * @access public
50
	 */
51
	public function __construct(\phpbb\auth\auth $auth, \phpbb\db\driver\driver_interface $db, \phpbb\cache\service $cache, \phpbb\config\config $config, \phpbb\log\log $log, \skouat\ppde\controller\main_controller $ppde_controller_main, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, $php_ext, $table_ppde_transactions)
52
	{
53
		$this->auth = $auth;
54
		$this->db = $db;
55
		$this->cache = $cache;
56
		$this->config = $config;
57
		$this->log = $log;
58
		$this->ppde_controller_main = $ppde_controller_main;
59
		$this->request = $request;
60
		$this->template = $template;
61
		$this->user = $user;
62
		$this->php_ext = $php_ext;
63
		$this->table_ppde_transactions = $table_ppde_transactions;
64
	}
65
66
	/**
67
	 * Display the overview page
68
	 *
69
	 * @param string $action Action name
70
	 *
71
	 * @return null
72
	 * @access public
73
	 */
74
	public function display_overview($action)
75
	{
76
		$this->do_action($action);
77
78
		//Load metadata for this extension
79
		$this->ext_meta = $this->ppde_controller_main->load_metadata();
80
81
		// Check if a new version is available
82
		$this->obtain_last_version();
83
84
		// Set output block vars for display in the template
85
		$this->template->assign_vars(array(
86
			'ANONYMOUS_DONORS_COUNT'    => $this->config['ppde_anonymous_donors_count'],
87
			'ANONYMOUS_DONORS_PER_DAY'  => $this->per_day_stats('ppde_anonymous_donors_count'),
88
			'INFO_CURL'                 => $this->config['ppde_curl_detected'] ? $this->user->lang('INFO_DETECTED') : $this->user->lang('INFO_NOT_DETECTED'),
89
			'INFO_FSOCKOPEN'            => $this->config['ppde_fsock_detected'] ? $this->user->lang('INFO_DETECTED') : $this->user->lang('INFO_NOT_DETECTED'),
90
			'KNOWN_DONORS_COUNT'        => $this->config['ppde_known_donors_count'],
91
			'KNOWN_DONORS_PER_DAY'      => $this->per_day_stats('ppde_known_donors_count'),
92
			'PPDE_INSTALL_DATE'         => $this->user->format_date($this->config['ppde_install_date']),
93
			'PPDE_VERSION'              => $this->ext_meta['version'],
94
			'TRANSACTIONS_COUNT'        => $this->config['ppde_count_transactions'],
95
			'TRANSACTIONS_PER_DAY'      => $this->per_day_stats('ppde_count_transactions'),
96
97
			'L_PPDE_INSTALL_DATE'       => $this->user->lang('PPDE_INSTALL_DATE', $this->ext_meta['extra']['display-name']),
98
			'L_PPDE_VERSION'            => $this->user->lang('PPDE_VERSION', $this->ext_meta['extra']['display-name']),
99
100
			'S_ACTION_OPTIONS'          => ($this->auth->acl_get('a_ppde_manage')) ? true : false,
101
			'S_FSOCKOPEN'               => $this->config['ppde_curl_detected'],
102
			'S_CURL'                    => $this->config['ppde_fsock_detected'],
103
104
			'U_PPDE_MORE_INFORMATION'   => append_sid("index.$this->php_ext", 'i=acp_extensions&amp;mode=main&amp;action=details&amp;ext_name=' . urlencode($this->ext_meta['name'])),
105
			'U_PPDE_VERSIONCHECK_FORCE' => $this->u_action . '&amp;versioncheck_force=1',
106
			'U_ACTION'                  => $this->u_action,
107
		));
108
	}
109
110
	/**
111
	 * Do action regarding the value of $action
112
	 *
113
	 * @param string $action Requested action
114
	 *
115
	 * @return null
116
	 * @access private
117
	 */
118
	private function do_action($action)
119
	{
120
		if ($action)
121
		{
122
			if (!confirm_box(true))
123
			{
124
				$this->display_confirm($action);
125
			}
126
			else
127
			{
128
				$this->exec_action($action);
129
			}
130
		}
131
	}
132
133
	/**
134
	 * Display confirm box
135
	 *
136
	 * @param string $action Requested action
137
	 *
138
	 * @return null
139
	 * @access private
140
	 */
141
	private function display_confirm($action)
142
	{
143
		switch ($action)
144
		{
145
			case 'date':
146
				$confirm = true;
147
				$confirm_lang = 'STAT_RESET_DATE_CONFIRM';
148
				break;
149
			case 'curl_fsock':
150
				$confirm = true;
151
				$confirm_lang = 'STAT_RETEST_CURL_FSOCK_CONFIRM';
152
				break;
153
			case 'donors':
154
			case 'transactions':
155
				$confirm = true;
156
				$confirm_lang = 'STAT_RESYNC_' . strtoupper($action) . 'COUNTS_CONFIRM';
157
				break;
158
			default:
159
				$confirm = true;
160
				$confirm_lang = 'CONFIRM_OPERATION';
161
		}
162
163
		if ($confirm)
164
		{
165
			confirm_box(false, $this->user->lang[$confirm_lang], build_hidden_fields(array(
166
				'action' => $action,
167
			)));
168
		}
169
	}
170
171
	/**
172
	 * @param string $action Requested action
173
	 *
174
	 * @return null
175
	 * @access private
176
	 */
177
	private function exec_action($action)
178
	{
179 View Code Duplication
		if (!$this->auth->acl_get('a_ppde_manage'))
180
		{
181
			trigger_error($this->user->lang['NO_AUTH_OPERATION'] . adm_back_link($this->u_action), E_USER_WARNING);
182
		}
183
184
		switch ($action)
185
		{
186
			case 'date':
187
				$this->config->set('ppde_install_date', time() - 1);
188
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_STAT_RESET_DATE');
189
				break;
190
			case 'remote':
191
				$this->config->set('ppde_curl_detected', $this->ppde_controller_main->check_curl());
192
				$this->config->set('ppde_fsock_detected', $this->ppde_controller_main->check_fsockopen());
193
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_STAT_RETEST_DATE');
194
				break;
195
			case 'transactions':
196
				$this->config->set('ppde_count_transactions', $this->sql_query_update_stats('ppde_count_transactions'), true);
197
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_STAT_RESYNC_TRANSACTIONSCOUNTS');
198
				break;
199
			case 'donors':
200
				$this->config->set('ppde_known_donors_count', $this->sql_query_update_stats('ppde_known_donors_count'), true);
201
				$this->config->set('ppde_anonymous_donors_count', $this->sql_query_update_stats('ppde_anonymous_donors_count'));
202
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_STAT_RESYNC_DONORSCOUNTS');
203
				break;
204
		}
205
	}
206
207
	/**
208
	 * Returns count result for updating stats
209
	 *
210
	 * @param string $config_name
211
	 *
212
	 * @return int
213
	 * @access private
214
	 */
215
	private function sql_query_update_stats($config_name)
216
	{
217
		if (!$this->config->offsetExists($config_name))
218
		{
219
			trigger_error($this->user->lang('EXCEPTION_INVALID_CONFIG_NAME', $config_name), E_USER_WARNING);
220
		}
221
222
		$this->db->sql_query($this->make_stats_sql_update($config_name));
223
224
		return (int) $this->db->sql_fetchfield('count_result');
225
	}
226
227
	/**
228
	 * Build SQL query for updating stats
229
	 *
230
	 * @param string $type
231
	 *
232
	 * @return string
233
	 * @access private
234
	 */
235
	private function make_stats_sql_update($type)
236
	{
237
		switch ($type)
238
		{
239
			case 'ppde_count_transactions':
240
				$sql = $this->make_stats_sql_select('txn_id');
241
				$sql .= " WHERE confirmed = 1 AND payment_status = 'Completed'";
242
243
				return $sql;
244
			case 'ppde_known_donors_count':
245
				$sql = $this->make_stats_sql_select('payer_id');
246
				$sql .= ' LEFT JOIN ' . USERS_TABLE . ' u
247
				 					ON txn.user_id = u.user_id
248
								WHERE u.user_type = ' . USER_NORMAL . ' OR u.user_type = ' . USER_FOUNDER;
249
250
				return $sql;
251
			case 'ppde_anonymous_donors_count':
252
				$sql = $this->make_stats_sql_select('payer_id');
253
				$sql .= ' WHERE txn.user_id = ' . ANONYMOUS;
254
255
				return $sql;
256
			default:
257
				return $this->make_stats_sql_select('txn_id');
258
		}
259
	}
260
261
	/**
262
	 * Make body of SQL query for stats calculation.
263
	 *
264
	 * @param string $field_name Name of the field
265
	 *
266
	 * @return string
267
	 * @access private
268
	 */
269
	private function make_stats_sql_select($field_name)
270
	{
271
		return 'SELECT COUNT(DISTINCT txn.' . $field_name . ') AS count_result
272
				FROM ' . $this->table_ppde_transactions . ' txn';
273
	}
274
275
	/**
276
	 * Obtains the last version for this extension
277
	 *
278
	 * @return null
279
	 * @access private
280
	 */
281
	private function obtain_last_version()
282
	{
283
		try
284
		{
285
			if (!isset($this->ext_meta['extra']['version-check']))
286
			{
287
				throw new \RuntimeException($this->user->lang('PPDE_NO_VERSIONCHECK'), 1);
288
			}
289
290
			$version_check = $this->ext_meta['extra']['version-check'];
291
292
			$version_helper = new \phpbb\version_helper($this->cache, $this->config, new \phpbb\file_downloader(), $this->user);
293
			$version_helper->set_current_version($this->ext_meta['version']);
294
			$version_helper->set_file_location($version_check['host'], $version_check['directory'], $version_check['filename']);
295
			$version_helper->force_stability($this->config['extension_force_unstable'] ? 'unstable' : null);
296
297
			$recheck = $this->request->variable('versioncheck_force', false);
298
			$s_up_to_date = $version_helper->get_suggested_updates($recheck);
299
300
			$this->template->assign_vars(array(
301
				'S_UP_TO_DATE'   => empty($s_up_to_date),
302
				'S_VERSIONCHECK' => true,
303
				'UP_TO_DATE_MSG' => $this->user->lang('PPDE_NOT_UP_TO_DATE', $this->ext_meta['extra']['display-name']),
304
			));
305
		}
306
		catch (\RuntimeException $e)
307
		{
308
			$this->template->assign_vars(array(
309
				'S_VERSIONCHECK_STATUS'    => $e->getCode(),
310
				'VERSIONCHECK_FAIL_REASON' => ($e->getMessage() !== $this->user->lang('VERSIONCHECK_FAIL')) ? $e->getMessage() : '',
311
			));
312
		}
313
	}
314
315
	/**
316
	 * Returns the percent of items (transactions, donors) per day
317
	 *
318
	 * @param string $config_name
319
	 *
320
	 * @return string
321
	 * @access private
322
	 */
323
	private function per_day_stats($config_name)
324
	{
325
		return sprintf('%.2f', (float) $this->config[$config_name] / $this->get_install_days());
326
	}
327
328
	/**
329
	 * Returns the number of days from the date of installation of the extension.
330
	 *
331
	 * @return float
332
	 * @access private
333
	 */
334
	private function get_install_days()
335
	{
336
		return (float) (time() - $this->config['ppde_install_date']) / 86400;
337
	}
338
}
339