Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sharing.php
1<?php
2namespace Bitrix\Report;
3
6use CExtranet;
7use CPHPCache;
8use CSocNetLogDestination;
11
13{
14 const ERROR_SHARING_ADD = 'REPORT_SHARING_ADD_12000';
15 const ERROR_SHARING_UPDATE = 'REPORT_SHARING_UPDATE_12001';
16 const ERROR_SHARING_DELETE = 'REPORT_SHARING_DELETE_12002';
17
18 const CODE_USER = 'U';
19 const CODE_GROUP = 'G';
20 const CODE_SOCNET_GROUP = 'SG';
21 const CODE_DEPARTMENT = 'DR';
22
23 const TYPE_USER = 'users';
24 const TYPE_GROUP = 'groups';
25 const TYPE_SOCNET_GROUP = 'sonetgroups';
26 const TYPE_DEPARTMENT = 'department';
27
30
31 protected $reportId;
32
33 public function __construct($reportId)
34 {
35 $this->reportId = intval($reportId);
36 $this->errorCollection = new ErrorCollection;
37 }
38
43 public function changeSharing($listNewEntity)
44 {
45 $listSharingRow = $this->getByReportId();
46 if(!empty($listNewEntity) && is_array($listNewEntity))
47 {
48 $newEntityData = array();
49 foreach($listNewEntity as $entityId => $entityData)
50 {
51 switch($entityData['right'])
52 {
55 $newEntityData[$entityId]['ENTITY'] = $entityId;
56 $newEntityData[$entityId]['RIGHTS'] = $entityData['right'];
57 $newEntityData[$entityId]['REPORT_ID'] = $this->reportId;
58 break;
59 }
60 }
61
62 $overwriteSharing = $deleteSharing = $addSharing = array();
63 foreach($listSharingRow as $sharingRow)
64 {
65 if(array_key_exists($sharingRow['ENTITY'], $newEntityData))
66 {
67 if($newEntityData[$sharingRow['ENTITY']]['RIGHTS'] != $sharingRow['RIGHTS'])
68 {
69 $overwriteSharing[$sharingRow['ENTITY']] = $newEntityData[$sharingRow['ENTITY']];
70 $overwriteSharing[$sharingRow['ENTITY']]['ID'] = $sharingRow['ID'];
71 }
72 elseif($newEntityData[$sharingRow['ENTITY']]['RIGHTS'] == $sharingRow['RIGHTS'])
73 {
74 unset($newEntityData[$sharingRow['ENTITY']]);
75 }
76 }
77 else
78 {
79 $deleteSharing[$sharingRow['ENTITY']] = $sharingRow;
80 }
81 }
82 $addSharing = array_diff_key($newEntityData, $overwriteSharing);
83 if($addSharing)
84 $this->addToManyEntity($addSharing);
85 if($overwriteSharing)
86 $this->updateManyEntity($overwriteSharing);
87 if($deleteSharing)
88 $this->deleteManyEntity($deleteSharing);
89 }
90 else
91 {
92 $this->deleteManyEntity($listSharingRow);
93 }
94
95 $cache = new CPHPCache;
96 $cache->cleanDir('/report/sharing/'.$this->reportId);
97 }
98
102 public function getErrors()
103 {
104 return $this->errorCollection->toArray();
105 }
106
113 public static function getEntityOfSharing($reportId)
114 {
115 $reportId = intval($reportId);
116 $entitySharing = array();
117
118 $cacheTime = defined('BX_COMP_MANAGED_CACHE') ? 3153600 : 3600*4;
119 $cacheId = 'report-sharing-'.$reportId;
120 $cacheDir = '/report/sharing/'.$reportId;
121 $cache = new CPHPCache;
122 if($cache->initCache($cacheTime, $cacheId, $cacheDir))
123 {
124 $entitySharing = $cache->getVars();
125 }
126 else
127 {
128 $cache->startDataCache();
129 $result = SharingTable::getList(array(
130 'select' => array('ID', 'ENTITY', 'RIGHTS'),
131 'filter' => array('=REPORT_ID' => $reportId),
132 ));
133 while($data = $result->fetch())
134 {
135 $entitySharing[] = array(
136 'ENTITY' => $data['ENTITY'],
137 'RIGHTS' => $data['RIGHTS'],
138 );
139 }
140 $cache->endDataCache($entitySharing);
141 }
142
143 return $entitySharing;
144 }
145
151 public static function getUserData($userId)
152 {
153 $userData = array();
154
155 $userId = intval($userId);
156 $users = \CUser::getList('id', 'asc',
157 array('ID' => $userId),
158 array('FIELDS' => array('ID', 'PERSONAL_PHOTO', 'NAME', 'LAST_NAME'))
159 );
160 $user = $users->fetch();
161 if ($user)
162 {
163 $avatar = self::getImage($user['PERSONAL_PHOTO']);
164 $userData['name'] = \CUser::formatName(\CSite::getNameFormat(false), $user, false, false);
165 $userData['avatar'] = $avatar ? $avatar['src'] : '/bitrix/js/report/css/images/default_avatar.png';
166 $userData['link'] = self::getUserUrl($userId);
167 $userData['access'] = RightsManager::ACCESS_READ;
168 }
169
170 return $userData;
171 }
172
179 public static function parseEntityValue($entity)
180 {
181 preg_match(
182 '%(' . self::CODE_USER . '|' . self::CODE_SOCNET_GROUP . '|' .
183 self::CODE_DEPARTMENT . '){1,2}([0-9]+)%u',
184 $entity,
185 $m
186 );
187 list(, $code, $id) = $m;
188 if($code === null || $id === null)
189 {
190 return null;
191 }
192 switch($code)
193 {
194 case self::CODE_USER:
195 return array(self::TYPE_USER, $id);
196 case self::CODE_GROUP:
197 return array(self::TYPE_GROUP, $id);
199 return array(self::TYPE_SOCNET_GROUP, $id);
201 return array(self::TYPE_DEPARTMENT, $id);
202 }
203 return null;
204 }
205
213 public static function getTypeData($type, $id)
214 {
215 $typeData = array();
216
217 if($type == self::TYPE_USER)
218 {
219 $typeData = self::getUserData($id);
220 }
221 elseif($type == self::TYPE_GROUP)
222 {
223
224 }
225 elseif($type == self::TYPE_SOCNET_GROUP)
226 {
227 if(Loader::includeModule('socialnetwork'))
228 {
229 $query = \CSocNetGroup::getList(array(), array('ID' => $id),
230 false, false, array('IMAGE_ID', 'NAME'));
231 while($group = $query->fetch())
232 {
233 $typeData['name'] = $group['NAME'];
234 $avatar = self::getImage($group['IMAGE_ID']);
235 $typeData['avatar'] = $avatar?$avatar['src']:'/bitrix/js/report/css/images/default_groupe.png';
236 }
237 }
238 }
239 elseif($type == self::TYPE_DEPARTMENT)
240 {
241 if(Loader::includeModule('socialnetwork'))
242 {
243 $structure = \CSocNetLogDestination::getStucture();
244 if(!empty($structure['department']['DR' . $id]))
245 {
246 $typeData['name'] = $structure['department']['DR' . $id]['name'];
247 $typeData['avatar'] = '/bitrix/js/report/css/images/default_groupe.png';
248 }
249 }
250 }
251
252 return $typeData;
253 }
254
262 public static function getSocNetDestination($userId, $selected = array())
263 {
264 if(!Loader::includeModule('socialnetwork'))
265 {
266 return array();
267 }
268
269 global $CACHE_MANAGER;
270
271 if (!is_array($selected))
272 {
273 $selected = array();
274 }
275
276 if (method_exists('CSocNetLogDestination','getDestinationSort'))
277 {
278 $destination = array(
279 'LAST' => array()
280 );
281
282 $lastDestination = CSocNetLogDestination::getDestinationSort(array(
283 "DEST_CONTEXT" => "REPORT_SHARE"
284 ));
285
286 CSocNetLogDestination::fillLastDestination($lastDestination, $destination['LAST']);
287 }
288 else
289 {
290 $destination = array(
291 'LAST' => array(
292 'SONETGROUPS' => CSocNetLogDestination::getLastSocnetGroup(),
293 'DEPARTMENT' => CSocNetLogDestination::getLastDepartment(),
294 'USERS' => CSocNetLogDestination::getLastUser()
295 )
296 );
297 }
298
299 $cacheTtl = defined("BX_COMP_MANAGED_CACHE") ? 3153600 : 3600*4;
300 $cacheId = 'dest_group_'.$userId;
301 $cacheDir = '/report/dest/'.$userId;
302
303 $cache = new CPHPCache;
304 if($cache->initCache($cacheTtl, $cacheId, $cacheDir))
305 {
306 $destination['SONETGROUPS'] = $cache->getVars();
307 }
308 else
309 {
310 $cache->startDataCache();
311 $destination['SONETGROUPS'] = CSocNetLogDestination::getSocnetGroup(
312 array('GROUP_CLOSED' => 'N', 'features' => array("files", array("view"))));
313 if(defined("BX_COMP_MANAGED_CACHE"))
314 {
315 $CACHE_MANAGER->startTagCache($cacheDir);
316 $CACHE_MANAGER->registerTag("sonet_feature_all_G_files");
317 foreach($destination['SONETGROUPS'] as $val)
318 {
319 $CACHE_MANAGER->registerTag("sonet_features_G_".$val["entityId"]);
320 $CACHE_MANAGER->registerTag("sonet_group_".$val["entityId"]);
321 }
322 $CACHE_MANAGER->registerTag("sonet_user2group_U".$userId);
323 $CACHE_MANAGER->endTagCache();
324 }
325 $cache->endDataCache($destination['SONETGROUPS']);
326 }
327
328 $destUser = array();
329 $destination['SELECTED'] = array();
330 foreach ($selected as $ind => $code)
331 {
332 if (mb_substr($code, 0, 2) == 'DR')
333 {
334 $destination['SELECTED'][$code] = "department";
335 }
336 elseif (mb_substr($code, 0, 2) == 'UA')
337 {
338 $destination['SELECTED'][$code] = "groups";
339 }
340 elseif (mb_substr($code, 0, 2) == 'SG')
341 {
342 $destination['SELECTED'][$code] = "sonetgroups";
343 }
344 elseif (mb_substr($code, 0, 1) == 'U')
345 {
346 $destination['SELECTED'][$code] = "users";
347 $destUser[] = str_replace('U', '', $code);
348 }
349 }
350
351 // intranet structure
352 $structure = CSocNetLogDestination::getStucture();
353 $destination['DEPARTMENT'] = $structure['department'];
354 $destination['DEPARTMENT_RELATION'] = $structure['department_relation'];
355 $destination['DEPARTMENT_RELATION_HEAD'] = $structure['department_relation_head'];
356
357 if (Loader::includeModule('extranet') && !CExtranet::isIntranetUser())
358 {
359 $destination['EXTRANET_USER'] = 'Y';
360 $destination['USERS'] = CSocNetLogDestination::getExtranetUser();
361 }
362 else
363 {
364 if(is_array($destination['LAST']['USERS']))
365 {
366 foreach ($destination['LAST']['USERS'] as $value)
367 $destUser[] = str_replace('U', '', $value);
368 }
369
370 $destination['EXTRANET_USER'] = 'N';
371 $destination['USERS'] = CSocNetLogDestination::getUsers(array('id' => $destUser));
372 }
373
374 return $destination;
375 }
376
382 public static function onReportDelete($reportId)
383 {
384 $reportId = intval($reportId);
385 $result = SharingTable::getList(array(
386 'select' => array('ID'),
387 'filter'=>array('=REPORT_ID' => $reportId),
388 ));
389 while($data = $result->fetch())
390 SharingTable::delete($data['ID']);
391 }
392
393 protected function getByReportId()
394 {
395 $listSharingRow = array();
396 $result = SharingTable::getList(array(
397 'filter'=>array('=REPORT_ID' => $this->reportId),
398 ));
399 while($data = $result->fetch())
400 $listSharingRow[] = $data;
401
402 return $listSharingRow;
403 }
404
405 protected function addToManyEntity(array $listData)
406 {
407 foreach($listData as $data)
408 {
409 $fields = array(
410 'REPORT_ID' => $data['REPORT_ID'],
411 'ENTITY' => $data['ENTITY'],
412 'RIGHTS' => $data['RIGHTS'],
413 );
414 $result = SharingTable::add($fields);
415 if(!$result->isSuccess())
416 {
417 foreach($result->getErrorMessages() as $errorMessage)
418 $this->errorCollection->add(array(new Error($errorMessage, self::ERROR_SHARING_ADD)));
419 }
420 }
421 }
422
423 protected function updateManyEntity(array $listData)
424 {
425 foreach($listData as $data)
426 {
427 $fields = array(
428 'RIGHTS' => $data['RIGHTS']
429 );
430 $result = SharingTable::update($data['ID'], $fields);
431 if(!$result->isSuccess())
432 {
433 foreach($result->getErrorMessages() as $errorMessage)
434 $this->errorCollection->add(array(new Error($errorMessage, self::ERROR_SHARING_UPDATE)));
435 }
436 }
437 }
438
439 protected function deleteManyEntity(array $listData)
440 {
441 foreach($listData as $data)
442 {
443 $result = SharingTable::delete($data['ID']);
444 if(!$result->isSuccess())
445 {
446 foreach($result->getErrorMessages() as $errorMessage)
447 $this->errorCollection->add(array(new Error($errorMessage, self::ERROR_SHARING_DELETE)));
448 }
449 }
450 }
451
452 protected static function getUserUrl($userId)
453 {
454 $userPage = \COption::getOptionString('socialnetwork', 'user_page', false, SITE_ID);
455 if(!$userPage)
456 {
457 $userPage = SITE_DIR.'company/personal/';
458 }
459 return $userPage.'user/'.$userId.'/';
460 }
461
462 protected static function getImage($imageId)
463 {
464 return \CFile::resizeImageGet($imageId,
465 array('width' => 21, 'height' => 21), \BX_RESIZE_IMAGE_EXACT, false);
466 }
467}
static onReportDelete($reportId)
Definition sharing.php:382
addToManyEntity(array $listData)
Definition sharing.php:405
changeSharing($listNewEntity)
Definition sharing.php:43
static getUserData($userId)
Definition sharing.php:151
static getUserUrl($userId)
Definition sharing.php:452
__construct($reportId)
Definition sharing.php:33
static getSocNetDestination($userId, $selected=array())
Definition sharing.php:262
static getEntityOfSharing($reportId)
Definition sharing.php:113
static parseEntityValue($entity)
Definition sharing.php:179
deleteManyEntity(array $listData)
Definition sharing.php:439
updateManyEntity(array $listData)
Definition sharing.php:423
static getTypeData($type, $id)
Definition sharing.php:213
static getImage($imageId)
Definition sharing.php:462