case 'moderate':
						$engine->moderateBanners($campaign['XML_ID'], array_keys($bannersList));
						$engine->updateCampaignManual($campaign['ID']);
						break;

					case 'archive':
						$engine->archiveBanners($campaign['XML_ID'], array_keys($bannersList));
						break;

					case 'unarchive':
						$engine->unArchiveBanners($campaign['XML_ID'], array_keys($bannersList));
						break;
				}

				$bannersListRemote = $engine->getBanners(array_keys($bannersList));
				$bannersListSorted = array();
				foreach($bannersListRemote as $key => $bannerInfo)
				{
					$bannersListSorted[$bannerInfo['BannerID']] = $bannerInfo;
				}

				Adv\YandexBannerTable::setSkipRemoteUpdate(true);
				foreach($bannersList as $bannerXmlId => $bannerId)
				{
					if(isset($bannersListSorted[$bannerXmlId]))
					{
						$result = Adv\YandexBannerTable::update(
							$bannerId, array(
								"SETTINGS" => $bannersListSorted[$bannerXmlId]
							)
Beispiel #2
0
	/**
	 * Checks banner data before sending it to Yandex.
	 *
	 * $data array format:
	 *
	 * <ul>
	 * <li>ID
	 * <li>XML_ID
	 * <li>NAME
	 * <li>SETTINGS<ul>
	 *    <li>BannerID
	 *    <li>CampaignID *
	 *    <li>Title *
	 *    <li>Text *
	 *    <li>Href *
	 *    <li>Geo - comma-separated list of yandex location IDs
	 *    <li>Phrases *
	 *    <li>MinusKeywords
	 *  </ul>
	 * </ul>
	 *
	 * @param Engine\YandexDirect $engine Engine object.
	 * @param array $data Banner data.
	 * @param Entity\EventResult $result Event result object.
	 *
	 * @return array
	 * @see http://api.yandex.ru/direct/doc/reference/CreateOrUpdateBanner.xml
	 */
	protected static function createParam(Engine\YandexDirect $engine, array $data, Entity\EventResult $result)
	{
		$bannerParam = array();

		$newBanner = true;

		if(!empty($data["XML_ID"]))
		{
			$newBanner = false;
			$bannerParam["BannerID"] = $data["XML_ID"];
		}

		if(!empty($data["CAMPAIGN_ID"]))
		{
			$dbRes = YandexCampaignTable::getByPrimary($data["CAMPAIGN_ID"]);
			$campaign = $dbRes->fetch();
			if($campaign)
			{
				$data['SETTINGS']['CampaignID'] = $campaign['XML_ID'];
			}
			else
			{
				$result->addError(new Entity\FieldError(
					static::getEntity()->getField('CAMPAIGN_ID'),
					Loc::getMessage('SEO_BANNER_ERROR_CAMPAIGN_NOT_FOUND')
				));
			}
		}

		if($newBanner || isset($data['SETTINGS']['CampaignID']))
		{
			$bannerParam['CampaignID'] = $data['SETTINGS']['CampaignID'];
		}

		if($newBanner || isset($data['SETTINGS']["Title"]))
		{
			$bannerParam["Title"] = trim($data['SETTINGS']["Title"]);

			if(strlen($bannerParam["Title"]) <= 0)
			{
				$result->addError(new Entity\FieldError(
					static::getEntity()->getField('NAME'),
					Loc::getMessage('SEO_BANNER_ERROR_NO_NAME')
				));
			}
			elseif(strlen($bannerParam["Title"]) > static::MAX_TITLE_LENGTH)
			{
				$result->addError(new Entity\FieldError(
					static::getEntity()->getField('NAME'),
					Loc::getMessage('SEO_BANNER_ERROR_LONG_NAME', array(
						"#MAX#" => static::MAX_TITLE_LENGTH,
					))
				));
			}
		}

		if($newBanner || isset($data['SETTINGS']["Text"]))
		{
			$bannerParam["Text"] = trim($data['SETTINGS']["Text"]);
			if(strlen($bannerParam["Text"]) <= 0)
			{
				$result->addError(new Entity\FieldError(
					static::getEntity()->getField('SETTINGS'),
					Loc::getMessage('SEO_BANNER_ERROR_NO_TEXT')
				));
			}
			elseif(strlen($bannerParam["Text"]) > static::MAX_TEXT_LENGTH)
			{
				$result->addError(new Entity\FieldError(
					static::getEntity()->getField('SETTINGS'),
					Loc::getMessage('SEO_BANNER_ERROR_LONG_TEXT', array(
						"#MAX#" => static::MAX_TEXT_LENGTH,
					))
				));
			}
		}

		if($newBanner || isset($data['SETTINGS']["Href"]))
		{
			$bannerParam["Href"] = trim($data['SETTINGS']["Href"]);
			if(strlen($bannerParam["Href"]) <= 0)
			{
				$result->addError(new Entity\FieldError(
					static::getEntity()->getField('SETTINGS'),
					Loc::getMessage('SEO_BANNER_ERROR_NO_HREF')
				));
			}
		}

		if($newBanner || isset($data["SETTINGS"]["Geo"]))
		{
			if(is_array($data["SETTINGS"]["Geo"]))
			{
				$data["SETTINGS"]["Geo"] = implode(",", $data["SETTINGS"]["Geo"]);
			}

			$bannerParam["Geo"] = $data["SETTINGS"]["Geo"];
		}

		if($newBanner || isset($data["SETTINGS"]["Phrases"]))
		{
			if(!is_array($data["SETTINGS"]["Phrases"]) || count($data["SETTINGS"]["Phrases"]) <= 0)
			{
				$result->addError(new Entity\FieldError(
					static::getEntity()->getField('SETTINGS'),
					Loc::getMessage('SEO_BANNER_ERROR_NO_PHRASES')
				));
			}
			else
			{
				$bannerParam["Phrases"] = $data["SETTINGS"]["Phrases"];

				foreach($bannerParam["Phrases"] as $key => $phraseInfo)
				{
					$phraseInfo['AutoBudgetPriority'] = static::$priorityList[intval($phraseInfo['AutoBudgetPriority'])];

					$bannerParam["Phrases"][$key] = $phraseInfo;
				}
			}
		}

		if($newBanner || 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"]);
				}
			}

			$bannerParam["MinusKeywords"] = $data["SETTINGS"]["MinusKeywords"];
		}

		if(!$newBanner && $result->getType() == Entity\EventResult::SUCCESS)
		{
			try
			{
				$yandexBannerParam = $engine->getBanners(array($data["XML_ID"]));

				if(!is_array($yandexBannerParam) || count($yandexBannerParam) <= 0)
				{
					$result->addError(new Entity\FieldError(
						static::getEntity()->getField('XML_ID'),
						Loc::getMessage(
							'SEO_CAMPAIGN_ERROR_BANNER_NOT_FOUND',
							array('#ID#' => $data["XML_ID"])
						)
					));
				}
				else
				{
					$bannerParam = array_replace_recursive($yandexBannerParam[0], $bannerParam);
				}
			}
			catch(Engine\YandexDirectException $e)
			{
				$result->addError(
					new Entity\FieldError(
						static::getEntity()->getField('ENGINE_ID'),
						$e->getMessage(),
						$e->getCode()
					)
				);
			}
		}

		return $bannerParam;
	}