<?
/**
 * VoiceOne Settings/Tech/edit
 *
 * Frontend to provide access to the tech system in voiceone.
 * @package voiceone
 * @subpackage  settings_tech
 */
  include ("../../../admin/config/cfg.php");
  global $cwlang,$asterisk; 
 	sajax_init();
  //$sajax_debug_mode=true;
  
  //================ PAGE LOGIC ================
  if (count($_POST)>0) {
    $entry["id"]=$_POST["id"];
    $entry["technology"]=$_POST["technology"];
    $entry["resource"]=$_POST["resource"];
    $entry["key"]=$_POST["key"];
    $entry["msns"]=$_POST["phonenumber"]; 
    $entry["rule"]=$_POST["rule"]; 
    $entry["trunk"]=1; 
    $entry["order"]=$_POST["order"]; 
    $entry["speeddial"]=$_POST["speeddial"]; 
    $ret=$asterisk->putChannel($entry); 
    if ($ret>0) $alert="<div class=\"AlertGood\">".$cwlang["isdn"]["alertAddGood"]."</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdn"]["alertAddBad"]."</div>";
    goToMainPage($alert);
  } 

  if (isset($_GET["id"])) {
    $ret=$asterisk->getTrunk($_GET["id"],0);
    if ($err=$asterisk->getError()) die("Error: $err");
    $v=$ret[0];
    $id=$v["id"];
    $name=$v["name"];
    $key=$v["key"];
    $speeddial=$v["speeddial"];
    $order=$v["order"];
    $resourceArr=split("/",$name);
    $resource=$resourceArr[1];
    $technology=$v["technology"];
    foreach ($v["msns"] as $n) {
      $numbers.="<option value=\"$n\">$n</option>";
    }
    $rule=$v["rule"];
    $title="Edit Trunk";
  } else {
    $title="New Custom Trunk";
  }
          
  //get the technologies list
  $tech=$asterisk->getTechnology(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($tech)) {
    foreach($tech as $t) {
      $id=$t["id"];
      $name=$t["name"];
      $voip=$t["voip"];
      if ($id==$technology) $sel=" selected=\"selected\" "; else $sel="";
      $optionTech.="<option value=\"$id\" $sel>$name</option>\n";
    }
  }  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
  <?=$alert?>
    <h1><?=$title?></h1>
    <h3><a href="../" class="admin" ><?=$cwlang["exten"]["return"]?></a></h3>
  </div>

  <div id="basic_form">
    <form action="<?=$PHP_SELF?>" method="post" onSubmit="return selectAll(getElementById('phonenumber')); ">
      <h2><?=$cwlang["isdn"][""]?>Add new custom trunk</h2>
      <p class="blockintro"><?=$cwlang["isdn"][""]?>Make sure you have the trunk ready and available.</p>
      <div class="block">
        <table>
          <tr>
            <td class="required" style="text-align: right" ><?=$cwlang["isdn"][""]?>Technology: </td>
            <td><select name="technology"><?=$optionTech?></select></td>
          </tr>
          <tr>
            <td class="required" style="text-align: right" ><?=$cwlang["isdn"][""]?>Resource: </td>
            <td><input type="text" name="resource" value="<?=$resource?>"></td>
          </tr>
          <tr>
            <td class="required" style="text-align: right" ><?=$cwlang["isdn"][""]?>Speed dial: </td>
            <td>
              <input type="text" name="speeddial" value="<?=$speeddial?>" size="3" maxlength="3"><big><strong>*</strong></big>
              <span class="help">Write a number to speed dial with this trunk. I.e. <strong>1</strong> or <strong>76</strong></span>
            </td>
          </tr>
        </table>
      </div>
        
    	<h2><?=$cwlang["isdngrp"]["listeningNumber"]?></h2>
      <p class="blockintro"><?=$cwlang["isdngrp"]["listeningNumberHelp"]?></p>
      <div class="block">
        <table>
          <tr>
            <td class="required"><label for="number"><?=$cwlang["isdngrp"]["phoneNumber"]?>:</label></td>
            <td class="required"><?=$cwlang["isdngrp"]["phoneNumberAdded"]?></td>
          </tr>
          <tr>
            <td valign="top">
              <input type="text" size="25" maxlength="100" id="number" name="number" value="" />
              <input type="button" value="+" onclick="addOption(getElementById('phonenumber'),document.getElementById('number').value); document.getElementById('number').value='' " title="Add Length">
              <div class="help"><br/>You can write a flat number (i.e. 0123456)<br/>or a range of numbers (i.e. 01234560-01234570)</div>
            </td>
            <td>
              <select name="phonenumber[]" size="4" onchange="saved = false" ondblclick="editOptions(this)" multiple style="float: left" id="phonenumber">
                <?=$numbers?>
              </select>
              <input type="button" value="edit" onclick="editOptions(getElementById('phonenumber'))" title="Edit Length(s)">
              <input type="button" value="X" onclick="deleteOptions(getElementById('phonenumber'))" title="Delete Length(s)">
            </td>
          </tr>
        </table>
      </div>

<?    echo listRule($js, $rule, RULE_INCOMING);?>      

      <div class="action">
        <?if ($_GET["id"]!="") {?> 
          <input type="hidden" name="id" value="<?=$_GET["id"]?>" />
          <input type="hidden" name="key" value="<?=$key?>" />
          <input type="hidden" name="order" value="<?=$order?>" />
        <?}?>
        <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["exten"]["otherExten"]?></h1>
          <p><?=$cwlang["exten"]["otherExtenDesc"]?></p>
          <ul>
            <?=$buddiesList?>
          </ul>
        </div>
        <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
        <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

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

  $smarty->assign("section", "tech");
  $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');
?>