<?
/**
 * VoiceOne Settings/Extensions
 *
 * 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();
  if($_GET["cmd"]=="del") {
    //delete extension
    $ret=$asterisk->delClient($_GET["id"]);
    //remove cid setting
    if($ret==1){
      $ret=$asterisk->delCid(0,$_GET["id"]);
    }
    //set return
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("Extension successfully deleted")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Delete failed")."</div>";
    goToMainPage($alert,"./");
  }
  if($_GET["cmd"]=="del-virtual"){
    //delete extension
    $ret=$asterisk->delVirtualExtension($_GET["id"]);
    //set return
    if($ret>0) $alert="<div class=\"AlertGood\">".T_('Virtual Extension successfully deleted')."</div>";
    else $alert="<div class=\"AlertBad\">".T_('Delete failed')."</div>";
    goToMainPage($alert,"./");
  }
  if((_isModuleLoaded('fax'))&&($_GET["cmd"]=="del-fax")){
    //delete extension
    $ret=$asterisk->delFaxExtension($_GET["id"]);
    //set return
    if($ret>0) $alert="<div class=\"AlertGood\">".T_('Fax Extension successfully deleted')."</div>";
    else $alert="<div class=\"AlertBad\">".T_('Delete failed')."</div>";
    goToMainPage($alert,"./");
  }
  //================ PAGE LOGIC ================
  $html="";
  $blacklist=array();
  //get fax clients
  if(_isModuleLoaded('fax')){
    $ret=$asterisk->getFaxExtension(0);
    if($err=$asterisk->getError()) die("Error: $err");
    $count=0;
    if((is_array($ret))&&(count($ret)>0)){
      $html.="<td colspan=\"2\" class=\"todolisttitle\">".T_("Fax Extensions")."</td></tr><tr>\n";
      foreach($ret as $f){
        $id=$f["id"];
        $phonenumber=$f["phonenumber"];
        //put linked clients in black list 
        if(is_array($f["client"])) $blacklist=array_merge($blacklist,array($f["client"]["id"]));
        //build html
        $fax_name=$f["client"]["name"]." ".$f["client"]["surname"]." &lt;".$f["client"]["extension"]."&gt; (".$phonenumber.")";
        $fax_icon="<img src=\"$relBasePath/public/img/fax.gif\" />";
        $html.="<td style=\"vertical-align: top;\">";
        $html.="$fax_icon <a href=\"edit-fax/?id=$id\">$fax_name</a>  <a href=\"$PHP_SELF?cmd=del-fax&id=$id\" onclick=\"if (confirm('".T_('Account:')." ".sprintf(__('Are you sure you want to remove the fax extension %s'),$f["client"]["extension"] )."')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a>\n";
        $html.="</td>\n";
        if ((++$count)%2==0) $html.="</tr><tr>\n";
      }
      $html.="<tr><td colspan=\"2\">&nbsp;</td></tr><tr>\n";
    }
  }
  //get virtual clients
  $ret=$asterisk->getVirtualExtension(0);
  if($err=$asterisk->getError()) die("Error: $err");
  $count=0;
  if((is_array($ret))&&(count($ret)>0)){
    $html.="<td colspan=\"2\" class=\"todolisttitle\">".T_("Virtual Extensions")."</td></tr><tr>\n";
    foreach($ret as $v){
      $id=$v["id"];
      //put linked clients in black list 
      if(is_array($v["linked_clients"])) $blacklist=array_merge($blacklist,$v["linked_clients"]);
      //build html
      $virtual_name=$v["name"]." ".$v["surname"]."&lt;".$v["extension"]."&gt;";
      $virtual_icon="<img src=\"$relBasePath/public/img/virtual_extension.jpg\" />";
      $html.="<td style=\"vertical-align: top;\">";
      $html.="$virtual_icon <a href=\"edit-virtual/?id=$id\">$virtual_name</a>  <a href=\"$PHP_SELF?cmd=del-virtual&id=$id\" onclick=\"if (confirm('".T_('Account:')." ".sprintf(__('Are you sure you want to remove the virtual extension %s'),$ext )."')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a>\n";
      if(is_array($v["linked_clients"])){
        $html.="<ul style=\"list-style:none; margin-left: 50px;padding-top: 0.5em;\">";
        foreach($v["linked_clients"] as $id_client){
        	$client=array();
          $client=getClient($id_client,0);
          if((is_array($client))&&(is_array($client[0]))){
          	$client=$client[0];
          	$client_tech=$client["technology"];
      			//$client_name=$client["name"]." ".$client["surname"]." &lt;".$client["extension"]."&gt";
            $client_name=$client["extension"];
      			$client_icon=getIcon($client_tech);
          	$html.="<li style=\"margin:0; padding:0\">$client_icon <a href=\"../extensions/edit/?id=".$client["id"]."\">".$client_name."</a></li>";
        	}
        }
        $html.="</ul>";
      }
      $html.="</td>\n";
      if ((++$count)%2==0) $html.="</tr><tr>\n";
    }
    $html.="<tr><td colspan=\"2\">&nbsp;</td></tr><tr>\n";
  }
  //get clients
  $ret=$asterisk->getClient(0,0);
  $count=0;
  if((is_array($ret))&&(count($ret)>0)){
  	$html.="<td colspan=\"2\" class=\"todolisttitle\">".T_("Extensions")."</td></tr><tr>\n";
    foreach($ret as $v){
      $id=$v["id"];
      //skip extensions that are contained in a virtual extension
    	if((is_array($blacklist))&&(in_array($id,$blacklist,true))){
      	continue;
      }
      $ext=$v["extension"];
      $tech=$v["technology"];
      $callerid=$v["name"]." ".$v["surname"];
      $icon=getIcon($tech);
      $html.="<td>$icon  <a href=\"edit/?id=$id\">$callerid &lt;$ext&gt;</a>  <a href=\"$PHP_SELF?cmd=del&id=$id\" onclick=\"if (confirm('".T_("Account:")." ".sprintf(T_("Are you sure you want to remove the extension %s"),$ext )."')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a></td>\n";
      if ((++$count)%2==0) $html.="</tr><tr>\n";
    }
  }

  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=T_("Extensions")?></h1>
    <h3>
      <a href="edit/" class="admin" ><?=T_("New extension")?></a>
      <a style="margin-left:10px;" href="edit-virtual/" class="admin" ><?=T_("New virtual extension")?></a>
      <? if(_isModuleLoaded('fax')){ ?><a style="margin-left:10px;" href="edit-fax/" class="admin" ><?=T_("New fax extension")?></a> <? } ?>
    </h3>
  </div>
  <table class="todolist">
    <tr>
      <?=$html?>
    </tr>
  </table>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <?$sel="extensions";include("../sidebar.php");?>
      <h1><?=T_("Informations")?></h1>
      <p><?=T_("This page shows the extensions configured on the system. To create a new extension follow the links under the section title, otherwise click on an existing extension to edit it or on the basket on the right of the name to delete it.")?></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');
?>

