Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
tileview.php
1<?php
10
18
19Loc::loadMessages(__FILE__);
20
28{
29 public const MAX_COUNT = 4;
30
32 protected $isInclude = true;
33
35 protected $message;
36
43 public static function create($isInclude = true)
44 {
45 return new static($isInclude);
46 }
47
53 public function __construct($isInclude = true)
54 {
55 $this->isInclude = $isInclude;
56 }
57
65 public function setMessage(Message\Adapter $message = null)
66 {
67 $this->message = $message;
68 return $this;
69 }
70
76 public function getSections()
77 {
78 $list = [
79 'last' => [
80 'id' => 'last',
81 'name' => Loc::getMessage('SENDER_SEGMENT_TILEVIEW_SECTION_LAST'),
82 'items' => [],
83 ],
84 'freq' => [
85 'id' => 'freq',
86 'name' => Loc::getMessage('SENDER_SEGMENT_TILEVIEW_SECTION_FREQ'),
87 'items' => [],
88 ],
89 'system' => [
90 'id' => 'system',
91 'name' => Loc::getMessage('SENDER_SEGMENT_TILEVIEW_SECTION_SYSTEM'),
92 'items' => [],
93 ],
94 'case' => [
95 'id' => 'case',
96 'name' => Loc::getMessage('SENDER_SEGMENT_TILEVIEW_SECTION_CASE'),
97 'items' => [],
98 ],
99 'my' => [
100 'id' => 'my',
101 'name' => Loc::getMessage('SENDER_SEGMENT_TILEVIEW_SECTION_MY'),
102 'items' => [],
103 ],
104 ];
105 $tiles = $this->getTiles(['filter' => ['=HIDDEN' => false]]);
106
107 foreach ($tiles as $tile)
108 {
109 // set last
110 if ($tile['data']['last'])
111 {
113 $last = $tile['data']['last'];
114 $tile['data']['last'] = $last->getTimestamp();
115
116 $list['last']['items'][] = $tile;
117 }
118
119 // set freq
120 if ($tile['data']['freq'])
121 {
122 $list['freq']['items'][] = $tile;
123 }
124
125 // set cases
126 if ($tile['data']['case'])
127 {
128 $list['case']['items'][] = $tile;
129 }
130
131 // set system or my
132 $key = $tile['data']['system'] ? 'system' : 'my';
133 $list[$key]['items'][] = $tile;
134 }
135
136 // sort & cut last
137 usort(
138 $list['last']['items'],
139 function ($a, $b) {
140 return ($a['data']['last'] > $b['data']['last']) ? -1 : 1;
141 }
142 );
143 $list['last']['items'] = array_slice($list['last']['items'], 0, self::MAX_COUNT);
144
145 // sort freq
146 usort(
147 $list['freq']['items'],
148 function ($a, $b) {
149 return ($a['data']['freq'] > $b['data']['freq']) ? -1 : 1;
150 }
151 );
152 $list['freq']['items'] = array_slice($list['freq']['items'], 0, self::MAX_COUNT);
153
154 // remove empty sections
155 foreach ($list as $sectionId => $section)
156 {
157 if (count($section['items']) > 0)
158 {
159 continue;
160 }
161
162 unset($list[$sectionId]);
163 }
164 $list = array_values($list);
165
166 return $list;
167 }
168
175 public function getTile($segmentId)
176 {
177 $tiles = $this->getTiles(['filter' => ['=ID' => $segmentId]]);
178 $tile = current($tiles) ?: null;
179 return $tile;
180 }
181
188 public function getTiles(array $parameters = [])
189 {
190 $result = [];
191 $ids = [];
192
193 $prefix = $this->isInclude ? '' : '_EXCLUDE';
194 $fieldDateUse = "DATE_USE$prefix";
195 $fieldUseCount = "USE_COUNT$prefix";
196
197 if (!isset($parameters['order']))
198 {
199 $parameters['order'] = [
200 'SORT' => 'ASC',
201 'NAME' => 'ASC',
202 $fieldDateUse => 'DESC',
203 ];
204 }
205
206 $segments = GroupTable::getList($parameters);
207
208 foreach ($segments as $segment)
209 {
210 $item = [
211 'id' => $segment['ID'],
212 'name' => $segment['NAME'],
213 'data' => [
214 'last' => $segment[$fieldDateUse],
215 'freq' => (int)$segment[$fieldUseCount],
216 'case' => mb_substr($segment['CODE'], 0, 5) === 'case_',
217 'hidden' => $segment['HIDDEN'] === 'Y',
218 'system' => $segment['IS_SYSTEM'] === 'Y',
219 'hasStatic' => false,
220 'count' => [],
221 ],
222 ];
223
224 $item['bgcolor'] = self::getBackgroundColor($item['data']);
225 $result[] = $item;
226 $ids[] = $item['id'];
227 }
228
229 if (count($ids) > 0)
230 {
231 $connectors = GroupConnectorTable::getList([
232 'filter' => [
233 '@GROUP_ID' => $ids,
234 ],
235 ]);
236
237 $hasStatic = [];
238 foreach ($connectors as $connector)
239 {
240 if (!is_array($connector['ENDPOINT']))
241 {
242 $hasStatic[$connector['GROUP_ID']] = false;
243 continue;
244 }
245 $entityConnector = \Bitrix\Sender\Connector\Manager::getConnector($connector['ENDPOINT']);
246 $hasStatic[$connector['GROUP_ID']] = $entityConnector instanceof Contact
247 && $connector['ADDRESS_COUNT'] > 0
248 ;
249 }
250
251 $duration = null;
252 $messageTypes = [];
253 if ($this->message)
254 {
255 $duration = Dispatch\DurationCountBased::create($this->message);
256 $messageTypes = $this->message->getSupportedRecipientTypes();
257 }
258
259 $counters = Entity\Segment::getAddressCounters($ids);
260 foreach ($result as $index => $item)
261 {
262 if (!isset($counters[$item['id']]))
263 {
264 continue;
265 }
266
267 // set count
268 $item['data']['count'] = $counters[$item['id']];
269 $item['data']['hasStatic'] = $hasStatic[$item['id']];
270
271 // set duration
272 foreach ($item['data']['count'] as $typeId => $count)
273 {
274 if (isset($item['data']['duration']) && $item['data']['duration'])
275 {
276 continue;
277 }
278
279 if (!$duration || !$this->message)
280 {
281 continue;
282 }
283
284 if (!in_array($typeId, $messageTypes))
285 {
286 continue;
287 }
288
289 $item['data']['duration'] = $duration->getIntervalDefault($count);
290 }
291
292 $result[$index] = $item;
293 }
294 }
295
296 return $result;
297 }
298
299 protected static function getBackgroundColor(array $data)
300 {
301 if ($data['system'])
302 {
303 return null;//'#d3ffcd';
304 }
305 elseif ($data['hidden'])
306 {
307 return '#eef2f4';
308 }
309 else
310 {
311 return null;
312 }
313 }
314}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
__construct($isInclude=true)
Definition tileview.php:53
static getBackgroundColor(array $data)
Definition tileview.php:299
setMessage(Message\Adapter $message=null)
Definition tileview.php:65
getTiles(array $parameters=[])
Definition tileview.php:188
static create($isInclude=true)
Definition tileview.php:43