Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions ts/WoltLabSuite/Core/Component/Confirmation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { dialogFactory } from "./Dialog";
import { getPhrase } from "../Language";
import * as DomUtil from "../Dom/Util";
import { ConfirmationCustom } from "./Confirmation/Custom";
import { escapeHTML } from "../StringUtil";

type ResultSoftDeleteWithoutReason = {
result: boolean;
Expand All @@ -40,8 +41,15 @@ class ConfirmationPrefab {
return new ConfirmationCustom(question);
}

async delete(title?: string): Promise<boolean> {
const html = `<p>${getPhrase("wcf.dialog.confirmation.cannotBeUndone")}</p>`;
async delete(title?: string, affectedObjects: string[] = []): Promise<boolean> {
let html = "";
if (affectedObjects.length > 0) {
const items = affectedObjects.map((name) => `<li>${escapeHTML(name)}</li>`).join("");
html += `<p>${getPhrase("wcf.dialog.confirmation.delete.affectedObjects")}</p>`;
html += `<ul class="nativeList">${items}</ul>`;
}
html += `<p>${getPhrase("wcf.dialog.confirmation.cannotBeUndone")}</p>`;

const dialog = dialogFactory()
.fromHtml(html)
.asConfirmation({
Expand Down
6 changes: 4 additions & 2 deletions ts/WoltLabSuite/Core/Component/Interaction/Bulk/Rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { deleteObject } from "WoltLabSuite/Core/Api/DeleteObject";
import { postObject } from "WoltLabSuite/Core/Api/PostObject";
import { ConfirmationType, handleConfirmation } from "../Confirmation";
import { ConfirmationType, handleConfirmation, parseAffectedObjects } from "../Confirmation";
import { showProgressSnackbar } from "WoltLabSuite/Core/Component/Snackbar";

async function handleRpcInteraction(
Expand All @@ -19,8 +19,9 @@ async function handleRpcInteraction(
label: string,
confirmationType: ConfirmationType,
customConfirmationMessage: string = "",
affectedObjects: string[] = [],
): Promise<void> {
const confirmationResult = await handleConfirmation("", confirmationType, customConfirmationMessage);
const confirmationResult = await handleConfirmation("", confirmationType, customConfirmationMessage, affectedObjects);
if (!confirmationResult.result) {
return;
}
Expand Down Expand Up @@ -75,6 +76,7 @@ export function setup(identifier: string, container: HTMLElement): void {
event.detail.label,
event.detail.confirmationType,
event.detail.confirmationMessage,
parseAffectedObjects(event.detail.affectedObjects),
);
}
});
Expand Down
24 changes: 23 additions & 1 deletion ts/WoltLabSuite/Core/Component/Interaction/Confirmation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,32 @@ type ResultConfirmationWithReason = {
reason?: string;
};

/**
* Parses the JSON encoded list of affected objects from a data attribute. Absent or
* malformed values yield an empty list, the confirmation is then shown without them.
*/
export function parseAffectedObjects(value: string | undefined): string[] {
if (!value) {
return [];
}

try {
const result: unknown = JSON.parse(value);
if (Array.isArray(result)) {
return result.filter((item): item is string => typeof item === "string");
}
} catch {
// Ignore malformed values.
}

return [];
}

export async function handleConfirmation(
objectName: string,
confirmationType: ConfirmationType,
customMessage: string = "",
affectedObjects: string[] = [],
): Promise<ResultConfirmationWithReason> {
if (confirmationType == ConfirmationType.SoftDelete) {
return await confirmationFactory().softDelete(objectName ? objectName : undefined);
Expand All @@ -45,7 +67,7 @@ export async function handleConfirmation(

if (confirmationType == ConfirmationType.Delete) {
return {
result: await confirmationFactory().delete(objectName ? objectName : undefined),
result: await confirmationFactory().delete(objectName ? objectName : undefined, affectedObjects),
};
}

Expand Down
11 changes: 9 additions & 2 deletions ts/WoltLabSuite/Core/Component/Interaction/Rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { deleteObject } from "WoltLabSuite/Core/Api/DeleteObject";
import { postObject } from "WoltLabSuite/Core/Api/PostObject";
import { ConfirmationType, handleConfirmation } from "./Confirmation";
import { ConfirmationType, handleConfirmation, parseAffectedObjects } from "./Confirmation";
import { showDefaultSuccessSnackbar, showSuccessSnackbar } from "WoltLabSuite/Core/Component/Snackbar";
import { getPhrase } from "WoltLabSuite/Core/Language";
import { InteractionEffect } from "./InteractionEffect";
Expand All @@ -24,9 +24,15 @@ async function handleRpcInteraction(
confirmationType: ConfirmationType,
customConfirmationMessage: string = "",
interactionEffect: InteractionEffect = InteractionEffect.ReloadItem,
affectedObjects: string[] = [],
detail: Payload,
): Promise<void> {
const confirmationResult = await handleConfirmation(objectName, confirmationType, customConfirmationMessage);
const confirmationResult = await handleConfirmation(
objectName,
confirmationType,
customConfirmationMessage,
affectedObjects,
);
if (!confirmationResult.result) {
return;
}
Expand Down Expand Up @@ -78,6 +84,7 @@ export function setup(identifier: string, container: HTMLElement): void {
event.detail.confirmationType as ConfirmationType,
event.detail.confirmationMessage,
event.detail.interactionEffect as InteractionEffect,
parseAffectedObjects(event.detail.affectedObjects),
event.detail,
);
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,10 @@ public function getAddControllerClass(): string
{
return '';
}

#[\Override]
public function getAffectedObjects(): array
{
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public function beforeDeletion(CategoryEditor $categoryEditor)
}
}

#[\Override]
public function getAffectedObjects(): array
{
return [WCF::getLanguage()->get('wcf.article.articles')];
}

#[\Override]
public function canAddCategory()
{
Expand Down
10 changes: 10 additions & 0 deletions wcfsetup/install/files/lib/system/category/ICategoryType.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,14 @@ public function getEditFormLink(Category $category): string;
* @since 6.3
*/
public function getAddFormLink(?Category $parentCategory = null): string;

/**
* Returns the names of the objects that are deleted along with a category of this type.
*
* The names are expected to be already localized.
*
* @return list<string>
* @since 6.3
*/
public function getAffectedObjects(): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public function canEditCategory()
return WCF::getSession()->hasPermission('admin.trophy.canManageTrophy');
}

#[\Override]
public function getAffectedObjects(): array
{
return [WCF::getLanguage()->get('wcf.user.trophy.trophies')];
}

#[\Override]
public function beforeDeletion(CategoryEditor $categoryEditor)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function __invoke(PreloadPhrasesCollecting $event): void
$event->preload('wcf.dialog.button.primary.submit');
$event->preload('wcf.dialog.confirmation.cannotBeUndone');
$event->preload('wcf.dialog.confirmation.delete');
$event->preload('wcf.dialog.confirmation.delete.affectedObjects');
$event->preload('wcf.dialog.confirmation.delete.indeterminate');
$event->preload('wcf.dialog.confirmation.disable');
$event->preload('wcf.dialog.confirmation.disable.indeterminate');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace wcf\system\interaction;

use wcf\data\DatabaseObject;

/**
* Represents a delete interaction.
*
Expand All @@ -12,9 +14,15 @@
*/
class DeleteInteraction extends RpcInteraction
{
/**
* @param list<string>|\Closure $affectedObjects Names of the objects that are implicitly deleted
* along with the object itself. A closure receives the `DatabaseObject` the
* interaction is rendered for and must return a list of names.
*/
public function __construct(
string $endpoint,
?\Closure $isAvailableCallback = null
?\Closure $isAvailableCallback = null,
private readonly array|\Closure $affectedObjects = []
) {
parent::__construct(
'delete',
Expand All @@ -26,4 +34,22 @@ public function __construct(
InteractionEffect::RemoveItem
);
}

#[\Override]
protected function getAdditionalDataAttributes(DatabaseObject $object): array
{
if (\is_array($this->affectedObjects)) {
$affectedObjects = $this->affectedObjects;
} else {
$affectedObjects = ($this->affectedObjects)($object);
}

if ($affectedObjects === []) {
return [];
}

return [
'data-affected-objects' => \json_encode(\array_values($affectedObjects), \JSON_THROW_ON_ERROR),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ public function render(DatabaseObject $object): string
}
}

$additionalAttributes = '';
foreach ($this->getAdditionalDataAttributes($object) as $name => $value) {
$additionalAttributes .= \sprintf(
' %s="%s"',
StringUtil::encodeHTML($name),
StringUtil::encodeHTML($value)
);
}

return <<<HTML
<button
type="button"
Expand All @@ -76,13 +85,26 @@ public function render(DatabaseObject $object): string
data-endpoint="{$endpoint}"
data-confirmation-type="{$this->confirmationType->toString()}"
data-confirmation-message="{$confirmationMessage}"
data-interaction-effect="{$this->interactionEffect->toString()}"
data-interaction-effect="{$this->interactionEffect->toString()}"{$additionalAttributes}
>
{$label}
</button>
HTML;
}

/**
* Returns additional data attributes that are added to the rendered button.
*
* The returned names and values are HTML encoded by the caller.
*
* @return array<string, string>
* @since 6.3
*/
protected function getAdditionalDataAttributes(DatabaseObject $object): array
{
return [];
}

#[\Override]
public function renderInitialization(string $containerId): string
{
Expand Down
Loading
Loading