<?php
# ID: -22-     IdFunction v tabulce Function   
# User_callacc_assign_module ver.1.0
# last modification 28.2.2010
# Formulář pro náhled záznamu k vybranému účtu
#   
# 
# 
require "files/javamd5.inc.php";
if (IsSet($_GET['UNSET'])){
   $query="UPDATE assign_tarif_mix SET validity=0 
          WHERE idTARIFMIX=".$_GET['idTARIFMIX']."
          AND id=".$_GET['idCALLACC']."
          AND idTECHNOLOGY=".$_GET['idTECHNOLOGY']." 
            ";
    $result = mysql_query($query);
    if (!$result) {
      die('Zaznam nelze zrusit:' . mysql_error());
  }
}
if (IsSet($_POST['CA_SUBMITED'])){
  #kontrola, zda je veden zaznam v tab user_account
  $query="SELECT 1
          FROM user_account 
          WHERE idTECHNOLOGY=".$_POST['idTECHNOLOGY']."
          AND id=".$_POST['idCALLACC']."";
  $result = mysql_query($query);
  if (!$result) {
      die('Chyba v proverovani zaznamu:' . mysql_error());
  }
  if (mysql_num_rows($result)!=0){
        $query="START TRANSACTION";
        $result = mysql_query($query);
        $query="UPDATE user_account SET idUSER=".$_POST['CA_idUSER']."
                  WHERE idTECHNOLOGY=".$_POST['idTECHNOLOGY']."
                  AND id=".$_POST['idCALLACC']."";
        $result = mysql_query($query);
        if (!$result) {
              $query="ROLLBACK";
              $result = mysql_query($query);
             die('Update uživatele se nezdařil:' . mysql_error());
        }
       $query="SELECT table_name FROM technology WHERE idTECHNOLOGY=".$_POST['idTECHNOLOGY']."";
        $result = mysql_query($query);
        if (!$result) {
              $query="ROLLBACK";
              $result = mysql_query($query);
             die('Nepodařilo se načíst správnou tabulku:' . mysql_error());
        }
        list($table)=mysql_fetch_row($result);
        $query="UPDATE ".$table." SET  name=\"".$_POST['CA_NAME']."\",callerid=\"".$_POST['CA_CALLERID']."\",
                context=\"".$_POST['CA_KONTEXT']."\",host=\"".$_POST['CA_HOST']."\",type=\"".$_POST['CA_TYPE']."\" ";
        if ($_POST['CA_PASSWD']!='')
        {
          $query .=",md5secret=\"".urlencode($_POST['CA_PASSWD'])."\" ";
        }        
              
        $query .=" WHERE id=".$_POST['idCALLACC']."";
        $result = mysql_query($query);
        if (!$result) {
              $query="ROLLBACK";
              $result = mysql_query($query);
             die('Nepodařilo se aktualizovat tabulku:' . mysql_error());
        }
        $query="COMMIT";
        $result = mysql_query($query);
        
                                                                
  }else{
        
            $query="START TRANSACTION";
            $result = mysql_query($query);
            $query="INSERT INTO bill (validity) VALUES(1)";
            $result = mysql_query($query);
            if (!$result) {
                 die('Záznam v tabulce bill nelze vytvořit:' . mysql_error());
            }
            $query="SELECT last_insert_id()";
            $result = mysql_query($query);
            list($idbill)=mysql_fetch_row($result);
            $query="INSERT INTO user_account (id,idTECHNOLOGY,idUSER,idBILL)
                    VALUES (".$_POST['idCALLACC'].",".$_POST['idTECHNOLOGY'].",".$_POST['CA_idUSER'].",".$idbill.")";
            $result = mysql_query($query);
            if (!$result) {
                 die('Záznam v tabulce user_account nelze vytvorit:' . mysql_error());
            }
        
        ##########################################
        $query="COMMIT"; 
        $result = mysql_query($query);        
  }
  echo "Zmeny uloženy. <a href=\"admin_page.php?NEXTFORM=22&PARENT=".$_POST['PARENT']."&idCALLACC=".$_POST['idCALLACC']."&idTECHNOLOGY=".$_POST['idTECHNOLOGY']."\">Zpět</a>";
  
}else if(IsSet($_POST['CA_SUBMITED1'])){
        if($_POST['CA_TG']!=''){
            $query="SELECT validity
                    FROM assign_tarif_mix
                    WHERE idTARIFMIX=".$_POST['CA_TG']."
                    AND id=".$_POST['idCALLACC']."
                    AND idTECHNOLOGY=".$_POST['idTECHNOLOGY']."" ;
            $result = mysql_query($query);
            if (!$result) {
                 die('Tarif nelze přidat:' . mysql_error());
            }
            if(mysql_num_rows($result)==0){                
                $query="INSERT INTO assign_tarif_mix (idTARIFMIX,id,idTECHNOLOGY)
                        VALUES (".$_POST['CA_TG'].",".$_POST['idCALLACC'].",".$_POST['idTECHNOLOGY'].")";
                $result = mysql_query($query);
                if (!$result) {
                     die('Tarif nelze přidat:' . mysql_error());
                }
            }
            else{
                list($validity)=mysql_fetch_row($result);
                if ($validity==0){
                    $query="UPDATE assign_tarif_mix SET validity=1 
                            WHERE idTARIFMIX=".$_POST['CA_TG']."
                            AND id=".$_POST['idCALLACC']."
                            AND idTECHNOLOGY=".$_POST['idTECHNOLOGY']."" ;
                    $result = mysql_query($query);
                    if (!$result) {
                     die('Tarif nelze přidat:' . mysql_error());
                  }
                }
               
            
            
            }
        }
         
  
  echo "Zmeny uloženy. <a href=\"admin_page.php?NEXTFORM=22&PARENT=".$_POST['PARENT']."&idCALLACC=".$_POST['idCALLACC']."&idTECHNOLOGY=".$_POST['idTECHNOLOGY']."\">Zpět</a>";
  
  


}else{

  require "build/db_connect.inc.php"  ;
  $query="SELECT name,callerid,context,host,type,username
          FROM sip_buddies
          WHERE id = ".$_GET['idCALLACC']."";
  $result = mysql_query($query);
  if (!$result) {
      die('Údaje o volacím účtu uživatele nelze načíst:' . mysql_error());
  }
  list($name,$callerid,$context,$host,$type,$username) = mysql_fetch_row($result); 
 
?>

<center>

<div class="forms">
<h3>Účet: <?php echo($name);?></h3>
    <?php if(IsSet($_GET['PARENT']) ){
        echo "<span class=\"backtoform\"><a href=admin_page.php?NEXTFORM=".$_GET['PARENT'].">Zpět</a></span>";
    }else if (IsSet($_POST['PARENT']) ){
         echo "<span class=\"backtoform\"><a href=admin_page.php?NEXTFORM=".$_POST['PARENT'].">Zpět</a></span>";
    }
    else{    
        echo "<span class=\"backtoform\"><a href=admin_page.php?NEXTFORM=8&idUSER=".$_SESSION['idUSER'].">Zpět</a></span>";
     } 
    ?>
<fieldset>
<legend>Detaily účtu</legend>

<script language="JavaScript">
var zmena=0;

// 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('NAME').value+":asterisk:"+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 (zmena==1){
          if(!CheckEmptyField(theForm.CA_PASSWD,"Po změně názvu musí být nastaveno nové heslo!") ) return false;
      } 
      if( !CheckPasswd(theForm.CA_PASSWD,theForm.CA_PASSWD1,"hesla se nerovnaji") ) return false;
      
  // uspech
  return true;
}
  function Passwarn(){
   zmena=1;
   document.getElementById('PASS').style.background='red';
   
  }
    
      
</script>

  <form name="form" action="admin_page.php" onsubmit="return Validate(this);" method="post" >
      <table width="500px">
          <tr>
            <td class="tablehead">Název:</td><td class="tableculombhead" style="text-align:left"><input type="text" size=10 id="NAME" name="CA_NAME" OnChange="Passwarn()" value=<?php echo("\"".$name."\"");?> ></td>
            <td class="tableculombhead"></td>
            <td class="tablehead">Heslo</td>
          </tr><tr>
            <td class="tablehead">Zobrazované ID:</td><td class="tableculombhead" style="text-align:left"><input type="text" size=10 name="CA_CALLERID" value=<?php echo("\"".$callerid."\"");?> ></td>
            <td class="tableculombhead"></td>
            <td id="PASSCHCK" class="tableculombhead" style="text-align:center"><input type="password" size=10 id="PASS"  name="CA_PASSWD"  ></td>
            
          </tr><tr>
            <td class="tablehead">Kontext</td><td class="tableculombhead" style="text-align:left"><input type="text" size=10 name="CA_KONTEXT" value=<?php echo("\"".$context."\"");?> ></td>
            <td class="tableculombhead"></td>
            <td class="tableculombhead" style="text-align:center"><input type="password" size=10 name="CA_PASSWD1"  ></td>
            
          </tr><tr>
            <td class="tablehead">Host:</td><td class="tableculombhead" style="text-align:left"><input type="text" size=10 name="CA_HOST" value=<?php echo("\"".$host."\"");?> ></td>
            <td class="tableculombhead"></td>
            <td class="tablehead">Uživatel:</td>
            
          </tr><tr>  
               <td class="tablehead">Typ:</td><td class="tableculombhead" style="text-align:left">
                           <?php  //ZjistiTyp();                              
                              $typy=array("friend","peer","user");
                          echo "<select name=\"CA_TYPE\">";                        
                              for ($a=0;$a<=2;$a++){
                                 if ($type==$typy[$a]){
                                    echo "<option value=".$typy[$a]." selected>".$typy[$a]."</option> ";
                                }else{
                                    echo "<option value=".$typy[$a].">".$typy[$a]."</option> ";
                                 }
                              }
                              
                           ?>
                         </select>
                         <td class="tableculombhead"></td>
                 <td class="tableculombhead"><select name="CA_idUSER"> 
                    <?php
                       $query="SELECT idUSER
                                FROM user_account
                                WHERE id=".$_GET['idCALLACC']."
                                AND idTECHNOLOGY=".$_GET['idTECHNOLOGY']." 
                                AND validity=1";
                       $result = mysql_query($query);         
                       if (!$result) {
                          die('Chyba při načítání přiřazené tarifní skupiny:' . mysql_error());
                        }         
                       list($uss_id)=mysql_fetch_row($result);         
                       $query="SELECT login,idUSER
                                FROM user
                                WHERE validity=1";
                        $result = mysql_query($query); 
                        if (!$result) {
                          die('Údaje o volacím účtu uživatele nelze načíst:' . mysql_error());
                        }
                        echo "<option value=\"1\">vyberte</option>";
                        while(list($uss_name,$us_id)=mysql_fetch_row($result)){
                          if ($uss_id==$us_id){
                            echo "<option value=".$us_id." selected>".$uss_name."</option>";
                          }else{
                            echo "<option value=".$us_id.">".$uss_name."</option>";
                          }
                        }       
                    ?>
                  </select>  
                    </td>        
              <!--       <input type="text" size=10 value=<?php echo("\"".$type."\"");?> disabled></td>   //-->
              </tr><tr>
            <td><input type="submit" name="CA_SUBMITED" value="Ulož"></td>
          </tr>
       <!--    <td class="tablehead">Username:</td><td><input type="text" value=<?php echo("\"".$username."\"");?> disabled></td>  //-->
            <input type="hidden" name="idCALLACC" value="<?php echo ($_GET['idCALLACC'])?>">
            <input type="hidden" name="idTECHNOLOGY" value="<?php echo ($_GET['idTECHNOLOGY'])?>">
             <input type="hidden" name="PARENT" value="<?php echo $_GET['PARENT']; ?>">
          
      </table>
  </form>

</fieldset>
<fieldset>
  <legend>Tarifní skupiny</legend>
  <form action="admin_page.php" method=post>
   <table width="400px">
   <tr>
  <td colspan=3><select name="CA_TG"> 
                    <?php
                       $query="SELECT idTARIFMIX
                                FROM assign_tarif_mix
                                WHERE id=".$_GET['idCALLACC']."
                                AND idTECHNOLOGY=".$_GET['idTECHNOLOGY']." 
                                AND validity=1
                               
                                ";
                                
                       $result = mysql_query($query);         
                       if (!$result) {
                          die('Chyba při načítání přiřazené tarifní skupiny:' . mysql_error());
                        }         
                       list($tms_id)=mysql_fetch_row($result);         
                       $query="SELECT TM.name,TM.idTARIFMIX
                                FROM tarif_mix TM
                                WHERE TM.validity=1
                                 AND NOT EXISTS (SELECT 1 FROM assign_tarif_mix
                                              WHERE id=".$_GET['idCALLACC']."
                                              AND idTECHNOLOGY=".$_GET['idTECHNOLOGY']."
                                              AND validity=1
                                              AND idTARIFMIX=TM.idTARIFMIX
                                              )";
                        $result = mysql_query($query); 
                        if (!$result) {
                          die('Údaje o volacím účtu uživatele nelze načíst:' . mysql_error());
                        }
                        echo "<option value=\"\">vyberte</option>";
                        while(list($tm_name,$tm_id)=mysql_fetch_row($result)){
                          if ($tms_id==$tm_id){
                            echo "<option value=".$tm_id." selected>".$tm_name."</option>";
                          }else{
                            echo "<option value=".$tm_id.">".$tm_name."</option>";
                          }
                        }       
                    ?>
            
                </select>
            </td><td><input type="submit" name="CA_SUBMITED1" value="Přidat"></td>
            </tr>
                <?php
                      $query="SELECT TM.idTARIFMIX
                              FROM assign_tarif_mix ATM
                              JOIN tarif_mix TM ON TM.idTARIFMIX=ATM.idTARIFMIX AND TM.validity=1
                              WHERE id=".$_GET['idCALLACC']."
                              AND ATM.idTECHNOLOGY=".$_GET['idTECHNOLOGY']."
                              AND ATM.validity=1
                              AND TM.start_date<now()
                              AND TM.start_date=(SELECT TM1.start_date
                                                  FROM assign_tarif_mix ATM1
                                                  JOIN tarif_mix TM1 ON TM1.idTARIFMIX=ATM1.idTARIFMIX AND TM1.validity=1
                                                  WHERE ATM1.id=".$_GET['idCALLACC']."
                                                  AND ATM1.idTECHNOLOGY=".$_GET['idTECHNOLOGY']."
                                                  AND ATM1.validity=1
                                                  AND TM1.start_date < now()
                                                  ORDER BY start_date DESC
                                                  LIMIT 1)
                              ";
                      $result = mysql_query($query);
                      if (!$result) {
                          die('Chyba při zjištění aktuálně platné tarifní skupiny:' . mysql_error());
                      }
                      list($actgroup)=mysql_fetch_row($result);
                      //echo "actgroup=".$actgroup;
                      $query="SELECT TM.idTARIFMIX,TM.Name,TM.start_date
                                FROM assign_tarif_mix ATM
                                JOIN tarif_mix TM ON TM.idTARIFMIX=ATM.idTARIFMIX AND TM.validity=1
                                WHERE ATM.id=".$_GET['idCALLACC']."
                                AND ATM.idTECHNOLOGY=".$_GET['idTECHNOLOGY']." 
                                AND ATM.validity=1";
                      $result = mysql_query($query);         
                      if (!$result) {
                          die('Chyba při načítání přiřazené tarifní skupiny:' . mysql_error());
                      }
                      $a=1;
                      if (mysql_num_rows($result)!=0){
                          echo "<tr>";
                          echo "<td width=\"10px\"></td><td>ID</td><td>Název</td><td>Platnost od</td>";
                          echo "</tr>";
                          while(list($_idtarifmix,$_name,$_startdate)=mysql_fetch_row($result)){
                              if ($_idtarifmix==$actgroup){
                              echo "<tr bgcolor=\"lightgreen\">";
                              echo "<td ><a href=\"admin_page.php?NEXTFORM=22&idTARIFMIX=".$_idtarifmix."&idCALLACC=".$_GET['idCALLACC']."&idTECHNOLOGY=".$_GET['idTECHNOLOGY']."&UNSET=\"><img src=\"files/pictures/no1.gif\"> </a></td><td>".$_idtarifmix."</td><td>".$_name."</td><td>".$_startdate."</td>";
                              echo "</tr>"  ;
                              }else{
                              if($a==1){$a=0; $barva="#ffb67a";}else{$a=1;$barva="#ffdfc5"; }
                             #####################################################################
                              echo "<tr bgcolor=$barva>";
                              echo "<td ><a href=\"admin_page.php?NEXTFORM=22&idTARIFMIX=".$_idtarifmix."&idCALLACC=".$_GET['idCALLACC']."&idTECHNOLOGY=".$_GET['idTECHNOLOGY']."&UNSET=\"><img src=\"files/pictures/no1.gif\"> </a></td><td>".$_idtarifmix."</td><td>".$_name."</td><td>".$_startdate."</td>";
                              echo "</tr>"  ;
                              }
                          }
                      }
                
                
                ?>
            
            
           
     </table> 
     <input type="hidden" name="PARENT" value="<?php echo $_GET['PARENT']; ?>">
     <input type="hidden" name="idCALLACC" value="<?php echo ($_GET['idCALLACC']); ?>"> 
     <input type="hidden" name="idTECHNOLOGY" value="<?php echo ($_GET['idTECHNOLOGY']); ?>">         
  </form>
</fieldset>
</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>
<?php
}
?>