<%doc>
# 
# $Id: sql-test2.html,v 1.10 2002/03/07 09:20:06 fpmip Exp $
#
# sql-test.html is an example of how to use sql queries

# Authors : Tycho Fruru <tycho@fruru.com>
#           Alexandre Dulaunoy <alex@thinkingsecure.com>
# Copyright (C) 2001, 2002 Tycho Fruru <tycho@fruru.com>
# Copyright (C) 2001, 2002 Conostix S.A. Luxembourg - Tycho Fruru <tycho@conostix.com>
# Copyright (C) 2001, 2002 Alexandre Dulaunoy <alex@foo.be>

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
</%doc>
<%args>
$session

$action=>"none"
$agent_id=>"0"
</%args>

<%perl>

my $agent_query = "select agent_id,agent_type_name,agent_location from agents ".
	"where exists (select login_name from user_rights where login_name = '".$session->{userid}."' and ".
	                "user_rights.agent_id = agents.agent_id and can_view = 't') ".
			"order by agent_id";
my @results = $m->comp('../internal/sql-query.ipfc',query=>$agent_query);

if ($action eq "add") {
    my (@x,@xsorted); 
    my ($cpt,$found,$resultaction,$where); 
    @x = split(/,/, $session->{agentview});
    foreach my $element (@x) {
        if ($agent_id eq $element){
	    $found = "1";
	    $where=$cpt;
	}
	$cpt++;
    }
    
    if ($found eq "") { 
        $x[$cpt] = $agent_id; 
        $session->{'lastaction'}="agent $agent_id added";
    } else {
        $session->{'lastaction'}="agent already in viewer";
    }
    
    @xsorted = sort { $a <=> $b } @x;
    
    $session->{agentview} = join(',', @xsorted);
    
} elsif ($action eq "remove") {
    my (@x,@xsorted);
    my ($cpt, $found,$where);
    @x = split(/,/, $session->{agentview});
    foreach my $element (@x) {
        if ($agent_id eq $element) {
	    $found = "1";
	    $where=$cpt;
	}
        $cpt++;
    }
    if ($found eq "1") {
        splice @x, $where, 1;
	$session->{'lastaction'}="agent $agent_id removed"
    } else {
        $session->{'lastaction'}="agent not in viewer";
    }
    @xsorted = sort { $a <=> $b } @x;
    $session->{agentview} = join(',', @xsorted);
} elsif ($action eq "none") {
    $session->{'lastaction'}="none";
}
</%perl>

<%perl>
my $return = $m->comp('../internal/head-page.html',title=>'Initial workscreen',session=>$session);
</%perl>

<body>
<hr noshade>
<form>
<select name=agent_id>
% foreach my $current_line (@results) {
%     my $cpt=0; 
%     foreach my $entry (@{$current_line}) {
%         if ($cpt==0) {
<option value="<% $entry %>">  <% $entry %> :: 
%             $cpt++;
%         } else {
<% $entry %>
%         }
%     }
</option>
%}
</select>
<input type=submit name=action value="add">
<input type=submit name=action value="remove">
</form>

</table>
<table> 
  <tr bgcolor=#BBBBBB>
    <td><b>ID</b></td>
    <td><b>Type</b></td>
    <td><b>Name</b></td>
    <td><b>Location</b></td> 
  </tr>
% my @x = split(/,/, $session->{agentview});
% foreach my $entry (@x) {
%     my $querya = "select agent_enabled,agent_id,agent_type_name,agent_location,agent_name from agents where agent_id=$entry";
%     my @results = $m->comp('../internal/sql-query.ipfc',query=>$querya);
%     my $cpt;
%     my $agentid;
%     my $current_line;
%     foreach $current_line (@results) {
<tr>
<td> <% $current_line->[1] %></td>
<td> <% $current_line->[2] %></td>
<td> <% $current_line->[4] %></td>
<td> <% $current_line->[3] %></td> 
<tr>
%     }
%}
</table>
<br>

<form action=events.html method=post>
<input type=hidden name=web_agent_id value="<% $session->{agentview} %>">
Search events containing <input type=text name=web_like>, which arrived in the last 
<select name=time>
  <option value="3600"> hour </option>
  <option value="7200"> 2 hours </option>
  <option value="21600"> 6 hours </option>
  <option value="86400"> day </option>
  <option value="604800"> 7 days </option>
  <option value="2052000"> 30 days </option>
  <option value="31536000"> year </option>
  <option value="0"> -unlimited- </option>
</select>
.
<br>
Order these events by :
<select name=order>
  <option value="event_id"> Event ID </option>
  <option value="time"> Time </option>
</select>
, with 
<select name=orderdirection>
  <option value="desc"> Highest/Newest first </option>
  <option value="asc"> Lowest/Oldest first </option>
</select>
.
<br>
Show maximally
<select name=max>
  <option value="50"> 50 </option>
  <option value="100"> 100 </option>
  <option value="250"> 250 </option>
  <option value="0"> unlimited </option>
</select>
results.
<input type=submit name=fulltext value="Full text search in agent : <% $session->{agentview} %>">
</form>

</body>

<%perl>
my $endbody = $m->comp('../internal/end-page.html');
</%perl>

