<?
/**
 * 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 templateAction($action,$id_template,$http_username,$http_password,$http_scheme){
    global $asterisk,$cwlang,$relBasePath;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Process request
    $asterisk->templatePhoneAction($action,$id_template,array(),$http_username,$http_password,$http_scheme);
    // Return the xajaxResponse object
    return $objResponse->getXML();  
  }
  $xajax = new xajax();
  $xajax->registerFunction("templateAction");
  $xajax->processRequests();
  //================= POST & GET ==================
  $alert=getAlert();
  if((isset($_GET["del"]))&&(trim($_GET["del"])!="")){
    $ret=$asterisk->delProvTemplate($_GET["del"]);
    if($ret>0) $alert="<div class=\"AlertGood\">".T_("Template has been deleted")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Template hasn't been deleted")."</div>";
    //go to main page
    goToMainPage($alert,"./");
  }
  //================= PAGE LOGIC ==================
  $ret=$asterisk->getProvTemplate(0);
  if($err=$asterisk->getError()) die("Error: $err");
  $count=0;
  if (is_array($ret)) {
    foreach($ret as $v) {
      $id=$v["id"];
      $name=$v["name"];
      $http_username=$v["http_username"];
      $http_password=$v["http_password"];
      $http_scheme=$v["http_scheme"];
      //get phone description
      $res_ph=$asterisk->getPhoneType($v["id_phone"]);
      if(is_array($res_ph)){
        $phone_name="(".$res_ph[0]["vendor"].$res_ph[0]["model"].")";
      }
      //html
      $html.="<td><img src=\"".$relBasePath."public/img/template.jpg\" alt=\"Tepmlate $id\"/> ";
      $html.="<a href=\"edit/?id=$id\">".$name." ".$phone_name."</a>&nbsp;&nbsp;";
      $html.="<img style=\"cursor: pointer;\" src=\"$relBasePath/public/img/reboot.png\" title=\"".T_("Reboot Template's phones")."\" onclick=\"if(confirm('".T_("Reboot all phones linked to the template")." ".$name."?')) xajax_templateAction('reboot',".$id.",'".$http_username."','".$http_password."',".$http_scheme."); return false;\"/>&nbsp;&nbsp;";
      $html.="<a href=\"$PHP_SELF?del=$id\" onclick=\"if (confirm('".sprintf(T_("Are you sure you want to remove template %1\$s"),$name )."')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a>";
      $html.="</td>\n";
      if((++$count)%2==0) $html.="</tr><tr>\n";
    }
  }
  
  //================ MAIN CONTENTS ================
  ob_start();
  $xajax->printJavascript("../../../../admin/lib/xajax/");
  ?>
    <?$nav="templates";include("../navigation.php");?>
    <div class="SectionHeader">
      <?=$alert?>
      <h1><?=T_("Provisioning Templates")?></h1>
      <h3>
        <a href="edit/" class="admin" ><?=T_("New Template")?></a> 
      </h3>
    </div>  
    <table class="todolist">
      <tr>
        <?=$html?>
      </tr>
    </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 configuration templates for phones under provisioning. To create a new template follow the link under the section title, otherwise click on an existing template 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');
?>