Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
group.php
1<?php
2
4
13
15{
16 const GROUP_COPY_ERROR = "GROUP_COPY_ERROR";
17
19
20 private $changedFields = [];
21
25 private $features = [];
26
27 private $projectTerm = [];
28
32 private $userGroupHelper = null;
33
35 {
36 parent::__construct();
37
38 $this->executiveUserId = $executiveUserId;
39 }
40
46 public function setFeature(Feature $feature)
47 {
48 $this->features[] = $feature;
49 }
50
56 public function setProjectTerm(array $projectTerm)
57 {
58 $this->projectTerm = $projectTerm;
59 }
60
61 public function setChangedFields($changedFields)
62 {
63 $this->changedFields = array_merge($this->changedFields, $changedFields);
64 }
65
71 public function setUserGroupHelper(UserGroupHelper $userGroupHelper)
72 {
73 $this->userGroupHelper = $userGroupHelper;
74 }
75
76 public function add(Container $container, array $fields)
77 {
78 $groupId = \CSocNetGroup::createGroup($this->executiveUserId, $fields, false);
79
80 if (!$groupId)
81 {
82 global $APPLICATION;
83
84 $this->result->addError(
85 new Error(
86 $APPLICATION->GetException()
87 ? $APPLICATION->GetException()->GetString()
88 : 'System error'
89 ,
91 )
92 );
93 }
94 else
95 {
96 \CSocNetFeatures::setFeature(SONET_ENTITY_GROUP, $groupId, "files", true, false);
97
98 if (isset($fields["OWNER_ID"]))
99 {
100 if (\CSocNetUserToGroup::setOwner($fields["OWNER_ID"], $groupId))
101 {
102 $dictionary = $container->getDictionary();
103
104 $dictionary["NEW_OWNER_ID"] = $fields["OWNER_ID"];
105
106 $container->setDictionary($dictionary);
107 }
108 }
109
110 if ($this->userGroupHelper)
111 {
112 $this->userGroupHelper->changeModerators($groupId);
113 }
114 }
115
116 return $groupId;
117 }
118
119 public function getFields(Container $container, $entityId)
120 {
121 $fields = [];
122
123 $queryObject = \CSocNetGroup::getList(
124 ["ID" => "DESC"], ["ID" => (int) $entityId], false, false, ["*"]);
125 while ($group = $queryObject->fetch())
126 {
127 if ($group["IMAGE_ID"] > 0)
128 {
129 $group["IMAGE_ID"] = \CFile::makeFileArray($group["IMAGE_ID"]);
130 }
131 if (!empty($group['NAME']))
132 {
133 $group['NAME'] = Emoji::decode($group['NAME']);
134 }
135 if (!empty($group['DESCRIPTION']))
136 {
137 $group['DESCRIPTION'] = Emoji::decode($group['DESCRIPTION']);
138 }
139
140 $fields["SITE_ID"] = $this->getSiteIds($group["ID"]);
141
142 $fields = $group;
143 }
144
145 return $fields;
146 }
147
148 public function prepareFieldsToCopy(Container $container, array $fields)
149 {
150 if (!empty($this->changedFields))
151 {
152 $fields = $this->changeFields($fields);
153 }
154
155 if ($fields["PROJECT"] == "Y" && $this->projectTerm)
156 {
157 if (!empty($this->projectTerm["start_point"]) && !empty($this->projectTerm["end_point"]))
158 {
159 $fields = $this->getFieldsProjectTerm($fields);
160 }
161 elseif (!empty($this->projectTerm["start_point"]))
162 {
163 $fields = $this->getRecountFieldsProjectTerm($fields, $this->projectTerm["start_point"]);
164 }
165 }
166
167 $fields = $this->prepareExtranetFields($fields);
168
169 unset($fields["ID"]);
170 unset($fields["DATE_CREATE"]);
171 unset($fields["DATE_UPDATE"]);
172 unset($fields["DATE_ACTIVITY"]);
173
174 return $fields;
175 }
176
185 public function copyChildren(Container $container, $groupId, $copiedGroupId)
186 {
187 $copiedGroupId = (int) $copiedGroupId;
188 if (!$copiedGroupId)
189 {
190 return new Result();
191 }
192
193 $this->copyUfFields($groupId, $copiedGroupId, "SONET_GROUP");
194
195 foreach ($this->features as $feature)
196 {
197 //todo Perhaps its worth making the parameters not in the array, but in the object.
198 // Until I made a decision, I do not want to write this to the interface.
199 if (method_exists($feature, "setProjectTerm"))
200 {
201 $feature->setProjectTerm($this->projectTerm);
202 }
203 $feature->copy($groupId, $copiedGroupId);
204 }
205
206 $this->copyFeatures($groupId, $copiedGroupId);
207
208 return $this->getResult();
209 }
210
211 private function changeFields(array $fields)
212 {
213 foreach ($this->changedFields as $fieldId => $fieldValue)
214 {
215 if (array_key_exists($fieldId, $fields))
216 {
217 $fields[$fieldId] = $fieldValue;
218 }
219 }
220
221 return $fields;
222 }
223
224 private function getFieldsProjectTerm($fields)
225 {
226 try
227 {
228 $projectTerm = [
229 "project" => true
230 ];
231
232 $startPoint = $this->projectTerm["start_point"];
233 $endPoint = $this->projectTerm["end_point"];
234
235 $phpDateFormat = \Bitrix\Main\Type\DateTime::convertFormatToPhp(FORMAT_DATE);
236
237 $newDateStart = new \DateTime($startPoint);
238 $fields["PROJECT_DATE_START"] = $newDateStart->format($phpDateFormat);
239
240 $newDateEnd = new \DateTime($endPoint);
241 $fields["PROJECT_DATE_FINISH"] = $newDateEnd->format($phpDateFormat);
242
243 $projectTerm["start_point"] = $fields["PROJECT_DATE_START"];
244 $projectTerm["end_point"] = $fields["PROJECT_DATE_FINISH"];
245
246 $this->setProjectTerm($projectTerm);
247 }
248 catch (\Exception $exception)
249 {
250 $fields["PROJECT_DATE_FINISH"] = "";
251 $this->result->addError(new Error($exception->getMessage()));
252 }
253
254 return $fields;
255 }
256
257 private function getRecountFieldsProjectTerm($fields, $startPoint)
258 {
259 try
260 {
261 $projectTerm = [
262 "project" => true,
263 "old_start_point" => $fields["PROJECT_DATE_START"]
264 ];
265
266 $oldDateStart = new \DateTime($fields["PROJECT_DATE_START"]);
267
268 $phpDateFormat = \Bitrix\Main\Type\DateTime::convertFormatToPhp(FORMAT_DATE);
269
270 $newDateStart = new \DateTime($startPoint);
271 $fields["PROJECT_DATE_START"] = $newDateStart->format($phpDateFormat);
272
273 if (!empty($fields["PROJECT_DATE_FINISH"]))
274 {
275 $dateFinish = new \DateTime($fields["PROJECT_DATE_FINISH"]);
276 $interval = new \DateInterval("PT".($dateFinish->getTimestamp()-$oldDateStart->getTimestamp())."S");
277 $newDateStart->add($interval);
278 $fields["PROJECT_DATE_FINISH"] = $newDateStart->format($phpDateFormat);
279 }
280
281 $projectTerm["start_point"] = $fields["PROJECT_DATE_START"];
282 $projectTerm["end_point"] = $fields["PROJECT_DATE_FINISH"];
283
284 $this->setProjectTerm($projectTerm);
285 }
286 catch (\Exception $exception)
287 {
288 $fields["PROJECT_DATE_FINISH"] = "";
289 $this->result->addError(new Error($exception->getMessage()));
290 }
291
292 return $fields;
293 }
294
295 private function prepareExtranetFields(array $fields)
296 {
297 if (!Loader::includeModule("extranet") || !$this->isExtranetSite($fields["SITE_ID"]))
298 {
299 if (
300 Loader::includeModule("extranet") &&
301 !$this->isExtranetSite($fields["SITE_ID"]) &&
302 $this->changedFields["IS_EXTRANET_GROUP"] == "Y"
303 )
304 {
305 $fields["SITE_ID"][] = \CExtranet::getExtranetSiteID();
306 $fields["VISIBLE"] = "N";
307 $fields["OPENED"] = "N";
308 }
309 }
310 elseif (Loader::includeModule("extranet") && $this->isExtranetSite($fields["SITE_ID"]))
311 {
312 $fields["SITE_ID"] = $this->getSiteIds($fields["ID"]);
313 }
314
315 return $fields;
316 }
317
318 private function getSiteIds(int $groupId): array
319 {
320 $siteIds = [];
321
322 $queryObject = WorkgroupSiteTable::getList([
323 "filter" => [
324 "GROUP_ID" => $groupId
325 ],
326 "select" => ["SITE_ID"]
327 ]);
328 while ($workGroupSite = $queryObject->fetch())
329 {
330 $siteIds[] = $workGroupSite["SITE_ID"];
331 }
332 $siteIds = array_unique($siteIds);
333
334 return $siteIds;
335 }
336
337 private function isExtranetSite(array $siteIds): bool
338 {
339 foreach ($siteIds as $siteId)
340 {
341 if (\CExtranet::isExtranetSite($siteId))
342 {
343 return true;
344 }
345 }
346
347 return false;
348 }
349
350 private function copyFeatures(int $groupId, int $copiedGroupId): void
351 {
352 $featuresMapIds = [];
353
354 $queryObject = \CSocNetFeatures::getList(
355 [],
356 [
357 "ENTITY_ID" => $groupId,
358 "ENTITY_TYPE" => SONET_ENTITY_GROUP
359 ]
360 );
361 while ($feature = $queryObject->fetch())
362 {
363 $copiedFeatureId = \CSocNetFeatures::setFeature(
364 SONET_ENTITY_GROUP,
365 $copiedGroupId,
366 $feature["FEATURE"],
367 ($feature["ACTIVE"] == "Y"),
368 false
369 );
370
371 if (is_numeric($copiedFeatureId))
372 {
373 $featuresMapIds[$feature["ID"]] = $copiedFeatureId;
374 }
375 }
376
377 if ($featuresMapIds)
378 {
379 $this->copyFeaturesPerms($groupId, $featuresMapIds);
380 }
381 }
382
383 private function copyFeaturesPerms(int $groupId, array $featuresMapIds): void
384 {
385 $queryObject = \CSocNetFeaturesPerms::getList(
386 [],
387 [
388 'FEATURE_ENTITY_ID' => $groupId,
389 ],
390 );
391 while ($permFields = $queryObject->fetch())
392 {
393 if (array_key_exists($permFields['FEATURE_ID'], $featuresMapIds))
394 {
395 \CSocNetFeaturesPerms::setPerm(
396 $featuresMapIds[$permFields['FEATURE_ID']],
397 $permFields['OPERATION_ID'],
398 $permFields['ROLE']
399 );
400 }
401 }
402 }
403}
setDictionary(Dictionary $dictionary)
Definition container.php:80
copyUfFields(int $entityId, int $copiedEntityId, string $ufObject)
static decode($text)
Definition emoji.php:24
copyChildren(Container $container, $groupId, $copiedGroupId)
Definition group.php:185
getFields(Container $container, $entityId)
Definition group.php:119
setUserGroupHelper(UserGroupHelper $userGroupHelper)
Definition group.php:71
add(Container $container, array $fields)
Definition group.php:76
prepareFieldsToCopy(Container $container, array $fields)
Definition group.php:148