Completed
Push — master ( 26776f...d9604e )
by Michael
11:31
created

xhelp_blocks.php ➔ b_xhelp_recent_show()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 26
nc 6
nop 1
dl 0
loc 40
rs 8.439
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 15 and the first side effect is on line 4.

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
//$Id: xhelp_blocks.php,v 1.65 2005/11/30 19:25:00 eric_juden Exp $
3
if (!defined('XOOPS_ROOT_PATH')) {
4
    exit();
5
}
6
7
if (!defined('XHELP_CONSTANTS_INCLUDED')) {
8
    include_once(XOOPS_ROOT_PATH.'/modules/xhelp/include/constants.php');
9
}
10
11
include_once(XHELP_BASE_PATH.'/functions.php');
12
include_once(XHELP_CLASS_PATH.'/session.php');
13
xhelpIncludeLang('main');
14
15
function b_xhelp_open_show($options)
16
{
17
    global $xoopsUser;
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...
18
19
    $max_char_in_title = $options[0];
20
21
    if($xoopsUser){
22
        $uid = $xoopsUser->getVar('uid');   // Get uid
23
        $block = array();
24
        $hTickets =& xhelpGetHandler('ticket');  // Get ticket handler
25
        $hStaff =& xhelpGetHandler('staff');
26
        if($isStaff =& $hStaff->isStaff($xoopsUser->getVar('uid'))){
27
            $crit = new CriteriaCompo(new Criteria('ownership', $uid));
28
            $crit->add(new Criteria('status', 2, '<'));
29
            $crit->setOrder('DESC');
30
            $crit->setSort('priority, posted');
31
            $crit->setLimit(5);
32
            $tickets =& $hTickets->getObjects($crit);
33
34
            foreach($tickets as $ticket){
35
                $overdue = false;
36
                if($ticket->isOverdue()){
37
                    $overdue = true;
38
                }
39
                $block['ticket'][] = array('id'=>$ticket->getVar('id'),
40
                                           'uid'=>$ticket->getVar('uid'),
41
                                           'subject'=>$ticket->getVar('subject'),
42
                                           'truncSubject'=>xoops_substr($ticket->getVar('subject'), 0, $max_char_in_title),
43
                                           'description'=>$ticket->getVar('description'),
44
                //'department'=>$department->getVar('department'),
45
                                           'priority'=>$ticket->getVar('priority'),
46
                                           'status'=>$ticket->getVar('status'),
47
                                           'posted'=>$ticket->posted(),
48
                //'ownership'=>$owner->getVar('uname'),
49
                                           'closedBy'=>$ticket->getVar('closedBy'),
50
                                           'totalTimeSpent'=>$ticket->getVar('totalTimeSpent'),
51
                //'uname'=>$user->getVar('uname'),
52
                                           'userinfo'=>XOOPS_URL . '/userinfo.php?uid=' . $ticket->getVar('uid'),
53
                //'ownerinfo'=>XOOPS_URL . '/userinfo.php?uid=' . $ticket->getVar('ownership'),
54
                                           'url'=>XOOPS_URL . '/modules/xhelp/ticket.php?id=' . $ticket->getVar('id'),
55
                                           'overdue' => $overdue);
56
            }
57
58
            $block['isStaff'] = true;
59
            $block['viewAll'] = XOOPS_URL . '/modules/xhelp/index.php?op=staffViewAll';
60
            $block['viewAllText'] = _MB_XHELP_TEXT_VIEW_ALL_OPEN;
61
            $block['priorityText'] = _MB_XHELP_TEXT_PRIORITY;
62
            $block['noTickets'] = _MB_XHELP_TEXT_NO_TICKETS;
63
        } else {
64
            $crit = new CriteriaCompo(new Criteria('uid', $uid));
65
            $crit->add(new Criteria('status', 2, '<'));
66
            $crit->setOrder('DESC');
67
            $crit->setSort('priority, posted');
68
            $crit->setLimit(5);
69
            $tickets =& $hTickets->getObjects($crit);
70
            $hDepartments =& xhelpGetHandler('department');
71
72
            foreach($tickets as $ticket){
73
                //$department =& $hDepartments->get($ticket->getVar('department'));
74
                $block['ticket'][] = array('id'=>$ticket->getVar('id'),
75
                                           'uid'=>$ticket->getVar('uid'),
76
                                           'subject'=>$ticket->getVar('subject'),
77
                                           'truncSubject'=>xoops_substr($ticket->getVar('subject'), 0, $max_char_in_title),
78
                                           'description'=>$ticket->getVar('description'),
79
                //'department'=>($department->getVar('department'),
80
                                           'priority'=>$ticket->getVar('priority'),
81
                                           'status'=>$ticket->getVar('status'),
82
                                           'posted'=>$ticket->posted(),
83
                //'ownership'=>$owner->getVar('uname'),
84
                                           'closedBy'=>$ticket->getVar('closedBy'),
85
                                           'totalTimeSpent'=>$ticket->getVar('totalTimeSpent'),
86
                //'uname'=>$user->getVar('uname'),
87
                                           'userinfo'=>XOOPS_URL . '/userinfo.php?uid=' . $ticket->getVar('uid'),
88
                //'ownerinfo'=>XOOPS_URL . '/userinfo.php?uid=' . $ticket->getVar('ownership'),
89
                                           'url'=>XOOPS_URL . '/modules/xhelp/ticket.php?id=' . $ticket->getVar('id'));
90
            }
91
        }
92
        $block['numTickets'] = count($tickets);
93
        $block['noTickets'] = _MB_XHELP_TEXT_NO_TICKETS;
94
        unset($tickets);
95
        $block['picPath'] = XOOPS_URL . '/modules/xhelp/images/';
96
97
        return $block;
98
    }
99
}
100
101
function b_xhelp_performance_show($options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
{
103
    global $xoopsUser, $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...
104
    $dirname = 'xhelp';
0 ignored issues
show
Unused Code introduced by
$dirname 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...
105
    $block = array();
106
107
    if (!$xoopsUser) {
108
        return false;
109
    }
110
111
    //Determine if the GD library is installed
112
    $block['use_img'] = function_exists("imagecreatefrompng");
113
114
    $xoopsModule =& xhelpGetModule();
115
116
    if ($xoopsUser->isAdmin($xoopsModule->getVar('mid'))) {
117
        $sql = sprintf(
118
            "SELECT COUNT(*) as TicketCount, d.department, d.id FROM %s t INNER JOIN %s d ON t.department = d.id  INNER JOIN %s s ON t.status = s.id WHERE s.state = 1 GROUP BY d.department, d.id ORDER BY d.department",
119
        $xoopsDB->prefix('xhelp_tickets'), $xoopsDB->prefix('xhelp_departments'), $xoopsDB->prefix('xhelp_status'));
120
    } else {
121
        $sql = sprintf(
122
            "SELECT COUNT(*) as TicketCount, d.department, d.id FROM %s t INNER JOIN %s j on t.department = j.department INNER JOIN %s d ON t.department = d.id INNER JOIN %s s on t.status = s.id WHERE s.state = 1 AND j.uid = %u GROUP BY d.department, d.id",
123
        $xoopsDB->prefix('xhelp_tickets'), $xoopsDB->prefix('xhelp_jstaffdept'), $xoopsDB->prefix('xhelp_departments'), $xoopsDB->prefix('xhelp_status'), $xoopsUser->getVar('uid'));
124
    }
125
126
    $ret = $xoopsDB->query($sql);
127
128
    $depts = array();
129
    $max_open = 0;
130
    while ($myrow = $xoopsDB->fetchArray($ret)) {
131
        $max_open = max($max_open,$myrow['TicketCount']);
132
        $url = xhelpMakeURI(XHELP_BASE_URL.'/index.php', array('op'=>'staffViewAll', 'dept'=>$myrow['id'], 'state'=>1));
133
        $depts[] = array('id'=>$myrow['id'], 'tickets'=>$myrow['TicketCount'], 'name'=>$myrow['department'], 'url'=>$url);
134
    }
135
136
    if (count($depts) == 0) {
137
        return false;
138
    }
139
140
    if ($block['use_img']) {
141
        //Retrieve the image path for each department
142
        for($i = 0; $i < count($depts); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
143
            $depts[$i]['img'] = _xhelp_getDeptImg($depts[$i]['id'], $depts[$i]['tickets'], $max_open, $i);
144
        }
145
    }
146
147
    $block['departments'] = $depts;
148
149
    return $block;
150
}
151
152
function _xhelp_getDeptImg($dept, $tickets, $max, $counter = 0)
153
{
154
    $dept = intval($dept);
155
    $tickets = intval($tickets);
156
    $max = intval($max);
157
    $counter = intval($counter);
158
159
    $width = 60;   //Width of resulting image
160
161
    $cachedir_local = XHELP_CACHE_PATH .'/';
162
    $cachedir_www = XHELP_CACHE_URL .'/';
163
    $imgdir = XHELP_IMAGE_PATH.'/';
164
    $filename = "xhelp_perf_$dept.png";
165
166
    $colors = array('green', 'orange', 'red', 'blue');
167
168
    if (!is_file($cachedir_local.$filename)) {
169
        //Generate Progress Image
170
        $cur_color  = $colors[$counter % count($colors)];
171
        $bg         = @imagecreatefrompng($imgdir.'dept-bg.png');
172
        $fill       = @imagecreatefrompng($imgdir."dept-$cur_color.png");
173
        $bg_cap     = @imagecreatefrompng($imgdir.'dept-bg-cap.png');
174
        $fill_cap   = @imagecreatefrompng($imgdir.'dept-fill-cap.png');
175
        $fill_width = round((($width - imagesx($bg_cap)) * $tickets) / $max) - imagesx($fill_cap);
176
177
        $image = imagecreatetruecolor($width, imagesy($bg));
178
        imagecopy($image, $bg, 0, 0, 0, 0, imagesx($bg), $width - imagesx($bg_cap));
179
        imagecopy($image, $bg_cap, $width - imagesx($bg_cap), 0, 0, 0, imagesx($bg_cap), imagesy($bg_cap));
180
        imagecopy($image, $fill, 0, 0, 0, 0, $fill_width, imagesy($fill));
181
        imagecopy($image, $fill_cap, $fill_width, 0, 0, 0, imagesx($fill_cap), imagesy($fill_cap));
182
183
        imagepng($image, $cachedir_local.$filename);
184
    }
185
186
    return ($cachedir_www.$filename);
187
}
188
189
function b_xhelp_recent_show($options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
b_xhelp_recent_show uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
190
{
191
    if(!isset($_COOKIE['xhelp_recent_tickets'])){
192
        return false;
193
    } else {
194
        $tmp   = $_COOKIE['xhelp_recent_tickets'];
195
    }
196
197
    $block = array();
198
199
    if (strlen($tmp) > 0) {
200
        $tmp2 = explode(',', $tmp);
201
202
        $crit    = new Criteria('id', "(". $tmp . ")", 'IN', 't');
203
        $hTicket = xhelpGetHandler('ticket');
204
        $tickets = $hTicket->getObjects($crit, true);
205
206
        foreach ($tmp2 as $ele) {
207
            if (isset($tickets[intval($ele)])) {
208
                $ticket =& $tickets[intval($ele)];
209
210
                $overdue = false;
211
                if($ticket->isOverdue()){
212
                    $overdue = true;
213
                }
214
215
                $block['tickets'][] = array('id' => $ticket->getVar('id'),
216
                            'trim_subject' => xoops_substr($ticket->getVar('subject'), 0, 25),
217
                            'subject' => $ticket->getVar('subject'),
218
                            'url' => XOOPS_URL . '/modules/xhelp/ticket.php?id='.$ticket->getVar('id'),
219
                            'overdue' => $overdue);
220
            }
221
        }
222
        $block['ticketcount'] = count($tickets);
223
224
        return $block;
225
    } else {
226
        return false;
227
    }
228
}
229
230
function b_xhelp_actions_show()
0 ignored issues
show
Coding Style introduced by
b_xhelp_actions_show uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
b_xhelp_actions_show uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
231
{
232
233
    $_xhelpSession = new Session();
0 ignored issues
show
Unused Code introduced by
$_xhelpSession 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...
234
    global $ticketInfo, $xoopsUser, $xoopsModule, $xoopsModuleConfig, $ticketInfo, $staff, $xoopsConfig;
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...
235
236
    $module_handler =& xoops_gethandler('module');
237
    $config_handler =& xoops_gethandler('config');
238
    $member_handler =& xoops_gethandler('member');
239
    $hTickets       =& xhelpGetHandler('ticket');
240
    $hMembership    =& xhelpGetHandler('membership');
241
    $hStaff         =& xhelpGetHandler('staff');
242
    $hDepartment    =& xhelpGetHandler('department');
243
244
    //Don't show block for anonymous users or for non-staff members
245
    if (!$xoopsUser) {
246
        return false;
247
    }
248
249
    //Don't show block if outside the xhelp module'
250
    if (!isset($xoopsModule) || $xoopsModule->getVar('dirname') != 'xhelp') {
251
        return false;
252
    }
253
254
    $block = array();
255
256
    $myPage = $_SERVER['PHP_SELF'];
257
    $currentPage = substr(strrchr($myPage, "/"), 1);
258
    if(($currentPage <> 'ticket.php') || ($xoopsModuleConfig['xhelp_staffTicketActions'] <> 2)){
259
        return false;
260
    }
261
262
    if(isset($_GET['id'])){
263
        $block['ticketid'] = intval($_GET['id']);
264
    } else {
265
        return false;
266
    }
267
268
    //Use Global $ticketInfo object (if exists)
269
    if (!isset($ticketInfo)) {
270
        $ticketInfo =& $hTickets->get($block['ticketid']);
271
    }
272
273
    if($xoopsModuleConfig['xhelp_staffTicketActions'] == 2){
274
        $aOwnership = array();
275
        $aOwnership[] = array('uid' => 0,
276
                              'uname' => _XHELP_NO_OWNER);
277
        if(isset($staff)){
278 View Code Duplication
            foreach($staff as $stf){
279
                //** BTW - Need to have a way to get all XoopsUser objects for the staff in 1 shot
280
                //$own =& $member_handler->getUser($stf->getVar('uid'));    // Create user object
281
                $aOwnership[] = array('uid'=>$stf->getVar('uid'),
282
                                              'uname'=>'');
283
                $all_users[$stf->getVar('uid')] = '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$all_users was never initialized. Although not strictly required by PHP, it is generally a good practice to add $all_users = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
284
            }
285
        } else {
286
            return false;
287
        }
288
289
        $xoopsDB =& XoopsDatabaseFactory::getDatabaseConnection();
290
        $users = array();
291
292
        //@Todo - why is this query here instead of using a function or the XoopsMemberHandler?
293
        $sql = sprintf("SELECT uid, uname, name FROM %s WHERE uid IN (%s)", $xoopsDB->prefix('users'), implode(array_keys($all_users), ','));
0 ignored issues
show
Bug introduced by
The variable $all_users does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
294
        $ret = $xoopsDB->query($sql);
295
        $displayName = $xoopsModuleConfig['xhelp_displayName'];
296
        while($member = $xoopsDB->fetchArray($ret)){
297
            if(($displayName == 2) && ($member['name'] <> '')){
298
                $users[$member['uid']] = $member['name'];
299
            } else {
300
                $users[$member['uid']] = $member['uname'];
301
            }
302
        }
303
304 View Code Duplication
        for($i=0;$i<count($aOwnership);$i++){
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
305
            if(isset($users[$aOwnership[$i]['uid']])){
306
                $aOwnership[$i]['uname'] = $users[$aOwnership[$i]['uid']];
307
            }
308
        }
309
        $block['ownership'] = $aOwnership;
310
    }
311
312
    $block['imagePath'] = XHELP_IMAGE_URL.'/';
313
    $block['xhelp_priorities']      = array(1, 2, 3, 4, 5);
314
    $block['xhelp_priorities_desc'] = array('5' => _XHELP_PRIORITY5, '4' => _XHELP_PRIORITY4,'3' => _XHELP_PRIORITY3, '2' => _XHELP_PRIORITY2, '1' => _XHELP_PRIORITY1);
315
    $block['ticket_priority']  = $ticketInfo->getVar('priority');
316
    $block['ticket_status']    = $ticketInfo->getVar('status');
317
    $block['xhelp_status0']    = _XHELP_STATUS0;
318
    $block['xhelp_status1']    = _XHELP_STATUS1;
319
    $block['xhelp_status2']    = _XHELP_STATUS2;
320
    $block['ticket_ownership'] = $ticketInfo->getVar('ownership');
321
322
    $block['xhelp_has_changeOwner'] = false;
323
    if($ticketInfo->getVar('uid') == $xoopsUser->getVar('uid')){
324
        $block['xhelp_has_addResponse'] = true;
325
    } else {
326
        $block['xhelp_has_addResponse'] = false;
327
    }
328
    $block['xhelp_has_editTicket'] = false;
329
    $block['xhelp_has_deleteTicket'] = false;
330
    $block['xhelp_has_changePriority'] = false;
331
    $block['xhelp_has_changeStatus'] = false;
332
    $block['xhelp_has_editResponse'] = false;
333
    $block['xhelp_has_mergeTicket'] = false;
334
    $rowspan = 2;
335
    $checkRights = array(
336
    XHELP_SEC_TICKET_OWNERSHIP => array('xhelp_has_changeOwner', false),
337
    XHELP_SEC_RESPONSE_ADD => array('xhelp_has_addResponse', false),
338
    XHELP_SEC_TICKET_EDIT => array('xhelp_has_editTicket', true),
339
    XHELP_SEC_TICKET_DELETE => array('xhelp_has_deleteTicket', true),
340
    XHELP_SEC_TICKET_MERGE => array('xhelp_has_mergeTicket', true),
341
    XHELP_SEC_TICKET_PRIORITY => array('xhelp_has_changePriority', false),
342
    XHELP_SEC_TICKET_STATUS => array('xhelp_has_changeStatus', false),
343
    XHELP_SEC_RESPONSE_EDIT => array('xhelp_has_editResponse', false),
344
    XHELP_SEC_FILE_DELETE => array('xhelp_has_deleteFile', false),
345
    XHELP_SEC_FAQ_ADD => array('xhelp_has_addFaq', false),
346
    XHELP_SEC_TICKET_TAKE_OWNERSHIP => array('xhelp_has_takeOwnership', false));
347
348
     
349
    $checkStaff =& $hStaff->getByUid($xoopsUser->getVar('uid'));
350
    // See if this user is accepted for this ticket
351
    $hTicketEmails =& xhelpGetHandler('ticketEmails');
352
    $crit = new CriteriaCompo(new Criteria('ticketid', $ticketInfo->getVar('id')));
353
    $crit->add(new Criteria('uid', $xoopsUser->getVar('uid')));
354
    $ticketEmails =& $hTicketEmails->getObjects($crit);
355
356
    //Retrieve all departments
357
    $crit = new Criteria('','');
358
    $crit->setSort('department');
359
    $alldepts = $hDepartment->getObjects($crit);
360
    $aDept = array();
361
    foreach($alldepts as $dept){
362
        $aDept[$dept->getVar('id')] = $dept->getVar('department');
363
    }
364
    unset($alldepts);
365
    $block['departments'] =& $aDept;
366
    $block['departmentid'] = $ticketInfo->getVar('department');
367
368
    foreach ($checkRights as $right=>$desc) {
369
        if(($right == XHELP_SEC_RESPONSE_ADD) && count($ticketEmails > 0)){
370
            $block[$desc[0]] = true;
371
            continue;
372
        }
373
        if(($right == XHELP_SEC_TICKET_STATUS) && count($ticketEmails > 0)){
374
            $block[$desc[0]] = true;
375
            continue;
376
        }
377 View Code Duplication
        if ($hasRights = $checkStaff->checkRoleRights($right, $ticketInfo->getVar('department'))) {
0 ignored issues
show
Unused Code introduced by
$hasRights 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...
378
            $block[$desc[0]] = true;
379
            if ($desc[1]) {
380
                $rowspan ++;
381
            }
382
        }
383
384
    }
385
386
    $block['xhelp_actions_rowspan'] = $rowspan;
387
388
    $hStatus =& xhelpGetHandler('status');
389
    $crit = new Criteria('', '');
390
    $crit->setSort('description');
391
    $crit->setOrder('ASC');
392
    $statuses =& $hStatus->getObjects($crit);
393
    $aStatuses = array();
394 View Code Duplication
    foreach($statuses as $status){
395
        $aStatuses[$status->getVar('id')] = array('id' => $status->getVar('id'),
396
                                                  'desc' => $status->getVar('description'),
397
                                                  'state' => $status->getVar('state'));
398
    }
399
400
    $block['statuses'] = $aStatuses;
401
402
    return $block;
403
}
404
405
function b_xhelp_actions_edit($options)
406
{
407
    $form = "<table>";
408
    $form .= "<tr>";
409
    $form .= "<td>" . _MB_XHELP_TRUNCATE_TITLE . "</td>";
410
    $form .= "<td>" . "<input type='text' name='options[]' value='" . $options[0] . "' /></td>";
411
    $form .= "</tr>";
412
    $form .= "</table>";
413
414
    return $form;
415
}
416
417
function b_xhelp_mainactions_show($options)
418
{
419
420
    global $xoopsUser, $xhelp_isStaff;
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...
421
    // @todo - use the constant here if possible instead of the raw string
422
    $dirname = 'xhelp';
0 ignored issues
show
Unused Code introduced by
$dirname 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...
423
    $block['linkPath'] = XHELP_BASE_URL.'/';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$block was never initialized. Although not strictly required by PHP, it is generally a good practice to add $block = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
424
    $block['imagePath'] = XHELP_IMAGE_URL.'/';
425
    $block['menustyle'] = $options[0];
426
    $block['showicon'] = !$block['menustyle'] && $options[1];
427
    $block['startitem'] = !$block['menustyle'] ? '<li>' : '';
428
    $block['enditem'] = !$block['menustyle'] ? '</li>' : '';
429
    $block['startblock'] = !$block['menustyle'] ? '<ul>' : '<table cellspacing="0"><tr><td id="usermenu">';
430
    $block['endblock'] = !$block['menustyle'] ? '</ul>' : '</td></tr></table>';
431
    $block['savedSearches'] = false;
432
    $block['items'][0] = array( 'link' => 'anon_addTicket.php', 'image' => 'addTicket.png', 'text' => _XHELP_MENU_LOG_TICKET );
433
434
    if($xoopsUser){
435
        $block['items'][0] = array( 'link' => 'index.php', 'image' => 'main.png', 'text' => _XHELP_MENU_MAIN );
436
        $block['items'][1] = array( 'link' => 'addTicket.php', 'image' => 'addTicket.png', 'text' => _XHELP_MENU_LOG_TICKET );
437
        $block['items'][2] = array( 'link' => 'index.php?viewAllTickets=1&op=userViewAll', 'image' => 'ticket.png', 'text' => _XHELP_MENU_ALL_TICKETS );
438
        $hStaff =& xhelpGetHandler('staff');
439
        if($xhelp_staff =& $hStaff->getByUid($xoopsUser->getVar('uid'))){
440
            $block['whoami'] = 'staff';
441
            $block['items'][3] = array( 'link' => 'search.php', 'image' => 'search2.png', 'text' => _XHELP_MENU_SEARCH );
442
            $block['items'][4] = array( 'link' => 'profile.php', 'image' => 'profile.png', 'text' => _XHELP_MENU_MY_PROFILE );
443
            $block['items'][2] = array( 'link' => 'index.php?viewAllTickets=1&op=staffViewAll', 'image' => 'ticket.png', 'text' => _XHELP_MENU_ALL_TICKETS );
444
            $hSavedSearch =& xhelpGetHandler('savedSearch');
445
            $savedSearches =& $hSavedSearch->getByUid($xoopsUser->getVar('uid'));
446
            $aSavedSearches = array();
447
            foreach($savedSearches as $sSearch){
448
                $aSavedSearches[$sSearch->getVar('id')] = array('id' => $sSearch->getVar('id'),
449
                                                                'name' => $sSearch->getVar('name'),
450
                                                                'search' => $sSearch->getVar('search'),
451
                                                                'pagenav_vars' => $sSearch->getVar('pagenav_vars'));
452
            }
453
            $block['savedSearches'] = (count($aSavedSearches) < 1) ? false : $aSavedSearches;
454
        }
455
    }
456
457
    return $block;
458
}
459
460
function b_xhelp_mainactions_edit($options)
461
{
462
    $form  = "<table border='0'>";
463
464
    // Menu style
465
    $form .= "<tr><td>"._MB_XHELP_TEXT_MENUSTYLE."</td><td>";
466
    $form .= "<input type='radio' name='options[0]' value='0'".(($options[0]==0)?" checked='checked'":"")." />"._MB_XHELP_OPTION_MENUSTYLE1."";
467
    $form .= "<input type='radio' name='options[0]' value='1'".(($options[0]==1)?" checked='checked'":"")." />"._MB_XHELP_OPTION_MENUSTYLE2."</td></tr>";
468
469
    // Auto select last items
470
    $form .= "<tr><td>"._MB_XHELP_TEXT_SHOWICON."</td><td>";
471
    $form .= "<input type='radio' name='options[1]' value='0'".(($options[1]==0)?" checked='checked'":"")." />"._NO."";
472
    $form .= "<input type='radio' name='options[1]' value='1'".(($options[1]==1)?" checked='checked'":"")." />"._YES."</td></tr>";
473
474
    $form .= "</table>";
475
476
    return $form;
477
}
478