<?php
# User_edit_detail_module ver.1.0
# last modification 11.2.2010
# Formulář pro zobrazení detailu uživatele vč editace základních údajů
#   a vytvaření/editace volacích účtů
# 
# 
require "files/javamd5.inc.php";
  #SIP aktualizace 
  $_SESSION['idUSER']=$_GET['idUSER'] ; 
  
########################################################  

  if (IsSet($_POST['UED_SUBMITED']))
  {
    include "build/db_connect.inc.php"  ;
    $query="START TRANSACTION";
    $result = mysql_query($query);
    if (!$result) {
      die('Transakci nelze zahajit:' . mysql_error());
    }
    $query="UPDATE user SET jmeno=\"".$_POST['UED_FIRSTNAME']."\",prijmeni=\"".$_POST['UED_LASTNAME']."\",
                            email=\"".$_POST['UED_EMAIL']."\",";
    if($_POST['UED_PASSWD']!='')
    {
        $query .="password=\"".$_POST['UED_PASSWD']."\",";
    }
     if(!IsSet($_POST['UED_VALIDITY'])) {
        $query=$query."validity=0";
      }
      else
      {
        $query=$query."validity=".$_POST['UED_VALIDITY']."";
      }                       
    $query=$query." WHERE idUSER=".$_POST['idUSER']."";
    $result = mysql_query($query);
          
    if (!$result) {
      $query="ROLLBACK";
      $result = mysql_query($query);
      die('Aktualizace dat nebyla zapsána:' . mysql_error());
    }
    $query="UPDATE spec_user_group SET idUSERGROUP=".$_POST['UED_GROUPID']."
            WHERE idUSER=".$_POST['idUSER']."";
    $result = mysql_query($query);        
    if (!$result) {
          $query="ROLLBACK";
          $result = mysql_query($query);
          die('Aktualizace skupinz nebyla zapsána:' . mysql_error());
    }
    
      $query="COMMIT";
      $result = mysql_query($query);
      if (!$result) {
        die('Transakci nelze potvrdit:' . mysql_error());
      }
      echo "Změny uloženy. <a href=\"admin_page.php?NEXTFORM=8&idUSER=".$_POST['idUSER']."\">Zpet</a>" ;
  
  
  
  
  
  }
  else{
  
  
  include "build/db_connect.inc.php"  ;
  $query="SELECT idUSER,jmeno,prijmeni,login,email,validity
          FROM user
          WHERE idUSER=\"".$_GET['idUSER']."\"";
  $result = mysql_query($query);
          
  if (!$result) {
      die('Could not query:' . mysql_error());
  }
  list($idUSER,$jmeno,$prijmeni,$login,$email,$validity) = mysql_fetch_row($result)  ;
?>

<script language="JavaScript">

 
 function RefreshForm(theForm){
      document.forms.form.submit();
  
  }

// soubor funkcí pro kontrolu hodnot formuláře na straně klienta
  function HideIt() {
        document.getElementById('required').style.visibility= "hidden";
        document.getElementById('requiredtext').style.visibility= "hidden";
  }
  function CheckPasswd( theField,theField1, theMessage ) {
   if( theField.value!=theField1.value ) {
      theField.focus();
      if(document.getElementById) {
        document.getElementById('required').style.visibility= "visible";
        document.getElementById('requiredtext').style.visibility= "visible";
        document.getElementById('requiredtext').innerHTML= theMessage;
        setTimeout( "HideIt()",1500);
        //document.getElementById('md5hash').value=MD5(theField.value);
      } else {
          alert(theMessage);
      }
      return false;
   } else {
        if (document.getElementById('PASS').value!=''){
        document.getElementById('PASS').value=MD5(document.getElementById('PASS').value);
        }
        
      return true;
   }
  }
  function CheckEmptyField( theField, theMessage ) {
   if( theField.value=="" ) {
      theField.focus();
      if(document.getElementById) {
        document.getElementById('required').style.visibility= "visible";
        document.getElementById('requiredtext').style.visibility= "visible";
        document.getElementById('requiredtext').innerHTML= theMessage;
        setTimeout( "HideIt()",1500);
      } else {
          alert(theMessage);
      }
      return false;
   } else {
      return true;
   }
  }
  function Validate(theForm) {
      if( !CheckEmptyField(theForm.UED_FIRSTNAME,"Vyplňte prosím jméno!") ) return false;
      if( !CheckEmptyField(theForm.UED_LASTNAME,"Zadejte prosím příjmení!") ) return false;
      if( !CheckEmptyField(theForm.UED_LOGIN,"Zadejte login!") ) return false;
      if( !CheckEmptyField(theForm.UED_EMAIL,"Zadejte email!") ) return false;
      if( !CheckPasswd(theForm.UED_PASSWD,theForm.UED_PASSWD1,"hesla se nerovnaji") ) return false;
      
  // uspech
  return true;
}
</script>
<center>
 <div class="forms">
<h3>Editace uživatele</h3><span class="backtoform"><a href="admin_page.php?NEXTFORM=7">Zpět</a> </span>
<fieldset>
  <legend>Detaily uživatele</legend>
<form action="admin_page.php" OnSubmit="return Validate(this);" method="post">
      <table align="center">
        <tr>
           <td class="tablehead">ID</td>
           <td class="tablehead">Jméno</td>
           <td class="tablehead">Příjmení</td>
           <td class="tablehead">Email</td>
           <td class="tablehead">Login</td>
        </tr><tr>
        
          <td class="tableculombhead"><input type="text" size=3 name="idUSER" disabled value="<?php echo($idUSER);?>"></td>
          <td class="tableculombhead"><input type="text" size=8 name="UED_FIRSTNAME" value="<?php echo($jmeno);?>"></td>
          <td class="tableculombhead"> <input type="text" size=8 name="UED_LASTNAME" value="<?php echo($prijmeni);?>"></td>
          <td class="tableculombhead"> <input type="text" name="UED_EMAIL" value="<?php echo($email);?>"></td>
          <td class="tableculombhead"><input type="text" size=8 name="UED_LOGIN" disabled value="<?php echo($login);?>"></td>
        </tr><tr>
          <td class="tablehead">Heslo</td>
          <td class="tableculombhead"><input type="password" size=10 id="PASS"  name="UED_PASSWD"  ></td>
          <td class="tableculombhead"><input type="password" size=10  name="UED_PASSWD1"  ></td>
          <td class="tableculombhead"></td>
          <td class="tableculombhead"></td>
        </tr><tr> 
          <td class="tablehead" colspan=2>Aktivní <input type="checkbox" name="UED_VALIDITY"  value="1"<?php if ($validity==1)  echo "checked";?>></td>
        
        <input type="hidden" name="idUSER" value="<?php echo($idUSER);?>">
        <input type="hidden" name="UED_SUBMITED" value="TRUE">
          <td class="tablehead" colspan=2 align="left">Oprávnění
            <select name="UED_GROUPID">
                <?php
                    include "build/db_connect.inc.php"  ;
                    $query="SELECT idUSERGROUP
                            FROM spec_user_group sUG
                            WHERE idUSER=".$_GET['idUSER']."
                            AND validity=1";
                    $result = mysql_query($query);
                    if (!$result) {
                      die('Nelze ov25it skupinu:' . mysql_error());
                    }
                    list($actualidgroup)=mysql_fetch_row($result) ;
                    $query="SELECT idUSERGROUP,Name
                            FROM user_group UG
                            WHERE validity=1";
                    $result = mysql_query($query);
                    if (!$result) {
                      die('Nelze načíst skupiny:' . mysql_error());
                    }
                    while(list($idusergroup,$name)=mysql_fetch_row($result)){
                        if($idusergroup==$actualidgroup){
                            echo "<option value=".$idusergroup." selected>".$name."</option>";
                        }else{
                            echo "<option value=".$idusergroup.">".$name."</option>";
                        }
                    }
                
                ?>
            </select>
          
          
          </td>
          <td class="tableculombhead" colspan=1 align="right"><input type="submit" value="Ulož" ></td>
        </tr>
      </table>
</form>

</fieldset>

  <h2>Přehled volacích ůčtu</h2>
  <fieldset>
    <legend>Volací účty</legend>
   <div class="userlist">
  <?php
  include "build/db_connect.inc.php"  ;
  $query="SELECT UA.id,T.table_name,T.technology,T.idTECHNOLOGY
          FROM user_account UA
          JOIN  user U ON U.idUSER=UA.idUSER AND U.validity=1
          JOIN technology T ON T.idTECHNOLOGY=UA.idTechnology AND T.validity=1
          WHERE
          U.idUSER=".$_GET['idUSER']."    ";
      $result = mysql_query($query);
      
      if (!$result) {
        die('Could not query:' . mysql_error());
      }
      if (mysql_num_rows($result)!=0)
      {
   
  
      echo  "<form name=\"form\" action=\"admin_page.php\" method=\"get\">";
      
        #Část která vyčítá do roletky formuláře tel.učty
          echo "<table align=\"center\"><tr>";
          echo "<td class=\"tablehead\" style=\"width:20px\"></td>";
          echo "<td class=\"tablehead\" style=\"width:20px\">ID</td>";
          echo "<td class=\"tablehead\" style=\"width:30px\">Technologie</td>";
          echo "<td class=\"tablehead\">Název</td>";
          echo "<td class=\"tablehead\">Kontext</td>";
          $a=1;          
          while (list($id,$table,$technology,$idtechnology ) = mysql_fetch_row($result)){
          #Výběr údajů z potřebné tabulky SIP,H323....
            include "build/db_connect.inc.php"  ;
            $subquery="SELECT name,context
                    FROM ".$table."
                    WHERE id=".$id."" ;
            $subresult = mysql_query($subquery); 
            if (!$subresult) {
              die('Could not query:' . mysql_error());
            }
            list($name,$context ) = mysql_fetch_row($subresult) ;
        #v roletce je předáván id asterisk učtu z tabulky obsažene v UED_TABLE
            #Stridani barev řádků tabulky
          if($a==1){$a=0; $barva="white";}else{$a=1;$barva="lightgrey"; }
           #####################################################################
            echo "<tr bgcolor=$barva>";
            echo("<td style=\"width:20px\"><a href=\"admin_page.php?NEXTFORM=22&idCALLACC=".$id."&idTECHNOLOGY=".$idtechnology."\"><img src=\"files/pictures/vyber.gif\"></a></td><td style=\"width:20px\" >".$id."</td><td style=\"width:20px;text-align:center\">".$technology."</td><td>".$name."</td><td>".$context."</td");
            echo ("</tr>");
        }
        unset($a);
      ?>
      </table>
        </form>
    </div>
    </fieldset>
</div>
</center>
<?php
      
    }
    else{
    echo "Uživatel namá žádné účty.";
    }
# OBSLUHA SIP formuláře
    if ($_GET['UED_TABLE']=="sip_buddies"){
    include "build/db_connect.inc.php"  ;
    $query="SELECT name,callerid,context
            FROM ".$_GET['UED_TABLE']."
            WHERE ID=".$_GET['UED_ACC']."";
    $result = mysql_query($query);     
    if (!$result) {
          die('Could not query:' . mysql_error());
    }
    
    
      list($sip_name,$sip_callerid,$sip_context ) = mysql_fetch_row($result) ;
      echo "<form action=\"admin_page.php\" method=\"get\" >";
      echo "<table align=\"center\" >";
      echo "<tr>";
      echo " <td> Jméno účtu-login: </td><td><input type=\"text\" name=\"UED_SIPNAME\" disabled value=\"".$sip_name."\" ></td>";
      echo "</tr><tr>";
      echo "  <td>Identifikace při volání: </td><td><input type=\"text\" name=\"UED_SIPCALLERID\" value=\"".$sip_callerid."\"></td>";
      echo "</tr><tr>";
      echo "  <td>Kontext: </td><td><input type=\"text\" name=\"UED_SIPCONTEXT\" value=\"".$sip_context."\"></td>";
      echo "</tr><td colspan=2 align=\"center\"> ";
      echo "<input type=\"hidden\" name=\"UED_SIPSUBMITED\" value=\"TRUE\" >";
      echo "<input type=\"hidden\" name=\"UED_TABLE\" value=\"sip_buddies\" >" ;
      echo "<input type=\"hidden\" name=\"UED_ACC\" value=".$_GET['UED_ACC']." >"  ;
      echo "<input type=\"hidden\" name=\"idUSER\" value=".$_GET['idUSER']." >"  ;
      echo "<input type=\"submit\" value=\"Ulož\" >"  ;
      echo "</td></tr></table></form>";
   
    
  }
 
   
###############################################################################  
 } 
?>

 </div>
</center>
<script language="JavaScript"><!-------------
// Tuto funkci je mozno vlozit na konec hlavního dokumentu a ušetři její opakování
if(document.getElementById) {
  document.write('<img ID="required" STYLE="visibility:hidden; position:absolute; left:300; top:120" src="required.gif">');
  document.write('<p ID="requiredtext" STYLE="visibility:hidden; position:absolute; left:365; top:155" >abc</p>');
}
</script>
