| Total Complexity | 44 |
| Total Lines | 427 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like admin_transactions_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.
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 admin_transactions_controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class admin_transactions_controller extends admin_main |
||
| 38 | { |
||
| 39 | public $ppde_operator; |
||
| 40 | protected $adm_relative_path; |
||
| 41 | protected $auth; |
||
| 42 | protected $entry_count; |
||
| 43 | protected $last_page_offset; |
||
| 44 | protected $php_ext; |
||
| 45 | protected $phpbb_admin_path; |
||
| 46 | protected $phpbb_root_path; |
||
| 47 | protected $ppde_actions; |
||
| 48 | protected $table_prefix; |
||
| 49 | protected $table_ppde_transactions; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor |
||
| 53 | * |
||
| 54 | * @param auth $auth Authentication object |
||
| 55 | * @param config $config Config object |
||
| 56 | * @param ContainerInterface $container Service container interface |
||
| 57 | * @param language $language Language user object |
||
| 58 | * @param log $log The phpBB log system |
||
| 59 | * @param core_actions $ppde_actions PPDE actions object |
||
| 60 | * @param transactions $ppde_operator_transactions Operator object |
||
| 61 | * @param request $request Request object |
||
| 62 | * @param template $template Template object |
||
| 63 | * @param user $user User object. |
||
| 64 | * @param string $adm_relative_path phpBB admin relative path |
||
| 65 | * @param string $phpbb_root_path phpBB root path |
||
| 66 | * @param string $php_ext phpEx |
||
| 67 | * @param string $table_prefix The table prefix |
||
| 68 | * @param string $table_ppde_transactions Name of the table used to store data |
||
| 69 | * |
||
| 70 | * @access public |
||
| 71 | */ |
||
| 72 | public function __construct(auth $auth, config $config, ContainerInterface $container, language $language, log $log, core_actions $ppde_actions, transactions $ppde_operator_transactions, request $request, template $template, user $user, $adm_relative_path, $phpbb_root_path, $php_ext, $table_prefix, $table_ppde_transactions) |
||
| 94 | ); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Display the transactions list |
||
| 99 | * |
||
| 100 | * @param string $id Module id |
||
| 101 | * @param string $mode Module categorie |
||
| 102 | * @param string $action Action name |
||
| 103 | * |
||
| 104 | * @return void |
||
| 105 | * @access public |
||
| 106 | */ |
||
| 107 | public function display_transactions($id, $mode, $action) |
||
| 108 | { |
||
| 109 | // Set up general vars |
||
| 110 | $args = array(); |
||
| 111 | $start = $this->request->variable('start', 0); |
||
| 112 | $deletemark = $this->request->is_set('delmarked') ? $this->request->variable('delmarked', false) : false; |
||
| 113 | $deleteall = $this->request->is_set('delall') ? $this->request->variable('delall', false) : false; |
||
| 114 | $marked = $this->request->variable('mark', array(0)); |
||
| 115 | $txn_approve = $this->request->is_set('approve'); |
||
| 116 | $txn_approved = $this->request->variable('txn_errors_approved', 0); |
||
| 117 | // Sort keys |
||
| 118 | $sort_days = $this->request->variable('st', 0); |
||
| 119 | $sort_key = $this->request->variable('sk', 't'); |
||
| 120 | $sort_dir = $this->request->variable('sd', 'd'); |
||
| 121 | |||
| 122 | // Prepares args for entries deletion |
||
| 123 | if (($deletemark || $deleteall) && $this->auth->acl_get('a_ppde_manage')) |
||
| 124 | { |
||
| 125 | $action = 'delete'; |
||
| 126 | $args = array( |
||
| 127 | 'hidden_fields' => array( |
||
| 128 | 'start' => $start, |
||
| 129 | 'delall' => $deleteall, |
||
| 130 | 'delmarked' => $deletemark, |
||
| 131 | 'mark' => $marked, |
||
| 132 | 'st' => $sort_days, |
||
| 133 | 'sk' => $sort_key, |
||
| 134 | 'sd' => $sort_dir, |
||
| 135 | 'i' => $id, |
||
| 136 | 'mode' => $mode, |
||
| 137 | ), |
||
| 138 | ); |
||
| 139 | } |
||
| 140 | |||
| 141 | if ($txn_approve) |
||
| 142 | { |
||
| 143 | $transaction_id = $this->request->variable('id', 0); |
||
| 144 | $action = 'approve'; |
||
| 145 | $args = array( |
||
| 146 | 'hidden_fields' => array( |
||
| 147 | 'approve' => true, |
||
| 148 | 'id' => $transaction_id, |
||
| 149 | 'txn_errors_approved' => $txn_approved, |
||
| 150 | ), |
||
| 151 | ); |
||
| 152 | } |
||
| 153 | |||
| 154 | $action = $this->do_action($action, $args); |
||
| 155 | |||
| 156 | if (!$action) |
||
| 157 | { |
||
| 158 | /** @type \phpbb\pagination $pagination */ |
||
| 159 | $pagination = $this->container->get('pagination'); |
||
| 160 | |||
| 161 | // Sorting |
||
| 162 | $limit_days = array(0 => $this->language->lang('ALL_ENTRIES'), 1 => $this->language->lang('1_DAY'), 7 => $this->language->lang('7_DAYS'), 14 => $this->language->lang('2_WEEKS'), 30 => $this->language->lang('1_MONTH'), 90 => $this->language->lang('3_MONTHS'), 180 => $this->language->lang('6_MONTHS'), 365 => $this->language->lang('1_YEAR')); |
||
| 163 | $sort_by_text = array('txn' => $this->language->lang('PPDE_DT_SORT_TXN_ID'), 'u' => $this->language->lang('PPDE_DT_SORT_DONORS'), 'ipn' => $this->language->lang('PPDE_DT_SORT_IPN_STATUS'), 'ipn_test' => $this->language->lang('PPDE_DT_SORT_IPN_TYPE'), 'ps' => $this->language->lang('PPDE_DT_SORT_PAYMENT_STATUS'), 't' => $this->language->lang('SORT_DATE')); |
||
| 164 | $sort_by_sql = array('txn' => 'txn.txn_id', 'u' => 'u.username_clean', 'ipn' => 'txn.confirmed', 'ipn_test' => 'txn.test_ipn', 'ps' => 'txn.payment_status', 't' => 'txn.payment_date'); |
||
| 165 | |||
| 166 | $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = ''; |
||
| 167 | gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param); |
||
| 168 | |||
| 169 | // Define where and sort sql for use in displaying transactions |
||
| 170 | $sql_where = ($sort_days) ? (time() - ($sort_days * 86400)) : 0; |
||
| 171 | $sql_sort = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC'); |
||
| 172 | |||
| 173 | $keywords = $this->request->variable('keywords', '', true); |
||
| 174 | $keywords_param = !empty($keywords) ? '&keywords=' . urlencode(htmlspecialchars_decode($keywords)) : ''; |
||
| 175 | |||
| 176 | // Grab log data |
||
| 177 | $log_data = array(); |
||
| 178 | $log_count = 0; |
||
| 179 | |||
| 180 | $this->view_txn_log($log_data, $log_count, (int) $this->config['topics_per_page'], $start, $sql_where, $sql_sort, $keywords); |
||
| 181 | |||
| 182 | $base_url = $this->u_action . '&' . $u_sort_param . $keywords_param; |
||
| 183 | $pagination->generate_template_pagination($base_url, 'pagination', 'start', $log_count, (int) $this->config['topics_per_page'], $start); |
||
| 184 | |||
| 185 | $this->template->assign_vars(array( |
||
| 186 | 'S_CLEARLOGS' => $this->auth->acl_get('a_ppde_manage'), |
||
| 187 | 'S_KEYWORDS' => $keywords, |
||
| 188 | 'S_LIMIT_DAYS' => $s_limit_days, |
||
| 189 | 'S_SORT_KEY' => $s_sort_key, |
||
| 190 | 'S_SORT_DIR' => $s_sort_dir, |
||
| 191 | 'S_TXN' => $mode, |
||
| 192 | 'U_ACTION' => $this->u_action . '&' . $u_sort_param . $keywords_param . '&start=' . $start, |
||
| 193 | )); |
||
| 194 | |||
| 195 | array_map(array($this, 'display_log_assign_template_vars'), $log_data); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Do action regarding the value of $action |
||
| 201 | * |
||
| 202 | * @param string $action Requested action |
||
| 203 | * @param array $args Arguments required for the action |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | * @access private |
||
| 207 | */ |
||
| 208 | private function do_action($action, $args) |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * View log |
||
| 305 | * |
||
| 306 | * @param array &$log The result array with the logs |
||
| 307 | * @param mixed &$log_count If $log_count is set to false, we will skip counting all entries in the |
||
| 308 | * database. Otherwise an integer with the number of total matching entries is returned. |
||
| 309 | * @param int $limit Limit the number of entries that are returned |
||
| 310 | * @param int $offset Offset when fetching the log entries, f.e. when paginating |
||
| 311 | * @param int $limit_days |
||
| 312 | * @param string $sort_by SQL order option, e.g. 'l.log_time DESC' |
||
| 313 | * @param string $keywords Will only return log entries that have the keywords in log_operation or log_data |
||
| 314 | * |
||
| 315 | * @return int Returns the offset of the last valid page, if the specified offset was invalid (too high) |
||
| 316 | * @access private |
||
| 317 | */ |
||
| 318 | private function view_txn_log(&$log, &$log_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'txn.payment_date DESC', $keywords = '') |
||
| 319 | { |
||
| 320 | $count_logs = ($log_count !== false); |
||
| 321 | |||
| 322 | $log = $this->get_logs($count_logs, $limit, $offset, $limit_days, $sort_by, $keywords); |
||
| 323 | $log_count = $this->get_log_count(); |
||
| 324 | |||
| 325 | return $this->get_valid_offset(); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param bool $count_logs |
||
| 330 | * @param int $limit |
||
| 331 | * @param int $offset |
||
| 332 | * @param int $log_time |
||
| 333 | * @param string $sort_by |
||
| 334 | * @param string $keywords |
||
| 335 | * |
||
| 336 | * @return array $log |
||
| 337 | * @access private |
||
| 338 | */ |
||
| 339 | private function get_logs($count_logs = true, $limit = 0, $offset = 0, $log_time = 0, $sort_by = 'txn.payment_date DESC', $keywords = '') |
||
| 340 | { |
||
| 341 | $this->entry_count = 0; |
||
| 342 | $this->last_page_offset = $offset; |
||
| 343 | $url_ary = array(); |
||
| 344 | |||
| 345 | if ($this->get_container_entity()->is_in_admin() && $this->phpbb_admin_path) |
||
| 346 | { |
||
| 347 | $url_ary['profile_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=users&mode=overview'); |
||
| 348 | $url_ary['txn_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=-skouat-ppde-acp-ppde_module&mode=transactions'); |
||
| 349 | |||
| 350 | } |
||
| 351 | else |
||
| 352 | { |
||
| 353 | $url_ary['profile_url'] = append_sid($this->phpbb_root_path . 'memberlist.' . $this->php_ext, 'mode=viewprofile'); |
||
| 354 | $url_ary['txn_url'] = ''; |
||
| 355 | } |
||
| 356 | |||
| 357 | $get_logs_sql_ary = $this->ppde_operator->get_logs_sql_ary($keywords, $sort_by, $log_time); |
||
| 358 | |||
| 359 | if ($count_logs) |
||
| 360 | { |
||
| 361 | $this->entry_count = $this->ppde_operator->query_sql_count($get_logs_sql_ary, 'txn.transaction_id'); |
||
| 362 | |||
| 363 | if ($this->entry_count == 0) |
||
| 364 | { |
||
| 365 | // Save the queries, because there are no logs to display |
||
| 366 | $this->last_page_offset = 0; |
||
| 367 | |||
| 368 | return array(); |
||
| 369 | } |
||
| 370 | |||
| 371 | // Return the user to the last page that is valid |
||
| 372 | while ($this->last_page_offset >= $this->entry_count) |
||
| 373 | { |
||
| 374 | $this->last_page_offset = max(0, $this->last_page_offset - $limit); |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | return $this->ppde_operator->build_log_ary($get_logs_sql_ary, $url_ary, $limit, $this->last_page_offset); |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @return integer |
||
| 383 | */ |
||
| 384 | public function get_log_count() |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return integer |
||
| 391 | */ |
||
| 392 | public function get_valid_offset() |
||
| 393 | { |
||
| 394 | return ($this->last_page_offset) ? (int) $this->last_page_offset : 0; |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Set log output vars for display in the template |
||
| 399 | * |
||
| 400 | * @param array $row |
||
| 401 | * |
||
| 402 | * @return void |
||
| 403 | * @access protected |
||
| 404 | */ |
||
| 405 | protected function display_log_assign_template_vars($row) |
||
| 418 | )); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Set output vars for display in the template |
||
| 423 | * |
||
| 424 | * @param array $data |
||
| 425 | * |
||
| 426 | * @return void |
||
| 427 | * @access protected |
||
| 428 | */ |
||
| 429 | protected function action_assign_template_vars($data) |
||
| 464 | )); |
||
| 465 | } |
||
| 466 | } |
||
| 467 |