<?
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
  
  //================ PAGE LOGIC ================

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

  //install a new card or update the port parameters
  if (count($_POST)>0) {

    if ($_POST["act"]=="install") {
      $card=$_POST["card"];
      $ret=$asterisk->putZap($card);
      if ($ret>0) $alert="<div class=\"AlertGood\">".$cwlang["zapata"]["alertAddGood"]."</div>";
      else $alert="<div class=\"AlertBad\">".$cwlang["zapata"]["alertAddBad"]."</div>";
    } else {
      $entry=Array();
      $card=$_POST["card"];

      foreach($_POST["frame_protocol"] as $id=>$l) {
        $entry[frame_protocol][]=Array("id"=>$id,"frame_protocol"=>$l);   }
      foreach($_POST["timing"] as $id=>$l) {
        $entry[timing][]=Array("id"=>$id,"timing"=>$l);   }
      foreach($_POST["lbo"] as $id=>$l) {
        $entry[lbo][]=Array("id"=>$id,"lbo"=>$l);   }
      foreach($_POST["framing"] as $id=>$l) {
        $entry[framing][]=Array("id"=>$id,"framing"=>$l);   }
      foreach($_POST["coding"] as $id=>$l) {
        $entry[coding][]=Array("id"=>$id,"coding"=>$l);   }
      foreach($_POST["yellow"] as $id=>$l) {
        $entry[yellow][]=Array("id"=>$id,"yellow"=>$l);   }
      foreach($_POST["protocol"] as $id=>$l) {
        $entry[protocol][]=Array("id"=>$id,"protocol"=>$l);   }
      foreach($_POST["loadzone"] as $id=>$l) {
        $entry[loadzone][]=Array("id"=>$id,"loadzone"=>$l);}
      foreach($_POST["groupName"] as $id=>$l) {
        $entry[groupname][]=Array("id"=>$id,"groupname"=>$l);   }
      foreach($_POST["channel1"] as $id=>$l) {
        $entry[channel1][]=Array("id"=>$id,"channel1"=>$l);   }
      foreach($_POST["channel2"] as $id=>$l) {
        $entry[channel2][]=Array("id"=>$id,"channel2"=>$l);   }
      foreach($_POST["type"] as $id=>$l) {
        $entry[type][]=Array("id"=>$id,"type"=>$l);   }
      foreach($_POST["signalling"] as $id=>$l) {
        $entry[signalling][]=Array("id"=>$id,"signalling"=>$l);   }
      foreach($_POST["portxlaw"] as $id=>$l) {
        $entry[portxlaw][]=Array("id"=>$id,"portxlaw"=>$l);   }
      foreach($_POST["xlaw"] as $id=>$l) {
        $entry[xlaw][]=Array("id"=>$id,"xlaw"=>$l);   }
      foreach($_POST["default"] as $id=>$l) {
        $entry[default1][]=Array("id"=>$id,"default1"=>$l);   }

      $entry[defzone]=$_POST["defzone"];

      $ret=$asterisk->updateZapPort($entry);

      if ($ret>0) $alert="<div class=\"AlertGood\">".$cwlang["zapata"]["alertSaveGood"]."</div>";
      else $alert="<div class=\"AlertBad\">".$cwlang["zapata"]["alertSaveBad"]."</div>";
    }
  }

  //count the total of span number for E1/T1

  $ret=$asterisk->getZapPort(0);
  if ($err=$asterisk->getError()) die("Error: $err");

  if (is_array($ret)) {
    $spantotal=0;
    $spanarr[]=0;
    foreach($ret as $v) {
      $frame_protocol=$v["frame_protocol"];
      if ($frame_protocol=="T1" or $frame_protocol=="E1") {
         $spantotal++;
         $spanarr[]=$spantotal;
      }
    }
  }

  //create the web interface

  $ret=$asterisk->getZapPort(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  $old_card_id=0;
  $count=1;
  $channelold=0;
  $channelnew;
  if (is_array($ret)) {
    $spannum=1;
    foreach($ret as $v) {
      $id=$v["id"];
      $port=$v["port"];
      $frame_protocol=$v["frame_protocol"];
      $groupname=$v["group"];
      $card=$v["card"];
      $protocol=$v["protocol"];
      $card_id=$v["card_id"];
      $timing=$v["timing"];
      $lbo=$v["lbo"];
      $framing=$v["framing"];
      $coding=$v["coding"];  
      $yellow=$v["yellow"];
      $portxlaw=$v["xlaw"];
      $default=$v['default'];
      if ($old_card_id!=$card_id) {
        if ($count>1) $html.="<tr><td>&nbsp;</td></tr>\n";
        $c=$asterisk->getZap($card);
        $name=$c[0]["name"];
        $line_type=$c[0]["line_type"]."/";
        $card_protocol=split(',',$c[0][line_type]);
        $find_type=strpos($line_type,'E1');
        $find_type2=strpos($line_type,'T1');
        $find_type3=strpos($line_type,'FXS');
        $find_type4=strpos($line_type,'FXO');
        $html.="<tr><td colspan=6><span style='color: green; font-weight: bold;'>".$cwlang["zapata"]["card"]." No. $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>
<td colspan=3></tr>\n";
        if($find_type !==false or $find_type2 !==false){ $html.="<tr><td><td>Type<td>Signalling<td>Timing<td>LBO<td>Framing<td>Coding<td>crc4 yellow<td>";}
        else if($find_type3 !==false or $find_type4 !==false){ $html.="<tr><td><td>Type<td colspan=7>Signalling";}
        else {$html.="<tr><td><td colspan8>Type";}
      }

      $option_protocol="";
      for($x=0;$x<count($card_protocol);$x++){
        $t=$card_protocol[$x];
        if ($t==$frame_protocol){ $chk_opt2=" selected ";}else{$chk_opt2="";}
        $option_protocol.="<option $chk_opt2>".$t;
      }
      $border="valign='top' style='border-bottom: 1px dotted gray;'";
      $html.="<tr><td $border>";
      $html.="<strong>$id</strong> -&gt; ".$cwlang["zapata"]["port"]."$port";
      //$html.=" <span style='margin-left: 10px;'> \n";

      if ($groupname!=""){$disabled="disabled ";
      $input="<input type=hidden name=frame_protocol[$id] value =$frame_protocol";}else {$disabled="";$input="";}
      $html.="<td $border><select $disabled name=frame_protocol[$id] onChange='return form.submit();'>
      <option>$option_protocol</select>".$input;     
      // $html.=" <span style='margin-left: 30px;'> \n";
      // $html.=" </span> \n";
      $option=$asterisk->getZapOption($frame_protocol);
      //$timing_opt=split(',',$option[0][timing_opt]);
      $lbo_opt=split(',',$option[0][lbo_opt]);
      $framing_opt=split(',',$option[0][framing_opt]);
      $crc_opt=split(';',$option[0][crc_opt]);
      $coding_opt=split(',',$option[0][coding_opt]);
      $protocol_opt=split(',',$option[0][protocol]);
      $option_lbo="";$option_framing="";$option_crc="";$option_coding="";$option_timing="";$option_protocol="";
      foreach ($lbo_opt as $c) {
        if ($c==$lbo) $chk_opt=" selected "; else $chk_opt="";
          $option_lbo.="<option $chk_opt>".$c;
        }
        foreach ($framing_opt as $c) {
          if ($c==$framing) $chk_opt=" selected "; else $chk_opt="";
          $option_framing.="<option $chk_opt>".$c;
        }
        foreach ($crc_opt as $c) {
          if ($c==$yellow) $chk_opt=" selected "; else $chk_opt="";
          $option_crc.="<option $chk_opt>".$c;
        }
        foreach ($coding_opt as $c) {
          if ($c==$coding) $chk_opt=" selected "; else $chk_opt="";
          $option_coding.="<option $chk_opt>".$c;
        }
        foreach ($spanarr as $c) {
          if ($c==$timing) $chk_opt=" selected "; else $chk_opt="";
          $option_timing.="<option $chk_opt>".$c;
        }
        foreach ($protocol_opt as $c) {
          if ($c==$protocol) $chk_opt=" selected "; else $chk_opt="";
          $option_protocol.="<option $chk_opt>".$c;
        }

        if(substr($protocol,0,3)=='pri' or $protocol=='fx'){$chk_opt="selected";$style='block';}else {$chk_opt='';$style='none';}
        if(substr($protocol,0,3)=='pri' or $frame_protocol=='FXS'or $frame_protocol=='FXO'){$chk_opt2="selected";$style2='block';}else{ $chk_opt2='';$style2='none';}
        $sign="<td $border><select name=protocol[$id] onchange=form.submit();><option><option value=fx $chk_opt>FX?<option value=pri $chk_opt2>PRI</select>";

        if($portxlaw=="alaw")$chk_opt="selected"; else $chk_opt="";
        if($portxlaw=="mulaw")$chk_opt2="selected"; else $chk_opt2="";
      
        $config1="<div id=xlaw[$id] style=display:$style2><select name=portxlaw[$id]><option><option $chk_opt>alaw<option $chk_opt2>mulaw</select></div>\n";
        $config2="
        <div id=chan[$id] style=display:$style><a href=# onclick=\"return showHide('channel$id')\">Advance</a></div>\n";
        if ($frame_protocol=="E1") { 

          $html.="
          $sign
          <td $border><select name=timing[$id] ><option>$option_timing</select>
          <td $border><select name=lbo[$id]><option>$option_lbo</select>
          <td $border><select name=framing[$id]><option>$option_framing</select>
          <td $border><select name=coding[$id]><option>$option_coding</select>
          <td $border><select name=yellow[$id]><option>$option_crc</select>
          <td $border>$config2
          <input type='hidden' name=groupName[$id] value=$groupname>
          ";
          $channelnew+=31;
          $spannum++;
        }
        if ($frame_protocol=="T1")
        { $html.="
          $sign
          <td $border><select name=timing[$id] ><option>$option_timing</select>
          <td $border><select name=lbo[$id]><option>$option_lbo</select>
          <td $border><select name=framing[$id]><option>$option_framing</select>
          <td $border><select name=coding[$id]><option>$option_coding</select>
          <td $border><input type='hidden' name=yellow[$id]>
          <td $border>$config2
          <input type='hidden' name=groupName[$id] value=$groupname>
          ";
          $spannum++;
          $channelnew+=24;
        }
     if($frame_protocol=='FXS' or $frame_protocol=='FXO') 
{ $html.="  
<td colspan=8 $border><table><tr><td><select name=protocol[$id]><option>$option_protocol</select>
<td>$config1$config2</table>
<input type='hidden' name=timing[$id]>
<input type='hidden' name=lbo[$id]>
<input type='hidden' name=framing[$id]>
<input type='hidden' name=coding[$id]>
<input type='hidden' name=yellow[$id]>
<input type='hidden' name=groupName[$id] value=$groupname>
";
 $channelnew+=1;
}
     if($frame_protocol=='' ) 
{ $html.="
<td colspan=8 $border><input type='hidden' name=protocol[$id]>
$config1$config2
<input type='hidden' name=timing[$id]>
<input type='hidden' name=lbo[$id]>
<input type='hidden' name=framing[$id]>
<input type='hidden' name=coding[$id]>
<input type='hidden' name=yellow[$id]>
<input type='hidden' name=groupName[$id] value=$groupname>
";
$channelnew+=0;
}

     //if($frame_protocol!=""){ $html.="<a href=config.php?do=config&id=$id>Config</a>  \n";}

if($portxlaw=="alaw")$chk_opt="selected"; else $chk_opt="";
if($portxlaw=="mulaw")$chk_opt2="selected"; else $chk_opt2="";

////////create channel menu//////

$channelframe="";$channelbox1="";$channelbox2="";$channelbox3="";$channelbox4="";$channelbox5="";$channelbox6="";
$html.="<tr><td colspan=9><div id=channel$id style=display:none><table><tr><td>";
if ($default=='1'){$chk='checked';$chk2='';}else {$chk='';$chk2='checked';}
if (substr($protocol,0,3)=='pri'){
$html.="<input type=radio name=default[$id] value=1 $chk onclick=\"return hide('contab[$id]')\";>Default<input $chk2 type=radio name=default[$id] value=0 onclick=\"return show('contab[$id]');\" >Config ";$displayed='none';
   }else {$html.="<input type=hidden name=default[$id] value=0>"; $displayed='block';}
if ($frame_protocol!="T1" and $frame_protocol!="E1")$config1="";
$html.="<td>$config1</table>";
if( $default=='0')$displayed='block';
$html.="<table bgcolor=#f6f6f6 id=contab[$id] border=0 cellpadding=0 cellspacing=0 style=display:$displayed>";
$ree=$asterisk->getZapChannel($id);  
//echo "<PRE>";print_r($ret); 
$countr=1;
$ret=array();
if ($frame_protocol=="T1"){
$ret[]=array(channelfrom => $channelold+1, channelto=>$channelnew-1, dchan=>$channelnew);
                         }
if ($frame_protocol=="E1"){
$ret[]=array(channelfrom => $channelold+1, channelto=>$channelold+15,dchan=>$channelold+16);
$ret[]=array(channelfrom => $channelold+17, channelto=>$channelnew);
                       }
if ($frame_protocol=='T1'){ $html.="<td>channel:".$ret[0][channelfrom]." - ".$ret[0][channelto]; $html.=" ; dchan=".$channelnew;}
if ($frame_protocol=='E1'){ $html.="<td>channel:".$ret[0][channelfrom]." - ". $ret[0][channelto].';'.$ret[1][channelfrom] . "-". $ret[1][channelto]; $html.=" ; dchan=".$channelnew;}
//$ree[]++;$ree[]++;
//error_log($ree[$protocol]);

 foreach($ree as $v) {
error_log("inside foreach");
$type=$v[type];
$channelfrom=$v[channelfrom];
$channelto=$v[channelto];
$signalling=$v[signalling];
$xlaw=$v[xlaw];
if ($protocol=='pri'){$disabled="disabled";
$hidden="<input type=hidden name=signalling[$id][$countr] value=''>";
$hidden2="<input type=hidden name=xlaw[$id][$countr] value=''>";
$hidden2.="<input type=hidden name=type[$id][$countr] value=''>";
} else {$disabled="";$hidden="";$hidden2=""; }
$html.="<tr><td># $hidden$hidden2<input type=text name=channel1[$id][$countr] size=5 value=$channelfrom > - <input type=text name=channel2[$id][$countr] size=5 value=$channelto > 
<select name='type[$id][$countr]' $disabled onchange=\"Buildkey(this.options[this.options.selectedIndex].value,'$id','$countr');\"><option value=$type>$type<option><option value=FXO>FXO<option value=FXS>FXS</select> 
<select id='signalling[$id][$countr]' name='signalling[$id][$countr]' $disabled><option>$signalling<option></select> 
<select name='xlaw[$id][$countr]' $disabled><option>$xlaw<option><option>alaw<option>mulaw</select>";
$countr++;
}
$channelold=$channelnew;
$html.="</table>";
$html.="<div id=more$id><a href='javascript:' onclick=\"return zapata('$id','$countr','$id')\">more</a></div>";
$html.="</div>";
      $old_card_id=$card_id;
      $count++;
    }
  }
if ($ret!=""){
$ret=$asterisk->getZone(0);  
//print_r($ret); 
 if (is_array($ret)) {
$count="0";
 foreach($ret as $v) {
      $id=$v["id"];
      $name=$v["name"];
      $fullname=$v["fullname"];
      $loadzone=$v["loadzone"];
      $defaultzone=$v["defaultzone"];
      if($loadzone!="0"){ $select="checked"; $loadzoneall.=$name.",";} else{ $select="";}
      if($defaultzone!="0"){ $select2="selected";} else{ $select2="";}
      if($count%4==0)$zone.="<tr>";
      $zone.="<td><input name=loadzone[$count] type=checkbox value='$name' $select>$name : $fullname";
      $defzone.="<option value='$name' $select2>$name : $fullname</option>\n";
    $count++; }
  }
    
$style="style=color:green;font-weight:bold";
$html.="<tr><td colspan=9><br><a href=# onclick=\" return showHide('table_zone')\">LoadZone</a>:".substr($loadzoneall,0,-1)."
<div id=table_zone style=display:none; >
 <table bgcolor=#f6f6f6 border=0 cellpadding=0 cellspacing=0 style=display:block><tr>";
$html.="<br>$zone";  
$html.="</table></div>";
$html.="<br><br><br><span $style>default Zone:</span> ";
$html.="<select name=defzone>$defzone</select>";  
} 
 $ret=$asterisk->getZap(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["zapata"]["title"]?></h1>
  </div>
  <form name="theform" action="<?=$PHP_SELF?>" method="post">
    <table class="todolist" cellpadding="0" cellspacing="0" border=0>
      <tr>
        <?=$html?>
      </tr>
    </table>
    <?if ($html!="") {?><input type="submit" value="<?=$cwlang["zapata"]["save"]?>" style="font-size: 11px; float: right; margin-right: 20px;"/><?}?>
    <input type="hidden" name="act" value="update" />
<br><br><hr>
  </form>
  
  <br /><br />
  <div id="basic_form">
    <form action="<?=$PHP_SELF?>" method="post">
      <h2><?=$cwlang["zapata"]["newCard"]?></h2>
      <p class="blockintro"><?=$cwlang["zapata"]["newCardDesc"]?></p>
      <div class="block">
        <table>
          <tr>
            <td class="required" style="text-align: right" ><?=$cwlang["zapata"]["card"]?>:</td>
            <td><select name="card"><?=$cards?></select></td>
          </tr>
          <tr><td></td><td><input type="submit" alt="Salva" value="<?=$cwlang["zapata"]["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="zapata";include("../sidebar.php");?>
          <h1><?=$cwlang["zapata"]["sidebarTitle"]?></h1>
            <?=$cwlang["zapata"]["sidebarDesc"]?>
          <p> 
            <img src="../../../admin/public/img/quad1.jpg" width="200" alt="zapata card" />
          </p>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

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

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

