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

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if (is_file ("../lang/".$lang."/".$lang.".php")) include "../lang/".$lang."/".$lang.".php";
     else include "../lang/ru/ru.php";

  $title=$titlepage = $TEXT['org-title3'];

  require_once "../config.php";
  require_once "../util/top.php";

  if($result2['master'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['manager'] == 1) puterror2($TEXT['error-loginform4']);

  if(empty($_GET['user_id'])) puterror2($TEXT['error-nd16']);
  if(strlen($_GET['user_id'])>11) puterror2($TEXT['error-nd13']);
  if(empty($_GET['position'])) puterror2($TEXT['error-nd4']);
  if(strlen($_GET['position'])>5) puterror2($TEXT['error-nd5']);
  if(empty($_GET['moving'])) puterror2($TEXT['error-otd4']);
  if(strlen($_GET['moving'])>5) puterror2($TEXT['error-otd5']);
  if(strlen($_GET['org'])>400) puterror2($TEXT['error-org4']);
  if(strlen($_GET['upr'])>400) puterror2($TEXT['error-upr4']);
  if(strlen($_GET['otd'])>280) puterror2($TEXT['error-otd3']);
  if(strlen($_GET['big'])>1) puterror2($TEXT['usr-big-err']);

  $_GET['position'] = rep($_GET['position']);
  $_GET['user_id'] = rep($_GET['user_id']);
  $_GET['moving'] = rep($_GET['moving']);
  $_GET['org'] = rep($_GET['org']);
  $_GET['upr'] = rep($_GET['upr']);
  $_GET['otd'] = rep($_GET['otd']);

  if(!empty($_GET['org']))
  {
  $query_organizaciya1 = "SELECT * FROM `tbl_organizaciya` WHERE `organizaciya`='".$_GET['org']."' ";
    $result_org1 = mysql_query($query_organizaciya1);
    if (!$result_org1) puterror2($TEXT['error-tbl7']);
     $result_organizaciya1 = mysql_fetch_array($result_org1);
     $org = "AND org_id = '".$result_organizaciya1['org_id']."' ";
  } else $org = "";

  if(!empty($_GET['upr']))
  {
  $query_upravlenie1 = "SELECT * FROM `tbl_upravlenie` WHERE `upravlenie`='".$_GET['upr']."' ";
    $result_upr1 = mysql_query($query_upravlenie1);
     if (!$result_upr1) puterror2($TEXT['error-tbl9']);
      $result_upravlenie1 = mysql_fetch_array($result_upr1);
      $upr = "AND upr_id = '".$result_upravlenie1['upr_id']."' ";
  } else $upr = "";

  if(!empty($_GET['otd']))
  {
  $query_otdel1 = "SELECT * FROM `tbl_otdel` WHERE `otdel`='".$_GET['otd']."' ";
    $result_otd1 = mysql_query($query_otdel1);
     if (!$result_otd1) puterror2($TEXT['error-tbl8']);
      $result_otdel1 = mysql_fetch_array($result_otd1);
      $otd = "AND otd_id = '".$result_otdel1['otd_id']."' ";
  } else $otd = "";

  if($_GET['moving']=="up"){
  $query_move = "SELECT user_id, org_id, otd_id, upr_id, position FROM `tbl_users` WHERE `position`<'".$_GET['position']."' $org $upr $otd ORDER BY `position` DESC LIMIT 1 ";
  $result_move = mysql_query($query_move);
  if (!$result_move) puterror2($TEXT['error-tbl2']);
   $result2_move = mysql_fetch_array($result_move);
   if($result2_move['position'] == 0) puterror2($TEXT['global-up-down']);

   $query = "UPDATE `tbl_users` SET `position`='".$result2_move['position']."' WHERE `user_id`='".$_GET['user_id']."' ";
   if(mysql_query($query))
   {
   $query = "UPDATE `tbl_users` SET `position`='".$_GET['position']."' WHERE `user_id`='".$result2_move['user_id']."' ";
     if(mysql_query($query))
      {
       echo "<html><head>
          <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php?org=".$_GET['org']."&amp;upr=".$_GET['upr']."&amp;otd=".$_GET['otd']."&amp;big=".$_GET['big']."' />
          </head></html>";
      } else puterror2($TEXT['error-nd21']);
   } else puterror2($TEXT['error-nd21']);
  }
  else
  {
  $query_move = "SELECT user_id, org_id, position FROM `tbl_users` WHERE `position`>'".$_GET['position']."' $org $upr $otd ORDER BY `position` LIMIT 1";
  $result_move = mysql_query($query_move);
  if (!$result_move) puterror2($TEXT['error-tbl2']);
   $result2_move = mysql_fetch_array($result_move);
  if($result2_move['position'] == 0) puterror2($TEXT['global-up-down']);

  $query = "UPDATE `tbl_users` SET `position`='".$_GET['position']."' WHERE `user_id`='".$result2_move['user_id']."' ";
  if(mysql_query($query))
  {
   $query = "UPDATE `tbl_users` SET `position`='".$result2_move['position']."' WHERE `user_id`='".$_GET['user_id']."' ";
     if(mysql_query($query))
      {
       echo "<html><head>
          <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php?org=".$_GET['org']."&amp;upr=".$_GET['upr']."&amp;otd=".$_GET['otd']."&amp;big=".$_GET['big']."' />
          </head></html>";
      } else puterror2($TEXT['error-nd21']);
    } else puterror2($TEXT['error-nd21']);
  }
  require_once "../util/bottom.php";
?>
