13use Bitrix\Main\Entity\ExpressionField;
26if (!defined(
'YANDEX_DIRECT_API_URL'))
28 define(
'YANDEX_DIRECT_API_URL',
'https://api.direct.yandex.ru/v4/json/');
104 $this->locale = in_array(LANGUAGE_ID, array(
"ru",
"en",
"ua")) ? LANGUAGE_ID :
'en';
106 parent::__construct();
115 return $currentAuth[
'user'];
135 $result = $this->
getProxy()->getInterface()->addCampaign(static::ENGINE_ID, $campaignParam);
137 if (!empty($result[
'error']))
157 $result = $this->
getProxy()->getInterface()->updateCampaign(static::ENGINE_ID, $campaignParam);
159 if (!empty($result[
'error']))
178 if (empty($campaignsId))
183 if (!is_array($campaignsId))
185 $campaignsId = array($campaignsId);
192 while ($offset < count($campaignsId))
194 $currentCampaigns = array_slice($campaignsId, $offset, static::CAMPAIGN_LIMIT);
196 $currentResult = $this->
getProxy()->getInterface()->getCampaign(static::ENGINE_ID, $currentCampaigns);
198 if (!empty($currentResult[
'error']))
203 $result = array_merge($result, $currentResult);
205 $offset += static::CAMPAIGN_LIMIT;
214 $result = $this->
getProxy()->getInterface()->getCampaignList(static::ENGINE_ID);
216 if (!empty($result[
'error']))
226 if (empty($campaignId))
231 $result = $this->
getProxy()->getInterface()->archiveCampaign(static::ENGINE_ID, $campaignId);
233 if (!empty($result[
'error']))
243 if (empty($campaignId))
248 $result = $this->
getProxy()->getInterface()->unArchiveCampaign(static::ENGINE_ID, $campaignId);
250 if (!empty($result[
'error']))
260 if (empty($campaignId))
265 $result = $this->
getProxy()->getInterface()->resumeCampaign(static::ENGINE_ID, $campaignId);
267 if (!empty($result[
'error']))
277 if (empty($campaignId))
282 $result = $this->
getProxy()->getInterface()->stopCampaign(static::ENGINE_ID, $campaignId);
284 if (!empty($result[
'error']))
294 if (empty($campaignId))
299 $result = $this->
getProxy()->getInterface()->deleteCampaign(static::ENGINE_ID, $campaignId);
301 if (!empty($result[
'error']))
321 $result = $this->
getProxy()->getInterface()->addBanner(static::ENGINE_ID, $bannerParam);
323 if (!empty($result[
'error']))
343 $result = $this->
getProxy()->getInterface()->updateBanner(static::ENGINE_ID, $bannerParam);
345 if (!empty($result[
'error']))
355 if (empty($bannerId))
360 if (!is_array($bannerId))
362 $bannerId = array($bannerId);
365 $result = $this->
getProxy()->getInterface()->getBannerList(static::ENGINE_ID, array(
366 'BannerIDS' => $bannerId,
369 if (!empty($result[
'error']))
379 if (empty($campaignId))
384 if (!is_array($campaignId))
386 $campaignId = array($campaignId);
389 $result = $this->
getProxy()->getInterface()->getBannerList(static::ENGINE_ID, array(
390 'CampaignIDS' => $campaignId,
393 if (!empty($result[
'error']))
403 if (empty($campaignId))
409 'CampaignID' => $campaignId,
410 'BannerIDS' => $bannerIDs,
413 $result = $this->
getProxy()->getInterface()->moderateBanners(static::ENGINE_ID, $queryData);
415 if (!empty($result[
'error']))
425 if (empty($campaignId))
431 'CampaignID' => $campaignId,
432 'BannerIDS' => $bannerIDs,
436 $result = $this->
getProxy()->getInterface()->stopBanners(static::ENGINE_ID, $queryData);
438 if (!empty($result[
'error']))
448 if (empty($campaignId))
454 'CampaignID' => $campaignId,
455 'BannerIDS' => $bannerIDs,
458 $result = $this->
getProxy()->getInterface()->resumeBanners(static::ENGINE_ID, $queryData);
460 if (!empty($result[
'error']))
470 if (empty($campaignId))
476 'CampaignID' => $campaignId,
477 'BannerIDS' => $bannerIDs,
480 $result = $this->
getProxy()->getInterface()->archiveBanners(static::ENGINE_ID, $queryData);
482 if (!empty($result[
'error']))
492 if (empty($campaignId))
498 'CampaignID' => $campaignId,
499 'BannerIDS' => $bannerIDs,
502 $result = $this->
getProxy()->getInterface()->unArchiveBanners(static::ENGINE_ID, $queryData);
504 if (!empty($result[
'error']))
514 if (empty($campaignId))
520 'CampaignID' => $campaignId,
521 'BannerIDS' => $bannerIDs,
524 $result = $this->
getProxy()->getInterface()->deleteBanners(static::ENGINE_ID, $queryData);
526 if (!empty($result[
'error']))
543 $result = $this->
getProxy()->getInterface()->getRegions(static::ENGINE_ID);
545 if (!empty($result[
'error']))
557 if ($cacheManager->read(self::CACHE_TTL, self::CACHE_ID))
559 $result = $cacheManager->get(self::CACHE_ID);
563 $result = $this->
getProxy()->getInterface()->getClientsSettings(static::ENGINE_ID);
566 if(!is_array($result) || empty($result))
567 $result = array(
'error' =>
'No authentication.');
569 if (!empty($result[
'error']))
572 $cacheManager->set(self::CACHE_ID, $result);
580 'Phrases' => $phrase,
585 $queryData[
'GeoID'] = $geo;
588 $result = $this->
getProxy()->getInterface()->createWordstatReport(static::ENGINE_ID, $queryData);
590 if (!empty($result[
'error']))
600 $result = $this->
getProxy()->getInterface()->deleteWordstatReport(static::ENGINE_ID, $reportId);
602 if (!empty($result[
'error']))
612 $result = $this->
getProxy()->getInterface()->getWordstatReport(static::ENGINE_ID, $reportId);
614 if (!empty($result[
'error']))
624 $result = $this->
getProxy()->getInterface()->getWordstatReportList(static::ENGINE_ID);
626 if (!empty($result[
'error']))
637 'Phrases' => $phrase,
642 $queryData[
'GeoID'] = $geo;
645 $result = $this->
getProxy()->getInterface()->createForecastReport(static::ENGINE_ID, $queryData);
647 if (!empty($result[
'error']))
657 $result = $this->
getProxy()->getInterface()->deleteForecastReport(static::ENGINE_ID, $reportId);
659 if (!empty($result[
'error']))
669 $result = $this->
getProxy()->getInterface()->getForecastReport(static::ENGINE_ID, $reportId);
671 if (!empty($result[
'error']))
681 $result = $this->
getProxy()->getInterface()->getForecastReportList(static::ENGINE_ID);
683 if (!empty($result[
'error']))
704 $result = $this->
getProxy()->getInterface()->getBannerStats(static::ENGINE_ID, $params);
706 if (!empty($result[
'error']))
725 protected function query($scope, $method =
"GET", $param = NULL, $skipRefreshAuth =
false)
732 if ($this->engineSettings[
'AUTH'])
735 $http->setRedirect(
false);
736 $http->setHeader(
"Content-Type",
"application/json; charset=utf-8");
740 "locale" => $this->locale,
741 "token" => $this->engineSettings[
'AUTH'][
'access_token'],
746 $postData[
"param"] = $param;
751 $ts = microtime(
true);
752 $http->post(static::API_URL, $postData);
755 'ENGINE_ID' => $this->
getId(),
756 'REQUEST_URI' => static::API_URL,
757 'REQUEST_DATA' => Text\Encoding::convertEncoding($postData,
'UTF-8', SITE_CHARSET),
758 'RESPONSE_TIME' => microtime(
true) - $ts,
759 'RESPONSE_STATUS' => $http->getStatus(),
760 'RESPONSE_DATA' => Text\Encoding::convertEncoding($http->getResult(),
'UTF-8', SITE_CHARSET),
763 if ($http->getStatus() == 401 && !$skipRefreshAuth)
767 $this->
query($scope, $method, $param,
true);
779 public function finance_query($method, $masterToken, $operationNum, $param = array(), $skipRefreshAuth =
false)
781 if ($this->engineSettings[
'AUTH'])
784 $http->setRedirect(
false);
785 $http->setHeader(
"Content-Type",
"application/json; charset=utf-8");
789 $financeToken = hash(
791 $masterToken . $operationNum . $method . $auth[
'login']);
795 "finance_token" => $financeToken,
796 "operation_num" => $operationNum,
797 "locale" => $this->locale,
798 "token" => $this->engineSettings[
'AUTH'][
'access_token'],
803 $postData[
"param"] = $param;
808 $http->post(self::API_URL, $postData);
810 if ($http->getStatus() == 401 && !$skipRefreshAuth)
814 $this->
query(
"", $method, $param,
true);
828 $newCampaigns = array();
838 if (!is_array($campaignId) && $campaignId > 0)
840 $campaignId = array($campaignId);
843 $campaignList = array();
844 if (is_array($campaignId) && count($campaignId) > 0)
847 $dbRes = YandexCampaignTable::getList(array(
849 '=ID' => $campaignId,
850 '=ENGINE_ID' => $this->
getId(),
852 'select' => array(
'XML_ID'),
855 while ($campaign = $dbRes->fetch())
857 $keys[] = $campaign[
'XML_ID'];
860 if (count($keys) > 0)
869 $campaignListSorted = array();
870 $campaignListToDelete = array();
872 foreach ($campaignList as $campaignInfo)
874 $campaignListSorted[$campaignInfo[
'CampaignID']] = $campaignInfo;
877 $filter = array(
'=ENGINE_ID' => $this->
getId());
879 if(is_array($campaignId) && count($campaignId) > 0 && count($campaignListSorted) > 0)
880 $filter[
'=XML_ID'] = array_keys($campaignListSorted);
881 $dbCampaigns = YandexCampaignTable::getList(array(
'filter' => $filter));
885 while ($campaign = $dbCampaigns->fetch())
887 if (isset($campaignListSorted[$campaign[
'XML_ID']]))
889 $result = YandexCampaignTable::update(
890 $campaign[
'ID'], array(
891 "SETTINGS" => $campaignListSorted[$campaign[
'XML_ID']],
895 unset($campaignListSorted[$campaign[
'XML_ID']]);
897 if ($result->isSuccess())
909 $campaignListToDelete[$campaign[
'ID']] = $campaign[
'ID'];
914 if (count($campaignListToDelete) > 0)
916 foreach ($campaignListToDelete as $campaignId)
918 $resultDelete = YandexCampaignTable::delete($campaignId);
925 foreach ($campaignListSorted as $campaignId => $campaignInfo)
927 $result = YandexCampaignTable::add(array(
928 "SETTINGS" => $campaignInfo,
931 if ($result->isSuccess())
933 $newCampaigns[] = $result->getId();
943 if (count($newCampaigns) > 0)
947 $res[
'new'] = $newCampaigns;
949 $res[
'banner'] = array();
950 $cnt = ceil(count($newCampaigns) / static::MAX_CAMPAIGNS_BANNER_UPDATE);
951 for ($i = 0; $i < $cnt; $i++)
953 $res[
'banner'] = array_merge(
958 $i * static::MAX_CAMPAIGNS_BANNER_UPDATE,
959 static::MAX_CAMPAIGNS_BANNER_UPDATE
977 if (!is_array($bannerId) && $bannerId > 0)
979 $bannerId = array($bannerId);
982 $bannerList = array();
983 if (is_array($bannerId) && count($bannerId) > 0)
986 $dbRes = YandexBannerTable::getList(array(
989 '=ENGINE_ID' => $this->
getId(),
991 'select' => array(
'XML_ID'),
994 while ($banner = $dbRes->fetch())
996 $keys[] = $banner[
'XML_ID'];
1004 $dbCampaigns = YandexCampaignTable::getList(array(
1006 '=ID' => $campaignId,
1007 '=ENGINE_ID' => $this->
getId(),
1009 'select' => array(
'ID',
'XML_ID'),
1012 while ($campaign = $dbCampaigns->fetch())
1014 $campaignIndex[$campaign[
'XML_ID']] = $campaign[
'ID'];
1018 if (count($campaignIndex) > 0)
1022 $bannerListSorted = array();
1023 $bannerListToDelete = array();
1024 foreach ($bannerList as $bannerInfo)
1026 $bannerListSorted[$bannerInfo[
'BannerID']] = $bannerInfo;
1029 $filter = array(
'=ENGINE_ID' => $this->
getId());
1031 if(is_array($bannerId) && count($bannerId) > 0 && count($bannerListSorted) > 0)
1032 $filter[
'=XML_ID'] = array_keys($bannerListSorted);
1034 $filter[
'=CAMPAIGN_ID'] = $campaignId;
1035 $dbBanners = YandexBannerTable::getList(array(
'filter' => $filter));
1037 YandexBannerTable::setSkipRemoteUpdate(
true);
1039 while ($banner = $dbBanners->fetch())
1041 if (isset($bannerListSorted[$banner[
'XML_ID']]))
1043 $result = YandexBannerTable::update(
1044 $banner[
'ID'], array(
1045 "SETTINGS" => $bannerListSorted[$banner[
'XML_ID']],
1049 unset($bannerListSorted[$banner[
'XML_ID']]);
1051 if ($result->isSuccess())
1063 $bannerListToDelete[$banner[
'ID']] = $banner[
'ID'];
1068 if (count($bannerListToDelete) > 0)
1070 foreach ($bannerListToDelete as $bannerId)
1072 $resultDelete = YandexBannerTable::delete($bannerId);
1073 YandexBannerTable::setSkipRemoteUpdate(
true);
1078 foreach ($bannerListSorted as $bannerId => $bannerInfo)
1080 $result = YandexBannerTable::add(array(
1081 "CAMPAIGN_ID" => $campaignIndex[$bannerInfo[
'CampaignID']],
1082 "SETTINGS" => $bannerInfo,
1085 if ($result->isSuccess())
1094 YandexBannerTable::setSkipRemoteUpdate(
false);
1108 return __CLASS__ .
"::updateAgent();";
1111 if (
$engine->getAuthSettings())
1115 $dbRes = YandexCampaignTable::getList(array(
1118 '=ENGINE_ID' =>
$engine->getId(),
1120 'select' => array(
'CNT'),
1126 $res = $dbRes->fetch();
1127 if ($res[
'CNT'] > 0)
1129 $engine->updateCampaignManual();
1132 $availableCampaigns = array();
1133 $campaignList =
$engine->getCampaignList();
1134 foreach ($campaignList as $campaignInfo)
1136 $availableCampaigns[] = $campaignInfo[
'CampaignID'];
1139 if (count($availableCampaigns) > 0)
1141 $dbRes = YandexBannerTable::getList(array(
1142 'group' => array(
'CAMPAIGN_ID'),
1145 '=ENGINE_ID' =>
$engine->getId(),
1146 '=CAMPAIGN.XML_ID' => $availableCampaigns,
1148 'select' => array(
'CAMPAIGN_ID'),
1151 $campaignId = array();
1152 while ($res = $dbRes->fetch())
1154 $campaignId[] = $res[
'CAMPAIGN_ID'];
1157 if (count($campaignId) > 0)
1159 $engine->updateBannersManual($campaignId);
1168 return __CLASS__ .
"::updateAgent();";
static createFromTimestamp($timestamp)
static setSkipRemoteUpdate($value)
const ERROR_WRONG_CURRENCY
const METHOD_FORECAST_REPORT_CREATE
const MAX_CAMPAIGNS_BANNER_UPDATE
updateBanner(array $bannerParam)
deleteCampaign($campaignId)
getForecastReport($reportId)
getCampaign($campaignsId)
const METHOD_CAMPAIGN_ARCHIVE
const METHOD_WORDSTAT_REPORT_CREATE
unArchiveCampaign($campaignId)
const METHOD_BANNER_MODERATE
resumeCampaign($campaignId)
createWordstatReport(array $phrase, $geo=NULL)
const METHOD_BANNER_UPDATE
const METHOD_CAMPAIGN_DELETE
const METHOD_WORDSTAT_REPORT_LIST
stopBanners($campaignId, array $bannerIDs)
updateCampaign(array $campaignParam)
const METHOD_FORECAST_REPORT_DELETE
addBanner(array $bannerParam)
addCampaign(array $campaignParam)
getCampaignBanners($campaignId)
const METHOD_FORECAST_REPORT_GET
const METHOD_CAMPAIGN_STOP
const METHOD_BANNER_UNARCHIVE
const METHOD_CAMPAIGN_LIST
updateCampaignManual($campaignId=NULL)
query($scope, $method="GET", $param=NULL, $skipRefreshAuth=false)
deleteForecastReport($reportId)
const METHOD_CAMPAIGN_ADD
createForecastReport(array $phrase, $geo=NULL)
const MAX_STAT_DAYS_DELTA
const METHOD_BANNER_DELETE
resumeBanners($campaignId, array $bannerIDs)
const METHOD_CAMPAIGN_RESUME
const METHOD_FORECAST_REPORT_LIST
const TTL_WORDSTAT_REPORT
archiveBanners($campaignId, array $bannerIDs)
moderateBanners($campaignId, array $bannerIDs)
const METHOD_BANNER_ARCHIVE
stopCampaign($campaignId)
const METHOD_CAMPAIGN_GET
const METHOD_CAMPAIGN_UNARCHIVE
unArchiveBanners($campaignId, array $bannerIDs)
deleteWordstatReport($reportId)
finance_query($method, $masterToken, $operationNum, $param=array(), $skipRefreshAuth=false)
const METHOD_CAMPAIGN_UPDATE
const TTL_FORECAST_REPORT
getWordstatReport($reportId)
getBannerStats(array $params)
updateBannersManual($campaignId, $bannerId=NULL)
const TTL_FORECAST_REPORT_EXT
const METHOD_WORDSTAT_REPORT_GET
deleteBanners($campaignId, array $bannerIDs)
const TTL_WORDSTAT_REPORT_EXT
const METHOD_WORDSTAT_REPORT_DELETE
const METHOD_BANNER_RESUME
archiveCampaign($campaignId)
const MAX_WORDSTAT_REPORTS
const MAX_FORECAST_REPORTS
static encode($data, $options=null)
static getAuth(string $engineCode)