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

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

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

    //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>";
    $ret=$asterisk->putOffice($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->getOffice($id);
    if ($err=$asterisk->getError()) die("Error: $err");
    $v=$ret[0];
    $id=$v["id"];
    $name=$v["name"];
    $speeddial=$v["speeddial"];
    $rule=$v["rule"];
    $username=$v["username"];  
    $password=$v["password"];  
    $inkey=$v["inkey"];  
    $host=$v["host"];  
    $port=$v["port"];   
    $register=$v["register"];   
    $did=$v["did"];   
    $optionsArr=$v["options"];   
    $groups=$v["groups"];

    //create the phone number list
    if (is_array($v["numbers"])) {
      foreach($v["numbers"] as $n) $numbers.="<option value='$n'>$n</option>";
    }  
    
    $title=$cwlang["dept"]["modDeptTitle"];
  } else $title=$cwlang["dept"]["newDeptTitle"];
  
  
  //================ 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'))">

      <h2><?=$cwlang["provider"][""]?>Office informations</h2>
      <p class="blockintro"><?=$cwlang["provider"]["sipDesc"]?></p>
      <div class="block">
        <dl>
          <dt class="required"><label for="off[name]"><?=$cwlang["provider"]["sipName"]?>:</label></dt>
          <dd><input type="text" size="25" maxlength="30" id="off[name]" name="off[name]" value="<?=$name?>" /></dd>
          <?=$cwlang["provider"]["sipNameHelp"]?> 
          <dt class="required"><label for="off[speeddial]"><?=$cwlang["provider"][""]?>Speed Dial:</label></dt>
          <dd><input type="text" size="25" maxlength="50" id="off[speeddial]" name="off[speeddial]" value="<?=$speeddial?>" /><big>*</big></dd>
          <?=$cwlang["provider"]["sipCodeHelp"]?> 
<!--
          <dt class="required"><label for="off[technology]"><?=$cwlang["provider"][""]?>Tecnology:</label></dt>
          <dd><select id="off[technology]" name="off[technology]"><?=$optVOIP?></select></dd>
-->            
        </dl>
      </div>

      <p class="blockintro"><?=$cwlang["provider"]["authTitle"]?></p>
      <div class="block">
        <dl>
          <dt class="required"><label for="off[username]"><?=$cwlang["provider"]["username"]?>:</label></dt>
          <dd><input type="text" size="25" maxlength="100" id="off[username]" name="off[username]" value="<?=$username?>" /></dd>
          <dt class="required"><label for="off[host]"><?=$cwlang["provider"]["ipAddress"]?>:</label></dt>
          <dd>
            <input type="text" size="25" maxlength="20" id="off[host]" name="off[host]" value="<?=$host?>" />
            <span class="help">Insert the IP address or the hostname</span>
          </dd>
          <dt class="required"><label for="off[port]"><?=$cwlang["provider"]["port"]?>:</label></dt>
          <dd><input type="text" size="25" maxlength="20" id="off[port]" name="off[port]" value="<?=$port?>" /></dd>

          <dt class="required"><label for="off[password]"><?=$cwlang["provider"]["password"]?>:</label></dt>
          <dd>
            <input type="password" size="25" maxlength="20" id="off[password]" name="off[password]" value="<?=$password?>" />
            <span class="help">Insert the password or the rsa key</span>
          </dd>

          <dt class="required"><label for="off[inkey]"><?=$cwlang["dept"]["rsaKey"]?>:</label></dt>
          <dd><textarea cols="65" style="font-size:xx-small" id="off[inkey]" name="off[inkey]"><?=$inkey?></textarea></dd>

          <dt class="required"><label for="off[register]"><?=$cwlang["dept"][""]?>Register?:</label></dt>
          <dd>
            <input type="checkbox" name="off[register]" id="off[register]" value="1" <?if ($register=="1") echo "checked=\"checked\"";?>/>
            <span class="help">If your server has a dynamic IP, you need to send your registration </span>
          </dd>

          <dt class="required"><label for="off[register]"><?=$cwlang["dept"][""]?>DID?:</label></dt>
          <dd>
            <?=showDid($did);?>
            <span class="help"> | This radio button allows the office to call your extensions directly</span>
          </dd>

<!--
          <dt class="required"><label for="off[mask]"><?=$cwlang["provider"]["nat"]?>:</label></dt>
          <dd>
            <input type="radio" id="off[nat]" name="off[nat]" value="yes" <?if ($v["peer"]["nat"]=='yes') echo "checked='checked'"; ?>/> <?=$cwlang["provider"]["Yes"]?>,
            <input type="radio" id="off[nat]" name="off[nat]" value="no" <?if ($v["peer"]["nat"]=='no') echo "checked='checked'"; ?>/> <?=$cwlang["provider"]["No"]?>
          </dd>
-->
        </dl>
      </div>


    	<h2><?=$cwlang["dept"]["phoneNumberTitle"]?></h2>
      <p class="blockintro"><?=$cwlang["dept"]["phoneNumberDesc"]?></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">
            </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, IAX2, $optionsArr,false,false,RULE_INCOMING);?>      
<?    echo listGroup(GROUP_OF_OFFICE, $js, $groups);?>      


      <div class="action">
        <input type="hidden" name="off[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["dept"]["sidebarEditTitle"]?></h1>
          <?=$cwlang["dept"]["sidebarEditText"]?>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

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

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