<?php
  ///////////////////////////////////////////////////
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  require_once "../utils/chlang.php";

  $title = $TEXT['book-title3'];
  $button = $TEXT['btn_add'];
  $action = "edit_izd.php";
  $button2 = $TEXT['btn_back'];
  $action2 = "editform.php";

require_once "../config_a.php";
require_once "../utils/functions.php";
require_once "../utils/top_adm.php";

if($result2['administrator'] == 0) {puterror($result2['skin'],$TEXT['error-loginform2']); require_once "../utils/bottom_adm.php"; exit();}

if(empty($_POST['book_id']))
{
if(empty($_GET['book_id'])) {puterror($result2['skin'],$TEXT['error-book5']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_GET['book_id'])>255) {puterror($result2['skin'],$TEXT['error-book6']); require_once "../utils/bottom_adm.php"; exit();}
$_GET['book_id'] = rep($_GET['book_id']);
$book_id = $_GET['book_id'];
}
else
{
if(strlen($_POST['book_id'])>255) {puterror($result2['skin'],$TEXT['error-book6']); require_once "../utils/bottom_adm.php"; exit();}
$_POST['book_id'] = rep($_POST['book_id']);
$book_id = $_POST['book_id'];
}

if(strlen($_GET['book_id_d'])>255) {puterror($result2['skin'],$TEXT['error-book6']); require_once "../utils/bottom_adm.php"; exit();}
$_GET['book_id_d'] = rep($_GET['book_id_d']);

if(strlen($_POST['izd_id_add'])>255) {puterror($result2['skin'],$TEXT['error-book6']); require_once "../utils/bottom_adm.php"; exit();}
$_POST['izd_id_add'] = rep($_POST['izd_id_add']);

if(strlen($_GET['izd_id_d'])>255) {puterror($result2['skin'],$TEXT['error-book6']); require_once "../utils/bottom_adm.php"; exit();}
$_GET['izd_id_d'] = rep($_GET['izd_id_d']);

// Delete book izdatelstvo
if(!empty($_GET['book_id_d'])) {
  $query_book2 = "DELETE FROM `tbl_s_book_izd` WHERE `book_id`='".$_GET['book_id_d']."' AND `izd_id`='".$_GET['izd_id_d']."' LIMIT 1";
        $result_book2 = mysqli_query($dbcnx,$query_book2);
         if(!$result_book2) {puterror($result2['skin'],$TEXT['error-tbl15']); require_once "../utils/bottom_adm.php"; exit();}  
} else {$_GET['book_id_d'] = NULL;}

// Add book izdatelstvo
if(!empty($_POST['izd_id_add'])) {
  $query_izd2 = "INSERT INTO tbl_s_book_izd (`s_book_izd` , `izd_id` , `book_id` ) VALUES ( NULL , '".$_POST['izd_id_add']."' , '".$_POST['book_id']."')";
        $result_izd2 = mysqli_query($dbcnx,$query_izd2);
         if(!$result_izd2) {puterror($result2['skin'],$TEXT['error-tbl15']); require_once "../utils/bottom_adm.php"; exit();}  
} else {$_POST['izd_id_add'] = NULL;}

?>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="30%" >
<form enctype="multipart/form-data" action="<?php echo $action; ?>" method="post" >
<tr>
  <td class="inform"><?php echo $TEXT['book-izd']; ?>:&nbsp;</td><td>
    <select name="izd_id_add">
    <?php
        $queryizd = "SELECT izd_id, izdatelstvo FROM `tbl_izdatelstvo` WHERE use_prog='1' ORDER BY `izdatelstvo` ";
          $resultizd = mysqli_query($dbcnx,$queryizd);
            if(!$resultizd) {puterror2($result2['skin'],$TEXT['error-tbl6']); require_once "utils/bottom.php"; exit();}
              while($result_izdatelstvo = mysqli_fetch_array($resultizd))
                {
                 $chk_izdatelstvo = "<option value=".$result_izdatelstvo['izd_id'].">".rep_back($result_izdatelstvo['izdatelstvo'])."";
                 echo $chk_izdatelstvo;
                }
    ?>
    </select></td>
  <td align="left"><input class="button2" type="submit" value="<?php echo $button; ?>" /></td>
</tr>
<input type="hidden" name="book_id" value="<?php echo $book_id; ?>" />
</table>
</form>
<br>
<form enctype="multipart/form-data" action="<?php echo $action2; ?>" method="post" >
<table align="center" border="1" cellpadding="3" cellspacing="0" width="30%" >
<?php

     $query_body = "SELECT
     tbl_s_book_izd.*,
     tbl_izdatelstvo.*,
     (SELECT COUNT(tbl_s_book_izd.book_id) FROM tbl_s_book_izd WHERE tbl_s_book_izd.book_id = '".$book_id."') AS cnt_book_id
     FROM tbl_s_book_izd
     JOIN tbl_izdatelstvo ON tbl_s_book_izd.izd_id=tbl_izdatelstvo.izd_id
     WHERE book_id = '".$book_id."' ";
     $result_body = mysqli_query($dbcnx,$query_body);
     if(!$result_body) {puterror($result2['skin'],$TEXT['error-tbl6']); require_once "../utils/bottom_adm.php"; exit();}
       $result_rows = mysqli_num_rows($result_body);
       if($result_rows > 0)
         {
           echo "<thead ><tr class='tableheadercat'>
            <td>".$TEXT['book-izd']."</td>
            <td>".$TEXT['global-action']."</td>
                 </tr></thead><tbody id='tab' >";
             while($result_body_izdatelstvo = mysqli_fetch_array($result_body))
               {
               if($result_body_izdatelstvo['cnt_book_id'] < 2)
               {
                $delete_pic = "<img alt='".$TEXT['global-lock']."' src='../skins/".$result2['skin']."/nodelete.png' />";
                $delete = $delete_pic."&nbsp;".$TEXT['delete-new'];
               }
                else
                 {
                  $delete_pic = "<img alt='".$TEXT['delete-new']."' src='../skins/".$result2['skin']."/delete.png' />";
                  $delete = $delete_pic."&nbsp;<a href='edit_izd.php?izd_id_d=".$result_body_izdatelstvo['izd_id']."&amp;book_id_d=".$book_id."&amp;book_id=".$book_id."' title='".$TEXT['delete-tit']."'>".$TEXT['delete-new']."</a>";
                 }
                 
               if($result_body_izdatelstvo['izd_izd_id'] == $result_body_izdatelstvo['chk_izd_id']) {
                echo "<tr><td>".rep_back($result_body_izdatelstvo['izdatelstvo'])."</td>";
                echo "<td>".$delete."</td></tr>";
                   
               } else {echo "</tr><td colspa='2'>".$TEXT['search-err4']."</td></tr>";}
              }
           ?>
            </tbody>
            <tfoot>
                <tr class="tableheadercat">
                <td colspan="3" align="right"><?php echo $TEXT['size-count2']; ?> = <?php echo $result_rows; ?></td>
                </tr>
            </tfoot>
              <?php
         }
?>
</table>
<table align="center" border="0" cellpadding="5" cellspacing="0" width="30%" >
<tr><td align="right"><input class="button2" type="submit" value="<?php echo $button2; ?>" />
<input type="hidden" name="book_id" value="<?php echo $book_id; ?>" />
</td>
</tr>
</table>
</form>
<?php
  require_once "../utils/bottom_adm.php";
?>