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

use POSIX qw(locale_h);
use locale;
use Locale::gettext;
use Vhffs::Panel::Group;

=head1 NAME

Vhffs::Panel::Tag - Handle tags information in the panel.

=head1 METHODS

=cut

=head2 get_groups

	Vhffs::Panel::Tag::get_groups($main, $category_name, $tag_name, $start)

Fetches 10 groups associated to tag C<$tag_name> in (optional) category
C<$category_name> starting at index C<$start>

=cut

sub get_groups {
	my ($main, $category_name, $tag_name, $start) = @_;

	my $select =  'SELECT g.gid, g.groupname, g.realname, o.description, owner.username AS owner_name ';
	my $restriction = ' FROM vhffs_groups g INNER JOIN vhffs_object o ON o.object_id=g.object_id '.
		'INNER JOIN vhffs_object_tag ot ON ot.object_id = o.object_id '.
		'INNER JOIN vhffs_tag t ON t.tag_id = ot.tag_id '.
		'INNER JOIN vhffs_tag_category c ON t.category_id = c.tag_category_id '.
		'INNER JOIN vhffs_users owner ON owner.uid = o.owner_uid '.
		'WHERE o.state = ? AND t.label = ? ';
	my @params;
	push @params, Vhffs::Constants::ACTIVATED;
	push @params, $tag_name;
	if(defined $category_name) {
		$restriction .= ' AND c.label = ?';
		push @params, $category_name;
	}

	my $limit = ' LIMIT 10';
	$limit .= ' OFFSET '.($start * 10) if(defined $start);

	my $groups = Vhffs::Panel::Group::fetch_groups_and_users($main, $select.$restriction.' ORDER BY g.groupname '.$limit, @params);

	my $dbh = $main->get_db_object();

	my $sth = $dbh->prepare('SELECT COUNT(*) '.$restriction);
	return undef unless ( $sth->execute(@params) );

	my ($count) = $sth->fetchrow_array();
	
	return ($groups, $count);
}

=head2 get_by_tag_ids

	my $tags = Vhffs::Panel::get_by_tag_ids($vhffs, id1, id2);

Return information about specific public tags.

=cut

sub get_by_tag_ids {
	
	my $main = shift;
	my @ids = @_;
	return _get_by_tag_ids($main, 0, @ids);
}

sub get_all_excluding {
	my $main = shift;
	my @ids = @_;
	return _get_by_tag_ids($main, 1, @ids);
}

sub _get_by_tag_ids {
	my $main = shift;
	my $exclude = shift;
	my @ids = @_;
	
	# There can be no ID if we are in exclude mode (meaning
	# that we want all tags)
	
	return undef unless($exclude ||  scalar(@ids) > 0);
	
	my $sql = 'SELECT c.tag_category_id AS category_id, c.label AS category_label, t.tag_id, t.label AS tag_label '.
		'FROM vhffs_tag t INNER JOIN vhffs_tag_category c ON c.tag_category_id = t.category_id '.
		'WHERE c.visibility = '.Vhffs::Constants::TAG_VISIBILITY_PUBLIC;
	
	if(scalar(@ids)) {
		$sql .= ' AND tag_id '.($exclude ? 'NOT ' : '').'IN(?';
		$sql .= ', ?' x (scalar(@ids) - 1);
		$sql .= ')';
	}
	$sql .= ' ORDER BY c.label, t.label';
	my $dbh = $main->get_db_object;
	return $dbh->selectall_arrayref($sql, { Slice => {} }, @ids);
}

1;
