Completed
Push — master ( a847dd...e68bf6 )
by Michael
39:50 queued 04:57
created

smarty_function_xoInboxCount()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 48
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 34
c 1
b 0
f 0
nc 13
nop 2
dl 0
loc 48
rs 8.1315
1
<?php
2
3
use Xoops\Core\Kernel\Criteria;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Criteria. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
4
use Xoops\Core\Kernel\CriteriaCompo;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, CriteriaCompo. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
6
/**
7
 * xoInboxCount lets templates access private message inbox statistics for the current user
8
 *
9
 * Example: {xoInboxCount assign='unread_count' total='inbox_total'}
10
 *
11
 * Both assign and total parameters are optional. If neither is specified the unread count is displayed.
12
 * - assign = variable name to assign with the current unread message count
13
 * - total  = variable name to assign with the current inbox total
14
 *
15
 * @param array                    $params parameters
16
 * @param Smarty_Internal_Template $smarty template
17
 * @return null
18
 */
19
function smarty_function_xoInboxCount($params, Smarty_Internal_Template $smarty)
20
{
21
    $assign = 'assign';
22
    $total = 'total';
23
    $inbox_count = 'xoops_inbox_count';
24
    $inbox_total = 'xoops_inbox_total';
25
    $inbox_expire = 'xoops_inbox_count_expire';
26
27
    $xoops = Xoops::getInstance();
28
    $session = $xoops->session();
29
30
    if (!$xoops->isUser()) {
31
        return;
32
    }
33
    $time = time();
34
    if ($session->has($inbox_count)
35
        && $session->has($inbox_expire)
36
        && $session->get($inbox_expire) > $time
37
    ) {
38
        $totals[$assign] = (int)$session->get($inbox_count);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$totals was never initialized. Although not strictly required by PHP, it is generally a good practice to add $totals = array(); before regardless.
Loading history...
39
        $totals[$total] = (int)$session->get($inbox_total);
40
    } else {
41
        $pmHandler = $xoops->getHandlerPrivateMessage();
42
        $eventArgs = [$pmHandler];
43
44
        $xoops->events()->triggerEvent('core.class.smarty.xoops_plugins.xoinboxcount', $eventArgs);
45
        $pmHandler = $eventArgs[0];
46
47
        $criteria = new CriteriaCompo(new Criteria('to_userid', $xoops->user->getVar('uid')));
0 ignored issues
show
Bug introduced by
It seems like $xoops->user->getVar('uid') can also be of type string[]; however, parameter $value of Xoops\Core\Kernel\Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        $criteria = new CriteriaCompo(new Criteria('to_userid', /** @scrutinizer ignore-type */ $xoops->user->getVar('uid')));
Loading history...
48
        $totals[$total] = $pmHandler->getCount($criteria);
49
50
        $criteria->add(new Criteria('read_msg', 0));
51
        $totals[$assign] = $pmHandler->getCount($criteria);
52
53
        $session->set($inbox_count, $totals[$assign]);
54
        $session->set($inbox_total, $totals[$total]);
55
        $session->set($inbox_expire, $time + 60);
56
    }
57
58
    $printCount = true;
59
    foreach ($totals as $key => $count) {
60
        if (!empty($params[$key])) {
61
            $smarty->assign($params[$key], $count);
62
            $printCount = false;
63
        }
64
    }
65
    if ($printCount) {
66
        echo $totals['assign'];
67
    }
68
}
69