<?
/**
 * VoiceOne Settings/Tech/zapata/groups
 *
 * Frontend to provide access to the tech system in voiceone.
 * @package voiceone
 * @subpackage  settings_tech
 */
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
  $alert=getAlert();
  if(!_isModuleLoaded('zap')) goToMainPage('',"../");
  //================ POST & GET ================
  if ($_GET["cmd"]=="del") {
    $ret=$asterisk->delZapataGroup($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("The group has been deleted")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("The group hasn't been deleted")."</div>";
    goToMainPage($alert,"./");
  }
  //================ PAGE LOGIC ================
  //get channels
  $channelArr=array();
  $ret=$asterisk->getZapataChannel(0);
  if(is_array($ret)){
    foreach($ret as $channel){
      $channelArr[$channel["id"]]=$channel["zchannel"];
    }
  }
  //get ports
  $ret=$asterisk->getZapataGroup(0);
  if($err=$asterisk->getError()) die("Error: $err");
  if(is_array($ret)){
    $count=0;
    foreach($ret as $v){
      $id       = $v["id"];
      $name     = $v["name"];
      $type     = $v["type"];
      $group    = $v["group"];
      $fromItem = $portList;
      $lastItem = $portList;
      //manage ports list for FXS/FXO groups
      if(($type=="FXS")||($type=="FXO")){
        if(is_array($v["port"])){
          $portList="";
          foreach($v["port"] as $c_id){
            if((is_array($channelArr))&&(array_key_exists($c_id,$channelArr))){
              $portList.=$channelArr[$c_id].", ";
            }
          }
          if(trim($portList)!="") $portList=substr($portList,0,-2);
        }
      }
      //manage ports list for PRI/E&M groups
      else{
        $portList = $v["port"][0];
        if(is_array($v["port"])){
          foreach($v["port"] as $p){
            if($fromItem == $p) continue;
            if(($p-1)==$lastItem){
              $lastItem=$p;
              continue;
            } 
            if ($fromItem != $lastItem) {
              $portList.= "-";
              $portList.= $lastItem;
            }
            $portList.= ",";
            $portList.= "$p";
            $fromItem = $p;
            $lastItem = $p;
          }
          if($fromItem!=$lastItem){
            $portList.= "-";
            $portList.= $p;
          }
        }
      }
      ob_start();
      ?>
        <td>
          <a href="edit/?id=<?=$id?>"><?=$name?></a>&nbsp;&nbsp;(<?=$type?> <?=T_("ports")?>: <strong><?=$portList?></strong>)&nbsp;
          <a href="<?=$PHP_SELF?>?cmd=del&id=<?=$id?>" onclick="if (confirm('Are you sure to delete the <?=$name?> group?')) return true; else return false;"><img src="<?=$relBasePath?>/public/img/trash.gif" /></a>
        </td>
      <?
      $html.=ob_get_contents();
			ob_end_clean();
      if((++$count)%2==0) $html.="</tr><tr>\n";
    }
  }  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>

  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=T_("Zapata Port Groups")?></h1>
    <h3>
      <a href="edit/?portType=PRI"   class="admin" ><?=sprintf(T_("New %s port Group"),"PRI")?></a> &nbsp;&nbsp;
      <a href="edit/?portType=E%26M" class="admin" ><?=sprintf(T_("New %s port Group"),"E&M")?></a> &nbsp;&nbsp;
      <a href="edit/?portType=FXO"   class="admin" ><?=sprintf(T_("New %s port Group"),"FXO")?></a> &nbsp;&nbsp;
      <a href="edit/?portType=FXS"   class="admin" ><?=sprintf(T_("New %s port Group"),"FXS")?></a> 
    </h3>
  </div>
  <table class="todolist">
    <tr>
      <?=$html?>
    </tr>
  </table>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <?$sel="zapGroup";include("../sidebar.php");?>
      <h1><?=T_("The group of Zapata ports")?></h1>
      <p></p>
    </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();
  //================ SETTINGS ================
  $smarty->assign("section", "zapGroup");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

