use ProcessMaker\Services\Api;
$appDel = new AppDelegation();
$actualThread = $appDel->Load($caseId, $actualIndex);
$actualLastIndex = $actualThread['DEL_PREVIOUS'];
$appDelPrev = $appDel->LoadParallel($caseId);
if ($appDelPrev == array()) {
    $appDelPrev['0'] = $actualThread;
}
$Pmgmail = new \ProcessMaker\BusinessModel\Pmgmail();
foreach ($appDelPrev as $app) {
    if ($app['DEL_INDEX'] != $actualIndex && $app['DEL_PREVIOUS'] != $actualLastIndex) {
        //Sending the email to all threads of the case except the actual thread
        $response = $Pmgmail->sendEmail($caseId, "", $app['DEL_INDEX']);
    }
}
require_once PATH_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP . "class.labelsGmail.php";
$oLabels = new labelsGmail();
$oResponse = $oLabels->setLabels($caseId, $actualIndex, $actualLastIndex, false);
$enablePMGmail = false;
G::LoadClass("pmDrive");
$pmDrive = new PMDrive();
$enablePMGmail = $pmDrive->getStatusService();
if (key_exists('gmail', $_SESSION) && $_SESSION['gmail'] == 1 && !empty($enablePMGmail) && $enablePMGmail == 1) {
    $_SESSION['gmail'] = 0;
    unset($_SESSION['gmail']);
    //cleaning session
    $mUrl = '/sys' . $_SESSION['WORKSPACE'] . '/en/neoclassic/cases/cases_Open?APP_UID=' . $caseId . '&DEL_INDEX=' . $actualIndex . '&action=sent';
} else {
    $mUrl = 'casesListExtJs';
}
header('location:' . $mUrl);
}
/* Includes */
G::LoadClass('case');
$oCase = new Cases();
$sAppUid = $_SESSION['APPLICATION'];
$iDelIndex = $_SESSION['INDEX'];
$oAppDelegation = new AppDelegation();
$aDelegation = $oAppDelegation->load($sAppUid, $iDelIndex);
//if there are no user in the delegation row, this case is still in selfservice
if ($aDelegation['USR_UID'] == "") {
    $oCase->setCatchUser($_SESSION['APPLICATION'], $_SESSION['INDEX'], $_SESSION['USER_LOGGED']);
    //changing email labels if the claim comes from gmail
    if (array_key_exists('gmail', $_SESSION) && $_SESSION['gmail'] == 1) {
        require_once PATH_HOME . "engine" . PATH_SEP . "classes" . PATH_SEP . "class.labelsGmail.php";
        $labGmail = new labelsGmail();
        $oResponse = $labGmail->setLabels($sAppUid, $iDelIndex, -1, true);
        die('<script type="text/javascript">
        parent.document.getElementById("iframePM").setAttribute("src", "' . $_SESSION["server"] . 'cases/cases_Open?APP_UID=' . $_SESSION["APPLICATION"] . '&DEL_INDEX=' . $_SESSION["INDEX"] . '&action=unassigned");
        </script>');
    }
} else {
    G::SendMessageText(G::LoadTranslation('ID_CASE_ALREADY_DERIVATED'), 'error');
}
$validation = SYS_SKIN != 'uxs' ? 'true' : 'false';
die('<script type="text/javascript">
  if (' . $validation . ') {
      if (window.parent.frames.length != 0) {
          parent.location = "open?APP_UID=' . $_SESSION['APPLICATION'] . '&DEL_INDEX=' . $_SESSION['INDEX'] . '&action=unassigned";
      } else {
          window.location = "../cases/cases_Open?APP_UID=' . $_SESSION['APPLICATION'] . '&DEL_INDEX=' . $_SESSION['INDEX'] . '&action=unassigned";
      }