Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
consent.php
1<?php
9
14
15Loc::loadLanguageFile(__FILE__);
16
22{
23 const EVENT_NAME_LIST = 'OnUserConsentProviderList';
24
34 public static function addByContext($agreementId, $originatorId = null, $originId = null, array $params = array())
35 {
36 $agreement = new Agreement($agreementId);
37 if (!$agreement->isExist() || !$agreement->isActive())
38 {
39 return null;
40 }
41
42 $request = Context::getCurrent()->getRequest();
43 $parameters = array(
44 'AGREEMENT_ID' => $agreementId
45 );
46
47 if (isset($params['USER_ID']) && intval($params['USER_ID']) > 0)
48 {
49 $parameters['USER_ID'] = intval($params['USER_ID']);
50 }
51 else if (isset($GLOBALS['USER']) && is_object($GLOBALS['USER']) && $GLOBALS['USER']->GetID())
52 {
53 $parameters['USER_ID'] = $GLOBALS['USER']->GetID();
54 }
55
56 $parameters['IP'] = (isset($params['IP']) && $params['IP']) ? $params['IP'] : $request->getRemoteAddress();
57 if (isset($params['URL']) && $params['URL'])
58 {
59 $parameters['URL'] = $params['URL'];
60 }
61 else
62 {
63 $parameters['URL'] = ($request->isHttps() ? "https" : "http")."://".$request->getHttpHost() . $request->getRequestUri();
64 }
65
66 if (mb_strlen($parameters['URL']) > 4000)
67 {
68 $parameters['URL'] = mb_substr($parameters['URL'], 0, 4000);
69 }
70
71 if ($originatorId && $originId)
72 {
73 $parameters['ORIGINATOR_ID'] = $originatorId;
74 $parameters['ORIGIN_ID'] = $originId;
75 }
76 $addResult = Internals\ConsentTable::add($parameters);
77 if ($addResult->isSuccess())
78 {
79 $userConsentId = $addResult->getId();
80
81 if (isset($params['ITEMS']) && is_array($params['ITEMS']))
82 {
83 Internals\UserConsentItemTable::addItems($userConsentId, $params['ITEMS']);
84 }
85
86 return $userConsentId;
87 }
88 else
89 {
90 return null;
91 }
92 }
93
103 public static function getByContext($agreementId, $originatorId = null, $originId = null, $params = Array())
104 {
105 $agreement = new Agreement($agreementId);
106 if (!$agreement->isExist() || !$agreement->isActive())
107 {
108 return null;
109 }
110
111 $filter = array(
112 '=AGREEMENT_ID' => $agreementId
113 );
114
115 if (isset($params['USER_ID']) && intval($params['USER_ID']) > 0)
116 {
117 $filter['=USER_ID'] = intval($params['USER_ID']);
118 }
119 else if (isset($GLOBALS['USER']) && is_object($GLOBALS['USER']) && $GLOBALS['USER']->GetID())
120 {
121 $filter['=USER_ID'] = $GLOBALS['USER']->GetID();
122 }
123
124 if ($originatorId && $originId)
125 {
126 $filter['=ORIGINATOR_ID'] = $originatorId;
127 $filter['=ORIGIN_ID'] = $originId;
128 }
129
130 $addResult = Internals\ConsentTable::getList(Array(
131 'filter' => $filter
132 ))->fetch();
133
134 return $addResult?: null;
135 }
136
144 public static function getOriginData($originatorId, $originId = null)
145 {
146 $list = self::getList();
147 foreach ($list as $provider)
148 {
149 if ($provider['CODE'] != $originatorId)
150 {
151 continue;
152 }
153 $name = null;
154 $url = null;
155 if ($originId)
156 {
157 $data = $provider['DATA']($originId);
158 if (!is_array($data))
159 {
160 return null;
161 }
162
163 if (isset($data['NAME']))
164 {
165 $name = $data['NAME'];
166 }
167 else
168 {
169 return null;
170 }
171
172 if (isset($data['URL']))
173 {
174 $url = $data['URL'];
175 }
176 }
177 else
178 {
179 if (isset($provider['NAME']))
180 {
181 $name = $provider['NAME'];
182 }
183 else
184 {
185 return null;
186 }
187 }
188
189 return array(
190 'NAME' => $name,
191 'URL' => $url
192 );
193 }
194
195 return null;
196 }
197
205 public static function getItems($originatorId, $items = [])
206 {
207 $list = self::getList();
208 foreach ($list as $provider)
209 {
210 if ($provider['CODE'] != $originatorId)
211 {
212 continue;
213 }
214
215 $values = [];
216
217 if ($items)
218 {
219 foreach ($items as $item)
220 {
221 if (!isset($item['VALUE']))
222 {
223 return null;
224 }
225 $values[] = $provider['ITEMS']($item['VALUE']);
226 }
227 }
228 else
229 {
230 return null;
231 }
232
233 return implode(', ', $values);
234 }
235
236 return null;
237 }
238
244 protected static function getList()
245 {
246 $data = array();
247 $event = new Event('main', self::EVENT_NAME_LIST, array($data));
248 $event->send();
249
250 static $list = null;
251 if ($list !== null)
252 {
253 return $list;
254 }
255
256 $list = array();
257 foreach ($event->getResults() as $eventResult)
258 {
259 if ($eventResult->getType() == EventResult::ERROR)
260 {
261 continue;
262 }
263
264 $params = $eventResult->getParameters();
265 if(!$params || !is_array($params))
266 {
267 continue;
268 }
269
270 foreach ($params as $item)
271 {
272 if (!is_array($item) || !isset($item['CODE']) || !isset($item['NAME']))
273 {
274 continue;
275 }
276
277 if (!isset($item['DATA']) || !is_callable($item['DATA']))
278 {
279 continue;
280 }
281
282 $list[] = $item;
283 }
284 }
285
286 return $list;
287 }
288}
static getCurrent()
Definition context.php:241
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
$GLOBALS['____1444769544']
Definition license.php:1