Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
conference.php
1<?php
2
4
15use Bitrix\Im\Call\Conference as ConferenceClass;
16
18{
19 protected function processBeforeAction(Action $action): bool
20 {
21 if (!Loader::includeModule('im'))
22 {
23 $this->addError(new Error("Module IM is not installed"));
24
25 return false;
26 }
27
28 if (User::getInstance()->isExtranet())
29 {
30 $this->addError(new Error("You dont have access to this action"));
31
32 return false;
33 }
34
35 return true;
36 }
37
38 public function prepareAction(JsonPayload $payload): array
39 {
40 $result = [];
41 $result['ALIAS_DATA'] = Alias::addUnique([
42 "ENTITY_TYPE" => Alias::ENTITY_TYPE_VIDEOCONF,
43 "ENTITY_ID" => 0
44 ]);
45 $result['DEFAULT_TITLE'] = \CIMChat::getNextConferenceDefaultTitle();
46
47 return $result;
48 }
49
50 public function createAction(JsonPayload $payload)
51 {
52 if (!isset($payload->getData()['fields']))
53 {
54 $this->addError(new Error(Loc::getMessage('IM_CONFERENCE_EDIT_CREATION_ERROR')));
55
56 return null;
57 }
58
59 $fields = $payload->getData()['fields'];
60 $fields = array_change_key_case($fields, CASE_UPPER);
61 $fields = self::recursiveWhiteList($fields, \Bitrix\Im\Call\Conference::AVAILABLE_PARAMS);
62
63 $fields['ID'] = isset($fields['ID']) ? (int)$fields['ID'] : 0;
64 if ($fields['ID'] > 0)
65 {
66 $updatingResult = $this->updateConference($fields);
67 if (!$updatingResult->isSuccess())
68 {
69 $this->addErrors($updatingResult->getErrors());
70
71 return null;
72 }
73
74 return $updatingResult;
75 }
76
77 $addingResult = $this->createConference($payload, $fields);
78 if (!$addingResult->isSuccess())
79 {
80 $this->addErrors($addingResult->getErrors());
81
82 return null;
83 }
84
85 $aliasData = $addingResult->getData()['ALIAS_DATA'];
86
87 return [
88 'CHAT_ID' => $addingResult->getData()['CHAT_ID'],
89 'ALIAS' => $aliasData['ALIAS'],
90 'LINK' => $aliasData['LINK']
91 ];
92 }
93
94 private function updateConference(array $fields): Result
95 {
96 $updatingResult = new Result();
97 $conference = ConferenceClass::getById($fields['ID']);
98
99 if (!$conference)
100 {
101 return $updatingResult->addError(new Error(Loc::getMessage('IM_CONFERENCE_EDIT_ERROR_WRONG_ID')));
102 }
103
104 if (!$conference->canUserEdit(CurrentUser::get()->getId()))
105 {
106 return $updatingResult->addError(new Error(Loc::getMessage('IM_CONFERENCE_EDIT_ERROR_CANT_EDIT')));
107 }
108
109 if ($conference->getOwnerId() !== (int)$this->getCurrentUser()->getId())
110 {
111 return $updatingResult->addError(new Error(Loc::getMessage('IM_CONFERENCE_EDIT_ERROR_CANT_EDIT')));
112 }
113
114 return $conference->update($fields);
115 }
116
117 private function createConference(JsonPayload $payload, array $fields): Result
118 {
119 $addingResult = new Result();
120
121 // link was created before
122 if (isset($payload->getData()['aliasData']))
123 {
124 $fields['ALIAS_DATA'] = $payload->getData()['aliasData'];
125 }
126
127 return ConferenceClass::add($fields);
128 }
129
130 private static function recursiveWhiteList(array $fields, $whiteList, bool $readOnly = false): array
131 {
132 $data = [];
133 foreach ($fields as $field => $value)
134 {
135 if (is_array($value))
136 {
137 $data[$field] = self::recursiveWhiteList($value, $whiteList[$field], true);
138 }
139 elseif ((is_array($whiteList) && in_array($field, $whiteList)) || $readOnly)
140 {
141 $data[$field] = $value;
142 }
143 }
144
145 return $data;
146 }
147}
const ENTITY_TYPE_VIDEOCONF
Definition alias.php:13
static addUnique(array $fields)
Definition alias.php:55
static add(array $fields=[])
static getById(int $id)
createAction(JsonPayload $payload)
processBeforeAction(Action $action)
prepareAction(JsonPayload $payload)
static getInstance($userId=null)
Definition user.php:44
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29