public static function updateChainTrigger($id) { // get first item of chain $chainDb = MailingChainTable::getList(array('select' => array('ID', 'TRIGGER_FIELDS' => 'MAILING.TRIGGER_FIELDS'), 'filter' => array('=MAILING_ID' => $id, '=IS_TRIGGER' => 'Y', '=PARENT_ID' => null))); $chain = $chainDb->fetch(); if (!$chain) { return; } $chainId = $chain['ID']; // get trigger settings from mailing $triggerFields = $chain['TRIGGER_FIELDS']; if (!is_array($triggerFields)) { $triggerFields = array(); } // init TriggerSettings objects $settingsList = array(); foreach ($triggerFields as $key => $point) { if (empty($point['CODE'])) { continue; } $point['IS_EVENT_OCCUR'] = true; $point['IS_PREVENT_EMAIL'] = false; $point['SEND_INTERVAL_UNIT'] = 'M'; $point['IS_CLOSED_TRIGGER'] = $point['IS_CLOSED_TRIGGER'] == 'Y' ? true : false; switch ($key) { case 'END': $point['IS_TYPE_START'] = false; break; case 'START': default: $point['IS_TYPE_START'] = true; } $settingsList[] = new \Bitrix\Sender\TriggerSettings($point); } // prepare fields for save $mailingTriggerList = array(); foreach ($settingsList as $settings) { /* @var \Bitrix\Sender\TriggerSettings $settings */ $trigger = \Bitrix\Sender\TriggerManager::getOnce($settings->getEndpoint()); if ($trigger) { $triggerFindId = $trigger->getFullEventType() . "/" . (int) $settings->isTypeStart(); $mailingTriggerList[$triggerFindId] = array('IS_TYPE_START' => $settings->isTypeStart(), 'NAME' => $trigger->getName(), 'EVENT' => $trigger->getFullEventType(), 'ENDPOINT' => $settings->getArray()); } } // add new, update exists, delete old rows $triggerDb = MailingTriggerTable::getList(array('select' => array('EVENT', 'MAILING_CHAIN_ID', 'IS_TYPE_START'), 'filter' => array('=MAILING_CHAIN_ID' => $chainId))); while ($trigger = $triggerDb->fetch()) { $triggerFindId = $trigger['EVENT'] . "/" . (int) $trigger['IS_TYPE_START']; if (!isset($mailingTriggerList[$triggerFindId])) { MailingTriggerTable::delete($trigger); } else { MailingTriggerTable::update($trigger, $mailingTriggerList[$triggerFindId]); unset($mailingTriggerList[$triggerFindId]); } } foreach ($mailingTriggerList as $triggerFindId => $settings) { $settings['MAILING_CHAIN_ID'] = $chainId; MailingTriggerTable::add($settings); } TriggerManager::actualizeHandlerForChild(); }
/** * @param array $params * @param bool $activate * @return void */ public static function actualizeHandler(array $params, $activate = null) { $moduleId = $params['MODULE_ID']; $eventType = $params['EVENT_TYPE']; $calledBeforeChange = $params['CALLED_BEFORE_CHANGE']; if ($params['IS_CLOSED_TRIGGER']) { return; } if ($activate === null) { // if actualizing will be called before deleting record (or updating record with clearing field), // query will select this record. // In this reason, it should be considered - check if more 1 or 0 selected rows. if ($calledBeforeChange) { $minRowsCount = 1; } else { $minRowsCount = 0; } $existsDb = MailingTriggerTable::getList(array('select' => array('MAILING_CHAIN_ID'), 'filter' => array('=EVENT' => $moduleId . '/' . $eventType, '=MAILING_CHAIN.IS_TRIGGER' => 'Y', '=MAILING_CHAIN.MAILING.ACTIVE' => 'Y'), 'group' => array('MAILING_CHAIN_ID'), 'limit' => 2)); $rowsCount = 0; while ($existsDb->fetch()) { $rowsCount++; } if ($rowsCount > $minRowsCount) { $activate = true; } else { $activate = false; } } if ($activate) { \Bitrix\Main\EventManager::getInstance()->registerEventHandler($moduleId, $eventType, 'sender', __CLASS__, 'handleEvent'); } else { \Bitrix\Main\EventManager::getInstance()->unRegisterEventHandler($moduleId, $eventType, 'sender', __CLASS__, 'handleEvent'); } }