Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like main_controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use main_controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class main_controller |
||
| 16 | { |
||
| 17 | protected $auth; |
||
| 18 | protected $config; |
||
| 19 | protected $container; |
||
| 20 | protected $extension_manager; |
||
| 21 | protected $helper; |
||
| 22 | protected $ppde_entity_currency; |
||
| 23 | protected $ppde_entity_donation_pages; |
||
| 24 | protected $ppde_entity_transactions; |
||
| 25 | protected $ppde_operator_currency; |
||
| 26 | protected $ppde_operator_donation_pages; |
||
| 27 | protected $ppde_operator_transactions; |
||
| 28 | protected $request; |
||
| 29 | protected $template; |
||
| 30 | protected $user; |
||
| 31 | protected $root_path; |
||
| 32 | protected $php_ext; |
||
| 33 | /** @var array */ |
||
| 34 | protected $ext_meta = array(); |
||
| 35 | /** @var string */ |
||
| 36 | protected $ext_name; |
||
| 37 | /** @var string */ |
||
| 38 | private $donation_body; |
||
| 39 | /** @var array */ |
||
| 40 | private $donation_content_data; |
||
| 41 | /** @var string */ |
||
| 42 | private $return_args_url; |
||
| 43 | /** @var string */ |
||
| 44 | private $u_action; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructor |
||
| 48 | * |
||
| 49 | * @param \phpbb\auth\auth $auth Auth object |
||
| 50 | * @param \phpbb\config\config $config Config object |
||
| 51 | * @param ContainerInterface $container Service container interface |
||
| 52 | * @param \phpbb\extension\manager $extension_manager An instance of the phpBB extension |
||
| 53 | * manager |
||
| 54 | * @param \phpbb\controller\helper $helper Controller helper object |
||
| 55 | * @param \skouat\ppde\entity\currency $ppde_entity_currency Currency entity object |
||
| 56 | * @param \skouat\ppde\entity\donation_pages $ppde_entity_donation_pages Donation pages entity object |
||
| 57 | * @param \skouat\ppde\entity\transactions $ppde_entity_transactions Transactions log entity object |
||
| 58 | * @param \skouat\ppde\operators\currency $ppde_operator_currency Currency operator object |
||
| 59 | * @param \skouat\ppde\operators\donation_pages $ppde_operator_donation_pages Donation pages operator object |
||
| 60 | * @param \skouat\ppde\operators\transactions $ppde_operator_transactions Transactions log operator object |
||
| 61 | * @param \phpbb\request\request $request Request object |
||
| 62 | * @param \phpbb\template\template $template Template object |
||
| 63 | * @param \phpbb\user $user User object |
||
| 64 | * @param string $root_path phpBB root path |
||
| 65 | * @param string $php_ext phpEx |
||
| 66 | * |
||
| 67 | * @return \skouat\ppde\controller\main_controller |
||
| 68 | * @access public |
||
| 69 | */ |
||
| 70 | public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, ContainerInterface $container, \phpbb\extension\manager $extension_manager, \phpbb\controller\helper $helper, \skouat\ppde\entity\currency $ppde_entity_currency, \skouat\ppde\entity\donation_pages $ppde_entity_donation_pages, \skouat\ppde\entity\transactions $ppde_entity_transactions, \skouat\ppde\operators\currency $ppde_operator_currency, \skouat\ppde\operators\donation_pages $ppde_operator_donation_pages, \skouat\ppde\operators\transactions $ppde_operator_transactions, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, $root_path, $php_ext) |
||
| 71 | { |
||
| 72 | $this->auth = $auth; |
||
| 73 | $this->config = $config; |
||
| 74 | $this->container = $container; |
||
| 75 | $this->extension_manager = $extension_manager; |
||
| 76 | $this->helper = $helper; |
||
| 77 | $this->ppde_entity_currency = $ppde_entity_currency; |
||
| 78 | $this->ppde_entity_donation_pages = $ppde_entity_donation_pages; |
||
| 79 | $this->ppde_entity_transactions = $ppde_entity_transactions; |
||
| 80 | $this->ppde_operator_currency = $ppde_operator_currency; |
||
| 81 | $this->ppde_operator_donation_pages = $ppde_operator_donation_pages; |
||
| 82 | $this->ppde_operator_transactions = $ppde_operator_transactions; |
||
| 83 | $this->request = $request; |
||
| 84 | $this->template = $template; |
||
| 85 | $this->user = $user; |
||
| 86 | $this->root_path = $root_path; |
||
| 87 | $this->php_ext = $php_ext; |
||
| 88 | } |
||
| 89 | |||
| 90 | public function handle() |
||
| 91 | { |
||
| 92 | // When this extension is disabled, redirect users back to the forum index |
||
| 93 | // Else if user is not allowed to use it, disallow access to the extension main page |
||
| 94 | if (empty($this->config['ppde_enable'])) |
||
| 95 | { |
||
| 96 | redirect(append_sid("{$this->root_path}index.{$this->php_ext}")); |
||
| 97 | } |
||
| 98 | else if (!$this->can_use_ppde()) |
||
| 99 | { |
||
| 100 | trigger_error('NOT_AUTHORISED'); |
||
| 101 | } |
||
| 102 | |||
| 103 | $entity = $this->container->get('skouat.ppde.entity.donation_pages'); |
||
| 104 | $this->set_return_args_url($this->request->variable('return', 'body')); |
||
| 105 | |||
| 106 | // Prepare message for display |
||
| 107 | if ($this->get_donation_content_data($this->return_args_url)) |
||
| 108 | { |
||
| 109 | $entity->get_vars(); |
||
| 110 | $this->donation_body = $entity->replace_template_vars($entity->get_message_for_display( |
||
| 111 | $this->donation_content_data[0]['page_content'], |
||
| 112 | $this->donation_content_data[0]['page_content_bbcode_uid'], |
||
| 113 | $this->donation_content_data[0]['page_content_bbcode_bitfield'], |
||
| 114 | $this->donation_content_data[0]['page_content_bbcode_options'] |
||
| 115 | )); |
||
| 116 | } |
||
| 117 | |||
| 118 | $this->template->assign_vars(array( |
||
| 119 | 'DEFAULT_CURRENCY' => $this->build_currency_select_menu($this->config['ppde_default_currency']), |
||
| 120 | 'DONATION_BODY' => $this->donation_body, |
||
| 121 | 'IMG_LOADER' => '<img src="' . $this->root_path . '../ext/skouat/ppde/images/loader.gif' . '" />', |
||
| 122 | 'PPDE_DEFAULT_VALUE' => $this->config['ppde_default_value'] ? $this->config['ppde_default_value'] : 0, |
||
| 123 | 'PPDE_LIST_VALUE' => $this->build_currency_value_select_menu(), |
||
| 124 | |||
| 125 | 'S_HIDDEN_FIELDS' => $this->paypal_hidden_fields(), |
||
| 126 | 'S_PPDE_FORM_ACTION' => $this->get_paypal_url(), |
||
| 127 | 'S_RETURN_ARGS' => $this->return_args_url, |
||
| 128 | 'S_SANDBOX' => $this->use_sandbox(), |
||
| 129 | )); |
||
| 130 | |||
| 131 | $this->display_stats(); |
||
| 132 | |||
| 133 | // Send all data to the template file |
||
| 134 | return $this->send_data_to_template(); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function donorlist_handle() |
||
| 138 | { |
||
| 139 | // When this extension is disabled, redirect users back to the forum index |
||
| 140 | // Else if user is not allowed to view the donors list, disallow access to the extension page |
||
| 141 | if (!$this->use_ipn()) |
||
| 142 | { |
||
| 143 | redirect(append_sid($this->root_path . 'index.' . $this->php_ext)); |
||
| 144 | } |
||
| 145 | else if (!$this->can_view_ppde_donorlist()) |
||
| 146 | { |
||
| 147 | trigger_error('NOT_AUTHORISED'); |
||
| 148 | } |
||
| 149 | |||
| 150 | // Get needed container |
||
| 151 | /** @type \phpbb\pagination $pagination */ |
||
| 152 | $pagination = $this->container->get('pagination'); |
||
| 153 | /** @type \phpbb\path_helper $path_helper */ |
||
| 154 | $path_helper = $this->container->get('path_helper'); |
||
| 155 | |||
| 156 | // Set up general vars |
||
| 157 | $default_key = 'd'; |
||
| 158 | $sort_key = $this->request->variable('sk', $default_key); |
||
| 159 | $sort_dir = $this->request->variable('sd', 'd'); |
||
| 160 | $start = $this->request->variable('start', 0); |
||
| 161 | |||
| 162 | // Sorting and order |
||
| 163 | $sort_key_sql = array('a' => 'amount', 'd' => 'txn.payment_date', 'u' => 'u.username_clean'); |
||
| 164 | |||
| 165 | if (!isset($sort_key_sql[$sort_key])) |
||
| 166 | { |
||
| 167 | $sort_key = $default_key; |
||
| 168 | } |
||
| 169 | |||
| 170 | $order_by = $sort_key_sql[$sort_key] . ' ' . (($sort_dir == 'a') ? 'ASC' : 'DESC'); |
||
| 171 | |||
| 172 | // Build a relevant pagination_url and sort_url |
||
| 173 | // We do not use request_var() here directly to save some calls (not all variables are set) |
||
| 174 | $check_params = array( |
||
| 175 | 'sk' => array('sk', $default_key), |
||
| 176 | 'sd' => array('sd', 'a'), |
||
| 177 | 'start' => array('start', 0), |
||
| 178 | ); |
||
| 179 | |||
| 180 | $params = $this->check_params($check_params, array('start')); |
||
| 181 | $sort_params = $this->check_params($check_params, array('sk', 'sd')); |
||
| 182 | |||
| 183 | // Set '$this->u_action' |
||
| 184 | $use_page = ($this->u_action) ? $this->u_action : $this->user->page['page_name']; |
||
| 185 | $this->u_action = reapply_sid($path_helper->get_valid_page($use_page, $this->config['enable_mod_rewrite'])); |
||
| 186 | |||
| 187 | $pagination_url = append_sid($this->u_action, implode('&', $params), true, false, true); |
||
| 188 | $sort_url = $this->set_url_delim(append_sid($this->u_action, implode('&', $sort_params), true, false, true), $sort_params); |
||
| 189 | |||
| 190 | $get_donorlist_sql_ary = $this->ppde_operator_transactions->get_sql_donorlist_ary(false, $order_by); |
||
| 191 | $total_donors = $this->ppde_operator_transactions->query_sql_count($get_donorlist_sql_ary, 'txn.user_id'); |
||
| 192 | $start = $pagination->validate_start($start, $this->config['topics_per_page'], $total_donors); |
||
| 193 | |||
| 194 | $pagination->generate_template_pagination($pagination_url, 'pagination', 'start', $total_donors, $this->config['topics_per_page'], $start); |
||
| 195 | |||
| 196 | // adds fields to the table schema needed by entity->import() |
||
| 197 | $additional_table_schema = array( |
||
| 198 | 'item_username' => array('name' => 'username', 'type' => 'string'), |
||
| 199 | 'item_user_colour' => array('name' => 'user_colour', 'type' => 'string'), |
||
| 200 | 'item_amount' => array('name' => 'amount', 'type' => 'float'), |
||
| 201 | 'item_max_txn_id' => array('name' => 'max_txn_id', 'type' => 'integer'), |
||
| 202 | ); |
||
| 203 | |||
| 204 | $data_ary = $this->ppde_entity_transactions->get_data($this->ppde_operator_transactions->build_sql_donorlist_data($get_donorlist_sql_ary), $additional_table_schema, $this->config['topics_per_page'], $start); |
||
| 205 | |||
| 206 | // Get default currency data from the database |
||
| 207 | $default_currency_data = $this->get_default_currency_data($this->config['ppde_default_currency']); |
||
| 208 | $this->template->assign_vars(array( |
||
| 209 | 'TOTAL_DONORS' => $this->user->lang('PPDE_DONORS', $total_donors), |
||
| 210 | 'U_SORT_AMOUNT' => $sort_url . 'sk=a&sd=' . $this->set_sort_key($sort_key, 'a', $sort_dir), |
||
| 211 | 'U_SORT_DONATED' => $sort_url . 'sk=d&sd=' . $this->set_sort_key($sort_key, 'd', $sort_dir), |
||
| 212 | 'U_SORT_USERNAME' => $sort_url . 'sk=u&sd=' . $this->set_sort_key($sort_key, 'u', $sort_dir), |
||
| 213 | )); |
||
| 214 | |||
| 215 | foreach ($data_ary as $data) |
||
| 216 | { |
||
| 217 | $get_last_transaction_sql_ary = $this->ppde_operator_transactions->get_sql_donorlist_ary($data['max_txn_id']); |
||
| 218 | $last_donation_data = $this->ppde_entity_transactions->get_data($this->ppde_operator_transactions->build_sql_donorlist_data($get_last_transaction_sql_ary)); |
||
| 219 | $this->template->assign_block_vars('donorrow', array( |
||
| 220 | 'PPDE_DONOR_USERNAME' => get_username_string('full', $data['user_id'], $data['username'], $data['user_colour']), |
||
| 221 | 'PPDE_LAST_DONATED_AMOUNT' => $this->get_amount(number_format($last_donation_data[0]['mc_gross'], 2), $default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']), |
||
| 222 | 'PPDE_LAST_PAYMENT_DATE' => $this->user->format_date($last_donation_data[0]['payment_date']), |
||
| 223 | 'PPDE_TOTAL_DONATED_AMOUNT' => $this->get_amount(number_format($data['amount'], 2), $default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']), |
||
| 224 | )); |
||
| 225 | } |
||
| 226 | |||
| 227 | // Set "return_args_url" object before sending data to template |
||
| 228 | $this->set_return_args_url('donorlist'); |
||
| 229 | |||
| 230 | // Send all data to the template file |
||
| 231 | return $this->send_data_to_template(); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Set the sort key value |
||
| 236 | * |
||
| 237 | * @param string $sk |
||
| 238 | * @param string $sk_comp |
||
| 239 | * @param string $sd |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | * @access private |
||
| 243 | */ |
||
| 244 | private function set_sort_key($sk, $sk_comp, $sd) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Simply adds an url or & delimiter to the url when params is empty |
||
| 251 | * |
||
| 252 | * @param $url |
||
| 253 | * @param $params |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | * @access private |
||
| 257 | */ |
||
| 258 | private function set_url_delim($url, $params) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param $params |
||
| 265 | * |
||
| 266 | * @return array |
||
| 267 | * @access private |
||
| 268 | */ |
||
| 269 | private function check_params($params_ary, $excluded_keys) |
||
| 270 | { |
||
| 271 | $params = array(); |
||
| 272 | |||
| 273 | foreach ($params_ary as $key => $call) |
||
| 274 | { |
||
| 275 | if (!isset($_REQUEST[$key])) |
||
| 276 | { |
||
| 277 | continue; |
||
| 278 | } |
||
| 279 | |||
| 280 | $param = call_user_func_array('request_var', $call); |
||
| 281 | $param = urlencode($key) . '=' . ((is_string($param)) ? urlencode($param) : $param); |
||
| 282 | |||
| 283 | if (!in_array($key, $excluded_keys)) |
||
| 284 | { |
||
| 285 | $params[] = $param; |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | return $params; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return bool |
||
| 294 | * @access public |
||
| 295 | */ |
||
| 296 | public function can_use_ppde() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return bool |
||
| 303 | * @access public |
||
| 304 | */ |
||
| 305 | public function can_view_ppde_donorlist() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $set_return_args_url |
||
| 312 | * |
||
| 313 | * @return null |
||
| 314 | * @access private |
||
| 315 | */ |
||
| 316 | private function set_return_args_url($set_return_args_url) |
||
| 317 | { |
||
| 318 | switch ($set_return_args_url) |
||
| 319 | { |
||
| 320 | case 'cancel': |
||
| 321 | case 'success': |
||
| 322 | $this->template->assign_vars(array( |
||
| 323 | 'L_PPDE_DONATION_TITLE' => $this->user->lang['PPDE_' . strtoupper($set_return_args_url) . '_TITLE'], |
||
| 324 | )); |
||
| 325 | $this->return_args_url = $set_return_args_url; |
||
| 326 | break; |
||
| 327 | case 'donorlist': |
||
| 328 | $this->template->assign_vars(array( |
||
| 329 | 'L_PPDE_DONORLIST_TITLE' => $this->user->lang['PPDE_DONORLIST_TITLE'], |
||
| 330 | )); |
||
| 331 | $this->return_args_url = $set_return_args_url; |
||
| 332 | break; |
||
| 333 | default: |
||
| 334 | $this->return_args_url = 'body'; |
||
| 335 | } |
||
| 336 | |||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Get content of current donation pages |
||
| 341 | * |
||
| 342 | * @param string $return_args_url |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | * @access private |
||
| 346 | */ |
||
| 347 | private function get_donation_content_data($return_args_url) |
||
| 348 | { |
||
| 349 | return $this->donation_content_data = |
||
| 350 | $this->ppde_entity_donation_pages->get_data( |
||
| 351 | $this->ppde_operator_donation_pages->build_sql_data($this->user->get_iso_lang_id(), $return_args_url)); |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Build pull down menu options of available currency |
||
| 356 | * |
||
| 357 | * @param int $config_value Currency identifier; default: 0 |
||
| 358 | * |
||
| 359 | * @return null |
||
| 360 | * @access public |
||
| 361 | */ |
||
| 362 | public function build_currency_select_menu($config_value = 0) |
||
| 363 | { |
||
| 364 | // Grab the list of all enabled currencies; 0 is for all data |
||
| 365 | $currency_items = $this->ppde_entity_currency->get_data($this->ppde_operator_currency->build_sql_data(0, true)); |
||
| 366 | |||
| 367 | // Process each rule menu item for pull-down |
||
| 368 | foreach ($currency_items as $currency_item) |
||
| 369 | { |
||
| 370 | // Set output block vars for display in the template |
||
| 371 | $this->template->assign_block_vars('options', array( |
||
| 372 | 'CURRENCY_ID' => (int) $currency_item['currency_id'], |
||
| 373 | 'CURRENCY_ISO_CODE' => $currency_item['currency_iso_code'], |
||
| 374 | 'CURRENCY_NAME' => $currency_item['currency_name'], |
||
| 375 | 'CURRENCY_SYMBOL' => $currency_item['currency_symbol'], |
||
| 376 | |||
| 377 | 'S_CURRENCY_DEFAULT' => $config_value == $currency_item['currency_id'], |
||
| 378 | )); |
||
| 379 | } |
||
| 380 | unset ($currency_items, $currency_item); |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Build pull down menu options of available currency value |
||
| 385 | * |
||
| 386 | * @return string List of currency value set in ACP for dropdown menu |
||
| 387 | * @access private |
||
| 388 | */ |
||
| 389 | private function build_currency_value_select_menu() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Get dropbox config value |
||
| 410 | * |
||
| 411 | * @return bool |
||
| 412 | * @access private |
||
| 413 | */ |
||
| 414 | private function get_dropbox_status() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Force dropbox value to integer |
||
| 421 | * |
||
| 422 | * @param int $value |
||
| 423 | * |
||
| 424 | * @return int |
||
| 425 | */ |
||
| 426 | private function settype_dropbox_int_value($value = 0) |
||
| 427 | { |
||
| 428 | if (settype($value, 'integer') && $value != 0) |
||
| 429 | { |
||
| 430 | return $value; |
||
| 431 | } |
||
| 432 | |||
| 433 | return 0; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Build PayPal hidden fields |
||
| 438 | * |
||
| 439 | * @return string PayPal hidden field needed to fill PayPal forms |
||
| 440 | * @access private |
||
| 441 | */ |
||
| 442 | private function paypal_hidden_fields() |
||
| 443 | { |
||
| 444 | return build_hidden_fields(array( |
||
| 445 | 'cmd' => '_donations', |
||
| 446 | 'business' => $this->get_account_id(), |
||
| 447 | 'item_name' => $this->user->lang['PPDE_DONATION_TITLE_HEAD'] . ' ' . $this->config['sitename'], |
||
| 448 | 'no_shipping' => 1, |
||
| 449 | 'return' => $this->generate_paypal_return_url('success'), |
||
| 450 | 'notify_url' => $this->generate_paypal_notify_return_url(), |
||
| 451 | 'cancel_return' => $this->generate_paypal_return_url('cancel'), |
||
| 452 | 'item_number' => 'uid_' . $this->user->data['user_id'] . '_' . time(), |
||
| 453 | 'tax' => 0, |
||
| 454 | 'bn' => 'Board_Donate_WPS', |
||
| 455 | 'charset' => 'utf-8', |
||
| 456 | )); |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Get PayPal account id |
||
| 461 | * |
||
| 462 | * @return string $this Paypal account Identifier |
||
| 463 | * @access private |
||
| 464 | */ |
||
| 465 | private function get_account_id() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Check if Sandbox is enabled |
||
| 472 | * |
||
| 473 | * @return bool |
||
| 474 | * @access public |
||
| 475 | */ |
||
| 476 | public function use_sandbox() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Check if IPN is enabled |
||
| 483 | * |
||
| 484 | * @return bool |
||
| 485 | * @access public |
||
| 486 | */ |
||
| 487 | public function use_ipn() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Generate PayPal return URL |
||
| 494 | * |
||
| 495 | * @param string $arg |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | * @access private |
||
| 499 | */ |
||
| 500 | private function generate_paypal_return_url($arg) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Generate PayPal return notify URL |
||
| 507 | * |
||
| 508 | * @return string |
||
| 509 | * @access private |
||
| 510 | */ |
||
| 511 | private function generate_paypal_notify_return_url() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Get PayPal URL |
||
| 518 | * Used in form and in IPN process |
||
| 519 | * |
||
| 520 | * @param bool $is_test_ipn |
||
| 521 | * |
||
| 522 | * @return string |
||
| 523 | * @access public |
||
| 524 | */ |
||
| 525 | public function get_paypal_url($is_test_ipn = false) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Assign statistics vars to the template |
||
| 532 | * |
||
| 533 | * @return null |
||
| 534 | * @access public |
||
| 535 | */ |
||
| 536 | public function display_stats() |
||
| 537 | { |
||
| 538 | if ($this->config['ppde_goal_enable'] || $this->config['ppde_raised_enable'] || $this->config['ppde_used_enable']) |
||
| 539 | { |
||
| 540 | // Get data from the database |
||
| 541 | $default_currency_data = $this->get_default_currency_data($this->config['ppde_default_currency']); |
||
| 542 | |||
| 543 | $this->template->assign_vars(array( |
||
| 544 | 'PPDE_GOAL_ENABLE' => $this->config['ppde_goal_enable'], |
||
| 545 | 'PPDE_RAISED_ENABLE' => $this->config['ppde_raised_enable'], |
||
| 546 | 'PPDE_USED_ENABLE' => $this->config['ppde_used_enable'], |
||
| 547 | |||
| 548 | 'L_PPDE_GOAL' => $this->get_ppde_goal_langkey($default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']), |
||
| 549 | 'L_PPDE_RAISED' => $this->get_ppde_raised_langkey($default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']), |
||
| 550 | 'L_PPDE_USED' => $this->get_ppde_used_langkey($default_currency_data[0]['currency_symbol'], (bool) $default_currency_data[0]['currency_on_left']), |
||
| 551 | )); |
||
| 552 | |||
| 553 | // Generate statistics percent for display |
||
| 554 | $this->generate_stats_percent(); |
||
| 555 | } |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Get default currency symbol |
||
| 560 | * |
||
| 561 | * @param int $id |
||
| 562 | * |
||
| 563 | * @return array |
||
| 564 | * @access public |
||
| 565 | */ |
||
| 566 | public function get_default_currency_data($id = 0) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Retrieve the language key for donation goal |
||
| 573 | * |
||
| 574 | * @param string $currency_symbol Currency symbol |
||
| 575 | * @param bool $on_left Symbol position |
||
| 576 | * |
||
| 577 | * @return string |
||
| 578 | * @access public |
||
| 579 | */ |
||
| 580 | public function get_ppde_goal_langkey($currency_symbol, $on_left = true) |
||
| 581 | { |
||
| 582 | if ((int) $this->config['ppde_goal'] <= 0) |
||
| 583 | { |
||
| 584 | $l_ppde_goal = $this->user->lang['DONATE_NO_GOAL']; |
||
| 585 | } |
||
| 586 | View Code Duplication | else if ((int) $this->config['ppde_goal'] < (int) $this->config['ppde_raised']) |
|
| 587 | { |
||
| 588 | $l_ppde_goal = $this->user->lang['DONATE_GOAL_REACHED']; |
||
| 589 | } |
||
| 590 | else |
||
| 591 | { |
||
| 592 | $l_ppde_goal = $this->user->lang('DONATE_GOAL_RAISE', $this->get_amount((int) $this->config['ppde_goal'], $currency_symbol, $on_left)); |
||
| 593 | } |
||
| 594 | |||
| 595 | return $l_ppde_goal; |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Put the currency on the left or on the right of the amount |
||
| 600 | * |
||
| 601 | * @param int $value |
||
| 602 | * @param string $currency |
||
| 603 | * @param bool $on_left |
||
| 604 | * |
||
| 605 | * @return string |
||
| 606 | */ |
||
| 607 | private function get_amount($value, $currency, $on_left = true) |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Retrieve the language key for donation raised |
||
| 614 | * |
||
| 615 | * @param string $currency_symbol Currency symbol |
||
| 616 | * @param bool $on_left Symbol position |
||
| 617 | * |
||
| 618 | * @return string |
||
| 619 | * @access public |
||
| 620 | */ |
||
| 621 | public function get_ppde_raised_langkey($currency_symbol, $on_left = true) |
||
| 622 | { |
||
| 623 | View Code Duplication | if ((int) $this->config['ppde_raised'] <= 0) |
|
| 624 | { |
||
| 625 | $l_ppde_raised = $this->user->lang['DONATE_NOT_RECEIVED']; |
||
| 626 | } |
||
| 627 | else |
||
| 628 | { |
||
| 629 | $l_ppde_raised = $this->user->lang('DONATE_RECEIVED', $this->get_amount((int) $this->config['ppde_raised'], $currency_symbol, $on_left)); |
||
| 630 | } |
||
| 631 | |||
| 632 | return $l_ppde_raised; |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Retrieve the language key for donation used |
||
| 637 | * |
||
| 638 | * @param string $currency_symbol Currency symbol |
||
| 639 | * @param bool $on_left Symbol position |
||
| 640 | * |
||
| 641 | * @return string |
||
| 642 | * @access public |
||
| 643 | */ |
||
| 644 | public function get_ppde_used_langkey($currency_symbol, $on_left = true) |
||
| 645 | { |
||
| 646 | if ((int) $this->config['ppde_used'] <= 0) |
||
| 647 | { |
||
| 648 | $l_ppde_used = $this->user->lang['DONATE_NOT_USED']; |
||
| 649 | } |
||
| 650 | else if ((int) $this->config['ppde_used'] < (int) $this->config['ppde_raised']) |
||
| 651 | { |
||
| 652 | $l_ppde_used = $this->user->lang('DONATE_USED', $this->get_amount((int) $this->config['ppde_used'], $currency_symbol, $on_left), $this->get_amount((int) $this->config['ppde_raised'], $currency_symbol, $on_left)); |
||
| 653 | } |
||
| 654 | else |
||
| 655 | { |
||
| 656 | $l_ppde_used = $this->user->lang('DONATE_USED_EXCEEDED', $this->get_amount((int) $this->config['ppde_used'], $currency_symbol, $on_left)); |
||
| 657 | } |
||
| 658 | |||
| 659 | return $l_ppde_used; |
||
| 660 | } |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Generate statistics percent for display |
||
| 664 | * |
||
| 665 | * @return null |
||
| 666 | * @access private |
||
| 667 | */ |
||
| 668 | private function generate_stats_percent() |
||
| 669 | { |
||
| 670 | if ($this->config['ppde_goal_enable'] && (int) $this->config['ppde_goal'] > 0) |
||
| 671 | { |
||
| 672 | $this->assign_vars_stats_percent((int) $this->config['ppde_raised'], (int) $this->config['ppde_goal'], 'GOAL_NUMBER'); |
||
| 673 | } |
||
| 674 | |||
| 675 | if ($this->config['ppde_used_enable'] && (int) $this->config['ppde_raised'] > 0 && (int) $this->config['ppde_used'] > 0) |
||
| 676 | { |
||
| 677 | $this->assign_vars_stats_percent((int) $this->config['ppde_used'], (int) $this->config['ppde_raised'], 'USED_NUMBER'); |
||
| 678 | } |
||
| 679 | } |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Assign statistics percent vars to template |
||
| 683 | * |
||
| 684 | * @param integer $multiplicand |
||
| 685 | * @param integer $dividend |
||
| 686 | * @param string $type |
||
| 687 | * |
||
| 688 | * @return null |
||
| 689 | * @access public |
||
| 690 | */ |
||
| 691 | private function assign_vars_stats_percent($multiplicand, $dividend, $type = '') |
||
| 692 | { |
||
| 693 | $this->template->assign_vars(array( |
||
| 694 | 'PPDE_' . $type => round(($multiplicand * 100) / $dividend, 2), |
||
| 695 | 'S_' . $type => !empty($type) ? true : false, |
||
| 696 | )); |
||
| 697 | } |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Send data to the template file |
||
| 701 | * |
||
| 702 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 703 | * @access private |
||
| 704 | */ |
||
| 705 | private function send_data_to_template() |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Check if cURL is available |
||
| 721 | * |
||
| 722 | * @return bool |
||
| 723 | * @access public |
||
| 724 | */ |
||
| 725 | public function check_curl() |
||
| 726 | { |
||
| 727 | if (function_exists('curl_init') && function_exists('curl_exec')) |
||
| 728 | { |
||
| 729 | $this->get_ext_meta(); |
||
| 730 | |||
| 731 | $ch = curl_init($this->ext_meta['extra']['version-check']['host']); |
||
| 732 | |||
| 733 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 734 | |||
| 735 | $response = curl_exec($ch); |
||
| 736 | $response_status = strval(curl_getinfo($ch, CURLINFO_HTTP_CODE)); |
||
| 737 | |||
| 738 | curl_close($ch); |
||
| 739 | |||
| 740 | return ($response !== false || $response_status !== '0') ? true : false; |
||
| 741 | } |
||
| 742 | |||
| 743 | return false; |
||
| 744 | } |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Get extension metadata |
||
| 748 | * |
||
| 749 | * @return null |
||
| 750 | * @access protected |
||
| 751 | */ |
||
| 752 | protected function get_ext_meta() |
||
| 753 | { |
||
| 754 | if (empty($this->ext_meta)) |
||
| 755 | { |
||
| 756 | $this->load_metadata(); |
||
| 757 | } |
||
| 758 | } |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Load metadata for this extension |
||
| 762 | * |
||
| 763 | * @return array |
||
| 764 | * @access public |
||
| 765 | */ |
||
| 766 | public function load_metadata() |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Retrieve the extension name |
||
| 791 | * |
||
| 792 | * @return null |
||
| 793 | * @access protected |
||
| 794 | */ |
||
| 795 | protected function retrieve_ext_name() |
||
| 796 | { |
||
| 797 | $namespace_ary = explode('\\', __NAMESPACE__); |
||
| 798 | $this->ext_name = $namespace_ary[0] . '/' . $namespace_ary[1]; |
||
| 799 | } |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Check if fsockopen is available |
||
| 803 | * |
||
| 804 | * @return bool |
||
| 805 | * @access public |
||
| 806 | */ |
||
| 807 | public function check_fsockopen() |
||
| 822 | } |
||
| 823 |