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