#!%PERL%
# 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 strict;
use utf8;

# This is a part of VHFFS platform

# This file is used to generate the admin part of the menu (left in the panel)

package Vhffs::Panel::Admin;

use DBI;
use POSIX qw(locale_h);
use HTML::Template;
use locale;
use Locale::gettext;
use Vhffs::User;
use Vhffs::Main;
use Vhffs::Acl;
use Vhffs::Panel::Main;
use Vhffs::Constants;

=pod

=head1 NAME

Vhffs::Panel::Admin - Handle administration functionnalities of VHFFS panel.

=head1 METHODS

=cut

=pod

=head2 get_modo_category

    Vhffs::Panel::Admin::get_modo_category;

Returns a hashref (CATNAME, ITEMS) where catname is the name of the
general category for moderation and ITEMS the corresponding menu
items.

=cut

sub get_modo_category {
    my $items = [
        { LINK => '/admin/stats.pl',            LABEL => gettext( 'Get Statistics' ) },
        { LINK => '/admin/moderation.pl',       LABEL => gettext( 'Moderation' ) }
    ];
    return { CATNAME => gettext( 'General' ),   ITEMS => $items, CATTYPE => 'general' };
}

=head2 get_admin_category

    Vhffs::Panel::Admin::get_admin_category;

Returns a hashref (CATNAME, ITEMS) containing general administration
items.

=cut

sub get_admin_category {
    my $items = [
        { LINK => '/admin/stats.pl',            LABEL => gettext( 'Get Statistics' ) },
        { LINK => '/admin/su.pl',               LABEL => gettext( 'Change user-id' ) },
        { LINK => '/admin/moderation.pl',       LABEL => gettext( 'Moderation' ) },
        { LINK => '/admin/object/list.pl',      LABEL => gettext( 'List all objects' ) },
        { LINK => '/admin/object/search.pl',    LABEL => gettext( 'Search for an object' ) },
        { LINK => '/admin/broadcast.pl',        LABEL => gettext( 'Mail to all hosted people' ) },
        { LINK => '/admin/broadcast_list.pl',   LABEL => gettext( 'Manage mailings' ) }
    ];
    return { CATNAME => gettext( 'General' ),   ITEMS => $items, CATTYPE => 'general' };
}


=head2 get_bazaar_category

    Vhffs::Panel::Admin::get_bazaar_category;

Returns a hashref (CATNAME, ITEM) containing bazaar's administration
items.

=cut

sub get_bazaar_category {
    my $items = [
        { LINK => '/admin/bazaar/list.pl',        LABEL => gettext( 'List all Bazaar repos' ) },
        { LINK => '/admin/bazaar/search.pl',      LABEL => gettext( 'Search for a Bazaar repository' ) }
    ];
    return { CATNAME => gettext( 'Bazaar Admin' ),    ITEMS => $items, CATTYPE => 'bazaar'  };
}

=head2 get_user_category

    Vhffs::Panel::Admin::get_user_category;

Returns a hashref (CATNAME, ITEM) containing users' administration
items.

=cut

sub get_user_category {
    my $items = [
        { LINK => '/admin/user/list.pl',        LABEL => gettext( 'List all users' ) },
        { LINK => '/admin/user/search.pl',      LABEL => gettext( 'Search for an user' ) }
    ];
    return { CATNAME => gettext( 'User Admin' ),    ITEMS => $items, CATTYPE => 'user'  };
}

=head2 get_user_category

    Vhffs::Panel::Admin::get_group_category;

Returns a hashref (CATNAME, ITEM) containing groups' administration
items.

=cut

sub get_group_category {
    my $items = [
        { LINK => '/admin/group/list.pl',        LABEL => gettext( 'List all groups' ) },
        { LINK => '/admin/group/search.pl',      LABEL => gettext( 'Search for a group' ) }
    ];
    return { CATNAME => gettext( 'Group Admin' ),    ITEMS => $items, CATTYPE => 'group'  };
}


=head2 get_tag_category

	Vhffs::Panel::Admin::get_tag_cagtegory
	
Returns a hashref (CATNAME, ITEM, CATTYPE) containing tags'
administration items

=cut

sub get_tag_category {
	my $items = [
		{ LINK => '/admin/tag/create.pl',			LABEL => gettext( 'Create new tag' ) },
		{ LINK => '/admin/tag/list.pl',	LABEL => gettext( 'Manage existing tags' )},
		{ LINK => '/admin/tag/category/create.pl',	LABEL => gettext( 'Create new category' )},
		{ LINK => '/admin/tag/category/list.pl',	LABEL => gettext( 'Manage existing categories' )},
		{ LINK => '/admin/tag/request/list.pl',			LABEL => gettext('Manage requests')}
	];
	return { CATNAME => gettext( 'Tags Admin'),		ITEMS => $items, CATTYPE => 'tags' };
}

=head2 get_web_category

    Vhffs::Panel::Admin::get_web_category;

Returns a hashref (CATNAME, ITEM) containing webareas' administration
items.

=cut

sub get_web_category {
    my $items = [
        { LINK => '/admin/web/list.pl',        LABEL => gettext( 'List all webareas' ) },
        { LINK => '/admin/web/search.pl',      LABEL => gettext( 'Search for a webarea' ) }
    ];
    return { CATNAME => gettext( 'Web Admin' ),    ITEMS => $items, CATTYPE => 'web'  };
}

=head2 get_svn_category

    Vhffs::Panel::Admin::get_svn_category;

Returns a hashref (CATNAME, ITEM) containing svn's administration
items.

=cut

sub get_svn_category {
    my $items = [
        { LINK => '/admin/svn/list.pl',        LABEL => gettext( 'List all SVN repos' ) },
        { LINK => '/admin/svn/search.pl',      LABEL => gettext( 'Search for a SVN repository' ) }
    ];
    return { CATNAME => gettext( 'SVN Admin' ),    ITEMS => $items, CATTYPE => 'svn'  };
}


=head2 get_cvs_category

    Vhffs::Panel::Admin::get_cvs_category;

Returns a hashref (CATNAME, ITEM) containing cvs' administration
items.

=cut

sub get_cvs_category {
    my $items = [
        { LINK => '/admin/cvs/list.pl',        LABEL => gettext( 'List all CVS repos' ) },
        { LINK => '/admin/cvs/search.pl',      LABEL => gettext( 'Search for a CVS repository' ) }
    ];
    return { CATNAME => gettext( 'CVS Admin' ),    ITEMS => $items, CATTYPE => 'cvs'  };
}


=head2 get_git_category

    Vhffs::Panel::Admin::get_git_category;

Returns a hashref (CATNAME, ITEM) containing git's administration
items.

=cut

sub get_git_category {
    my $items = [
        { LINK => '/admin/git/list.pl',        LABEL => gettext( 'List all Git repos' ) },
        { LINK => '/admin/git/search.pl',      LABEL => gettext( 'Search for a Git repository' ) }
    ];
    return { CATNAME => gettext( 'Git Admin' ),    ITEMS => $items, CATTYPE => 'git'  };
}


=head2 get_mercurial_category

    Vhffs::Panel::Admin::get_mercurial_category;

Returns a hashref (CATNAME, ITEM) containing mercurial's administration
items.

=cut

sub get_mercurial_category {
    my $items = [
        { LINK => '/admin/mercurial/list.pl',        LABEL => gettext( 'List all Mercurial repos' ) },
        { LINK => '/admin/mercurial/search.pl',      LABEL => gettext( 'Search for a Mercurial repository' ) }
    ];
    return { CATNAME => gettext( 'Mercurial Admin' ),    ITEMS => $items, CATTYPE => 'mercurial'  };
}

=head2 get_mysql_category

    Vhffs::Panel::Admin::get_mysql_category;

Returns a hashref (CATNAME, ITEM) containing mysql's administration
items.

=cut

sub get_mysql_category {
    my $items = [
        { LINK => '/admin/mysql/list.pl',        LABEL => gettext( 'List all MySQL databases' ) },
        { LINK => '/admin/mysql/search.pl',      LABEL => gettext( 'Search for a MySQL database' ) }
    ];
    return { CATNAME => gettext( 'MySQL Admin' ),    ITEMS => $items, CATTYPE => 'mysql'  };
}

=head2 get_pgsql_category

    Vhffs::Panel::Admin::get_pgsql_category;

Returns a hashref (CATNAME, ITEM) containing PostgreSQL's administration
items.

=cut

sub get_pgsql_category {
    my $items = [
        { LINK => '/admin/pgsql/list.pl',        LABEL => gettext( 'List all Pg databases' ) },
        { LINK => '/admin/pgsql/search.pl',      LABEL => gettext( 'Search for a Pg database' ) }
    ];
    return { CATNAME => gettext( 'PostgreSQL Admin' ),    ITEMS => $items, CATTYPE => 'pgsql'  };
}

=head2 get_mail_category

    Vhffs::Panel::Admin::get_mail_category;

Returns a hashref (CATNAME, ITEM) containing mail domains' administration
items.

=cut

sub get_mail_category {
    my $items = [
        { LINK => '/admin/mail/list.pl',        LABEL => gettext( 'List all mail domains' ) },
        { LINK => '/admin/mail/search.pl',      LABEL => gettext( 'Search for a mail domain' ) }
    ];
    return { CATNAME => gettext( 'Mail domains Admin' ),    ITEMS => $items, CATTYPE => 'mail'  };
}


=head2 get_mailing_category

    Vhffs::Panel::Admin::get_mailing_category;

Returns a hashref (CATNAME, ITEM) containing mailing lists' administration
items.

=cut

sub get_mailing_category {
    my $items = [
        { LINK => '/admin/mailinglist/list.pl',        LABEL => gettext( 'List all mailing lists' ) },
        { LINK => '/admin/mailinglist/search.pl',      LABEL => gettext( 'Search for a mailing list' ) }
    ];
    return { CATNAME => gettext( 'Mailing lists Admin' ),    ITEMS => $items, CATTYPE => 'mailing'  };
}


=head2 get_dns_category

    Vhffs::Panel::Admin::get_dns_category;

Returns a hashref (CATNAME, ITEM) containing DNS' administration
items.

=cut

sub get_dns_category {
    my $items = [
        { LINK => '/admin/dns/list.pl',        LABEL => gettext( 'List all domain names' ) },
        { LINK => '/admin/dns/search.pl',      LABEL => gettext( 'Search for a domain name' ) }
    ];
    return { CATNAME => gettext( 'DNS Admin' ),    ITEMS => $items, CATTYPE => 'dns'  };
}


=head2 get_repo_category

    Vhffs::Panel::Admin::get_repo_category;

Returns a hashref (CATNAME, ITEM) containing download repositories' administration
items.

=cut

sub get_repo_category {
    my $items = [
        { LINK => '/admin/repository/list.pl',        LABEL => gettext( 'List all download repositories' ) },
        { LINK => '/admin/repository/search.pl',      LABEL => gettext( 'Search for a download repository' ) }
    ];
    return { CATNAME => gettext( 'Download repositories Admin' ),    ITEMS => $items, CATTYPE => 'repository'  };
}

=head2 get_cron_category

    Vhffs::Panel::Admin::get_cron_category;

Returns a hashref (CATNAME, ITEM) containing cron jobs administration
items.

=cut

sub get_cron_category {
    my $items = [
        { LINK => '/admin/cron/list.pl',        LABEL => gettext( 'List all cron jobs' ) },
        { LINK => '/admin/cron/search.pl',      LABEL => gettext( 'Search for a cron job' ) }
    ];
    return { CATNAME => gettext( 'Crons Admin' ),    ITEMS => $items, CATTYPE => 'cron'  };
}

=head2 get_all_admin_categories

    Vhffs::Panel::Admin::get_all_admin_categories($vhffs);

Return an arrayref of hashrefs (CATNAME, ITEM) containing all administration
categories and items based on configuration of $vhffs.

=cut

sub get_all_admin_categories($) {
    my $vhffs = shift;
    my $config = $vhffs->get_config;
    my $categories = [];

    push @$categories, get_admin_category;
    push @$categories, get_user_category;
    push @$categories, get_group_category;
    push @$categories, get_web_category       if($config->get_service_availability('web'));
    push @$categories, get_mysql_category     if($config->get_service_availability('mysql'));
    push @$categories, get_pgsql_category     if($config->get_service_availability('pgsql'));
    push @$categories, get_cvs_category       if($config->get_service_availability('cvs'));
    push @$categories, get_svn_category       if($config->get_service_availability('svn'));
    push @$categories, get_dns_category       if($config->get_service_availability('dns'));
    push @$categories, get_git_category       if($config->get_service_availability('git'));
    push @$categories, get_mercurial_category if($config->get_service_availability('mercurial'));
    push @$categories, get_bazaar_category    if($config->get_service_availability('bazaar'));
    push @$categories, get_mail_category      if($config->get_service_availability('mail'));
    push @$categories, get_mailing_category   if($config->get_service_availability('mailinglist'));
    push @$categories, get_repo_category      if($config->get_service_availability('repository'));
    push @$categories, get_cron_category      if($config->get_service_availability('cron'));
    push @$categories, get_tag_category;

    return $categories;
}


=head2 get_all_modo_categories

    Vhffs::Panel::Admin::get_all_modo_categories($vhffs);

Return an arrayref of hashrefs (CATNAME, ITEM) containing all administration
categories and items based on configuration of $vhffs.

=cut

sub get_all_modo_categories($) {
    my $vhffs = shift;
    my $config = $vhffs->get_config;
    my $categories = [];

    push @$categories, get_modo_category;
    push @$categories, get_user_category;
    push @$categories, get_group_category;
    push @$categories, get_web_category       if($config->get_service_availability('web'));
    push @$categories, get_mysql_category     if($config->get_service_availability('mysql'));
    push @$categories, get_pgsql_category     if($config->get_service_availability('pgsql'));
    push @$categories, get_cvs_category       if($config->get_service_availability('cvs'));
    push @$categories, get_svn_category       if($config->get_service_availability('svn'));
    push @$categories, get_git_category       if($config->get_service_availability('git'));
    push @$categories, get_mercurial_category if($config->get_service_availability('mercurial'));
    push @$categories, get_bazaar_category    if($config->get_service_availability('bazaar'));
    push @$categories, get_dns_category       if($config->get_service_availability('dns'));
    push @$categories, get_mail_category      if($config->get_service_availability('mail'));
    push @$categories, get_mailing_category   if($config->get_service_availability('mailinglist'));
    push @$categories, get_repo_category      if($config->get_service_availability('repository'));
    push @$categories, get_cron_category      if($config->get_service_availability('cron'));
    push @$categories, get_tag_category;

    return $categories;
}

1;
