#!%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;

package Vhffs::Panel::Web;

use DBI;
use POSIX qw(locale_h);
use HTML::Template;
use locale;
use Locale::gettext;
use CGI;
use CGI::Session;
use Vhffs::User;
use Vhffs::Main;
use Vhffs::Panel::Main;
use Vhffs::Services::Web;
use Vhffs::Constants;
use Vhffs::Functions;

=pod

=head1 NAME

Vhffs::Panel::Web - Light weight objects to handle webareas in VHFFS panel.

=head2 METHODS

=head2 get_all_per_group

    my $areas = Vhffs::Panel::Web::getall_per_group($vhffs, $main);

Returns an array of hashrefs (oid, display, active, state) of all webareas owned by
a given group.

=cut

sub getall_per_group {
    my ($main, $gid) = @_;

    my $dbh = $main->get_db_object;
    my $sql = 'SELECT w.servername AS displayname, w.object_id AS oid, o.state FROM vhffs_httpd w INNER JOIN vhffs_object o ON o.object_id = w.object_id WHERE o.owner_gid = ? ORDER BY w.servername';
    my $sth = $dbh->prepare($sql) or return -1;
    $sth->execute($gid) or return -2;
    my $web = [];
    while(my $w = $sth->fetchrow_hashref) {
        $w->{active} = ($w->{state} == Vhffs::Constants::ACTIVATED);
        $w->{refused} = ($w->{state} == Vhffs::Constants::VALIDATION_REFUSED);
        $w->{state} = Vhffs::Functions::status_string_from_status_id($w->{state});
        push @$web, $w;
    }
    return $web;
}

=head2 get_websites_per_group

Returns an array of hashrefs {servername, description} containing all active websites for a
givent group.

=cut

sub get_websites_per_group {
    my ($main, $gid) = @_;

    my $dbh = $main->get_db_object;
    my $sql = 'SELECT w.servername, o.description FROM vhffs_httpd w INNER JOIN vhffs_object o ON o.object_id = w.object_id WHERE o.owner_gid = ? AND o.state = ?';
    return $dbh->selectall_arrayref($sql, { Slice => {} }, $gid, Vhffs::Constants::ACTIVATED);
}

sub search {
    my ($main, $name) = @_;

    my $sql;
    my @params;
    my $webs = [];

    if( defined $name ) {
        $sql = 'SELECT w.servername, o.state, g.groupname  FROM vhffs_httpd w INNER JOIN vhffs_object o ON o.object_id = w.object_id INNER JOIN vhffs_groups g ON g.gid = o.owner_gid WHERE w.servername LIKE ?';
        push(@params, '%'.lc($name).'%');
    } else {
        $sql = 'SELECT w.servername, o.state, g.groupname  FROM vhffs_httpd w INNER JOIN vhffs_object o ON o.object_id = w.object_id INNER JOIN vhffs_groups g ON g.gid = o.owner_gid';
    }

    my $dbh = $main->get_db_object();
    my $sth = $dbh->prepare($sql) or return undef;

    return undef unless($sth->execute(@params));

    while(my $w = $sth->fetchrow_hashref('NAME_lc')) {
        $w->{state} = Vhffs::Functions::status_string_from_status_id($w->{state});
        push(@$webs, $w);
    }

    return $webs;
}

sub public_search {
    my ($main, $servername, $description, $start, $count) = @_;

    my $select = 'SELECT w.servername, g.groupname, o.description';
    my $from = ' FROM vhffs_httpd w INNER JOIN vhffs_object o ON o.object_id = w.object_id INNER JOIN vhffs_groups g ON g.gid = o.owner_gid WHERE o.state = ?';
    my @params;
    push @params, Vhffs::Constants::ACTIVATED;
    if(defined $servername) {
        $from .= ' AND w.servername LIKE ?';
        push @params, '%'.lc($servername).'%';
    }

    if(defined $description) {
        $from .= ' AND o.description ILIKE ?';
        push @params, '%'.$description.'%';
    }

   return Vhffs::Panel::Commons::fetch_slice_and_count($main, $select, $from, ' ORDER BY w.servername', $start, $count, \@params);
}


sub create_web {
        my( $main, $servername, $description, $user, $group ) = @_;

        return undef if( !defined $user );
        return undef if( !defined $group );

        my $web = Vhffs::Services::Web::create($main, $servername, $description, $user, $group);
		return undef if( ! defined $web );

        return undef if ( Vhffs::Acl::add_acl( $user , $web , Vhffs::Constants::ACL_DELETE , $main ) < 0 );
        return undef if( Vhffs::Acl::add_acl( $group , $web , Vhffs::Constants::ACL_VIEW , $main ) < 0 );

        return $web;
}

sub get_websites_starting_with {
    my ($vhffs, $letter, $start, $count) = @_;

    my @params;
    my $select = 'SELECT w.servername, g.groupname, o.description';
    my $from = ' FROM vhffs_httpd w INNER JOIN vhffs_object o ON o.object_id = w.object_id INNER JOIN vhffs_groups g ON g.gid = o.owner_gid WHERE o.state = ?';
    push @params, Vhffs::Constants::ACTIVATED;
    if(defined $letter) {
        $from .= ' AND substr(w.servername, 1, 1) = ?';
        push @params, $letter;
    }
    my $dbh = $vhffs->get_db_object;
    return Vhffs::Panel::Commons::fetch_slice_and_count($vhffs, $select, $from, ' ORDER BY w.servername', $start, $count, \@params);
}

=head2 get_used_letters

Returns a reference on an array, each field is a hash with two fields
letter and count (count is the number of websites starting with letter).
0 site letters aren't stored.

=cut

sub get_used_letters {
    my $main = shift;
    my $sql = 'SELECT substr(servername, 1, 1) as letter, COUNT(*) as count FROM vhffs_httpd h INNER JOIN vhffs_object o ON o.object_id = h.object_id WHERE state = ? GROUP BY substr(servername, 1, 1) ORDER BY substr(servername, 1, 1)';
    my $dbh = $main->get_db_object;
    return $dbh->selectall_arrayref($sql, { Slice => {} }, Vhffs::Constants::ACTIVATED);

}


1;
