Conditions | 8 |
Paths | 48 |
Total Lines | 96 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
46 | public function handle() |
||
47 | { |
||
48 | // If the donorlist is not enabled, redirect users back to the forum index. |
||
49 | // Else if user is not allowed to view the donors list, disallow access to the extension page. |
||
50 | if (!$this->donorlist_is_enabled()) |
||
51 | { |
||
52 | redirect(append_sid($this->root_path . 'index.' . $this->php_ext)); |
||
53 | } |
||
54 | else if (!$this->ppde_auth->can_view_ppde_donorlist()) |
||
55 | { |
||
56 | trigger_error('NOT_AUTHORISED'); |
||
57 | } |
||
58 | |||
59 | // Set up general vars |
||
60 | $default_key = 'd'; |
||
61 | $sort_key = $this->request->variable('sk', $default_key); |
||
62 | $sort_dir = $this->request->variable('sd', 'd'); |
||
63 | $start = $this->request->variable('start', 0); |
||
64 | |||
65 | // Sorting and order |
||
66 | $sort_key_sql = ['a' => 'amount', 'd' => 'txn.payment_date', 'u' => 'u.username_clean']; |
||
67 | |||
68 | if (!isset($sort_key_sql[$sort_key])) |
||
69 | { |
||
70 | $sort_key = $default_key; |
||
71 | } |
||
72 | |||
73 | $order_by = ($sort_key === 'a' ? $sort_key_sql[$sort_key] . ' ' : 'MAX(' . $sort_key_sql[$sort_key] . ') ') . (($sort_dir === 'a') ? 'ASC' : 'DESC'); |
||
74 | |||
75 | // Build a relevant pagination_url and sort_url. |
||
76 | // We do not use request_var() here directly to save some calls (not all variables are set) |
||
77 | $check_params = [ |
||
78 | 'sk' => ['sk', $default_key], |
||
79 | 'sd' => ['sd', 'a'], |
||
80 | 'start' => ['start', 0], |
||
81 | ]; |
||
82 | |||
83 | $params = $this->check_params($check_params, ['start']); |
||
84 | $sort_params = $this->check_params($check_params, ['sk', 'sd']); |
||
85 | |||
86 | // Set '$this->u_action' |
||
87 | $use_page = $this->u_action ?: $this->user->page['page_name']; |
||
88 | $this->u_action = reapply_sid($this->path_helper->get_valid_page($use_page, (bool) $this->config['enable_mod_rewrite'])); |
||
89 | |||
90 | $pagination_url = append_sid($this->u_action, implode('&', $params), true, false, true); |
||
91 | $sort_url = $this->set_url_delim(append_sid($this->u_action, implode('&', $sort_params), true, false, true), $sort_params); |
||
92 | |||
93 | $sql_count_donors = $this->ppde_operator_transactions->sql_donorlist_ary(); |
||
94 | $total_donors = $this->ppde_operator_transactions->query_sql_count($sql_count_donors, 'txn.user_id'); |
||
95 | $start = $this->pagination->validate_start($start, (int) $this->config['topics_per_page'], $total_donors); |
||
96 | |||
97 | $this->pagination->generate_template_pagination($pagination_url, 'pagination', 'start', $total_donors, (int) $this->config['topics_per_page'], $start); |
||
98 | |||
99 | // Assign vars to the template |
||
100 | $this->template->assign_vars([ |
||
101 | 'L_PPDE_DONORLIST_TITLE' => $this->language->lang('PPDE_DONORLIST_TITLE'), |
||
102 | 'TOTAL_DONORS' => $this->language->lang('PPDE_DONORS', $total_donors), |
||
103 | 'U_SORT_AMOUNT' => $sort_url . 'sk=a&sd=' . $this->set_sort_key($sort_key, 'a', $sort_dir), |
||
104 | 'U_SORT_DONATED' => $sort_url . 'sk=d&sd=' . $this->set_sort_key($sort_key, 'd', $sort_dir), |
||
105 | 'U_SORT_USERNAME' => $sort_url . 'sk=u&sd=' . $this->set_sort_key($sort_key, 'u', $sort_dir), |
||
106 | ]); |
||
107 | |||
108 | // Adds fields to the table schema needed by entity->import() |
||
109 | $donorlist_table_schema = [ |
||
110 | 'item_amount' => ['name' => 'amount', 'type' => 'float'], |
||
111 | 'item_max_txn_id' => ['name' => 'max_txn_id', 'type' => 'integer'], |
||
112 | 'item_user_id' => ['name' => 'user_id', 'type' => 'integer'], |
||
113 | 'item_mc_currency' => ['name' => 'mc_currency', 'type' => 'string'], |
||
114 | ]; |
||
115 | |||
116 | $sql_donorlist_ary = $this->ppde_operator_transactions->sql_donorlist_ary(true, $order_by); |
||
117 | $data_ary = $this->ppde_entity_transactions->get_data($this->ppde_operator_transactions->build_sql_donorlist_data($sql_donorlist_ary), $donorlist_table_schema, (int) $this->config['topics_per_page'], $start, true); |
||
118 | |||
119 | // Adds fields to the table schema needed by entity->import() |
||
120 | $last_donation_table_schema = [ |
||
121 | 'item_payment_date' => ['name' => 'payment_date', 'type' => 'integer'], |
||
122 | 'item_mc_gross' => ['name' => 'mc_gross', 'type' => 'float'], |
||
123 | 'item_mc_currency' => ['name' => 'mc_currency', 'type' => 'string'], |
||
124 | ]; |
||
125 | |||
126 | foreach ($data_ary as $data) |
||
127 | { |
||
128 | $get_last_transaction_sql_ary = $this->ppde_operator_transactions->sql_last_donation_ary($data['max_txn_id']); |
||
129 | $last_donation_data = $this->ppde_entity_transactions->get_data($this->ppde_operator_transactions->build_sql_donorlist_data($get_last_transaction_sql_ary), $last_donation_table_schema, 0, 0, true); |
||
130 | $this->actions_currency->set_currency_data_from_iso_code($last_donation_data[0]['mc_currency']); |
||
131 | |||
132 | $this->template->assign_block_vars('donorrow', [ |
||
133 | 'PPDE_DONOR_USERNAME' => $this->user_loader->get_username($data['user_id'], 'full', false, false, true), |
||
134 | 'PPDE_LAST_DONATED_AMOUNT' => $this->actions_currency->format_currency((float) $last_donation_data[0]['mc_gross']), |
||
135 | 'PPDE_LAST_PAYMENT_DATE' => $this->user->format_date($last_donation_data[0]['payment_date']), |
||
136 | 'PPDE_TOTAL_DONATED_AMOUNT' => $this->actions_currency->format_currency((float) $data['amount']), |
||
137 | ]); |
||
138 | } |
||
139 | |||
140 | // Send all data to the template file |
||
141 | return $this->send_data_to_template(); |
||
142 | } |
||
214 |