<?
/**
 * VoiceOne Settings/Rules/outgoing/edit
 *
 * 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) {
    if (is_array($_POST["listRight"])) $resource=implode("|",$_POST["listRight"]);
    $entry=Array(
      "id"  => $_POST["id"],
      "rule" => $_POST["rule"],
      "priority" => $_POST["priority"],
      "timesegment" => $_POST["timesegment"], 
      "action" => $_POST["action"], 
      "resource" => $resource 
    );

    if (substr($_POST["group"],0,2)=="g_")  $entry["group"]=substr($_POST["group"],2); 
    else $entry["entity"]=$_POST["group"]; 
    if ($_POST["kind"]==GROUP_OF_ALL) $entry["group"]=GROUP_OF_ALL;

    //save the group informations
    $ret=$asterisk->putOutgoingRule($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->getOutgoingRule($_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"];  
    $group=$ret[0]["group"]; 
    $entity=$ret[0]["entity"];  
    $resource=split("\|",$ret[0]["resource"]);  
    $provider=$ret[0]["provider"];  
    $action=$ret[0]["action"]; 
  }  

  //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";
  }
    
  //get the trunk available
  $ret=$asterisk->getTrunk(0,0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($ret)) 
  foreach ($ret as $tr) {
    $id=$tr["id"];
    $name=$tr["name"];
    if (!@in_array($id,$resource)) $listLeft.="<option value=\"$id\" >$name</option>";    
  }

  //get the trunk already selected
  if (is_array($resource)) 
  foreach ($resource as $r) {
    $id=$r;
    $tr=$asterisk->getTrunk($id,0);
    $name=$tr[0]["name"];
    if ($id=="0") $name="[any trunk]";
    $listRight.="<option value=\"$id\" >$name</option>"; 
  }
  
  //adjust the trunk list
  if (isset($_GET["id"])) {
    if (!in_array("0",$resource)) $listLeft.="<option value=\"0\">[any trunk]</option>\n";
  } else $listRight.="<option value=\"0\">[any trunk]</option>\n";


  
  $id=$_REQUEST["id"];
  $rule=$_REQUEST["rule"];  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <?$sel="outgoing";include("../../navigation.php");?> 
  
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=$cwlang["isdngrp"][""]?>Outgoing 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" onSubmit=" return selectAll(getElementById('listRight'));">
      
    	<h2><?=$cwlang["isdngrp"][""]?>1) When...</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 colspan="2"><select id="timesegment" name="timesegment" ><?=$tsHtml?></select></td>
          </tr>
        </table>
      </div>

<?    echo listGroupEntity($js, $group, $entity);?>      
      
    	<h2><?=$cwlang["isdngrp"][""]?>3) Action...</h2>
      <p class="blockintro"><?=$cwlang["isdngrp"][""]?>What do you want to do?</p>
      <div class="block">
        <table>
          <tr>
            <td class="required" width="160"><label for="action"><?=$cwlang["isdngrp"][""]?>Action:</label></td>
            <td >
              <select id="action" name="action">
                <option value="<?=RULE_ALLOW?>" <?if ($action==RULE_ALLOW) echo "selected='selected'";?>>ALLOW</option>
                <option value="<?=RULE_DENY?>" <?if ($action==RULE_DENY) echo "selected='selected'";?>>DENY</option>
              </select>
            </td>
          </tr>
        </table>
      </div>

      <div id="trunkDiv">
      	<h2><?=$cwlang["isdngrp"][""]?>4) Which trunk to be used...</h2>
        <p class="blockintro"><?=$cwlang["isdngrp"][""]?>Choose which of the available trunk to be used in this rule</p>
        <div class="block">
          <table>
            <tr>
              <td class="required">
                <div ><?=$cwlang["isdngrp"][""]?>Available Resources:</div>
                <select multiple size="5"  name="listLeft[]" id="listLeft"><?=$listLeft?></select>
              </td>
              <td>   
                <div><input type="button" onclick="moveDualList( document.getElementById('listLeft'),  document.getElementById('listRight'), false )" name="Add     >>"  value="Aggiungi       >>" style="width:100px"></div>
                <div><input type="button" onclick="moveDualList( document.getElementById('listRight'), document.getElementById('listLeft'),  false )" name="Add     <<"  value="<<     Rimuovi" style="width:100px"></div>
                <div><input type="button" onclick="moveDualList( document.getElementById('listLeft'),  document.getElementById('listRight'), true  )" name="Add All >>"  value="Aggiungi tutte >>" style="width:100px"></div>
                <div><input type="button" onclick="moveDualList( document.getElementById('listRight'), document.getElementById('listLeft'),  true  )" name="Add All <<"  value="<< Rimuovi tutto " style="width:100px"></div>
              </td>
              <td class="required"><div ><?=$cwlang["isdngrp"]["groupPorts"]?>:</div>
                <div style="float: left">
                  <input type="image"  src="<?=$relBasePath?>public/img/icon_up.gif"  onclick="moveOptions ( document.getElementById('listRight'), -1); return false" name="Up"  value="Down" style="display: block">
                  <input type="image"  src="<?=$relBasePath?>public/img/icon_down.gif"  onclick="moveOptions ( document.getElementById('listRight'), 1); return false;" name="Down"  value="Down" style="display: block">
                </div>
                  <select multiple size="5" name="listRight[]" id="listRight"><?=$listRight?></select>
              </td>
            </tr>
          </table>
        </div>
      </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>  
  
  <?

  $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');
?>

