Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Composition.php
1<?php
2
4
16use Exception;
17
19{
20 public const FILTER = 'MODULE_ID';
21 public const CACHE_ID = 'socialnetwork-toolbar-composition-';
22
23 private CompositionItemCollection $collection;
24 private Cache $cache;
25
26 public function __construct(private int $userId, private int $spaceId = 0)
27 {
28 $this->init();
29 }
30
31 public function setDefaultSettings(): Result
32 {
33 $result = new Result();
34 try
35 {
36 if (!SpaceCompositionTable::isDataFilled($this->userId, $this->spaceId))
37 {
38 $result = $this->setSettings($this->getDefaultSettings());
39 }
40 }
41 catch (Exception $exception)
42 {
43 $result->addError(Error::createFromThrowable($exception));
44 }
45
46 return $result;
47 }
48
49 public function setSettings(array $settings): Result
50 {
51 $result = new Result();
53 try
54 {
55 $composition = SpaceCompositionTable::getByIds($this->userId, $this->spaceId);
56 if (is_null($composition))
57 {
58 $result = SpaceCompositionTable::fill($this->userId, $this->spaceId, $collection->toArray());
59 }
60 else
61 {
62 $result = $composition
63 ->setSettings($collection->toArray())
64 ->save();
65 }
66 }
67 catch (Exception $exception)
68 {
69 $result->addError(Error::createFromThrowable($exception));
70 }
71
72 if ($result->isSuccess())
73 {
74 $this->cache->store($result->getData()['SETTINGS']);
75 }
76
77 return $result;
78 }
79
80 public function getDefaultSettings(bool $withHidden = true): array
81 {
82 $items = [];
83 foreach ($this->collection as $item)
84 {
86 if ($item->isHidden() && !$withHidden)
87 {
88 continue;
89 }
90
91 $items[] = $item->getModuleId();
92 }
93
94 return $items;
95 }
96
97 public function getSettings(bool $withHidden = true): array
98 {
99 if ($data = $this->cache->get())
100 {
102 !$withHidden && $collection->hideItems();
103
104 return $collection->toArray();
105 }
106
107 try
108 {
109 $composition = SpaceCompositionTable::getByIds($this->userId, $this->spaceId);
110 $collection = CompositionItemCollection::createFromModuleIds((array)$composition?->getSettings());
111 !$withHidden && $collection->hideItems();
112
113 return $collection->toArray();
114 }
115 catch (Exception)
116 {
117 return $this->getDefaultSettings();
118 }
119 }
120
121 public function getDeselectedSettings(bool $withHidden = true): array
122 {
123 return array_diff($this->getDefaultSettings($withHidden), $this->getSettings());
124 }
125
126 private function init(): void
127 {
128 $this->cache = new Cache($this->userId, $this->spaceId);
129
130 $this->collection = (new CompositionItemCollection())
131 ->addItem(new Task())
132 ->addItem(new CalendarEvent())
133 ->addItem(new Message())
134 ->addItem(new BusinessProcess())
135 ->addItem(new ListElement());
136 }
137
138 private function getCacheId(): string
139 {
140 return static::CACHE_ID . $this->userId . '-' . $this->spaceId;
141 }
142}
__construct(private int $userId, private int $spaceId=0)