<?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']['id']) && true === is_numeric($_SESSION['SessionAuth']['id'])) {
  //SiteLibs::GetVarDump($_POST);
  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']));
  }
  
  if(true === isset($_POST['UrlUsersId']) && true === is_numeric($_POST['UrlUsersId']) && 0 < $_POST['UrlUsersId']){
    if(true === isset($VarWhere) && '' != $VarWhere){
      $VarWhere .= ' AND ';
    }
    $VarWhere .= "a.users_id_to = '". $_POST['UrlUsersId'] ."'";
  }

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

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

  $VarLimit = "$start, $rp";

  $ObjModMessage = new MessageMod(&$MS_SP);
  $total = $ObjModMessage->GetFromDb('SqlCount', 'GetOne', $VarWhere);
  $ArrMessage = $ObjModMessage->GetFromDb('SqlAll', 'GetAll', $VarWhere, $VarOrder, $VarLimit);

  $ArrFields = array();
  foreach ($ArrMessage AS $key => $row){
    if(true === isset($row['message_date_familiarize']) && '' != $row['message_date_familiarize']){
      $VarId = '<span style="background-color: #6dc462;">'. $row['message_id'] .'</span>';
    }
    else{
      $VarId = '<span style="background-color: #ff0000;">'. $row['message_id'] .'</span>';
    }
    $ArrFields[$key]['id'] = $row['message_id'];
    $ArrFields[$key]['cell'] = array(
      '<input type="checkbox" id="RowId'. $row['message_id'] .'" value="1" checked="checked" />',
      $VarId,
      SiteLibs::ConvertArrayIconvBack($row['users_fio_to'], '', $MS_SP->Sp_Config['charset']).'',
      SiteLibs::ConvertArrayIconvBack($row['users_fio_from'], '', $MS_SP->Sp_Config['charset']).'',
      SiteLibs::ConvertSqlDatetimeToText($row['message_date_send']).'',
      SiteLibs::ConvertSqlDatetimeToText($row['message_date_familiarize']).'',
      SiteLibs::ConvertSqlDatetimeToText($row['users_lasttime_to']).'',
      SiteLibs::ConvertArrayIconvBack($row['message_text'], '', $MS_SP->Sp_Config['charset']).''
    );
  }
  $json = array("page" => $page, "total" => $total, "rows" => $ArrFields);
  echo json_encode($json);
}
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>