%args>
$pua_id
$rule_id
$num_actions => 1
$name
$contact => -1
$starttime => ''
$endtime => ''
%args>
<%once>
use DBI;
use Pg;
%once>
<%init>
my $errors = '';
# if contact == -1 set to null
if($contact == -1) {
$contact = 'null';
}
if ($name eq '') {
$errors .= "You must give this rule a name.
";
}
# if starttime and endtime are valid 24h times criteria = hours:$starttime-$endtime
my $criteria = '';
my $criteriastring = ', ';
if ($starttime ne '' && $endtime ne '') {
$starttime =~ s/\D//g;
$endtime =~ s/\D//g;
if ($starttime ne '' && ($starttime < 2400 && $starttime =~ /\d{4}/)) {
if ($endtime ne '' && ($endtime < 2400 && $endtime =~ /\d{4}/)) {
$criteria = "hours:$starttime-$endtime";
$criteriastring = "criteria=\'hours:$starttime-$endtime\', ";
} else {
$errors .= "End time was not a valid 24 hour time.
";
}
} else {
$errors .= "Start time was not a valid 24 hour time.
";
}
} elsif ($starttime ne '' || $endtime ne '') {
$errors .= "If using time as criteria both start time and end time must be specified.
";
}
# for each action if it's "connect" then concatenate ":$device" to end, concat
# all actions together with spaces
my $action = '';
my $count = 0;
while ($count < $num_actions) {
if ($ARGS{"action$count"} eq "connect") {
$action .= $ARGS{"action$count"} . ":" . $ARGS{"device$count"} . ":::";
} else {
$action .= "check:" . $ARGS{"action$count"} . ":::";
}
$count++;
}
# Eat the last set of seperators.
$action =~ s/:::$//;
if ($errors eq '') {
my $dbh = DBI->connect('dbi:Pg:dbname=pua', 'pua', 'pua') or die "Couldn't connect to database: " . DBI->errstr;
# if rule_id > -1 then "update rule set rule_name='$name', contact_id=$contact,
# criteria='$criteria', action='$action' where rule_id=$rule_id and $pua_id=$pua_id;"
if ($rule_id > -1) {
my $sth = $dbh->prepare("update rule set rule_name='$name', contact_id=$contact $criteriastring action='$action' where rule_id=$rule_id and pua_id=$pua_id;");
$sth->execute or die "Execute failed: " . $sth->errstr;
}
# else "select max(rule_id) from rule"
# and "insert into rule(rule_id, pua_id, rule_name, contact_id, criteria, action)
# values($rule_id, $pua_id, '$name', $contact, '$criteria', '$action');"
else {
my $sth = $dbh->prepare("select max(rule_id) from rule;");
$sth->execute or die "Execute failed: " . $sth->errstr;
$rule_id = $sth->fetchrow_array();
$rule_id++;
$sth = $dbh->prepare("insert into rule(rule_id, pua_id, rule_name, contact_id, criteria, action) values($rule_id, $pua_id, '$name', $contact, '$criteria', '$action');");
$sth->execute or die "Execute failed: " . $sth->errstr;
}
$m->comp('pua_rules.html', pua_id => $pua_id);
} else {
$m->comp('rule_edit.html', %ARGS, errors => $errors);
}
%init>