<?
/**
 * VoiceOne Settings/Extensions/edit
 *
 * Frontend to provide access to the extensions system in voiceone.
 * @package voiceone
 * @subpackage  settings_extensions
 * @todo Remove hardcoded italian message for confirmation box (AlertGood. AlertBad etc)
 */
 
  include ("../../../admin/config/cfg.php");
  global $cwlang; 
 	sajax_init();
  //$sajax_debug_mode=true;
  
  //================ PAGE LOGIC ================
  if (count($_POST)>0) {
    $data=$_POST["client"];
    $entry=Array(
            "id" => $data["id"],
            "name" => $data["name"],
            "surname" => $data["surname"],
            "extension" => $data["extension"],
            "alias" => $data["alias"],
//            "username" => $data["extension"],
            "password" => $data["password"],
            "language" => $data["language"],
            "groups" => $_POST["group"], 
//            "rule" => $_POST["rule"], 
            "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;
    
    //voicemail
    if ($data["voicemailOn"]=="1") {
      $mailbox["mailbox"]=$data["extension"];
      $mailbox["password"]=$data["vpassword"];
      $mailbox["fullname"]=$data["name"]." ".$data["surname"];
      $mailbox["email"]=$data["email"];
      $mailbox["attach"]=$data["attach"];
      $mailbox["saycid"]=$data["saycid"];
      $mailbox["hidefromdir"]=$data["hidefromdir"];
      //$ret=$asterisk->putVoiceMail($mailbox);
      $entry["voicemail"]=$mailbox;
    } 
    
    //echo "<pre>"; print_r($entry); echo "</pre>";die;
    $ret=$asterisk->putClient($entry);
    if ($ret>0) {
      $alert="<div class=\"AlertGood\">L'interno è stato correttamente modificato </div>";
      $_GET["id"]=$ret;
    }
    else $alert="<div class=\"AlertBad\">L'interno NON è stato modificato</div>";
    
    goToMainPage($alert);
  }
  
  //get the client informations
  if (isset($_GET["id"]) and $_GET["id"]!="") {
    $id=$_GET["id"];
    $ret=$asterisk->getClient($id,0);
    if ($err=$asterisk->getError()) die("Error: $err");
  
    //extract some variables
    $v=$ret[0];
    $id=$v["id"];
    $name=$v["name"];
    $surname=$v["surname"];
    $extension=$v["extension"];
    $alias=$v["alias"];
    $username=$v["username"];
    $password=$v["password"];
    $language=$v["language"];
    $rule=$v["rule"];
    $technology=$v["technology"];
    $optionsArr=$v["options"];
    $groupsArr=$v["groups"];
    $voicemailArr=$v["voicemail"]; 

    //tech icon
    if ($technology==SIP) $prot="<img src=\"$relBasePath/public/img/sip.gif\" alt=\"sip\" />";
    if ($technology==IAX2) $prot="<img src=\"$relBasePath/public/img/iax.gif\" alt=\"iax\" />";
  
    //read the mailbox
    if (count($voicemailArr)>0) {
      $voicemailOn="1"; 
      $vpassword=$voicemailArr["password"];
      $email=$voicemailArr["email"];
      $attach=$voicemailArr["attach"];
      $saycid=$voicemailArr["saycid"];
      $hidefromdir=$voicemailArr["hidefromdir"];
    } else $voicemailOn="0";
    
    $title="Modify extension $extension &lt;$name $surname&gt;";
  } else {
    $title="New Extension";
    unset($v);
  }

  //create the language list
  $availLang=getAvailLang();
  foreach($availLang as $l) {
    if ($l==$language or (!isset($language) and $l==$cwLanguage)) $s="selected=\"selected\""; else $s="";
    $langSelect.="<option value=\"$l\" $s>$l</option>\n";
  }

  //prepare the sidebar
  $retBuddy=$asterisk->getClient(0,0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($retBuddy))
  foreach ($retBuddy as $vBuddy) {
    $buddyid=$vBuddy["id"];
    $buddyExt=$vBuddy["extension"];
    $buddyName=$vBuddy["name"];
    $buddySurname=$vBuddy["surname"];
    $buddiesList.="<tr><td class=\"postit\">$buddyExt</td><td><a href=\"./?id=$buddyid\">$buddyName $buddySurname</a></td></tr>\n";
  }  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
  <?=$alert?>
    <h1><?=$title?></h1>
    <h3><a href="../" class="admin" ><?=$cwlang["exten"]["return"]?></a></h3>
  </div>

  <div id="basic_form">
    <form action="<?=$PHP_SELF?><?if ($_GET["id"]!="") echo "?id=".$_GET["id"]; ?>" method="post">
      <table>
        <tr>
          <td>
            <p class="blockintro"><?=$cwlang["exten"]["personalinfo"]?></p>
            <div class="block">
              <dl>
                <dt class="required"><label for="client[name]"><?=$cwlang["exten"]["name"]?>:</label></dt>
                <dd><input type="text" size="25" maxlength="30" id="client[name]" name="client[name]" value="<?=$name?>" /></dd>
                <dt class="required"><label for="client[surname]"><?=$cwlang["exten"]["surname"]?>:</label></dt>
                <dd><input type="text" size="25" maxlength="50" id="client[surname]" name="client[surname]" value="<?=$surname?>" /></dd>
                <dt class="required"><label for="client[language]"><?=$cwlang["exten"][""]?>Language:</label></dt>
                <dd><select id="client[language]" name="client[language]"><?=$langSelect?></select></dd>
                <!--
                <dt class="required"><label for="client[email]"><?=$cwlang["exten"]["email"]?>:</label></dt>
                <dd><input type="text" size="25" maxlength="100" id="client[email]" name="client[email]" value="<?=$v["email"]?>" /></dd>
                -->
              </dl>
            </div>
          </td>
          <td>
            <p class="blockintro"><?=$cwlang["exten"]["authdata"]?></p>
            <div class="block">
              <dl>
                <dt class="required"><label for="client[extension]"><?=$cwlang["exten"]["exten"]?>:</label></dt>
                <dd><input type="text" size="25" maxlength="20" id="client[extension]" name="client[extension]" value="<?=$extension?>" /></dd>
                <dt class="required"><label for="client[password]"><?=$cwlang["exten"]["password"]?>:</label></dt>
                <dd><input type="password" size="25" maxlength="20" id="client[password]" name="client[password]" value="<?=$password?>" /></dd>
                <dt class="required"><label for="client[alias]"><?=$cwlang["exten"]["alias"]?>:</label></dt>
                <dd><input type="text" size="25" maxlength="100" id="client[alias]" name="client[alias]" value="<?=$alias?>" /></dd>
              </dl>
            </div> 
          </td>
        </tr>
      </table>

<?    echo listOption($js, $technology, $optionsArr, false, true, RULE_OUTGOING, true);?>      
      
      
    	<h2><?=$cwlang["exten"][""]?>VoiceMail</h2>
      <p class="blockintro"><?=$cwlang["exten"][""]?>Insert here the voicemail informations</p>
      <div class="block">
        <dl>
          <dt class="required"><label for="client[voicemailOn]"><?=$cwlang["exten"][""]?>Active:</label></dt>
          <dd>
            <input type="radio" id="client[voicemailOn]" name="client[voicemailOn]" value="1" <?if ($voicemailOn==1) echo "checked=\"checked\""; ?> /> ON
            <input type="radio" id="client[voicemailOn]" name="client[voicemailOn]" value="0" <?if ($voicemailOn!=1) echo "checked=\"checked\""; ?> /> OFF
          </dd>
 
          <dt class="required"><label for="client[email]"><?=$cwlang["exten"]["email"]?>:</label></dt>
          <dd><input type="text" size="25" maxlength="100" id="client[email]" name="client[email]" value="<?=$email?>" />
          the email where it will be sent the voicemail</dd>
          <dt class="required"><label for="client[vpassword]"><?=$cwlang["exten"]["password"]?>:</label></dt>
          <dd><input type="password" size="25" maxlength="20" id="client[vpassword]" name="client[vpassword]" value="<?=$vpassword?>" />
          set the password to read the voicemail</dd>
          <dt class="required"><label for="client[attach]"><?=$cwlang["exten"][""]?>Attach:</label></dt>
          <dd><input type="checkbox" id="client[attach]" name="client[attach]" value="1" <?if ($attach==1) echo "checked=\"checked\""; ?> />
          if on, the message will be sent as attach in your email </dd>
          <dt class="required"><label for="client[saycid]"><?=$cwlang["exten"][""]?>Say CID:</label></dt>
          <dd><input type="checkbox" id="client[saycid]" name="client[saycid]" value="1" <?if ($saycid==1) echo "checked=\"checked\""; ?> />
          if on, will be included the Caller ID </dd>
          <dt class="required"><label for="client[hidefromdir]"><?=$cwlang["exten"][""]?>Hide:</label></dt>
          <dd><input type="checkbox" id="client[hidefromdir]" name="client[hidefromdir]" value="1" <?if ($hidefromdir==1) echo "checked=\"checked\""; ?> />
          if on, you will be hidden from directory </dd>
        </dl>
      </div>
        

<?    echo listGroup(GROUP_OF_EXTENSION, $js, $groupsArr);?>      
<?    //echo listRight($js, $rights);?>      
<?    //echo listRule($js, $rule);?>      

    
  
    <div class="action">
      <?if ($_GET["id"]!="") {?> <input type="hidden" name="client[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["exten"]["otherExten"]?></h1>
          <p><?=$cwlang["exten"]["otherExtenDesc"]?></p>
          <table class="small">
            <?=$buddiesList?>
          </table>
        </div>
        <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
        <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

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

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

