Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Pin.php
1<?php
2
4
15use Exception;
16
17class Pin extends AbstractSwitcher
18{
19 public const PIN_CONTEXT = '';
20
21 private ?Internals\Pin\Pin $pin = null;
22
23 public function enable(): Result
24 {
25 $result = new Result();
26 if ($this->isEnabled())
27 {
28 return $result;
29 }
30
31 $result = (new Internals\Pin\Pin())
32 ->setGroupId($this->spaceId)
33 ->setUserId($this->userId)
34 ->setContext($this->code)
35 ->save();
36
37 $this->invalidate();
38
39 return $result;
40 }
41
42 public function disable(): Result
43 {
44 $result = new Result();
45 if (!$this->isEnabled())
46 {
47 return $result;
48 }
49
50 $result = $this->pin->delete();
51 $this->invalidate();
52
53 return $result;
54 }
55
56 public function getValue(): string
57 {
58 if ($this->isInitialized)
59 {
60 return $this->value;
61 }
62
63 try
64 {
65 $this->setPin();
66 $this->value = is_null($this->pin) ? static::TYPE_OFF : static::TYPE_ON;
67 $this->isInitialized = true;
68 }
69 catch (Exception)
70 {
71 $this->value = static::TYPE_OFF;
72 }
73
74 return $this->value;
75 }
76
80 private function getContextFilter(): ConditionTree
81 {
82 $filter = Query::filter();
83 return $this->code === ''
84 ? $filter->logic('or')->whereNull('CONTEXT')->where('CONTEXT', '')
85 : $filter->where('CONTEXT', $this->code);
86 }
87
88 public function getMessage(): ?string
89 {
90 return $this->isEnabled() ? static::getUnpinnedMessage() : static::getPinnedMessage();
91 }
92
98 private function setPin(): static
99 {
100 $query = WorkgroupPinTable::query()
101 ->setSelect([
102 'ID',
103 'GROUP_ID',
104 'USER_ID',
105 ])
106 ->where('GROUP_ID', $this->spaceId)
107 ->where('USER_ID', $this->userId)
108 ->where($this->getContextFilter());
109
110 $this->pin = $query->exec()->fetchObject();
111
112 return $this;
113 }
114
115 protected function canSwitch(): bool
116 {
117 return $this->isSpaceExists();
118 }
119
120 public static function getDefaultCode(): string
121 {
122 return static::PIN_CONTEXT;
123 }
124
125 public static function getPinnedMessage(): ?string
126 {
127 Loc::loadMessages(__FILE__);
128 return Loc::getMessage('SOCIALNETWORK_SPACES_SPACE_PIN');
129 }
130
131 public static function getUnpinnedMessage(): ?string
132 {
133 Loc::loadMessages(__FILE__);
134 return Loc::getMessage('SOCIALNETWORK_SPACES_SPACE_UNPIN');
135 }
136}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29