<?
/**
 * VoiceOne Settings/Provider/edit
 *
 * Frontend to provide access to the provider system in voiceone.
 * @package voiceone
 * @subpackage  settings_provider
 */
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
 	sajax_init();

  //================ PAGE LOGIC ================
  if (count($_POST)>0) {
    $data=$_POST["res"];

    $entry=Array(
            "id"        => $data["id"],
            "name"      => $data["name"],
            "speeddial" => $data["speeddial"],
            "username"  => $data["username"], 
            "password"  => $data["password"], 
            "host"      => $data["host"], 
            "port"      => $data["port"], 
            "did"       => $_POST["did"],
            "technology"=> $_POST["technology"] 
        );

    //add options to the entry
    foreach ($_POST as $k=>$v) {
      if (substr($k,0,7)=="status_") {
        $k=substr($k,7);
        if ($v=="1") {
          $o["key"]=$k; 
          $o["value"]=$_POST["option_".$k];
          $options[]=$o;
        }
      }
    }
    $entry["options"]=$options;
    
    //add the phonenumbers
    if (is_array($_POST["phonenumber"])) 
    foreach($_POST["phonenumber"] as $pn) $entry["numbers"][]=$pn;

    //echo "<pre>"; print_r($entry);echo "</pre>";die;
    $ret=$asterisk->putProvider($entry);

    if ($err=$asterisk->getError()) $alert="<div class=\"AlertBad\">ERROR: $err</div>";
    if ($ret>0) {
      $alert.="<div class=\"AlertGood\">".$cwlang["provider"]["alertDelGood"]."</div>";
      $_GET["id"]=$ret;
    } else $alert.="<div class=\"AlertBad\">".$cwlang["provider"]["alertDelBad"]."</div>";

    goToMainPage($alert);
  }
  
  //get the client informations
  if (isset($_GET["id"]) and $_GET["id"]!="") {
    $id=$_GET["id"];
    $ret=$asterisk->getProvider($id,0);
    if ($err=$asterisk->getError()) die("Error: $err");
    $v=$ret[0];
    $id=$v["id"];
    $name=$v["name"];
    $speeddial=$v["speeddial"];
    $technology=$v["technology"];
    $rule=$v["rule"];
    $username=$v["username"];  
    $password=$v["password"];  
    $host=$v["host"];  
    $port=$v["port"];   
    $did=$v["did"];
    $optionsArr=$v["options"];   

    //create the phone number list
    if (is_array($v["numbers"])) {
      foreach($v["numbers"] as $n) $numbers.="<option value='$n'>$n</option>";
    }  
    
    if ($technology==0) $_GET["type"]="telco"; else $_GET["type"]="voip";

    $title=$cwlang["provider"]["modTitle"].$name;
  } else $title=$cwlang["provider"]["newTitle"]." (".$_GET["type"].")";
  

  $ret=$asterisk->getTechnology(0);
  foreach($ret as $t) {
    $vid=$t["id"];
    $vname=$t["name"];
    if ($t["voip"]==1) $optVOIP.="<option value=\"$vid\">$vname</option>\n";
  }
  
  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
  <?=$alert?>
    <h1><?=$title?></h1>
    <h3>
      <a href="../" class="admin" ><?=$cwlang["provider"]["return"]?></a> 
    </h3>
  </div>

  <div id="basic_form">
    <form action="<?=$PHP_SELF?>?id=<?=$_GET["id"]?>" method="post"  onSubmit="return selectAll(getElementById('phonenumber'))">

      
<? if ($_GET["type"]=="voip") { ?>

      <h2><?=$cwlang["provider"][""]?>Provider</h2>
      <p class="blockintro"><?=$cwlang["provider"]["sipDesc"]?></p>
      <div class="block">
        <dl>
          <dt class="required"><label for="res[name]"><?=$cwlang["provider"]["sipName"]?>:</label></dt>
          <dd><input type="text" size="25" maxlength="30" id="res[name]" name="res[name]" value="<?=$name?>" /></dd>
          <?=$cwlang["provider"]["sipNameHelp"]?> 
          <dt class="required"><label for="res[speeddial]"><?=$cwlang["provider"][""]?>Speed Dial:</label></dt>
          <dd><input type="text" size="25" maxlength="50" id="res[speeddial]" name="res[speeddial]" value="<?=$speeddial?>" /><big>*</big></dd>
          <?=$cwlang["provider"]["sipCodeHelp"]?> 
        </dl>
      </div>
      
      <p class="blockintro"><?=$cwlang["provider"]["authTitle"]?></p>
      <div class="block">
        <dl>
          <dt class="required"><label for="res[username]"><?=$cwlang["provider"]["username"]?>:</label></dt>
          <dd><input type="text" size="25" maxlength="100" id="res[username]" name="res[username]" value="<?=$username?>" /></dd>
          <dt class="required"><label for="res[password]"><?=$cwlang["provider"]["password"]?>:</label></dt>
          <dd><input type="password" size="25" maxlength="20" id="res[password]" name="res[password]" value="<?=$password?>" /></dd>
          <dt class="required"><label for="res[host]"><?=$cwlang["provider"]["ipAddress"]?>:</label></dt>
          <dd>
            <input type="text" size="25" maxlength="20" id="res[host]" name="res[host]" value="<?=$host?>" />
            <span class="help">Insert the IP address or the hostname</span>
          </dd>
          <dt class="required"><label for="res[port]"><?=$cwlang["provider"]["port"]?>:</label></dt>
          <dd><input type="text" size="25" maxlength="20" id="res[port]" name="res[port]" value="<?=$port?>" /></dd>
        </dl>
      </div>

    	<h2><?=$cwlang["dept"]["phoneNumberTitle"]?></h2>
      <p class="blockintro"><?=$cwlang["dept"][""]?>Provider phone number</p>
      <div class="block">
        <table>
          <tr>
            <td class="required"><label for="number"><?=$cwlang["dept"]["phoneNumber"]?>:</label></td>
            <td class="required"><?=$cwlang["dept"]["phoneNumberAdded"]?></td>
          </tr>
          <tr>
            <td valign="top">
              <input type="text" size="25" maxlength="100" id="number" name="number" value="" />
              <input type="button" value="+" onclick="addOption(getElementById('phonenumber'),document.getElementById('number').value)" title="Add Length">
              <div class="help">Digit the numbers to listen (i.e. <strong>123456</strong>)<br/>or a range of numbers as (<strong>123456-123459</strong>)</div>
              <div style="margin: 0.5em 0"><strong>DID:</strong> <?=showDid($did);?></div>
            </td>
            <td>
              <select name="phonenumber[]" size="4" onchange="saved = false" ondblclick="editOptions(this)" multiple style="float: left" id="phonenumber">
                <?=$numbers?>
              </select>
              <input type="button" value="edit" onclick="editOptions(getElementById('phonenumber'))" title="Edit Length(s)">
              <input type="button" value="X" onclick="deleteOptions(getElementById('phonenumber'))" title="Delete Length(s)">
            </td>
          </tr>
        </table>
      </div>
      
      
      
<?    echo listOption($js, $technology, $optionsArr,false,true,RULE_INCOMING,false,true);?>      

<?  } else { ?>

      <h2><?=$cwlang["provider"][""]?>Provider</h2>
      <p class="blockintro"><?=$cwlang["provider"]["sipDesc"]?></p>
      <div class="block">
        <dl>
          <dt class="required"><label for="res[name]"><?=$cwlang["provider"]["sipName"]?>:</label></dt>
          <dd><input type="text" size="25" maxlength="30" id="res[name]" name="res[name]" value="<?=$name?>" /></dd>
          <?=$cwlang["provider"]["sipNameHelp"]?> 
          <dt class="required"><label for="res[speeddial]"><?=$cwlang["provider"][""]?>Prefix:</label></dt>
          <dd><input type="text" size="25" maxlength="50" id="res[speeddial]" name="res[speeddial]" value="<?=$speeddial?>" /><big>*</big></dd>
          <?=$cwlang["provider"][""]?> Insert the telco prefix, such as <strong>1033</strong> or <strong>1099</strong> 
        </dl>
      </div>

<?  } ?>


      <div class="action">
        <input type="hidden" name="res[id]" value="<?=$id?>" />
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Cancel" onclick="location.href='../'; return false;"/>
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" />
      </div>
    </form>
  </div>  
  
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <h1><?=$cwlang["provider"]["editSidebarTitle"]?></h1>
          <?=$cwlang["provider"]["editSidebarDesc"]?>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================
 	sajax_handle_client_request();

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

