<?
/**
 * VoiceOne Settings/Extensions/Voicemail
 *
 * Frontend to provide access to the extensions system in voiceone.
 * @package voiceone
 * @subpackage  settings_extensions
 */

  include ("../../../admin/config/cfg.php");
  global $cwlang;

  //================ POST & GET =================
  $alert=getAlert();
  //delete mailbox
  if((isset($_GET["del"]))&&($_GET["del"]!="")){
    $extension=$_GET["del"];
    //set asterisk database
    $r1=$asterisk->rmFromDB("DND",$extension);
    $r2=$asterisk->rmFromDB("CFU",$extension);
    $r3=$asterisk->rmFromDB("CFB",$extension);
    $r4=$asterisk->rmFromDB("CFNR",$extension);
    $ret=$r1*$r2*$r3*$r4;
    if ($ret>0) $alert="<div class=\"AlertGood\">".T_("Extension's Call Forward settings has been edited")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Extension's Call Forward settings hasn't been edited")."</div>";
    //go to main page
    goToMainPage($alert,"./");
  }
  if((isset($_GET["del-virtual"]))&&($_GET["del-virtual"]!="")){
    $virtual=$asterisk->getVirtualExtension($_GET["del-virtual"]);
    if((is_array($virtual))&&(is_array($virtual[0]))){
    	$extension=$virtual[0]["extension"];
    	//delete virtaul extension cf settings
    	$r1=$asterisk->rmFromDB("DND",$extension);
      $r2=$asterisk->rmFromDB("CFU",$extension);
      $r3=$asterisk->rmFromDB("CFB",$extension);
      $r4=$asterisk->rmFromDB("CFNR",$extension);
      $ret=$r1*$r2*$r3*$r4;
      //delete cf settings for each linked clients
    	if(($ret!=0)&&(is_array($virtual[0]["linked_clients"]))&&(count($virtual[0]["linked_clients"])>0)){
      	foreach($virtual[0]["linked_clients"] as $client_id){
        	$clientArr=$asterisk->getClient($client_id,0);
          if((is_array($clientArr))&&(is_array($clientArr[0]))){
          	$extension=$clientArr[0]["extension"];
          	//set asterisk database
            $r1=$asterisk->rmFromDB("DND",$extension);
            $r2=$asterisk->rmFromDB("CFU",$extension);
            $r3=$asterisk->rmFromDB("CFB",$extension);
            $r4=$asterisk->rmFromDB("CFNR",$extension);
            $ret=$r1*$r2*$r3*$r4*$ret;
            if($ret==0) break;
          }
        }
      }
    }else $ret=0;
    if ($ret>0) $alert="<div class=\"AlertGood\">".T_("Extension's Call Forward settings has been edited")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Extension's Call Forward settings hasn't been edited")."</div>";
    //go to main page
    goToMainPage($alert,"./");
  }
  if((isset($_GET["del-group"]))&&($_GET["del-group"]!="")){
    $groupString='';
    $entityArr=$asterisk->resolveExstensionGroup($_GET["del-group"]);
    if((is_array($entityArr))&&(count($entityArr)>1)){
      foreach($entityArr as $entity) $groupString.=$entity."&";
      if(trim($groupString)!='') $groupString=substr($groupString,0,-1);
      //set asterisk database
      $r1=$asterisk->rmFromDB("DND",$groupString);
      $r2=$asterisk->rmFromDB("CFU",$groupString);
      $r3=$asterisk->rmFromDB("CFB",$groupString);
      $r4=$asterisk->rmFromDB("CFNR",$groupString);
      $ret=$r1*$r2*$r3*$r4;
    }else $ret=0;
    if ($ret>0) $alert="<div class=\"AlertGood\">".T_("Group's Call Forward settings has been edited")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Group's Call Forward settings hasn't been edited")."</div>";
    //go to main page
    goToMainPage($alert,"./");
  }
  //================ PAGE LOGIC ================
  //verify that asterisk is ON
  $status=$asterisk->pbxSwitch("status");
  if($status=="ON"){
    //get clients
    $clientArr=$asterisk->getClient(0,0);
    $virtualArr=$asterisk->getVirtualExtension(0);
    $groupArr=array();
    $groupsList=$asterisk->getGroup(0,GROUP_OF_EXTENSION);
    if(is_array($groupsList)){
      foreach($groupsList as $group){
        if($group['metagroup']!=0) continue;
        $groupString='';
        $entityArr=$asterisk->resolveExstensionGroup($group['id']);
        if((is_array($entityArr))&&(count($entityArr)>1)){
          foreach($entityArr as $entity) $groupString.=$entity."&";
          if(trim($groupString)!='') $groupString=substr($groupString,0,-1);
          $groupArr[]=array(
              "id" => $group['id'],
              "name" => $group['name'],
              "string" => $groupString
            );
        }
      }
    }
    //get cf settings
    $cfArr=$asterisk->getClientsDBEntry(0);
    //build extension's html
    $blacklist=array();
    if(is_array($groupArr)){
      foreach($groupArr as $group){
        $entryArr[$group["name"]]["id"]=$group["id"];
        $entryArr[$group["name"]]["settings"]=$cfArr[$group["string"]];
        $entryArr[$group["name"]]["type"]="group";
      }
    }
    if(is_array($virtualArr)){
      foreach($virtualArr as $virtual){
      	//add virtual extensions to client list
      	$entryArr[$virtual["extension"]]["id"]=$virtual["id"];
        $entryArr[$virtual["extension"]]["settings"]=$cfArr[$virtual["extension"]];
        $entryArr[$virtual["extension"]]["type"]="virtual";
        $blacklist=array_merge($blacklist,$virtual["linked_clients"]);
      }
    }
    if(is_array($clientArr)){
      foreach($clientArr as $client){
      	//skip extensions that are contained in a virtual extension
      	if((is_array($blacklist))&&(in_array($client["id"],$blacklist,true))){
        	continue;
        }
        //add single extensions to client list
        $entryArr[$client["extension"]]["id"]=$client["id"];
        $entryArr[$client["extension"]]["settings"]=$cfArr[$client["extension"]];
        $entryArr[$client["extension"]]["type"]="extension";
      }
      foreach($entryArr as $extension=>$v){
        $id=$v["id"];
        $settings=$v["settings"];
        $type=$v["type"];
        $html.="<tr>";
        $html.="  <td class=\"first small\"><span class=\"ext_list\"><a href=\"./edit/?id=".$id."&type=".$type."\" style=\"cursor: pointer;\">".$extension."</a></span></td>";
        //inizialize DND
        if((isset($settings["DND"]))&&($settings["DND"]!="")){
          $html.="<td><img src=\"$relBasePath/public/img/ok.gif\" title=\"$title\" /></td>";
        }else $html.="<td>&nbsp;-&nbsp;</td>";
        //inizialize CFU
        if((isset($settings["CFU"]))&&($settings["CFU"]!="")){
          $html.="<td><span style=\"font-size: 10px; font-weight: bold;\">".$settings["CFU"]."</span></td>";
        }else $html.="<td>&nbsp;-&nbsp;</td>";
        //inizialize CFB
        if((isset($settings["CFB"]))&&($settings["CFB"]!="")){
          $html.="<td><span style=\"font-size: 10px; font-weight: bold;\">".$settings["CFB"]."</span></td>";
        }else $html.="<td>&nbsp;-&nbsp;</td>";
        //inizialize CFNR
        if((isset($settings["CFNR"]))&&($settings["CFNR"]!="")){
          $html.="<td><span style=\"font-size: 10px; font-weight: bold;\">".$settings["CFNR"]."</span></td>";
        }else $html.="<td>&nbsp;-&nbsp;</td>";
        //delete button
        switch($type){
          case 'extension':
            $html.="  <td><img style=\"cursor: pointer;\" src=\"$relBasePath/public/img/trash.gif\" onclick=\"if(confirm('".T_("Are you sure you want to deactivate all settings for this Extension?")."')) location.href='./?del=".$extension."'; else return false;\" /></td>";
            break;
          case 'virtual':
            $html.="  <td><img style=\"cursor: pointer;\" src=\"$relBasePath/public/img/trash.gif\" onclick=\"if(confirm('".T_("Are you sure you want to deactivate all settings for this Extension?")."')) location.href='./?del-virtual=".$id."'; else return false;\" /></td>";
            break;
          case 'group':
            $html.="  <td><img style=\"cursor: pointer;\" src=\"$relBasePath/public/img/trash.gif\" onclick=\"if(confirm('".T_("Are you sure you want to deactivate all settings for this Group?")."')) location.href='./?del-group=".$id."'; else return false;\" /></td>";
            break;
        }
        $html.="</tr>";
      }
    }else{
      $html.="<tr>";
      $html.="  <td class=\"first\" colspan=\"6\"><span style=\"font-size: 10px; font-weight: bold;\">".T_("There are no defined extensions")."</span></td>";
      $html.="</tr>";
    }
  }else{
    $html.="<tr>";
    $html.="  <td class=\"first\">&nbsp;</td>";
    $html.="  <td colspan=\"4\"><span style=\"font-size: 10px; font-weight: bold;\">".T_("No information available when PBX is off")."</span></td>";
    $html.="  <td>&nbsp;</td>";
    $html.="</tr>";
  }

  //================ MAIN CONTENTS ================
  ob_start();
  ?>
    <div class="SectionHeader">
      <?=$alert?>
      <h1><?=T_("Call Forward")?></h1>
    </div>
    
    <table id="ext_table" cellpadding="0" cellspacing="0" width="100%">
  		<tr>
        <th class="small none">&nbsp;</th>
        <th class="first bordered" width="20%"><?=T_("DND")?></th>
        <th class="bordered" width="20%"><?=T_("CFU")?></th>
        <th class="bordered" width="20%"><?=T_("CFB")?></th>
        <th class="bordered" width="20%"><?=T_("CFNR")?></th>
        <th class="small none">&nbsp;</th>
      </tr>
      <?=$html?>
    </table>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start();
  ?>
    <div class="Sidebar">
      <?$sel="callforward";include("../sidebar.php");?>
      <h1><?=T_("Informations")?></h1>
      <p><?=T_("This page allows you to configure DND and call forward services. The extensions which have DND enabled are always unreachable. The calls to extensions which have call forward enabled are transfered to the defined number only if the extension is busy (CFB) or only on no reply (CFNR) or always (CFU). Click on an extension to edit the configuration or on the basket to clean settings.")?></p>
    </div>
  <?
  $sidebar=ob_get_contents();
  ob_end_clean();

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

