Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
options.php
1<?php
2
3namespace Bitrix\Main\Grid;
4
6
12class Options extends \CGridOptions
13{
15 protected $id;
16
17
24 public function __construct($gridId, array $filterPresets = array())
25 {
26 $this->id = $gridId;
27 parent::__construct($gridId, $filterPresets);
28
29 if (!static::isAuthorized() &&
30 isset(Application::getInstance()->getSession()["main.ui.grid.options"][$this->id]) &&
31 is_array(Application::getInstance()->getSession()["main.ui.grid.options"][$this->id]) &&
32 !empty(Application::getInstance()->getSession()["main.ui.grid.options"][$this->id]))
33 {
34 $this->all_options = Application::getInstance()->getSession()["main.ui.grid.options"][$this->id];
35 }
36 }
37
38
43 public function getId()
44 {
45 return $this->id;
46 }
47
48
53 protected static function getUser()
54 {
55 global $USER;
56 return $USER;
57 }
58
59
65 public function setColumnsSizes($expand, $sizes)
66 {
67 $columns = array();
68 foreach ((array) $sizes as $name => $width)
69 {
70 $name = trim($name);
71 $width = is_scalar($width) ? (int) $width : 0;
72 if ($name != '' && $width > 0)
73 $columns[$name] = $width;
74 }
75
76 $this->all_options['views'][$this->currentView]['columns_sizes'] = array(
77 'expand' => is_scalar($expand) ? round((float) $expand, 8) : 1,
78 'columns' => $columns
79 );
80 }
81
82
87 public function setPageSize($size)
88 {
89 $size = is_scalar($size) ? (int) $size : 20;
90 $size = $size >= 0 ? $size : 20;
91
92 $this->all_options['views'][$this->currentView]['page_size'] = $size;
93 }
94
95
100 public function setCustomNames($names)
101 {
102 $this->all_options["views"]["default"]["custom_names"] = $names;
103 }
104
105
109 public function resetExpandedRows()
110 {
111 $this->setExpandedRows();
112 }
113
114
119 public function setExpandedRows($ids = array())
120 {
121 Application::getInstance()->getSession()["main.ui.grid"][$this->getId()]["expanded_rows"] = $ids;
122 }
123
124
129 public function getExpandedRows()
130 {
131 return Application::getInstance()->getSession()["main.ui.grid"][$this->getId()]["expanded_rows"] ?? null;
132 }
133
134
139 public function setCollapsedGroups($ids = array())
140 {
141 Application::getInstance()->getSession()["main.ui.grid"][$this->getId()]["collapsed_groups"] = is_array($ids) ? $ids : array();
142 }
143
144
149 public function getCollapsedGroups()
150 {
151 return Application::getInstance()->getSession()["main.ui.grid"][$this->getId()]["collapsed_groups"] ?? null;
152 }
153
154
159 public function resetView($viewId)
160 {
161 $gridId = $this->getId();
162 $this->all_options["views"][$viewId] = array();
163 unset(Application::getInstance()->getSession()["main.interface.grid"][$gridId]);
164 unset(Application::getInstance()->getSession()["main.ui.grid"][$gridId]);
165 $this->Save();
166 parent::__construct($gridId);
167 }
168
169
174 public function deleteView($viewId)
175 {
176 $gridId = $this->getId();
177 unset($this->all_options["views"][$viewId]);
178 unset(Application::getInstance()->getSession()["main.interface.grid"][$gridId]);
179 unset(Application::getInstance()->getSession()["main.ui.grid"][$gridId]);
180 $this->Save();
181 parent::__construct($gridId);
182 }
183
184
188 public function getCurrentOptions()
189 {
190 $options = $this->getOptions();
191 $currentViewId = $options["current_view"];
192 return $options["views"][$currentViewId];
193 }
194
195
199 private static function getDefaultGetSortingResult()
200 {
201 return array(
202 "sort" => array(),
203 "vars" => array(
204 "by" => "by",
205 "order" => "order"
206 )
207 );
208 }
209
210
216 public function getSorting($default = array())
217 {
218 $result = static::getDefaultGetSortingResult();
219 $result["sort"] = isset($default["sort"]) && is_array($default["sort"]) ? $default["sort"] : $result["sort"];
220 $result["vars"] = isset($default["vars"]) && is_array($default["vars"]) ? $default["vars"] : $result["vars"];
221
222 $options = $this->getCurrentOptions();
223
224 if (!empty($options["last_sort_by"]) && !empty($options["last_sort_order"]))
225 {
226 $result["sort"] = array($options["last_sort_by"] => $options["last_sort_order"]);
227 }
228
229 return $result;
230 }
231
232
237 protected static function getUserId()
238 {
239 $userId = static::getUser()->getID();
240 return is_scalar($userId) ? (int) $userId : 0;
241 }
242
243
248 protected static function isAuthorized()
249 {
250 return static::getUser()->isAuthorized();
251 }
252
253
257 public function save()
258 {
259 $gridId = $this->getId();
260
261 if (static::getUser()->isAuthorized())
262 {
263 \CUserOptions::setOption("main.interface.grid", $gridId, $this->all_options);
264 }
265 else
266 {
267 Application::getInstance()->getSession()["main.ui.grid.options"][$gridId] = $this->all_options;
268 }
269 }
270
271
277 public function getUsedColumns($defaultColumns = array())
278 {
279 $currentOptions = $this->getCurrentOptions();
280
281 if (is_string($currentOptions["columns"]) && $currentOptions["columns"] !== "")
282 {
283 return explode(",", $currentOptions["columns"]);
284 }
285
286 return $defaultColumns;
287 }
288
289
294 public function setStickedColumns($columns = [])
295 {
296 $this->all_options["views"]["default"]["sticked_columns"] = is_array($columns) ? $columns : [];
297 }
298
303 public function getStickedColumns()
304 {
305 $currentOptions = $this->getCurrentOptions();
306
307 if (isset($currentOptions["sticked_columns"]) && is_array($currentOptions["sticked_columns"]))
308 {
309 return $currentOptions["sticked_columns"];
310 }
311
312 return null;
313 }
314}
setStickedColumns($columns=[])
Definition options.php:294
setColumnsSizes($expand, $sizes)
Definition options.php:65
getUsedColumns($defaultColumns=array())
Definition options.php:277
setExpandedRows($ids=array())
Definition options.php:119
getSorting($default=array())
Definition options.php:216
setCollapsedGroups($ids=array())
Definition options.php:139
__construct($gridId, array $filterPresets=array())
Definition options.php:24