<?php
  
$webdoc["page_title"].=" - Add user";

/** zadani, pro jakeho uzivatele je tento soubor k dispozici
 *  soubor je k dispozici take vsem uzivatelum, kteri maji vyssi person_role
 * DA - 1
 * DE - 2
 * TA - 3
 * TE - 4
 * TT - 5    
*/ 
$person_role = 1;
if (!auth($person_role)) 
{
  $webdoc["message"][]=database_get_message("no_permission");
  $template->template_assign("no_display",true);
  return;
}


/** ------------------------------------------------------
 *  ZACATEK vlastniho PHP kodu
    ------------------------------------------------------ */ 

if (!isset($_POST["button"]))
{
  $person_id=$_REQUEST["person_id"];
  $role_id=$_REQUEST["role_id"];
  $record = database_get_auth_select($person_id,1,2); // nacteni dat z databaze, hledani dle person_id
  $template->template_assign("record",$record); // zobrazeni hodnot ve formulari
  $result = database_get_person_role(); // nacteni dat z databaze person_role
  while ($record_person_role = database_fetch_array($result)) // vytvoreni podkladu pro funkci na vytvoreni formulare type radio
  {
  $values[]=$record_person_role["role_id"];
  $names[]=$record_person_role["name"];
  } 
  $template->template_assign("values",$values);
  $template->template_assign("names",$names);
  $template->template_assign("id",$role_id);
   
}

if (isset($_POST["button"])) // prevedeni zadaneho jmena a hesla z formulare 
{
  $record["person_id"]=$_POST["person_id"];
  $record["login"]=$_POST["login"];
  $record["pass"]=$_POST["pass"];
  $record["pass_again"]=$_POST["pass_again"];
  $record["role_id"]=$_POST["role_id"];
  $record["name"]=$_POST["name"];
  $record["surname"]=$_POST["surname"];
  $record["email"]=$_POST["email"];
  $record["register_id"]=$_SESSION["$webdoc[main_table]_auth"];
  $record["register_time"]= Date("Y-m-d H:i:s");
  $record["active"]=$_POST["active"];
  
  $result = database_get_person_role(); // nacteni dat z databaze person_role
  while ($record_person_role = database_fetch_array($result)) // vytvoreni podkladu pro funkci na vytvoreni formulare type radio
  {
  $values[]=$record_person_role["role_id"];
  $names[]=$record_person_role["name"];
  } 
  $template->template_assign("values",$values);
  $template->template_assign("names",$names);
  $template->template_assign("id",$record["role_id"]);
  
  $template->template_assign("record",$record); //zobrazeni hodnot ve formulari
  
  if ($record["person_id"]) // jestlize je z formulare zadano person_id, jedna se o editaci
  {
    $edit=true;
    $record["edit"]=1;
  }
   
  // zacatek --- testy, zda jsou vyplnena vsechna pole
  $filled_ok=true;
  if ($record["login"]=="")
  {
    $webdoc["message"]["form_error"][]=database_get_message("filled_login");
    $filled_ok=false;
  }
  if ($record["pass"]=="" && !$edit)
  {
    $webdoc["message"]["form_error"][]=database_get_message("filled_pass");
    $filled_ok=false;
  }
  if ($record["pass_again"]=="" && !$edit)
  {
    $webdoc["message"]["form_error"][]=database_get_message("filled_pass_again");
    $filled_ok=false;
  }
  if ($record["role_id"]=="")
  {
    $webdoc["message"]["form_error"][]=database_get_message("filled_role");
    $filled_ok=false;
  }
  if ($record["name"]=="")
  {
    $webdoc["message"]["form_error"][]=database_get_message("filled_name");
    $filled_ok=false;
  }
  if ($record["surname"]=="")
  {
    $webdoc["message"]["form_error"][]=database_get_message("filled_surname");
    $filled_ok=false;
  }
  if ($record["email"]=="")
  {
    $webdoc["message"]["form_error"][]=database_get_message("filled_email");
    $filled_ok=false;
  }
  // konec --- testy, zda jsou vyplnena vsechna pole
  
  if ($record["pass"]=="" && $record["pass_again"]=="" && $edit)
  {
    $pass_no_change=true;
  }  
  
  if ($record["pass"]<>$record["pass_again"]) // test, zda se shoduji zadana hesla
  {  
    $pass_ok=false;
    $webdoc["message"]["form_error"][]=database_get_message("pass_not_match");
    
  }
  else
  { 
    $pass_ok=true;  
  }
  
  
  if ($edit) // test, zda se nepokousi zadat login, jaky je jiz jednou v databazi Person; pokud se jedna o editaci, testuje, zda se shoduje person_id s login
  { 
    $stejny_login = database_get_auth_select($record["login"],$person_id,3);
    if ($stejny_login)
    {
      $login_ok=true;
    }
    else
    {
      if (database_get_auth_select($record["login"],1,1)) // test, zda se nepokousi zadat login, jaky je jiz jednou v databazi Person
      {
        $login_ok=true;
      }
      else
      { 
        $login_ok=false;
        $webdoc["message"]["form_error"][]=database_get_message("exists_login");
      }
    }  
  }
  
  else
  {
  if (database_get_auth_select($record["login"],1,1)) 
  {
    $login_ok=true;
  }
  else
  { 
    $login_ok=false;
    $webdoc["message"]["form_error"][]=database_get_message("exists_login");
  }
  }
  
  if (is_right_email($record["email"])) // test, zda je zadan spravny format emailu
  {  
    $email_ok=true;
  }
  else
  { 
    $email_ok=false;
    $webdoc["message"]["form_error"][]=database_get_message("email_correct");
  }
  
  if ($filled_ok && $pass_ok && $login_ok && $email_ok && !$edit) // pokud jsou vsechny podminky splneny, prida noveho uzivatele do Person
  {
    database_save_record_auth($record,1);  
  }
  
  if ($filled_ok && $pass_ok && $login_ok && $email_ok && $edit && $pass_no_change) // pokud jsou vsechny podminky splneny, edituje uzivatele do Person (nebylo meneno heslo)
  {
    database_save_record_auth($record,2);  
  }
  
  if ($filled_ok && $pass_ok && $login_ok && $email_ok && $edit && !$pass_no_change) // pokud jsou vsechny podminky splneny, edituje uzivatele do Person (nebylo meneno heslo)
  {
    database_save_record_auth($record,3);  
  }

}

/** ------------------------------------------------------
 *  KONEC vlastniho PHP kodu
    ------------------------------------------------------ */ 

  
?>
