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 string[] $params
13: * @param Smarty $smarty
14: * @return null
15: */
16: function smarty_function_xoInboxCount($params, $smarty)
17: {
18: global $xoopsUser;
19:
20: if (!isset($xoopsUser) || !is_object($xoopsUser)) {
21: return null;
22: }
23:
24: // unset cache in pm programs so stale cache won't show inconsistencies
25: $freshRead = isset($GLOBALS['xoInboxCountFresh']);
26: $pmScripts = array('pmlite', 'readpmsg', 'viewpmsg');
27: if (in_array(basename($_SERVER['SCRIPT_FILENAME'], '.php'), $pmScripts)) {
28: if (!$freshRead) {
29: unset($_SESSION['xoops_inbox_count'], $_SESSION['xoops_inbox_total'], $_SESSION['xoops_inbox_count_expire']);
30: $GLOBALS['xoInboxCountFresh'] = true;
31: }
32: }
33:
34: $time = time();
35: if (isset($_SESSION['xoops_inbox_count']) && (isset($_SESSION['xoops_inbox_count_expire']) && $_SESSION['xoops_inbox_count_expire'] > $time)) {
36: $totals['assign'] = (int)$_SESSION['xoops_inbox_count'];
37: $totals['total'] = (int)$_SESSION['xoops_inbox_total'];
38: } else {
39: /** @var \XoopsPrivmessageHandler $pm_handler */
40: $pm_handler = xoops_getHandler('privmessage');
41:
42: $xoopsPreload = XoopsPreload::getInstance();
43: $xoopsPreload->triggerEvent('core.class.smarty.xoops_plugins.xoinboxcount', array($pm_handler));
44:
45: $criteria = new CriteriaCompo(new Criteria('to_userid', $xoopsUser->getVar('uid')));
46: $totals['total'] = $pm_handler->getCount($criteria);
47:
48: $criteria->add(new Criteria('read_msg', 0));
49: $totals['assign'] = $pm_handler->getCount($criteria);
50:
51: $_SESSION['xoops_inbox_count'] = $totals['assign'];
52: $_SESSION['xoops_inbox_total'] = $totals['total'];
53: $_SESSION['xoops_inbox_count_expire'] = $time + 60;
54: }
55:
56: $printCount = true;
57: foreach ($totals as $key => $count) {
58: if (!empty($params[$key])) {
59: $smarty->assign($params[$key], $count);
60: $printCount = false;
61: }
62: }
63: if ($printCount) {
64: echo $totals['assign'];
65: }
66: }
67: