package WebGUI::Asset::Wobject::NewWobject;

$VERSION = "1.0.0";

#-------------------------------------------------------------------
# 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
#-------------------------------------------------------------------

use strict;
use Tie::IxHash;
use WebGUI::International;
use WebGUI::Utility;
use base 'WebGUI::Asset::Wobject';

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

=head2 definition ( )

defines wobject properties for New Wobject instances.  You absolutely need 
this method in your new Wobjects.  If you choose to "autoGenerateForms", the
getEditForm method is unnecessary/redundant/useless.  

=cut

sub definition {
	my $class = shift;
	my $session = shift;
	my $definition = shift;
	my $i18n = WebGUI::International->new($session, 'NewWobject');
	my %properties;
	tie %properties, 'Tie::IxHash';
	%properties = (
		templateId =>{
			
			#See the list of field/control types in /lib/WebGUI/Form/
			fieldType=>"template",  
			
			defaultValue=>'NewWobjectTmpl00000001',
			tab=>"display",
			
			#www_editSave will ignore anyone's attempts to update this field if this is set to 1
			noFormPost=>0,  
			
			#This is an option specific to the template fieldType.
			namespace=>"NewWobject", 
			
			#This is what will appear when the user hovers the mouse over the label 
			# of your form field.
			hoverHelp=>$i18n->get('templateId label description'),
			
			# This is the text that will appear to the left of your form field.
			label=>$i18n->get('templateId label'),
		}
	);
	push(@{$definition}, {
		assetName=>$i18n->get('assetName'),
		icon=>'newWobject.gif',
		autoGenerateForms=>1,
		tableName=>'NewWobject',
		className=>'WebGUI::Asset::Wobject::NewWobject',
		properties=>\%properties
		});
        return $class->SUPER::definition($session, $definition);
}


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

=head2 duplicate ( )

duplicates a New Wobject.  This method is unnecessary, but if you have 
auxiliary, ancillary, or "collateral" data or files related to your 
wobject instances, you will need to duplicate them here.

=cut

sub duplicate {
	my $self = shift;
	my $newAsset = $self->SUPER::duplicate(@_);
	return $newAsset;
}

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

=head2 getEditForm ( )

returns the tabform object that will be used in generating the edit page for New Wobjects.
This method is optional if you set autoGenerateForms=1 in the definition.

=cut

sub getEditForm {
	my $self = shift;
	my $tabform = $self->SUPER::getEditForm();

	$tabform->getTab("display")->template(
		-value=>$self->getValue("templateId"),
		-label=>WebGUI::International::get("template_label","NewWobject"),
		-namespace=>"NewWobject"
	);
	
	return $tabform;
}

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

=head2 prepareView ( )

See WebGUI::Asset::prepareView() for details.

=cut

sub prepareView {
	my $self = shift;
	$self->SUPER::prepareView();
	my $template = WebGUI::Asset::Template->new($self->session, $self->get("templateId"));
	$template->prepare;
	$self->{_viewTemplate} = $template;
}


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

=head2 purge ( )

removes collateral data associated with a NewWobject when the system
purges it's data.  This method is unnecessary, but if you have 
auxiliary, ancillary, or "collateral" data or files related to your 
wobject instances, you will need to purge them here.

=cut

sub purge {
	my $self = shift;
	#purge your wobject-specific data here.  This does not include fields 
	# you create for your NewWobject asset/wobject table.
	return $self->SUPER::purge;
}

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

=head2 view ( )

method called by the www_view method.  Returns a processed template
to be displayed within the page style.  

=cut

sub view {
	my $self = shift;
	my $session = $self->session;	

	#This automatically creates template variables for all of your wobject's properties.
	my $var = $self->get;
	
	 #This is where you will store your wobject's extras files, any javascript, css, or any other files your wobject may use.
	$var->{'extrasFolder'} = $session->url->extras("/wobject/NewWobject"); 
	
	#This is an example of debugging code to help you diagnose problems.
	#WebGUI::ErrorHandler::warn($self->get("templateId")); 
	
	return $self->processTemplate($var, undef, $self->{_viewTemplate});
}

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

=head2 www_edit ( )

Web facing method which is the default edit page.  This method is entirely
optional.  Take it out unless you specifically want to set a submenu in your
adminConsole views.

=cut

#sub www_edit {
#   my $self = shift;
#   return WebGUI::Privilege::insufficient() unless $self->canEdit;
#   return WebGUI::Privilege::locked() unless $self->canEditIfLocked;
#   $self->getAdminConsole->setHelp("new wobject add/edit", "NewWobject");
#   return $self->getAdminConsole->render($self->getEditForm->print,
#                  WebGUI::International::get("edit_title","NewWobject"));
#}



#-------------------------------------------------------------------
# Everything below here is to make it easier to install your custom
# wobject, but has nothing to do with wobjects in general
#-------------------------------------------------------------------
# cd /data/WebGUI/lib
# perl -MWebGUI::Asset::Wobject::NewWobject -e install www.example.com.conf [ /path/to/WebGUI ]
# 	- or -
# perl -MWebGUI::Asset::Wobject::NewWobject -e uninstall www.example.com.conf [ /path/to/WebGUI ]
#-------------------------------------------------------------------


use base 'Exporter';
our @EXPORT = qw(install uninstall);
use WebGUI::Session;

#-------------------------------------------------------------------
sub install {
	my $config = $ARGV[0];
	my $home = $ARGV[1] || "/data/WebGUI";
	die "usage: perl -MWebGUI::Asset::Wobject::NewWobject -e install www.example.com.conf\n" unless ($home && $config);
	print "Installing asset.\n";
	my $session = WebGUI::Session->open($home, $config);
	$session->config->addToArray("assets","WebGUI::Asset::Wobject::NewWobject");
	$session->db->write("create table NewWobject (
		assetId varchar(22) binary not null,
		revisionDate bigint not null,
		primary key (assetId, revisionDate)
		)");
	$session->var->end;
	$session->close;
	print "Done. Please restart Apache.\n";
}

#-------------------------------------------------------------------
sub uninstall {
	my $config = $ARGV[0];
	my $home = $ARGV[1] || "/data/WebGUI";
	die "usage: perl -MWebGUI::Asset::Wobject::NewWobject -e uninstall www.example.com.conf\n" unless ($home && $config);
	print "Uninstalling asset.\n";
	my $session = WebGUI::Session->open($home, $config);
	$session->config->deleteFromArray("assets","WebGUI::Asset::Wobject::NewWobject");
	my $rs = $session->db->read("select assetId from asset where className='WebGUI::Asset::Wobject::NewWobject'");
	while (my ($id) = $rs->array) {
		my $asset = WebGUI::Asset->new($session, $id, "WebGUI::Asset::Wobject::NewWobject");
		$asset->purge if defined $asset;
	}
	$session->db->write("drop table NewWobject");
	$session->var->end;
	$session->close;
	print "Done. Please restart Apache.\n";
}


1;
