<?php
# ID: 23
# Trunk_edit_module ver.1.0
# last modification 12.5.2010
# Formulář pro edit trunků
#   
define (KONTEXT,"incomming-calls");

if (IsSet($_POST['TC_SUBMIT'])&& $_POST['TC_SUBMIT']=="TRUE" ){
   include "build/db_connect.inc.php"  ;
   $query="SELECT idUSER
            FROM user_account
            WHERE id=".$_POST['TC_ACCOUNT']."
            AND idTECHNOLOGY=".$_POST['TC_TECHNOLOGY']."
            ";
   $result = mysql_query($query);
    if (!$result) {
      die('Nelze dohledat uživatele:' . mysql_error());
    }
    list($idUSER) =  mysql_fetch_row($result)  ;
   $query="START TRANSACTION ";
    $result = mysql_query($query);
    
    if (!$result) {
          die('Transakci nelze spustit:' . mysql_error());
    } 
    
    
   $query="UPDATE trunk SET Name=\"".$_POST['TC_NAME']."\",idPROVIDER=".$_POST['TC_PROVIDER'].",HOST=\"".$_POST['TC_HOST']."\",description=\"".$_POST['TC_DESCRIPTION']."\"";
   if ($_POST['TC_VALIDITY']==1){
      $query.=",validity=1 ";
   }else{
      $query.=",validity=0 ";
   }
    $query.=" WHERE idTrunk=".$_SESSION['TC_TEMP'][TC_ID]."";
    $result = mysql_query($query);      
    if (!$result) {
        die('Data nelze vložit mezi trunky:' . mysql_error());
    }
     $query="UPDATE sip_buddies SET context=\"".KONTEXT."\",host=\"".$_POST['TC_HOST']."\" WHERE id=".$_POST['TC_ACCOUNT']." ";
     $result = mysql_query($query);
      if (!$result) {
        die('Aktualizace kontextu se nezdařila:' . mysql_error());
      }

    $query="UPDATE trunk_detail SET id=".$_POST['TC_ACCOUNT'].",idTECHNOLOGY=".$_POST['TC_TECHNOLOGY']."
            WHERE idTRUNK=".$_SESSION['TC_TEMP'][TC_ID]."";
    $result = mysql_query($query);
      if (!$result) {
        die('K trunku nelze aktualizovat účet:' . mysql_error());
      }
      $query="SELECT table_name 
            FROM technology
            WHERE idTECHNOLOGY=\"".$_POST['TC_TECHNOLOGY']."\"";
            $result = mysql_query($query);
            if (!$result) {
                die('Nelye naleznot tabulku technologie:' . mysql_error());
            }
            list($table) = mysql_fetch_row($result)  ;          
            $query="SELECT TAB.name,TAB.md5secret
                      FROM ".$table." TAB
                      WHERE TAB.id=".$_POST['TC_ACCOUNT']."
                  ";
            $result = mysql_query($query);
            if (!$result) {
                die('Účet nelze načíst:' . mysql_error());
            }
            list($trunk_user,$trunk_md5pass) = mysql_fetch_row($result)  ;
    $query="SELECT 1 FROM ast_config WHERE cat_metric=2 AND var_metric=".$_SESSION['TC_TEMP'][TC_ID]."" ;
    $result = mysql_query($query);
    if (!$result) {
        die('Nelze prohlednout tabulku ast_config:' . mysql_error());
    }
    $file_conf="";
          switch($_POST['TC_TECHNOLOGY']){
            case 1 : $file_conf="sip.conf";
            break;
      }
    if (mysql_num_rows($result)==0)
    {
        
      if(IsSet($_POST['TC_VALIDITY']) && $_POST['TC_VALIDITY']==1 ){
          
          $query="INSERT INTO ast_config (cat_metric,var_metric,filename,category,var_name,var_val)
                  VALUES (2,".$_SESSION['TC_TEMP'][TC_ID].",\"".$file_conf."\",\"general\",\"register\",\"".$trunk_user.":".$_POST['TED_PASSWD']."@".$_POST['TC_HOST']."\")";
          $result = mysql_query($query);
          if (!$result) {
              die('Nelze aktualizovat konfigurační soubor sip.conf:' . mysql_error());
          }
      
      }
    
    }else{
         if(IsSet($_POST['TC_VALIDITY']) && $_POST['TC_VALIDITY']==1 ){ 
            
            $query="UPDATE ast_config SET var_val=\"".$trunk_user.":".$_POST['TED_PASSWD']."@".$_POST['TC_HOST']."\"
                    WHERE cat_metric=2 AND var_metric=".$_SESSION['TC_TEMP'][TC_ID]."";
            $result = mysql_query($query);
            if (!$result) {
                die('Nelze aktualizovat konfigurační soubor sip.conf:' . mysql_error());
          }   
         }else{
            $query="DELETE FROM ast_config 
                    WHERE cat_metric=2 
                    AND var_metric=".$_SESSION['TC_TEMP'][TC_ID]."
                    AND filename=\"".$file_conf."\"";
            $result = mysql_query($query);
            if (!$result) {
                die('Záznam z tabulky ast_conf nelze smazat:' . mysql_error());
            }
         
         }
    }
    
################################################################################      
            
      
      
    $query="COMMIT ";
    $result = mysql_query($query);      
    
    
  echo "Aktualizace trunku byla uložena. ";
  echo "<a href=\"".selfURL()."?idTrunk=".$_SESSION['TC_TEMP'][TC_ID]."\"> Pokračovat </a>";



}else{

if (!IsSet($_POST['TC_SUBMIT'])){
    if (IsSet($_SESSION['TC_TEMP'])){
        unset($_SESSION['TC_TEMP']);
    }
    $query="SELECT T.idTRUNK,T.name,T.HOST,TD.idTECHNOLOGY,UA.id,T.idPROVIDER,T.description,T.validity
            FROM trunk T
            JOIN trunk_detail TD ON TD.idTRUNK=T.idtrunk
            JOIN user_account UA ON UA.id=TD.id AND UA.idTECHNOLOGY=TD.idTECHNOLOGY
            WHERE T.idTRUNK=".$_GET['idTrunk']."";
    $result = mysql_query($query);
    if (!$result) {
        die('Nelze načíst informace o trunku:' . mysql_error());
    }
    list($_SESSION['TC_TEMP'][TC_ID],$_SESSION['TC_TEMP'][TC_NAME],$_SESSION['TC_TEMP'][TC_HOST],$_SESSION['TC_TEMP'][TC_TECHNOLOGY],$_SESSION['TC_TEMP'][TC_ACCOUNT],$_SESSION['TC_TEMP'][TC_PROVIDER],$_SESSION['TC_TEMP'][TC_DESCRIPTION],$_SESSION['TC_TEMP'][TC_VALIDITY])=mysql_fetch_row($result)  ;
          require "build/db_connect.inc.php"  ;
          $query="SELECT table_name
                  FROM technology
                  WHERE validity=1 
                  AND idTECHNOLOGY=".$_SESSION['TC_TEMP'][TC_TECHNOLOGY]."";
          $result = mysql_query($query);
          
          if (!$result) {
            die('Nelze naleznou správnou tabulku:' . mysql_error());
          }
          list($table ) = mysql_fetch_row($result);
          $query="SELECT name,md5secret
                  FROM ".$table." TAB
                  WHERE id=".$_SESSION['TC_TEMP'][TC_ACCOUNT]." 
                ";
          $result = mysql_query($query);
          list($user,$heslomd5) = mysql_fetch_row($result);
          if (!$result) {
          echo " user-".$user;
            die('Účet nelze načíst:' . mysql_error());
          }   
}else{
    $_SESSION['TC_TEMP'][TC_ID]=$_POST['TC_ID'];
    $_SESSION['TC_TEMP'][TC_NAME]=$_POST['TC_NAME'];
    $_SESSION['TC_TEMP'][TC_HOST]=$_POST['TC_HOST'];
    $_SESSION['TC_TEMP'][TC_TECHNOLOGY]=$_POST['TC_TECHNOLOGY'];
    $_SESSION['TC_TEMP'][TC_ACCOUNT]=$_POST['TC_ACCOUNT'];
    $_SESSION['TC_TEMP'][TC_DESCRIPTION]=$_POST['TC_DESCRIPTION'];
    $_SESSION['TC_TEMP'][TC_TC_PROVIDER]=$_POST['TC_TC_PROVIDER'];
    if ($_SESSION['TC_TEMP'][TC_ACCOUNT]!=''){
         require "build/db_connect.inc.php"  ;
          $query="SELECT table_name
                  FROM technology
                  WHERE validity=1 
                  AND idTECHNOLOGY=".$_SESSION['TC_TEMP'][TC_TECHNOLOGY]."";
          $result = mysql_query($query);
          
          if (!$result) {
            die('Nelze naleznou správnou tabulku:' . mysql_error());
          }
          list($table ) = mysql_fetch_row($result);
          $query="SELECT name,md5secret
                  FROM ".$table." TAB
                  WHERE id=".$_SESSION['TC_TEMP'][TC_ACCOUNT]." 
                ";
          $result = mysql_query($query);
          list($user,$heslomd5) = mysql_fetch_row($result);
          if (!$result) {
          echo " user-".$user;
            die('Účet nelze načíst:' . mysql_error());
          }
    
    
    }
    
    
}




 
?>




<script language="JavaScript">
// 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 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 CheckPasswd( theField,theField1, theMessage ) {
   if( MD5(document.getElementById('NAME').value+":asterisk:"+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 {
      return true;
   }
  }
  function Validate(theForm) {
      if( !CheckEmptyField(theForm.TC_NAME,"Zadejte prosím název trunku!") ) return false;
      if( !CheckEmptyField(theForm.TC_HOST,"Zadejte prosím adresu hosta!") ) return false;
      if( !CheckEmptyField(theForm.TC_TECHNOLOGY,"Vyberte typ požadovaného účtu!") ) return false;
      if( !CheckEmptyField(theForm.TC_ACCOUNT,"Vyberte konkrétní účet!") ) return false;
      if( !CheckPasswd(theForm.TED_PASSWD,theForm.TED_PASSWD1,"hesla se nerovnaji") ) return false;
  // uspech
  return true;
}
     <!-- <![CDATA[
      var MaxLengthLock = false;
      function MaxLengthCount(fieldObj,fieldMaxLength)
      {
        if (!MaxLengthLock)
        {
          MaxLengthLock = true;
          if (fieldObj.value.length > fieldMaxLength)
          {
            alert("Text je delší než " + fieldMaxLength + " znaků!");
            fieldObj.value = fieldObj.value.substring(0,fieldMaxLength);
          }
          var percentage = parseInt(100 - (( fieldMaxLength - fieldObj.value.length) * 100)/fieldMaxLength);
          document.getElementById(fieldObj.id + "_PBar").style.width = parseInt((parseInt(fieldObj.offsetWidth)*percentage)/100)+"px";
          MaxLengthLock = false;
        }
      }
      //]]> -->

  function RefreshForm(theForm){
      document.form1.TC_SUBMIT.value="FALSE";
      document.forms.form1.submit();
  
  }  

</script>

<center>
 <div class="forms">
<h2>Editace trunku</h2><span class="backtoform"><a href="admin_page.php?NEXTFORM=17">Zpět</a> </span>
<fieldset>
  <legend>Informace o trunku</legend>
<form name="form1" action="admin_page.php" onSubmit="return Validate(this);" method="post">
      <table >
        <tr>
          <td class="tablehead">ID: </td><td class="tableculombhead"><input type="text" value="<?php echo $_SESSION['TC_TEMP'][TC_ID];?>" disabled></td>
        </tr><tr>
          <td class="tablehead">Název: </td><td class="tableculombhead"><input type="text" name="TC_NAME" value="<?php echo $_SESSION['TC_TEMP'][TC_NAME];?>" ></td>
        </tr>
        <tr>
          <td class="tablehead">HOST: </td><td class="tableculombhead"><input type="text" name="TC_HOST" value="<?php echo $_SESSION['TC_TEMP'][TC_HOST];?>"> </td>
        </tr><tr>
             <?php 
        #Část která vyčítá do roletky formuláře možné uživatelské skupiny
          include "build/db_connect.inc.php"  ;
          $query="SELECT idtechnology,technology
                  FROM technology
                  WHERE validity=1
                  ORDER BY technology ASC";
          $result = mysql_query($query);
          
          if (!$result) {
            die('Could not query:' . mysql_error());
          }
          
          if (mysql_num_rows($result)!=0){
        
            
            echo ("<td class=\"tablehead\">Typ účtu: </td><td class=\"tableculombhead\"><select name=\"TC_TECHNOLOGY\" size=\"1\" OnChange=\"RefreshForm()\">");
                                             //   echo("<option value=\"\"></option>");
                                                while (list($id_technology, $technology ) = mysql_fetch_row($result)){
                                                #podmínka pro nastavení defaultní hodnoty skupiny na User
                                                    if ($_SESSION['TC_TEMP'][TC_TECHNOLOGY]==$id_technology){
                                                        echo("<option value=\"".$id_technology."\" selected>".$technology."</option>"); 
                                                    }else{
                                                        echo("<option value=\"".$id_technology."\">".$technology."</option>");
                                                    }
                                                }
                                                echo ("</select>");
                                                echo ("</td>");
          }     
          ?>
        </tr><tr>
           <?php 
        #Část která vyčítá do roletky formuláře možné uživatelské skupiny
         if (IsSet($_SESSION['TC_TEMP'][TC_TECHNOLOGY]) && $_SESSION['TC_TEMP'][TC_TECHNOLOGY]!='') {     
          include "build/db_connect.inc.php"  ;
          $query="SELECT table_name
                  FROM technology
                  WHERE validity=1 
                  AND idTECHNOLOGY=".$_SESSION['TC_TEMP'][TC_TECHNOLOGY]."";
          $result = mysql_query($query);
          
          if (!$result) {
            die('Nelze naleznou správnou tabulku:' . mysql_error());
          }
          list($table ) = mysql_fetch_row($result);
          $query="SELECT TAB.id,TAB.name
                  FROM ".$table." TAB
                  JOIN user_account UA ON UA.id=TAB.id 
                  WHERE NOT TAB.type='user' 
                  ";
          $result = mysql_query($query);
          if (!$result) {
            die('Účet nelze načíst:' . mysql_error());
          }
          }
         
        
             if(!IsSet($_SESSION['TC_TEMP'][TC_TECHNOLOGY]) || $_SESSION['TC_TEMP'][TC_TECHNOLOGY]==''){$pom="disabled";} else {$pom=''; }
            echo ("<td class=\"tablehead\">Účet: </td><td class=\"tableculombhead\"><select name=\"TC_ACCOUNT\" ".$pom." size=\"1\" onChange=\"RefreshForm()\" \">");
                                                unset ($pom);
                                                echo("<option value=\"\">vyberte</option>");
                                                while (list($id, $name ) = mysql_fetch_row($result)){
                                                #podmínka pro nastavení defaultní hodnoty skupiny na User
                                                    if ($_SESSION['TC_TEMP'][TC_ACCOUNT]==$id){                                                
                                                        echo("<option value=\"".$id."\" selected>".$name."</option>");
                                                    }else{
                                                        echo("<option value=\"".$id."\" >".$name."</option>");
                                                    }
                                                }
                                                echo ("</select>");
                                                echo ("</td>");
           
             
          ?>
        </tr><tr>
             <td class="tablehead">Ověření hesla účtu:</td><td class="tableculombhead"><input type="password" id="pass" name="TED_PASSWD" </td>
                      <input type="hidden" id="PASS" name="TED_PASSWD1" value="<?php echo $heslomd5;?>">
                      <input type="hidden" id="NAME" value="<?php echo $user;?>">
                       <input type="hidden" name="TC_ID" value="<?php echo $_SESSION['TC_TEMP'][TC_ID];?>">
                  
        </tr><tr>
         
            <?php 
        #Část která vyčítá do roletky formuláře možné uživatelské skupiny
          include "build/db_connect.inc.php"  ;
          $query="SELECT idPROVIDER,Name
                  FROM provider
                  ORDER BY Name ASC";
          $result = mysql_query($query);
          
          if (!$result) {
            die('Could not query:' . mysql_error());
          }
          
          if (mysql_num_rows($result)!=0){
        
            
            echo ("<td class=\"tablehead\">Provider: </td><td class=\"tableculombhead\"><select name=\"TC_PROVIDER\" size=\"1\">");
                                                 
                                                while (list($id_provider, $Name ) = mysql_fetch_row($result)){
                                                #podmínka pro nastavení defaultní hodnoty skupiny na User
                                                    if ($_SESSION['TC_TEMP'][TC_PROVIDER]==$id_provider){
                                                        echo("<option value=\"".$id_provider."\" selected>".$Name."</option>");
                                                    }else{
                                                        echo("<option value=\"".$id_provider."\">".$Name."</option>");
                                                    }
                                                }
         
                                       echo ("</select>");
                                       echo ("</td>");
                                       
          }     
          ?>
          
          
          
          
        
        </tr><tr>
          <td class="tablehead" >Aktivní:</td><td class="tableculombhead"><input type="checkbox" name="TC_VALIDITY" value="1" <?php if ($_SESSION['TC_TEMP'][TC_VALIDITY]==1 ) echo "checked"; ?>></td>
        </tr><tr>
          <td class="tablehead" colspan=2>Popis: </td>
        </tr><tr>
          <td colspan=2 class="tableculombhead"><textarea name="TC_DESCRIPTION" id="TC_DESCRIPTION" accesskey="T" rows="6" cols="30" wrap="virtual"
            onpropertychange="MaxLengthCount(this,160);"
            onclick="MaxLengthCount(this,160);"
            onmousemove="MaxLengthCount(this,160);"
            onblur="MaxLengthCount(this,160);"
            onkeyup="MaxLengthCount(this,160);"
            onfocus="MaxLengthCount(this,160);"
            onchange="MaxLengthCount(this,160);"
            onafterupdate="MaxLengthCount(this,160);"><?php echo $_SESSION['TC_TEMP'][TC_DESCRIPTION] ?></textarea>
          <div id="TC_DESCRIPTION_PBar" style="width:0px;height:10px;border:0;font-size:8px;overflow:hidden;background-color: #3399FF;"></div></td>
        </tr><tr>
           <td colspan=2 style="background-color:red;color:white">Po uložení bude účet zařazen do kontextu <b>incomming-calls</b><br> a položka host bude nastavena na novou zadanou hodnotu.</td>
        <input name="TC_SUBMIT" type="hidden" value="TRUE" >
        </tr><tr>
        <td class="tablehead" colspan=2><input type="reset" value="Storno"><input type="submit" value="Ulož">
        </tr>
      </table>
  </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
}
?>