<?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']) {
  array_walk($_POST, 'SiteLibs::ConvertArrayIconv', $MS_SP->Sp_Config['charset']);
  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']) && true === SiteLibs::RuleCheckSqlTextField($_POST['query'])){
    $VarWhere = SiteLibs::StripAndOrFirst(SiteLibs::ConvertStringToSql($_POST['qtype'], $_POST['query']));
    //echo "VarWhere: ". $VarWhere ."<br />";
  }
  $start = (($page-1) * $rp);

  $limit = "LIMIT $start, $rp";

  $ObjNewsMod = new NewsMod($MS_SP);
  $VarOrder = "$sortname $sortorder";
  //$VarOrder = "";
  $VarLimit = "$start, $rp";
  $total = $ObjNewsMod->GetFromDb('SqlCount', 'GetOne', $VarWhere);
  $ArrNews = $ObjNewsMod->GetFromDb('SqlAll', 'GetAll', $VarWhere, $VarOrder, $VarLimit);
//SiteLibs::GetVarDump($ArrObond);

  $ArrFields = array();
  foreach ($ArrNews AS $key => $row){
    $ArrFields[$key]['id'] = $row['news_id'];
    $ArrFields[$key]['cell'] = array(
      SiteLibs::ConvertSqlDateToText($row['news_date']).'',
      SiteLibs::ConvertArrayIconvBack($row['news_text'], '', $MS_SP->Sp_Config['charset']).''
    );
  }
  $json = array("page" => $page, "total" => $total, "rows" => $ArrFields);
  echo json_encode($json);
}
else{
  echo json_encode(array('error' => SiteLibs::ConvertArrayIconvBack($MS_SP->Sp_LangMessage['error']['ErrFuncAccessDenied'], '', $MS_SP->Sp_Config['charset'])));
}
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>
