/class/model/stats.php

https://gitlab.com/VoyaTrax/vtCMS3 · PHP · 105 lines · 61 code · 5 blank · 39 comment · 18 complexity · a269007cff1adf7afab1f49a2e2bb5ce MD5 · raw file

  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. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. */
  10. /**
  11. * Object stats handler class.
  12. *
  13. * @copyright The XOOPS Project http://sourceforge.net/projects/xoops/
  14. * @license GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
  15. * @package class
  16. * @subpackage model
  17. * @since 2.3.0
  18. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  19. * @version $Id: stats.php 10318 2012-12-04 20:08:57Z dugris $
  20. */
  21. defined('XOOPS_ROOT_PATH') or die('Restricted access');
  22. /**
  23. * Object stats handler class.
  24. *
  25. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  26. *
  27. * {@link XoopsObjectAbstract}
  28. */
  29. class XoopsModelStats extends XoopsModelAbstract
  30. {
  31. /**
  32. * count objects matching a condition
  33. *
  34. * @param CriteriaElement|null $criteria {@link CriteriaElement} to match
  35. * @return int count of objects
  36. */
  37. public function getCount(CriteriaElement $criteria = null)
  38. {
  39. $field = '';
  40. $groupby = false;
  41. if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  42. if ($criteria->getGroupby() != '') {
  43. $groupby = true;
  44. $field = $criteria->getGroupby() . ", ";
  45. }
  46. }
  47. $sql = "SELECT {$field} COUNT(*) FROM `{$this->handler->table}`";
  48. if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  49. $sql .= ' ' . $criteria->renderWhere();
  50. if ($criteria->getGroupby() != '') {
  51. $sql .= ' GROUP BY (' . $criteria->getGroupby() . ')';
  52. }
  53. }
  54. $result = $this->handler->db->query($sql);
  55. if (!$result) {
  56. return 0;
  57. }
  58. if ($groupby == false) {
  59. list ($count) = $this->handler->db->fetchRow($result);
  60. return $count;
  61. } else {
  62. $ret = array();
  63. while (list ($id, $count) = $this->handler->db->fetchRow($result)) {
  64. $ret[$id] = $count;
  65. }
  66. return $ret;
  67. }
  68. }
  69. /**
  70. * get counts matching a condition
  71. *
  72. * @param CriteriaElement|null $criteria {@link CriteriaElement} to match
  73. * @return array of counts
  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->getGroupby()) {
  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. return $ret;
  98. }
  99. }