#!%PERL%
# Copyright (c) vhffs project and its contributors
# Copyright (c) 2007 Julien Danjou <julien@danjou.info>
# 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::Git;

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


=pod

=head2 getall_per_group

    $git = Vhffs::Panel::Git::getall_per_group($vhffs, $gid);

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

=cut


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

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

sub get_repos_per_group {
    my ($main, $gid, $public_only) = @_;
    $public_only = 1 unless(defined $public_only);

    my $dbh = $main->get_db_object;
    my $sql = 'SELECT g.reponame, o.description FROM vhffs_git g INNER JOIN vhffs_object o ON o.object_id = g.object_id '.
	'WHERE '.($public_only ? 'public = 1 AND ' : '').'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 $gits = [];

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

    return $gits;
}



sub create_git
{
    my ($main, $repo, $description, $user, $group) = @_;

    return -1 unless( defined $user );
    return -2 unless( defined $group );
    
    my $git = Vhffs::Services::Git::create( $main, $repo, $description, $user, $group );
    
    return -1 unless( defined $git );
    
    return ( -3 ) if ( Vhffs::Acl::add_acl( $user , $git , Vhffs::Constants::ACL_DELETE , $main ) < 0 );
    return ( -3 ) if( Vhffs::Acl::add_acl( $group , $git , Vhffs::Constants::ACL_VIEW , $main ) < 0 );
    
    return $git;
}

1;
