Completed
Push — master ( e4b0a5...06e90c )
by Michael
10:53
created

function.xoInboxCount.php ➔ smarty_function_xoInboxCount()   C

Complexity

Conditions 8
Paths 13

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 26
nc 13
nop 2
dl 0
loc 39
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * xoInboxCount lets templates access private message inbox statistics for the current user
5
 *
6
 * Example: <{xoInboxCount assign='unread_count' total='inbox_total'}>
7
 *
8
 * Both assign and total parameters are optional. If neither is specified the unread count is displayed.
9
 * - assign = variable name to assign with the current unread message count
10
 * - total  = variable name to assign with the current inbox total
11
 *
12
 * @param $params
13
 * @param $smarty
14
 * @return null
15
 */
16
function smarty_function_xoInboxCount($params, &$smarty)
17
{
18
    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...
19
20
    if (!isset($xoopsUser) || !is_object($xoopsUser)) {
21
        return null;
22
    }
23
    $time = time();
24
    if (isset($_SESSION['xoops_inbox_count']) && @$_SESSION['xoops_inbox_count_expire'] > $time) {
25
        $totals['assign'] = (int)$_SESSION['xoops_inbox_count'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$totals was never initialized. Although not strictly required by PHP, it is generally a good practice to add $totals = 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...
26
        $totals['total'] = (int)$_SESSION['xoops_inbox_total'];
27
    } else {
28
        $pm_handler = xoops_getHandler('privmessage');
29
30
        $xoopsPreload = XoopsPreload::getInstance();
31
        $xoopsPreload->triggerEvent('core.class.smarty.xoops_plugins.xoinboxcount', array($pm_handler));
32
33
        $criteria = new CriteriaCompo(new Criteria('to_userid', $xoopsUser->getVar('uid')));
34
        $totals['total'] = $pm_handler->getCount($criteria);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$totals was never initialized. Although not strictly required by PHP, it is generally a good practice to add $totals = 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...
35
36
        $criteria->add(new Criteria('read_msg', 0));
37
        $totals['assign'] = $pm_handler->getCount($criteria);
38
39
        $_SESSION['xoops_inbox_count'] = $totals['assign'];
40
        $_SESSION['xoops_inbox_total'] = $totals['total'];
41
        $_SESSION['xoops_inbox_count_expire'] = $time + 60;
42
    }
43
44
    $printCount = true;
45
    foreach ($totals as $key => $count) {
46
        if (!empty($params[$key])) {
47
            $smarty->assign($params[$key], $count);
48
            $printCount = false;
49
        }
50
    }
51
    if ($printCount) {
52
        echo $totals['assign'];
53
    }
54
}
55