Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
voteusertype.php
1<?php
2
3namespace Bitrix\Vote\Uf;
4
8
9Loc::loadMessages(__FILE__);
10
11final class VoteUserType
12{
13 const TYPE_NEW_ATTACH = 'NewAttach';
14 const TYPE_SAVED_ATTACH = 'SavedAttach';
15 const NEW_VOTE_PREFIX = 'n';
16
20 public static function getUserTypeDescription()
21 {
22 AddEventHandler("main", "OnBeforeUserTypeUpdate", array(__CLASS__, "checkSettings"));
23 AddEventHandler("main", "OnBeforeUserTypeAdd", array(__CLASS__, "checkSettings"));
24 if (IsModuleInstalled("blog"))
25 {
26 AddEventHandler("blog", "OnBeforePostUserFieldUpdate", array(__CLASS__, "onBeforePostUserFieldUpdate"));
27 }
28
29 return array(
30 "USER_TYPE_ID" => "vote",
31 "CLASS_NAME" => __CLASS__,
32 "DESCRIPTION" => Loc::getMessage("V_USER_TYPE_DESCRIPTION"),
33 "BASE_TYPE" => "int",
34 );
35 }
36
41 public static function getDBColumnType($userField)
42 {
43 $connection = \Bitrix\Main\Application::getConnection();
44 $helper = $connection->getSqlHelper();
45 return $helper->getColumnTypeByField(new \Bitrix\Main\ORM\Fields\IntegerField('x'));
46 }
47
58 public static function onBeforePostUserFieldUpdate($entityId, $ID, $fields)
59 {
60 global $USER_FIELD_MANAGER;
61 $userFields = $USER_FIELD_MANAGER->GetUserFields($entityId, $ID, LANGUAGE_ID);
62 if (is_array($userFields) && !empty($userFields))
63 {
64 $userFields = array_intersect_key($userFields, $fields);
65 $path = str_replace("#post_id#", $ID, $fields["PATH"] ?? '');
66 $userField = reset($userFields);
67 do {
68 if (
69 is_array($userField)
70 && is_array($userField["USER_TYPE"])
71 && $userField["USER_TYPE"]["USER_TYPE_ID"] == "vote" &&
72 $userField["USER_TYPE"]["CLASS_NAME"] == __CLASS__ &&
73 isset($GLOBALS[__CLASS__.$userField["ENTITY_VALUE_ID"]]))
74 {
75 $GLOBALS[__CLASS__.$userField["ENTITY_VALUE_ID"]]["VOTE"]["URL"] = $path;
76 }
77 } while ($userField = next($userFields));
78 }
79 }
80
125 public static function prepareSettings($userField)
126 {
127 $userField["SETTINGS"] = (is_array($userField["SETTINGS"]) ? $userField["SETTINGS"] : @unserialize($userField["SETTINGS"], ["allowed_classes" => false]));
128 $userField["SETTINGS"] = (is_array($userField["SETTINGS"]) ? $userField["SETTINGS"] : array());
129 $tmp = array("CHANNEL_ID" => intval($userField["SETTINGS"]["CHANNEL_ID"]));
130
131 if ($userField["SETTINGS"]["CHANNEL_ID"] == "add")
132 {
133 $tmp["CHANNEL_TITLE"] = trim($userField["SETTINGS"]["CHANNEL_TITLE"]);
134 $tmp["CHANNEL_SYMBOLIC_NAME"] = trim($userField["SETTINGS"]["CHANNEL_SYMBOLIC_NAME"]);
135 $tmp["CHANNEL_USE_CAPTCHA"] = ($userField["SETTINGS"]["CHANNEL_USE_CAPTCHA"] == "Y" ? "Y" : "N");
136 }
137
138 $uniqType = $userField["SETTINGS"]["UNIQUE"];
139 if (is_array($userField["SETTINGS"]["UNIQUE"]))
140 {
141 $uniqType = 0;
142 foreach ($userField["SETTINGS"]["UNIQUE"] as $z)
143 $uniqType |= $z;
144 }
145
146 $tmp["UNIQUE"] = $uniqType;
147 $tmp["UNIQUE_IP_DELAY"] = is_array($userField["SETTINGS"]["UNIQUE_IP_DELAY"]) ?
148 $userField["SETTINGS"]["UNIQUE_IP_DELAY"] : array();
149 $tmp["NOTIFY"] = (in_array($userField["SETTINGS"]["NOTIFY"], array("I", "Y", "N")) ?
150 $userField["SETTINGS"]["NOTIFY"] : "N");
151
152 return $tmp;
153 }
154
160 public static function checkSettings(&$params)
161 {
162 $settings = (is_array($params["SETTINGS"]) ? $params["SETTINGS"] : @unserialize($params["SETTINGS"], ["allowed_classes" => false]));
163 $settings = is_array($settings) ? $settings : array($settings);
164 if (array_key_exists("CHANNEL_ID", $settings))
165 {
166 $settings["CHANNEL_ID"] = intval($settings["CHANNEL_ID"]);
167 if ($settings["CHANNEL_ID"] <= 0 && \Bitrix\Main\Loader::includeModule("vote"))
168 {
169 $dbRes = \CVoteChannel::GetList('', '',
170 array("SYMBOLIC_NAME" => $settings["CHANNEL_SYMBOLIC_NAME"], "SYMBOLIC_NAME_EXACT_MATCH" => "Y"));
171 if (!($dbRes && ($channel = $dbRes->fetch()) && !!$channel))
172 {
173 $res = array(
174 "TITLE" => $settings["CHANNEL_TITLE"],
175 "SYMBOLIC_NAME" => $settings["CHANNEL_SYMBOLIC_NAME"],
176 "ACTIVE" => "Y",
177 "HIDDEN" => "Y",
178 "C_SORT" => 100,
179 "VOTE_SINGLE" => "N",
180 "USE_CAPTCHA" => $settings["CHANNEL_USE_CAPTCHA"],
181 "SITE" => array(),
182 "GROUP_ID" => array()
183 );
184 $dbRes = \CSite::GetList();
185 while ($site = $dbRes->getNext())
186 $res["SITE"][] = $site["ID"];
187 $dbRes = \CGroup::GetList("sort", "asc", Array("ADMIN" => "N"));
188 while ($group = $dbRes->getNext())
189 $res["GROUP_ID"][$group["ID"]] = ($group["ID"] == 2 ? 1 : 4);
190 $res["GROUP_ID"] = (is_array($settings["GROUP_ID"]) ? array_intersect_key($settings["GROUP_ID"], $res["GROUP_ID"]) : $res["GROUP_ID"]);
191 $channelId = \CVoteChannel::Add($res);
192 }
193 else
194 {
195 $channelId = $channel["ID"];
196 }
197
198 $settings["CHANNEL_ID"] = $channelId;
199 unset($settings["CHANNEL_TITLE"]);
200 unset($settings["CHANNEL_SYMBOLIC_NAME"]);
201 unset($settings["CHANNEL_USE_CAPTCHA"]);
202 if (!$settings["CHANNEL_ID"])
203 return false;
204 }
205 $uniqType = $settings["UNIQUE"];
206 if (is_array($settings["UNIQUE"]))
207 {
208 foreach ( $settings["UNIQUE"] as $res)
209 $uniqType |= $res;
210 }
211
212 $settings["UNIQUE"] = $uniqType;
213 $settings["UNIQUE_IP_DELAY"] = is_array($settings["UNIQUE_IP_DELAY"]) ?
214 $settings["UNIQUE_IP_DELAY"] : array("DELAY" => "10", "DELAY_TYPE" => "D");
215 $params["SETTINGS"] = serialize($settings);
216 $params["MULTIPLE"] = "N";
217 $params["MANDATORY"] = "N";
218 $params["SHOW_FILTER"] = "N";
219 $params["IS_SEARCHABLE"] = "N";
220 }
221 return true;
222 }
230 public static function getSettingsHTML($userField, $htmlControl, $varsFromForm)
231 {
232 if (!\Bitrix\Main\Loader::includeModule("vote"))
233 return '';
234 global ${$htmlControl["NAME"]}, $aVotePermissions;
235 $entity = ${$htmlControl["NAME"]};
236 $value = "";
237 if($varsFromForm)
238 $value = $entity["CHANNEL_ID"];
239 elseif(is_array($userField))
240 {
241 $value = $userField["SETTINGS"]["CHANNEL_ID"];
242 $entity["NOTIFY"] = $userField["SETTINGS"]["NOTIFY"];
243 }
244 $value = (!empty($value) ? intval($value) : "add");
245 $dbRes = \CVoteChannel::GetList("", "", array("ACTIVE" => "Y"));
246 $voteChannels = array("reference" => array(Loc::getMessage("V_NEW_CHANNEL")), "reference_id" => array("add"));
247 if ($dbRes && $res = $dbRes->fetch())
248 {
249 do
250 {
251 $voteChannels["reference"][] = $res["TITLE"];
252 $voteChannels["reference_id"][] = $res["ID"];
253 } while ($res = $dbRes->fetch());
254 }
255
256 ob_start();
257 ?>
258 <tr>
259 <td><?=Loc::getMessage("V_CHANNEL_ID_COLON")?></td>
260 <td><?=str_replace(
261 "<select",
262 "<select onchange='if(this.value!=\"add\"){BX.hide(BX(\"channel_create\"));BX.show(this.nextSibling);}".
263 "else{BX(\"channel_create\").style.display=\"\";BX.hide(this.nextSibling);}' ",
264 SelectBoxFromArray(
265 $htmlControl["NAME"]."[CHANNEL_ID]",
266 $voteChannels,
267 $value)
268 )?><a style="margin-left: 1em;" href="" rel="/bitrix/admin/vote_channel_edit.php?ID=#id#" <?
269 ?>onmousedown="this.href=this.rel.replace('#id#',this.previousSibling.value);"><?=Loc::getMessage("V_CHANNEL_ID_EDIT")?></a></td>
270 </tr>
271 <tbody id="channel_create" style="<?if ($value != "add"): ?>display:none;<? endif; ?>">
272 <tr class="adm-detail-required-field">
273 <td class="adm-detail-content-cell-l" width="40%"><?=Loc::getMessage("V_CHANNEL_ID_TITLE")?></td>
274 <td class="adm-detail-content-cell-r" width="60%"><input type="text" name="<?=$htmlControl["NAME"]?>[CHANNEL_TITLE]" <?
275 ?>value="<?=htmlspecialcharsbx($entity["CHANNEL_TITLE"]);?>" /></td>
276 </tr>
277 <tr class="adm-detail-required-field">
278 <td class="adm-detail-content-cell-l"><?=Loc::getMessage("V_CHANNEL_ID_SYMBOLIC_NAME")?></td>
279 <td class="adm-detail-content-cell-r"><input type="text" name="<?=$htmlControl["NAME"]?>[CHANNEL_SYMBOLIC_NAME]" <?
280 ?>value="<?=htmlspecialcharsbx($entity["CHANNEL_SYMBOLIC_NAME"]);?>" /></td>
281 </tr>
282 <tr>
283 <td class="adm-detail-content-cell-l">&nbsp;</td>
284 <td class="adm-detail-content-cell-r"><input type="checkbox" name="<?=$htmlControl["NAME"]?>[CHANNEL_USE_CAPTCHA]" <?
285 ?>id="CHANNEL_USE_CAPTCHA" <?if ($entity["CHANNEL_USE_CAPTCHA"] == "Y"): ?> checked <? endif;
286 ?>value="Y" /> <label for="CHANNEL_USE_CAPTCHA"><?=Loc::getMessage("V_CHANNEL_ID_USE_CAPTCHA")?></label></td>
287 </tr><?
288 $dbRes = \CGroup::GetList("sort", "asc", Array("ADMIN" => "N"));
289 while ($group = $dbRes->getNext())
290 {
291 if($varsFromForm)
292 $value = $entity["GROUP_ID"][$group["ID"]];
293 else
294 $value = ($group["ID"] == 2 ? 1 : ($group["ID"] == 1 ? 4 : 2));
295 ?>
296 <tr>
297 <td class="adm-detail-content-cell-l"><?=$group["NAME"].":"?></td>
298 <td class="adm-detail-content-cell-r"><?=SelectBoxFromArray("GROUP_ID[".$group["ID"]."]", $aVotePermissions, $value);?></td>
299 </tr><?
300 }
301
302 ?>
303 </tbody>
304 <?
305 if($varsFromForm)
306 {
307 $entity['UNIQUE'] = is_array($entity['UNIQUE']) ?
308 $entity['UNIQUE'] : array();
309 $uniqType = 0;
310 foreach ($entity['UNIQUE'] as $res)
311 $uniqType |= $res;
312 }
313 else
314 {
316 if (is_array($userField) && array_key_exists("SETTINGS", $userField) && array_key_exists("UNIQUE", $userField["SETTINGS"]))
317 {
318 $uniqType = 0;
319 if (is_array($userField["SETTINGS"]["UNIQUE"]))
320 {
321 foreach ( $userField["SETTINGS"]["UNIQUE"] as $res)
322 $uniqType |= $res;
323 }
324 else
325 {
326 $uniqType = intval($userField["SETTINGS"]["UNIQUE"]);
327 }
328 }
329 }
330 if ($uniqType&\Bitrix\Vote\Vote\EventLimits::BY_USER_AUTH)
331 {
333 }
334 ?>
335 <script language="javascript">
336 function __utch(show)
337 {
338 if (BX("UNIQUE_TYPE_IP").checked)
339 BX.show(BX("DELAY_TYPE"), "");
340 else
341 BX.hide(BX("DELAY_TYPE"));
342
343 var
344 show = BX("UNIQUE_TYPE_USER_ID").checked,
345 res = BX("UNIQUE_TYPE_USER_ID_NEW");
346 res.disabled = !show;
347 if (!!show)
348 BX.show(res.parentNode.parentNode, "");
349 else
350 BX.hide(res.parentNode.parentNode);
351 }
352 </script>
353 <tr>
354 <td class="adm-detail-content-cell-l adm-detail-valign-top" width="40%"><?=Loc::getMessage("VOTE_NOTIFY")?></td>
355 <td class="adm-detail-content-cell-r" width="60%"><?
356 $entity["NOTIFY"] = (
357 $entity["NOTIFY"] != "I" && $entity["NOTIFY"] != "Y" ?
358 "N" : $entity["NOTIFY"]);
359 if (IsModuleInstalled("im")): ?>
360 <?=InputType("radio", $htmlControl["NAME"]."[NOTIFY]", "I", $entity["NOTIFY"], false, Loc::getMessage("VOTE_NOTIFY_IM"))?><br /><?
361 else:
362 $entity["NOTIFY"] = ($entity["NOTIFY"] == "I" ?
363 "N" : $entity["NOTIFY"]);
364 endif; ?>
365 <?=InputType("radio", $htmlControl["NAME"]."[NOTIFY]", "Y", $entity["NOTIFY"], false, Loc::getMessage("VOTE_NOTIFY_EMAIL"))?><br />
366 <?=InputType("radio", $htmlControl["NAME"]."[NOTIFY]", "N", $entity["NOTIFY"], false, Loc::getMessage("VOTE_NOTIFY_N"))?><?
367 ?></td>
368 </tr>
369 <tr>
370 <td class="adm-detail-content-cell-l adm-detail-valign-top"><?=Loc::getMessage("V_UNIQUE")?></td>
371 <td class="adm-detail-content-cell-r">
372 <? if (IsModuleInstalled('statistic')): ?>
373 <input type="checkbox" name="<?=$htmlControl["NAME"]?>[UNIQUE][]" id="UNIQUE_TYPE_SESSION" value="1" <?=($uniqType & \Bitrix\Vote\Vote\EventLimits::BY_SESSION)?" checked":""?> />
374 <label for="UNIQUE_TYPE_SESSION"><?=Loc::getMessage("V_UNIQUE_SESSION")?></label><br />
375 <? endif; ?>
376 <input type="checkbox" name="<?=$htmlControl["NAME"]?>[UNIQUE][]" id="UNIQUE_TYPE_COOKIE" value="2" <?=($uniqType & \Bitrix\Vote\Vote\EventLimits::BY_COOKIE)?" checked":""?> />
377 <label for="UNIQUE_TYPE_COOKIE"><?=Loc::getMessage("V_UNIQUE_COOKIE_ONLY")?></label><br />
378 <input type="checkbox" name="<?=$htmlControl["NAME"]?>[UNIQUE][]" id="UNIQUE_TYPE_IP" onclick="__utch()" value="<?=\Bitrix\Vote\Vote\EventLimits::BY_IP?>" <?
379 ?><?=($uniqType & \Bitrix\Vote\Vote\EventLimits::BY_IP) ? " checked":""?> />
380 <label for="UNIQUE_TYPE_IP"><?=Loc::getMessage("V_UNIQUE_IP_ONLY")?></label><br />
381 <input type="checkbox" name="<?=$htmlControl["NAME"]?>[UNIQUE][]" id="UNIQUE_TYPE_USER_ID" onclick="__utch();" value="<?=(\Bitrix\Vote\Vote\EventLimits::BY_USER_AUTH|\Bitrix\Vote\Vote\EventLimits::BY_USER_ID)?>" <?
383 <label for="UNIQUE_TYPE_USER_ID"><?=Loc::getMessage("V_UNIQUE_USER_ID_ONLY")?></label><br />
384 </td>
385 </tr>
386 <tr>
387 <td class="adm-detail-content-cell-l" width="40%">&nbsp;</td>
388 <td class="adm-detail-content-cell-r" width="60%"><input type="checkbox" name="<?=$htmlControl["NAME"]?>[UNIQUE][]" id="UNIQUE_TYPE_USER_ID_NEW" value="16" <?
389 ?><?=($uniqType & 16)?" checked ":""?><?
390 ?><?=($uniqType & 8)?"": " disabled"?> /> <label for="UNIQUE_TYPE_USER_ID_NEW"><?=Loc::getMessage("V_UNIQUE_USER_ID_NEW")?></label>
391 </td>
392 </tr>
393 <?
394 if($varsFromForm)
395 $value = $entity["UNIQUE_IP_DELAY"];
396 else
397 $value = (is_array($userField) ?
398 $userField["SETTINGS"]["UNIQUE_IP_DELAY"] :
399 array("DELAY" => "10", "DELAY_TYPE" => "D"));
400 ?>
401 <tr id="DELAY_TYPE">
402 <td class="adm-detail-content-cell-l" width="40%"><?=Loc::getMessage("V_UNIQUE_IP_DELAY")?></td>
403 <td class="adm-detail-content-cell-r" width="60%">
404 <input type="text" name="<?=$htmlControl["NAME"]?>[UNIQUE_IP_DELAY][DELAY]" value="<?=htmlspecialcharsbx($value["DELAY"]);?>" />
405 <?=SelectBoxFromArray(
406 $htmlControl["NAME"]."[UNIQUE_IP_DELAY][DELAY_TYPE]",
407 array(
408 "reference_id" => array("S", "M", "H", "D"),
409 "reference" => array(
410 Loc::getMessage("V_SECONDS"), Loc::getMessage("V_MINUTES"),
411 Loc::getMessage("V_HOURS"), Loc::getMessage("V_DAYS"))
412 ),
413 $value["DELAY_TYPE"]);?>
414 <script type="text/javascript">
415 BX.ready(function(){
416 if (!!document.forms.post_form.MULTIPLE)
417 BX.hide(document.forms.post_form.MULTIPLE.parentNode.parentNode);
418 __utch();
419 });
420 </script>
421
422 </td>
423 </tr>
424 <?
425 return ob_get_clean();
426 }
427
433 public static function getEditFormHTML($userField, $value, $bVarsFromForm = false)
434 {
435 ob_start();
436 $params = array(
437 "arUserField" => $userField,
438 "bVarsFromForm" => $bVarsFromForm
439 );
440 \Bitrix\Vote\UF\Manager::getInstance($userField)->showEdit($params, $value);
441 return ob_get_clean();
442 }
443
449 public static function getPublicViewHTML($userField, $value)
450 {
451 ob_start();
452 $params = array(
453 "arUserField" => $userField
454 );
455 $result = array(
456 $value
457 );
458 \Bitrix\Vote\UF\Manager::getInstance($userField)->showView($params, $result);
459 return ob_get_clean();
460 }
461
465 public static function getFilterHTML()
466 {
467 return '';
468 }
469
475 public static function getAdminListViewHTML($userField, $htmlControl)
476 {
477 global $APPLICATION;
478 $return = '&nbsp;';
479 $returnUrl = $APPLICATION->GetCurPageParam("", array("admin_history", "mode", "table_id"));
480
481 if($htmlControl["VALUE"] > 0)
482 {
483 $dbRes = \CVote::GetByIDEx($htmlControl["VALUE"]);
484 if ($dbRes && ($vote = $dbRes->GetNext()))
485 {
486 if ($vote["LAMP"] == "yellow")
487 $vote["LAMP"] = ($vote["ID"] == \CVote::GetActiveVoteId($vote["CHANNEL_ID"]) ? "green" : "red");
488 $return = "<div class=\"lamp-red\" title=\"".($vote["ACTIVE"] != 'Y' ? Loc::getMessage("VOTE_NOT_ACTIVE") : Loc::getMessage("VOTE_ACTIVE_RED_LAMP"))."\" style=\"display:inline-block;\"></div>";
489 if ($vote["LAMP"]=="green")
490 $return = "<div class=\"lamp-green\" title=\"".Loc::getMessage("VOTE_LAMP_ACTIVE")."\" style=\"display:inline-block;\"></div>";
491 $return .= " [<a href='vote_edit.php?lang=".LANGUAGE_ID."&ID=".$vote["ID"]."&return_url=".urlencode($returnUrl)."' title='".Loc::getMessage("VOTE_EDIT_TITLE")."'>".$vote["ID"]."</a>] ";
492 $return .= $vote["TITLE"].(!empty($vote["DESCRIPTION"]) ? " <i>(".$vote["DESCRIPTION"].")</i>" : "");
493 if ($vote["COUNTER"] > 0)
494 $return .= Loc::getMessage("VOTE_VOTES")." <a href=\"vote_user_votes_table.php?lang={LANGUAGE_ID}&VOTE_ID={$vote["ID"]}\">".$vote["COUNTER"]."</a>";
495 }
496
497 }
498 return $return;
499 }
500
506 public static function getAdminListEditHTML($userField, $htmlControl)
507 {
508 return '<input type="text" '.
509 'name="'.$htmlControl["NAME"].'" '.
510 'size="'.$userField["SETTINGS"]["SIZE"].'" '.
511 'value="'.$htmlControl["VALUE"].'" '.
512 '>';
513 }
514
520 public static function getAdminListEditHTMLMulty($userField, $htmlControl)
521 {
522 return "&nbsp;";
523 }
524
532 public static function checkFields($userField, $value, $userId = false)
533 {
534 if ($userField && is_array($userField["USER_TYPE"]) && $userField["USER_TYPE"]["CLASS_NAME"] == __CLASS__)
535 {
536 $userId = self::getOrCheckUserId($userId);
537 try
538 {
539 global ${$userField["FIELD_NAME"] . "_" . $value . "_DATA"};
540 $data = ${$userField["FIELD_NAME"] . "_" . $value . "_DATA"} ?: false;
541 if (!is_array($data) || empty($data))
542 return array();
543
544 $userFieldManager = Manager::getInstance($userField);
545
546 [$type, $realValue] = self::detectType($value);
547
548 try
549 {
550 $attach = ($type == self::TYPE_SAVED_ATTACH ? $userFieldManager->loadFromAttachId($realValue) :
551 ($data["ID"] > 0 ? $userFieldManager->loadFromVoteId($data["ID"]) : $userFieldManager->loadEmptyObject()));
552 }
553 catch (\Bitrix\Main\ObjectNotFoundException $exception)
554 {
555 $attach = $userFieldManager->loadEmptyObject();
556 unset($data["ID"]);
557 }
558
559 if (isset($attach["ID"]))
560 {
561 if ($data["ID"] > 0 && $attach["VOTE_ID"] != $data["ID"])
562 {
563 throw new \Bitrix\Main\ArgumentException(Loc::getMessage("VOTE_IS_NOT_EXPECTED"));
564 }
565 if (intval($data["ID"]) <= 0 && $attach->getStorage()->getId() !== $userField["SETTINGS"]["CHANNEL_ID"])
566 {
567 $attach = $userFieldManager->loadEmptyObject();
568 }
569 }
570
571 if (!$userFieldManager->belongsToEntity($attach, $userField["ENTITY_ID"], $userField["ENTITY_VALUE_ID"] ?? null))
572 {
573 throw new \Bitrix\Main\ObjectNotFoundException(Loc::getMessage("VOTE_IS_NOT_FOUND"));
574 }
575
576 $data["OPTIONS"] = isset($data["OPTIONS"]) ? (is_array($data["OPTIONS"]) ? array_sum($data["OPTIONS"]) : (int) $data["OPTIONS"]) : 0;
577 $data["UNIQUE_TYPE"] = ($userField["SETTINGS"]["UNIQUE"] & \Bitrix\Vote\Vote\EventLimits::BY_USER_AUTH ? $userField["SETTINGS"]["UNIQUE"] | \Bitrix\Vote\Vote\EventLimits::BY_USER_ID : $userField["SETTINGS"]["UNIQUE"]);
578 $interval = intval($userField["SETTINGS"]["UNIQUE_IP_DELAY"]["DELAY"]);
579 $interval = in_array($userField["SETTINGS"]["UNIQUE_IP_DELAY"]["DELAY_TYPE"], array("S", "M", "H")) ? "PT".$interval.$userField["SETTINGS"]["UNIQUE_IP_DELAY"]["DELAY_TYPE"] : "P".$interval."D";
580 $data["KEEP_IP_SEC"] = (new \DateTime("@0"))->add(new \DateInterval($interval))->getTimestamp();
581 $data["NOTIFY"] = $userField["SETTINGS"]["NOTIFY"];
582
583 $attach->checkData($data);
584
585 if (!isset($attach["ID"]) &&
586 $attach->getStorage()->getId() != $userField["SETTINGS"]["CHANNEL_ID"] &&
587 !$attach->getStorage()->canEditVote($userId))
588 {
589 throw new \Bitrix\Main\AccessDeniedException(Loc::getMessage("VOTE_EDIT_ACCESS_IS_DENIED"));
590 }
591 if (!$attach->canRead($userId))
592 {
593 throw new \Bitrix\Main\AccessDeniedException(Loc::getMessage("VOTE_READ_ACCESS_IS_DENIED"));
594 }
595 if (!empty($data) && !$attach->canEdit($userId))
596 {
597 throw new \Bitrix\Main\AccessDeniedException(Loc::getMessage("VOTE_EDIT_ACCESS_IS_DENIED"));
598 }
599 }
600 catch (\Exception $e)
601 {
602 return array(array("id" => "voteUserType" , "text" => $e->getMessage()));
603 }
604 }
605 return array();
606 }
607
612 public static function onSearchIndex($userField)
613 {
614 return "";
615 }
616
624 public static function onBeforeSave($userField, $value, $userId = false)
625 {
626/* if (empty($value))
627 {
628 $alreadyExistsValues = $userField['VALUE'];
629 if (!is_array($alreadyExistsValues))
630 {
631 $alreadyExistsValues = array($userField['VALUE']);
632 }
633 Attach::detachByFilter(array('ID' => $alreadyExistsValues));
634 }*/
635 try
636 {
637 $userId = self::getOrCheckUserId($userId);
638
639 global ${$userField["FIELD_NAME"] . "_" . $value . "_DATA"};
640 $data = ${$userField["FIELD_NAME"] . "_" . $value . "_DATA"} ?: false;
641
642 $userFieldManager = Manager::getInstance($userField);
643 [$type, $realValue] = self::detectType($value);
644 if ($type == self::TYPE_SAVED_ATTACH && (!is_array($data) || empty($data)))
645 {
646 return $value;
647 }
648
649 if (!is_array($data) || empty($data))
650 return "";
651
652 /*@var \Bitrix\Vote\Attach $attach*/
653 try
654 {
655 if ($type == self::TYPE_SAVED_ATTACH)
656 {
657 $attach = $userFieldManager->loadFromAttachId($realValue);
658
659 if ($attach->getStorage()->getId() != $userField["SETTINGS"]["CHANNEL_ID"] &&
660 $userFieldManager->belongsToEntity($attach, $userField["ENTITY_ID"], $userField["ENTITY_VALUE_ID"])
661 )
662 {
663 $attach->delete();
664 $attach = $userFieldManager->loadEmptyObject();
665 unset($data["ID"]);
666 }
667 }
668 else
669 {
670 $attach = ($data["ID"] > 0 ? $userFieldManager->loadFromVoteId($data["ID"]) : $userFieldManager->loadEmptyObject());
671 }
672 }
673 catch (\Bitrix\Main\ObjectNotFoundException $exception)
674 {
675 $attach = $userFieldManager->loadEmptyObject();
676 unset($data["ID"]);
677 }
678
679 $data["OPTIONS"] = !empty($data["OPTIONS"]) ? (is_array($data["OPTIONS"]) ? array_sum($data["OPTIONS"]) : (int) $data["OPTIONS"]) : 0;
680 $data["UNIQUE_TYPE"] = intval($userField["SETTINGS"]["UNIQUE"] & \Bitrix\Vote\Vote\EventLimits::BY_USER_AUTH ? $userField["SETTINGS"]["UNIQUE"] | \Bitrix\Vote\Vote\EventLimits::BY_USER_ID : $userField["SETTINGS"]["UNIQUE"]);
681 $interval = intval($userField["SETTINGS"]["UNIQUE_IP_DELAY"]["DELAY"]);
682 $interval = in_array($userField["SETTINGS"]["UNIQUE_IP_DELAY"]["DELAY_TYPE"], array("S", "M", "H")) ? "PT".$interval.$userField["SETTINGS"]["UNIQUE_IP_DELAY"]["DELAY_TYPE"] : "P".$interval."D";
683 $data["KEEP_IP_SEC"] = (new \DateTime("@0"))->add(new \DateInterval($interval))->getTimestamp();
684 $data["NOTIFY"] = $userField["SETTINGS"]["NOTIFY"];
685
686 $attach->save($data, $userId);
687
688 return $attach->getAttachId();
689 }
690 catch (\Exception $e)
691 {
692 throw $e;
693 }
694 }
695
705 public static function onBeforeCopy(array $userField, int $newEntityId, $attachedId, $implementer, $userId = false)
706 {
707 if (empty($userField) || empty($attachedId))
708 {
709 return "";
710 }
711
712 $userId = self::getOrCheckUserId($userId);
713 $userFieldManager = Manager::getInstance($userField);
714
715 $attachedObject = $userFieldManager->loadFromAttachId($attachedId);
716
717 $voteId = 0;
718 if (is_callable([$implementer, "copyVote"]))
719 {
720 $voteId = $implementer->copyVote($attachedObject->getVoteId());
721 }
722
723 $attachedId = "";
724 if ($voteId > 0)
725 {
726 $attachedId = AttachTable::add([
727 "MODULE_ID" => $attachedObject->getModuleId(),
728 "OBJECT_ID" => $voteId,
729 "ENTITY_ID" => $newEntityId,
730 "ENTITY_TYPE" => $attachedObject->getEntityType(),
731 "CREATED_BY" => $userId,
732 "CREATE_TIME" => new DateTime()
733 ])->getId();
734 }
735
736 return $attachedId;
737 }
738
744 public static function onDelete($userField, $value)
745 {
746 if (empty($value))
747 return;
748 $userFieldManager = Manager::getInstance($userField);
749
750 [$type, $realValue] = self::detectType($value);
751 $attach = ($type == self::TYPE_SAVED_ATTACH ? $userFieldManager->loadFromAttachId($realValue) :
752 ($realValue > 0 ? $userFieldManager->loadFromVoteId($realValue) : $userFieldManager->loadEmptyObject()));
753
754 global $USER;
755 if ($userFieldManager->belongsToEntity($attach, $userField['ENTITY_ID'], $userField['ENTITY_VALUE_ID']) && $attach->canEdit($USER->getId()))
756 $attach->delete();
757 }
758
764 public static function detectType($value)
765 {
766 $prefix = "";
767 if (mb_strpos($value, self::NEW_VOTE_PREFIX) === 0)
768 {
769 $prefix = self::NEW_VOTE_PREFIX;
770 $value = intval(mb_substr($value, 1));
771 }
772 else
773 $value = intval($value);
774
775 $return = ($prefix == self::NEW_VOTE_PREFIX ? array(self::TYPE_NEW_ATTACH, $value) : array(self::TYPE_SAVED_ATTACH, $value));
776
777 return $return;
778 }
779
780 private static function getOrCheckUserId($userId = false)
781 {
782 if ($userId === false)
783 {
784 global $USER;
785 if ($USER instanceof \CUser)
786 {
787 return $USER->GetID();
788 }
789 }
790 return intval($userId);
791 }
792}
static includeModule($moduleName)
Definition loader.php:69
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getInstance(array $params)
Definition manager.php:87
static onBeforeSave($userField, $value, $userId=false)
static getSettingsHTML($userField, $htmlControl, $varsFromForm)
static getPublicViewHTML($userField, $value)
static checkFields($userField, $value, $userId=false)
static onSearchIndex($userField)
static getAdminListEditHTMLMulty($userField, $htmlControl)
static onBeforePostUserFieldUpdate($entityId, $ID, $fields)
static getAdminListEditHTML($userField, $htmlControl)
static onDelete($userField, $value)
static getDBColumnType($userField)
static getAdminListViewHTML($userField, $htmlControl)
static getEditFormHTML($userField, $value, $bVarsFromForm=false)
static prepareSettings($userField)
static onBeforeCopy(array $userField, int $newEntityId, $attachedId, $implementer, $userId=false)
static checkSettings(&$params)
$GLOBALS['____1444769544']
Definition license.php:1