1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
recentaddressesservice.php
См. документацию.
1<?php
2
3namespace Bitrix\Location\Infrastructure\Service;
4
5use Bitrix\Location\Common\BaseService;
6use Bitrix\Location\Entity\Address;
7use Bitrix\Location\Infrastructure\Service\Config\Container;
8use Bitrix\Location\Model\RecentAddressTable;
9use Bitrix\Main\Application;
10use Bitrix\Main\Engine\CurrentUser;
11use Bitrix\Main\Type\DateTime;
12
14{
16 protected static $instance;
17
18 private const MAX_CNT = 20;
19
20 private int $currentUserId;
21
22 protected function __construct(Container $config)
23 {
24 parent::__construct($config);
25
26 $this->currentUserId = (int)CurrentUser::get()?->getId();
27 }
28
29 public function add(Address $address): void
30 {
31 $normalizedAddress = $this->getNormalizedAddress($address);
32
33 $recentAddressList = RecentAddressTable::query()
34 ->setSelect(['ID', 'ADDRESS'])
35 ->where('USER_ID', $this->currentUserId)
36 ->setOrder(['USED_AT' => 'DESC'])
37 ->setLimit(self::MAX_CNT)
38 ->fetchAll()
39 ;
40
41 $isExisting = false;
42 foreach ($recentAddressList as $recentAddressListItem)
43 {
44 $recentAddress = null;
45 try
46 {
47 $recentAddress = Address::fromJson($recentAddressListItem['ADDRESS']);
48 }
49 catch (\Exception $e) {}
50
51 if ($recentAddress === null)
52 {
53 continue;
54 }
55
56 if ($this->areAddressesEqual($normalizedAddress, $recentAddress))
57 {
59 (int)$recentAddressListItem['ID'],
60 [
61 'ADDRESS' => $normalizedAddress->toJson(),
62 'USED_AT' => new DateTime(),
63 ]
64 );
65 $isExisting = true;
66
67 break;
68 }
69 }
70
71 if (!$isExisting)
72 {
74 'USER_ID' => $this->currentUserId,
75 'ADDRESS' => $normalizedAddress->toJson(),
76 ]);
77 }
78 }
79
80 public function get(int $limit = self::MAX_CNT): array
81 {
82 $result = [];
83
84 $recentAddressList = RecentAddressTable::query()
85 ->setSelect(['ADDRESS'])
86 ->where('USER_ID', $this->currentUserId)
87 ->setLimit(min($limit, self::MAX_CNT))
88 ->setOrder(['USED_AT' => 'DESC'])
89 ->fetchAll()
90 ;
91 foreach ($recentAddressList as $recentAddressListItem)
92 {
93 $recentAddressJson = $recentAddressListItem['ADDRESS'];
94
95 $address = Address::fromJson($recentAddressJson);
96 if (!$address)
97 {
98 continue;
99 }
100
101 $result[] = $address;
102 }
103
104 return $result;
105 }
106
107 public static function cleanUp(): string
108 {
109 $connection = Application::getConnection();
110 $helper = $connection->getSqlHelper();
111
112 $connection->query('
113 DELETE FROM b_location_recent_address
114 WHERE
115 USED_AT < ' . $helper->addDaysToDateTime(-30) . '
116 ');
117
118 return '\\Bitrix\\Location\\Infrastructure\\Service\\RecentAddressesService::cleanUp();';
119 }
120
121 private function areAddressesEqual(Address $address1, Address $address2): bool
122 {
123 return $this->getAddressFields($address1) === $this->getAddressFields($address2);
124 }
125
126 private function getAddressFields(Address $address): array
127 {
128 $result = [];
129
131 foreach ($address->getFieldCollection() as $field)
132 {
133 $result[$field->getType()] = $field->getValue();
134 }
135
136 ksort($result);
137
138 return $result;
139 }
140
141 private function getNormalizedAddress(Address $address): Address
142 {
143 $result = new Address(
144 $address->getLanguageId()
145 );
146 $fieldCollection = new Address\FieldCollection();
147
148 $result->setLatitude($address->getLatitude());
149 $result->setLongitude($address->getLongitude());
150
152 foreach ($address->getFieldCollection() as $field)
153 {
154 $fieldCollection->addItem(
155 new Address\Field($field->getType(), $field->getValue())
156 );
157 }
158
159 $result->setFieldCollection($fieldCollection);
160
161 return $result;
162 }
163}
$connection
Определения actionsdefinitions.php:38
static add(array $data)
Определения datamanager.php:877
static update($primary, array $data)
Определения datamanager.php:1256
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$config
Определения quickway.php:69