<%doc>

Search and create addresses and blocks of addresses

</%doc>
%
<%attr>
title => 'Address Tasks' 
</%attr>
%
%
%#######################################################################
%#
%# Args section
%#
%#######################################################################
<%args>
$search_address  => undef
$search_regex    => undef
$search_other    => undef
$search          => undef
$search_live     => undef
$user            => $ui->get_current_user($r);
$submit          => undef
$show_tasks      => undef
$showheader      => 1
$task            => 'search'
$dowindow        => undef
$view            => 'search'
</%args>
%
%
%
%#######################################################################
%#
%# INIT section
%#
%#######################################################################
%
<%init>
my $DEBUG = 0;
print '%ARGS is  <pre>', Dumper(%ARGS), '</pre><br>' 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 $covering_block;
my @list;
my $live_info;
my $manager = $ui->get_permission_manager($r);
</%init>

<div id="sectiontools" <% $hideheader %>>
  <div class="container">
    <div class="containerheadleft">
        Address Space Tasks
    </div>
    <div class="containerheadright">
%   if ( $manager && $manager->can($user, 'access_admin_section', 'address_tasks:new') ){
%        my @views = qw(search new tree);
%        foreach my $v ( @views ){
%	     next if $v eq $view;
%		 my $arg = "&onload=focus-ipblock" if $v eq "new";
             <a href="address_tasks.html?view=<% $v.$arg %>">[<% $v %>]</a>
%        }
%   }
%       print_showtaskslink($show_tasks);
     </div>
    <div class="containerbody" id="tasks" style="display:<% ($show_tasks eq "show"?"block":"none") %>">

    <table><tr><td>
%   if ( $view eq 'search' ){
        <fieldset class="large">
            <legend>Search</legend>
            <form action="address_tasks.html" method="POST">
                <p>
                <label for="Find">IP[/prefix]:</label>
                <input type="text" name="search_address" class="longtxt" value=""> 
                <input name="submit" value="Search" class="btn" type="submit">
                </p>
            </form>
            <form action="address_tasks.html" method="POST">
                <p>
                <label for="Find">IP regex:</label>
                <input type="text" name="search_regex" class="longtxt" value=""> 
                <input name="submit" value="Search" class="btn" type="submit">
                </p>
            </form>
            <form action="address_tasks.html" method="POST">
                <p>
                <label for="Find">Keywords:</label>
                <input type="text" name="search_other" class="longtxt" value=""> 
                <input name="submit" value="Search" class="btn" type="submit">
                </p>
            </form>
        </fieldset>
%   }elsif ( $view eq 'new' ){				     

%       unless ( $manager && $manager->can($user, 'access_admin_section', 'address_tasks.html:new') ){
%	    $m->comp('/generic/error.mhtml', error => "You do not have permission to add new blocks here");    
%       }

        <fieldset class="large">
            <legend>Add IP Block</legend>
            <form action="ip.html" method="POST" name="ipblock">
                <input type="hidden" name="_action" value="ADD_BLOCK">
                <p>
                <label for="add_block">IP[/prefix]:</label>
                <input type="text" name="add_block_prefix" class="longtxt" value="">
                </p>
                <p>
                <label for="block_owner">Owner:</label>
                <select name="block_owner" id="block_owner">
                <option value="">-- Select --</option>
%               my @allents = Entity->retrieve_all; 
%               @allents = sort { $a->name cmp $b->name } @allents;
%               foreach my $ent ( @allents ){
                    <option value="<% $ent->id %>"><% $ent->name %></option>                
%               }
                </select>
                <a class="hand" onClick="openinsertwindow('table=Entity&select_id=block_owner&selected=1&dowindow=1')">[new]</a>
                </p>
                <p>
                <label for="block_used_by">Used By:</label>
                <select name="block_used_by" id="block_used_by">
                <option value="">-- Select --</option>
%               foreach my $ent ( @allents ){
                    <option value="<% $ent->id %>"><% $ent->name %></option>                
%               }
                </select>
                <a class="hand" onClick="openinsertwindow('table=Entity&select_id=block_used_by&selected=1&dowindow=1')">[new]</a>
                </p>
                <p>
                <label for="block_status">Status:</label>
                <select name="block_status" id="block_status">
                <option value="">-- Select --</option>
%               my @allstatus = qw( Subnet Container Static Reserved ); 
%               foreach my $status ( @allstatus ){
                    <option value="<% $status %>"><% $status %></option>                
%               }
                </select>
                </p>
                <p>
                <label for="block_description">Description:</label>
                <input type="text" class="longtxt" name="block_description">
                </p>
		<p>
                <input name="submit" value="Save" class="btn" type="submit">
                </p>
            </form>
        </fieldset>
%   }elsif ( $view eq 'tree' ){
        <fieldset class="medium">
            <legend>Rebuild IP tree</legend>
            <form name="netdotform" action="ip.html" method="POST">
	        <input type="hidden" name="_action" value="REBUILD_TREE">
                <p>
	        <label for="tree_version" >Tree Version:</label>
	        <select name="tree_version">
%	        foreach my $version ('4', '6', 'all'){
		    <option value="<% $version %>"><% $version %></option>
%               }
                </select>
                <input type="submit" name="submit" class="btn" value="Go">
                </p>
            </form>
        </fieldset>
%   }
        </td></tr></table>
    </div> <!-- close containerbody -->
  </div> <!-- close container -->
</div> <!-- close sectiontools -->

<%perl>

#######################################################################################
# Search
#
#######################################################################################

if ( $submit ){
    if ( $search_address ){
	$search = $search_address;
	# Remove trailing and leading spaces
	$search = $ui->rem_lt_sp($search);
	# These might throw exceptions
	eval { @list = Ipblock->search(address=>$search) };
	if ( my $e = $@ ){
	    $m->comp('/generic/error.mhtml', error=>$e);
	}
	if ( !@list ){
	    # Return next existing higher block in the tree if not found
	    $covering_block = Ipblock->get_covering_block(address=>$search);
	}
    }elsif ( $search_regex ){
	$search_regex =~ s/\s+//g; # Remove spaces
	eval { qr/$search_regex/i };
	if ( my $e = $@ ){
	    $m->comp('/generic/error.mhtml', error=>"Your regular expression doesn't seem valid: $e");
	}
	$search = $search_regex;
	@list = Ipblock->search_like(address=>$search);
    }elsif ( $search_other ){
	$search = $search_other;
	$search = $ui->rem_lt_sp($search);
	@list = Ipblock->keyword_search($search);
    }elsif( $search_live ){
	eval { $live_info = Device->search_address_live(ip=>$search_live) };
	if ( my $e = $@ ){
	    $m->comp('/generic/error.mhtml', error=>$e);
	}
    }elsif( $submit eq "Search" ){
	$m->comp('/generic/no_search_criteria.html');
    }   
}
</%perl>      

<div class="container">
  <div class="containerbody">
% if ( $search ){
%   if ( @list ){
      <div class="containerhead">
        Query <em><% $search %></em> returned: <% scalar(@list) %> matches
      </div>
      <& ipblock_list.mhtml, objects => \@list, parent=>0  &>
%   }else{
        <div class="containerhead"><em><% $search %></em> not found</div>
%       if ( $covering_block ){
            <p>
            <div class="container">
              <div class="containerhead">Closest Covering Block:</div>
              <div class="containerbody">
                <& ipblock_list.mhtml, objects =>[$covering_block], parent=>0  &>
              </div>
            </div>
%       }

%	if ($search_address){
%           # We do not want to offer a live search unless it's an end-node address
%           my $nip = NetAddr::IP->new($search_address);
%           if ( defined($nip) && 
%		 (($nip->version == 4 && $nip->masklen == 32) ||
%	         ($nip->version == 6 && $nip->masklen == 128)) ){
                <p>
                <div class="container">
                <div class="containerhead">Live Search:</div>
                <div class="containerbody">
                <p>Would you like to perform a "live" search in the network? (It might take a while).
                <p>
                <form action="address_tasks.html" method="POST">
                 <input name="submit" value="Live Search" class="btn" type="submit">
                 <input type="hidden" name="search_live" value="<% $search %>">
                </form>
                </div>
                </div>
%           }
%      }
%   }
% }
  </div>
</div>


% if ( $search_live && $live_info ){
    <& live_results.mhtml, info=>$live_info &>
% }

