use strict;
return 1;

=head1 Name

OSSTag - A Social Tagging Engine

Copyright (c) 2006 Open Source Development Labs, Inc.

=head1 Usage

=head2 Initialisation

OSSTag assumes that you already have a database-connection which it may use

In order to use osstag, put the following code in your script:

 	use osstag;
	my $osstag = new osstag($dbh, $cgi);

Parameters:

	$dbh: DBI Database Handler
	$cgi: CGI Handler

After this you can call any of the included functions.

Example: 

	$osstag->printFormattedTaglist($whatid);

=cut

package osstag;

use osstag_dbmapper;
use osstag_config;
use osstag_helper;
use osstag_obj_phymap;
use osstag_obj_semmap;
use osstag_obj_assoziations;

sub new($$)
{
	my $class = shift;
	my $self = {};
	bless $self, $class;

	my ($dbh, $cgi) = @_;
	$self->{dbm} = new osstag_dbmapper($dbh);
	$self->{cgi} = $cgi;

	my %config = osstag_config::getConfig();
	$self->{taglist_betweeneachtag} = $config{taglist_betweeneachtag};
	$self->{taglist_beforeeachtag} = $config{taglist_beforeeachtag};
	$self->{taglist_aftereatchtag} = $config{taglist_aftereatchtag};

	return $self;
}

=head1 Functions

=head2 printFormattedTaglist

	Prints a list of all tags associated with the given id to stdout

	Parameters:
	whatid: the id of what is tagged

	This function is protected via preventHTMLInjection

=cut

sub printFormattedTaglist($)
{
	my $self = shift;
	my ($phy_pointer) = @_;
	print $self->getFormattedTaglist($phy_pointer);
}

=head2 getFormattedTaglist

	Returns a formatted string used to display the taglist assiciated with the given whatid

	Parameters:
	whatid: the id of what is tagged

	This function is protected via preventHTMLInjection

=cut

sub getFormattedTaglist($$) 
{
	my $self = shift;
	my ($project, $phy_pointer) = @_;

	my $string = '';

	my $phymap = $self->{dbm}->createPhymapForPhy_pointer($project, $phy_pointer);
	foreach my $semmap (@{$phymap->{semmap}})
	{
		$string .= '<br><br><b>What is tagged:</b> ';
		$string .= osstag_helper::preventHTMLInjection($semmap->{sem_aspect});
		$string .= '<br><b>Description of what is tagged:</b> ';
		$string .= osstag_helper::preventHTMLInjection($semmap->{sem_desc});

		$string .= '<br><b>Assoziations:</b>     ';
		foreach my $tag (@{$semmap->{assoziations}})
		{
			$string .= $self->{taglist_beforeeachtag};
			$string .= osstag_helper::preventHTMLInjection($tag->{original_tag});
			$string .= $self->{taglist_aftereatchtag};
			$string .= $self->{taglist_betweeneachtag};
		}
		# remove last delimiter
		$string = substr($string, 0, length($string) - length($self->{taglist_betweeneachtag}));
	}

	return $string;
}

sub getTagCloud($)
{
	my $self = shift;
	my (@phymaps) = @_;

	my %tags = ();
	my @alltags;
	foreach my $phymap (@phymaps)
	{
		foreach my $semmap (@{$phymap->{semmap}})
		{
			foreach my $tag (@{$semmap->{assoziations}})
			{
				if( $tags{$tag->{original_tag}})
				{
					$tags{$tag->{original_tag}} += 1;
				}
				else
				{
					$tags{$tag->{original_tag}} = 1;
					push @alltags, $tag->{original_tag};
				}
			}
		}
	}
	my $string;
	foreach my $tag (@alltags)
	{
		$string .= '<font size=+'.($tags{$tag}-1).'>'.osstag_helper::preventHTMLInjection($tag).'</font> ';
	}
	return $string;
}

=head2 storeTagsIfSupplied

	Include this function in each script that might be called via the tag-submission form

	This function expects the following CGI Parameters:

	osstag_aspect 
	osstag_new_desc - the descritption of what is to be tagged
	osstag_new - the list of tags, separated with ',' commas

	osstag_phypointer - the pointer to what physical thing was tagged
	This is usually the name of a hidden field in your html form

	This function is protected via preventSQLInjection*

=cut

sub storeTagsIfSupplied()
{
	my $self = shift;

	if( $self->{cgi}->param('osstag_new'))
	{
		if( length($self->{cgi}->param('osstag_new')) >= 2) # 2 is minimal allowed tag-length
		{
			my $newaspect = $self->{cgi}->param('osstag_aspect');
			my $newdesc = $self->{cgi}->param('osstag_new_desc');
			my $newtaglist = $self->{cgi}->param('osstag_new');
			my $phypointer = $self->{cgi}->param('osstag_phypointer');
			my $project = $self->{cgi}->param('osstag_project');

			my $whatistagged_fk;

			my $phymap = $self->{dbm}->createPhymapForPhy_pointer($project, $phypointer);
			if( $phymap == -1)
			{
				$phymap = new osstag_obj_phymap(0, $phypointer, $project); 
				$self->{dbm}->saveNewPhymap($phymap);
			}

			my $new_semmap = new osstag_obj_semmap(0, $phymap, $newdesc, $newaspect);

			my @tags = split(/ *, */, $newtaglist);

			foreach my $newtag (@tags)
			{	my $assoziation = new osstag_obj_assoziations(0, $new_semmap, $newtag, $newtag); 
				# TODO: normalization of newtag in last parameter
			}
			$self->{dbm}->saveNewSemmap($new_semmap);
		}
	}
}



=head1 License

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License 
version 2 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

=head1 Version

Version: 0.2

Date:    07-JUN-2006

=head1 Author

Author:  Jan Kechel (jan@kechel.de)

=cut

# the end

