<?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']) && 0 < $_SESSION['SessionAuth']['id']) {
  $ObjSvtMod = new SvtMod($MS_SP);
  $ObjSvtStatusValMod = new SvtStatusValMod($MS_SP);
  if (true === isset($_POST['svt_id']) && true === is_numeric($_POST['svt_id'])){
    $ArrSvt = $ObjSvtMod->GetFromDb('SqlAll', 'GetRow', 'a.svt_id = "'. $_POST['svt_id'] .'"');
    $VarSvtId = $ObjSvtMod->Edit($_POST);
    if (true === is_numeric($VarSvtId)){
      $_POST['structureorganization_id_from'] = $ArrSvt['structureorganization_id'];
      $_POST['users_id_from'] = $ArrSvt['users_id'];
      $_POST['structureorganization_id_to'] = NULL;
      $_POST['users_id_to'] = NULL;
      if(true === isset($_POST['FrmAgreeStructureOrganizationCheckboxes']) && true === is_numeric(current($_POST['FrmAgreeStructureOrganizationCheckboxes']))){
        $_POST['structureorganization_id_to'] = current($_POST['FrmAgreeStructureOrganizationCheckboxes']);
      }
      if(true === isset($_POST['FrmAgreeUsersCheckboxes']) && true === is_numeric(current($_POST['FrmAgreeUsersCheckboxes']))){
        $_POST['users_id_to'] = current($_POST['FrmAgreeUsersCheckboxes']);
      }
      if(($_POST['structureorganization_id_from'] != $_POST['structureorganization_id_to']) || ($_POST['users_id_from'] != $_POST['users_id_to'])){
        $ObjSvtMoveMod = new SvtMoveMod($MS_SP);
        $VarSvtMoveId = $ObjSvtMoveMod->Add($_POST);
      }
      $ObjSvtStatusValMod->Del($VarSvtId, 'svt_id');
      if (true === is_array($_POST['FrmAgreeSvtStatusCheckboxes']) && 1 <= count($_POST['FrmAgreeSvtStatusCheckboxes'])){
        foreach ($_POST['FrmAgreeSvtStatusCheckboxes'] AS $key => $val){
          $VarResultAddSvtStatusVal = $ObjSvtStatusValMod->Add(array('svt_id' => $_POST['svt_id'], 'svtstatus_id' => $val));
        }
      }
      $ArrReturn = array('message' => $MS_SP->Sp_LangMessage['message']['MessRecordEditSuccess']);
    }
    else{
      $ArrReturn = $VarSvtId;
    }
  }
  else{
    $VarSvtId = $ObjSvtMod->Add($_POST);
    if (true === is_numeric($VarSvtId)){
      $_POST['structureorganization_id_from'] = NULL;
      $_POST['users_id_from'] = NULL;
      $_POST['structureorganization_id_to'] = NULL;
      $_POST['users_id_to'] = NULL;
      $_POST['svt_id'] = $VarSvtId;
      if(true === isset($_POST['FrmAgreeStructureOrganizationCheckboxes']) && true === is_numeric(current($_POST['FrmAgreeStructureOrganizationCheckboxes']))){
        $_POST['structureorganization_id_to'] = current($_POST['FrmAgreeStructureOrganizationCheckboxes']);
      }
      if(true === isset($_POST['FrmAgreeUsersCheckboxes']) && true === is_numeric(current($_POST['FrmAgreeUsersCheckboxes']))){
        $_POST['users_id_to'] = current($_POST['FrmAgreeUsersCheckboxes']);
      }
      $ObjSvtMoveMod = new SvtMoveMod($MS_SP);
      $VarSvtMoveId = $ObjSvtMoveMod->Add($_POST);
      if (true === is_array($_POST['FrmAgreeSvtStatusCheckboxes']) && 1 <= count($_POST['FrmAgreeSvtStatusCheckboxes'])){
        foreach ($_POST['FrmAgreeSvtStatusCheckboxes'] AS $key => $val){
          $VarResultAddSvtStatusVal = $ObjSvtStatusValMod->Add(array('svt_id' => $VarSvtId, 'svtstatus_id' => $val));
        }
      }
      $ArrReturn = array('message' => $MS_SP->Sp_LangMessage['message']['MessRecordAddSuccess']);
    }
    else{
      $ArrReturn = $VarSvtId;
    }
  }
}
else{
  $ArrReturn = array('error' => $MS_SP->Sp_LangMessage['error']['ErrFuncAccessDenied']);
}
echo json_encode($ArrReturn);
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>