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

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

  $title = $TEXT['book-title2'];
  $button = $TEXT['btn_filter'];
  $action = "add_filtr_autor.php";
  $button2 = $TEXT['btn_add'];
  $action2 = "add_autor_run.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'];
}

?>
<form enctype='multipart/form-data' action=<?php echo $action; ?> method=post >
<table align="right" border="0" cellpadding="0" cellspacing="0" width="98%">
<tr>
<td align="right"><input size="20" maxlength="255" type=text name=familiya title='<?php echo $TEXT['login-familiya']; ?>' value='<?php echo $familiya; ?>'></td>
<td width="2%" align="right">
 <input class="button2" type="submit" value='<?php echo $button; ?>' />
 <input type=hidden name=book_id value='<?php echo $book_id; ?>' />
</td>
</form>
<form enctype='multipart/form-data' action=<?php echo $action2; ?> method=post >
<td width="2%" 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>
<table align='center' border='0' cellpadding='2' cellspacing='0' width='98%'>
<?php
  $query_body1 = "SELECT DISTINCT first_bt AS bukva FROM `tbl_autor` ORDER BY bukva ";
   $result_body1 = mysqli_query($dbcnx,$query_body1);
    if(!$result_body1) {puterror($result2['skin'],$TEXT['error-tbl1']); require_once "utils/bottom.php"; exit();}
     while($result_body_autor1 = mysqli_fetch_array($result_body1))
      {
      echo "<tr><td colspan='5' class='zagolovok_und'>";
      echo "&nbsp;".$result_body_autor1['bukva']."&nbsp;";
      echo "</td></tr>";
      $td = 0;

     $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
     WHERE tbl_autor.first_bt = '".$result_body_autor1['bukva']."'
     ORDER BY tbl_autor.name, 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)
         {
          $colsp = "";
           while($result_body_autor = mysqli_fetch_array($result_body))
               {
//               $tmp_col = $result_rows/5;
//               if($tmp_col > 1)
//               {
//                $tmp_col = (ceil($tmp_col)*5-$result_rows)+1;
//                $tmp_col2 = ceil($tmp_col)*5-$result_rows;
//                if($tmp_col == 1) $colsp = "";
//                if($tmp_col2 == $td) $colsp = "colspan='".$tmp_col."'";
//
//               }
//               else
//               {
               if($result_rows == 1 && $td == 0) {$colsp = "colspan='5'";}
               if($result_rows == 2 && $td == 1) {$colsp = "colspan='4'";}
               if($result_rows == 3 && $td == 2) {$colsp = "colspan='3'";}
               if($result_rows == 4 && $td == 3) {$colsp = "colspan='2'";}
//               }
               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";
                $usr_name = "<a class='informlft' href=info_autor.php?autor_id=".$result_body_autor['autor_book_id']."&amp;book_id=".$book_id."".$fam." title='".$TEXT['aut-viewtit']."'>".$result_body_autor['name']."&nbsp;".$result_body_autor['otchestvo']."&nbsp;".$result_body_autor['familiya']."</a>";
               }
                 else 
                 {
                  $aut_est = "";
                  $usr_name = "<a href=info_autor.php?autor_id=".$result_body_autor['autor_book_id']."&amp;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'];
              if($td == 0)
              {
               echo "<tr>";
              }
              if($td < 5)
               {
             ?>
              <td <?php echo $colsp; ?>><input type="checkbox" name=autor[<?php echo $aut_id; ?>] <?php echo $aut_est ; ?> />&nbsp;<?php echo $usr_name; ?></td>
           <?php
               $td++;
               }
              if($td == 5)
               {
               echo "</tr>";
               $td = 0;
               }
               
             }
         }
         
         if($td > 0 && $td < 5)
          {
           echo "</tr>";
          }
          echo "<tr><td colspan='5'><hr align='left' width='98%' /></td></tr>";
        }
?>
</table>
</form>
<?php
  require_once "../utils/bottom_adm.php";
?>