function ChangeStatus($ID, $status)
{
global $DB;
$ID = intval($ID);
$this->LAST_ERROR = "";
$strSql = "SELECT STATUS, VERSION FROM b_posting WHERE ID=".$ID;
$db_result = $DB->Query($strSql, false, "File: ".__FILE__."
Line: ".__LINE__);
$arResult = $db_result->Fetch();
if(!$arResult)
{
$this->LAST_ERROR = GetMessage("class_post_err_notfound")."
";
return false;
}
if($arResult["STATUS"]==$status)
return true;
switch($arResult["STATUS"].$status)
{
case "DP":
//BCC_TO_SEND fill
$post = $this->GetByID($ID);
if(!($post_arr = $post->Fetch()))
{
$this->LAST_ERROR .= GetMessage("class_post_err_notfound")."
";
return false;
}
$DB->Query("DELETE from b_posting_email WHERE POSTING_ID = ".$ID);
$DB->Query("
INSERT INTO b_posting_email (POSTING_ID, STATUS, EMAIL, SUBSCRIPTION_ID, USER_ID)
SELECT DISTINCT
PR.POSTING_ID, 'Y', S.EMAIL, S.ID, S.USER_ID
FROM
b_posting_rubric PR
INNER JOIN b_subscription_rubric SR ON SR.LIST_RUBRIC_ID = PR.LIST_RUBRIC_ID
INNER JOIN b_subscription S ON S.ID = SR.SUBSCRIPTION_ID
LEFT JOIN b_user U ON U.ID = S.USER_ID
WHERE
PR.POSTING_ID = ".$ID."
AND S.CONFIRMED = 'Y'
AND S.ACTIVE = 'Y'
AND (U.ID IS NULL OR U.ACTIVE = 'Y')
".($post_arr["SUBSCR_FORMAT"] == '' || $post_arr["SUBSCR_FORMAT"]==="NOT_REF" ? "": "AND S.FORMAT='".($post_arr["SUBSCR_FORMAT"]=="text"? "text": "html")."'")."
".($post_arr["EMAIL_FILTER"] == '' || $post_arr["EMAIL_FILTER"]==="NOT_REF" ? "": "AND ".GetFilterQuery("S.EMAIL", $post_arr["EMAIL_FILTER"], "Y", array("@", ".", "_")))."
");
$DB->Query("
DELETE pe
from b_posting_email pe
left join b_posting_email pe0 on
pe0.POSTING_ID = pe.POSTING_ID
and pe0.EMAIL = pe.EMAIL
WHERE pe.POSTING_ID = ".$ID."
AND pe0.ID < pe.ID
");
//send to user groups
$res = $DB->Query("SELECT * FROM b_posting_group WHERE POSTING_ID = ".$ID." AND GROUP_ID = 2");
if($res->Fetch())
{
$DB->Query("
INSERT INTO b_posting_email (POSTING_ID, STATUS, EMAIL, SUBSCRIPTION_ID, USER_ID)
SELECT
".$ID.", 'Y', U.EMAIL, NULL, MIN(U.ID)
FROM
b_user U
WHERE
U.ACTIVE = 'Y'
".($post_arr["EMAIL_FILTER"] == '' || $post_arr["EMAIL_FILTER"]==="NOT_REF" ? "": "AND ".GetFilterQuery("U.EMAIL", $post_arr["EMAIL_FILTER"], "Y", array("@", ".", "_")))."
and U.EMAIL not in (SELECT EMAIL FROM b_posting_email WHERE POSTING_ID = ".$ID.")
GROUP BY U.EMAIL
");
}
else
{
$DB->Query("
INSERT INTO b_posting_email (POSTING_ID, STATUS, EMAIL, SUBSCRIPTION_ID, USER_ID)
SELECT
PG.POSTING_ID, 'Y', U.EMAIL, NULL, MIN(U.ID)
FROM
b_posting_group PG
INNER JOIN b_user_group UG ON UG.GROUP_ID = PG.GROUP_ID
INNER JOIN b_user U ON U.ID = UG.USER_ID
WHERE
PG.POSTING_ID = ".$ID."
and (UG.DATE_ACTIVE_FROM is null or UG.DATE_ACTIVE_FROM <= ".$DB->CurrentTimeFunction().")
and (UG.DATE_ACTIVE_TO is null or UG.DATE_ACTIVE_TO >= ".$DB->CurrentTimeFunction().")
and U.ACTIVE = 'Y'
".($post_arr["EMAIL_FILTER"] == '' || $post_arr["EMAIL_FILTER"]==="NOT_REF" ? "": "AND ".GetFilterQuery("U.EMAIL", $post_arr["EMAIL_FILTER"], "Y", array("@", ".", "_")))."
and U.EMAIL not in (SELECT EMAIL FROM b_posting_email WHERE POSTING_ID = ".$ID.")
GROUP BY PG.POSTING_ID, U.EMAIL
");
}
//from additional emails
$BCC = $post_arr["BCC_FIELD"];
if($post_arr["DIRECT_SEND"] == "Y")
$BCC .= ($BCC <> ""? ",":"").$post_arr["TO_FIELD"];
$BCC = str_replace("rn", "n", $BCC);
$BCC = str_replace("n", ",", $BCC);
$aBcc = explode(",", $BCC);
foreach($aBcc as $email)
{
$email = trim($email);
if($email <> "")
{
$DB->Query("
INSERT INTO b_posting_email (POSTING_ID, STATUS, EMAIL, SUBSCRIPTION_ID, USER_ID)
SELECT
P.ID, 'Y', '".($DB->ForSQL($email))."', NULL, NULL
FROM
b_posting P
WHERE
P.ID = ".$ID."
and '".($DB->ForSQL($email))."' not in (SELECT EMAIL FROM b_posting_email WHERE POSTING_ID = ".$ID.")
");
}
}
$res = $DB->Query("SELECT count(*) CNT from b_posting_email WHERE POSTING_ID = ".$ID);
$ar = $res->Fetch();
if($ar["CNT"] > 0)
{
$DB->Query("UPDATE b_posting SET STATUS='".$status."', VERSION='2', BCC_TO_SEND=null, ERROR_EMAIL=null, SENT_BCC=null WHERE ID=".$ID);
}
else
{
$this->LAST_ERROR .= GetMessage("class_post_err_status4");
return false;
}
break;
case "PW":
case "WP":
case "PE":
case "PS":
$DB->Query("UPDATE b_posting SET STATUS='".$status."' WHERE ID=".$ID);
break;
case "EW"://This is the way to resend error e-mails
case "EP":
if($arResult["VERSION"] == "2")
{
$DB->Query("UPDATE b_posting_email SET STATUS='Y' WHERE POSTING_ID=".$ID." AND STATUS='E'");
$DB->Query("UPDATE b_posting SET STATUS='".$status."' WHERE ID=".$ID);
}
else
{
//Send it in old fashion way
$DB->Query("UPDATE b_posting SET STATUS='".$status."', BCC_TO_SEND=ERROR_EMAIL, ERROR_EMAIL=null WHERE ID=".$ID);
}
break;
case "ED":
case "SD":
case "WD":
$DB->Query("UPDATE b_posting SET STATUS='".$status."', VERSION='2', SENT_BCC=null, ERROR_EMAIL=null, BCC_TO_SEND=null, DATE_SENT=null WHERE ID=".$ID);
break;
default:
$this->LAST_ERROR = GetMessage("class_post_err_status2");
return false;
}
return true;
}