switch ($_REQUEST['action']) {
                 case 'archive':
                     $engine->stopCampaign($campaign['XML_ID']);
                     $engine->archiveCampaign($campaign['XML_ID']);
                     break;
                 case 'unarchive':
                     $engine->unArchiveCampaign($campaign['XML_ID']);
                     break;
                 case 'stop':
                     $engine->stopCampaign($campaign['XML_ID']);
                     break;
                 case 'resume':
                     $engine->resumeCampaign($campaign['XML_ID']);
                     break;
             }
             $campaignInfo = $engine->getCampaign(array($campaign['XML_ID']));
             $campaignInfo = $campaignInfo[0];
             Adv\YandexCampaignTable::setSkipRemoteUpdate(true);
             $result = Adv\YandexCampaignTable::update($campaignId, array("SETTINGS" => $campaignInfo));
             Adv\YandexCampaignTable::setSkipRemoteUpdate(false);
             if (!$result->isSuccess()) {
                 $errorsList = $result->getErrorMessages();
                 foreach ($errorsList as $errorMessage) {
                     $adminList->AddGroupError($errorMessage, $campaignId);
                 }
             }
         } catch (Engine\YandexDirectException $e) {
             $adminList->AddGroupError($e->getCode() . ': ' . $e->getMessage());
         }
     }
 }
示例#2
0
 /**
  * Checks campaign data before sending it to Yandex
  *
  * $data array format:
  *
  * <ul>
  * <li>ID
  * <li>XML_ID
  * <li>NAME
  * <li>SETTINGS<ul>
  *    <li>FIO
  *    <li>StartDate
  *    <li>Strategy<ul>
  *        <li>StrategyName
  *        <li>MaxPrice
  *        <li>AveragePrice
  *        <li>WeeklySumLimit
  *        <li>ClicksPerWeek
  *    </ul>
  *    <li>EmailNotification<ul>
  *        <li>Email
  *        <li>WarnPlaceInterval
  *        <li>MoneyWarningValue
  *    </ul>
  *  </ul>
  * </ul>
  *
  * @param Engine\YandexDirect $engine Engine object.
  * @param array $data Campaign data.
  * @param Entity\EventResult $result Event result object.
  *
  * @return array
  * @see http://api.yandex.ru/direct/doc/reference/CreateOrUpdateCampaign.xml
  */
 protected static function createParam(Engine\YandexDirect $engine, array $data, Entity\EventResult $result)
 {
     $settings = $engine->getSettings();
     $campaignParam = array("Login" => $settings["AUTH_USER"]["login"]);
     $newCampaign = true;
     if (!empty($data["XML_ID"])) {
         $newCampaign = false;
         $campaignParam["CampaignID"] = $data["XML_ID"];
     }
     if ($newCampaign || isset($data['SETTINGS']["Name"])) {
         $campaignParam["Name"] = trim($data['SETTINGS']["Name"]);
         if (strlen($campaignParam["Name"]) <= 0) {
             $result->addError(new Entity\FieldError(static::getEntity()->getField('NAME'), Loc::getMessage('SEO_CAMPAIGN_ERROR_NO_NAME')));
         }
     }
     if ($newCampaign || isset($data["SETTINGS"]["FIO"])) {
         $campaignParam["FIO"] = trim($data["SETTINGS"]["FIO"]);
         if (strlen($campaignParam["FIO"]) <= 0) {
             $result->addError(new Entity\FieldError(static::getEntity()->getField('SETTINGS'), Loc::getMessage('SEO_CAMPAIGN_ERROR_NO_FIO')));
         }
     }
     if (is_array($data["SETTINGS"]) && array_key_exists("StartDate", $data["SETTINGS"])) {
         if (is_a($data["SETTINGS"]["StartDate"], "Bitrix\\Main\\Type\\Date")) {
             $campaignParam["StartDate"] = $data["SETTINGS"]["StartDate"]->convertFormatToPhp("Y-m-d");
         } elseif (is_string($data["SETTINGS"]["StartDate"])) {
             if (preg_match("/^\\d{4}-\\d{2}-\\d{2}\$/", $data["SETTINGS"]["StartDate"])) {
                 $campaignParam["StartDate"] = $data["SETTINGS"]["StartDate"];
             } else {
                 $ts = MakeTimeStamp($data["SETTINGS"]["StartDate"], FORMAT_DATE);
                 if ($ts > 0) {
                     $campaignParam["StartDate"] = date('Y-m-d', $ts);
                 }
             }
         }
         if (!$campaignParam["StartDate"]) {
             $result->addError(new Entity\FieldError(static::getEntity()->getField('SETTINGS'), Loc::getMessage('SEO_CAMPAIGN_ERROR_NO_START_DATE')));
         }
     }
     if ($newCampaign || isset($data["SETTINGS"]["Strategy"])) {
         if (empty($data["SETTINGS"]["Strategy"]) || !is_array($data["SETTINGS"]["Strategy"]) || empty($data["SETTINGS"]["Strategy"]["StrategyName"])) {
             $result->addError(new Entity\FieldError(static::getEntity()->getField('SETTINGS'), Loc::getMessage('SEO_CAMPAIGN_ERROR_NO_STRATEGY')));
         }
         if (array_key_exists($data["SETTINGS"]["Strategy"]["StrategyName"], self::$strategyConfig)) {
             $strategy = $data["SETTINGS"]["Strategy"]["StrategyName"];
             $config = self::$strategyConfig[$strategy];
             $campaignParam["Strategy"] = array("StrategyName" => $strategy);
             foreach ($data["SETTINGS"]["Strategy"] as $param => $value) {
                 if ($param !== "StrategyName") {
                     if (array_key_exists($param, $config)) {
                         $campaignParam["Strategy"][$param] = $value;
                     } else {
                         $result->addError(new Entity\FieldError(static::getEntity()->getField('SETTINGS'), Loc::getMessage('SEO_CAMPAIGN_ERROR_STRATEGY_PARAM_NOT_SUPPORTED', array('#PARAM#' => $param, '#STRATEGY#' => $strategy))));
                     }
                 }
             }
             foreach ($config as $key => $def) {
                 if ($def['mandatory'] || isset($campaignParam["Strategy"][$key])) {
                     switch ($def['type']) {
                         case 'int':
                             $campaignParam["Strategy"][$key] = intval($campaignParam["Strategy"][$key]);
                             break;
                         case 'float':
                             $campaignParam["Strategy"][$key] = doubleval($campaignParam["Strategy"][$key]);
                             break;
                     }
                     if (!$def['mandatory'] && empty($campaignParam["Strategy"][$key])) {
                         unset($campaignParam["Strategy"][$key]);
                     }
                 }
                 if ($def['mandatory'] && empty($campaignParam["Strategy"][$key])) {
                     $result->addError(new Entity\FieldError(static::getEntity()->getField('SETTINGS'), Loc::getMessage('SEO_CAMPAIGN_ERROR_STRATEGY_PARAM_MANDATORY', array('#PARAM#' => Loc::getMessage('SEO_CAMPAIGN_STRATEGY_PARAM_' . ToUpper($key)), '#STRATEGY#' => Loc::getMessage('SEO_CAMPAIGN_STRATEGY_' . $strategy)))));
                 }
             }
         } else {
             $result->addError(new Entity\FieldError(static::getEntity()->getField('SETTINGS'), Loc::getMessage('SEO_CAMPAIGN_ERROR_STRATEGY_NOT_SUPPORTED', array('#STRATEGY#' => $data["SETTINGS"]["Strategy"]["StrategyName"]))));
         }
     }
     if ($newCampaign || !empty($data["SETTINGS"]["EmailNotification"])) {
         if (empty($data["SETTINGS"]["EmailNotification"]) || !is_array($data["SETTINGS"]["EmailNotification"]) || !check_email($data["SETTINGS"]["EmailNotification"]['Email'])) {
             $result->addError(new Entity\FieldError(static::getEntity()->getField('SETTINGS'), Loc::getMessage('SEO_CAMPAIGN_ERROR_WRONG_EMAIL')));
         }
         $campaignParam["EmailNotification"] = array("Email" => trim($data["SETTINGS"]["EmailNotification"]['Email']), "WarnPlaceInterval" => intval($data["SETTINGS"]["EmailNotification"]['WarnPlaceInterval']), "MoneyWarningValue" => intval($data["SETTINGS"]["EmailNotification"]['MoneyWarningValue']), "SendWarn" => intval($data["SETTINGS"]["EmailNotification"]['SendWarn']));
         if ($campaignParam["EmailNotification"]['SendWarn'] === true || $campaignParam["EmailNotification"]['SendWarn'] === 1 || $campaignParam["EmailNotification"]['SendWarn'] === 'Y') {
             $campaignParam["EmailNotification"]['SendWarn'] = Engine\YandexDirect::BOOL_YES;
         }
         if ($campaignParam["EmailNotification"]['SendWarn'] === false || $campaignParam["EmailNotification"]['SendWarn'] === 0 || $campaignParam["EmailNotification"]['SendWarn'] === 'N') {
             $campaignParam["EmailNotification"]['SendWarn'] = Engine\YandexDirect::BOOL_NO;
         }
         if (!in_array($campaignParam["EmailNotification"]["WarnPlaceInterval"], self::$allowedWarnPlaceIntervalValues)) {
             if ($campaignParam["EmailNotification"]['SendWarn'] == Engine\YandexDirect::BOOL_YES) {
                 $result->addError(new Entity\FieldError(static::getEntity()->getField('SETTINGS'), Loc::getMessage('SEO_CAMPAIGN_ERROR_WRONG_INTERVAL', array('#VALUES#' => implode(',', self::$allowedWarnPlaceIntervalValues)))));
             } else {
                 $campaignParam["EmailNotification"]["WarnPlaceInterval"] = self::MONEY_WARN_PLACE_INTERVAL_DEFAULT;
             }
         }
         if ($campaignParam["EmailNotification"]["MoneyWarningValue"] < self::$allowedMoneyWarningInterval[0] || $campaignParam["EmailNotification"]["MoneyWarningValue"] > self::$allowedMoneyWarningInterval[1]) {
             $result->addError(new Entity\FieldError(static::getEntity()->getField('SETTINGS'), Loc::getMessage('SEO_CAMPAIGN_ERROR_WRONG_WARNING', array('#MIN#' => self::$allowedMoneyWarningInterval[0], '#MAX#' => self::$allowedMoneyWarningInterval[1]))));
         }
     }
     if ($newCampaign || isset($data["SETTINGS"]["MinusKeywords"])) {
         if (!is_array($data["SETTINGS"]["MinusKeywords"])) {
             if (strlen($data["SETTINGS"]["MinusKeywords"]) > 0) {
                 $data["SETTINGS"]["MinusKeywords"] = array();
             } else {
                 $data["SETTINGS"]["MinusKeywords"] = array($data["SETTINGS"]["MinusKeywords"]);
             }
         }
         $campaignParam["MinusKeywords"] = $data["SETTINGS"]["MinusKeywords"];
     }
     if (!$newCampaign && $result->getType() == Entity\EventResult::SUCCESS) {
         try {
             $yandexCampaignParam = $engine->getCampaign($data["XML_ID"]);
             if (!is_array($yandexCampaignParam) || count($yandexCampaignParam) <= 0) {
                 $result->addError(new Entity\FieldError(static::getEntity()->getField('XML_ID'), Loc::getMessage('SEO_CAMPAIGN_ERROR_CAMPAIGN_NOT_FOUND', array('#ID#' => $data["XML_ID"]))));
             } else {
                 $campaignParam = array_replace_recursive($yandexCampaignParam[0], $campaignParam);
             }
         } catch (Engine\YandexDirectException $e) {
             $result->addError(new Entity\FieldError(static::getEntity()->getField('ENGINE_ID'), $e->getMessage(), $e->getCode()));
         }
     }
     return $campaignParam;
 }