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

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

  $title = $TEXT['book-title2'];
  $button2 = $TEXT['btn_add'];
  $action2 = "add_fltr_autor.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'])) {puterror($result2['skin'],$TEXT['error-book5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['book_id'])>255) {puterror($result2['skin'],$TEXT['error-book6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['familiya'])>255) {puterror($result2['skin'],$TEXT['error-aut4']); require_once "../utils/bottom_adm.php"; exit();}
  $_POST['familiya'] = rep($_POST['familiya']);
  $_POST['book_id'] = rep($_POST['book_id']);
  $book_id = $_POST['book_id'];
  $familiya= $_POST['familiya'];
?>
<form enctype='multipart/form-data' action=<?php echo $action2; ?> method=post >
<table align="center" border="0" cellpadding="0" 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; ?>' />
<input type=hidden name=familiya value='<?php echo $familiya; ?>' />
</td>
</tr>
</table>
<?php
 if(!empty($_POST['familiya']))
 {
 $familiya = " WHERE LOWER(tbl_autor.familiya) LIKE LOWER('%".$familiya."%') OR LOWER(tbl_autor.name) LIKE LOWER('%".$familiya."%') ORDER BY tbl_autor.name, tbl_autor.familiya";
 }
    else
    {
     $familiya = "ORDER BY tbl_autor.name, tbl_autor.familiya";
    }
     $query_body = "SELECT
     tbl_autor.name,
     tbl_autor.familiya,
     tbl_autor.otchestvo,
     tbl_autor.autor_id AS autor_book_id,
     (SELECT tbl_s_book_aut.autor_id FROM tbl_s_book_aut WHERE tbl_s_book_aut.book_id = '".$book_id."' AND tbl_s_book_aut.autor_id = tbl_autor.autor_id) AS chk_aut_id
     FROM tbl_autor
     $familiya ";
     $result_body = mysqli_query($dbcnx,$query_body);
     if(!$result_body) {puterror($result2['skin'],$TEXT['error-tbl1']); require_once "../utils/bottom_adm.php"; exit();}
       $result_rows = mysqli_num_rows($result_body);
       if($result_rows > 0)
         {
           echo "<table align='center' border='1' cellpadding='3' cellspacing='0' width='30%'>
            <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(empty($_POST['familiya'])) {$fam = "";}
                else {$fam = "&familiya=".$_POST['familiya']."";}

               if(!empty($result_body_autor['autor_book_id']) == $result_body_autor['chk_aut_id']) {$aut_est = "checked";}
                 else {$aut_est = "";}

              $usr_name = "<a href=info_autor.php?autor_id=".$result_body_autor['autor_book_id']."&book_id=".$book_id."".$fam." title='".$TEXT['aut-viewtit']."'>".$result_body_autor['name']."&nbsp;".$result_body_autor['otchestvo']."&nbsp;".$result_body_autor['familiya']."</a>";
              $aut_id = $result_body_autor['autor_book_id'];
             ?>
             <tr>
              <td><?php echo $usr_name; ?></td>
              <td><input type="checkbox" name=autor[<?php echo $aut_id; ?>] <?php echo $aut_est ; ?> /></td>
             </tr>
           <?php
               }
           ?>
               </tbody>
               <tfoot>
                <tr class="tableheadercat">
                <td colspan=3 align="right"><?php echo $TEXT['size-count2']; ?> = <?php echo $result_rows; ?></td>
                </tr>
                </tfoot>
               </table>
              </form>
          <?php
         }
         else
         {
          echo "<html><head>
              <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=add_autor.php?book_id=".$book_id."&amp;familiya=".$_POST['familiya']."' />
              </head></html>";
         }
//         else
//         {
//          $result_body_autor = mysqli_fetch_array($result_body);
//
//          if(!empty($result_body_autor['autor_book_id']) == $result_body_autor['chk_aut_id'])
//          {
//          echo "<html><head>
//              <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=add_autor.php?book_id=".$book_id."&amp;familiya=".$_POST['familiya']."' />
//              </head></html>";
//          }
//          else
//          {
//          $query = "INSERT INTO tbl_s_book_aut (`s_book_aut` , `autor_id` , `book_id` ) VALUES ( NULL , '".$result_body_autor['autor_book_id']."' , '$book_id')";
//          $result = mysqli_query($dbcnx,$query);
//           if(!$result) {puterror($result2['skin'],$TEXT['error-tbl3']); require_once "../utils/bottom_adm.php"; exit();}
//
//          echo "<html><head>
//              <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=add_autor.php?book_id=".$book_id."&amp;familiya=".$_POST['familiya']."' />
//              </head></html>";
//          }
//
//         }
  require_once "../utils/bottom_adm.php";
?>