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

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::Constants;
use Vhffs::Panel::Main;
use Vhffs::Services::Pgsql;


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

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

    if( defined $name ) {
        $sql = 'SELECT p.dbname, g.groupname, o.state  FROM vhffs_pgsql p INNER JOIN vhffs_object o ON o.object_id = p.object_id INNER JOIN vhffs_groups g ON g.gid = o.owner_gid WHERE p.dbname LIKE ? OR p.dbuser LIKE ?';
        push(@params, '%'.lc($name).'%', '%'.lc($name).'%');
    } else {
        $sql = 'SELECT p.dbname, g.groupname, o.state  FROM vhffs_pgsql p INNER JOIN vhffs_object o ON o.object_id = p.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 $p = $sth->fetchrow_hashref('NAME_lc')) {
        $p->{state} = Vhffs::Functions::status_string_from_status_id($p->{state});
        push(@$pgs, $p);
    }
    return $pgs;
}


=pod

=head2 getall_per_group

    $pgsql = Vhffs::Panel::Postgres::getall_per_group($vhffs, $gid);

Returns an array of hashrefs (oid, displayname, active, state (localized string)) of all PgSQL DBs owned by
a given group.

=cut


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

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

sub create_pgsql
{
    my( $main , $dbname , $user , $group , $dbuser , $dbpass, $description ) = @_;

    return -1 if( ! defined $user );
    return -2 if( ! defined $group );
    
    my $pgsql = Vhffs::Services::Pgsql::create($main, $dbname, $dbuser, $dbpass, $description, $user, $group);

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

	return $pgsql;
}


1;
