-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathRpcInteraction.class.php
More file actions
100 lines (87 loc) · 3.39 KB
/
RpcInteraction.class.php
File metadata and controls
100 lines (87 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace wcf\system\interaction;
use wcf\action\ApiAction;
use wcf\data\DatabaseObject;
use wcf\data\DatabaseObjectDecorator;
use wcf\data\ITitledObject;
use wcf\system\request\LinkHandler;
use wcf\system\WCF;
use wcf\util\StringUtil;
/**
* Represents an interaction that call a rpc endpoint.
*
* @author Marcel Werk
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.2
*/
class RpcInteraction extends AbstractInteraction
{
public function __construct(
string $identifier,
protected readonly string $endpoint,
protected readonly string|\Closure $languageItem,
protected readonly InteractionConfirmationType $confirmationType = InteractionConfirmationType::None,
protected readonly string|\Closure $confirmationMessage = '',
?\Closure $isAvailableCallback = null,
protected readonly InteractionEffect $interactionEffect = InteractionEffect::ReloadItem,
) {
parent::__construct($identifier, $isAvailableCallback);
}
#[\Override]
public function render(DatabaseObject $object): string
{
$identifier = StringUtil::encodeJS($this->getIdentifier());
if (\is_string($this->languageItem)) {
$label = WCF::getLanguage()->get($this->languageItem);
} else {
$label = ($this->languageItem)($object);
}
if (\is_string($this->confirmationMessage)) {
$confirmationMessage = WCF::getLanguage()->getDynamicVariable($this->confirmationMessage);
} else {
$confirmationMessage = ($this->confirmationMessage)($object);
}
$endpoint = StringUtil::encodeHTML(
LinkHandler::getInstance()->getControllerLink(ApiAction::class, ['id' => 'rpc']) .
\sprintf($this->endpoint, $object->getObjectID())
);
if ($object instanceof ITitledObject) {
$objectName = StringUtil::encodeHTML($object->getTitle());
} else {
$objectName = '';
if ($object instanceof DatabaseObjectDecorator) {
$baseObject = $object->getDecoratedObject();
if ($baseObject instanceof ITitledObject) {
$objectName = StringUtil::encodeHTML($baseObject->getTitle());
}
}
}
return <<<HTML
<button
type="button"
data-interaction="{$identifier}"
data-object-name="{$objectName}"
data-endpoint="{$endpoint}"
data-confirmation-type="{$this->confirmationType->toString()}"
data-confirmation-message="{$confirmationMessage}"
data-interaction-effect="{$this->interactionEffect->toString()}"
>
{$label}
</button>
HTML;
}
#[\Override]
public function renderInitialization(string $containerId): ?string
{
$identifier = StringUtil::encodeJS($this->getIdentifier());
$containerId = StringUtil::encodeJS($containerId);
return <<<HTML
<script data-relocate="true">
require(['WoltLabSuite/Core/Component/Interaction/Rpc'], ({ setup }) => {
setup('{$identifier}', document.getElementById('{$containerId}'));
});
</script>
HTML;
}
}