Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
tileview.php
1<?php
8namespace Bitrix\Sender\UI;
9
12
13Loc::loadMessages(__FILE__);
14
20{
21 public const MAX_COUNT = 4;
22 public const COLOR_GREY = '#eef2f4';
23
24 public const SECTION_ALL = 'all';
25 public const SECTION_LAST = 'last';
26 public const SECTION_FREQ = 'freq';
27 public const SECTION_SYS = 'system';
28 public const SECTION_MY = 'my';
29
31 protected $tiles = [];
32
34 protected $sections = [];
35
41 public static function create()
42 {
43 return new static();
44 }
45
49 public function __construct()
50 {
51
52 }
53
65 public function addTile($id, $name, $data = [], $bgColor = null, $color = null)
66 {
67 $this->tiles[] = $this->getTile($id, $name, $data, $bgColor, $color);
68 return $this;
69 }
70
82 public function getTile($id, $name, $data = [], $bgColor = null, $color = null)
83 {
84 return [
85 'id' => $id,
86 'name' => $name,
87 'data' => $data,
88 'bgcolor' => $bgColor,
89 'color' => $color
90 ];
91 }
92
98 public function getTiles()
99 {
100 return $this->tiles;
101 }
102
108 public function removeTiles()
109 {
110 $this->tiles = [];
111 return $this;
112 }
113
122 public function addSection($id, $name = null)
123 {
124 $name = $name ?: Loc::getMessage('SENDER_UI_TILEVIEW_SECTION_'.mb_strtoupper($id));
125 $name = $name ?: $id;
126
127 $this->sections[$id] = [
128 'id' => $id,
129 'name' => $name,
130 'items' => [],
131 ];
132 return $this;
133 }
134
140 public function getSections()
141 {
142 return $this->sections;
143 }
144
150 public function removeSections()
151 {
152 $this->sections = [];
153 return $this;
154 }
155
161 public function get()
162 {
163 $list = $this->sections;
164 $sections = array_keys($list);
165
166 if (in_array(self::SECTION_ALL, $sections))
167 {
168 $list[self::SECTION_ALL]['items'] = $this->tiles;
169 }
170
171 foreach ($this->tiles as $tile)
172 {
173 foreach ($sections as $section)
174 {
175 if ($section === self::SECTION_ALL)
176 {
177 continue;
178 }
179
180 if (empty($tile['data'][$section]))
181 {
182 continue;
183 }
184
185 self::prepareTileForSorting($tile, $section);
186 $list[$section]['items'][] = $tile;
187 }
188 }
189
190 self::sortTiles($list);
191 return array_values($list);
192 }
193
194 protected static function prepareTileForSorting(&$tile, $section)
195 {
196 if ($section === self::SECTION_LAST)
197 {
198 $last = $tile['data'][self::SECTION_LAST];
199 if ($last instanceof DateTime)
200 {
201 $tile['data'][self::SECTION_LAST] = $last->getTimestamp();
202 }
203 }
204 }
205
206 protected static function sortTiles(&$list)
207 {
208 foreach ([self::SECTION_LAST, self::SECTION_FREQ] as $section)
209 {
210 if (!isset($list[$section]))
211 {
212 continue;
213 }
214
215 // sort & cut
216 usort(
217 $list[$section]['items'],
218 function ($a, $b) use ($section)
219 {
220 return ($a['data'][$section] > $b['data'][$section]) ? -1 : 1;
221 }
222 );
223 $list[$section]['items'] = array_slice($list[$section]['items'], 0, self::MAX_COUNT);
224 }
225 }
226}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
addTile($id, $name, $data=[], $bgColor=null, $color=null)
Definition tileview.php:65
static prepareTileForSorting(&$tile, $section)
Definition tileview.php:194
getTile($id, $name, $data=[], $bgColor=null, $color=null)
Definition tileview.php:82
static sortTiles(&$list)
Definition tileview.php:206
addSection($id, $name=null)
Definition tileview.php:122