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

  if(is_file ("lang/".$lang."/trans.php")) {require_once "lang/".$lang."/trans.php";}
   else {require_once "lang/ru/trans.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']);

  // add 22.06.2021
  $bnt = substr($_GET['position'],0,1);
  if($bnt == '0')
    {
     $book_n = substr($_GET['position'],1,1);
     if($book_n == '0')
       {
        $book_num = substr($_GET['position'],2,3);
       } else {$book_num = substr($_GET['position'],1,3);}
    } else {$book_num = $_GET['position'];}
  
  $query111 = "SELECT `cicle` FROM `tbl_cicle` WHERE `cicle_id`='".$_GET['cicle_id']."' LIMIT 1";
  $result111 = mysqli_query($dbcnx,$query111);
    if(!$result111) {puterror2($result2['skin'],$TEXT['error-tbl15']); require_once "utils/bottom.php"; exit();}
      $result_prev_books = mysqli_fetch_array($result111);
      if($result_prev_books['cicle'] != '' && $result_prev_books['cicle'] != '---') {$book_cicle_name = translate("_".rep_back($result_prev_books['cicle'])."_");}
        else {$book_cicle_name = "_";}
        
  $query_move_src = "SELECT `book_id`, `book_num`, `book_name`, `a_book`, `cd_book`, `e_book` FROM `tbl_book` WHERE `book_id`='".$_GET['book_id']."' LIMIT 1 ";
  $result_move_src = mysqli_query($dbcnx,$query_move_src);
  if(!$result_move_src) {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "utils/bottom.php"; exit();}
   $result2_move_src = mysqli_fetch_array($result_move_src);
   if($result2_move_src['book_num'] == 0) {puterror2($result2['skin'],$TEXT['error-booklang14']); require_once "utils/bottom.php"; exit();}

  // end add 22.06.2021

  if($_GET['moving']=="up"){
  $query_move = "SELECT `book_id`, `book_num`, `book_name`, `a_book`, `cd_book`, `e_book` 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();}
   
  $bnt_up = substr($result2_move['book_num'],0,1);
  if($bnt_up == '0')
    {
     $book_n_up = substr($result2_move['book_num'],1,1);
     if($book_n_up == '0')
       {
        $book_num_up = substr($result2_move['book_num'],2,3);
       } else {$book_num_up = substr($result2_move['book_num'],1,3);}
    } else {$book_num_up = $result2_move['book_num'];}

      //Проверим можно-ли перенименовать исходную печатную книгу
      $fl_ebook_src = substr($result2_move_src['e_book'],3,255);
      if(!empty($result2_move_src['e_book']))
        {
        $date_for_rne = "_".date("YmdHis",time());
        $ebook_name_now_src = substr(strrchr($result2_move_src['e_book'], "/"), 1);
        $ext_rne_src = strrchr($ebook_name_now_src, ".");
        $trans_book_rne_src = translate($result2_move_src['book_name']);
        $name2_rne_src = "book".$book_cicle_name.$book_num_up."_".$trans_book_rne_src;
        $ebook_nrne_src = "ebooks/".$name2_rne_src.$ext_rne_src;
        $rename_ebook_src = "ebooks/".$name2_rne_src.$date_for_rne.$ext_rne_src;
        if(is_file($fl_ebook_src)) 
          {
          if(is_file($ebook_nrne_src)) 
          {
              rename($fl_ebook_src,$rename_ebook_src); 
              $ebook_to_db_src = "../".$rename_ebook_src;
          }
            else 
            {
                rename($fl_ebook_src,$ebook_nrne_src); 
                $ebook_to_db_src = "../".$ebook_nrne_src;
            }
          }
        }
        else {$to_log_ebook_ch_filename = "";}
      //Конец блока переименования файла печатной книги

      //Проверим можно-ли перенименовать исходную аудио книгу
      $fl_abook_src = substr($result2_move_src['a_book'],3,255);
      if(!empty($result2_move_src['a_book']))
        {
        $date_for_rna = "_".date("YmdHis",time());
        $abook_name_now_src = substr(strrchr($result2_move_src['a_book'], "/"), 1);
        $ext_rna_src = strrchr($abook_name_now_src, ".");
        $trans_book_rna_src = translate($result2_move_src['book_name']);
        $name2_rna_src = "audio_book".$book_cicle_name.$book_num_up."_".$trans_book_rna_src;
        $abook_nrna_src = "abooks/".$name2_rna_src.$ext_rna_src;
        $rename_abook_src = "abooks/".$name2_rna_src.$date_for_rna.$ext_rna_src;
        if(is_file($fl_abook_src)) 
          {
          if(is_file($abook_nrna_src)) 
          {
              rename($fl_abook_src,$rename_abook_src); 
              $abook_to_db_src = "../".$rename_abook_src;
          }
            else 
            {
                rename($fl_abook_src,$abook_nrna_src); 
                $abook_to_db_src = "../".$abook_nrna_src;
            }
          }
        }
        else {$to_log_abook_ch_filename = "";}
      //Конец блока переименования файла аудио книги

      //Проверим можно-ли перенименовать исходную CD-rom книгу
      $fl_cdbook_src = substr($result2_move_src['cd_book'],3,255);
      if(!empty($result2_move_src['cd_book']))
        {
        $date_for_rncd = "_".date("YmdHis",time());
        $cdbook_name_now_src = substr(strrchr($result2_move_src['cd_book'], "/"), 1);
        $ext_rncd_src = strrchr($cdbook_name_now_src, ".");
        $trans_book_rncd_src = translate($result2_move_src['book_name']);
        $name2_rncd_src = "cdrom_book".$book_cicle_name.$book_num_up."_".$trans_book_rncd_src;
        $cdbook_nrncd_src = "cdbooks/".$name2_rncd_src.$ext_rncd_src;
        $rename_cdbook_src = "cdbooks/".$name2_rncd_src.$date_for_rncd.$ext_rncd_src;
        if(is_file($fl_cdbook_src)) 
          {
          if(is_file($cdbook_nrncd_src)) 
          {
              rename($fl_cdbook_src,$rename_cdbook_src); 
              $cdbook_to_db_src = "../".$rename_cdbook_src;
          }
            else 
            {
                rename($fl_cdbook_src,$cdbook_nrncd_src); 
                $cdbook_to_db_src = "../".$cdbook_nrncd_src;
            }
          }
        }
        else {$to_log_cdbook_ch_filename = "";}
      //Конец блока переименования файла CD-rom книги

   $query1 = "UPDATE `tbl_book` SET `book_num`='".$result2_move['book_num']."', `e_book`='".$ebook_to_db_src."', `a_book`='".$abook_to_db_src."', `cd_book`='".$cdbook_to_db_src."' WHERE `book_id`='".$_GET['book_id']."' ";
   if(mysqli_query($dbcnx,$query1))
   {
      //Проверим можно-ли переименовать зависимую печатную книгу
      $fl_ebook_prnt = substr($result2_move['e_book'],3,255);
      if(!empty($result2_move['e_book']))
        {
        $date_for_rne = "_".date("YmdHis",time());
        $ebook_name_now = substr(strrchr($result2_move['e_book'], "/"), 1);
        $ext_rne = strrchr($ebook_name_now, ".");
        $trans_book_rne = translate($result2_move['book_name']);
        $name2_rne = "book".$book_cicle_name.$book_num."_".$trans_book_rne;
        $ebook_nrne = "ebooks/".$name2_rne.$ext_rne;
        $rename_ebook = "ebooks/".$name2_rne.$date_for_rne.$ext_rne;
        if(is_file($fl_ebook_prnt)) 
          {
          if(is_file($ebook_nrne)) 
          {
              rename($fl_ebook_prnt,$rename_ebook); 
              $ebook_to_db_prnt = "../".$rename_ebook;
          }
            else 
            {
                rename($fl_ebook_prnt,$ebook_nrne); 
                $ebook_to_db_prnt = "../".$ebook_nrne;
            }
          }
        } else {$to_log_ebook_ch_filename = "";}
      //Конец блока переименования файла печатной книги

      //Проверим можно-ли переименовать зависимую аудио книгу
      $fl_abook_prnt = substr($result2_move['a_book'],3,255);
      if(!empty($result2_move['a_book']))
        {
        $date_for_rna = "_".date("YmdHis",time());
        $abook_name_now = substr(strrchr($result2_move['a_book'], "/"), 1);
        $ext_rna = strrchr($abook_name_now, ".");
        $trans_book_rna = translate($result2_move['book_name']);
        $name2_rna = "audio_book".$book_cicle_name.$book_num."_".$trans_book_rna;
        $abook_nrna = "abooks/".$name2_rna.$ext_rna;
        $rename_abook = "abooks/".$name2_rna.$date_for_rna.$ext_rna;
        if(is_file($fl_abook_prnt)) 
          {
          if(is_file($abook_nrna)) 
          {
              rename($fl_abook_prnt,$rename_abook); 
              $abook_to_db_prnt = "../".$rename_abook;
          }
            else 
            {
                rename($fl_abook_prnt,$abook_nrna); 
                $abook_to_db_prnt = "../".$abook_nrna;
            }
          }
        } else {$to_log_abook_ch_filename = "";}
      //Конец блока переименования файла аудио книги

      //Проверим можно-ли переименовать зависимую CD-rom книгу
      $fl_cdbook_prnt = substr($result2_move['cd_book'],3,255);
      if(!empty($result2_move['cd_book']))
        {
        $date_for_rncd = "_".date("YmdHis",time());
        $cdbook_name_now = substr(strrchr($result2_move['cd_book'], "/"), 1);
        $ext_rncd = strrchr($cdbook_name_now, ".");
        $trans_book_rncd = translate($result2_move['book_name']);
        $name2_rncd = "cdrom_book".$book_cicle_name.$book_num."_".$trans_book_rncd;
        $cdbook_nrncd = "cdbooks/".$name2_rncd.$ext_rncd;
        $rename_cdbook = "cdbooks/".$name2_rncd.$date_for_rncd.$ext_rncd;
        if(is_file($fl_cdbook_prnt)) 
          {
          if(is_file($cdbook_nrncd)) 
          {
              rename($fl_cdbook_prnt,$rename_cdbook); 
              $cdbook_to_db_prnt = "../".$rename_cdbook;
          }
            else 
            {
                rename($fl_cdbook_prnt,$cdbook_nrncd); 
                $cdbook_to_db_prnt = "../".$cdbook_nrncd;
            }
          }
        } else {$to_log_cdbook_ch_filename = "";}
      //Конец блока переименования файла CD-rom книги

   $query2 = "UPDATE `tbl_book` SET `book_num`='".$_GET['position']."', `e_book`='".$ebook_to_db_prnt."', `a_book`='".$abook_to_db_prnt."', `cd_book`='".$cdbook_to_db_prnt."' WHERE `book_id`='".$result2_move['book_id']."' ";
     if(mysqli_query($dbcnx,$query2))
      {
       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, `book_name`, `a_book`, `cd_book`, `e_book` 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();}

      //Проверим можно-ли переименовать зависимую печатную книгу
      $fl_ebook_prnt = substr($result2_move['e_book'],3,255);
      if(!empty($result2_move['e_book']))
        {
        $date_for_rne = "_".date("YmdHis",time());
        $ebook_name_now = substr(strrchr($result2_move['e_book'], "/"), 1);
        $ext_rne = strrchr($ebook_name_now, ".");
        $trans_book_rne = translate($result2_move['book_name']);
        $name2_rne = "book".$book_cicle_name.$book_num."_".$trans_book_rne;
        $ebook_nrne = "ebooks/".$name2_rne.$ext_rne;
        $rename_ebook = "ebooks/".$name2_rne.$date_for_rne.$ext_rne;
        if(is_file($fl_ebook_prnt)) 
          {
          if(is_file($ebook_nrne)) 
          {
              rename($fl_ebook_prnt,$rename_ebook); 
              $ebook_to_db_prnt = "../".$rename_ebook;
          }
            else 
            {
                rename($fl_ebook_prnt,$ebook_nrne); 
                $ebook_to_db_prnt = "../".$ebook_nrne;
            }
          }
        } else {$to_log_ebook_ch_filename = "";}
      //Конец блока переименования файла печатной книги

      //Проверим можно-ли переименовать зависимую аудио книгу
      $fl_abook_prnt = substr($result2_move['a_book'],3,255);
      if(!empty($result2_move['a_book']))
        {
        $date_for_rna = "_".date("YmdHis",time());
        $abook_name_now = substr(strrchr($result2_move['a_book'], "/"), 1);
        $ext_rna = strrchr($abook_name_now, ".");
        $trans_book_rna = translate($result2_move['book_name']);
        $name2_rna = "audio_book".$book_cicle_name.$book_num."_".$trans_book_rna;
        $abook_nrna = "abooks/".$name2_rna.$ext_rna;
        $rename_abook = "abooks/".$name2_rna.$date_for_rna.$ext_rna;
        if(is_file($fl_abook_prnt)) 
          {
          if(is_file($abook_nrna)) 
          {
              rename($fl_abook_prnt,$rename_abook); 
              $abook_to_db_prnt = "../".$rename_abook;
          }
            else 
            {
                rename($fl_abook_prnt,$abook_nrna); 
                $abook_to_db_prnt = "../".$abook_nrna;
            }
          }
        } else {$to_log_abook_ch_filename = "";}
      //Конец блока переименования файла аудио книги

      //Проверим можно-ли переименовать зависимую CD-rom книгу
      $fl_cdbook_prnt = substr($result2_move['cd_book'],3,255);
      if(!empty($result2_move['cd_book']))
        {
        $date_for_rncd = "_".date("YmdHis",time());
        $cdbook_name_now = substr(strrchr($result2_move['cd_book'], "/"), 1);
        $ext_rncd = strrchr($cdbook_name_now, ".");
        $trans_book_rncd = translate($result2_move['book_name']);
        $name2_rncd = "cdrom_book".$book_cicle_name.$book_num."_".$trans_book_rncd;
        $cdbook_nrncd = "cdbooks/".$name2_rncd.$ext_rncd;
        $rename_cdbook = "cdbooks/".$name2_rncd.$date_for_rncd.$ext_rncd;
        if(is_file($fl_cdbook_prnt)) 
          {
          if(is_file($cdbook_nrncd)) 
          {
              rename($fl_cdbook_prnt,$rename_cdbook); 
              $cdbook_to_db_prnt = "../".$rename_cdbook;
          }
            else 
            {
                rename($fl_cdbook_prnt,$cdbook_nrncd); 
                $cdbook_to_db_prnt = "../".$cdbook_nrncd;
            }
          }
        } else {$to_log_cdbook_ch_filename = "";}
      //Конец блока переименования файла CD-rom книги

  $query = "UPDATE `tbl_book` SET `book_num`='".$_GET['position']."', `e_book`='".$ebook_to_db_prnt."', `a_book`='".$abook_to_db_prnt."', `cd_book`='".$cdbook_to_db_prnt."'  WHERE `book_id`='".$result2_move['book_id']."' ";
  if(mysqli_query($dbcnx,$query))
  {
   
  $bnt_up = substr($result2_move['book_num'],0,1);
  if($bnt_up == '0')
    {
     $book_n_up = substr($result2_move['book_num'],1,1);
     if($book_n_up == '0')
       {
        $book_num_up = substr($result2_move['book_num'],2,3);
       } else {$book_num_up = substr($result2_move['book_num'],1,3);}
    } else {$book_num_up = $result2_move['book_num'];}

      //Проверим можно-ли перенименовать исходную печатную книгу
      $fl_ebook_src = substr($result2_move_src['e_book'],3,255);
      if(!empty($result2_move_src['e_book']))
        {
        $date_for_rne = "_".date("YmdHis",time());
        $ebook_name_now_src = substr(strrchr($result2_move_src['e_book'], "/"), 1);
        $ext_rne_src = strrchr($ebook_name_now_src, ".");
        $trans_book_rne_src = translate($result2_move_src['book_name']);
        $name2_rne_src = "book".$book_cicle_name.$book_num_up."_".$trans_book_rne_src;
        $ebook_nrne_src = "ebooks/".$name2_rne_src.$ext_rne_src;
        $rename_ebook_src = "ebooks/".$name2_rne_src.$date_for_rne.$ext_rne_src;
        if(is_file($fl_ebook_src)) 
          {
          if(is_file($ebook_nrne_src)) 
          {
              rename($fl_ebook_src,$rename_ebook_src); 
              $ebook_to_db_src = "../".$rename_ebook_src;
          }
            else 
            {
                rename($fl_ebook_src,$ebook_nrne_src); 
                $ebook_to_db_src = "../".$ebook_nrne_src;
            }
          }
        }
        else {$to_log_ebook_ch_filename = "";}
      //Конец блока переименования файла печатной книги

      //Проверим можно-ли перенименовать исходную аудио книгу
      $fl_abook_src = substr($result2_move_src['a_book'],3,255);
      if(!empty($result2_move_src['a_book']))
        {
        $date_for_rna = "_".date("YmdHis",time());
        $abook_name_now_src = substr(strrchr($result2_move_src['a_book'], "/"), 1);
        $ext_rna_src = strrchr($abook_name_now_src, ".");
        $trans_book_rna_src = translate($result2_move_src['book_name']);
        $name2_rna_src = "audio_book".$book_cicle_name.$book_num_up."_".$trans_book_rna_src;
        $abook_nrna_src = "abooks/".$name2_rna_src.$ext_rna_src;
        $rename_abook_src = "abooks/".$name2_rna_src.$date_for_rna.$ext_rna_src;
        if(is_file($fl_abook_src)) 
          {
          if(is_file($abook_nrna_src)) 
          {
              rename($fl_abook_src,$rename_abook_src); 
              $abook_to_db_src = "../".$rename_abook_src;
          }
            else 
            {
                rename($fl_abook_src,$abook_nrna_src); 
                $abook_to_db_src = "../".$abook_nrna_src;
            }
          }
        }
        else {$to_log_abook_ch_filename = "";}
      //Конец блока переименования файла аудио книги

      //Проверим можно-ли перенименовать исходную CD-rom книгу
      $fl_cdbook_src = substr($result2_move_src['cd_book'],3,255);
      if(!empty($result2_move_src['cd_book']))
        {
        $date_for_rncd = "_".date("YmdHis",time());
        $cdbook_name_now_src = substr(strrchr($result2_move_src['cd_book'], "/"), 1);
        $ext_rncd_src = strrchr($cdbook_name_now_src, ".");
        $trans_book_rncd_src = translate($result2_move_src['book_name']);
        $name2_rncd_src = "cdrom_book".$book_cicle_name.$book_num_up."_".$trans_book_rncd_src;
        $cdbook_nrncd_src = "cdbooks/".$name2_rncd_src.$ext_rncd_src;
        $rename_cdbook_src = "cdbooks/".$name2_rncd_src.$date_for_rncd.$ext_rncd_src;
        if(is_file($fl_cdbook_src)) 
          {
          if(is_file($cdbook_nrncd_src)) 
          {
              rename($fl_cdbook_src,$rename_cdbook_src); 
              $cdbook_to_db_src = "../".$rename_cdbook_src;
          }
            else 
            {
                rename($fl_cdbook_src,$cdbook_nrncd_src); 
                $cdbook_to_db_src = "../".$cdbook_nrncd_src;
            }
          }
        }
        else {$to_log_cdbook_ch_filename = "";}
      //Конец блока переименования файла CD-rom книги

   $query = "UPDATE `tbl_book` SET `book_num`='".$result2_move['book_num']."', `e_book`='".$ebook_to_db_src."', `a_book`='".$abook_to_db_src."', `cd_book`='".$cdbook_to_db_src."'  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";
?>