<?
/**
 * VoiceOne Settings/Queues
 *
 * Frontend to provide access to the queue system in voiceone.
 * @package voiceone
 * @subpackage  settings_queues
 */
  include ("../../../admin/config/cfg.php");
  global $cwlang;
    
  //=================== GET ====================
  $alert=getAlert();
  if(($_GET["cmd"]=="logoff")&&($_GET["id"]!="")) {
    $ret=$asterisk->logoffAgent($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".T_("The agent has been logged off")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("The agent hasn't been logged off")."</div>";
    goToMainPage($alert,"./");
  }  
  if(($_GET["cmd"]=="del")&&(trim($_GET["id"])!="")){
    $ret=$asterisk->delAgent($_GET["id"]);
    if($ret==1) $alert="<div class=\"AlertGood\">".T_("The agent has been deleted")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("The agent hasn't been deleted")."</div>";
    goToMainPage($alert,"./");
  }  
  //================ PAGE LOGIC ================ 
  $ret=$asterisk->getAgent(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  $html_ext="";$html_virtual="";$html_appl="";
  $count_ext=0;$count_virtual=0;$count_appl=0;
  if(is_array($ret)) {
    //Extract logged agents
    $availAgents=$asterisk->getRegisteredAgent();
    if ($err=$asterisk->getError()) die("Error: $err");
    //Build html list
    foreach($ret as $v) {
      $id=$v["id"];
      $clientArr=getClient($v["client"]);
      //Is agent linked to an extension?
      if((is_array($clientArr))&&(is_array($clientArr[0]))){
        $client=$clientArr[0];
        $ext=$client["extension"];
        $callerid=$client["name"]." ".$client["surname"];
        if((is_array($availAgents))&&(in_array($ext,$availAgents))){
          $icon="<img src=\"$relBasePath/public/img/agent_on.gif\" />";
          $logoff="<a href=\"$PHP_SELF?cmd=logoff&id=$id\" onclick=\"if(confirm('".sprintf(T_("Are you sure you want to log off agent %1\$s ?"),$ext )."')) return true; else return false;\"><small>(".T_("Log Off").")</small></a>";
        }else{
          $icon="<img src=\"$relBasePath/public/img/agent_off.gif\" />";
          $logoff="";
        }
      	$html_ext.="<td>$icon $callerid &lt;$ext&gt; $logoff</td>\n";
        if((++$count_ext)%2==0) $html_ext.="</tr><tr>\n";
      }
      //Is agent linked to a virtual extension?
      else{
      	$virtualArr=getVirtualExtension($v["client"]);
        if((is_array($virtualArr))&&(is_array($virtualArr[0]))){
        	$virtual=$virtualArr[0];
          $ext=$virtual["extension"];
          $callerid=$virtual["name"]." ".$virtual["surname"];
          if((is_array($availAgents))&&(in_array($ext,$availAgents))){
            $icon="<img src=\"$relBasePath/public/img/agent_on.gif\" />";
            $logoff="<a href=\"$PHP_SELF?cmd=logoff&id=$id\" onclick=\"if(confirm('".sprintf(T_("Are you sure you want to log off agent %1\$s ?"),$ext )."')) return true; else return false;\"><small>(".T_("Log Off").")</small></a>";
          }else{
            $icon="<img src=\"$relBasePath/public/img/agent_off.gif\" />";
            $logoff="";
          }
      		$html_virtual.="<td>$icon $callerid &lt;$ext&gt; $logoff</td>\n";
          if((++$count_virtual)%2==0) $html_virtual.="</tr><tr>\n";
        }
        //Is agent linked to an application?
       	else{
        	$applArr=getApplication($v["client"]);
          if((is_array($applArr))&&(is_array($applArr[0]))){
          	$appl=$applArr[0];
            $ext=$appl["extension"];
            $appl_name="<a href=\"edit/?id=".$v["id"]."\">".$appl["label"]."</a>";
            $del="<a href=\"$PHP_SELF?cmd=del&id=$id\" onclick=\"if (confirm('".sprintf(T_("Are you sure you want to remove agent %1\$s"),$appl["label"] )."')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a>";
            if((is_array($availAgents))&&(in_array($ext,$availAgents))){
              $icon="<img src=\"$relBasePath/public/img/agent_on.gif\" />";
              $logoff="<a href=\"$PHP_SELF?cmd=logoff&id=$id\" onclick=\"if(confirm('".sprintf(T_("Are you sure you want to log off agent %1\$s ?"),$ext )."')) return true; else return false;\"><small>(".T_("Log Off").")</small></a>";
            }else{
              $icon="<img src=\"$relBasePath/public/img/agent_off.gif\" />";
              $logoff="";
            }
        		$html_appl.="<td>$icon $appl_name &lt;$ext&gt; $del $logoff</td>\n";
            if((++$count_appl)%2==0) $html_appl.="</tr><tr>\n";
          }
        }
      }  
    }
  }  
  if(trim($html_ext)!="") $html_ext="<tr><td colspan=\"2\" class=\"todolisttitle\">".T_("Extensions")."</td></tr><tr>\n".$html_ext."</tr><tr><td colspan=\"2\">&nbsp;</td></tr>";
  if(trim($html_virtual)!="") $html_virtual="<tr><td colspan=\"2\" class=\"todolisttitle\">".T_("Virtual Extensions")."</td></tr><tr>\n".$html_virtual."</tr><tr><td colspan=\"2\">&nbsp;</td></tr>";
  if(trim($html_appl)!="") $html_appl="<tr><td colspan=\"2\" class=\"todolisttitle\">".T_("Applications")."</td></tr><tr>\n".$html_appl."</tr><tr><td colspan=\"2\">&nbsp;</td></tr>";
  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=T_("Agent")?></h1> 
    <h3>
      <a href="edit/" class="admin" ><?=T_("New agent")?></a> 
    </h3>
  </div>
  <table class="todolist">
    <?=$html_appl?>
  	<?=$html_virtual?>
    <?=$html_ext?>
  </table>
  <?
  $contents=ob_get_contents();
  ob_end_clean();
  
  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <?$sel="agents";include("../sidebar.php");?>
      <h1><?=T_("Informations")?></h1>
      <p><?=T_("This page shows the agents configured on the system, showing the logged ones with an active icon. System automatically creates an agent for each extension and virtual extension. If this agent is logged in, the extension receives the calls from the queues in which it is included. It is also possible to create an agent for an application following the link under the section title.")?></p>
    </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

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