Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
yandexdirect.php
1<?
8namespace Bitrix\Seo\Engine;
9
13use Bitrix\Main\Entity\ExpressionField;
24
25// to use Yandex.Direct Sandbox
26if (!defined('YANDEX_DIRECT_API_URL'))
27{
28 define('YANDEX_DIRECT_API_URL', 'https://api.direct.yandex.ru/v4/json/');
29}
30
31class YandexDirect extends Engine\YandexBase implements IEngine
32{
33 const ENGINE_ID = 'yandex_direct';
34
35 const API_URL = YANDEX_DIRECT_API_URL;
36
37 const METHOD_REGION_GET = 'GetRegions';
38 const METHOD_CAMPAIGN_ADD = 'CreateOrUpdateCampaign';
39 const METHOD_CAMPAIGN_UPDATE = 'CreateOrUpdateCampaign';
40 const METHOD_CAMPAIGN_GET = 'GetCampaignsParams';
41 const METHOD_CAMPAIGN_LIST = 'GetCampaignsList';
42 const METHOD_CAMPAIGN_ARCHIVE = 'ArchiveCampaign';
43 const METHOD_CAMPAIGN_UNARCHIVE = 'UnArchiveCampaign';
44 const METHOD_CAMPAIGN_STOP = 'StopCampaign';
45 const METHOD_CAMPAIGN_RESUME = 'ResumeCampaign';
46 const METHOD_CAMPAIGN_DELETE = 'DeleteCampaign';
47 const METHOD_BANNER_ADD = 'CreateOrUpdateBanners';
48 const METHOD_BANNER_UPDATE = 'CreateOrUpdateBanners';
49 const METHOD_BANNER_LIST = 'GetBanners';
50 const METHOD_BANNER_MODERATE = 'ModerateBanners';
51 const METHOD_BANNER_STOP = 'StopBanners';
52 const METHOD_BANNER_RESUME = 'ResumeBanners';
53 const METHOD_BANNER_ARCHIVE = 'ArchiveBanners';
54 const METHOD_BANNER_UNARCHIVE = 'UnArchiveBanners';
55 const METHOD_BANNER_DELETE = 'DeleteBanners';
56 const METHOD_WORDSTAT_REPORT_CREATE = 'CreateNewWordstatReport';
57 const METHOD_WORDSTAT_REPORT_DELETE = 'DeleteWordstatReport';
58 const METHOD_WORDSTAT_REPORT_GET = 'GetWordstatReport';
59 const METHOD_WORDSTAT_REPORT_LIST = 'GetWordstatReportList';
60 const METHOD_FORECAST_REPORT_CREATE = 'CreateNewForecast';
61 const METHOD_FORECAST_REPORT_DELETE = 'DeleteForecastReport';
62 const METHOD_FORECAST_REPORT_GET = 'GetForecast';
63 const METHOD_FORECAST_REPORT_LIST = 'GetForecastList';
64 const METHOD_STAT_BANNER = 'GetBannersStat';
65
66 const BOOL_YES = "Yes";
67 const BOOL_NO = "No";
68
69 const STATUS_NEW = "New";
70 const STATUS_PENDING = "Pending";
71
72 const PRIORITY_LOW = "Low";
73 const PRIORITY_MEDIUM = "Medium";
74 const PRIORITY_HIGH = "High";
75
76 const TTL_WORDSTAT_REPORT = 3600; // session report lifetime
77 const TTL_WORDSTAT_REPORT_EXT = 18000; // yandex report lifetime
78 const TTL_FORECAST_REPORT = 3600; // session report lifetime
79 const TTL_FORECAST_REPORT_EXT = 18000; // yandex report lifetime
80
84
85 const ERROR_NOT_FOUND = 27;
86 const ERROR_NO_STATS = 2;
87
89 const CAMPAIGN_LIMIT = 100;
90
92
93 const CACHE_DIR = '/seo/yandexdirect/';
94 const CACHE_TTL = 86400;
95 const CACHE_ID = "yandexdirect_client_cache";
96
97 public $allowedCurrency = array('RUB', 'CHF', 'EUR', 'KZT', 'TRY', 'UAH', 'USD');
98
99 protected $engineId = 'yandex_direct';
100 protected $locale = NULL;
101
102 public function __construct()
103 {
104 $this->locale = in_array(LANGUAGE_ID, array("ru", "en", "ua")) ? LANGUAGE_ID : 'en';
105
106 parent::__construct();
107 }
108
109 public function getCurrentUser()
110 {
112 {
113 $currentAuth = Service::getAuth($this->getCode());
114
115 return $currentAuth['user'];
116 }
117 else
118 {
119 return false;
120 }
121 }
122
133 public function addCampaign(array $campaignParam)
134 {
135 $result = $this->getProxy()->getInterface()->addCampaign(static::ENGINE_ID, $campaignParam);
136
137 if (!empty($result['error']))
138 {
139 throw new YandexDirectException($result);
140 }
141
142 return $result;
143 }
144
155 public function updateCampaign(array $campaignParam)
156 {
157 $result = $this->getProxy()->getInterface()->updateCampaign(static::ENGINE_ID, $campaignParam);
158
159 if (!empty($result['error']))
160 {
161 throw new YandexDirectException($result);
162 }
163
164 return $result;
165 }
166
176 public function getCampaign($campaignsId)
177 {
178 if (empty($campaignsId))
179 {
180 throw new ArgumentNullException("campaignId");
181 }
182
183 if (!is_array($campaignsId))
184 {
185 $campaignsId = array($campaignsId);
186 }
187
188 $offset = 0;
189
190 $result = array();
191
192 while ($offset < count($campaignsId))
193 {
194 $currentCampaigns = array_slice($campaignsId, $offset, static::CAMPAIGN_LIMIT);
195
196 $currentResult = $this->getProxy()->getInterface()->getCampaign(static::ENGINE_ID, $currentCampaigns);
197
198 if (!empty($currentResult['error']))
199 {
200 throw new YandexDirectException($currentResult);
201 }
202
203 $result = array_merge($result, $currentResult);
204
205 $offset += static::CAMPAIGN_LIMIT;
206 }
207
208 return $result;
209 }
210
211// get ALL campaigns for current client
212 public function getCampaignList()
213 {
214 $result = $this->getProxy()->getInterface()->getCampaignList(static::ENGINE_ID);
215
216 if (!empty($result['error']))
217 {
218 throw new YandexDirectException($result);
219 }
220
221 return $result;
222 }
223
224 public function archiveCampaign($campaignId)
225 {
226 if (empty($campaignId))
227 {
228 throw new ArgumentNullException("campaignId");
229 }
230
231 $result = $this->getProxy()->getInterface()->archiveCampaign(static::ENGINE_ID, $campaignId);
232
233 if (!empty($result['error']))
234 {
235 throw new YandexDirectException($result);
236 }
237
238 return $result;
239 }
240
241 public function unArchiveCampaign($campaignId)
242 {
243 if (empty($campaignId))
244 {
245 throw new ArgumentNullException("campaignId");
246 }
247
248 $result = $this->getProxy()->getInterface()->unArchiveCampaign(static::ENGINE_ID, $campaignId);
249
250 if (!empty($result['error']))
251 {
252 throw new YandexDirectException($result);
253 }
254
255 return $result;
256 }
257
258 public function resumeCampaign($campaignId)
259 {
260 if (empty($campaignId))
261 {
262 throw new ArgumentNullException("campaignId");
263 }
264
265 $result = $this->getProxy()->getInterface()->resumeCampaign(static::ENGINE_ID, $campaignId);
266
267 if (!empty($result['error']))
268 {
269 throw new YandexDirectException($result);
270 }
271
272 return $result;
273 }
274
275 public function stopCampaign($campaignId)
276 {
277 if (empty($campaignId))
278 {
279 throw new ArgumentNullException("campaignId");
280 }
281
282 $result = $this->getProxy()->getInterface()->stopCampaign(static::ENGINE_ID, $campaignId);
283
284 if (!empty($result['error']))
285 {
286 throw new YandexDirectException($result);
287 }
288
289 return $result;
290 }
291
292 public function deleteCampaign($campaignId)
293 {
294 if (empty($campaignId))
295 {
296 throw new ArgumentNullException("campaignId");
297 }
298
299 $result = $this->getProxy()->getInterface()->deleteCampaign(static::ENGINE_ID, $campaignId);
300
301 if (!empty($result['error']))
302 {
303 throw new YandexDirectException($result);
304 }
305
306 return $result;
307 }
308
319 public function addBanner(array $bannerParam)
320 {
321 $result = $this->getProxy()->getInterface()->addBanner(static::ENGINE_ID, $bannerParam);
322
323 if (!empty($result['error']))
324 {
325 throw new YandexDirectException($result);
326 }
327
328 return $result;
329 }
330
341 public function updateBanner(array $bannerParam)
342 {
343 $result = $this->getProxy()->getInterface()->updateBanner(static::ENGINE_ID, $bannerParam);
344
345 if (!empty($result['error']))
346 {
347 throw new YandexDirectException($result);
348 }
349
350 return $result;
351 }
352
353 public function getBanners($bannerId)
354 {
355 if (empty($bannerId))
356 {
357 throw new ArgumentNullException("bannerId");
358 }
359
360 if (!is_array($bannerId))
361 {
362 $bannerId = array($bannerId);
363 }
364
365 $result = $this->getProxy()->getInterface()->getBannerList(static::ENGINE_ID, array(
366 'BannerIDS' => $bannerId,
367 ));
368
369 if (!empty($result['error']))
370 {
371 throw new YandexDirectException($result);
372 }
373
374 return $result;
375 }
376
377 public function getCampaignBanners($campaignId)
378 {
379 if (empty($campaignId))
380 {
381 throw new ArgumentNullException("campaignId");
382 }
383
384 if (!is_array($campaignId))
385 {
386 $campaignId = array($campaignId);
387 }
388
389 $result = $this->getProxy()->getInterface()->getBannerList(static::ENGINE_ID, array(
390 'CampaignIDS' => $campaignId,
391 ));
392
393 if (!empty($result['error']))
394 {
395 throw new YandexDirectException($result);
396 }
397
398 return $result;
399 }
400
401 public function moderateBanners($campaignId, array $bannerIDs)
402 {
403 if (empty($campaignId))
404 {
405 throw new ArgumentNullException("campaignId");
406 }
407
408 $queryData = array(
409 'CampaignID' => $campaignId,
410 'BannerIDS' => $bannerIDs,
411 );
412
413 $result = $this->getProxy()->getInterface()->moderateBanners(static::ENGINE_ID, $queryData);
414
415 if (!empty($result['error']))
416 {
417 throw new YandexDirectException($result);
418 }
419
420 return $result;
421 }
422
423 public function stopBanners($campaignId, array $bannerIDs)
424 {
425 if (empty($campaignId))
426 {
427 throw new ArgumentNullException("campaignId");
428 }
429
430 $queryData = array(
431 'CampaignID' => $campaignId,
432 'BannerIDS' => $bannerIDs,
433 );
434
435
436 $result = $this->getProxy()->getInterface()->stopBanners(static::ENGINE_ID, $queryData);
437
438 if (!empty($result['error']))
439 {
440 throw new YandexDirectException($result);
441 }
442
443 return $result;
444 }
445
446 public function resumeBanners($campaignId, array $bannerIDs)
447 {
448 if (empty($campaignId))
449 {
450 throw new ArgumentNullException("campaignId");
451 }
452
453 $queryData = array(
454 'CampaignID' => $campaignId,
455 'BannerIDS' => $bannerIDs,
456 );
457
458 $result = $this->getProxy()->getInterface()->resumeBanners(static::ENGINE_ID, $queryData);
459
460 if (!empty($result['error']))
461 {
462 throw new YandexDirectException($result);
463 }
464
465 return $result;
466 }
467
468 public function archiveBanners($campaignId, array $bannerIDs)
469 {
470 if (empty($campaignId))
471 {
472 throw new ArgumentNullException("campaignId");
473 }
474
475 $queryData = array(
476 'CampaignID' => $campaignId,
477 'BannerIDS' => $bannerIDs,
478 );
479
480 $result = $this->getProxy()->getInterface()->archiveBanners(static::ENGINE_ID, $queryData);
481
482 if (!empty($result['error']))
483 {
484 throw new YandexDirectException($result);
485 }
486
487 return $result;
488 }
489
490 public function unArchiveBanners($campaignId, array $bannerIDs)
491 {
492 if (empty($campaignId))
493 {
494 throw new ArgumentNullException("campaignId");
495 }
496
497 $queryData = array(
498 'CampaignID' => $campaignId,
499 'BannerIDS' => $bannerIDs,
500 );
501
502 $result = $this->getProxy()->getInterface()->unArchiveBanners(static::ENGINE_ID, $queryData);
503
504 if (!empty($result['error']))
505 {
506 throw new YandexDirectException($result);
507 }
508
509 return $result;
510 }
511
512 public function deleteBanners($campaignId, array $bannerIDs)
513 {
514 if (empty($campaignId))
515 {
516 throw new ArgumentNullException("campaignId");
517 }
518
519 $queryData = array(
520 'CampaignID' => $campaignId,
521 'BannerIDS' => $bannerIDs,
522 );
523
524 $result = $this->getProxy()->getInterface()->deleteBanners(static::ENGINE_ID, $queryData);
525
526 if (!empty($result['error']))
527 {
528 throw new YandexDirectException($result);
529 }
530
531 return $result;
532 }
533
541 public function getRegions()
542 {
543 $result = $this->getProxy()->getInterface()->getRegions(static::ENGINE_ID);
544
545 if (!empty($result['error']))
546 {
547 throw new YandexDirectException($result);
548 }
549
550 return $result;
551 }
552
553 public function getClientsSettings()
554 {
555 $cacheManager = Application::getInstance()->getManagedCache();
556
557 if ($cacheManager->read(self::CACHE_TTL, self::CACHE_ID))
558 {
559 $result = $cacheManager->get(self::CACHE_ID);
560 }
561 else
562 {
563 $result = $this->getProxy()->getInterface()->getClientsSettings(static::ENGINE_ID);
564 }
565
566 if(!is_array($result) || empty($result))
567 $result = array('error' => 'No authentication.');
568
569 if (!empty($result['error']))
570 throw new YandexDirectException($result);
571 else
572 $cacheManager->set(self::CACHE_ID, $result);
573
574 return $result;
575 }
576
577 public function createWordstatReport(array $phrase, $geo = NULL)
578 {
579 $queryData = array(
580 'Phrases' => $phrase,
581 );
582
583 if (is_array($geo))
584 {
585 $queryData['GeoID'] = $geo;
586 }
587
588 $result = $this->getProxy()->getInterface()->createWordstatReport(static::ENGINE_ID, $queryData);
589
590 if (!empty($result['error']))
591 {
592 throw new YandexDirectException($result);
593 }
594
595 return $result;
596 }
597
598 public function deleteWordstatReport($reportId)
599 {
600 $result = $this->getProxy()->getInterface()->deleteWordstatReport(static::ENGINE_ID, $reportId);
601
602 if (!empty($result['error']))
603 {
604 throw new YandexDirectException($result);
605 }
606
607 return $result;
608 }
609
610 public function getWordstatReport($reportId)
611 {
612 $result = $this->getProxy()->getInterface()->getWordstatReport(static::ENGINE_ID, $reportId);
613
614 if (!empty($result['error']))
615 {
616 throw new YandexDirectException($result);
617 }
618
619 return $result;
620 }
621
622 public function getWordstatReportList()
623 {
624 $result = $this->getProxy()->getInterface()->getWordstatReportList(static::ENGINE_ID);
625
626 if (!empty($result['error']))
627 {
628 throw new YandexDirectException($result);
629 }
630
631 return $result;
632 }
633
634 public function createForecastReport(array $phrase, $geo = NULL)
635 {
636 $queryData = array(
637 'Phrases' => $phrase,
638 );
639
640 if (is_array($geo))
641 {
642 $queryData['GeoID'] = $geo;
643 }
644
645 $result = $this->getProxy()->getInterface()->createForecastReport(static::ENGINE_ID, $queryData);
646
647 if (!empty($result['error']))
648 {
649 throw new YandexDirectException($result);
650 }
651
652 return $result;
653 }
654
655 public function deleteForecastReport($reportId)
656 {
657 $result = $this->getProxy()->getInterface()->deleteForecastReport(static::ENGINE_ID, $reportId);
658
659 if (!empty($result['error']))
660 {
661 throw new YandexDirectException($result);
662 }
663
664 return $result;
665 }
666
667 public function getForecastReport($reportId)
668 {
669 $result = $this->getProxy()->getInterface()->getForecastReport(static::ENGINE_ID, $reportId);
670
671 if (!empty($result['error']))
672 {
673 throw new YandexDirectException($result);
674 }
675
676 return $result;
677 }
678
679 public function getForecastReportList()
680 {
681 $result = $this->getProxy()->getInterface()->getForecastReportList(static::ENGINE_ID);
682
683 if (!empty($result['error']))
684 {
685 throw new YandexDirectException($result);
686 }
687
688 return $result;
689 }
690
702 public function getBannerStats(array $params)
703 {
704 $result = $this->getProxy()->getInterface()->getBannerStats(static::ENGINE_ID, $params);
705
706 if (!empty($result['error']))
707 {
708 throw new YandexDirectException($result);
709 }
710
711 return $result;
712 }
713
725 protected function query($scope, $method = "GET", $param = NULL, $skipRefreshAuth = false)
726 {
727 if ($param === NULL)
728 {
729 $param = array();
730 }
731
732 if ($this->engineSettings['AUTH'])
733 {
734 $http = new HttpClient();
735 $http->setRedirect(false);
736 $http->setHeader("Content-Type", "application/json; charset=utf-8");
737
738 $postData = array(
739 "method" => $method,
740 "locale" => $this->locale,
741 "token" => $this->engineSettings['AUTH']['access_token'],
742 );
743
744 if (!empty($param))
745 {
746 $postData["param"] = $param;
747 }
748
749 $postData = YandexJson::encode($postData, JSON_UNESCAPED_UNICODE);
750
751 $ts = microtime(true);
752 $http->post(static::API_URL, $postData);
753
754 LogTable::add(array(
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),
761 ));
762
763 if ($http->getStatus() == 401 && !$skipRefreshAuth)
764 {
765 if ($this->checkAuthExpired())
766 {
767 $this->query($scope, $method, $param, true);
768 }
769 }
770
771 return $http;
772 }
773 else
774 {
775 throw new SystemException("No Yandex auth data");
776 }
777 }
778
779 public function finance_query($method, $masterToken, $operationNum, $param = array(), $skipRefreshAuth = false)
780 {
781 if ($this->engineSettings['AUTH'])
782 {
783 $http = new HttpClient();
784 $http->setRedirect(false);
785 $http->setHeader("Content-Type", "application/json; charset=utf-8");
786
787 $auth = $this->getCurrentUser();
788
789 $financeToken = hash(
790 "sha256",
791 $masterToken . $operationNum . $method . $auth['login']);
792
793 $postData = array(
794 "method" => $method,
795 "finance_token" => $financeToken,
796 "operation_num" => $operationNum,
797 "locale" => $this->locale,
798 "token" => $this->engineSettings['AUTH']['access_token'],
799 );
800
801 if (!empty($param))
802 {
803 $postData["param"] = $param;
804 }
805
806 $postData = YandexJson::encode($postData, JSON_UNESCAPED_UNICODE);
807
808 $http->post(self::API_URL, $postData);
809
810 if ($http->getStatus() == 401 && !$skipRefreshAuth)
811 {
812 if ($this->checkAuthExpired())
813 {
814 $this->query("", $method, $param, true);
815 }
816 }
817
818 return $http;
819 }
820 else
821 {
822 throw new SystemException("No Yandex auth data");
823 }
824 }
825
826 public function updateCampaignManual($campaignId = NULL)
827 {
828 $newCampaigns = array();
829
830 $res = array(
831 'added' => 0,
832 'updated' => 0,
833 'error' => 0,
834 );
835
836 $keys = array();
837
838 if (!is_array($campaignId) && $campaignId > 0)
839 {
840 $campaignId = array($campaignId);
841 }
842
843 $campaignList = array();
844 if (is_array($campaignId) && count($campaignId) > 0)
845 {
846// get just current campaigns by ID
847 $dbRes = YandexCampaignTable::getList(array(
848 'filter' => array(
849 '=ID' => $campaignId,
850 '=ENGINE_ID' => $this->getId(),
851 ),
852 'select' => array('XML_ID'),
853 ));
854
855 while ($campaign = $dbRes->fetch())
856 {
857 $keys[] = $campaign['XML_ID'];
858 }
859
860 if (count($keys) > 0)
861 $campaignList = $this->getCampaign($keys);
862 }
863 else
864 {
865// get ALL campaigns, if IDs not set
866 $campaignList = $this->getCampaignList();
867 }
868
869 $campaignListSorted = array();
870 $campaignListToDelete = array();
871
872 foreach ($campaignList as $campaignInfo)
873 {
874 $campaignListSorted[$campaignInfo['CampaignID']] = $campaignInfo;
875 }
876
877 $filter = array('=ENGINE_ID' => $this->getId());
878// get filtered items only if we update only selected campaigns. Else - get ALL from table
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));
882
884// UPDATE existing in table campaigns
885 while ($campaign = $dbCampaigns->fetch())
886 {
887 if (isset($campaignListSorted[$campaign['XML_ID']]))
888 {
889 $result = YandexCampaignTable::update(
890 $campaign['ID'], array(
891 "SETTINGS" => $campaignListSorted[$campaign['XML_ID']],
892 )
893 );
894
895 unset($campaignListSorted[$campaign['XML_ID']]);
896
897 if ($result->isSuccess())
898 {
899 $res['updated']++;
900 }
901 else
902 {
903 $res['error']++;
904 }
905 }
906// collect campaigns, then not exist in YD, but exist in table
907 else
908 {
909 $campaignListToDelete[$campaign['ID']] = $campaign['ID'];
910 }
911 }
912
913// REMOVE from table deleted campaigns
914 if (count($campaignListToDelete) > 0)
915 {
916 foreach ($campaignListToDelete as $campaignId)
917 {
918 $resultDelete = YandexCampaignTable::delete($campaignId);
919// todo: skipRempoteUpdate in campaign table or in banner table? o_O
921 }
922 }
923
924// ADD in table new campaignt from YD
925 foreach ($campaignListSorted as $campaignId => $campaignInfo)
926 {
927 $result = YandexCampaignTable::add(array(
928 "SETTINGS" => $campaignInfo,
929 ));
930
931 if ($result->isSuccess())
932 {
933 $newCampaigns[] = $result->getId();
934 $res['added']++;
935 }
936 else
937 {
938 $res['error']++;
939 }
940 }
942
943 if (count($newCampaigns) > 0)
944 {
945 set_time_limit(300);
946
947 $res['new'] = $newCampaigns;
948
949 $res['banner'] = array();
950 $cnt = ceil(count($newCampaigns) / static::MAX_CAMPAIGNS_BANNER_UPDATE);
951 for ($i = 0; $i < $cnt; $i++)
952 {
953 $res['banner'] = array_merge(
954 $res['banner'],
955 $this->updateBannersManual(
956 array_slice(
957 $newCampaigns,
958 $i * static::MAX_CAMPAIGNS_BANNER_UPDATE,
959 static::MAX_CAMPAIGNS_BANNER_UPDATE
960 )
961 )
962 );
963 }
964 }
965
966 return $res;
967 }
968
969 public function updateBannersManual($campaignId, $bannerId = NULL)
970 {
971 $res = array(
972 'added' => 0,
973 'updated' => 0,
974 'error' => 0,
975 );
976
977 if (!is_array($bannerId) && $bannerId > 0)
978 {
979 $bannerId = array($bannerId);
980 }
981
982 $bannerList = array();
983 if (is_array($bannerId) && count($bannerId) > 0)
984 {
985// get banners by ID
986 $dbRes = YandexBannerTable::getList(array(
987 'filter' => array(
988 '=ID' => $bannerId,
989 '=ENGINE_ID' => $this->getId(),
990 ),
991 'select' => array('XML_ID'),
992 ));
993
994 while ($banner = $dbRes->fetch())
995 {
996 $keys[] = $banner['XML_ID'];
997 }
998
999 if(count($keys) > 0)
1000 $bannerList = $this->getBanners($keys);
1001 }
1002 else
1003 {
1004 $dbCampaigns = YandexCampaignTable::getList(array(
1005 'filter' => array(
1006 '=ID' => $campaignId,
1007 '=ENGINE_ID' => $this->getId(),
1008 ),
1009 'select' => array('ID', 'XML_ID'),
1010 ));
1011
1012 while ($campaign = $dbCampaigns->fetch())
1013 {
1014 $campaignIndex[$campaign['XML_ID']] = $campaign['ID'];
1015 }
1016
1017// get ALL banners for current campaign
1018 if (count($campaignIndex) > 0)
1019 $bannerList = $this->getCampaignBanners(array_keys($campaignIndex));
1020 }
1021
1022 $bannerListSorted = array();
1023 $bannerListToDelete = array();
1024 foreach ($bannerList as $bannerInfo)
1025 {
1026 $bannerListSorted[$bannerInfo['BannerID']] = $bannerInfo;
1027 }
1028
1029 $filter = array('=ENGINE_ID' => $this->getId());
1030// get filtered items only if we update only selected banners. Else - get ALL from table
1031 if(is_array($bannerId) && count($bannerId) > 0 && count($bannerListSorted) > 0)
1032 $filter['=XML_ID'] = array_keys($bannerListSorted);
1033 if($campaignId)
1034 $filter['=CAMPAIGN_ID'] = $campaignId;
1035 $dbBanners = YandexBannerTable::getList(array('filter' => $filter));
1036
1037 YandexBannerTable::setSkipRemoteUpdate(true);
1038// UPDATE existing in table banners
1039 while ($banner = $dbBanners->fetch())
1040 {
1041 if (isset($bannerListSorted[$banner['XML_ID']]))
1042 {
1043 $result = YandexBannerTable::update(
1044 $banner['ID'], array(
1045 "SETTINGS" => $bannerListSorted[$banner['XML_ID']],
1046 )
1047 );
1048
1049 unset($bannerListSorted[$banner['XML_ID']]);
1050
1051 if ($result->isSuccess())
1052 {
1053 $res['updated']++;
1054 }
1055 else
1056 {
1057 $res['error']++;
1058 }
1059 }
1060// collect banners, then not exist in YD, but exist in table
1061 else
1062 {
1063 $bannerListToDelete[$banner['ID']] = $banner['ID'];
1064 }
1065 }
1066
1067// REMOVE from table deleted banners
1068 if (count($bannerListToDelete) > 0)
1069 {
1070 foreach ($bannerListToDelete as $bannerId)
1071 {
1072 $resultDelete = YandexBannerTable::delete($bannerId);
1073 YandexBannerTable::setSkipRemoteUpdate(true);
1074 }
1075 }
1076
1077// ADD in table new campaignt from YD
1078 foreach ($bannerListSorted as $bannerId => $bannerInfo)
1079 {
1080 $result = YandexBannerTable::add(array(
1081 "CAMPAIGN_ID" => $campaignIndex[$bannerInfo['CampaignID']],
1082 "SETTINGS" => $bannerInfo,
1083 ));
1084
1085 if ($result->isSuccess())
1086 {
1087 $res['added']++;
1088 }
1089 else
1090 {
1091 $res['error']++;
1092 }
1093 }
1094 YandexBannerTable::setSkipRemoteUpdate(false);
1095
1096 return $res;
1097 }
1098
1099
1100 public static function updateAgent()
1101 {
1102 try
1103 {
1104 $engine = new self();
1105 }
1106 catch (\Bitrix\Main\SystemException $e)
1107 {
1108 return __CLASS__ . "::updateAgent();";
1109 }
1110
1111 if ($engine->getAuthSettings())
1112 {
1113 try
1114 {
1115 $dbRes = YandexCampaignTable::getList(array(
1116 'filter' => array(
1118 '=ENGINE_ID' => $engine->getId(),
1119 ),
1120 'select' => array('CNT'),
1121 'runtime' => array(
1122 new ExpressionField('CNT', 'COUNT(*)'),
1123 ),
1124 ));
1125
1126 $res = $dbRes->fetch();
1127 if ($res['CNT'] > 0)
1128 {
1129 $engine->updateCampaignManual();
1130 }
1131
1132 $availableCampaigns = array();
1133 $campaignList = $engine->getCampaignList();
1134 foreach ($campaignList as $campaignInfo)
1135 {
1136 $availableCampaigns[] = $campaignInfo['CampaignID'];
1137 }
1138
1139 if (count($availableCampaigns) > 0)
1140 {
1141 $dbRes = YandexBannerTable::getList(array(
1142 'group' => array('CAMPAIGN_ID'),
1143 'filter' => array(
1144 '<LAST_UPDATE' => DateTime::createFromTimestamp(time() - YandexBannerTable::CACHE_LIFETIME),
1145 '=ENGINE_ID' => $engine->getId(),
1146 '=CAMPAIGN.XML_ID' => $availableCampaigns,
1147 ),
1148 'select' => array('CAMPAIGN_ID'),
1149 ));
1150
1151 $campaignId = array();
1152 while ($res = $dbRes->fetch())
1153 {
1154 $campaignId[] = $res['CAMPAIGN_ID'];
1155 }
1156
1157 if (count($campaignId) > 0)
1158 {
1159 $engine->updateBannersManual($campaignId);
1160 }
1161 }
1162 }
1163 catch (YandexDirectException $e)
1164 {
1165 }
1166 }
1167
1168 return __CLASS__ . "::updateAgent();";
1169 }
1170}
static createFromTimestamp($timestamp)
Definition datetime.php:246
static setSkipRemoteUpdate($value)
Definition adventity.php:88
updateBanner(array $bannerParam)
createWordstatReport(array $phrase, $geo=NULL)
stopBanners($campaignId, array $bannerIDs)
updateCampaign(array $campaignParam)
addBanner(array $bannerParam)
addCampaign(array $campaignParam)
updateCampaignManual($campaignId=NULL)
query($scope, $method="GET", $param=NULL, $skipRefreshAuth=false)
createForecastReport(array $phrase, $geo=NULL)
resumeBanners($campaignId, array $bannerIDs)
archiveBanners($campaignId, array $bannerIDs)
moderateBanners($campaignId, array $bannerIDs)
unArchiveBanners($campaignId, array $bannerIDs)
finance_query($method, $masterToken, $operationNum, $param=array(), $skipRefreshAuth=false)
updateBannersManual($campaignId, $bannerId=NULL)
deleteBanners($campaignId, array $bannerIDs)
static encode($data, $options=null)
Definition yandexjson.php:8
static getAuth(string $engineCode)
Definition service.php:72
static isRegistered()
Definition service.php:59