1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits |
4
|
|
|
of supporting developers from this source code or any supporting source code |
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xoops\Core\Kernel\Model; |
13
|
|
|
|
14
|
|
|
use Xoops\Core\Kernel\CriteriaElement; |
15
|
|
|
use Xoops\Core\Kernel\XoopsModelAbstract; |
16
|
|
|
use Doctrine\DBAL\FetchMode; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Object stats handler class. |
20
|
|
|
* |
21
|
|
|
* @category Xoops\Core\Kernel\Model\Stats |
22
|
|
|
* @package Xoops\Core\Kernel |
23
|
|
|
* @author Taiwen Jiang <[email protected]> |
24
|
|
|
* @copyright 2000-2019 XOOPS Project (https://xoops.org) |
25
|
|
|
* @license GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
26
|
|
|
*/ |
27
|
|
|
class Stats extends XoopsModelAbstract |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* count objects matching a condition |
31
|
|
|
* |
32
|
|
|
* @param CriteriaElement|null $criteria criteria to match |
33
|
|
|
* |
34
|
|
|
* @return int count of objects |
35
|
|
|
*/ |
36
|
9 |
|
public function getCount(CriteriaElement $criteria = null) |
37
|
|
|
{ |
38
|
9 |
|
$qb = \Xoops::getInstance()->db()->createXoopsQueryBuilder(); |
39
|
|
|
|
40
|
9 |
|
$groupBy = false; |
41
|
9 |
|
if (isset($criteria) && ($criteria instanceof CriteriaElement)) { |
42
|
3 |
|
$temp = $criteria->getGroupBy(); |
43
|
3 |
|
if (!empty($temp)) { |
44
|
|
|
$qb->select($temp); |
45
|
|
|
$groupBy = true; |
46
|
|
|
} |
47
|
|
|
} |
48
|
9 |
|
if (!$groupBy) { |
49
|
9 |
|
$qb->select('COUNT(*)'); |
50
|
|
|
} else { |
51
|
|
|
$qb->addSelect('COUNT(*)'); |
52
|
|
|
} |
53
|
|
|
|
54
|
9 |
|
$qb->from($this->handler->table, null); |
55
|
9 |
|
if (isset($criteria) && ($criteria instanceof CriteriaElement)) { |
56
|
3 |
|
$qb = $criteria->renderQb($qb); |
57
|
|
|
} |
58
|
|
|
try { |
59
|
9 |
|
$result = $qb->execute(); |
60
|
9 |
|
if (!$result) { |
61
|
9 |
|
return 0; |
62
|
|
|
} |
63
|
|
|
} catch (\Exception $e) { |
64
|
|
|
\Xoops::getInstance()->events()->triggerEvent('core.exception', $e); |
65
|
|
|
return 0; |
66
|
|
|
} |
67
|
|
|
|
68
|
9 |
|
if ($groupBy == false) { |
|
|
|
|
69
|
9 |
|
list ($count) = $result->fetch(FetchMode::NUMERIC); |
70
|
9 |
|
return $count; |
71
|
|
|
} else { |
72
|
|
|
$ret = array(); |
73
|
|
|
while (list ($id, $count) = $result->fetch(FetchMode::NUMERIC)) { |
74
|
|
|
$ret[$id] = $count; |
75
|
|
|
} |
76
|
|
|
return $ret; |
|
|
|
|
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* get counts matching a condition |
82
|
|
|
* |
83
|
|
|
* @param CriteriaElement|null $criteria criteria to match |
84
|
|
|
* |
85
|
|
|
* @return array of counts |
86
|
|
|
*/ |
87
|
2 |
|
public function getCounts(CriteriaElement $criteria = null) |
88
|
|
|
{ |
89
|
2 |
|
$qb = \Xoops::getInstance()->db()->createXoopsQueryBuilder(); |
90
|
|
|
|
91
|
2 |
|
$ret = array(); |
92
|
2 |
|
$limit = null; |
|
|
|
|
93
|
2 |
|
$start = null; |
|
|
|
|
94
|
2 |
|
$groupby_key = $this->handler->keyName; |
95
|
2 |
|
if (isset($criteria) && ($criteria instanceof CriteriaElement)) { |
96
|
|
|
if ($groupBy = $criteria->getGroupBy()) { |
97
|
|
|
$groupby_key = $groupBy; |
98
|
|
|
} |
99
|
|
|
} |
100
|
2 |
|
$qb->select($groupby_key) |
101
|
2 |
|
->addSelect('COUNT(*)') |
102
|
2 |
|
->from($this->handler->table, null) |
103
|
2 |
|
->groupBy($groupby_key); |
104
|
|
|
|
105
|
2 |
|
if (isset($criteria) && ($criteria instanceof CriteriaElement)) { |
106
|
|
|
$qb = $criteria->renderQb($qb); |
107
|
|
|
} |
108
|
2 |
|
$result = $qb->execute(); |
109
|
2 |
|
if (!$result) { |
110
|
|
|
return $ret; |
111
|
|
|
} |
112
|
2 |
|
while (list ($id, $count) = $result->fetch(FetchMode::NUMERIC)) { |
113
|
2 |
|
$ret[$id] = $count; |
114
|
|
|
} |
115
|
2 |
|
return $ret; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.