<?
/**
 * VoiceOne Settings/Contacts List
 *
 * Frontend to provide access to the contacts list system in voiceone.
 * @package voiceone
 * @subpackage  settings_extensions
 */

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

  //================ PAGE LOGIC ================
  //get exstension status
  $statusArr=array();
  $dir=$asterisk->getDirectory(0);
  if(is_array($dir)){
    foreach($dir as $v){
      $statusArr[$v["id"]]=$v;
    }
  }
  if((is_array($dir))&&(count($dir)>0)){
    $blacklist=array();
    //build html for virtual exstensions
    $count=0;
    $ret=$asterisk->getVirtualExtension(0);
    if((is_array($ret))&&(count($ret)>0)){
      $html_int="<tr>";
      foreach($ret as $v){
        $id=$v["id"];
        $label=$v["name"]." ".$v["surname"]." &lt;".$v["extension"]."&gt;";
        $linked_clients=$v["linked_clients"];
        //build linked clients html
        $html_ext="";
        $staus_ok=false;
        if((is_array($linked_clients))&&(count($linked_clients)>0)){
          foreach($linked_clients as $c_id){
            $client=$statusArr[$c_id];
            $c_label="&lt;".$client["extension"]."&gt;";
            $c_status=$client["status"];
            if(strpos($c_status,"OK")!==false){ $class="online"; $staus_ok=true;}
            if(strpos($c_status,"UNKNOWN")!==false) $class="offline";
            if(strpos($c_status,"Unmonitored")!==false) $class="unknown";
            $html_ext.="<li style=\"margin:0; padding:0\">";
            $html_ext.="<img src=\"".$relBasePath."public/img/$class.gif\" alt=\"".$class."\"/> ";
            if((is_array($cwUserInfo))&&(trim($cwUserInfo["exten"])!="")&&($class=="online")){
              $html_ext.="<a href=\"dial/?contact=".$c_id."&type=extension&TB_iframe=true&height=150&width=350\" class=\"thickbox\" title=\"Call Extension\">".$c_label."</a>";
            }else $html_ext.=$c_label;
            $html_ext.="</li>\n";
            $blacklist[]=$c_id;
          }
        }
        //build extension html
        if(trim($html_ext)!=""){
          if($staus_ok===true) $class="online"; else $class="offline";
          $html.="<td class=\"$class\">";
          $html.="<img src=\"".$relBasePath."public/img/$class.gif\" alt=\"".$class."\"/> ";
          if((is_array($cwUserInfo))&&(trim($cwUserInfo["exten"])!="")&&($class=="online")){
            $html.="<a href=\"dial/?contact=".$id."&type=virtual&TB_iframe=true&height=150&width=350\" class=\"thickbox\" title=\"Call Extension\">".$label."</a>";
          }else $html.=$label;
          $html.="<ul id=\"vext_".$id."\" style=\"list-style:none; margin-left: 30px;padding-top: 0.5em;\">".$html_ext."</ul>";
          $html.="</td>";
          if((++$count)%2==0) $html.="</tr><tr>\n";
        }
      }
      if(($count!=0)&&(($count%2)!=0)) $html.="<td colspan=\"".($count%2)."\">&nbsp;</td>\n";
      $html.="</tr>";
    }
    //build html for singol exstensions
    $count=0;
    $html.="<tr>";
    foreach($dir as $v){
      if((is_array($blacklist))&&(in_array($v["id"],$blacklist))) continue;
      $id=$v["id"];
      $label=$v["name"]." ".$v["username"]." &lt;".$v["extension"]."&gt;";
      $status=$v["status"];
      if (strpos($status,"OK")!==false) $class="online";
      if (strpos($status,"UNKNOWN")!==false) $class="offline";
      if (strpos($status,"Unmonitored")!==false) $class="unknown";
      $html.="<td class=\"$class\">";
      $html.="<img src=\"".$relBasePath."public/img/$class.gif\" alt=\"".$class."\"/> ";
      if((is_array($cwUserInfo))&&(trim($cwUserInfo["exten"])!="")&&($class=="online")){
        $html.="<a href=\"dial/?contact=".$id."&type=extension&TB_iframe=true&height=150&width=350\" class=\"thickbox\" title=\"Call Extension\">".$label."</a>";
      }else{
        $html.=$label;
      }
      $html.="</td>";
      if((++$count)%2==0) $html.="</tr><tr>\n";
    }
    if(($count!=0)&&(($count%2)!=0)) $html.="<td colspan=\"".($count%2)."\">&nbsp;</td>\n";
    $html.="</tr>";
  }
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=T_("Internal Contacts")?></h1>
  </div>
  <table class="todolist">
    <?=$html?>
  </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_("In this page you can find all the extensions in use on the pbx. For each item, you can see the status. The possible status are:<br><br>")?></p>
       <ul>
        <li style="margin-bottom:5px;"><img src="<?=$relBasePath?>/public/img/online.gif" align="left"/><strong><?=T_("Online")?>: </strong><?=T_("The user is currently connected")?></li>
        <li style="margin-bottom:5px;"><img src="<?=$relBasePath?>/public/img/offline.gif" align="left"/><strong><?=T_("Offline")?>: </strong><?=T_("The user is offline")?></li>
        <li style="margin-bottom:5px;"><img src="<?=$relBasePath?>/public/img/unknown.gif" align="left"/><strong><?=T_("Unknown")?>: </strong><?=T_("The status is unknonwn")?></li>
       </ul>
       <div style="text-align: center"><img src="<?=$relBasePath?>public/img/book.jpg" alt="Rubrica" align="center"/></div>
    </div>
  <?
  $sidebar=ob_get_contents();
  ob_end_clean();
  //================ SETTINGS ================
  $smarty->assign("section", "contacts");
  $smarty->assign("subsection", "");
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('main.tpl');
?>
