<?
/**
 * VoiceOne Settings/Extensions/Provisioning
 *
 * 
 * @package voiceone
 * @subpackage  settings_extensions
 */
  include ("../../../../admin/config/cfg.php");
  global $cwlang;
  require_once("$absBasePath/lib/xajax/xajax.inc.php"); 
  //================ XAJAX ================
  function action($action,$ip,$http_username,$http_password,$http_scheme){
    global $asterisk,$cwlang,$relBasePath;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Process request
    $asterisk->phoneAction($action,$ip,array(),$http_username,$http_password,$http_scheme);
    // Return the xajaxResponse object
    return $objResponse->getXML();  
  }
  $xajax = new xajax();
  $xajax->registerFunction("action");
  $xajax->processRequests();
  //================= POST & GET ==================
  $alert=getAlert();
  if((isset($_GET["del"]))&&(trim($_GET["del"])!="")&&(isset($_GET["client"]))&&(trim($_GET["client"])!="")){
    $ret=$asterisk->touchProvClient($_GET["del"],$_GET["client"]);
    if($ret>0) $alert="<div class=\"AlertGood\">".T_("Client's provisioning settings has been deleted")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Client's provisioning settings hasn't been deleted")."</div>";
    //go to main page
    goToMainPage($alert,"./");
  }
  //================= PAGE LOGIC ==================
  //get phone types clients
  $res=$asterisk->getPhoneType(0);
  if(is_array($res)){
    foreach($res as $phone){
      $phoneArr[$phone["id"]]=$phone["vendor"].$phone["model"];
    }
  }
  //get provisioning clients
  $html="";
  $res=$asterisk->getProvClient(0);
  if(is_array($res)){
    foreach($res as $p){
      $id=$p["id"];
      $id_client=$p["id_client"];
      $id_template=$p['id_template'];
      $mac=trim($p['mac']);
      $ip=$p['ip'];
      $external_ip=$p['external_ip'];
      $external_port=$p['external_port'];
      //extension
      $html.="<tr>";
      $clientArr=$asterisk->getClient($id_client,0);
      if(is_array($clientArr)){
        $extension=$clientArr[0]["extension"];
        $html.="  <td class=\"first small\"><span class=\"ext_list\"><a href=\"./edit/?id=".$id."\" style=\"cursor: pointer;\">".$extension."</a></span></td>";
      }else $html.="<td>&nbsp;-&nbsp;</td>";
      //prov client is ON
      if((isset($id_template))&&($id_template!=0)){
        $templateArr=$asterisk->getProvTemplate($p['id_template']);
        if((is_array($templateArr))&&(is_array($templateArr[0]))){
                $template_name=$templateArr[0]["name"];
          $id_phone=$templateArr[0]["id_phone"];
                $dhcp=$templateArr[0]["dhcp"];
          $local=$templateArr[0]["local"];
          $http_username=$templateArr[0]["http_username"];
          $http_password=$templateArr[0]["http_password"];
          $http_scheme=$templateArr[0]["http_scheme"];
        }
        $html.="<td><a href=\"../templates/edit/?id=".$id_template."\" style=\"font-size: 10px; font-weight: bold;\">".$template_name."</a></td>";
        //phone type
        if((isset($id_phone))&&($id_phone!=0)){
                                        $res_ph=$asterisk->getPhoneType($id_phone);
          if(is_array($res_ph)){
            $phone_name=$res_ph[0]["vendor"].$res_ph[0]["model"];
          }else $phone_name=$id_phone;
          $html.="<td><span style=\"font-size: 10px; font-weight: bold;\">".$phone_name."</span></td>";
        }else $html.="<td>&nbsp;-&nbsp;</td>";
        //mac
        if((isset($mac))&&($mac!="")){
                $html.="<td><img src=\"$relBasePath/public/img/ok.gif\" alt=\"MAC: ".$mac."\" /></td>";
          //$html.="<td><span style=\"font-size: 10px; font-weight: bold;\">".$mac."</span></td>";
        }else $html.="<td>&nbsp;-&nbsp;</td>";
        //dhcp
        if((isset($dhcp))&&($dhcp==1)){
          //$html.="<td><img src=\"$relBasePath/public/img/ok.gif\" /></td>";
          $html.="<td><span style=\"font-size: 10px; font-weight: bold;\">On</span></td>";
        }else{
                //$html.="<td>&nbsp;-&nbsp;</td>";
          $html.="<td><span style=\"font-size: 10px; font-weight: bold;\">Off</span></td>";
        }
        //local ip
        if((isset($ip))&&($ip!="")){
          $html.="<td><a href=\"http://".$ip."\" style=\"cursor: pointer;\"><span style=\"font-size: 10px; font-weight: bold;\">".$ip."</span></a></td>";
        }else $html.="<td>&nbsp;-&nbsp;</td>";
        //external ip
        if((isset($local))&&($local==0)){
          $html.="<td><span style=\"font-size: 10px; font-weight: bold;\">".$external_ip.":".$external_port."</span></td>";
        }else $html.="<td>&nbsp;-&nbsp;</td>";
        //actions
        $addr=$asterisk->getProvClientAddress($id);
        if($addr!=""){
          $html.="<td>";
          $html.="<img style=\"cursor: pointer;\" src=\"$relBasePath/public/img/ring.gif\" title=\"".T_("Ring")."\" onclick=\"if(confirm('".T_("Play ringer extension")." ".$extension."?')) xajax_action('ring','".$addr."','".$http_username."','".$http_password."','".$http_scheme."'); return false;\"/>&nbsp;";
          $html.="<img style=\"cursor: pointer;\" src=\"$relBasePath/public/img/register.gif\" title=\"".T_("Re-Register")."\" onclick=\"if(confirm('".T_("Re-Register extension")." ".$extension."?')) xajax_action('reregister','".$addr."','".$http_username."','".$http_password."','".$http_scheme."'); return false;\"/>&nbsp;";
          $html.="<img style=\"cursor: pointer;\" src=\"$relBasePath/public/img/reboot.png\" title=\"".T_("Reboot")."\" onclick=\"if(confirm('".T_("Reboot extension")." ".$extension."?')) xajax_action('reboot','".$addr."','".$http_username."','".$http_password."','".$http_scheme."'); return false;\"/>&nbsp;";
          $html.="<img style=\"cursor: pointer;\" src=\"$relBasePath/public/img/shutdown.png\" title=\"".T_("Reset")."\" onclick=\"if(confirm('".T_("Reset extension")." ".$extension."?')) xajax_action('reset','".$addr."','".$http_username."','".$http_password."','".$http_scheme."'); return false;\"/>";
          $html.="</td>";
        }else $html.="<td>&nbsp;-&nbsp;</td>";
        //delete
        $html.="  <td><img style=\"cursor: pointer;\" src=\"$relBasePath/public/img/trash.gif\" title=\"".T_("Delete")."\" onclick=\"if(confirm('".T_("Are you sure you want to delete provisioning settings for this client?")."')) location.href='./?del=".$id."&client=".$id_client."'; else return false;\" /></td>";
      }
      //prov client is OFF
      else{ 
        for($i=0;$i<7;$i++) $html.="<td style=\"background:#DADADA;\">&nbsp;-&nbsp;</td>";
        $html.="<td>&nbsp;-&nbsp;</td>";
      }
      $html.="</tr>";
    }
  }else{
    $html.="<tr><td class=\"first\" colspan=\"9\"><span style=\"font-size: 10px; font-weight: bold;\">".T_("There are no defined extensions")."</span></td></tr>";
  }
  //================ MAIN CONTENTS ================
  ob_start();
  $xajax->printJavascript("../../../../admin/lib/xajax/");
  ?>
    <?$nav="clients";include("../navigation.php");?>
    <div class="SectionHeader">
      <?=$alert?>
      <h1><?=T_("Provisioning Clients")?></h1>
    </div>  
    <table id="ext_table" cellpadding="0" cellspacing="0" width="100%">
                <tr>
        <th class="small none">&nbsp;</th>
        <th class="first bordered" width="14%"><?=T_("Template")?></th>
        <th class="bordered" width="14%"><nobr><?=T_("Phone Type")?></nobr></th>
        <th class="bordered" width="8%"><?=T_("Bind")?></th>
        <th class="bordered" width="8%"><?=T_("DHCP")?></th>
        <th class="bordered" width="16%"><?=T_("IP")?></th>
        <th class="bordered" width="16%"><nobr><?=T_("Remote Address")?></nobr></th>
        <th class="bordered" width="14%"><?=T_("Actions")?></nobr></th>
        <th class="small none">&nbsp;</th>
      </tr>
      <?=$html?>
    </table>
  <?
  $contents=ob_get_contents();
  ob_end_clean();
  
  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <?$sel="provisioning";include("../../sidebar.php");?>
      <h1><?=T_("Informations")?></h1>
      <p><?=T_("This page shows the extensions which have joined a phone with configuration managed by system. It's possible to join an extension to a phone and a configuration template. Click on an extension to create a join or to modify an existing one or on the basket 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');
?>
