<?PHP
$operators = array("+" => "Add",
"-" => "Subtract",
"=" => "Assign");
$fields = array("Current_In" => "Hits In",
"Current_Out" => "Hits Out",
"Total_In" => "Total Hits In",
"Total_Out" => "Total Hits Out",
"Current_Prod" => "Productivity",
"Total_Prod" => "Total Productivity");
#"Num_Ratings" => "Number of Ratings",
#"Rating_Total" => "Rating Total");
$types = array("PreRerank" => "Before a Rerank",
"PostRerank" => "After a Rerank",
"PreReset" => "Before a Reset",
"PostReset" => "After a Reset",
"PreTotalReset" => "Before a Total Reset",
"PostTotalReset" => "After a Total Reset");
#"PreRatingReset" => "Before a Rating Reset",
#"PostRatingReset" => "After a Rating Reset");
function GenerateTriggerDescription($type, $action, $username)
{
global $types, $fields, $operators;
list($operand, $operation) = explode('=', $action);
$operator = '=';
$amount = $operation;
$to = 'to';
$account = "for account $username";
if( preg_match("/$operand([\-+])(.*)/", $operation, $matches) )
{
$operator = $matches[1];
$amount = $matches[2];
}
if( preg_match("/rand\((\d+),(\d+)\)/i", $amount, $matches) )
{
$amount = "a random value from $matches[1] to $matches[2]";
}
if( $operator == '-' )
{
$to = 'from';
}
if( $username == 'ALL' )
{
$account = 'for all accounts';
}
return ucfirst(strtolower($types[$type])) . " " . strtolower($operators[$operator]) . " " . $amount . " " . $to . " " . strtolower($fields[$operand]) . " " . $account;
}
?>
<script type="text/javascript">
function checkForm(form)
{
if( form.Run.value == 'DeleteTrigger' )
{
if( !confirm('Are you sure you want to delete this trigger?') )
{
return false;
}
}
else
{
if( !form.Operand.value || !form.Username.value )
{
alert('All fields must be filled in');
return false;
}
}
}
function editTrigger(id)
{
window.open('newwin.php?page=edittrigger&ID=' + id, '_blank', 'menubar=no,height=250,width=470,scrollbars=yes,top=300,left=300');
return false;
}
function changeTo(operator)
{
var to = document.getElementById('to');
if( operator.selectedIndex == 1 )
{
to.innerHTML = 'from';
}
else
{
to.innerHTML = 'to';
}
}
function selectAll(button)
{
var form = document.form;
var state = (button.value == 'Check All' ? true : false);
for( var i = 0; i < form.length; i++ )
{
if( form.elements[i].name == 'ID[]' )
{
form.elements[i].checked = state;
}
}
if( state )
{
button.value = 'Uncheck All';
}
else
{
button.value = 'Check All';
}
}
function setRun(value)
{
document.form.Run.value = value;
}
</script>
<?PHP
if( isset($message) )
{
?>
<div id="message">
<?PHP echo $message; ?>
</div>
<br />
<?PHP
}
?>
<form name="form" action="admin.php" target="_self" method="post" onSubmit="return checkForm(this);">
<div>
<table>
<tr>
<td>
Add a Trigger
</td>
</tr>
<tr>
<td>
<b>Type</b>
</td>
<td>
<select name="Type">
<?PHP
foreach($types as $key => $value)
{
echo "<option value=\"$key\">$value</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<b>Account</b>
</td>
<td>
<input type="text" name="Username" size="20">
</td>
</tr>
<tr>
<td>
<b>Action</b>
</td>
<td>
<select name="Operator" onChange="changeTo(this)">
<?PHP
foreach($operators as $key => $value)
{
echo "<option value=\"$key\">$value</option>\n";
}
?>
</select>
<input type="text" name="Operand" size="10">
<b id="to">to</b>
<select name="Field">
<?PHP
foreach($fields as $key => $value)
{
echo "<option value=\"$key\">$value</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Add Trigger" onClick="setRun('AddTrigger')">
</td>
</tr>
</table>
</div>
<br />
<?PHP
$DB->Connect();
$result = $DB->Query("SELECT * FROM topsites_Triggers ORDER BY Unique_ID");
if( $DB->NumRows($result) > 0 )
{
?>
<table>
<tr>
<td>
Existing Triggers
</td>
</tr>
<tr>
<td>
ID
</td>
<td>
Description
</td>
<td>
Actions
</td>
</tr>
<?PHP
while( $trigger = $DB->NextRow($result) )
{
?>
<tr>
<td>
<input type="checkbox" name="ID[]" value="<?PHP echo $trigger['Unique_ID']; ?>">
<?PHP echo $trigger['Unique_ID']; ?>
</td>
<td>
<?PHP echo GenerateTriggerDescription($trigger['Type'], $trigger['Action'], $trigger['Username']); ?>
</td>
<td>
<a href="" onClick="return editTrigger('<?PHP echo $trigger['Unique_ID']; ?>')">[Edit]</a>
<a href="admin.php?Run=DeleteTrigger&ID[]=<?PHP echo $trigger['Unique_ID']; ?>" onClick="return confirm('Are you sure you want to delete this trigger?')">[Delete]</a>
</td>
</tr>
<?PHP
}
?>
<tr>
<td colspan=3>
<br>
<input type="button" value="Check All" onClick="selectAll(this)">
<input type="submit" value="Delete Selected" onClick="setRun('DeleteTrigger')">
</td>
</tr>
</table>
<?PHP
}
$DB->Free($result);
$DB->Disconnect();
?>
<input type="hidden" name="Run" value="">
</form>