| Conditions | 32 |
| Paths | 398 |
| Total Lines | 361 |
| Code Lines | 229 |
| Lines | 43 |
| Ratio | 11.91 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 19 | public function main($id, $mode) |
||
| 20 | { |
||
| 21 | global $config, $db, $user, $template, $table_prefix, $request, $phpbb_root_path, $phpEx; |
||
| 22 | |||
| 23 | $this->user = $user; |
||
| 24 | $this->phpbb_root_path = $phpbb_root_path; |
||
| 25 | $this->php_ext = $phpEx; |
||
| 26 | |||
| 27 | $user->add_lang_ext('tas2580/paypal', 'common'); |
||
| 28 | |||
| 29 | $sql = 'SELECT * |
||
| 30 | FROM ' . $table_prefix . 'paypal_config'; |
||
| 31 | $result = $db->sql_query($sql); |
||
| 32 | $paypal_config = $db->sql_fetchrow($result); |
||
| 33 | |||
| 34 | switch ($mode) |
||
| 35 | { |
||
| 36 | case 'settings': |
||
| 37 | $this->tpl_name = 'acp_paypal_body'; |
||
| 38 | $this->page_title = $user->lang('ACP_PAYPAL_TITLE'); |
||
| 39 | |||
| 40 | add_form_key('acp_paypal'); |
||
| 41 | |||
| 42 | // delete amount |
||
| 43 | View Code Duplication | if ($request->is_set('delete')) |
|
| 44 | { |
||
| 45 | $id = $request->variable('delete', 0); |
||
| 46 | if (confirm_box(true)) |
||
| 47 | { |
||
| 48 | $sql = 'DELETE FROM ' . $table_prefix . 'paypal_amount WHERE amount_id = ' . (int) $id; |
||
| 49 | $result = $db->sql_query($sql); |
||
| 50 | trigger_error($user->lang['AMOUNT_DELETED'] . adm_back_link($this->u_action)); |
||
| 51 | } |
||
| 52 | else |
||
| 53 | { |
||
| 54 | confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array( |
||
| 55 | 'action' => 'delete', |
||
| 56 | 'id' => $id)) |
||
| 57 | ); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | $error = array(); |
||
| 62 | // Form is submitted |
||
| 63 | if ($request->is_set_post('submit')) |
||
| 64 | { |
||
| 65 | View Code Duplication | if (!check_form_key('acp_paypal')) |
|
| 66 | { |
||
| 67 | trigger_error($user->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING); |
||
| 68 | } |
||
| 69 | |||
| 70 | $data['paypal_email'] = $request->variable('paypal_email', '', true); |
||
| 71 | $data['paypal_sandbox'] = $request->variable('paypal_sandbox', 0); |
||
| 72 | $data['paypal_currency'] = $request->variable('paypal_currency', ''); |
||
| 73 | $data['paypal_title'] = $request->variable('paypal_title', '', true); |
||
| 74 | $data['paypal_text'] = $request->variable('paypal_text', '', true); |
||
| 75 | |||
| 76 | // Validate user input |
||
| 77 | $validate_array = array( |
||
| 78 | 'paypal_email' => array('email'), |
||
| 79 | 'paypal_title' => array('string', true, 0, 255), |
||
| 80 | ); |
||
| 81 | |||
| 82 | if (!function_exists('validate_data')) |
||
| 83 | { |
||
| 84 | include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext); |
||
| 85 | } |
||
| 86 | $error = validate_data($data, $validate_array); |
||
| 87 | |||
| 88 | if (!sizeof($error)) |
||
| 89 | { |
||
| 90 | $bbcode_options = 7; |
||
| 91 | generate_text_for_storage($data['paypal_text'], $bbcode_uid, $bbcode_bitfield, $bbcode_options, true, true, true); |
||
| 92 | |||
| 93 | $sql_data = array( |
||
| 94 | 'paypal_email' => $data['paypal_email'], |
||
| 95 | 'paypal_title' => $data['paypal_title'], |
||
| 96 | 'paypal_currency' => $data['paypal_currency'], |
||
| 97 | 'paypal_text' => $data['paypal_text'], |
||
| 98 | 'bbcode_uid' => $bbcode_uid, |
||
| 99 | 'bbcode_bitfield' => $bbcode_bitfield, |
||
| 100 | 'paypal_sandbox' => $data['paypal_sandbox'], |
||
| 101 | ); |
||
| 102 | $sql = 'UPDATE ' . $table_prefix . 'paypal_config SET |
||
| 103 | ' . $db->sql_build_array('UPDATE', $sql_data); |
||
| 104 | |||
| 105 | $db->sql_query($sql); |
||
| 106 | trigger_error($user->lang('ACP_SAVED') . adm_back_link($this->u_action)); |
||
| 107 | } |
||
| 108 | else |
||
| 109 | { |
||
| 110 | $template->assign_vars(array( |
||
| 111 | 'ERROR' => implode('<br />', $error), |
||
| 112 | 'U_ACTION' => $this->u_action, |
||
| 113 | 'PAYPAL_EMAIL' => $data['paypal_email'], |
||
| 114 | 'PAYPAL_TITLE' => $data['paypal_title'], |
||
| 115 | 'PAYPAL_SANDBOX' => $data['paypal_sandbox'], |
||
| 116 | 'PAYPAL_TEXT' => $data['paypal_text'], |
||
| 117 | 'CURRENCY_CODE' => $this->currency_code_select($data['paypal_currency']), |
||
| 118 | )); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | // Form is submitted |
||
| 123 | if ($request->is_set_post('submit_amount')) |
||
| 124 | { |
||
| 125 | $add_amount = $request->variable('add_amount', 0); |
||
| 126 | $sql_data = array( |
||
| 127 | 'amount_value' => $add_amount, |
||
| 128 | ); |
||
| 129 | $sql = 'INSERT INTO ' . $table_prefix . 'paypal_amount |
||
| 130 | ' . $db->sql_build_array('INSERT', $sql_data); |
||
| 131 | $db->sql_query($sql); |
||
| 132 | trigger_error($user->lang('ACP_SAVED') . adm_back_link($this->u_action)); |
||
| 133 | } |
||
| 134 | |||
| 135 | $sql = 'SELECT * |
||
| 136 | FROM ' . $table_prefix . 'paypal_amount |
||
| 137 | ORDER BY amount_value'; |
||
| 138 | $result = $db->sql_query($sql); |
||
| 139 | while ($row = $db->sql_fetchrow($result)) |
||
| 140 | { |
||
| 141 | $template->assign_block_vars('amounts', array( |
||
| 142 | 'AMOUNT' => number_format($row['amount_value'] / 100, 2), |
||
| 143 | 'U_DELETE' => $this->u_action . '&delete=' . $row['amount_id'], |
||
| 144 | )); |
||
| 145 | } |
||
| 146 | |||
| 147 | if (!sizeof($error)) |
||
| 148 | { |
||
| 149 | $paypal_text = generate_text_for_edit($paypal_config['paypal_text'], $paypal_config['bbcode_uid'], $paypal_config['bbcode_bitfield']); |
||
| 150 | |||
| 151 | $template->assign_vars(array( |
||
| 152 | 'U_ACTION' => $this->u_action, |
||
| 153 | 'PAYPAL_EMAIL' => $paypal_config['paypal_email'], |
||
| 154 | 'PAYPAL_TITLE' => $paypal_config['paypal_title'], |
||
| 155 | 'PAYPAL_SANDBOX' => $paypal_config['paypal_sandbox'], |
||
| 156 | 'PAYPAL_TEXT' => $paypal_text['text'], |
||
| 157 | 'CURRENCY_CODE' => $this->currency_code_select($paypal_config['paypal_currency']), |
||
| 158 | )); |
||
| 159 | } |
||
| 160 | |||
| 161 | break; |
||
| 162 | |||
| 163 | case 'items': |
||
| 164 | $this->tpl_name = 'acp_paypal_items_body'; |
||
| 165 | $this->page_title = $user->lang('ACP_PAYPAL_ITEMS'); |
||
| 166 | |||
| 167 | $item_id = $request->variable('item_id', 0); |
||
| 168 | $action = $request->variable('action', ''); |
||
| 169 | switch ($action) |
||
| 170 | { |
||
| 171 | case 'add': |
||
| 172 | add_form_key('acp_paypal'); |
||
| 173 | $template->assign_vars(array( |
||
| 174 | 'ITEM_FORM_NAME' => $this->user->lang('ACP_ADD_ITEM'), |
||
| 175 | 'U_ACTION' => $this->u_action . '&action=add', |
||
| 176 | 'S_FORM' => true, |
||
| 177 | 'ITEM_NAME' => $request->variable('item_name', '', true), |
||
| 178 | )); |
||
| 179 | break; |
||
| 180 | |||
| 181 | case 'edit': |
||
| 182 | add_form_key('acp_paypal'); |
||
| 183 | $sql = 'SELECT * |
||
| 184 | FROM ' . $table_prefix . 'paypal_items |
||
| 185 | WHERE item_id = ' . (int) $item_id; |
||
| 186 | $result = $db->sql_query($sql); |
||
| 187 | $data = $db->sql_fetchrow($result); |
||
| 188 | |||
| 189 | $item_text = generate_text_for_edit($data['item_text'], $data['bbcode_uid'], $data['bbcode_bitfield']); |
||
| 190 | |||
| 191 | $template->assign_vars(array( |
||
| 192 | 'ITEM_FORM_NAME' => $this->user->lang('ACP_EDIT_ITEM'), |
||
| 193 | 'U_ACTION' => $this->u_action . '&action=edit&item_id=' . $data['item_id'], |
||
| 194 | 'S_FORM' => true, |
||
| 195 | 'ITEM_NAME' => $data['item_name'], |
||
| 196 | 'ITEM_TEXT' => $item_text['text'], |
||
| 197 | )); |
||
| 198 | break; |
||
| 199 | |||
| 200 | case 'delete': |
||
| 201 | |||
| 202 | if (confirm_box(true)) |
||
| 203 | { |
||
| 204 | $sql = 'DELETE FROM ' . $table_prefix . 'paypal_items WHERE item_id = ' . (int) $item_id; |
||
| 205 | $result = $db->sql_query($sql); |
||
| 206 | trigger_error($user->lang['ITEM_DELETED'] . adm_back_link($this->u_action)); |
||
| 207 | } |
||
| 208 | else |
||
| 209 | { |
||
| 210 | confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array( |
||
| 211 | 'action' => 'delete', |
||
| 212 | 'i' => $id, |
||
| 213 | 'item_id' => $item_id)) |
||
| 214 | ); |
||
| 215 | } |
||
| 216 | |||
| 217 | break; |
||
| 218 | } |
||
| 219 | switch ($action) |
||
| 220 | { |
||
| 221 | case 'add': |
||
| 222 | case 'edit': |
||
| 223 | |||
| 224 | // Form is submitted |
||
| 225 | if ($request->is_set_post('submit')) |
||
| 226 | { |
||
| 227 | View Code Duplication | if (!check_form_key('acp_paypal')) |
|
| 228 | { |
||
| 229 | trigger_error($user->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING); |
||
| 230 | } |
||
| 231 | $data['item_name'] = $request->variable('item_name', '', true); |
||
| 232 | $data['item_text'] = $request->variable('item_text', '', true); |
||
| 233 | |||
| 234 | // Validate user input |
||
| 235 | $validate_array = array( |
||
| 236 | 'item_name' => array('string', false, 1, 255), |
||
| 237 | 'item_text' => array('string', false, 1, 99999), |
||
| 238 | ); |
||
| 239 | |||
| 240 | if (!function_exists('validate_data')) |
||
| 241 | { |
||
| 242 | include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext); |
||
| 243 | } |
||
| 244 | $error = validate_data($data, $validate_array); |
||
| 245 | if (!sizeof($error)) |
||
| 246 | { |
||
| 247 | $bbcode_options = 7; |
||
| 248 | generate_text_for_storage($data['item_text'], $bbcode_uid, $bbcode_bitfield, $bbcode_options, true, true, true); |
||
| 249 | $sql_data = array( |
||
| 250 | 'item_name' => $data['item_name'], |
||
| 251 | 'item_text' => $data['item_text'], |
||
| 252 | 'bbcode_uid' => $bbcode_uid, |
||
| 253 | 'bbcode_bitfield' => $bbcode_bitfield, |
||
| 254 | ); |
||
| 255 | if ($action == 'add') |
||
| 256 | { |
||
| 257 | $sql = 'INSERT INTO ' . $table_prefix . 'paypal_items |
||
| 258 | ' . $db->sql_build_array('INSERT', $sql_data); |
||
| 259 | $db->sql_query($sql); |
||
| 260 | trigger_error($user->lang('ACP_ITEM_ADD_SUCCESS') . adm_back_link($this->u_action)); |
||
| 261 | } |
||
| 262 | else if ($action == 'edit') |
||
| 263 | { |
||
| 264 | $sql = 'UPDATE ' . $table_prefix . 'paypal_items SET |
||
| 265 | ' . $db->sql_build_array('UPDATE', $sql_data) .' |
||
| 266 | WHERE item_id = ' . (int) $item_id; |
||
| 267 | $db->sql_query($sql); |
||
| 268 | trigger_error($user->lang('ACP_ITEM_EDIT_SUCCESS') . adm_back_link($this->u_action)); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | else |
||
| 272 | { |
||
| 273 | $template->assign_vars(array( |
||
| 274 | 'ERROR' => implode('<br />', $error), |
||
| 275 | 'ITEM_NAME' => $data['item_name'], |
||
| 276 | 'ITEM_TEXT' => $data['item_text'], |
||
| 277 | )); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | break; |
||
| 281 | |||
| 282 | default: |
||
| 283 | $ammount = array(); |
||
| 284 | $sql = 'SELECT item_id, donation_amount |
||
| 285 | FROM ' . $table_prefix . 'paypal_donations |
||
| 286 | ORDER BY item_name'; |
||
| 287 | $result = $db->sql_query($sql); |
||
| 288 | while ($row = $db->sql_fetchrow($result)) |
||
| 289 | { |
||
| 290 | if (isset($ammount[$row['item_id']])) |
||
| 291 | { |
||
| 292 | $ammount[$row['item_id']] += $row['donation_amount']; |
||
| 293 | } |
||
| 294 | else |
||
| 295 | { |
||
| 296 | $ammount[$row['item_id']] = $row['donation_amount']; |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | $sql = 'SELECT item_name, item_id |
||
| 301 | FROM ' . $table_prefix . 'paypal_items |
||
| 302 | ORDER BY item_name'; |
||
| 303 | $result = $db->sql_query($sql); |
||
| 304 | while ($row = $db->sql_fetchrow($result)) |
||
| 305 | { |
||
| 306 | $template->assign_block_vars('items', array( |
||
| 307 | 'ITEM' => $row['item_name'], |
||
| 308 | 'AMMOUNT' => (isset($ammount[$row['item_id']]) ? number_format($ammount[$row['item_id']], 2) : '0.00') . ' ' . $paypal_config['paypal_currency'], |
||
| 309 | 'U_EDIT' => $this->u_action . '&action=edit&item_id=' . $row['item_id'], |
||
| 310 | 'U_DELETE' => $this->u_action . '&action=delete&item_id=' . $row['item_id'], |
||
| 311 | )); |
||
| 312 | } |
||
| 313 | $template->assign_vars(array( |
||
| 314 | 'U_ACTION' => $this->u_action . '&action=add', |
||
| 315 | )); |
||
| 316 | |||
| 317 | break; |
||
| 318 | } |
||
| 319 | |||
| 320 | break; |
||
| 321 | |||
| 322 | case 'donations': |
||
| 323 | $this->tpl_name = 'acp_paypal_donations_body'; |
||
| 324 | $this->page_title = $user->lang('ACP_PAYPAL_DONATIONS'); |
||
| 325 | |||
| 326 | $action = $request->variable('action', ''); |
||
| 327 | View Code Duplication | if ($action == 'delete') |
|
| 328 | { |
||
| 329 | $donation_id = $request->variable('donation_id', 0); |
||
| 330 | if (confirm_box(true)) |
||
| 331 | { |
||
| 332 | $sql = 'DELETE FROM ' . $table_prefix . 'paypal_donations WHERE donation_id = ' . (int) $donation_id; |
||
| 333 | $result = $db->sql_query($sql); |
||
| 334 | trigger_error($user->lang['DONATION_DELETED'] . adm_back_link($this->u_action)); |
||
| 335 | } |
||
| 336 | else |
||
| 337 | { |
||
| 338 | confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array( |
||
| 339 | 'action' => 'delete', |
||
| 340 | 'i' => $id, |
||
| 341 | 'donation_id' => $donation_id)) |
||
| 342 | ); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | $sql_array = array( |
||
| 347 | 'SELECT' => 'd.*, u.user_id, u.username, u.user_colour, i.item_name', |
||
| 348 | 'FROM' => array($table_prefix . 'paypal_donations' => 'd'), |
||
| 349 | 'LEFT_JOIN' => array( |
||
| 350 | array( |
||
| 351 | 'FROM' => array(USERS_TABLE => 'u'), |
||
| 352 | 'ON' => 'u.user_id = d.user_id' |
||
| 353 | ), |
||
| 354 | array( |
||
| 355 | 'FROM' => array($table_prefix . 'paypal_items' => 'i'), |
||
| 356 | 'ON' => 'i.item_id = d.item_id' |
||
| 357 | ) |
||
| 358 | ), |
||
| 359 | 'ORDER_BY' => 'd.donation_time DESC', |
||
| 360 | ); |
||
| 361 | $sql = $db->sql_build_query('SELECT', $sql_array); |
||
| 362 | $result = $db->sql_query($sql); |
||
| 363 | while ($row = $db->sql_fetchrow($result)) |
||
| 364 | { |
||
| 365 | $template->assign_block_vars('donations', array( |
||
| 366 | 'USER' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']), |
||
| 367 | 'ITEM' => $row['item_name'], |
||
| 368 | //'USER' => $row['item_name'], |
||
| 369 | 'TIME' => $this->user->format_date($row['donation_time']), |
||
| 370 | 'AMOUNT' => number_format($row['donation_amount'], 2) . ' ' . $paypal_config['paypal_currency'], |
||
| 371 | 'U_DELETE' => $this->u_action . '&action=delete&donation_id=' . $row['donation_id'], |
||
| 372 | )); |
||
| 373 | } |
||
| 374 | $template->assign_vars(array( |
||
| 375 | 'S_CURL' => function_exists('curl_init'), |
||
| 376 | )); |
||
| 377 | break; |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 414 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.