Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
segment.php
1<?php
10
14
18
19Loc::loadMessages(__FILE__);
20
26{
32 public function getId()
33 {
34 return 'crm-segment';
35 }
36
42 public function isInstalled()
43 {
44 $list = Entity\Segment::getList(array(
45 'select' => array('ID'),
46 'filter' => array(
47 '=IS_SYSTEM' => true,
48 '%CODE' => 'crm_%'
49 ),
50 'limit' => 1
51 ));
52
53 return ($list->fetch()) ? true : false;
54 }
55
62 public function install()
63 {
64 Loader::includeModule('crm');
65 foreach ($this->getSegments() as $data)
66 {
67 if ($this->getInstalledSegment($data['CODE']))
68 {
69 continue;
70 }
71
72 $data['IS_SYSTEM'] = 'Y';
73
74 try
75 {
76 $segment = new Entity\Segment;
77 $segment->mergeData($data)->save();
78 }
79 catch (SqlQueryException $exception)
80 {
81 if (mb_strpos($exception->getDatabaseMessage(), '(1062)') === false)
82 {
83 throw $exception;
84 }
85 }
86
87
88 }
89
90 return true;
91 }
92
98 public function uninstall()
99 {
100 $segments = Entity\Segment::getList(array(
101 'select' => array('ID'),
102 'filter' => array(
103 '=IS_SYSTEM' => true,
104 '%CODE' => 'crm_%'
105 )
106 ));
107 foreach ($segments as $segment)
108 {
109 if (Entity\Segment::removeById($segment['ID']))
110 {
111 continue;
112 }
113
114 return false;
115 }
116
117 return true;
118 }
119
120 private function getSegments()
121 {
122 $endpointsAll = array();
123
124 $list = array();
126 foreach (Connectors\Lead::getUiFilterPresets() as $code => $data)
127 {
128 if (empty($data['sender_segment_name']))
129 {
130 continue;
131 }
132
133 $segmentCode = $code;
134 if (!empty($data['sender_segment_business_case']))
135 {
136 $segmentCode = "case_" . $segmentCode;
137 }
138
139 $fields = $data['fields'];
140 $fields['BX_PRESET_ID'] = $code;
141
142 $item = array(
143 'CODE' => $segmentCode,
144 'NAME' => $data['sender_segment_name'],
145 'SORT' => 100,
146 'ENDPOINTS' => array(
147 array(
148 'MODULE_ID' => 'sender',
149 'CODE' => $connector->getCode(),
150 'FIELDS' => self::convertPresetFields($fields)
151 )
152 )
153 );
154
155 $list[] = $item;
156 if ($item['CODE'] === 'crm_lead_all')
157 {
158 $endpointsAll = array_merge($endpointsAll, $item['ENDPOINTS']);
159 }
160 }
161
162
163 $connector = new Connectors\Client;
164 foreach (Connectors\Client::getUiFilterPresets() as $code => $data)
165 {
166 if (empty($data['sender_segment_name']))
167 {
168 continue;
169 }
170
171 $segmentCode = $code;
172 if (!empty($data['sender_segment_business_case']))
173 {
174 $segmentCode = "case_" . $segmentCode;
175 }
176
177 $fields = $data['fields'];
178 $fields['BX_PRESET_ID'] = $code;
179
180 $item = array(
181 'CODE' => $segmentCode,
182 'NAME' => $data['sender_segment_name'],
183 'SORT' => 100,
184 'ENDPOINTS' => array(
185 array(
186 'MODULE_ID' => 'sender',
187 'CODE' => $connector->getCode(),
188 'FIELDS' => self::convertPresetFields($fields)
189 )
190 )
191 );
192
193 $list[] = $item;
194 if ($item['CODE'] === 'crm_client_all')
195 {
196 $endpointsAll = array_merge($endpointsAll, $item['ENDPOINTS']);
197 }
198 }
199
200 if (count($endpointsAll) > 1)
201 {
202 $list[] = array(
203 'CODE' => Entity\Segment::CODE_ALL,
204 'NAME' => Loc::getMessage('SENDER_INTEGRATION_CRM_PRESET_SEGMENT_ALL'),
205 'SORT' => 50,
206 'ENDPOINTS' => $endpointsAll
207 );
208 }
209
210 return $list;
211 }
212
213 private static function convertPresetFields($fields)
214 {
215 if (!is_array($fields))
216 {
217 return $fields;
218 }
219
220 $codes = ['allow_year', 'datesel', 'from', 'to', 'days'];
221 $result = [];
222 foreach ($fields as $key => $value)
223 {
224 $baseKey = null;
225 foreach ($codes as $code)
226 {
227 $code = "_" . $code;
228 if (mb_substr($key, -mb_strlen($code)) == $code)
229 {
230 $baseKey = mb_substr($key, 0, -mb_strlen($code));
231 break;
232 }
233 }
234
235 if ($baseKey)
236 {
237 if (empty($result[$baseKey]))
238 {
239 $result[$baseKey] = [];
240 }
241
242 $result[$baseKey][$key] = $value;
243 }
244 else
245 {
246 $result[$key] = $value;
247 }
248 }
249
250 return $result;
251 }
252
253 private function getInstalledSegment($code = null)
254 {
255 $filter = array(
256 '=IS_SYSTEM' => true,
257 );
258 if ($code)
259 {
260 $filter['=CODE'] = $code;
261 }
262 else
263 {
264 $filter['CODE'] = 'crm_%';
265 }
266
267 $list = Entity\Segment::getList(array(
268 'select' => array('ID'),
269 'filter' => $filter,
270 'limit' => 1
271 ));
272 if ($segment = $list->fetch())
273 {
274 return $segment['ID'];
275 }
276 else
277 {
278 return null;
279 }
280 }
281}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29