%doc>
Entities
%doc>
%
<%attr>
title => 'Entities'
%attr>
%
%
%#######################################################################
%#
%# Args section
%#
%#######################################################################
<%args>
$search => undef
$search_entities => undef
$user => $ui->get_current_user($r);
$submit => undef
$show_tasks => undef
$showheader => 1
$view_entity => 'search'
$_action => undef
#add a new entity
@entityroles => undef
$sitestatus => undef
$clstatus => undef
$entity_name => undef
$site => undef
$newsite => undef
$contactlist => undef
$newcontactlist => undef
%args>
%
%
%
%#######################################################################
%#
%# INIT section
%#
%#######################################################################
%
<%init>
my $DEBUG = 0;
print '%ARGS is
', Dumper(%ARGS), '
' if $DEBUG;
$show_tasks = $show_tasks || $user->getAttribute("SHOW_TASKS");
if ( $show_tasks eq "" ) {
$user->setAttribute($r, "SHOW_TASKS", "show");
$show_tasks = "show";
}
*print_showtaskslink = $m->comp('SELF:.sub_print_showtaskslink');
my $hideheader = 'style="display:none"' if ( !$showheader );
my @list;
my $entityid;
my $manager = $ui->get_permission_manager($r);
%init>
<%perl>
#######################################################################################
# Search
#
#######################################################################################
if ( $_action eq "SEARCH" && $submit ){
# Remove trailing and leading spaces
if ( $search_entities ){
$search = $search_entities;
$search =~ s/^\s*(.*)\s*$/$1/g;
my %idx;
map { $idx{$_->id} = $_ } Entity->search_like(name => $search);
map { $idx{$_->id} = $_ } Entity->search_like(short_name => $search);
map { $idx{$_->id} = $_ } Entity->search_like(acctnumber => $search);
# deeper search, within entitysite
my @sites = Site->search_like(name => $search);
foreach my $s ( @sites ) {
my @entitysites = EntitySite->search(site => $s->id);
foreach my $es (@entitysites) {
my $entid = $es->entity;
map { $idx{$_->id} = $_ } Entity->search(id => $entid);
}
}
@list = values %idx;
}else {
$m->comp('/generic/no_search_criteria.html');
}
}
#######################################################################################
# Add Entities
#
#######################################################################################
if ( $_action eq "INSERT" && $submit ){
# Remove trailing and leading spaces
if ( $entity_name ) { $entity_name =~ s/^\s*(.*)\s*$/$1/g; }
if ( $newsite ) { $newsite =~ s/^\s*(.*)\s*$/$1/g; }
if ( $newcontactlist ) { $newcontactlist =~ s/^\s*(.*)\s*$/$1/g; }
if ( Entity->search(name=>$entity_name)->first ){
$m->comp("/generic/error.mhtml", error=>"Entity $entity_name already exists!");
}
if ( ($clstatus eq "new") && ($newcontactlist) && ($newcontactlist ne "") ) {
if ( $contactlist = ContactList->search(name=>$newcontactlist)->first ){
# There's already one with that name. Just use that.
}else{
eval {
$contactlist = ContactList->insert({name=>$newcontactlist});
};
if ( my $e = $@ ){
$m->comp("/generic/error.mhtml", error=>$e);
}
}
}
if ( ($sitestatus eq "new") && ($newsite) && ($newsite ne "") ) {
if ( $site = Site->search(name=>$newsite)->first ){
# Use the existing one
}else{
eval {
$site = Site->insert({name=>$newsite, contactlist=>$contactlist});
};
if ( my $e = $@ ){
$m->comp("/generic/error.mhtml", error=>$e);
}
}
}
if ( ($entity_name) && ($entity_name ne "") ) {
my $newentity;
eval {
$newentity = Entity->insert({name=>$entity_name, contactlist=>$contactlist});
};
if ( my $e = $@ ){
$m->comp("/generic/error.mhtml", error=>$e);
}
$entityid = $newentity->id;
}
if ( ($entityid) && ($site) ) {
eval {
EntitySite->insert({entity=>$entityid, site=>$site});
};
if ( my $e = $@ ){
$m->comp("/generic/error.mhtml", error=>$e);
}
}
if ( ($entityid) && @entityroles ) {
eval {
foreach my $r ( @entityroles ) {
if ( $r ) {
EntityRole->insert({entity=>$entityid, type=>$r});
}
}
};
if ( my $e = $@ ){
$m->comp("/generic/error.mhtml", error=>$e);
}
}
$_action = 'VIEW';
}
%perl>
% if ( $search ){
% if ( scalar @list > 1 ){
Query <% $search %> returned: <% scalar(@list) %> matches
<& /generic/sortresults.mhtml, object=>\@list, page=>'view.html', withedit=>0 &>
% }elsif ( scalar @list == 1 ){
% $entityid = $list[0]->id;
% $m->comp('/generic/view.html', table=>"Entity", id=>$entityid);
% }else{
% $m->comp('/generic/no_search_results.html', search=>$search);
% }
% }
% if ( $_action eq "VIEW" && defined $entityid ){
% $m->comp('/generic/view.html', table=>"Entity", id=>$entityid);
% }