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

  //================ PAGE LOGIC ================
  if (count($_POST)>0) {
    $entry=Array(
      "name"      => $_POST["name"],
      "emergency" => $_POST["emergency"],
      "number"    => Array(),
      "groups"    => $_POST["group"] 
    );

    if (is_array($_POST["phonenumber"])) { 
      foreach($_POST["phonenumber"] as $pn) $entry["number"][]=$pn;
    }

    //insert new entry or modify only??
    if (isset($_POST["id"])) $entry["id"]=$_POST["id"];

    $ret=$asterisk->putPhoneNumber($entry);
    if ($ret>0) {
      $alert="<div class=\"AlertGood\">".$cwlang["numb"]["alertModGood"]."</div>";
      $_GET["id"]=$ret;
    } else $alert="<div class=\"AlertBad\">".$cwlang["numb"]["alertModBad"]."</div>";

    goToMainPage($alert);
  }
  
  if (isset($_GET["id"])) {
    //get the phonenumber informations
    $id=$_GET["id"];
    $ret=$asterisk->getPhoneNumber($id);
    if ($err=$asterisk->getError()) die("Error: $err");
  
    $v=$ret[0];
    $id=$v["id"];
    $name=$v["name"];
    $emergency=$v["emergency"];
    $groups=$v["groups"];
    //retrieve the phone numbers
    if (is_array($v["number"])) {
      foreach($v["number"] as $e) $numbers.="<option value='$e'>$e</option>";
    }  

  }
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
  <?=$alert?>
    <h1><?=$name?></h1>
    <h3>
      <a href="../" class="admin" ><?=$cwlang["numb"]["return"]?></a> 
    </h3>
  </div>


  
  
  <div id="basic_form">
    <form action="<?=$PHP_SELF?><?if (isset($_GET["id"])) echo "?id=".$_GET["id"];?>" method="post" onSubmit="return selectAll(getElementById('phonenumber'))">
      
    	<h2><?=$cwlang["numb"]["numbersTitle"]?></h2>
      <p class="blockintro"><?=$cwlang["numb"]["numbersHelp"]?></p>
      <div class="block">
        <table>
          <tr><td class="required" colspan="2"><label for="name"><?=$cwlang["numb"]["name"]?>:</label></td></tr>
          <tr><td colspan="2"><input type="text" size="25" maxlength="100" id="name" name="name" value="<?=$name?>" />
              <?=$cwlang["numb"]["nameHelp"]?>
          </td></tr>

          <tr>
            <td class="required"><label for="number"><?=$cwlang["numb"]["addNumber"]?>:</label></td>
            <td class="required"><?=$cwlang["numb"]["listNumber"]?></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">
            </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>


          <tr><td class="required" colspan="2"><label for="emergency"><?=$cwlang["numb"]["emergency"]?>?</label></td></tr>
          <tr><td colspan="2">
            <?=$cwlang["numb"]["emergencyHelp"]?><br/>
            <input type="radio" id="emergency" name="emergency" value="1" <?if ($emergency==1) echo "checked=\"checked\"";?>/> <?=$cwlang["numb"]["yes"]?>
            <input type="radio" id="emergency" name="emergency" value="0" <?if ($emergency!=1) echo "checked=\"checked\"";?>/> <?=$cwlang["numb"]["no"]?> 
          </td></tr>

        </table>
      </div>

<?    echo listGroup(GROUP_OF_PHONENUMBER, $js, $groups);?>      
  
    <div class="action">
      <?if (isset($_GET["id"])) {?><input type="hidden" name="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["numb"]["editSidebarTitle"]?></h1>
          <?=$cwlang["numb"]["editSidebarDesc"]?>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

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

  $smarty->assign("section", "numbers");
  $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');
?>

