Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
baseobject.php
1<?php
2
4
8Loc::loadMessages(__FILE__);
10{
12 protected $id;
15
16 protected static $userGroups = [];
17
18 protected static $objectStorage = [];
19
20 public function __construct($id)
21 {
22 $this->id = $id;
23 $this->errorCollection = new ErrorCollection;
24 $this->init();
25 }
30 public function init()
31 { }
35 public function getId()
36 {
37 return $this->id;
38 }
39
43 public function getErrors()
44 {
45 return $this->errorCollection->toArray();
46 }
47
54 public function getErrorByCode($code)
55 {
56 return $this->errorCollection->getErrorByCode($code);
57 }
62 public function canRead($userId)
63 {
64 if ($userId == $this->getUser()->getId())
65 {
66 $right = \CMain::getUserRight("vote");
67 }
68 else
69 {
70 $groups = self::loadUserGroups($userId);
71 $right = \CMain::getUserRight("vote", $groups);
72 }
73 return ($right >= "R");
74 }
75
80 public function canEdit($userId)
81 {
82 if ($userId == $this->getUser()->getId())
83 {
84 $right = \CMain::getUserRight("vote");
85 }
86 else
87 {
88 $groups = self::loadUserGroups($userId);
89 $right = \CMain::getUserRight("vote", $groups);
90 }
91 return ($right >= "W");
92 }
93
97 public function getUser()
98 {
99 global $USER;
100 return $USER;
101 }
102
106 public function getApplication()
107 {
108 global $APPLICATION;
109 return $APPLICATION;
110 }
111
116 public static function loadUserGroups($userId)
117 {
118 /* @global \CUser $USER */
119 global $USER;
120 if (!array_key_exists($userId, self::$userGroups))
121 {
122 if ($USER->getId() == $userId)
123 {
124 $groups = $USER->getUserGroupArray();
125 }
126 else
127 {
128 $groups = \Bitrix\Main\UserTable::getUserGroupIds($userId);
129 if (empty($groups))
130 $groups = array(2);
131 }
132 self::$userGroups[$userId] = $groups;
133 }
134 return self::$userGroups[$userId];
135 }
141 public static function loadFromId($id, $shouldBeNewIfIdIsNull = false)
142 {
143
144 if (empty(self::$objectStorage))
145 {
146 register_shutdown_function([__CLASS__, "shutdown"]);
147 }
148 $obj = null;
149 $c = get_called_class();
150 $innerId = $id = intval($id);
151 if (!array_key_exists($c, self::$objectStorage))
152 {
153 self::$objectStorage[$c] = ["maxId" => 0, "data" => []];
154 }
155 if ($shouldBeNewIfIdIsNull === true && $id <= 0)
156 {
157 $innerId = implode("", ["n", self::$objectStorage[$c]["maxId"]++]);
158 }
159 if (array_key_exists($innerId, self::$objectStorage[$c]["data"]))
160 {
161 $obj = self::$objectStorage[$c]["data"][$innerId];
162 }
163 if ($obj === null)
164 {
165 $obj = new $c($id);
166 self::$objectStorage[$c]["data"][$innerId] = $obj;
167 }
168 return $obj;
169 }
170
174 public static function shutdown()
175 {
176 $res = [];
177 foreach (self::$objectStorage as $c => $r)
178 {
179 $res[$c] = array_keys($r["data"]);
180 }
181 }
182}
static loadMessages($file)
Definition loc.php:64
static loadUserGroups($userId)
static loadFromId($id, $shouldBeNewIfIdIsNull=false)