Passed
Push — develop-3.2.x ( 3e428e...12e171 )
by Mario
02:31
created

main_display_stats::ppde_css_classname()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 6
nop 2
dl 0
loc 37
rs 8.9617
c 0
b 0
f 0
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
use phpbb\config\config;
14
use phpbb\language\language;
15
use phpbb\template\template;
16
use skouat\ppde\actions\currency;
17
18
class main_display_stats
19
{
20
	protected $config;
21
	protected $language;
22
	protected $ppde_actions_currency;
23
	protected $template;
24
25
	/**
26
	 * Constructor
27
	 *
28
	 * @param config   $config                Config object
29
	 * @param language $language              Language user object
30
	 * @param currency $ppde_actions_currency Currency actions object
31
	 * @param template $template              Template object
32
	 *
33
	 * @access public
34
	 */
35
	public function __construct(config $config, language $language, currency $ppde_actions_currency, template $template)
36
	{
37
		$this->config = $config;
38
		$this->language = $language;
39
		$this->ppde_actions_currency = $ppde_actions_currency;
40
		$this->template = $template;
41
	}
42
43
	/**
44
	 * Assign statistics vars to the template
45
	 *
46
	 * @return void
47
	 * @access public
48
	 */
49
	public function display_stats()
50
	{
51
		if ($this->config['ppde_goal_enable'] || $this->config['ppde_raised_enable'] || $this->config['ppde_used_enable'])
52
		{
53
			// Get data from the database
54
			$default_currency_data = $this->ppde_actions_currency->get_default_currency_data((int) $this->config['ppde_default_currency']);
55
56
			$this->template->assign_vars(array(
57
				'PPDE_GOAL_ENABLE'   => $this->config['ppde_goal_enable'],
58
				'PPDE_RAISED_ENABLE' => $this->config['ppde_raised_enable'],
59
				'PPDE_USED_ENABLE'   => $this->config['ppde_used_enable'],
60
61
				'L_PPDE_GOAL'   => $this->get_ppde_goal_langkey($default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']),
62
				'L_PPDE_RAISED' => $this->get_ppde_raised_langkey($default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']),
63
				'L_PPDE_USED'   => $this->get_ppde_used_langkey($default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']),
64
			));
65
66
			// Generate statistics percent for display
67
			$this->generate_stats_percent();
68
		}
69
	}
70
71
	/**
72
	 * Retrieve the language key for donation goal
73
	 *
74
	 * @param string $currency_symbol Currency symbol
75
	 * @param bool   $on_left         Symbol position
76
	 *
77
	 * @return string
78
	 * @access public
79
	 */
80
	public function get_ppde_goal_langkey($currency_symbol, $on_left = true)
81
	{
82
		if ((int) $this->config['ppde_goal'] <= 0)
83
		{
84
			$l_ppde_goal = $this->language->lang('PPDE_DONATE_NO_GOAL');
85
		}
86
		else if ((int) $this->config['ppde_goal'] < (int) $this->config['ppde_raised'])
87
		{
88
			$l_ppde_goal = $this->language->lang('PPDE_DONATE_GOAL_REACHED');
89
		}
90
		else
91
		{
92
			$l_ppde_goal = $this->language->lang('PPDE_DONATE_GOAL_RAISE', $this->ppde_actions_currency->currency_on_left((float) $this->config['ppde_goal'], $currency_symbol, $on_left));
93
		}
94
95
		return $l_ppde_goal;
96
	}
97
98
	/**
99
	 * Retrieve the language key for donation raised
100
	 *
101
	 * @param string $currency_symbol Currency symbol
102
	 * @param bool   $on_left         Symbol position
103
	 *
104
	 * @return string
105
	 * @access public
106
	 */
107
	public function get_ppde_raised_langkey($currency_symbol, $on_left = true)
108
	{
109
		if ((int) $this->config['ppde_raised'] <= 0)
110
		{
111
			$l_ppde_raised = $this->language->lang('PPDE_DONATE_NOT_RECEIVED');
112
		}
113
		else
114
		{
115
			$l_ppde_raised = $this->language->lang('PPDE_DONATE_RECEIVED', $this->ppde_actions_currency->currency_on_left((float) $this->config['ppde_raised'], $currency_symbol, $on_left));
116
		}
117
118
		return $l_ppde_raised;
119
	}
120
121
	/**
122
	 * Retrieve the language key for donation used
123
	 *
124
	 * @param string $currency_symbol Currency symbol
125
	 * @param bool   $on_left         Symbol position
126
	 *
127
	 * @return string
128
	 * @access public
129
	 */
130
	public function get_ppde_used_langkey($currency_symbol, $on_left = true)
131
	{
132
		if ((int) $this->config['ppde_used'] <= 0)
133
		{
134
			$l_ppde_used = $this->language->lang('PPDE_DONATE_NOT_USED');
135
		}
136
		else if ((int) $this->config['ppde_used'] < (int) $this->config['ppde_raised'])
137
		{
138
			$l_ppde_used = $this->language->lang('PPDE_DONATE_USED', $this->ppde_actions_currency->currency_on_left((float) $this->config['ppde_used'], $currency_symbol, $on_left), $this->ppde_actions_currency->currency_on_left((float) $this->config['ppde_raised'], $currency_symbol, $on_left));
139
		}
140
		else
141
		{
142
			$l_ppde_used = $this->language->lang('PPDE_DONATE_USED_EXCEEDED', $this->ppde_actions_currency->currency_on_left((float) $this->config['ppde_used'], $currency_symbol, $on_left));
143
		}
144
145
		return $l_ppde_used;
146
	}
147
148
	/**
149
	 * Generate statistics percent for display
150
	 *
151
	 * @return void
152
	 * @access private
153
	 */
154
	private function generate_stats_percent()
155
	{
156
		if ($this->is_ppde_goal_stats())
157
		{
158
			$percent = $this->percent_value((float) $this->config['ppde_raised'], (float) $this->config['ppde_goal']);
159
			$this->assign_vars_stats_percent('GOAL_NUMBER', $percent);
160
		}
161
162
		if ($this->is_ppde_used_stats())
163
		{
164
			$percent = $this->percent_value((float) $this->config['ppde_used'], (float) $this->config['ppde_raised']);
165
			$this->assign_vars_stats_percent('USED_NUMBER', $percent, true);
166
		}
167
	}
168
169
	/**
170
	 * Checks if stats can be displayed
171
	 *
172
	 * @return bool
173
	 * @access private
174
	 */
175
	private function is_ppde_goal_stats()
176
	{
177
		return $this->config['ppde_goal_enable'] && (int) $this->config['ppde_goal'] > 0;
178
	}
179
180
	/**
181
	 * Checks if stats can be displayed
182
	 *
183
	 * @return bool
184
	 * @access private
185
	 */
186
	private function is_ppde_used_stats()
187
	{
188
		return $this->config['ppde_used_enable'] && (int) $this->config['ppde_raised'] > 0 && (int) $this->config['ppde_used'] > 0;
189
	}
190
191
	/**
192
	 * Returns percent value
193
	 *
194
	 * @param float $multiplicand
195
	 * @param float $dividend
196
	 *
197
	 * @return float
198
	 * @access private
199
	 */
200
	private function percent_value($multiplicand, $dividend)
201
	{
202
		return ($multiplicand * 100) / $dividend;
203
	}
204
205
	/**
206
	 * Assign statistics percent vars to template
207
	 *
208
	 * @param string $varname
209
	 * @param float  $percent
210
	 * @param bool   $reverse_css
211
	 *
212
	 * @return void
213
	 * @access private
214
	 */
215
	private function assign_vars_stats_percent($varname, $percent, $reverse_css = false)
216
	{
217
		// Force $varname to be in upper case
218
		$varname = strtoupper($varname);
219
220
		$this->template->assign_vars(array(
221
			'PPDE_' . $varname     => ($percent < 100) ? round($percent, 2) : round($percent, 0),
222
			'PPDE_CSS_' . $varname => $this->ppde_css_classname($percent, $reverse_css),
223
			'S_' . $varname        => true,
224
		));
225
	}
226
227
	/**
228
	 * Returns the CSS class name based on the percent of stats
229
	 *
230
	 * @param float $value
231
	 * @param bool  $reverse
232
	 *
233
	 * @return string
234
	 * @access private
235
	 */
236
	private function ppde_css_classname($value, $reverse = false)
237
	{
238
		$css_reverse = '';
239
		// Array of CSS class name
240
		$css_data_ary = array(
241
			10  => 'ten',
242
			20  => 'twenty',
243
			30  => 'thirty',
244
			40  => 'forty',
245
			50  => 'fifty',
246
			60  => 'sixty',
247
			70  => 'seventy',
248
			80  => 'eighty',
249
			90  => 'ninety',
250
			100 => 'hundred',
251
		);
252
253
		// Determine the index based on the value rounded up to the next highest
254
		$index = ceil($value / 10) * 10;
255
256
		// Reverse the CSS color
257
		if ($reverse && $value < 100)
258
		{
259
			// Determine the index based on the value rounded to the next lowest integer.
260
			$index = floor($value / 10) * 10;
261
262
			$value = 100 - $value;
263
			$css_reverse = '-reverse';
264
		}
265
266
		if (isset($css_data_ary[$index]) && $value < 100)
267
		{
268
			return $css_data_ary[$index] . $css_reverse;
269
		}
270
		else
271
		{
272
			return $reverse ? 'red' : 'green';
273
		}
274
	}
275
}
276