150 $result =
new Entity\EventResult();
152 $data = $event->getParameter(
"fields");
156 $ownerInfo = $engine->getCurrentUser();
158 if(!static::$skipRemoteUpdate)
161 $data[
"XML_ID"] =
'Error';
165 $data[
"XML_ID"] = $data[
"SETTINGS"][
"CampaignID"];
168 $data[
"NAME"] = $data[
"SETTINGS"][
"Name"];
170 $data[
"ENGINE_ID"] = $engine->getId();
172 $data[
'OWNER_ID'] = $ownerInfo[
'id'];
173 $data[
'OWNER_NAME'] = $ownerInfo[
'login'];
175 if(!static::$skipRemoteUpdate && $result->getType() ==
Entity\EventResult::SUCCESS)
179 $data[
"XML_ID"] = $engine->addCampaign($data[
"SETTINGS"]);
181 $campaignSettings = $engine->getCampaign(array($data[
'XML_ID']));
182 $data[
'SETTINGS'] = $campaignSettings[0];
187 static::getEntity()->getField(
'ENGINE_ID'),
195 $data[
'ACTIVE'] = $data[
'SETTINGS'][
'StatusArchive'] == Engine\YandexDirect::BOOL_YES
199 $result->modifyFields($data);
217 $result =
new Entity\EventResult();
219 $primary = $event->getParameter(
"primary");
220 $data = $event->getParameter(
"fields");
222 $currentData = static::getByPrimary($primary);
223 $currentData = $currentData->fetch();
229 if($currentData[
'ENGINE_ID'] != $engine->getId())
232 static::getEntity()->getField(
'ENGINE_ID'),
237 $ownerInfo = $engine->getCurrentUser();
239 if($currentData[
'OWNER_ID'] != $ownerInfo[
'id'])
242 static::getEntity()->getField(
'OWNER_ID'),
247 $data[
'OWNER_NAME'] = $ownerInfo[
'login'];
248 $data[
'XML_ID'] = $currentData[
'XML_ID'];
250 if(!static::$skipRemoteUpdate)
255 $data[
"NAME"] = $data[
'SETTINGS'][
'Name'];
257 if(!static::$skipRemoteUpdate && $result->getType() ==
Entity\EventResult::SUCCESS)
261 $engine->updateCampaign($data[
"SETTINGS"]);
263 $campaignSettings = $engine->getCampaign(array($data[
'XML_ID']));
264 $data[
'SETTINGS'] = $campaignSettings[0];
270 static::getEntity()->getField(
'ENGINE_ID'),
279 $data[
'ACTIVE'] = $data[
'SETTINGS'][
'StatusArchive'] == Engine\YandexDirect::BOOL_YES
283 $result->modifyFields($data);
384 $settings = $engine->getSettings();
386 $campaignParam = array(
387 "Login" => $settings[
"AUTH_USER"][
"login"],
392 if(!empty($data[
"XML_ID"]))
394 $newCampaign =
false;
395 $campaignParam[
"CampaignID"] = $data[
"XML_ID"];
398 if($newCampaign || isset($data[
'SETTINGS'][
"Name"]))
400 $campaignParam[
"Name"] = trim($data[
'SETTINGS'][
"Name"]);
401 if($campaignParam[
"Name"] ==
'')
404 static::getEntity()->getField(
'NAME'),
410 if($newCampaign || isset($data[
"SETTINGS"][
"FIO"]))
412 $campaignParam[
"FIO"] = trim($data[
"SETTINGS"][
"FIO"]);
414 if($campaignParam[
"FIO"] ==
'')
417 static::getEntity()->getField(
'SETTINGS'),
423 if(is_array($data[
"SETTINGS"]) && array_key_exists(
"StartDate", $data[
"SETTINGS"]))
425 if(is_a($data[
"SETTINGS"][
"StartDate"],
"Bitrix\\Main\\Type\\Date"))
427 $campaignParam[
"StartDate"] = $data[
"SETTINGS"][
"StartDate"]->convertFormatToPhp(
"Y-m-d");
429 elseif(is_string($data[
"SETTINGS"][
"StartDate"]))
431 if(preg_match(
"/^\\d{4}-\\d{2}-\\d{2}$/", $data[
"SETTINGS"][
"StartDate"]))
433 $campaignParam[
"StartDate"] = $data[
"SETTINGS"][
"StartDate"];
437 $ts = MakeTimeStamp($data[
"SETTINGS"][
"StartDate"], FORMAT_DATE);
440 $campaignParam[
"StartDate"] = date(
'Y-m-d', $ts);
445 if(!$campaignParam[
"StartDate"])
448 static::getEntity()->getField(
'SETTINGS'),
454 if($newCampaign || isset($data[
"SETTINGS"][
"Strategy"]))
457 empty($data[
"SETTINGS"][
"Strategy"])
458 || !is_array($data[
"SETTINGS"][
"Strategy"])
459 || empty($data[
"SETTINGS"][
"Strategy"][
"StrategyName"])
463 static::getEntity()->getField(
'SETTINGS'),
468 if(array_key_exists($data[
"SETTINGS"][
"Strategy"][
"StrategyName"], self::$strategyConfig))
470 $strategy = $data[
"SETTINGS"][
"Strategy"][
"StrategyName"];
471 $config = self::$strategyConfig[$strategy];
473 $campaignParam[
"Strategy"] = array(
474 "StrategyName" => $strategy,
477 foreach($data[
"SETTINGS"][
"Strategy"] as $param => $value)
479 if($param !==
"StrategyName")
481 if(array_key_exists($param, $config))
483 $campaignParam[
"Strategy"][$param] = $value;
488 static::getEntity()->getField(
'SETTINGS'),
490 'SEO_CAMPAIGN_ERROR_STRATEGY_PARAM_NOT_SUPPORTED',
493 '#STRATEGY#' => $strategy,
501 foreach($config as $key => $def)
503 if($def[
'mandatory'] || isset($campaignParam[
"Strategy"][$key]))
508 $campaignParam[
"Strategy"][$key] = intval($campaignParam[
"Strategy"][$key]);
511 $campaignParam[
"Strategy"][$key] = doubleval($campaignParam[
"Strategy"][$key]);
515 if(!$def[
'mandatory'] && empty($campaignParam[
"Strategy"][$key]))
517 unset($campaignParam[
"Strategy"][$key]);
521 if($def[
'mandatory'] && empty($campaignParam[
"Strategy"][$key]))
524 static::getEntity()->getField(
'SETTINGS'),
526 'SEO_CAMPAIGN_ERROR_STRATEGY_PARAM_MANDATORY',
528 '#PARAM#' =>
Loc::getMessage(
'SEO_CAMPAIGN_STRATEGY_PARAM_'.ToUpper($key)),
539 static::getEntity()->getField(
'SETTINGS'),
541 'SEO_CAMPAIGN_ERROR_STRATEGY_NOT_SUPPORTED',
543 '#STRATEGY#' => $data[
"SETTINGS"][
"Strategy"][
"StrategyName"],
550 if($newCampaign || !empty($data[
"SETTINGS"][
"EmailNotification"]))
553 empty($data[
"SETTINGS"][
"EmailNotification"])
554 || !is_array($data[
"SETTINGS"][
"EmailNotification"])
555 || !check_email($data[
"SETTINGS"][
"EmailNotification"][
'Email'])
559 static::getEntity()->getField(
'SETTINGS'),
564 $campaignParam[
"EmailNotification"] = array(
565 "Email" => trim($data[
"SETTINGS"][
"EmailNotification"][
'Email']),
566 "WarnPlaceInterval" => intval($data[
"SETTINGS"][
"EmailNotification"][
'WarnPlaceInterval']),
567 "MoneyWarningValue" => intval($data[
"SETTINGS"][
"EmailNotification"][
'MoneyWarningValue']),
568 "SendWarn" => intval($data[
"SETTINGS"][
"EmailNotification"][
'SendWarn']),
571 if($campaignParam[
"EmailNotification"][
'SendWarn'] ===
true
572 || $campaignParam[
"EmailNotification"][
'SendWarn'] === 1
573 || $campaignParam[
"EmailNotification"][
'SendWarn'] ===
'Y'
576 $campaignParam[
"EmailNotification"][
'SendWarn'] = Engine\YandexDirect::BOOL_YES;
580 $campaignParam[
"EmailNotification"][
'SendWarn'] ===
false
581 || $campaignParam[
"EmailNotification"][
'SendWarn'] === 0
582 || $campaignParam[
"EmailNotification"][
'SendWarn'] ===
'N'
585 $campaignParam[
"EmailNotification"][
'SendWarn'] = Engine\YandexDirect::BOOL_NO;
588 if(!in_array($campaignParam[
"EmailNotification"][
"WarnPlaceInterval"], self::$allowedWarnPlaceIntervalValues))
590 if($campaignParam[
"EmailNotification"][
'SendWarn'] ==
Engine\YandexDirect::BOOL_YES)
593 static::getEntity()->getField(
'SETTINGS'),
595 'SEO_CAMPAIGN_ERROR_WRONG_INTERVAL',
596 array(
'#VALUES#' => implode(
597 ',', self::$allowedWarnPlaceIntervalValues
609 $campaignParam[
"EmailNotification"][
"MoneyWarningValue"] < self::$allowedMoneyWarningInterval[0]
610 || $campaignParam[
"EmailNotification"][
"MoneyWarningValue"] > self::$allowedMoneyWarningInterval[1]
614 static::getEntity()->getField(
'SETTINGS'),
616 'SEO_CAMPAIGN_ERROR_WRONG_WARNING',
618 '#MIN#' => self::$allowedMoneyWarningInterval[0],
619 '#MAX#' => self::$allowedMoneyWarningInterval[1],
626 if($newCampaign || isset($data[
"SETTINGS"][
"MinusKeywords"]))
628 if(!is_array($data[
"SETTINGS"][
"MinusKeywords"]))
630 if($data[
"SETTINGS"][
"MinusKeywords"] <>
'')
632 $data[
"SETTINGS"][
"MinusKeywords"] = array();
636 $data[
"SETTINGS"][
"MinusKeywords"] = array($data[
"SETTINGS"][
"MinusKeywords"]);
640 $campaignParam[
"MinusKeywords"] = $data[
"SETTINGS"][
"MinusKeywords"];
643 if(!$newCampaign && $result->getType() ==
Entity\EventResult::SUCCESS)
647 $yandexCampaignParam = $engine->getCampaign($data[
"XML_ID"]);
649 if(!is_array($yandexCampaignParam) || count($yandexCampaignParam) <= 0)
652 static::getEntity()->getField(
'XML_ID'),
654 'SEO_CAMPAIGN_ERROR_CAMPAIGN_NOT_FOUND',
655 array(
'#ID#' => $data[
"XML_ID"])
661 $campaignParam = array_replace_recursive($yandexCampaignParam[0], $campaignParam);
668 static::getEntity()->getField(
'ENGINE_ID'),
676 return $campaignParam;