<?
/**
 * VoiceOne Settings/User/edit
 *
 * Frontend to provide access to the tech system in voiceone.
 * @package voiceone
 * @subpackage  settings_user
 */
 
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
 	sajax_init();
  
  //================ PAGE LOGIC ================
  if (count($_POST)>0) {

    $data=$_POST["user"];
    $entry=Array(
            "username" => $data["username"],
            "password" => $data["password"],
            "lang" => $data["language"],
            "type" => $data["type"],
            "entity_id" => $data["entity_id"]
          );

    //modify mode or insert mode??
    if ($data["id"]!="") $entry["id"]=$data["id"];
  
    //echo "<pre>"; print_r($entry); echo "</pre>";die;
    $ret=$asterisk->putUser($entry);
    if ($ret>0) $alert="<div class=\"AlertGood\">".$cwlang["user"]["alertModGood"]."</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["user"]["alertModBad"]."</div>";
    goToMainPage($alert);
  }
  
  //get the client informations
  if (isset($_GET["id"]) and $_GET["id"]!="") {
    $id=$_GET["id"];
    $ret=$asterisk->getUser($id);
    if ($err=$asterisk->getError()) die("Error: $err");
  
    $v=$ret[0];
    $id=$v["id"];
    $username=$v["username"];
    $password=$v["password"];
    $lang=$v["lang"];
    $type=$v["type"];
    $entity_id=$v["entity_id"];

    //create the language list
    $availLang=getAvailLang();
    foreach($availLang as $l) {
      if ($l==$lang) $s="selected=\"selected\""; else $s="";
      $select.="<option value=\"$l\" $s>$l</option>\n";
    }

    //check if it is locked
    if ($entity_id>0) $disabled=" disabled=\"disabled\" "; else $disabled="";

    $title=$cwlang["user"]["modUser"]." ".$username;
  } else $title=$cwlang["user"]["newUser"];
  //echo "<pre>";print_r($cwUserInfo);echo "</pre>";
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
  <?=$alert?>
    <h1><?=$title?></h1>
    <h3>
      <a href="../" class="admin" ><?=$cwlang["user"]["return"]?></a> 
    </h3>
  </div>

  <div id="basic_form">
    <form action="<?=$PHP_SELF?><?if ($_GET["id"]!="") echo "?id=".$_GET["id"]; ?>" method="post">


      <div id="client_form" >
        <h3><?=$cwlang["user"]["title"]?></h3>
        <p class="blockintro"><?=$cwlang["user"]["desc"]?></p>
        <div class="block">
          <dl>
            <dt class="required"><label for="user[username]"><?=$cwlang["user"]["username"]?>:</label></dt>
            <dd><input type="text" size="25" maxlength="30" id="user[username]" name="user[username]" value="<?=$username?>" <?=$disabled?>/></dd>
          </dl>
          <dl>
            <dt class="required"><label for="user[password]"><?=$cwlang["user"]["password"]?>:</label></dt>
            <dd><input type="password" size="25" maxlength="30" id="user[password]" name="user[password]" value="<?=$password?>" <?=$disabled?>/></dd>
          </dl>
          <dl>
            <dt class="required"><label for="user[language]"><?=$cwlang["user"][""]?>Language:</label></dt>
            <dd><select id="user[language]" name="user[language]" <?=$disabled?> ><?=$select?></select></dd>
          </dl>
        </div>


        <h3><?=$cwlang["user"]["typeTitle"]?></h3>
        <p class="blockintro"><?=$cwlang["user"]["typeDesc"]?></p>
          <dl>
            <dt class="required"><label for="user[type]"><?=$cwlang["user"]["type"]?>:</label></dt>
            <dd>
              <img src="<?=$relBasePath?>/public/img/user_standard.gif" alt="Standard User" />
              <input type="radio" size="25" maxlength="30" id="user[type1]" name="user[type]" value="<?=USER_STANDARD?>" <?if ($type==USER_STANDARD) echo "checked=\"checked\"";?>/>
              <?=$cwlang["user"]["standard"]?>
              <img src="<?=$relBasePath?>/public/img/user_configurator.gif" alt="Configurator User" />
              <input type="radio" size="25" maxlength="30" id="user[type2]" name="user[type]" value="<?=USER_CONFIGURATOR?>" <?if ($type==USER_CONFIGURATOR) echo "checked=\"checked\"";?>/>
              <?=$cwlang["user"]["configurator"]?>
<?  if ($cwUserInfo["type"]==USER_SUPERUSER ) { ?>              
              <img src="<?=$relBasePath?>/public/img/user_superuser.gif" alt="Superuser" />
              <input type="radio" size="25" maxlength="30" id="user[type3]" name="user[type]" value="<?=USER_SUPERUSER?>" <?if ($type==USER_SUPERUSER) echo "checked=\"checked\"";?>/>
              <?=$cwlang["user"]["superuser"]?>
<?  } ?>
            </dd>
          </dl>

      </div>      


    <div class="action">
      <?if ($_GET["id"]!="") {?> <input type="hidden" name="user[id]" value="<?=$id?>" /><?}?>
      <input type="hidden" name="user[entity_id]" value="<?=$entity_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["user"]["sidebarTitle"]?></h1>
          <?=$cwlang["user"]["sidebarDesc"]?>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

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

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

