1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20:
21: 22: 23: 24: 25: 26: 27:
28: class XoopsModelStats extends XoopsModelAbstract
29: {
30: 31: 32: 33: 34: 35:
36: public function getCount(CriteriaElement $criteria = null)
37: {
38: $field = '';
39: $groupby = false;
40: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
41: if ($criteria->groupby != '') {
42: $groupby = true;
43: $field = $criteria->groupby . ', ';
44: }
45: }
46: $sql = "SELECT {$field} COUNT(*) FROM `{$this->handler->table}`";
47: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
48: $sql .= ' ' . $criteria->renderWhere();
49: $sql .= $criteria->getGroupby();
50: }
51: $result = $this->handler->db->query($sql);
52: if (!$result) {
53: return 0;
54: }
55: if ($groupby == false) {
56: list($count) = $this->handler->db->fetchRow($result);
57:
58: return $count;
59: } else {
60: $ret = array();
61: while (list($id, $count) = $this->handler->db->fetchRow($result)) {
62: $ret[$id] = $count;
63: }
64:
65: return $ret;
66: }
67: }
68:
69: 70: 71: 72: 73: 74:
75: public function getCounts(CriteriaElement $criteria = null)
76: {
77: $ret = array();
78: $sql_where = '';
79: $limit = null;
80: $start = null;
81: $groupby_key = $this->handler->keyName;
82: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
83: $sql_where = $criteria->renderWhere();
84: $limit = $criteria->getLimit();
85: $start = $criteria->getStart();
86: if ($groupby = $criteria->groupby) {
87: $groupby_key = $groupby;
88: }
89: }
90: $sql = "SELECT {$groupby_key}, COUNT(*) AS count" . " FROM `{$this->handler->table}`" . " {$sql_where}" . " GROUP BY {$groupby_key}";
91: if (!$result = $this->handler->db->query($sql, $limit, $start)) {
92: return $ret;
93: }
94: while (list($id, $count) = $this->handler->db->fetchRow($result)) {
95: $ret[$id] = $count;
96: }
97:
98: return $ret;
99: }
100: }
101: