<?
/**
 * 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 processFormData($formData){
    global $asterisk,$cwlang,$relBasePath;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Extract form params
    $res=$formData["res"];
    $imgErr="<img src=\"".$relBasePath."/public/img/shutdown.png\" alt=\"error\">";
    // Process Form
    if(isset($errOut)){
      $error="<div class=\"AlertBad\"><ul>";
      foreach($errOut as $err) $error.="<li>".$err."</li>";
      $error.="</ul></div>";
      $objResponse->addAssign("alert","innerHTML",$error);
      $objResponse->addAssign("loading","innerHTML","");
    }else{
      $objResponse->addAssign("loading","innerHTML","");
      $script="document.getElementById('mainform').submit(); ";
      $objResponse->addScript($script);
    }
    // Return the xajaxResponse object
    return $objResponse->getXML();  
  }
  $xajax = new xajax();
  $xajax->registerFunction("processFormData");
  $xajax->processRequests();
  
  //================== POST ====================
  if(count($_POST)>0){
    //extract request
    if(is_array($_POST["res"])){
      foreach($_POST["res"] as $k=>$p){
        if((isset($p["disabled"]))&&($p["disabled"]==1)) $check=0; else $check=1;
        $entry[]=array(
            "id" => $k,
            "mac" => $p["mac"],
            "check" => $check
          );
      }
    }
    //put template
    $ret=$asterisk->putPhoneMac($entry);
    if($ret>0) $alert="<div class=\"AlertGood\">".T_("Phones have been successfully edited")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Phones have NOT been edited")."</div>";
    //go to main page
    goToMainPage($alert,"./");
  }

  //================= PAGE LOGIC ==================
  $html="";
  $count=0;
  $ret=$asterisk->getPhoneType(0);
  if($err=$asterisk->getError()) die("Error: $err");
  if(is_array($ret)){
    foreach($ret as $v){
      $id=$v["id"];
      $phone_name=$v["vendor"]." ".$v["model"];
      $mac=$v["mac"];
      if(trim($v["check"])!='1') $checked="checked=\"checked\""; else $checked=""; 
      //html
      if(trim($html)!=''){
        if((++$count)%2==0) $html.="</tr><tr><td colspan=\"2\">&nbsp;</td></tr><tr>\n";
      }
      $html.="<td valign=\"top\">\n";
      $html.="  <h3 style=\"margin:0;\">".$phone_name."</h3>\n";
      $html.="  <textarea name=\"res[$id][mac]\">".$mac."</textarea>";
      $html.="  <table cellpadding=\"0\" cellspacing=\"0\"><tr>";
      $html.="    <td width=\"1%\" style=\"padding:0;\"><input type=\"checkbox\" name=\"res[$id][disabled]\" value=\"1\" ".$checked."/></td>";
      $html.="    <td style=\"padding:0;\">".T_('Disable MAC address check')."</td>";
      $html.="  </tr></table>";
      $html.="</td>";
    }
  }
  $html.="</tr><tr><td colspan=\"2\">&nbsp;</td></tr>";
  //================ MAIN CONTENTS ================
  ob_start();
  $xajax->printJavascript("../../../../admin/lib/xajax/");
  ?>
    <?$nav="phones";include("../navigation.php");?>
    <div class="SectionHeader">
      <? echo $alert; unset($_SESSION['alert']); ?>
      <h1><?=T_("Phones")?></h1>
    </div>
    <div id="alert"></div>
    <div id="basic_form">
      <p style="text-align:justify"><?=T_('When you assign a template to a provisining client, VoiceOne check the first eight characters of the given MAC address in order to ensure that the phone you want to configure match the phone\'s model selected in the template. You can add newer mac addresses in this section to provide the configuration to the last-built phones or disable the check for a phone model.')?></p>
      <!-- Main Form -->
      <form id="mainform" action="<?=$PHP_SELF?>" method="post">
        <table width="100%" cellpadding="0" cellspacing="0">
          <tr>
            <?=$html?>
          </tr>
        </table>
        <!-- Buttons -->
        <div class="action">
          <a href="#"><img src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" onclick=" document.getElementById('alert').innerHTML=''; xajax_processFormData(xajax.getFormValues('mainform')); "/></a>
        </div>
      </form>
    </div> 
  <?
  $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 MAC address base strings (first eight characters) that VoiceOne uses to check the phone vendor and model. To update the lists insert the firsts eight characters of the MAC address of your Snom phone (the string under the bar code) and press the save button.")?></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');
?>
