<?
/**
 * VoiceOne Settings/Rules/outgoing
 *
 * Frontend to provide access to the rules system in voiceone.
 * @package voiceone
 * @subpackage  settings_rules
 */
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage,$relBasePath;

  //================ PAGE LOGIC ================
  $alert=getAlert();
  
  if ($_GET["cmd"]=="del") {
    $ret=$asterisk->delRule($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdngrp"][""]." Rule deleted</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdngrp"][""]." rule not deleted</div>";
  }

  if ($_GET["cmd"]=="delrule") {
    $ret=$asterisk->delOutgoingRule($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdngrp"][""]."Outgoing Rule deleted</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdngrp"][""]."Outgoing rule not deleted</div>";
  }

  if ($_GET["cmd"]=="up") {
    $ret=$asterisk->moveRule($_GET["id"],1,RULE_OUTGOING);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdngrp"][""]."Outgoing Rule Moved</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdngrp"][""]."Outgoing rule not moved</div>";
    $scriptFade="<script>Fat.fade_element(\"row".$_GET["id"]."\", 30, 500, \"#FFFF33\", \"#FFFFFF\");</script>";
  } 
  
  if ($_GET["cmd"]=="down") {
    $ret=$asterisk->moveRule($_GET["id"],-1,RULE_OUTGOING); 
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdngrp"][""]."Outgoing Rule moved</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdngrp"][""]."Outgoing rule not not moved</div>";
    $scriptFade="<script>Fat.fade_element(\"row".$_GET["id"]."\", 30, 500, \"#FFFF33\", \"#FFFFFF\");</script>";
  }
  
  //get the rules
  $ret=$asterisk->getRule(0,RULE_OUTGOING);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($ret)) 
  foreach($ret as $v) {
    $id=$v["id"];
    $name=$v["name"];
    $description=$v["description"];
    $outgoing=$v["outgoing"];
    $add="<a class=\"admin\" href=\"edit-rule/?rule=$id\">(<!--<img src=\"$relBasePath/public/img/icon_add.gif\" alt=\"Add a new rule\" />-->Add a new Rule)</a> ";
    $delete="<a class=\"admin\" href=\"$PHP_SELF?cmd=del&id=$id\" onclick=\"if (confirm('Are you sure to delete the $name rule?')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a> ";
    $html.="<div class=\"ruleTitle\"><strong>Rule set n° $id: <a href=\"edit/?id=$id\">$name</a></strong> $add $delete</div>\n";
    if ($description!="") $html.="<div class=\"ruleTitle\"><p>$description</p></div>\n";
    $html.="<table class=\"todolist\" cellspacing=0><tr><th colspan=\"4\">&nbsp;</th><th>N°</th><th >Time Segment</th><th >Destination</th><th >Action</th><th >Trunk</th><th >Provider</th></tr>";

    $count=1;
    if (is_array($outgoing)) 
    foreach ($outgoing as $o) {        
      $idr=$o["id"];
      $priority=$o["priority"];
      
      if ($o["timesegment"]=="0") $timesegment="*";
      else {
        $timesegment=$asterisk->getTimeSegment($o["timesegment"]);
        $timesegment=$timesegment[0]["name"];
      }
      
      if ($o["group"]>0) {
        $g=$asterisk->getGroup($o["group"],0);
        if ($o["group"]<6) $g[0]["name"]="[".substr($g[0]["name"],1,-1)."]"; 
        $destination="<b>G:</b> ".$g[0]["name"];
      }
      if ($o["group"]==GROUP_OF_ALL) $destination="*";
      if ($o["entity"]>0) $destination=$asterisk->getEntityName($o["entity"])."<br/>";
      
      $provider=$o["provider"];

      if ($o["action"]==RULE_ALLOW) $action="<span class=\"green\">ALLOW</span>";
      if ($o["action"]==RULE_DENY) $action="<span class=\"red\">DENY</span>";
      
      $trunk=split("\|",$o["resource"]);
      $resource="";
      foreach ($trunk as $t) {
        if ($t=="0") $resource.="*<br>";
        else {
          $tr=$asterisk->getTrunk($t,0);
          $resource.=$tr[0]["name"]."<br>";
        }
      } 
      
      if ($count==1) $button="<td>&nbsp;</td>";
      else $button="<td><a href=\"?cmd=up&id=$idr\"><img src=\"$relBasePath/public/img/icon_up.gif\" alt=\"up\" /></a></td>";
      if ($count==count($outgoing)) $button.="<td>&nbsp;</td>";
      else $button.="<td><a href=\"?cmd=down&id=$idr\"><img src=\"$relBasePath/public/img/icon_down.gif\" alt=\"down\" /></a></td>";
      $button.="<td><a href=\"edit-rule/?rule=$id&id=$idr\"><img src=\"$relBasePath/public/img/icon_edit.gif\" alt=\"edit\" /></a></td>";
      $button.="<td><a href=\"?cmd=delrule&id=$idr\"><img src=\"$relBasePath/public/img/icon_remove.gif\" alt=\"remove\" /></a></td>";
      $html.="<tr class=\"firstofgroup rule\" id=\"row$idr\">$button<td class=\"green\"><b>$idr</b></td><td>$timesegment </td><td class=\"highlight\">$destination </td><td>$action </td><td>$resource </td><td>$provider </td></tr>\n";
      $count++;
    }
    $html.="</table><br/>\n";
  }

  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <?$sel="outgoing";include("../navigation.php");?>

  <div class="SectionHeader">
    <?=$alert?>
    <h1>Outgoing Rules</h1>
    <h3><a href="edit/" class="admin" >New outgoing rule set</a></h3>
  </div>
  <div id="basic_form">
    <?=$html?>
  </div>
  <?
  echo $scriptFade;
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <h1><?=$cwlang["isdngrp"][""]?>Outgoing Rule</h1>
          <?=$cwlang["isdngrp"][""]?>
          This rules will be applied on the extensions....
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================

  $smarty->assign("section", "rules");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

