Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
entity.php
1<?php
2
4
9
10class Entity
11{
12 const ENTITY_TYPE = 'default';
13 const MODULE_ID = 'forum';
14 const XML_ID_PREFIX = 'TOPIC_';
15
17 protected $entity;
19 protected $forum;
21 protected static $permissions = array();
23 private $editOwn = false;
24 protected static $pathToUser = '/company/personal/user/#user_id#/';
25 protected static $pathToGroup = '/workgroups/group/#group_id#/';
26
28 protected static $entities;
29
34 public function __construct(array $entity, array $storage)
35 {
36 $this->entity = array(
37 "type" => $entity["type"],
38 "id" => $entity["id"],
39 "xml_id" => $entity["xml_id"]
40 );
41 $this->forum = $storage;
42 $this->editOwn = (\COption::GetOptionString("forum", "USER_EDIT_OWN_POST", "Y") == "Y");
43 }
44
45 public function getId()
46 {
47 return $this->entity["id"];
48 }
49
50 public function getType()
51 {
52 return $this->entity["type"];
53 }
54
55 public function getXmlId()
56 {
57 if (!empty($this->entity["xml_id"]))
58 return $this->entity["xml_id"];
59 return mb_strtoupper($this->entity["type"]."_".$this->entity["id"]);
60 }
61
65 public function getFullId()
66 {
67 return $this->entity;
68 }
69
70 public static function className()
71 {
72 return get_called_class();
73 }
74
75 public static function getModule()
76 {
77 return static::MODULE_ID;
78 }
79
80 public static function getEntityType()
81 {
82 return static::ENTITY_TYPE;
83 }
84
85 public static function getXmlIdPrefix()
86 {
87 return static::XML_ID_PREFIX;
88 }
89
94 public function canRead($userId)
95 {
96 return $this->getPermission($userId) >= "E";
97 }
102 public function canAdd($userId)
103 {
104 return $this->getPermission($userId) >= "I";
105 }
106
111 public function canEdit($userId)
112 {
113 return $this->getPermission($userId) >= "U";
114 }
119 public function canEditOwn($userId)
120 {
121 return $this->canEdit($userId) || $this->getPermission($userId) >= "I" && $this->editOwn;
122 }
127 public function canModerate($userId)
128 {
129 return $this->getPermission($userId) >= "Q";
130 }
131
139 public function setPermission($userId, $permission)
140 {
141 if (is_string($permission))
142 {
143 if (!isset(self::$permissions[$userId]))
144 self::$permissions[$userId] = [];
145 self::$permissions[$userId][$this->forum["ID"]] = $permission;
146 }
147 return $this;
148 }
149
154 public function setEditOwn($permission)
155 {
156 $this->editOwn = $permission;
157 return $this;
158 }
159
164 public function getPermission($userId)
165 {
166 if (!array_key_exists($userId, self::$permissions))
167 {
168 self::$permissions[$userId] = [];
169 if (!array_key_exists($this->forum["ID"], self::$permissions[$userId]))
170 {
171 if (\CForumUser::IsAdmin($userId))
172 $result = "Y";
173 else if ($this->forum["ACTIVE"] != "Y")
174 $result = "A";
175 else if (\CForumUser::IsLocked($userId))
176 $result = \CForumNew::GetPermissionUserDefault($this->forum["ID"]);
177 else
178 {
179 if (in_array($this->getType(), array('PH', 'TR', 'TM', 'IBLOCK')))
180 {
181 $result = 'Y';
182 }
183 else
184 {
185 $res = ForumTable::getList(array(
186 'filter' => array(
187 '=ID' => $this->forum["ID"],
188 '@XML_ID' => array(
189 'USERS_AND_GROUPS'
190 )
191 ),
192 'select' => array('ID')
193 ));
194 if ($forumFields = $res->fetch())
195 {
196 $result = 'Y';
197 }
198 else
199 {
200 $result = \CForumNew::GetUserPermission($this->forum["ID"], $userId);
201 }
202 }
203 }
204
205 self::$permissions[$userId][$this->forum["ID"]] = $result;
206 }
207 }
208 return self::$permissions[$userId][$this->forum["ID"]];
209 }
214 public static function getEntityByType($type = "")
215 {
216 $type = mb_strtolower($type);
217 $entities = self::getEntities();
218 return (array_key_exists($type, $entities) ? $entities[$type] : null);
219 }
220
225 public static function getEntityByXmlId($xmlId = "")
226 {
227 $xmlId = mb_strtoupper($xmlId);
228 $entities = self::getEntities();
229 $result = null;
230 foreach ($entities as $entity)
231 {
232 if (preg_match("/^".$entity["xmlIdPrefix"]."(\\d+)/", $xmlId))
233 {
234 $result = $entity;
235 break;
236 }
237 }
238 return $result;
239 }
240
241 private static function getEntities()
242 {
243 if (!is_array(self::$entities))
244 {
245 self::$entities = array(
246 "tk" => array(
247 "entityType" => "tk",
248 "className" => TaskEntity::className(),
249 "moduleId" => "tasks",
250 "xmlIdPrefix" => TaskEntity::getXmlIdPrefix()),
251 "wf" => array(
252 "entityType" => "wf",
253 "className" => WorkflowEntity::className(),
254 "moduleId" => "lists",
255 "xmlIdPrefix" => WorkflowEntity::getXmlIdPrefix()),
256 "ev" => array(
257 "entityType" => "ev",
258 "className" => CalendarEntity::className(),
259 "moduleId" => "calendar",
260 "xmlIdPrefix" => CalendarEntity::getXmlIdPrefix()),
261 "tm" => array(
262 "entityType" => "tm",
263 "className" => Entity::className(),
264 "moduleId" => "timeman",
265 "xmlIdPrefix" => 'TIMEMAN_ENTRY_'
266 ),
267 "tr" => array(
268 "entityType" => "tr",
269 "className" => Entity::className(),
270 "moduleId" => "timeman",
271 "xmlIdPrefix" => 'TIMEMAN_REPORT_'
272 ),
273 "default" => array(
274 "entityType" => "default",
275 "className" => Entity::className(),
276 "moduleId" => "forum",
277 "xmlIdPrefix" => Entity::getXmlIdPrefix()
278 )
279 );
280
281 $event = new Event("forum", "onBuildAdditionalEntitiesList");
282 $event->send();
283
284 foreach ($event->getResults() as $evenResult)
285 {
286 $result = $evenResult->getParameters();
287 if (!is_array($result))
288 {
289 throw new SystemException('Event onBuildAdditionalEntitiesList: result must be an array.');
290 }
291
292 foreach ($result as $connector)
293 {
294 if (empty($connector['ENTITY_TYPE']))
295 {
296 throw new SystemException('Event onBuildAdditionalEntitiesList: key ENTITY_TYPE is not found.');
297 }
298
299 if (empty($connector['MODULE_ID']))
300 {
301 throw new SystemException('Event onBuildAdditionalEntitiesList: key MODULE_ID is not found.');
302 }
303
304 if (empty($connector['CLASS']))
305 {
306 throw new SystemException('Event onBuildAdditionalEntitiesList: key CLASS is not found.');
307 }
308
309 if (is_string($connector['CLASS']) && class_exists($connector['CLASS']))
310 {
311 self::$entities[mb_strtolower($connector['ENTITY_TYPE'])] = array(
312 "id" => mb_strtolower($connector['ENTITY_TYPE']),
313 "className" => str_replace('\\\\', '\\', $connector['CLASS']),
314 "moduleId" => $connector['MODULE_ID'],
315 "xmlIdPrefix" => mb_strtoupper($connector['ENTITY_TYPE'])."_"
316 );
317 }
318 }
319 }
320 }
321 return self::$entities;
322 }
330 public static function onMessageIsIndexed($id, array $message, array &$index)
331 {
332 return (empty($message["PARAM1"]) && empty($message["PARAM2"]));
333 }
334}
__construct(array $entity, array $storage)
Definition entity.php:34
static getEntityByType($type="")
Definition entity.php:214
static onMessageIsIndexed($id, array $message, array &$index)
Definition entity.php:330
static getEntityByXmlId($xmlId="")
Definition entity.php:225
setPermission($userId, $permission)
Definition entity.php:139