<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/x-json");
include($_SERVER['DOCUMENT_ROOT'] .'/script/HeaderInc.php');
if('XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH'] && 
  true === isset($_SESSION['SessionAuth']['IpsAdmin']['reg']) && 1 == $_SESSION['SessionAuth']['IpsAdmin']['reg']){
  array_walk($_POST, 'SiteLibs::ConvertArrayIconv', $MS_SP->Sp_Config['charset']);
  //SiteLibs::GetVarDump($_POST);
  if (true === isset($_POST['page']) && true === is_numeric($_POST['page'])){
    $page = $_POST['page'];
  }
  else{
    $page = 1;
  }
  if (true === isset($_POST['rp']) && true === is_numeric($_POST['rp'])){
    $rp = $_POST['rp'];
  }
  else{
    $rp = 10;
  }
  if (true === isset($_POST['sortname']) && SiteLibs::RuleCheckSqlTextField($_POST['sortname'])){
    $sortname = $_POST['sortname'];
  }
  else{
    $sortname = 1;
  }
  if (true === isset($_POST['sortorder']) && ('desc' == $_POST['sortorder'] || 'asc' == $_POST['sortorder'])){
    $sortorder = $_POST['sortorder'];
  }
  else{
    $sortorder = 'desc';
  }
  $VarWhere = '';
  if (true === isset($_POST['qtype']) && true === SiteLibs::RuleCheckSqlTextField($_POST['qtype']) && true === isset($_POST['query']) && '' != $_POST['query']){
    $VarWhere .= SiteLibs::StripAndOrFirst(SiteLibs::ConvertStringToSql($_POST['qtype'], $_POST['query']));
  }

  $start = (($page-1) * $rp);

  $VarOrder = $sortname ." ". $sortorder;

  $VarLimit = "$start, $rp";

  require_once($_SERVER['DOCUMENT_ROOT'] .'/script/Country.mod.php');
  $ObjCountryMod = new CountryMod($MS_SP);
  $total = $ObjCountryMod->GetFromDb('SqlCount', 'GetOne', $VarWhere);
  $ArrCountry = $ObjCountryMod->GetFromDb('SqlAll', 'GetAll', $VarWhere, $VarOrder, $VarLimit);

  $ArrFields = array();
  foreach ($ArrCountry AS $key => $row){
    $ArrFields[$key]['id'] = $row['country_id'];
    $ArrFields[$key]['cell'] = array(
      SiteLibs::ConvertArrayIconvBack($row['country_name'], '', $MS_SP->Sp_Config['charset']).'',
      SiteLibs::ConvertArrayIconvBack($row['country_nameshort'], '', $MS_SP->Sp_Config['charset']).''
    );
  }
  $json = array("page" => $page, "total" => $total, "rows" => $ArrFields);
  echo json_encode($json);
}
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>