<?
/**
 * VoiceOne Settings/Tech/misdn
 *
 * Frontend to provide access to the tech system in voiceone.
 * @package voiceone
 * @subpackage  settings_tech
 */
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
  
  //================ PAGE LOGIC ================

  if ($_GET["cmd"]=="del") {
    $ret=$asterisk->delCard($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdn"]["alertDelGood"]."</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdn"]["alertDelBad"]."</div>";
  }

  //install a new card or update the port parameters
  if (count($_POST)>0) {
    if ($_POST["act"]=="install") {
      $card=$_POST["card"];
      $ret=$asterisk->putCard($card);
      if ($ret>0) $alert="<div class=\"AlertGood\">".$cwlang["isdn"]["alertAddGood"]."</div>";
      else $alert="<div class=\"AlertBad\">".$cwlang["isdn"]["alertAddBad"]."</div>";
    } else {
      $entry=Array();
      $card=$_POST["card"];
      foreach($_POST["layermask"] as $id=>$l) {
        if ($_POST["layermask"][$id]=="te") {
          $layermask="0xf"; 
          if ($_POST["protocol"][$id]=="pmp") $protocol="0x2"; else $protocol="0x22";
        } else {
          $layermask="0x3";
          if ($_POST["protocol"][$id]=="pmp") $protocol="0x12"; else $protocol="0x32";
        }
        $entry[]=Array("id"=>$id, "layermask"=>$layermask, "protocol"=>$protocol);
      }
      
      $ret=$asterisk->updateIsdnPort($entry);
      if ($ret>0) $alert="<div class=\"AlertGood\">".$cwlang["isdn"]["alertSaveGood"]."</div>";
      else $alert="<div class=\"AlertBad\">".$cwlang["isdn"]["alertSaveBad"]."</div>";
    }
  }

  
  //create the web interface
  $ret=$asterisk->getIsdnPort(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  $old_card_id=0;
  $count=1;
  if (is_array($ret)) {
    foreach($ret as $v) {
      $id=$v["id"];
      $port=$v["port"];
      $name=$v["name"];
      $layermask=$v["layermask"];
      $protocol=$v["protocol"];
      $card=$v["card"];
      $card_id=$v["card_id"];

      if ($old_card_id!=$card_id) {
        if ($count>1) $html.="<tr><td>&nbsp;</td></tr>\n";
        $c=$asterisk->getCard($card);
        $name=$c[0]["name"];
        $html.="<tr><td><span style='color: green; font-weight: bold;'>".$cwlang["isdn"]["card"]." n° $card_id:</span> $name  <a href=\"$PHP_SELF?cmd=del&id=$card_id\" onclick=\"if (confirm('Are you sure to remove the card?')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a></td></tr>\n";
      }
      $html.="<tr><td style='border-bottom: 1px dotted gray;'>";
      $html.="<strong>$id</strong> -&gt; ".$cwlang["isdn"]["port"]." n° $port";
      $html.=" <span style='margin-left: 20px;'> \n";
      if ($layermask=='0x3') $chk="checked='checked'"; else $chk=""; 
      $html.=" <input type=\"radio\" name=\"layermask[$id]\" value=\"nt\" $chk/> NT ";
      if ($layermask=='0xf') $chk="checked='checked'"; else $chk=""; 
      $html.=" <input type=\"radio\" name=\"layermask[$id]\" value=\"te\" $chk/> TE ";
      $html.=" </span> \n";
      $html.=" <span style='margin-left: 30px;'> \n";
      if ($protocol=='0x2' or $protocol=='0x12') $chk="checked='checked'"; else $chk=""; 
      $html.=" <input type=\"radio\" name=\"protocol[$id]\" value=\"pmp\" $chk/> Pmp ";
      if ($protocol=='0x22' or $protocol=='0x32') $chk="checked='checked'"; else $chk=""; 
      $html.=" <input type=\"radio\" name=\"protocol[$id]\" value=\"p2p\" $chk/> P2p ";
      $html.=" </span> \n";
      $html.="</td></tr>\n";

      $old_card_id=$card_id;
      $count++;
    }
  }  
  
  $ret=$asterisk->getCard(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($ret)) {
    foreach($ret as $v) {
      $id=$v["id"];
      $name=$v["name"];
      $cards.="<option value=\"$id\">$name</option>\n";
    }
  }    
  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <? $sel="cards"; include ("navigation.php");?>
  
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=$cwlang["isdn"]["title"]?></h1>
  </div>
  <form action="<?=$PHP_SELF?>" method="post">
    <table class="todolist">
      <tr>
        <?=$html?>
      </tr>
    </table>
    <?if ($html!="") {?><input type="submit" value="<?=$cwlang["isdn"]["save"]?>" style="font-size: 11px; float: right; margin-right: 20px;"/><?}?>
    <input type="hidden" name="act" value="update" />
  </form>
  
  <br /><br />
  <div id="basic_form">
    <form action="<?=$PHP_SELF?>" method="post">
      <h2><?=$cwlang["isdn"]["newCard"]?></h2>
      <p class="blockintro"><?=$cwlang["isdn"]["newCardDesc"]?></p>
      <div class="block">
        <table>
          <tr>
            <td class="required" style="text-align: right" ><?=$cwlang["isdn"]["card"]?>:</td>
            <td><select name="card"><?=$cards?></select></td>
          </tr>
          <tr><td></td><td><input type="submit" alt="Salva" value="<?=$cwlang["isdn"]["install"]?>"/></td></tr> 
        </table>
      </div>
      <input type="hidden" name="act" value="install" />
    </form>
  </div>  
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <?$sel="isdn";include("../sidebar.php");?>
          <h1><?=$cwlang["isdn"]["sidebarTitle"]?></h1>
            <?=$cwlang["isdn"]["sidebarDesc"]?>
          <p> 
            <img src="../../../admin/public/img/quad1.jpg" width="200" alt="isdn card" />
          </p>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

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

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

