package WebGUI::Workflow::Activity::RemindAboutPending;


=head1 LEGAL



 -------------------------------------------------------------------
  WebGUI is Copyright 2001-2006 Plain Black Corporation.
 -------------------------------------------------------------------
  Please read the legal notices (docs/legal.txt) and the license
  (docs/license.txt) that came with this distribution before using
  this software.
 -------------------------------------------------------------------
  http://www.plainblack.com                     info@plainblack.com
 -------------------------------------------------------------------

=cut

use strict;
use base 'WebGUI::Workflow::Activity';
use WebGUI::International;
use WebGUI::Mail::Send;
use Time::Local;
use Data::Dumper;

=head1 NAME

Package WebGUI::Workflow::Activity::RemindAboutPending

=head1 DESCRIPTION

Send a message to a specific email address reminding of a pending version tag

=head1 SYNOPSIS

See WebGUI::Workflow::Activity for details on how to use any activity.

=head1 METHODS

These methods are available from this class:

=cut


#-------------------------------------------------------------------

=head2 definition ( session, definition )

See WebGUI::Workflow::Activity::defintion() for details.

=cut 

sub definition {
	my $class = shift;
	my $session = shift;
	my $definition = shift;
	my $i18n = WebGUI::International->new($session, "Workflow_Activity_RemindAboutPending");
	my @Instances = ();

	push(@{$definition}, {
		name=>$i18n->get("activityName"),
		properties=> {
			sendTo => {
				fieldType=>"text",
				label=>$i18n->get("send email to"),
				defaultValue=>"",
				hoverHelp=>$i18n->get("send email to help")
				},
#			subject => {
#				fieldType=>"text",
#				label=>$i18n->get("subject"),
#				defaultValue=>"",
#				hoverHelp=>$i18n->get("subject help")
#				},
			body => {
				fieldType=>"textarea",
				label=>$i18n->get("body"),
				defaultValue=>"",
				hoverHelp=>$i18n->get("body help")
				},
			selectWorkflow => {
				fieldType=>"workflow",
				label=>$i18n->get("selectWorkflow"),
				defaultValue=>"",
				hoverHelp=>$i18n->get("selectWorkflow help")
				},
                        footer => {
                                fieldType=>"textarea",
                                label=>$i18n->get("footer"),
                                defaultValue=>undef,
                                hoverHelp=>$i18n->get("footer help")
                                }
			}
		});

	return $class->SUPER::definition($session,$definition);
}


#-------------------------------------------------------------------

=head2 execute (  )

See WebGUI::Workflow::Activity::execute() for details.

=cut

sub execute {
	my $self = shift;
	my $session = shift;
	my $inbox = WebGUI::Inbox->new($session);
	my $to = $self->get("sendTo");
	my $subject = $self->get("subject");
	my $body = $self->get("body");
	my $footer = $self->get("footer");
	my $runWorkflowId = $self->get("selectWorkflow");
	my @allInstances = ();
	my $versionTag = shift;
        my $instance = shift;
	my $mail;
	my $item;
	my $urlOfSingleAsset;
	my $time = time();
	
	#gather tagId's that are pending
	my @tagIds = $self->session->db->buildArray("select tagId from assetVersionTag where isCommitted=0 and isLocked=1");

	
	foreach my $tagId (@tagIds) {
		 my $tag = WebGUI::VersionTag->new($self->session,$tagId);
  		# send information about version tag

        
        # Lets link them to the approve pending page, IF ther is in fact an asset contained in the version tag

        if ($tag->getAssetCount) {
                $urlOfSingleAsset = $self->session->url->getSiteURL().$self->session->url->page("op=manageRevisionsInTag;workflowInstanceId=".$tag->get("workflowInstanceId").";tagId=".$tag->getId);
        }


#$self->session->errorHandler->warn($tag->get("name"));


		#if tags match and tag is older than 2 days (172800 seconds)
                if ($tag->get("workflowId") eq $runWorkflowId && ($time - $tag->get("creationDate") > 172800)){
                        $mail = WebGUI::Mail::Send->create($self->session, {
                                to=>$to,
                                subject=>"Reminder: ".$tag->get("name")." needs approval!"
                });
		$body.="\n";
                $mail->addText($body.$urlOfSingleAsset."\n\n");
		$mail->addText($footer);
                $mail->send;
	        }
	
   } #end forloop
	return $self->COMPLETE;
}



1;


