Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
external.php
1<?php
9
10use Bitrix\Main;
13
15
16Loc::loadMessages(__FILE__);
17
34class ExternalTable extends Entity\DataManager
35{
36 public static function getFilePath()
37 {
38 return __FILE__;
39 }
40
41 public static function getTableName()
42 {
43 return 'b_sale_loc_ext';
44 }
45
46 public static function addMultipleForOwner($primaryOwner, $external = array())
47 {
48 $primaryOwner = Assert::expectIntegerPositive($primaryOwner, '$primaryOwner');
49
50 // nothing to connect to, simply exit
51 if(!is_array($external) || empty($external))
52 return false;
53
54 foreach($external as $data)
55 {
56 $serivceId = intval($data['SERVICE_ID']);
57
58 if($serivceId && mb_strlen($data['XML_ID']))
59 {
60 $res = self::add(array(
61 'SERVICE_ID' => $serivceId,
62 'XML_ID' => $data['XML_ID'],
63 'LOCATION_ID' => $primaryOwner
64 ));
65 if(!$res->isSuccess())
66 throw new Main\SystemException(Loc::getMessage('SALE_LOCATION_EXTERNAL_ENTITY_CANNOT_ADD_DATA_EXCEPTION')); // .': '.implode(', ', $res->getErrorMessages())
67 }
68 }
69
70 return true;
71 }
72
73 public static function updateMultipleForOwner($primaryOwner, $external)
74 {
75 $primaryOwner = Assert::expectIntegerPositive($primaryOwner, '$primaryOwner');
76
77 $res = self::getList(array(
78 'filter' => array('LOCATION_ID' => $primaryOwner)
79 ));
80
81 $existed = array();
82 while($item = $res->fetch())
83 $existed[$item['ID']][$item['SERVICE_ID']] = $item['XML_ID'];
84
85 foreach($external as $id => $data)
86 {
87 $data['REMOVE'] ??= false;
88 $data['SERVICE_ID'] ??= 0;
89 $data['XML_ID'] = (string)($data['XML_ID'] ?? '');
90
91 $serivceId = (int)$data['SERVICE_ID'];
92 $id = (int)$id;
93
94 if (isset($existed[$id]))
95 {
96 if ($data['XML_ID'] === '' || !$serivceId || $data['REMOVE'])
97 {
98 // field either empty or prepared to remove
99 self::delete($id);
100 }
101 else
102 {
103 $res = self::update(
104 $id,
105 [
106 'SERVICE_ID' => $serivceId,
107 'XML_ID' => $data['XML_ID'],
108 ]
109 );
110 if (!$res->isSuccess())
111 {
112 throw new Main\SystemException(Loc::getMessage('SALE_LOCATION_EXTERNAL_ENTITY_CANNOT_UPDATE_DATA_EXCEPTION'));
113 }
114 }
115 }
116 else
117 {
118 if ($serivceId && $data['XML_ID'] !== '')
119 {
120 $res = self::add([
121 'SERVICE_ID' => $serivceId,
122 'XML_ID' => $data['XML_ID'],
123 'LOCATION_ID' => $primaryOwner,
124 ]);
125 if (!$res->isSuccess())
126 {
127 throw new Main\SystemException(Loc::getMessage('SALE_LOCATION_EXTERNAL_ENTITY_CANNOT_ADD_DATA_EXCEPTION'));
128 }
129 }
130 }
131 }
132 }
133
134 public static function deleteMultipleForOwner($primaryOwner)
135 {
136 $primaryOwner = Assert::expectIntegerPositive($primaryOwner, '$primaryOwner');
137
138 $listRes = self::getList(array(
139 'filter' => array('LOCATION_ID' => $primaryOwner),
140 'select' => array('ID')
141 ));
142 while($item = $listRes->fetch())
143 {
144 $res = self::delete($item['ID']);
145 if(!$res->isSuccess())
146 throw new Main\SystemException(Loc::getMessage('SALE_LOCATION_EXTERNAL_ENTITY_CANNOT_DELETE_DATA_EXCEPTION'));
147 }
148 }
149
155 public static function deleteMultipleByParentRangeSql($sql)
156 {
157 if($sql == '')
158 throw new Main\SystemException('Range sql is empty');
159
160 $dbConnection = Main\HttpApplication::getConnection();
161
162 $dbConnection->query('delete from '.static::getTableName().' where LOCATION_ID in ('.$sql.')');
163 }
164
165 public static function getMap()
166 {
167 return array(
168
169 'ID' => array(
170 'data_type' => 'integer',
171 'primary' => true,
172 'autocomplete' => true,
173 ),
174 'SERVICE_ID' => array(
175 'data_type' => 'integer',
176 'required' => true,
177 'title' => Loc::getMessage('SALE_LOCATION_EXTERNAL_ENTITY_SERVICE_ID_FIELD')
178 ),
179 'XML_ID' => array(
180 'data_type' => 'string',
181 'required' => true,
182 'title' => Loc::getMessage('SALE_LOCATION_EXTERNAL_ENTITY_XML_ID_FIELD')
183 ),
184 'LOCATION_ID' => array(
185 'data_type' => 'integer',
186 'required' => true,
187 ),
188
189 // virtual
190 'SERVICE' => array(
191 'data_type' => '\Bitrix\Sale\Location\ExternalService',
192 'reference' => array(
193 '=this.SERVICE_ID' => 'ref.ID'
194 )
195 ),
196 'LOCATION' => array(
197 'data_type' => '\Bitrix\Sale\Location\Location',
198 'reference' => array(
199 '=this.LOCATION_ID' => 'ref.ID'
200 )
201 )
202 );
203 }
204}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static updateMultipleForOwner($primaryOwner, $external)
Definition external.php:73
static deleteMultipleForOwner($primaryOwner)
Definition external.php:134
static addMultipleForOwner($primaryOwner, $external=array())
Definition external.php:46
static deleteMultipleByParentRangeSql($sql)
Definition external.php:155