1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
group.php
См. документацию.
1<?php
2
4
18
20{
21 const GROUP_COPY_ERROR = "GROUP_COPY_ERROR";
22
24
25 private $changedFields = [];
26
30 private $features = [];
31
32 private $projectTerm = [];
33
37 private $userGroupHelper = null;
38
40 {
41 parent::__construct();
42
43 $this->executiveUserId = $executiveUserId;
44 }
45
51 public function setFeature(Feature $feature)
52 {
53 $this->features[] = $feature;
54 }
55
61 public function setProjectTerm(array $projectTerm)
62 {
63 $this->projectTerm = $projectTerm;
64 }
65
66 public function setChangedFields($changedFields)
67 {
68 $this->changedFields = array_merge($this->changedFields, $changedFields);
69 }
70
76 public function setUserGroupHelper(UserGroupHelper $userGroupHelper)
77 {
78 $this->userGroupHelper = $userGroupHelper;
79 }
80
81 public function add(Container $container, array $fields)
82 {
83 $accessController = GroupAccessController::getInstance($this->executiveUserId);
84 $groupModel = GroupModel::createFromArray(['siteIds' => $fields['SITE_ID']]);
85
86 if (!$accessController->check(GroupDictionary::CREATE, $groupModel))
87 {
88 throw new AccessDeniedException();
89 }
90
91 $groupId = \CSocNetGroup::createGroup($this->executiveUserId, $fields, false);
92
93 if (!$groupId)
94 {
95 global $APPLICATION;
96
97 $this->result->addError(
98 new Error(
99 $APPLICATION->GetException()
100 ? $APPLICATION->GetException()->GetString()
101 : 'System error'
102 ,
103 self::GROUP_COPY_ERROR
104 )
105 );
106 }
107 else
108 {
109 \CSocNetFeatures::setFeature(SONET_ENTITY_GROUP, $groupId, "files", true, false);
110
111 if (isset($fields["OWNER_ID"]))
112 {
113 if (\CSocNetUserToGroup::setOwner($fields["OWNER_ID"], $groupId))
114 {
115 $dictionary = $container->getDictionary();
116
117 $dictionary["NEW_OWNER_ID"] = $fields["OWNER_ID"];
118
119 $container->setDictionary($dictionary);
120 }
121 }
122
123 if ($this->userGroupHelper)
124 {
125 $this->userGroupHelper->changeModerators($groupId);
126 }
127 }
128
129 return $groupId;
130 }
131
132 public function getFields(Container $container, $entityId)
133 {
134 $fields = [];
135
136 $queryObject = \CSocNetGroup::getList(
137 ["ID" => "DESC"], ["ID" => (int) $entityId], false, false, ["*"]);
138 while ($group = $queryObject->fetch())
139 {
140 if ($group["IMAGE_ID"] > 0)
141 {
142 $group["IMAGE_ID"] = \CFile::makeFileArray($group["IMAGE_ID"]);
143 }
144 if (!empty($group['NAME']))
145 {
146 $group['NAME'] = Emoji::decode($group['NAME']);
147 }
148 if (!empty($group['DESCRIPTION']))
149 {
150 $group['DESCRIPTION'] = Emoji::decode($group['DESCRIPTION']);
151 }
152
153 $fields["SITE_ID"] = $this->getSiteIds($group["ID"]);
154
155 $fields = $group;
156 }
157
158 return $fields;
159 }
160
161 public function prepareFieldsToCopy(Container $container, array $fields)
162 {
163 if (!empty($this->changedFields))
164 {
165 $fields = $this->changeFields($fields);
166 }
167
168 if ($fields["PROJECT"] == "Y" && $this->projectTerm)
169 {
170 if (!empty($this->projectTerm["start_point"]) && !empty($this->projectTerm["end_point"]))
171 {
172 $fields = $this->getFieldsProjectTerm($fields);
173 }
174 elseif (!empty($this->projectTerm["start_point"]))
175 {
176 $fields = $this->getRecountFieldsProjectTerm($fields, $this->projectTerm["start_point"]);
177 }
178 }
179
180 $fields = $this->prepareExtranetFields($fields);
181
182 unset($fields["ID"]);
183 unset($fields["DATE_CREATE"]);
184 unset($fields["DATE_UPDATE"]);
185 unset($fields["DATE_ACTIVITY"]);
186
187 return $fields;
188 }
189
198 public function copyChildren(Container $container, $groupId, $copiedGroupId)
199 {
200 $copiedGroupId = (int) $copiedGroupId;
201 if (!$copiedGroupId)
202 {
203 return new Result();
204 }
205
206 $this->copyUfFields($groupId, $copiedGroupId, "SONET_GROUP");
207
208 foreach ($this->features as $feature)
209 {
210 //todo Perhaps it’s worth making the parameters not in the array, but in the object.
211 // Until I made a decision, I do not want to write this to the interface.
212 if (method_exists($feature, "setProjectTerm"))
213 {
214 $feature->setProjectTerm($this->projectTerm);
215 }
216 $feature->copy($groupId, $copiedGroupId);
217 }
218
219 $this->copyFeatures($groupId, $copiedGroupId);
220
221 return $this->getResult();
222 }
223
224 private function changeFields(array $fields)
225 {
226 foreach ($this->changedFields as $fieldId => $fieldValue)
227 {
228 if (array_key_exists($fieldId, $fields))
229 {
230 $fields[$fieldId] = $fieldValue;
231 }
232 }
233
234 return $fields;
235 }
236
237 private function getFieldsProjectTerm($fields)
238 {
239 try
240 {
241 $projectTerm = [
242 "project" => true
243 ];
244
245 $startPoint = $this->projectTerm["start_point"];
246 $endPoint = $this->projectTerm["end_point"];
247
249
250 $newDateStart = new \DateTime($startPoint);
251 $fields["PROJECT_DATE_START"] = $newDateStart->format($phpDateFormat);
252
253 $newDateEnd = new \DateTime($endPoint);
254 $fields["PROJECT_DATE_FINISH"] = $newDateEnd->format($phpDateFormat);
255
256 $projectTerm["start_point"] = $fields["PROJECT_DATE_START"];
257 $projectTerm["end_point"] = $fields["PROJECT_DATE_FINISH"];
258
259 $this->setProjectTerm($projectTerm);
260 }
261 catch (\Exception $exception)
262 {
263 $fields["PROJECT_DATE_FINISH"] = "";
264 $this->result->addError(new Error($exception->getMessage()));
265 }
266
267 return $fields;
268 }
269
270 private function getRecountFieldsProjectTerm($fields, $startPoint)
271 {
272 try
273 {
274 $projectTerm = [
275 "project" => true,
276 "old_start_point" => $fields["PROJECT_DATE_START"]
277 ];
278
279 $oldDateStart = new \DateTime($fields["PROJECT_DATE_START"]);
280
281 $phpDateFormat = \Bitrix\Main\Type\DateTime::convertFormatToPhp(FORMAT_DATE);
282
283 $newDateStart = new \DateTime($startPoint);
284 $fields["PROJECT_DATE_START"] = $newDateStart->format($phpDateFormat);
285
286 if (!empty($fields["PROJECT_DATE_FINISH"]))
287 {
288 $dateFinish = new \DateTime($fields["PROJECT_DATE_FINISH"]);
289 $interval = new \DateInterval("PT".($dateFinish->getTimestamp()-$oldDateStart->getTimestamp())."S");
290 $newDateStart->add($interval);
291 $fields["PROJECT_DATE_FINISH"] = $newDateStart->format($phpDateFormat);
292 }
293
294 $projectTerm["start_point"] = $fields["PROJECT_DATE_START"];
295 $projectTerm["end_point"] = $fields["PROJECT_DATE_FINISH"];
296
297 $this->setProjectTerm($projectTerm);
298 }
299 catch (\Exception $exception)
300 {
301 $fields["PROJECT_DATE_FINISH"] = "";
302 $this->result->addError(new Error($exception->getMessage()));
303 }
304
305 return $fields;
306 }
307
308 private function prepareExtranetFields(array $fields): array
309 {
310 if (!Loader::includeModule('extranet'))
311 {
312 return $fields;
313 }
314
315 $isAllowCreateExtranetGroup = $this->isAllowCreateExtranetGroup();
316 $existsExtranetSiteId = $this->isExtranetSite($fields['SITE_ID']);
317
318 if ($isAllowCreateExtranetGroup)
319 {
320 $wantedExtranetGroup = $this->changedFields['IS_EXTRANET_GROUP'] === 'Y';
321
322 if ($wantedExtranetGroup && !$existsExtranetSiteId)
323 {
324 $fields['SITE_ID'][] = \CExtranet::getExtranetSiteID();
325 $fields['VISIBLE'] = 'N';
326 $fields['OPENED'] = 'N';
327 }
328 }
329 elseif ($existsExtranetSiteId)
330 {
331 $fields['SITE_ID'] = $this->getSiteIds($fields['ID']);
332
333 $extranetSiteId = \CExtranet::getExtranetSiteID();
334 $fields['SITE_ID'] = array_filter($fields['SITE_ID'], static fn($siteId) => $siteId !== $extranetSiteId);
335 }
336
337 if (empty($fields['SITE_ID']))
338 {
339 throw new \InvalidArgumentException('Site id cannot be empty');
340 }
341
342 return $fields;
343 }
344
345 private function isAllowCreateExtranetGroup(): bool
346 {
347 $isExtranetForGroupsEnabled = (bool)Option::get('socialnetwork', 'enable_extranet_for_groups', 0);
348
349 return Loader::includeModule('extranet') && $isExtranetForGroupsEnabled;
350 }
351
352 private function getSiteIds(int $groupId): array
353 {
354 $siteIds = [];
355
356 $queryObject = WorkgroupSiteTable::getList([
357 "filter" => [
358 "GROUP_ID" => $groupId
359 ],
360 "select" => ["SITE_ID"]
361 ]);
362 while ($workGroupSite = $queryObject->fetch())
363 {
364 $siteIds[] = $workGroupSite["SITE_ID"];
365 }
366 $siteIds = array_unique($siteIds);
367
368 return $siteIds;
369 }
370
371 private function isExtranetSite(array $siteIds): bool
372 {
373 foreach ($siteIds as $siteId)
374 {
375 if (\CExtranet::isExtranetSite($siteId))
376 {
377 return true;
378 }
379 }
380
381 return false;
382 }
383
384 private function copyFeatures(int $groupId, int $copiedGroupId): void
385 {
386 $featuresMapIds = [];
387
388 $queryObject = \CSocNetFeatures::getList(
389 [],
390 [
391 "ENTITY_ID" => $groupId,
392 "ENTITY_TYPE" => SONET_ENTITY_GROUP
393 ]
394 );
395 while ($feature = $queryObject->fetch())
396 {
397 $copiedFeatureId = \CSocNetFeatures::setFeature(
399 $copiedGroupId,
400 $feature["FEATURE"],
401 ($feature["ACTIVE"] == "Y"),
402 false
403 );
404
405 if (is_numeric($copiedFeatureId))
406 {
407 $featuresMapIds[$feature["ID"]] = $copiedFeatureId;
408 }
409 }
410
411 if ($featuresMapIds)
412 {
413 $this->copyFeaturesPerms($groupId, $featuresMapIds);
414 }
415 }
416
417 private function copyFeaturesPerms(int $groupId, array $featuresMapIds): void
418 {
419 $queryObject = \CSocNetFeaturesPerms::getList(
420 [],
421 [
422 'FEATURE_ENTITY_ID' => $groupId,
423 ],
424 );
425 while ($permFields = $queryObject->fetch())
426 {
427 if (array_key_exists($permFields['FEATURE_ID'], $featuresMapIds))
428 {
429 \CSocNetFeaturesPerms::setPerm(
430 $featuresMapIds[$permFields['FEATURE_ID']],
431 $permFields['OPERATION_ID'],
432 $permFields['ROLE']
433 );
434 }
435 }
436 }
437}
global $APPLICATION
Определения include.php:80
$accessController
Определения options.php:23
setDictionary(Dictionary $dictionary)
Определения container.php:80
getDictionary()
Определения container.php:89
copyUfFields(int $entityId, int $copiedEntityId, string $ufObject)
Определения copyimplementer.php:67
getResult(array $results=[])
Определения copyimplementer.php:119
Определения error.php:15
Определения loader.php:13
Определения emoji.php:10
static convertFormatToPhp($format)
Определения date.php:309
setFeature(Feature $feature)
Определения group.php:51
copyChildren(Container $container, $groupId, $copiedGroupId)
Определения group.php:198
getFields(Container $container, $entityId)
Определения group.php:132
setUserGroupHelper(UserGroupHelper $userGroupHelper)
Определения group.php:76
setProjectTerm(array $projectTerm)
Определения group.php:61
__construct($executiveUserId)
Определения group.php:39
add(Container $container, array $fields)
Определения group.php:81
setChangedFields($changedFields)
Определения group.php:66
prepareFieldsToCopy(Container $container, array $fields)
Определения group.php:161
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
const FORMAT_DATE
Определения include.php:63
$siteId
Определения ajax.php:8
trait Error
Определения error.php:11
$entityId
Определения payment.php:4
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
const SONET_ENTITY_GROUP
Определения include.php:117
$fields
Определения yandex_run.php:501