define("ADMIN_MODULE_NAME", "sender");
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_before.php");
use \Bitrix\Main\Localization\Loc;
if(!\Bitrix\Main\Loader::includeModule("sender"))
ShowError(\Bitrix\Main\Localization\Loc::getMessage("MAIN_MODULE_NOT_INSTALLED"));
IncludeModuleLangFile(__FILE__);
$POST_RIGHT = $APPLICATION->GetGroupRight("sender");
if($POST_RIGHT=="D")
$APPLICATION->AuthForm(GetMessage("ACCESS_DENIED"));
$isUserHavePhpAccess = $USER->CanDoOperation('edit_php');
$aTabs = array(
array("DIV" => "edit1", "TAB" => GetMessage("sender_chain_edit_tab_main"), "ICON"=>"main_user_edit", "TITLE"=>GetMessage("sender_chain_edit_tab_main_title")),
array("DIV" => "edit2", "TAB" => GetMessage("sender_chain_edit_tab_message"), "ICON"=>"main_user_edit", "TITLE"=>GetMessage("sender_chain_edit_tab_message_title")),
array("DIV" => "edit3", "TAB" => GetMessage("sender_chain_edit_tab_send"), "ICON"=>"main_user_edit", "TITLE"=>GetMessage("sender_chain_edit_tab_send_title")),
);
$tabControl = new CAdminTabControl("tabControl", $aTabs);
$ID = intval($ID); // Id of the edited record
$MAILING_ID = intval($MAILING_ID);
$message = null;
$bVarsFromForm = false;
if($_SERVER['REQUEST_METHOD'] == "POST" && ($save!="" || $apply!="") && $POST_RIGHT=="W" && check_bitrix_sessid())
{
$arError = array();
$DAYS_OF_WEEK = empty($DAYS_OF_WEEK) ? '' : implode(',',$DAYS_OF_WEEK);
if(!$isUserHavePhpAccess)
{
$MESSAGE_OLD = false;
if($ID>0)
{
$mailingChainOld = \Bitrix\Sender\MailingChainTable::getRowById(array('ID' => $ID));
if($mailingChainOld)
{
$MESSAGE_OLD = $mailingChainOld['MESSAGE'];
}
}
$MESSAGE = LPA::Process($MESSAGE, $MESSAGE_OLD);
}
$arFields = Array(
"MAILING_ID" => $MAILING_ID,
"TITLE" => $TITLE,
"SUBJECT" => $SUBJECT,
"EMAIL_FROM" => $EMAIL_FROM,
"MESSAGE" => $MESSAGE,
"TEMPLATE_TYPE" => $TEMPLATE_TYPE,
"TEMPLATE_ID" => $TEMPLATE_ID,
"PRIORITY" => $PRIORITY,
"LINK_PARAMS" => $LINK_PARAMS,
"CREATED_BY" => $USER->GetID(),
"REITERATE" => "N",
"AUTO_SEND_TIME" => "",
"DAYS_OF_WEEK" => "",
"DAYS_OF_MONTH" => "",
"TIMES_OF_DAY" => "",
);
if(empty($MESSAGE) && isset($IS_TEMPLATE_LIST_SHOWN) && $IS_TEMPLATE_LIST_SHOWN=='Y')
{
$arError[] = GetMessage("sender_chain_edit_error_select_template");
}
switch($SEND_TYPE)
{
case 'MANUAL':
break;
case 'TIME':
if(empty($AUTO_SEND_TIME))
$arError[] = GetMessage("sender_chain_edit_error_empty_time");
if(!\Bitrix\Main\Type\DateTime::isCorrect($AUTO_SEND_TIME))
$arError[] = GetMessage("sender_chain_edit_error_time_format");
else
$arFields["AUTO_SEND_TIME"] = new \Bitrix\Main\Type\DateTime($AUTO_SEND_TIME);
if ($ID <= 0)
{
$arFields["STATUS"] = \Bitrix\Sender\MailingChainTable::STATUS_SEND;
}
else
{
$arMailingChainOld = \Bitrix\Sender\MailingChainTable::getRowById(array('ID' => $ID));
if($arMailingChainOld['STATUS'] == \Bitrix\Sender\MailingChainTable::STATUS_NEW)
$arFields["STATUS"] = \Bitrix\Sender\MailingChainTable::STATUS_SEND;
}
break;
case 'REITERATE':
if(empty($DAYS_OF_WEEK) && empty($DAYS_OF_MONTH))
$arError[] = GetMessage("sender_chain_edit_error_reiterate");
$arFields["REITERATE"] = "Y";
$arFields["DAYS_OF_MONTH"] = $DAYS_OF_MONTH;
$arFields["DAYS_OF_WEEK"] = $DAYS_OF_WEEK;
$arFields["TIMES_OF_DAY"] = $TIMES_OF_DAY;
if ($ID <= 0)
{
$arFields["STATUS"] = \Bitrix\Sender\MailingChainTable::STATUS_WAIT;
}
else
{
$arMailingChainOld = \Bitrix\Sender\MailingChainTable::getRowById(array('ID' => $ID));
$arFields["AUTO_SEND_TIME"] = $arMailingChainOld["AUTO_SEND_TIME"];
if($arMailingChainOld['STATUS'] == \Bitrix\Sender\MailingChainTable::STATUS_NEW)
$arFields["STATUS"] = \Bitrix\Sender\MailingChainTable::STATUS_WAIT;
}
break;
default:
$arError[] = GetMessage("sender_chain_edit_error_send_type");
}
if(empty($arError))
{
$res = false;
if ($ID > 0)
{
unset($arFields['CREATED_BY']);
unset($arFields['MAILING_ID']);
$mailingUpdateDb = \Bitrix\Sender\MailingChainTable::update(array('ID' => $ID), $arFields);
$res = $mailingUpdateDb->isSuccess();
if(!$res)
$arError = $mailingUpdateDb->getErrorMessages();
}
elseif ($MAILING_ID > 0)
{
$mailingAddDb = \Bitrix\Sender\MailingChainTable::add($arFields);
if ($mailingAddDb->isSuccess())
{
$ID = $mailingAddDb->getId();
\Bitrix\Sender\MailingChainTable::initPosting($ID);
$res = ($ID > 0);
}
else
{
$arError = $mailingAddDb->getErrorMessages();
}
}
}
if($res)
{
//add or delete files
//Delete checked
if(is_array($FILES_del))
{
$FILE_ID_tmp = array();
foreach($FILES_del as $file=>$fileMarkDel)
{
$file = intval($file);
if($file>0)
$FILE_ID_tmp[] = $file;
}
if(count($FILE_ID_tmp)>0)
{
$deleteFileDb = \Bitrix\Sender\MailingAttachmentTable::getList(array(
'select' => array('FILE_ID', 'CHAIN_ID'),
'filter' => array('CHAIN_ID' => $ID, 'FILE_ID' => $FILE_ID_tmp),
));
while($arDeleteFile = $deleteFileDb->fetch())
{
if(!empty($arDeleteFile))
{
CFile::Delete($arDeleteFile["FILE_ID"]);
\Bitrix\Sender\MailingAttachmentTable::delete($arDeleteFile);
}
}
}
}
//Brandnew
$arFiles = array();
if(is_array($_FILES["NEW_FILE"]))
{
foreach($_FILES["NEW_FILE"] as $attribute=>$files)
{
if(is_array($files))
foreach($files as $index=>$value)
$arFiles[$index][$attribute]=$value;
}
foreach($arFiles as $index => $file)
{
if(!is_uploaded_file($file["tmp_name"]))
unset($arFiles[$index]);
}
}
//New from media library and file structure
if(array_key_exists("NEW_FILE", $_POST) && is_array($_POST["NEW_FILE"]))
{
foreach($_POST["NEW_FILE"] as $index=>$value)
{
if (is_string($value) && preg_match("/^https?:\\/\\//", $value))
{
$arFiles[$index] = CFile::MakeFileArray($value);
}
else
{
if(is_array($value))
{
$filePath = $value['tmp_name'];
}
else
{
$filePath = $value;
}
$isCheckedSuccess = false;
$io = CBXVirtualIo::GetInstance();
$docRoot = \Bitrix\Main\Application::getDocumentRoot();
if(strpos($filePath, CTempFile::GetAbsoluteRoot()) === 0)
{
$absPath = $filePath;
}
elseif(strpos($io->CombinePath($docRoot, $filePath), CTempFile::GetAbsoluteRoot()) === 0)
{
$absPath = $io->CombinePath($docRoot, $filePath);
}
else
{
$absPath = $io->CombinePath(CTempFile::GetAbsoluteRoot(), $filePath);
}
if ($io->ValidatePathString($absPath) && $io->FileExists($absPath))
{
$docRoot = $io->CombinePath($docRoot, '/');
$relPath = str_replace($docRoot, '', $absPath);
$perm = $APPLICATION->GetFileAccessPermission($relPath);
if ($perm >= "W")
{
$isCheckedSuccess = true;
}
}
if($isCheckedSuccess)
{
$arFiles[$index] = CFile::MakeFileArray($io->GetPhysicalName($absPath));
if(is_array($value))
{
$arFiles[$index]['name'] = $value['name'];
}
}
}
}
}
foreach($arFiles as $file)
{
if(strlen($file["name"])>0 and intval($file["size"])>0)
{
$resultInsertAttachFile = false;
$file["MODULE_ID"] = "main";
$fid = intval(CFile::SaveFile($file, "sender", true));
if($fid > 0)
{
$resultAddAttachFile = \Bitrix\Sender\MailingAttachmentTable::add(array(
'CHAIN_ID' => $ID,
'FILE_ID' => $fid
));
$resultInsertAttachFile = $resultAddAttachFile->isSuccess();
}
if(!$resultInsertAttachFile)
break;
}
}
// save email to list of emails from
\Bitrix\Sender\MailingChainTable::setEmailFromToList($EMAIL_FROM);
// save template body to my templates
if(isset($TEMPLATE_ACTION_SAVE) && $TEMPLATE_ACTION_SAVE == 'Y')
{
if(!empty($TEMPLATE_ACTION_SAVE_NAME) && !empty($MESSAGE))
{
$CONTENT = $MESSAGE;
$useBlockEditor = false;
if($TEMPLATE_TYPE && $TEMPLATE_ID)
{
\Bitrix\Main\Loader::includeModule('fileman');
$chainTemplate = \Bitrix\Sender\Preset\Template::getById($TEMPLATE_TYPE, $TEMPLATE_ID);
if($chainTemplate && $chainTemplate['HTML'])
{
$CONTENT = \Bitrix\Fileman\Block\Editor::fillTemplateBySliceContent($chainTemplate['HTML'], $CONTENT);
if($CONTENT)
{
$useBlockEditor = true;
}
}
}
$addResult = \Bitrix\Sender\TemplateTable::add(array(
'NAME' => $TEMPLATE_ACTION_SAVE_NAME,
'CONTENT' => $CONTENT
));
if($useBlockEditor && $addResult->isSuccess())
{
\Bitrix\Sender\MailingChainTable::update(
array('ID' => $ID),
array('TEMPLATE_TYPE' => 'USER', 'TEMPLATE_ID' => $addResult->getId())
);
}
}
}
if($apply!="")
LocalRedirect("/bitrix/admin/sender_mailing_chain_edit.php?MAILING_ID=".$MAILING_ID."&ID=".$ID."&lang=".LANG."&".$tabControl->ActiveTabParam());
else
LocalRedirect("/bitrix/admin/sender_mailing_chain_admin.php?MAILING_ID=".$MAILING_ID."&lang=".LANG);
}
else
{
if($e = $APPLICATION->GetException())
$arError[] = GetMessage("rub_save_error");
if(!empty($arError))
$message = new CAdminMessage(implode('
', $arError));
$bVarsFromForm = true;
}
}
//Edit/Add part
ClearVars();
$str_SORT = 100;
$str_ACTIVE = "Y";
$str_VISIBLE = "Y";
$arMailngChainAttachment = array();
$recommendedSendTime = null;
if($ID>0)
{
$rubric = new CDBResult(\Bitrix\Sender\MailingChainTable::getList(array(
'select' => array('*'),
'filter' => array('ID' => $ID)
)));
if(!$rubric->ExtractFields("str_"))
$ID=0;
if($ID>0)
{
$postingDb = \Bitrix\Sender\PostingTable::getList(array(
'select' => array('*'),
'filter' => array('MAILING_CHAIN_ID' => $ID, '!DATE_SENT' => null),
'order' => array('DATE_SENT' => 'DESC'),
'limit' => 1
));
$arPosting = $postingDb->fetch();
$str_DATE_SENT = $arPosting['DATE_SENT'];
$attachmentFileDb = \Bitrix\Sender\MailingAttachmentTable::getList(array(
'select' => array('FILE_ID'),
'filter' => array('CHAIN_ID' => $ID),
));
while($ar = $attachmentFileDb->fetch())
{
if($arFileFetch = CFile::GetFileArray($ar['FILE_ID']))
$arMailngChainAttachment[] = $arFileFetch;
}
}
}
$chainCharset = '';
if($MAILING_ID>0)
{
$mailingSiteDb = \Bitrix\Sender\MailingTable::getList(array('select' => array('SITE_ID'), 'filter' => array('ID' => $MAILING_ID)));
if($mailingSite = $mailingSiteDb->fetch())
{
$mailingSiteCharsetDb = \Bitrix\Main\SiteTable::getList(array(
'select'=>array('CULTURE_CHARSET'=>'CULTURE.CHARSET'),
'filter' => array('LID' => $mailingSite['SITE_ID'])
));
if($mailingSiteCharset = $mailingSiteCharsetDb->fetch())
{
$chainCharset = $mailingSiteCharset['CULTURE_CHARSET'];
}
}
}
if($bVarsFromForm)
$DB->InitTableVarsForEdit("b_sender_mailing_chain", "", "str_");
if(!isset($SEND_TYPE))
{
if ($str_REITERATE == 'Y')
$SEND_TYPE = 'REITERATE';
elseif (!empty($str_AUTO_SEND_TIME))
$SEND_TYPE = 'TIME';
else
$SEND_TYPE = 'MANUAL';
}
$templateListHtml = \Bitrix\Sender\Preset\Template::getTemplateListHtml('tabControl_layout');
$templateName = '';
$template = \Bitrix\Sender\Preset\Template::getById($str_TEMPLATE_TYPE, $str_TEMPLATE_ID);
if($template)
{
$templateName = $template['NAME'];
}
if ($MAILING_ID > 0 && ($ID <= 0 || $SEND_TYPE == 'MANUAL'))
{
$statistics = \Bitrix\Sender\Stat\Statistics::create()->filter('mailingId', $MAILING_ID);
$recommendedSendTime = $statistics->getRecommendedSendTime();
}
\CJSCore::Init(array("sender_admin"));
$APPLICATION->SetTitle(($ID>0? GetMessage("sender_chain_edit_title_edit").$ID : GetMessage("sender_chain_edit_title_add")));
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_after.php");
$aMenu = array(
array(
"TEXT"=>GetMessage("sender_chain_edit_list"),
"TITLE"=>GetMessage("sender_chain_edit_list_title"),
"LINK"=>"/bitrix/admin/sender_mailing_chain_admin.php?MAILING_ID=".$MAILING_ID."&lang=".LANG,
"ICON"=>"btn_list",
)
);
if($ID>0 && $POST_RIGHT>="W")
{
$aMenu[] = array("SEPARATOR"=>"Y");
$aMenu[] = array(
"TEXT"=>GetMessage("sender_chain_edit_action_add"),
"TITLE"=>GetMessage("sender_chain_edit_action_add_title"),
"LINK"=>"/bitrix/admin/sender_mailing_chain_edit.php?MAILING_ID=".$MAILING_ID."&lang=".LANG,
"ICON"=>"btn_new",
);
$aMenu[] = array(
"TEXT"=>GetMessage("sender_chain_edit_action_copy"),
"TITLE"=>GetMessage("sender_chain_edit_action_copy_title"),
"LINK"=>"/bitrix/admin/sender_mailing_chain_admin.php?MAILING_ID=".$MAILING_ID."&ID=".$ID."&action=copy&lang=".LANG."&".bitrix_sessid_get(),
"ICON"=>"btn_copy",
);
$aMenu[] = array(
"TEXT"=>GetMessage("sender_chain_edit_action_del"),
"TITLE"=>GetMessage("sender_chain_edit_action_del_title"),
"LINK"=>"javascript:if(confirm('".GetMessage("sender_chain_edit_action_del_confirm")."'))window.location='/bitrix/admin/sender_mailing_chain_admin.php?MAILING_ID=".$MAILING_ID."&ID=".$ID."&action=delete&lang=".LANGUAGE_ID."&".bitrix_sessid_get()."';",
"ICON"=>"btn_delete",
);
$aMenu[] = array("SEPARATOR"=>"Y");
}
$context = new CAdminContextMenu($aMenu);
$context->Show();
\Bitrix\Sender\PostingRecipientTable::setPersonalizeList(\Bitrix\Sender\MailingTable::getPersonalizeList($MAILING_ID));
$arMailing = \Bitrix\Sender\MailingTable::getRowById($MAILING_ID);
if($_REQUEST["mess"] == "ok" && $ID>0)
CAdminMessage::ShowMessage(array("MESSAGE"=>GetMessage("sender_chain_edit_saved"), "TYPE"=>"OK"));
if($_REQUEST["mess"] == "copied" && $ID>0)
CAdminMessage::ShowMessage(array("MESSAGE"=>GetMessage("sender_chain_edit_copied"), "TYPE"=>"OK"));
if($message)
echo $message->Show();
?>