1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Core\Kernel\Model;
13:
14: use Xoops\Core\Kernel\CriteriaElement;
15: use Xoops\Core\Kernel\XoopsModelAbstract;
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class Stats extends XoopsModelAbstract
29: {
30: 31: 32: 33: 34: 35: 36:
37: public function getCount(CriteriaElement $criteria = null)
38: {
39: $qb = \Xoops::getInstance()->db()->createXoopsQueryBuilder();
40:
41: $groupBy = false;
42: if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
43: $temp = $criteria->getGroupBy();
44: if (!empty($temp)) {
45: $qb->select($temp);
46: $groupBy = true;
47: }
48: }
49: if (!$groupBy) {
50: $qb->select('COUNT(*)');
51: } else {
52: $qb->addSelect('COUNT(*)');
53: }
54:
55: $qb->from($this->handler->table, null);
56: if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
57: $qb = $criteria->renderQb($qb);
58: }
59: try {
60: $result = $qb->execute();
61: if (!$result) {
62: return 0;
63: }
64: } catch (\Exception $e) {
65: \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
66: return 0;
67: }
68:
69: if ($groupBy == false) {
70: list ($count) = $result->fetch(\PDO::FETCH_NUM);
71: return $count;
72: } else {
73: $ret = array();
74: while (list ($id, $count) = $result->fetch(\PDO::FETCH_NUM)) {
75: $ret[$id] = $count;
76: }
77: return $ret;
78: }
79: }
80:
81: 82: 83: 84: 85: 86: 87:
88: public function getCounts(CriteriaElement $criteria = null)
89: {
90: $qb = \Xoops::getInstance()->db()->createXoopsQueryBuilder();
91:
92: $ret = array();
93: $limit = null;
94: $start = null;
95: $groupby_key = $this->handler->keyName;
96: if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
97: if ($groupBy = $criteria->getGroupBy()) {
98: $groupby_key = $groupBy;
99: }
100: }
101: $qb->select($groupby_key)
102: ->addSelect('COUNT(*)')
103: ->from($this->handler->table, null);
104:
105: if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
106: $qb = $criteria->renderQb($qb);
107: }
108: $result = $qb->execute();
109: if (!$result) {
110: return $ret;
111: }
112: while (list ($id, $count) = $result->fetch(\PDO::FETCH_NUM)) {
113: $ret[$id] = $count;
114: }
115: return $ret;
116: }
117: }
118: