donors.php ➔ b_donations_donors_edit()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 16
nop 1
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 44 and the first side effect is on line 34.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/************************************************************************/
3
/* Donations - Paypal financial management module for Xoops 2           */
4
/* Copyright (c) 2016 XOOPS Project                                     */
5
/* http://dev.xoops.org/modules/xfmod/project/?group_id=1060            */
6
/* 
7
/************************************************************************/
8
/*                                                                      */
9
/* Based on NukeTreasury for PHP-Nuke - by Dave Lawrence AKA Thrash     */
10
/* NukeTreasury - Financial management for PHP-Nuke                     */
11
/* Copyright (c) 2004 by Dave Lawrence AKA Thrash                       */
12
/*                       [email protected]                         */
13
/*                       [email protected]                          */
14
/*                                                                      */
15
/************************************************************************/
16
/*                                                                      */
17
/* This program is free software; you can redistribute it and/or modify */
18
/* it under the terms of the GNU General Public License as published by */
19
/* the Free Software Foundation; either version 2 of the License.       */
20
/*                                                                      */
21
/* This program is distributed in the hope that it will be useful, but  */
22
/* WITHOUT ANY WARRANTY; without even the implied warranty of           */
23
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU     */
24
/* General Public License for more details.                             */
25
/*                                                                      */
26
/* You should have received a copy of the GNU General Public License    */
27
/* along with this program; if not, write to the Free Software          */
28
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  */
29
/* USA                                                                  */
30
/************************************************************************/
31
32
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
34
$moduleDirName = basename(dirname(__DIR__));
35
36
xoops_loadLanguage('main', $moduleDirName);
37
38
include_once XOOPS_ROOT_PATH . "/modules/{$moduleDirName}/include/functions.php";
39
40
/**
41
 * @param $options
42
 * @return array
43
 */
44
function b_donations_donors_show($options)
45
{
46
    global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
47
48
    $tr_config = configInfo();
49
    //determine the currency
50
    $PP_CURR_CODE = explode('|', $tr_config['pp_curr_code']); // [USD,GBP,JPY,CAD,EUR]
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
    $PP_CURR_CODE = $PP_CURR_CODE[0];
52
    $currencySign    = defineCurrency($PP_CURR_CODE);
53
54
    $dmshowdate = $options[1];
55
    $dmshowamt  = $options[2];
56
    $block      = array();
57
    $swingd     = $tr_config['swing_day'];
58
59
    if (($swingd < 0) or ($swingd > 31)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
60
        $swingd = 6;
61
    }
62
63
    if (date('d') >= $swingd) {
64
        $query_Recordset1 = "SELECT custom AS muser_id, option_selection1 as showname, DATE_FORMAT(payment_date, '%b %e') AS date, CONCAT('" . $currencySign . "',SUM(mc_gross)) AS amt FROM " . $xoopsDB->prefix('donations_transactions') . " WHERE (payment_date >= DATE_FORMAT(NOW(),'%Y-%m-" . $swingd . "')) GROUP BY txn_id ORDER BY payment_date DESC";
65
    } else {
66
        $query_Recordset1 = "SELECT custom AS muser_id, option_selection1 as showname, DATE_FORMAT(payment_date, '%b-%e') AS date, CONCAT('" . $currencySign . "',SUM(mc_gross)) AS amt FROM " . $xoopsDB->prefix('donations_transactions') . " WHERE (payment_date < DATE_FORMAT(NOW(), '%Y-%m-" . $swingd . "')) AND payment_date > DATE_FORMAT(SUBDATE(NOW(),INTERVAL " . $swingd . " DAY), '%Y-%m-" . $swingd . "') GROUP BY txn_id ORDER BY payment_date DESC";
67
    }
68
69
    $Recordset1           = $xoopsDB->query($query_Recordset1);
70
    $totalRows_Recordset1 = $xoopsDB->getRowsNum($Recordset1);
0 ignored issues
show
Unused Code introduced by
$totalRows_Recordset1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
71
72
    $ROWS_DONATORS = '';
73
    // Fill out the donators table tag
74
    while (false!=($row_Recordset1 = $xoopsDB->fetchArray($Recordset1))) {
75
        if ($row_Recordset1['amt'] > $currencySign . '0') {
76
            $ROWS_DONATORS .= '<tr>';
77
            $ROWS_DONATORS .= "<td style=\"font-weight: bold;\">&nbsp; ";
78
79
            $muser_id = $row_Recordset1['muser_id'];
80 View Code Duplication
            if (strcmp($row_Recordset1['showname'], 'Yes') == 0 && ($userfoin = mgetUserInfo($muser_id))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
                $ROWS_DONATORS .= "<a href='" . XOOPS_URL . '/userinfo.php?uid=' . $userfoin->getVar('uid') . "'>" . xdshorten($userfoin->getVar('uname')) . "</a>\n";
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $userfoin (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
82
            } else {
83
                $ROWS_DONATORS .= _MB_DON_ANONYMOUS_SHORT;
84
            }
85
86
            $ROWS_DONATORS .= "</td>\n";
87
            if ($dmshowamt) {
88
                $ROWS_DONATORS .= "<td style=\"width: 2px;\">&nbsp;</td>\n";
89
                $ROWS_DONATORS .= "<td style=\"width: 55px; font-weight: bold;\">&nbsp;&nbsp;";
90
                $ROWS_DONATORS .= $row_Recordset1['amt'];
91
                $ROWS_DONATORS .= "</td>\n";
92
            }
93
            if ($dmshowdate) {
94
                $ROWS_DONATORS .= "<td style=\"width: 2px;\">&nbsp;</td>\n";
95
                $ROWS_DONATORS .= "<td style=\"font-weight: bold;\">&nbsp;&nbsp;";
96
                $ROWS_DONATORS .= $row_Recordset1['date'];
97
                $ROWS_DONATORS .= "</td>\n";
98
            }
99
            $ROWS_DONATORS .= "</tr>\n";
100
        }
101
    }
102
103
    // Ok, output the page
104
105
    $block['showamt']  = $dmshowamt;
106
    $block['showdate'] = $dmshowdate;
107
    $block['list']     = $ROWS_DONATORS;
108
    $block['amount']   = _MB_DON_AMOUNT;
109
    $block['date']     = _MB_DON_DATE;
110
    $block['name']     = _MB_DON_NAME;
111
112
    return $block;
113
}
114
115
/**
116
 * @param     $var
117
 * @param int $len
118
 * @return string
119
 */
120
function xdshorten($var, $len = 10)
121
{
122
    $var = trim($var);
123
    if (empty($var)) {
124
        return '';
125
    }
126
    if (strlen($var) < $len) {
127
        return $var;
128
    }
129
130
    if (preg_match("/(.{1,$len})\s/", $var, $match)) {
131
        return $match [1] . '...';
132
    } else {
133
        return substr($var, 0, $len) . '...';
134
    }
135
}
136
137
/**
138
 * @param $options
139
 * @return string
140
 */
141
function b_donations_donors_edit($options)
142
{
143
    $form = _MB_DON_NUM_DONORS . ":&nbsp;<input type='text' name='options[0]' value='" . $options[0] . "'  size='4'/>";
144
    $form .= '<br />' . _MB_DON_REVEAL_DATES . ":&nbsp;<select size='1' name='options[1]'><option value='1'";
145
    if ($options[1] == 1) {
146
        $form .= ' selected';
147
    }
148
    $form .= ' />' . _YES . "</option><option value='0'";
149
    if ($options[1] == 0) {
150
        $form .= ' selected';
151
    }
152
    $form .= ' />' . _NO . '</option></select>';
153
    $form .= '<br />' . _MB_DON_REVEAL_AMOUNTS . ":&nbsp;<select size='1' name='options[2]'><option value='1'";
154
    if ($options[2] == 1) {
155
        $form .= ' selected';
156
    }
157
    $form .= ' />' . _YES . "</option><option value='0'";
158
    if ($options[2] == 0) {
159
        $form .= ' selected';
160
    }
161
    $form .= ' />' . _NO . '</option></select>';
162
163
    return $form;
164
}
165