<?
/**
 * VoiceOne Settings/Rules/incoming
 *
 * 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->delIncomingRule($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdngrp"][""]."Incoming Rule deleted</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdngrp"][""]."Incoming rule not deleted</div>";
  }

  if ($_GET["cmd"]=="up") {
    $ret=$asterisk->moveRule($_GET["id"],1,RULE_INCOMING);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdngrp"][""]."Incoming Rule Moved</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdngrp"][""]."Incoming 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_INCOMING);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdngrp"][""]."Incoming Rule moved</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdngrp"][""]."Incoming 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_INCOMING);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($ret)) 
  foreach($ret as $v) {
    $id=$v["id"];
    $name=$v["name"];
    $description=$v["description"];
    $incoming=$v["incoming"];
    $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 >Caller N</th><th >Trunk</th><th >Number</th><th >Macro</th><th >Params</th><th>&nbsp;</th></tr>";

    $count=1;
    if (is_array($incoming)) 
    foreach ($incoming as $i) {        
      $idr=$i["id"];
      $priority=$i["priority"];
      $duration=intval($i["duration"]);

      //caller id
      $callerid=$i["callerid"];
      if ($callerid=="") $callerid="*";
      
      //time segment
      if ($i["timesegment"]=="0") $timesegment="*";
      else {
        $timesegment=$asterisk->getTimeSegment($i["timesegment"]);
        $timesegment=$timesegment[0]["name"];
      }

      //get trunk name
      if ($i["from_channel"]!="" and $i["from_channel"]!=0) {
        $t=$asterisk->getTrunk($i["from_channel"],0);
        $trunk=$t[0]["name"];
        if ($trunk=="") $trunk="Unknown (may be deleted)";
      } else $trunk="*";
        
      //get phone numbers
      if (count($i["from_number"])==0 or $i["from_number"]=="" or in_array("",$i["from_number"]) ) $number="*";
      else $number=implode("<br/>",$i["from_number"]);
      
      //get macro name
      $parameter="";
      if ($i["macro"]!="" and $i["macro"]!=0) {
        $m=$asterisk->getMacro($i["macro"]);
        $macro=$m[0]["label"];
        $p=split("\|",$i["parameter"]);
        foreach ($p as $param) $parameter.="$param<br/>";
        if ($parameter=="") $parameter="[none]";
      } else {
        $macro="[none]";
        $parameter="[none]";
      }
            
      //buttons Up and Down      
      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($incoming)) $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>";

      if ($duration=="0") $stop="<img src=\"$relBasePath/public/img/stop.gif\" alt=\"stop executing after this rule\" />"; 
      else $stop="<img src=\"$relBasePath/public/img/continue.gif\" alt=\"continue executing after this rule\" />";
      
      $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=\"green\">$callerid</td><td> $trunk</td><td> $number</td><td class=\"highlight\"> $macro</td><td> $parameter</td><td>$stop</td></tr>\n";
      $count++;
    }
    $html.="</table><br/>\n";
  }

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

  <div class="SectionHeader">
    <?=$alert?>
    <h1>Incoming calls Rules</h1>
    <h3><a href="edit/" class="admin" >New incoming 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"][""]?>Incoming Rule</h1>
          <?=$cwlang["isdngrp"][""]?>
          This rules will be applied on incoming calls
        </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');
?>

