<?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($_POST['demands_id']) && true === is_numeric($_POST['demands_id'])) {
  $ObjDemandsCommentsMod = new DemandsCommentsMod($MS_SP);
  if (true === isset($_POST['demandscommentsparent_id']) && true === is_numeric($_POST['demandscommentsparent_id'])){
    $VarDemandsCommentsId = $ObjDemandsCommentsMod->Add($_POST);
    if(true === is_numeric($VarDemandsCommentsId)){
      $ArrReturn = array('message' => $MS_SP->Sp_LangMessage['message']['MessRecordAddSuccess']);
    }
    else{
      $ArrReturn = $VarDemandsCommentsId;
    }
  }
  elseif (true === isset($_POST['demandscomments_id']) && true === is_numeric($_POST['demandscomments_id'])){
    $VarDemandsCommentsId = $ObjDemandsCommentsMod->Edit($_POST);
    if(true === is_numeric($VarDemandsCommentsId)){
      $ArrReturn = array('message' => $MS_SP->Sp_LangMessage['message']['MessRecordEditSuccess']);
    }
    else{
      $ArrReturn = $VarDemandsCommentsId;
    }
  }
  else{
    $ArrReturn = array('error' => $MS_SP->Sp_LangMessage['error']['ErrSettingWrong']);
  }
  if(true === is_numeric($VarDemandsCommentsId)){
    $ObjDemandsMod = new DemandsMod($MS_SP);
    $ArrDemands = $ObjDemandsMod->GetFromDb('SqlAll', 'GetRow', "a.demands_id = '". $_POST['demands_id'] ."'");
    $smarty->assign('ArrDemands', $ArrDemands);
    $ArrDemandsComments = $ObjDemandsCommentsMod->GetFromDb('SqlAll', 'GetRow', "a.demandscomments_id = '". $VarDemandsCommentsId ."'");
    $smarty->assign('ArrDemandsComments', $ArrDemandsComments);
    //SiteLibs::GetVarDump($VarDemandsId);
    //  SiteLibs::GetVarDump($ArrDemandsComments);

    $VarHtml = $smarty->fetch('Demands/SendDemands.tpl');

    $ObjUsersMod = new UsersMod($MS_SP);
    if(true === isset($MS_SP->Sp_Config['SendDemandsToOperatorsFlag']) && 1 == $MS_SP->Sp_Config['SendDemandsToOperatorsFlag']){
      $VarWhere = "(us.webservices_id = 1 || us.webservices_id = 2 || us.webservices_id = 3)";
    }
    else{
      $VarWhere = "(us.webservices_id = 1)";
    }
    $ArrUsers = $ObjUsersMod->GetFromDb('SqlServices', 'GetAll', $VarWhere);
    if(true === isset($MS_SP->Sp_Config['SendDemandsToUsersFlag']) && 1 == $MS_SP->Sp_Config['SendDemandsToUsersFlag'] && true === isset($ArrDemands['users_id']) && true === is_numeric($ArrDemands['users_id'])){
      $ArrUsers[] = $ObjUsersMod->GetFromDb('SqlAll', 'GetRow', 'u.users_id = '. $ArrDemands['users_id']);
    }

    if ('development' != @$_SERVER['APPLICATION_ENV']){
      $ObjMailMod = new MailMod($MS_SP);
      $ObjMailMod->AddressFrom = $MS_SP->Sp_Config['SendDemandsToOperatorsFrom'];
      $ObjMailMod->AddressSubject = $MS_SP->Sp_Config['SendDemandsToOperatorsSubject'];
      $ObjMailMod->AddressMailServer = $MS_SP->Sp_Config['AddressMailServer'];
      foreach ($ArrUsers as $key => $value) {
        $ObjMailMod->AddressTo = $value['users_email'];
        $ObjMailMod->MailSend($VarHtml);
      }
    }
  }
}
else{
  $ArrReturn = array('error' => $MS_SP->Sp_LangMessage['error']['ErrFuncAccessDenied']);
}
echo json_encode($ArrReturn);
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>