1 | <?php |
||
28 | class Stats extends XoopsModelAbstract |
||
29 | { |
||
30 | /** |
||
31 | * count objects matching a condition |
||
32 | * |
||
33 | * @param CriteriaElement|null $criteria criteria to match |
||
34 | * |
||
35 | * @return int count of objects |
||
36 | */ |
||
37 | 8 | public function getCount(CriteriaElement $criteria = null) |
|
38 | { |
||
39 | 8 | $qb = \Xoops::getInstance()->db()->createXoopsQueryBuilder(); |
|
40 | |||
41 | 8 | $groupBy = false; |
|
42 | 8 | if (isset($criteria) && ($criteria instanceof CriteriaElement)) { |
|
43 | 3 | $temp = $criteria->getGroupBy(); |
|
44 | 3 | if (!empty($temp)) { |
|
45 | $qb->select($temp); |
||
46 | $groupBy = true; |
||
47 | } |
||
48 | } |
||
49 | 8 | if (!$groupBy) { |
|
50 | 8 | $qb->select('COUNT(*)'); |
|
51 | } else { |
||
52 | $qb->addSelect('COUNT(*)'); |
||
53 | } |
||
54 | |||
55 | 8 | $qb->from($this->handler->table, null); |
|
56 | 8 | if (isset($criteria) && ($criteria instanceof CriteriaElement)) { |
|
57 | 3 | $qb = $criteria->renderQb($qb); |
|
58 | } |
||
59 | try { |
||
60 | 8 | $result = $qb->execute(); |
|
61 | 8 | if (!$result) { |
|
62 | return 0; |
||
63 | } |
||
64 | } catch (\Exception $e) { |
||
65 | \Xoops::getInstance()->events()->triggerEvent('core.exception', $e); |
||
66 | return 0; |
||
67 | } |
||
68 | |||
69 | 8 | if ($groupBy == false) { |
|
|
|||
70 | 8 | list ($count) = $result->fetch(\PDO::FETCH_NUM); |
|
71 | 8 | 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 | * get counts matching a condition |
||
83 | * |
||
84 | * @param CriteriaElement|null $criteria criteria to match |
||
85 | * |
||
86 | * @return array of counts |
||
87 | */ |
||
88 | 2 | public function getCounts(CriteriaElement $criteria = null) |
|
118 | } |
||
119 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.