<?
/**
 * VoiceOne Settings/Rules/incoming/edit-rule
 *
 * Frontend to provide access to the rules system in voiceone.
 * @package voiceone
 * @subpackage  settings_rules
 */
  include ("../../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
 	sajax_init();
  
  //================ PAGE LOGIC ================
  if (count($_POST)>0) {
    $entry=Array(
      "id"  => $_POST["id"],
      "rule" => $_POST["rule"],
      "priority" => $_POST["priority"],
      "timesegment" => $_POST["timesegment"], 
      "callerid" => $_POST["callerid"], 
      "from_channel" => $_POST["trunk"], 
      "from_number" => $_POST["number"], 
      "macro" => $_POST["macro"], 
      "duration" => $_POST["duration"] 
    );

    //merge the parameter
    if (is_array($_POST["macro_param"]))
    foreach ($_POST["macro_param"] as $k=>$v) $p.="$k=$v|";
    $p=substr($p,0,-1);
    $entry["parameter"]=$p;

    //save the group informations
    //echo "<pre>";  print_r($entry); die;
    $ret=$asterisk->putIncomingRule($entry);
    if ($ret>0) {
      $alert="<div class=\"AlertGood\">".$cwlang["isdngrp"][""]." ok </div>";
      $_GET["id"]=$ret;
    } else $alert="<div class=\"AlertBad\">".$cwlang["isdngrp"][""]."no </div>";
    
    goToMainPage($alert);
  }
  //--------------------------------------------------------------------------
  
  //if modify, read the group informations
  if (isset($_GET["id"])) {
    $ret=$asterisk->getIncomingRule($_GET["id"]);
    if ($err=$asterisk->getError()) die("Error: $err");
    $id=$ret[0]["id"];
    $rule=$ret[0]["rule"];
    $priority=$ret[0]["priority"];
    $timesegment=$ret[0]["timesegment"];  
    $callerid=$ret[0]["callerid"];  
    $trunk=$ret[0]["from_channel"]; 
    $number=implode("|",$ret[0]["from_number"]);  
    $macro=$ret[0]["macro"];  
    $parameter=$ret[0]["parameter"];  
    $duration=$ret[0]["duration"]; 
  }  

  //get the time segment available
  $tsHtml.="<option value=\"0\">[any time]</option>\n";
  $ret=$asterisk->getTimeSegment(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($ret)) 
  foreach ($ret as $ts) {
    $id=$ts["id"];
    $name=$ts["name"];
    if ($id==$timesegment) $selected=" selected=\"selected\" ";
    else $selected="";
    $tsHtml.="<option value=\"$id\" $selected>$name</option>\n";
  }
  
  $id=$_REQUEST["id"];
  $rule=$_REQUEST["rule"];  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  
  <?$sel="incoming";include("../../navigation.php");?>
  
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=$cwlang["isdngrp"][""]?>Incoming rule set</h1>
    <h3><a href="../" class="admin" ><?=$cwlang["isdngrp"]["return"]?></a></h3>
  </div>
  
  <div id="basic_form">
    <form action="<?=$PHP_SELF?><?if (isset($_GET["id"])) echo "?id=".$_GET["id"];?>" method="post">
      
    	<h2><?=$cwlang["isdngrp"][""]?>1) When and who...</h2>
      <p class="blockintro"><?=$cwlang["isdngrp"][""]?>set the parameters that you need</p>
      <div class="block">
        <table>
          <tr>
            <td class="required" width="160"><label for="timesegment"><?=$cwlang["isdngrp"][""]?>Time Segment:</label></td>
            <td ><select id="timesegment" name="timesegment" ><?=$tsHtml?></select></td>
          </tr>
          <tr>
            <td class="required" width="160"><label for="callerid"><?=$cwlang["isdngrp"][""]?>Caller Number:</label></td>
            <td valign="top" width="400" >
              <input type="text" name="callerid" value="<?=$callerid?>" />
            </td>
          </tr>
        </table>
      </div>

      <?echo listTrunk($js, $trunk, $number);?>      

      <?echo listMacro($js, $macro, $parameter);?>      

    	<h2><?=$cwlang["isdngrp"][""]?>4) Stop</h2>
      <p class="blockintro"><?=$cwlang["isdngrp"][""]?>Stop executing the next rule after this.</p>
      <div class="block">
        <table>
          <tr>
            <td class="required" width="160"><label for="duration"><?=$cwlang["isdngrp"][""]?>Action to do:</label></td>
            <td >
              <input type="radio" id="duration" name="duration" value="0" <?if ($duration=="0" or $duration=="") echo "checked=\"checked\"";?>/>
              Stop or 
              <input type="radio" id="duration" name="duration" value="1" <?if ($duration=="1") echo "checked=\"checked\"";?>/>
              Continue
            </td>
          </tr>
        </table>
      </div>
      
      <?=$numHidden?>      
      <div class="action">
        <input type="hidden" name="id" value="<?=$id?>" />
        <input type="hidden" name="rule" value="<?=$rule?>" />
        <input type="hidden" name="priority" value="<?=$priority?>" />
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Cancel" onclick="location.href='../'; return false;"/>
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" />
      </div>
    </form>
   </div>  
  
  <?
  if (isset($_GET["id"])) echo "<script>show($show);</script>";

  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <h1><?=$cwlang["isdngrp"]["sidebarTitle"]?></h1>
          <p><?=$cwlang["isdngrp"]["sidebarDesc"]?>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================
 	sajax_handle_client_request();

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

