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

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

  $title = $TEXT['book-title2'];
  $button = $TEXT['btn_add'];
  //$action = "add_filtr_autor.php";
  $action = "add_autor.php";
  $button2 = $TEXT['btn-next'];
  //$action2 = "add_autor_run.php";
  $action2 = "add_izd.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(strlen($_GET['familiya'])>255) {puterror($result2['skin'],$TEXT['error-aut4']); require_once "../utils/bottom_adm.php"; exit();}
$_GET['familiya'] = rep($_GET['familiya']);
$familiya=$_GET['familiya'];

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['autor_id_add'])>255) {puterror($result2['skin'],$TEXT['error-book6']); require_once "../utils/bottom_adm.php"; exit();}
$_POST['autor_id_add'] = rep($_POST['autor_id_add']);

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

// Delete book autor
if(!empty($_GET['book_id_d'])) {
  $query_book2 = "DELETE FROM `tbl_s_book_aut` WHERE `book_id`='".$_GET['book_id_d']."' AND `autor_id`='".$_GET['autor_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 autor
if(!empty($_POST['autor_id_add'])) {
  $query_izd2 = "INSERT INTO tbl_s_book_aut (`s_book_aut` , `autor_id` , `book_id` ) VALUES ( NULL , '".$_POST['autor_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['autor_id_add'] = NULL;}

?>
<form enctype="multipart/form-data" action="<?php echo $action; ?>" method="post" >
<table align="center" border="0" cellpadding="0" cellspacing="0" width="30%">
<tr>
  <td class="inform"><?php echo $TEXT['aut-full']; ?>:&nbsp;</td><td>
    <select name="autor_id_add">
    <?php
        $queryaut = "SELECT autor_id, name, familiya, otchestvo FROM `tbl_autor` ORDER BY name, familiya, otchestvo ";
          $resultaut = mysqli_query($dbcnx,$queryaut);
            if(!$resultaut) {puterror2($result2['skin'],$TEXT['error-tbl1']); require_once "utils/bottom.php"; exit();}
              while($result_autor = mysqli_fetch_array($resultaut))
                {
                 $chk_autor = "<option value=".$result_autor['autor_id']." title=".rep_back($result_autor['name'])."&nbsp;".rep_back($result_autor['otchestvo'])."&nbsp;".rep_back($result_autor['familiya']).">".rep_back($result_autor['name'])." ".rep_back($result_autor['familiya'])."";
                 echo $chk_autor;
                }
    ?>
    </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="2" cellspacing="0" width="30%">
<?php

     $query_body = "SELECT
     tbl_s_book_aut.*,
     tbl_autor.autor_id,
     tbl_autor.name,
     tbl_autor.familiya,
     tbl_autor.otchestvo,
     (SELECT COUNT(tbl_s_book_aut.book_id) FROM tbl_s_book_aut WHERE tbl_s_book_aut.book_id = '".$book_id."') AS cnt_book_id
     FROM tbl_s_book_aut
     JOIN tbl_autor ON tbl_s_book_aut.autor_id=tbl_autor.autor_id
     WHERE book_id = '".$book_id."'
     ORDER BY tbl_autor.name, tbl_autor.familiya, tbl_autor.otchestvo ";
     $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['aut-full']."</td>
            <td>".$TEXT['global-action']."</td>
                 </tr></thead><tbody id='tab' >";
             while($result_body_autor = mysqli_fetch_array($result_body))
               {
               if($result_body_autor['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='add_autor.php?autor_id_d=".$result_body_autor['autor_id']."&amp;book_id_d=".$book_id."&amp;book_id=".$book_id."' title='".$TEXT['delete-tit']."'>".$TEXT['delete-new']."</a>";
                 }
                 
               if($result_body_autor['izd_izd_id'] == $result_body_autor['chk_izd_id']) {
                echo "<tr><td title=".rep_back($result_body_autor['name'])."&nbsp;".rep_back($result_body_autor['otchestvo'])."&nbsp;".rep_back($result_body_autor['familiya']).">".rep_back($result_body_autor['name'])."&nbsp;".rep_back($result_body_autor['familiya'])."</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%" >
<?php
if($result_rows > 0) {echo "<tr><td align='right'><input class='button2' type='submit' value='".$button2."' /></td></tr>";}
    else {echo "<tr><td align='right'>&nbsp;</td></tr>";}
?>
<input type="hidden" name="book_id" value="<?php echo $book_id; ?>" />
</table>
</form>
<?php
  require_once "../utils/bottom_adm.php";
?>