#!%PERL% -w 
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
#3. Neither the name of vhffs nor the names of its contributors 
#   may be used to endorse or promote products derived from this 
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.


use utf8;
use POSIX qw(locale_h);
use HTML::Template;
use locale;
use Locale::gettext;
use CGI;
use CGI::Session;
use strict;


use lib '%VHFFS_LIB_DIR%';
use Vhffs::User;
use Vhffs::Group;
use Vhffs::Main;
use Vhffs::Panel::Main;
use Vhffs::Panel::Menu;
use Vhffs::Stats;
use Vhffs::Constants;


my $panel = new Vhffs::Panel::Main();
exit 0 unless $panel;
my $session = $panel->get_session;
exit 0 unless $session;

my $vhffs = $panel->{'vhffs'};
my $maintemplate = $panel->{'template'};
my $user = $panel->{'user'};
my $cgi = $panel->{'cgi'};
my $servername = $cgi->param("name");
my $template;

my $templatedir = $vhffs->get_config->get_templatedir;

my $stats = new Vhffs::Stats( $vhffs );

if( ! defined $stats )
{

	$template = new HTML::Template( filename => $templatedir."/panel/misc/simplemsg.tmpl" );
	my $message = gettext( "Cannot get statistics");
	$template->param( MESSAGE => $message );

}
elsif( ($user->is_moderator != 1 ) && ( $user->is_admin != 1 ) )
{
	
	$template = new HTML::Template( filename => $templatedir."/panel/misc/simplemsg.tmpl" );
	my $message = gettext( "You are not allowed to see it");
	$template->param( MESSAGE => $message );
}
else
{
	$template = new HTML::Template( filename => $templatedir."/panel/admin/misc/stats.tmpl" );

	$template->param( TITLE => gettext("Vhffs Statistics") );

	$template->param( TEXT_USERS => gettext("Users stats") );
	$template->param( TEXT_TOTAL_USERS => gettext("Total Users in VHFFS database") );
	$template->param( VALUE_TOTAL_USERS => $stats->get_user_total );
	$template->param( TEXT_TOTAL_USER_ADMIN => gettext("Total Admin Users in VHFFS database") );
	$template->param( VALUE_TOTAL_USER_ADMIN => $stats->get_user_total_admin );
	$template->param( TEXT_TOTAL_USER_MODERATION => gettext("Total Moderator Users in VHFFS database") );
	$template->param( VALUE_TOTAL_USER_MODERATION => $stats->get_user_total_moderator );

	$template->param( TEXT_GROUPS => gettext("Groups stats") );
	$template->param( TEXT_TOTAL_GROUPS => gettext("Total groups in database") );
	$template->param( VALUE_TOTAL_GROUPS => $stats->get_groups_total );
	$template->param( TEXT_TOTAL_GROUPS_ACTIVATED => gettext("Total activated groups") );
	$template->param( VALUE_TOTAL_GROUPS_ACTIVATED => $stats->get_groups_activated );

	$template->param( TEXT_WEB => gettext("WEB stats") );
	$template->param( TEXT_TOTAL_WEB_IN_MODERATION => gettext("Total Web Areas in moderation") );
	$template->param( VALUE_TOTAL_WEB_IN_MODERATION => $stats->get_web_in_moderation );
	$template->param( TEXT_TOTAL_WEB_ACTIVATED => gettext("Total activated Web Areas") );
	$template->param( VALUE_TOTAL_WEB_ACTIVATED => $stats->get_web_activated );


	$template->param( TEXT_DNS => gettext("DNS stats") );
	$template->param( TEXT_TOTAL_DNS_IN_MODERATION => gettext("Total DNS in moderation") );
	$template->param( VALUE_TOTAL_DNS_IN_MODERATION => $stats->get_dns_in_moderation );
	$template->param( TEXT_TOTAL_DNS_ACTIVATED => gettext("Total activated DNS") );
	$template->param( VALUE_TOTAL_DNS_ACTIVATED => $stats->get_dns_activated );



	$template->param( TEXT_CVS => gettext("CVS stats") );
	$template->param( TEXT_TOTAL_CVS_IN_MODERATION => gettext("Total CVS in moderation") );
	$template->param( VALUE_TOTAL_CVS_IN_MODERATION => $stats->get_cvs_in_moderation );
	$template->param( TEXT_TOTAL_CVS_ACTIVATED => gettext("Total activated CVS") );
	$template->param( VALUE_TOTAL_CVS_ACTIVATED => $stats->get_cvs_activated );


	$template->param( TEXT_SVN => gettext("SVN stats") );
	$template->param( TEXT_TOTAL_SVN_IN_MODERATION => gettext("Total SVN in moderation") );
	$template->param( VALUE_TOTAL_SVN_IN_MODERATION => $stats->get_svn_in_moderation );
	$template->param( TEXT_TOTAL_SVN_ACTIVATED => gettext("Total activated SVN") );
	$template->param( VALUE_TOTAL_SVN_ACTIVATED => $stats->get_svn_activated );

	$template->param( TEXT_GIT => gettext("GIT stats") );
	$template->param( TEXT_TOTAL_GIT_IN_MODERATION => gettext("Total GIT in moderation") );
	$template->param( VALUE_TOTAL_GIT_IN_MODERATION => $stats->get_git_in_moderation );
	$template->param( TEXT_TOTAL_GIT_ACTIVATED => gettext("Total activated GIT") );
	$template->param( VALUE_TOTAL_GIT_ACTIVATED => $stats->get_git_activated );

	$template->param( TEXT_MERCURIAL => gettext("MERCURIAL stats") );
	$template->param( TEXT_TOTAL_MERCURIAL_IN_MODERATION => gettext("Total MERCURIAL in moderation") );
	$template->param( VALUE_TOTAL_MERCURIAL_IN_MODERATION => $stats->get_mercurial_in_moderation );
	$template->param( TEXT_TOTAL_MERCURIAL_ACTIVATED => gettext("Total activated MERCURIAL") );
	$template->param( VALUE_TOTAL_MERCURIAL_ACTIVATED => $stats->get_mercurial_activated );

	$template->param( TEXT_BAZAAR => gettext("BAZAAR stats") );
	$template->param( TEXT_TOTAL_BAZAAR_IN_MODERATION => gettext("Total BAZAAR in moderation") );
	$template->param( VALUE_TOTAL_BAZAAR_IN_MODERATION => $stats->get_bazaar_in_moderation );
	$template->param( TEXT_TOTAL_BAZAAR_ACTIVATED => gettext("Total activated BAZAAR") );
	$template->param( VALUE_TOTAL_BAZAAR_ACTIVATED => $stats->get_bazaar_activated );

	$template->param( TEXT_MAIL => gettext("Mail stats") );
	$template->param( TEXT_TOTAL_MAIL_IN_MODERATION => gettext("Total Mail domains in moderation") );
	$template->param( VALUE_TOTAL_MAIL_IN_MODERATION => $stats->get_mail_in_moderation );
	$template->param( TEXT_TOTAL_MAIL_ACTIVATED => gettext("Total activated Mail domains") );
	$template->param( VALUE_TOTAL_MAIL_ACTIVATED => $stats->get_mail_activated );
	$template->param( TEXT_MAIL_FORWARDS => gettext("Total hosted forwards") );
	$template->param( VALUE_MAIL_FORWARDS => $stats->get_mail_total_forwards );
	$template->param( TEXT_MAIL_BOXES => gettext("Total hosted boxes") );
	$template->param( VALUE_MAIL_BOXES => $stats->get_mail_total_boxes );


	
	$template->param( TEXT_MYSQL => gettext("MySQL stats") );
	$template->param( TEXT_TOTAL_MYSQL_IN_MODERATION => gettext("Total MySQL in moderation") );
	$template->param( VALUE_TOTAL_MYSQL_IN_MODERATION => $stats->get_mysql_in_moderation );
	$template->param( TEXT_TOTAL_MYSQL_ACTIVATED => gettext("Total activated MySQL") );
	$template->param( VALUE_TOTAL_MYSQL_ACTIVATED => $stats->get_mysql_activated );


	$template->param( TEXT_PGSQL => gettext("PgSQL stats") );
	$template->param( TEXT_TOTAL_PGSQL_IN_MODERATION => gettext("Total PostgreSQL in moderation") );
	$template->param( VALUE_TOTAL_PGSQL_IN_MODERATION => $stats->get_pgsql_in_moderation );
	$template->param( TEXT_TOTAL_PGSQL_ACTIVATED => gettext("Total activated PostgreSQL") );
	$template->param( VALUE_TOTAL_PGSQL_ACTIVATED => $stats->get_pgsql_activated );
	

	#Lists part
	$template->param( TEXT_LISTS => gettext("Listengine stats") );
	$template->param( TEXT_TOTAL_LISTS => gettext("Total lists in moderation") );
	$template->param( VALUE_TOTAL_LISTS => $stats->get_lists_total );
	$template->param( TEXT_TOTAL_LISTS_ACTIVATED => gettext("Total activated lists") );
	$template->param( VALUE_TOTAL_LISTS_ACTIVATED => $stats->get_lists_activated );
	$template->param( TEXT_TOTAL_SUBS => gettext("Total subscribtion for lists") );
	$template->param( VALUE_TOTAL_SUBS => $stats->get_lists_totalsubs );

	#tags part
	$template->param( TEXT_TAGS => gettext("Tags stats") );
	$template->param( TEXT_TOTAL_TAGS_CATEGORIES => gettext("Total categories") );
	$template->param( VALUE_TOTAL_TAGS_CATEGORIES => $stats->get_tags_categories_total );
	$template->param( TEXT_TOTAL_TAGS_USED_EXISTING => gettext("Total tags used") );
	$template->param( VALUE_TOTAL_TAGS_USED => $stats->get_tags_used_total );
	#$template->param( TEXT_TOTAL_TAGS_EXISTING => gettext("Total tags existing") );
	$template->param( VALUE_TOTAL_TAGS_EXISTING => $stats->get_tags_total );
	$template->param( TEXT_TOTAL_TAGS_GROUPS => gettext("Total tagged groups" ) );
	$template->param( VALUE_TOTAL_TAGS_GROUPS => $stats->get_tags_groups_total );
	$template->param( TEXT_MAX_TAGS_GROUPS => gettext("Max tags for a group" ) );
	$template->param( VALUE_MAX_TAGS_GROUPS => $stats->get_tags_groups_max );
	$template->param( TEXT_10MOST_USED_TAGS => gettext("Popular tags" ) );
	#my $tags =  Vhffs::Tag::get_10_tagsgroups($panel->{vhffs});
        #my $sql = q{SELECT vhffs_tag_category.label as category, vhffs_tag.label as tag_label, COUNT(object_id) as nb_groups 
                #FROM vhffs_object_tag, vhffs_tag_category, vhffs_tag
                #WHERE vhffs_tag.category_id=vhffs_tag_category.tag_category_id
                  #AND vhffs_object_tag.tag_id=vhffs_tag.tag_id
                #GROUP BY category,tag_label ORDER BY nb_groups DESC LIMIT 10};
	
        #my $tags = [];
        #my $main = $panel->{vhffs};

        #my $dbh = $main->get_db_object();
        #my $sth = $dbh->prepare($sql);
        #$sth->execute() or return undef;
        #while( (my $t = $sth->fetchrow_hashref() )  ) {
              #push @$tags, $t;
        #}

	$template->param( 'TAGS' => $stats->get_most_popular_tags );

}

$panel->build( $template );
$panel->display;
