Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
FacebookAgent.php
1<?php
2
4
8
9final class FacebookAgent
10{
11 public const AGENT_NAME = '\Bitrix\Catalog\v2\Integration\Seo\Facebook\FacebookAgent::executeProductUpdate();';
12
13 public static function executeProductUpdate(): string
14 {
15 if (
16 !Loader::includeModule('crm')
17 && !Loader::includeModule('iblock')
18 )
19 {
20 return '';
21 }
22
23 if (!self::getFacebookFacade()->isExportAvailable() || !self::getFacebookFacade()->hasAuth())
24 {
25 return '';
26 }
27
28 if (self::checkRequirements())
29 {
30 $lastModifiedProducts = self::getLastModifiedProductIds();
31 if (!empty($lastModifiedProducts))
32 {
33 self::getFacebookFacade()->refreshExportedProducts($lastModifiedProducts);
34 }
35 }
36
37 return self::AGENT_NAME;
38 }
39
40 public static function registerCatalogFacebookAgent(): void
41 {
42 $agent = \CAgent::GetList(
43 ['ID' => 'DESC'],
44 ["MODULE_ID" => 'catalog', "NAME" => self::AGENT_NAME]
45 );
46
47 if (!$agent->Fetch())
48 {
49 \CTimeZone::Disable();
50 \CAgent::AddAgent(
51 self::AGENT_NAME,
52 'catalog',
53 'Y',
54 300,
55 '',
56 'Y',
57 \ConvertTimeStamp(time() + 300, 'FULL'),
58 );
59 \CTimeZone::Enable();
60 }
61 }
62
63 public static function unregisterCatalogFacebookAgent(): void
64 {
65 $agent = \CAgent::GetList(
66 ['ID' => 'DESC'],
67 ["MODULE_ID" => 'catalog', "NAME" => self::AGENT_NAME]
68 );
69
70 if ($agent->Fetch())
71 {
72 \CAgent::RemoveAgent(
73 self::AGENT_NAME,
74 'catalog'
75 );
76 }
77 }
78
79 private static function checkRequirements(): bool
80 {
81 $facebookFacade = self::getFacebookFacade();
82 if ($facebookFacade)
83 {
84 return
85 $facebookFacade
86 ->checkRequirements()
87 ->isSuccess()
88 ;
89 }
90
91 return false;
92 }
93
94 private static function getLastModifiedProductIds(): array
95 {
96 $productIds = [];
97 $elementsIterator = \Bitrix\Catalog\v2\Integration\Seo\Entity\ExportedProductTable::getList([
98 'order' => ['ID' => 'ASC'],
99 'select' => ['ID', 'PRODUCT_ID', 'TIMESTAMP_X'],
100 'filter' => [
101 'SERVICE_ID' => 'facebook',
102 ],
103 'runtime' => [
104 new \Bitrix\Main\Entity\ReferenceField(
105 'IBLOCK_ELEMENT',
106 \Bitrix\Iblock\ElementTable::class,
107 [
108 '=this.PRODUCT_ID' => 'ref.ID',
109 '<this.TIMESTAMP_X' => 'ref.TIMESTAMP_X',
110 ],
111 [
112 'join_type' => 'INNER',
113 ]
114 ),
115 ]
116 ]);
117 while ($element = $elementsIterator->fetch())
118 {
119 $productIds[] = (int)$element['PRODUCT_ID'];
120 }
121
122 return $productIds;
123 }
124
125 private static function getFacebookFacade(): FacebookFacade
126 {
127 return ServiceContainer::get('integration.seo.facebook.facade', [
128 'iblockId' => self::getProductIblockId(),
129 ]);
130 }
131
132 private static function getProductIblockId(): int
133 {
134 return \CCrmCatalog::EnsureDefaultExists() ?: 0;
135 }
136}