Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
reportstorelist.php
1<?php
2
4
9
10abstract class ReportStoreList extends \CBitrixComponent
11{
12 abstract protected function getGridColumns(): array;
13
14 abstract protected function getReportProductGridComponentName(): string;
15
16 abstract protected function getTotalFields(): array;
17
18 abstract protected function getGridId(): string;
19
20 public function executeComponent()
21 {
22 if (!Loader::includeModule('catalog') || !self::checkDocumentReadRights())
23 {
24 $this->arResult['ERROR_MESSAGES'][] = Loc::getMessage('CATALOG_REPORT_STORE_LIST_NO_READ_RIGHTS_ERROR');
25 $this->includeComponentTemplate();
26
27 return;
28 }
29
30 $this->arResult['GRID'] = $this->getGridData();
31 $this->arResult['GRID_FILTER'] = $this->getGridFilter();
32 $this->arResult['PRODUCT_LIST_SLIDER_URL'] = $this->getProductListComponentUrl();
33
34 $this->includeComponentTemplate();
35 }
36
37 private function getGridData(): array
38 {
39 $result = [
40 'GRID_ID' => $this->getGridId(),
41 'COLUMNS' => $this->getGridColumns(),
42 'ROWS' => [],
43 ];
44
45 if (
46 isset($this->arParams['RESULT']['data']['stub'])
47 && is_array($this->arParams['RESULT']['data']['stub'])
48 )
49 {
50 $result['STUB'] = $this->arParams['RESULT']['data']['stub'];
51
52 return $result;
53 }
54
55 $providerData = $this->arParams['RESULT']['data']['items'];
56 $overallData = $this->arParams['RESULT']['data']['overall'];
57
58 if (!empty($providerData))
59 {
60 foreach($providerData as $storeId => $item)
61 {
62 $result['ROWS'][] = [
63 'id' => $storeId,
64 'data' => $item,
65 'columns' => $this->prepareItemColumn($item),
66 ];
67 }
68
69 $result['ROWS'][] = $this->prepareOverallTotalRow($overallData);
70 }
71
72 $result['SHOW_PAGINATION'] = false;
73 $result['SHOW_NAVIGATION_PANEL'] = false;
74 $result['SHOW_PAGESIZE'] = false;
75 $result['SHOW_ROW_CHECKBOXES'] = false;
76 $result['SHOW_CHECK_ALL_CHECKBOXES'] = false;
77 $result['SHOW_ACTION_PANEL'] = false;
78 $result['HANDLE_RESPONSE_ERRORS'] = true;
79 $result['SHOW_GRID_SETTINGS_MENU'] = false;
80 $result['ALLOW_STICKED_COLUMNS'] = true;
81
82 return $result;
83 }
84
85 private function prepareItemColumn(array $item): array
86 {
87 $column = $item;
88
89 $column['TITLE'] = $this->prepareTitleViewForColumn($column);
90 if (isset($column['STORE_ID']))
91 {
92 $column['STORE_ID'] = (int)$column['STORE_ID'];
93 }
94
95 foreach ($this->getTotalFields() as $totalField)
96 {
97 $column[$totalField] = $this->prepareTotalField($column['TOTALS'], $totalField);
98 }
99
100 unset($column['TOTALS']);
101
102 return $column;
103 }
104
105 private function prepareOverallTotalRow(array $overallData): array
106 {
107 $overallColumns = [];
108 $overallColumns['TITLE'] = Loc::getMessage('CATALOG_REPORT_STORE_LIST_OVERALL_TOTAL');
109
110 foreach ($this->getTotalFields() as $totalField)
111 {
112 $overallColumns[$totalField] = $this->prepareTotalField($overallData, $totalField);
113 }
114
115 return [
116 'id' => 'overallTotal',
117 'data' => $overallData,
118 'columns' => $overallColumns,
119 ];
120 }
121
122 private function prepareTotalField(array $totals, string $field): string
123 {
124 if (empty($totals))
125 {
126 return 0;
127 }
128
129 $result = '';
130 foreach ($totals as $measureId => $total)
131 {
132 $result .= $this->formatNumberWithMeasure($total[$field], (int)$measureId);
133 $result .= '<br>';
134 }
135
136 return $result;
137 }
138
139 private function formatNumberWithMeasure($number, int $measureId)
140 {
141 return Loc::getMessage(
142 'CATALOG_REPORT_STORE_LIST_MEASURE_TEMPLATE',
143 [
144 '#NUMBER#' => $number,
145 '#MEASURE_SYMBOL#' => $this->getMeasureSymbol($measureId),
146 ]
147 );
148 }
149
150 private function prepareTitleViewForColumn(array $column): string
151 {
152 if (!isset($column['TITLE'], $column['STORE_ID']))
153 {
154 return '';
155 }
156
157 if ($column['TITLE'])
158 {
159 $title = htmlspecialcharsbx($column['TITLE']);
160 }
161 else
162 {
163 $title = Loc::getMessage('CATALOG_REPORT_STORE_LIST_EMPTY_STORE_NAME');
164 }
165
166 return $title;
167 }
168
169 private function getMeasureSymbol(int $measureId): string
170 {
171 $measure = $this->getMeasures()[$measureId] ?? null;
172
173 return $measure !== null ? htmlspecialcharsbx($measure['SYMBOL']) : '';
174 }
175
176 private function getMeasures(): array
177 {
178 static $measures = [];
179
180 if (empty($measures))
181 {
182 $measuresResult = \CCatalogMeasure::getList();
183 while ($measure = $measuresResult->Fetch())
184 {
185 $measures[$measure['ID']] = $measure;
186 }
187 }
188
189 return $measures;
190 }
191
192 private function getGridFilter(): array
193 {
194 return $this->arParams['RESULT']['data']['filter'];
195 }
196
197 protected function getProductListComponentUrl(): string
198 {
199 $productGridPath = \CComponentEngine::makeComponentPath($this->getReportProductGridComponentName());
200
201 return getLocalPath('components' . $productGridPath . '/slider.php');
202 }
203
204 private static function checkDocumentReadRights(): bool
205 {
206 return AccessController::getCurrent()->check(ActionDictionary::ACTION_CATALOG_READ);
207 }
208}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29