<%doc>

-- Device tasks --

</%doc>
%
<%attr>
title => 'Device Tasks' 
</%attr>
%
%
%#######################################################################
%#
%# Args section
%#
%#######################################################################
<%args>
$device_search  => undef
$zone_id        => undef
$ip_search      => undef
$mac_search     => undef
$user           => $ui->get_current_user($r);
$submit         => undef
$show_tasks     => undef
$showheader     => 1
</%args>
%
%
%
%#######################################################################
%#
%# INIT section
%#
%#######################################################################
%
<%init>
my $DEBUG = 0;
print '%ARGS is  <pre>', Dumper(%ARGS), '</pre><br>' if $DEBUG;
my $MAC  = Netdot->get_mac_regex();
my @list;

$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 );
$ARGS{show_tasks} = $show_tasks;
</%init>


<%perl>

if ( $submit eq 'Find' ){
    # Get rid of spaces
    $device_search =~ s/\s+//g;
    if ( $device_search =~ /$MAC/ ){
	$m->comp('.show_tasks', %ARGS, user=>$user);
	$m->comp('/management/mac.html', search=>$device_search, dowindow=>1 );
    }elsif ( Ipblock->matches_ip($device_search) ){
	if ( scalar(@list = Device->search(name=>$device_search)) == 1 ){
	    my $o  = $list[0];
	    my $id = $o->id;
	    $m->comp('/management/device.html', id => $id, user=>$user );
	}else{
	    $m->comp('/management/address_tasks.html', submit=>1, search_address=>$device_search, user=>$user, dowindow=>1 );
	}
    }else{
	$m->comp('.show_tasks', %ARGS, user=>$user);
	my %args = (name=>$device_search);
	$args{zone} = $zone_id if $zone_id;
	unless ( (@list = Device->search_like(%args)) ){
	    $m->comp('/generic/no_search_results.html', search=>$device_search);
	}
	if ( scalar(@list) == 1 ){
	    # Don't offer list.  Just display device
	    my $o  = $list[0];
	    my $id = $o->id;
	    $m->comp('/management/device.html', id => $id, user=>$user );
	}else{
	    $m->comp('/generic/show_search_results.mhtml', search=>$device_search, list=>\@list, dowindow=>1 );
	}
    }
}else{
    $m->comp('.show_tasks', %ARGS, user=>$user);
}


</%perl>

%################################################################
%# show_tasks sub-component
%################################################################

<%def .show_tasks>

<%args>
$show_tasks     => undef
$hideheader     => undef
$device_search  => undef
$ip_search      => undef
$mac_search     => undef
$view           => 'search'
$zone_id        => undef
$user
</%args>

<%init>
my @communities = @{Netdot->config->get('DEFAULT_SNMPCOMMUNITIES')};
my @all_zones = Zone->retrieve_all();

my $manager = $ui->get_permission_manager($r);
</%init>

<div id="sectiontools" <% $hideheader %>>
<div class="container">
    <div class="containerheadleft">
        Device Tasks
    </div>
    <div class="containerheadright">

%   if ( $manager && $manager->can($user, 'access_admin_section', 'device_tasks:new') ){
%       if ( $view eq 'search' ){ 
	    <a href="device_tasks.html?view=new">[new]</a>
%       }elsif ( $view eq 'new' ){
            <a href="device_tasks.html?view=search">[search]</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="medium">
            <legend>Find Devices</legend>
            <form action="device_tasks.html" method="POST">
                <p>
                <label for="search">Name/IP/MAC:</label>
                <input type="text" name="device_search" class="longtxt" value="<% $device_search %>"> 
                </p>
                <p>
                <label for="Select">Names within:</label>
                <select name="zone_id">
		<option value="0">All Zones</option>
%               foreach my $zone ( sort { $a->name cmp $b->name } @all_zones ){
%                   if ( $zone->id eq $zone_id ){
                        <option value="<% $zone->id %>" SELECTED><% $zone->get_label %></option>
%                   }else{
                        <option value="<% $zone->id %>"><% $zone->get_label %></option>
%                   }
%               }
                </select>
                <input name="submit" value="Find" class="btn" type="submit">
                </p>
            </form>
        </fieldset>
%   }elsif ( $view eq 'new' ){
%       unless ( $manager && $manager->can($user, 'access_admin_section', 'device_tasks.html:new') ){
%           $m->comp('/generic/error.mhtml', error=>'You do not have permission to add new devices');
%       }
        <fieldset class="medium">
            <legend>Discover Device</legend>
            <form action="updatedevice.html" method="POST">
                <p>
                <label for="host">Name/IP:</label>
                <input type="text" name="host" class="longtxt" value="">
                </p>
                <p>
                <label for="version">SNMP Version:</label>
                <select name="version" id="version" onChange="showSnmpV3Args('version', 'v3args', 'v2args')">
%               foreach my $version ( qw(1 2 3) ){
%                   if ( $version eq $ui->config->get('DEFAULT_SNMPVERSION') ){
 	                <option value="<% $version %>" SELECTED><% $version %></option>
%                   }else{
 	                <option value="<% $version %>"><% $version %></option>
%  		    }
%               }
                </select>
                </p>
%		if ( $ui->config->get('DEFAULT_SNMPVERSION') == 3 ){
                    <div id="v3args" style="display:block">
%               }else{
                    <div id="v3args" style="display:none">
%               }
                <p>
                <label for="sec_name">Sec Name:</label>
                <input type="text" name="sec_name" class="txt" value="<% $ui->config->get('DEFAULT_SNMP_SECNAME') %>">
                </p>
                <p>
                <label for="sec_level">Sec Level:</label>
                <select name="sec_level">
%               foreach my $seclevel ( qw(noAuthNoPriv authNoPriv authPriv) ){
%		    if ( $ui->config->get('DEFAULT_SNMP_SECLEVEL') eq $seclevel ){
		        <option value="<% $seclevel %>" selected="selected"><% $seclevel %></option>
%		    }else{
		        <option value="<% $seclevel %>"><% $seclevel %></option>
%		    }
%		}
		</select>
                </p>
                <p>
                <label for="auth_proto">Auth Proto:</label>
                <select name="auth_proto">
%               foreach my $proto ( qw(MD5 SHA) ){
%		    if ( $ui->config->get('DEFAULT_SNMP_AUTHPROTO') eq $proto ){
		        <option value="<% $proto %>" selected="selected"><% $proto %></option>
%		    }else{
		        <option value="<% $proto %>"><% $proto %></option>
%		    }
%		}
		</select>
                </p>
                <p>
                <label for="auth_pass">Auth Pass:</label>
                <input type="password" name="auth_pass" class="txt" value="<% $ui->config->get('DEFAULT_SNMP_AUTHPASS') %>">
                </p>
                <p>
                <label for="">Priv Proto:</label>
                <select name="priv_proto">
%               foreach my $proto ( qw(DES AES) ){
%		    if ( $ui->config->get('DEFAULT_SNMP_PRIVPROTO') eq $proto ){
		        <option value="<% $proto %>" selected="selected"><% $proto %></option>
%		    }else{
		        <option value="<% $proto %>"><% $proto %></option>
%		    }
%		}
		</select>
                </p>
                <p>
                <label for="priv_pass">Priv Pass:</label>
                <input type="password" name="priv_pass" class="txt" value="<% $ui->config->get('DEFAULT_SNMP_PRIVPASS') %>">
                </p>
                <p>
                </div>
%		if ( $ui->config->get('DEFAULT_SNMPVERSION') == 3 ){
		    <div id="v2args" style="display:none">
%	        }else{
		    <div id="v2args" style="display:block">
%	        }
                <p>
                <label for="comstr">SNMP Community:</label>
                <select name="comstr" id="comstr" onChange="otherOption('comstr','back','other_comstr');">
%               foreach my $comm ( @communities ){
 	            <option name="<% $comm %>"><% $comm %></option>
%               }
                <option name="Other" id="Other" value="Other">Other</option>
                </select>
                <input type="button" style="visibility:hidden" name="back" id="back" value="<" onClick="backSelectOpt('comstr','back','other_comstr');">
                <label for="other_comstr"></label>
                <input type="text" style="visibility:hidden" name="other_comstr" id="other_comstr" value="Other" size="13" onFocus="if( this.value == 'Other' ){this.value = '';} return true;">
                </p>
		</div>
		<p><br>
                <input type="hidden" name="action" value="discover">
                <input type="submit" name="submit" class="btn" value="discover">
            </form>    
        </fieldset>

        <fieldset class="medium">
            <legend>Add Device manually</legend>
            <form action="device.html" method="POST">
                <p>
                <label for="newhost">Name:&nbsp;&nbsp;&nbsp;&nbsp;</label>
                <input type="text" name="newhost" class="longtxt" value="">
                <input type="hidden" name="deviceadd" value="add">
                <input type="submit" name="submit" class="btn" value="add">
                </p>
            </form>
        </fieldset>
%   }

        </td></tr></table>
    </div> <!-- close containerbody -->
</div> <!-- close container -->
</div> <!-- close sectiontools -->
</%def>


