Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
3
7
8abstract class Base
9{
10 const ANALITICS_ORIGINATOR_ID = 'bitrix.cms.sync';
11
12 protected $collection;
13
14 public function __construct()
15 {
17 }
18
19 abstract public function getSrcEntityTypeId();
20 abstract public function getDstEntityTypeId();
21 abstract public function init($params);
22 abstract static public function proxyAdds(array $list);
23
24 protected function relationLoad()
25 {
26 $collection = $this->getCollection();
27
28 $relations = static::relationEntityList([
29 'SRC_ENTITY_ID' => $collection->getIndexes(),
30 'SRC_ENTITY_TYPE_ID' => $this->getSrcEntityTypeId(),
31 'DST_ENTITY_TYPE_ID' => $this->getDstEntityTypeId()]
32 );
33 if(count($relations)>0)
34 {
36 foreach ($relations as $relation)
37 {
38 $item = $collection->getItemByIndex($relation->getSourceEntityId());
39 $item->getEntity()
40 ->setRelation($relation);
41 }
42 }
43 return $this;
44 }
45 public function relationListDstEntity()
46 {
47 $result = [];
49 foreach ($this->getCollection() as $item)
50 {
51 if($item->getEntity()->hasRelation())
52 {
53 $result[] = $item->getEntity()->getRelation()->getDestinationEntityId();
54 }
55 }
56 return $result;
57 }
58 public function relationDeleteByDstEntity($dstEntityList)
59 {
61 foreach($this->getCollection() as $item)
62 {
63 if($item->getEntity()->hasRelation())
64 {
65 if(!in_array($item->getEntity()->getRelation()->getDestinationEntityId(), $dstEntityList))
66 {
67 $item->getEntity()->getRelation()->setDestinationEntityId(0);
68 $item->getEntity()->getRelation()->save();
69 }
70 }
71 }
72 }
73 public function relationVoid()
74 {
75 $collection = new Integration\Service\Internal\Container\Collection();
76
78 foreach($this->getCollection() as $item)
79 {
80 if($item->getEntity()->hasRelation() == false
81 || $item->getEntity()->getRelation()->getDestinationEntityId() == 0)
82 {
83 $collection->addItem($item);
84 }
85 }
86 return $collection;
87 }
88 public function relationRefreshByDstEntity($list)
89 {
90 foreach($list as $index=>$dstEntityId)
91 {
92 $item = $this->getCollection()->getItemByIndex($index);
93 if(is_null($item) == false)
94 {
95 $relation = new \Bitrix\Sale\Exchange\Integration\Relation\Relation(
96 $this->getSrcEntityTypeId(),
97 $index,
98 $this->getDstEntityTypeId(),
99 $dstEntityId);
100
101 $relation->save();
102 //collection->getItemByIndex(index)->getEntity->getRelation->setDstEntityId($data['ID'])
103 }
104 }
105 $this->relationLoad();
106 }
107
108 static protected function relationEntityList($filter=[])
109 {
110 $result = [];
111
112 $list = Integration\Entity\B24IntegrationRelationTable::getList(['filter'=>$filter])
113 ->fetchAll();
114
115 foreach ($list as $item)
116 {
117 $result[] = Integration\Relation\Relation::createFromArray([
118
119 'SRC_ENTITY_TYPE_ID'=>$item['SRC_ENTITY_TYPE_ID'],
120 'SRC_ENTITY_ID'=>$item['SRC_ENTITY_ID'],
121 'DST_ENTITY_TYPE_ID'=>$item['DST_ENTITY_TYPE_ID'],
122 'DST_ENTITY_ID'=>$item['DST_ENTITY_ID']
123 ]);
124 }
125
126 return $result;
127 }
128
129 /*protected function clientRelation($params, $typeName)
130 {
131 $list = $params[$typeName];
132 $client = static::factoryClient($typeName);
133
134 return $this->relationEntityList([
135 'SRC_ENTITY_ID' => array_keys($list),
136 'SRC_ENTITY_TYPE_ID' => $client->getSrcEntityTypeId(),
137 'DST_ENTITY_TYPE_ID' => $client->getDstEntityTypeId()]
138 );
139 }*/
140 static protected function clientRelation($indexes, $typeName)
141 {
142 $client = static::factoryClient($typeName);
143 return count($indexes)>0 ?
144 static::relationEntityList([
145 'SRC_ENTITY_ID' => $indexes,
146 'SRC_ENTITY_TYPE_ID' => $client->getSrcEntityTypeId(),
147 'DST_ENTITY_TYPE_ID' => $client->getDstEntityTypeId()])
148 :[];
149 }
150
151 static protected function factoryClient($typeName)
152 {
153 $typeId = BusinessValuePersonDomainType::resolveID($typeName);
154
155 if(BusinessValuePersonDomainType::isDefined($typeId))
156 {
157 if($typeName == BusinessValuePersonDomainType::TYPE_E_NAME)
158 {
159 return new Company();
160 }
161 elseif($typeName == BusinessValuePersonDomainType::TYPE_I_NAME)
162 {
163 return new Contact();
164 }
165 }
166
167 throw new \Bitrix\Main\NotSupportedException("Client : '".$typeId."' is not supported in current context");
168 }
169
170 static protected function getIndexesFromParams(array $params)
171 {
172 return array_keys($params);
173 }
174 static protected function getIndexesContactFromParams($contacts)
175 {
176 $result = [];
177 if(count($contacts)>0)
178 {
179 foreach ($contacts as $contact)
180 {
181 $result[] = $contact['CONTACT_ID'];
182 }
183 }
184 return $result;
185 }
186 static protected function getContactItemsFromIndexes($indexes)
187 {
188 $result = [];
189 if(count($indexes)>0)
190 {
191 foreach ($indexes as $index)
192 {
193 $result[] = ['CONTACT_ID'=>$index];
194 }
195 }
196 return $result;
197 }
198
199 static protected function prepareFieldsAdds($fields)
200 {
201 $result = [];
202 foreach ($fields as $index=>$item)
203 {
204 $result[$index] = ['fields'=>$item];
205 }
206 return $result;
207 }
208
209 protected function addsFromParams(array $params)
210 {
211 $res = static::proxyAdds($params);
212 if(count($res['result'])>0)
213 {
214 $this->relationRefreshByDstEntity($res['result']);
215 }
216 if(count($res['result_error'])>0)
217 {
218 foreach($res['result_error'] as $index=>$error)
219 {
220 $item = $this->getCollection()->getItemByIndex($index);
221 $item->setError(new Error($error['error_description']));
222 }
223 }
224 return $this;
225 }
226 public function adds()
227 {
228 return $this
229 ->addsFromParams($this
230 ->getCollection()->toArray());
231 }
232
233 public function getCollection()
234 {
235 return $this->collection;
236 }
237}