PageRenderTime 6ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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/**
 13 * Object stats handler class.
 14 *
 15 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
 16 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 17 * @package         class
 18 * @subpackage      model
 19 * @since           2.3.0
 20 * @author          Taiwen Jiang <phppp@users.sourceforge.net>
 21 * @version         $Id: stats.php 10318 2012-12-04 20:08:57Z dugris $
 22 */
 23
 24defined('XOOPS_ROOT_PATH') or die('Restricted access');
 25
 26/**
 27 * Object stats handler class.
 28 *
 29 * @author Taiwen Jiang <phppp@users.sourceforge.net>
 30 *
 31 * {@link XoopsObjectAbstract}
 32 */
 33
 34class XoopsModelStats extends XoopsModelAbstract
 35{
 36    /**
 37     * count objects matching a condition
 38     *
 39     * @param CriteriaElement|null $criteria {@link CriteriaElement} to match
 40     * @return int count of objects
 41     */
 42    public function getCount(CriteriaElement $criteria = null)
 43    {
 44        $field = '';
 45        $groupby = false;
 46        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
 47            if ($criteria->getGroupby() != '') {
 48                $groupby = true;
 49                $field = $criteria->getGroupby() . ", ";
 50            }
 51        }
 52        $sql = "SELECT {$field} COUNT(*) FROM `{$this->handler->table}`";
 53        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
 54            $sql .= ' ' . $criteria->renderWhere();
 55            if ($criteria->getGroupby() != '') {
 56                $sql .= ' GROUP BY (' . $criteria->getGroupby() . ')';
 57            }
 58        }
 59        $result = $this->handler->db->query($sql);
 60        if (!$result) {
 61            return 0;
 62        }
 63        if ($groupby == false) {
 64            list ($count) = $this->handler->db->fetchRow($result);
 65            return $count;
 66        } else {
 67            $ret = array();
 68            while (list ($id, $count) = $this->handler->db->fetchRow($result)) {
 69                $ret[$id] = $count;
 70            }
 71            return $ret;
 72        }
 73    }
 74
 75    /**
 76     * get counts matching a condition
 77     *
 78     * @param CriteriaElement|null $criteria {@link CriteriaElement} to match
 79     * @return array of counts
 80     */
 81    public function getCounts(CriteriaElement $criteria = null)
 82    {
 83        $ret = array();
 84        $sql_where = '';
 85        $limit = null;
 86        $start = null;
 87        $groupby_key = $this->handler->keyName;
 88        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
 89            $sql_where = $criteria->renderWhere();
 90            $limit = $criteria->getLimit();
 91            $start = $criteria->getStart();
 92            if ($groupby = $criteria->getGroupby()) {
 93                $groupby_key = $groupby;
 94            }
 95        }
 96        $sql = "SELECT {$groupby_key}, COUNT(*) AS count" . " FROM `{$this->handler->table}`" . " {$sql_where}" . " GROUP BY {$groupby_key}";
 97        if (!$result = $this->handler->db->query($sql, $limit, $start)) {
 98            return $ret;
 99        }
100        while (list ($id, $count) = $this->handler->db->fetchRow($result)) {
101            $ret[$id] = $count;
102        }
103        return $ret;
104    }
105}