<?php
#BEGIN_LICENSE
#-------------------------------------------------------------------------
# Module: HitCounter (c) 2009-2014 by Robert Campbell
#         (calguy1000@cmsmadesimple.org)
#  An addon module for CMS Made Simple to allow management of frontend
#  users, and their login process within a CMS Made Simple powered
#  website.
#
#-------------------------------------------------------------------------
# CMS - CMS Made Simple is (c) 2009 by Ted Kulp (wishy@cmsmadesimple.org)
# This projects homepage is: http://www.cmsmadesimple.org
#
#-------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# However, as a special exception to the GPL, this software is distributed
# as an addon module to CMS Made Simple.  You may not use this software
# in any Non GPL version of CMS Made simple, or in any version of CMS
# Made simple that does not indicate clearly and obviously in its admin
# section that the site was built with CMS Made simple.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------
#END_LICENSE
if( !isset($gCms) ) exit;

$limit = cge_utils::get_param($params,'limit',10);
$key1 = cge_utils::get_param($params,'key1');
$key2 = cge_utils::get_param($params,'key2');
if( $key1 == '' && $key2 == '' ) $key1 = 'Page';

$query = 'SELECT key1,key2,key3,count(id) AS cnt FROM '.cms_db_prefix().'module_hitcounter GROUP BY key1,key2 HAVING key1 = ?';
$parms = array($key1);
if( $key2 ) {
    $query .= ' AND key2 = ?';
    $parms[] = $key2;
}
$query .= ' ORDER BY cnt DESC';
$query .= ' LIMIT '.$limit;

//$template = cge_utils::get_param($params,'popular_template',$this->GetPreference('current_popular_template','orig_popular.tpl'));
$get_template = function(array $params,$key,$typename) {
    $thetemplate = null;
    if( !($thetemplate = \cge_param::get_string($params,$key)) ) {
        $tpl = \CmsLayoutTemplate::load_dflt_by_type($typename);
        if( !is_object($tpl) ) {
            audit('','HitCounter','No default '.$typename.' template found');
            return;
        }
        $thetemplate = $tpl->get_name();
        unset($tpl);
    }
    return $thetemplate;
};

$tpl = $this->CreateSmartyTemplate($get_template($params,'popular_template','HitCounter::popular'));
$res = $db->GetArray($query,array($parms));
$out = $errors = null;
if( is_array($res) && count($res) ) {
    $out = array();
    $errors = array();
    $factory = new \CGExtensions\LinkDefinition\LinkDefinitionGeneratorFactory();
    foreach( $res as $row ) {
        try {
            // need to create a dataref for each result
            $dr = new \CGExtensions\LinkDefinition\DataRef();
            $dr->key1 = $row['key1'];
            $dr->key2 = $row['key2'];
            $dr->key3 = $row['key3'];

            // convert the dataref into a linkdefn
            $generator = $factory->get_generator($dr);
            $ld = $generator->get_linkdefinition();
            $ld->text = $ld->text." ({$row['cnt']})";
            $out[] = $ld;
        }
        catch( \Exception $e ) {
            $errors[] = $e->GetMessage();
        }
    }

}
$tpl->assign('popular',$out);
if( count($errors) ) $tpl->assign('errors',$errors);
$tpl->display();

#
# EOF
#
