<?php
  ///////////////////////////////////////////////////
  //перемещение строк вверх/вниз
  //2009 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////
  require_once "utils/chlang_osn.php";

  $title = $TEXT['menu-btn35-title'];
  require_once "config_a.php";
  require_once "utils/functions.php";
  require_once "utils/top.php";

  if($result2['administrator'] == 0) {puterror2($result2['skin'],$TEXT['error-loginform2']); require_once "utils/bottom.php"; exit();}
  if(strlen($_GET['book_id'])>255) {puterror2($result2['skin'],$TEXT['error-book6']); require_once "utils/bottom.php"; exit();}
  if(strlen($_GET['cicle_id'])>5) {puterror2($result2['skin'],$TEXT['error-cicle6']); require_once "utils/bottom.php"; exit();}
  if(empty($_GET['position'])) {puterror2($result2['skin'],$TEXT['error-booklang11']); require_once "utils/bottom.php"; exit();}
  if(strlen($_GET['position'])>5) {puterror2($result2['skin'],$TEXT['error-booklang10']); require_once "utils/bottom.php"; exit();}
  if(empty($_GET['moving'])) {puterror2($result2['skin'],$TEXT['error-booklang12']); require_once "utils/bottom.php"; exit();}
  if(strlen($_GET['moving'])>5) {puterror2($result2['skin'],$TEXT['error-booklang13']); require_once "utils/bottom.php"; exit();}

  $_GET['position'] = rep($_GET['position']);
  $_GET['book_id'] = rep($_GET['book_id']);
  $_GET['cicle_id'] = rep($_GET['cicle_id']);
  $_GET['moving'] = rep($_GET['moving']);

  if($_GET['moving']=="up"){
  $query_move = "SELECT book_id, book_num FROM `tbl_book` WHERE `book_num`<'".$_GET['position']."' AND `cicle_id`='".$_GET['cicle_id']."' ORDER BY `book_num` DESC LIMIT 1 ";
  $result_move = mysqli_query($dbcnx,$query_move);
  if(!$result_move) {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "utils/bottom.php"; exit();}
   $result2_move = mysqli_fetch_array($result_move);
   if($result2_move['book_num'] == 0) {puterror2($result2['skin'],$TEXT['error-booklang14']); require_once "utils/bottom.php"; exit();}

   $query = "UPDATE `tbl_book` SET `book_num`='".$result2_move['book_num']."' WHERE `book_id`='".$_GET['book_id']."' ";
   if(mysqli_query($dbcnx,$query))
   {
   $query = "UPDATE `tbl_book` SET `book_num`='".$_GET['position']."' WHERE `book_id`='".$result2_move['book_id']."' ";
     if(mysqli_query($dbcnx,$query))
      {
       echo "<html><head>
          <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=extsearch.php?cicle_id=".$_GET['cicle_id']."&cicle_ch=on' />
          </head></html>";
       } else {puterror2($result2['skin'],$TEXT['error-booklang14']); require_once "utils/bottom.php"; exit();}
   } else {puterror2($result2['skin'],$TEXT['error-booklang14']); require_once "utils/bottom.php"; exit();}
  }
  else
  {
  $query_move = "SELECT book_id, book_num FROM `tbl_book` WHERE `book_num`>'".$_GET['position']."' AND `cicle_id`='".$_GET['cicle_id']."' ORDER BY `book_num` LIMIT 1";
  $result_move = mysqli_query($dbcnx,$query_move);
  if(!$result_move) {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "utils/bottom.php"; exit();}
   $result2_move = mysqli_fetch_array($result_move);
  if($result2_move['book_num'] == 0) {puterror2($result2['skin'],$TEXT['error-booklang14']); require_once "utils/bottom.php"; exit();}

  $query = "UPDATE `tbl_book` SET `book_num`='".$_GET['position']."' WHERE `book_id`='".$result2_move['book_id']."' ";
  if(mysqli_query($dbcnx,$query))
  {
   $query = "UPDATE `tbl_book` SET `book_num`='".$result2_move['book_num']."' WHERE `book_id`='".$_GET['book_id']."' ";
     if(mysqli_query($dbcnx,$query))
      {
       echo "<html><head>
          <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=extsearch.php?cicle_id=".$_GET['cicle_id']."&cicle_ch=on' />
          </head></html>";
        } else {puterror2($result2['skin'],$TEXT['error-booklang14']); require_once "utils/bottom.php"; exit();}
    } else {puterror2($result2['skin'],$TEXT['error-booklang14']); require_once "utils/bottom.php"; exit();}
  }
  require_once "utils/bottom.php";
?>