Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ public function __get(string $name): string
}
}

if ($this->recipient === null) {
foreach ($this->contexts as $context) {
if ($context->requiresRecipientByPlaceholderName($name)) {
return $name;
}
}
}

return '';
}
}
4 changes: 0 additions & 4 deletions components/ILIAS/Mail/classes/Service/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ public function placeholderResolver(): \ilMailTemplatePlaceholderResolver
);
}

public function placeholderToEmptyResolver(): \ilMailTemplatePlaceholderToEmptyResolver
{
return new \ilMailTemplatePlaceholderToEmptyResolver();
}

public function mustacheFactory(): \ilMustacheFactory
{
Expand Down
14 changes: 4 additions & 10 deletions components/ILIAS/Mail/classes/class.ilMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public function __construct(
private ?int $mail_obj_ref_id = null,
private ?ilObjUser $actor = null,
private ?ilMailTemplatePlaceholderResolver $placeholder_resolver = null,
private ?ilMailTemplatePlaceholderToEmptyResolver $placeholder_to_empty_resolver = null,
?Conductor $legal_documents = null,
?MailSignatureService $signature_service = null,
) {
Expand Down Expand Up @@ -102,7 +101,6 @@ public function __construct(
$this->table_mail_saved = 'mail_saved';
$this->setSaveInSentbox(false);
$this->placeholder_resolver = $placeholder_resolver ?? $DIC->mail()->placeholderResolver();
$this->placeholder_to_empty_resolver = $placeholder_to_empty_resolver ?? $DIC->mail()->placeholderToEmptyResolver();
$this->legal_documents = $legal_documents ?? $DIC['legalDocuments'];
$this->signature_service = $signature_service ?? $DIC->mail()->signature();
}
Expand Down Expand Up @@ -563,7 +561,7 @@ private function sendInternalMail(

private function replacePlaceholders(
string $message,
int $usrId = 0
?int $usrId = null
): string {
try {
if ($this->context_id) {
Expand All @@ -572,7 +570,7 @@ private function replacePlaceholders(
$context = new ilMailTemplateGenericContext();
}

$user = $usrId > 0 ? $this->getUserInstanceById($usrId) : null;
$user = ($usrId !== null && $usrId > 0) ? $this->getUserInstanceById($usrId) : null;
$message = $this->placeholder_resolver->resolve(
$context,
$message,
Expand All @@ -591,10 +589,6 @@ private function replacePlaceholders(
return $message;
}

private function replacePlaceholdersEmpty(string $message): string
{
return $this->placeholder_to_empty_resolver->resolve($message);
}

private function distributeMail(MailDeliveryData $mail_data): bool
{
Expand Down Expand Up @@ -650,7 +644,7 @@ private function sendMailWithReplacedEmptyPlaceholder(
$this->sendChanneledMails(
$mail_data,
$recipients,
$this->replacePlaceholdersEmpty($mail_data->getMessage()),
$this->replacePlaceholders($mail_data->getMessage()),
);
}

Expand Down Expand Up @@ -1125,7 +1119,7 @@ public function sendMail(
$externalMailRecipientsBcc,
$mail_data->getSubject(),
$mail_data->isUsePlaceholder() ?
$this->replacePlaceholders($mail_data->getMessage(), 0) :
$this->replacePlaceholders($mail_data->getMessage()) :
$mail_data->getMessage(),
$mail_data->getAttachments()
);
Expand Down
83 changes: 78 additions & 5 deletions components/ILIAS/Mail/classes/class.ilMailTemplateContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

declare(strict_types=1);

use OrgUnit\PublicApi\OrgUnitUserService;
use OrgUnit\User\ilOrgUnitUser;
use OrgUnit\PublicApi\OrgUnitUserService;

abstract class ilMailTemplateContext
{
Expand Down Expand Up @@ -53,44 +53,60 @@ abstract public function getTitle(): string;
abstract public function getDescription(): string;

/**
* @return array{mail_salutation: array{placeholder: string, label: string, supportsNestedPlaceholders?: true}, first_name: array{placeholder: string, label: string}, last_name: array{placeholder: string, label: string}, login: array{placeholder: string, label: string}, title: array{placeholder: string, label: string, supportsCondition: true}, firstname_lastname_superior: array{placeholder: string, label: string}, ilias_url: array{placeholder: string, label: string}, installation_name: array{placeholder: string, label: string}}
* @return array{
* mail_salutation: array{placeholder: string, label: string, requiresRecipient: true, supportsNestedPlaceholders: true},
* first_name: array{placeholder: string, label: string, requiresRecipient: true},
* last_name: array{placeholder: string, label: string, requiresRecipient: true},
* login: array{placeholder: string, label: string, requiresRecipient: true},
* title: array{placeholder: string, label: string, requiresRecipient: true},
* firstname_lastname_superior: array{placeholder: string, label: string, requiresRecipient: true},
* ilias_url: array{placeholder: string, label: string},
* installation_name: array{placeholder: string, label: string}
* }
*/
private function getGenericPlaceholders(): array
{
return [
'mail_salutation' => [
'placeholder' => 'MAIL_SALUTATION',
'label' => $this->getLanguage()->txt('mail_nacc_salutation'),
'requiresRecipient' => true,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • requiresRecipient should be part of the array shape definition (PHPDocs)
  • Maybe there are other placeholders in specific mail template contexts which are also generic (like a course title). Therefore, I carefully ask: Do you see any other approach to achieve the desired behaviour?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the interim fix, we also need to add requiresRecipient to all mail contexts. Right now there’s no reliable way to tell which specific placeholders are user-specific vs. context-specific. Generic ones already have it; specific ones don’t — and some contexts still short-circuit everything when there’s no recipient. Study Programme is the worst offender: if (is_null($recipient)) { return ''; } wipes all placeholders, even ones that only need the object/context (title, link, etc.).

Long-term (planned for ILIAS 12): a proper refactor. Placeholders become small classes implementing interfaces, collected the same way mail signatures already work — placeholder classes + a supports() check on the consumer side. Contexts would declare what they support (installation, user, object/course/study programme, …), and only matching placeholders get resolved. That way we reuse placeholders across contexts instead of reimplementing them in every il*MailTemplateContext.

'supportsNestedPlaceholders' => true,
],
'first_name' => [
'placeholder' => 'FIRST_NAME',
'label' => $this->getLanguage()->txt('firstname'),
'requiresRecipient' => true,
],
'last_name' => [
'placeholder' => 'LAST_NAME',
'label' => $this->getLanguage()->txt('lastname'),
'requiresRecipient' => true,
],
'login' => [
'placeholder' => 'LOGIN',
'label' => $this->getLanguage()->txt('mail_nacc_login'),
'requiresRecipient' => true,
],
'title' => [
'placeholder' => 'TITLE',
'label' => $this->getLanguage()->txt('mail_nacc_title'),
'supportsCondition' => true,
'requiresRecipient' => true,
],
'firstname_lastname_superior' => [
'placeholder' => 'FIRSTNAME_LASTNAME_SUPERIOR',
'label' => $this->getLanguage()->txt('mail_firstname_last_name_superior'),
'requiresRecipient' => true,
],
'ilias_url' => [
'placeholder' => 'ILIAS_URL',
'label' => $this->getLanguage()->txt('mail_nacc_ilias_url'),
'requiresRecipient' => false,
],
'installation_name' => [
'placeholder' => 'INSTALLATION_NAME',
'label' => $this->getLanguage()->txt('mail_nacc_installation_name'),
'requiresRecipient' => false,
],
];
}
Expand All @@ -111,7 +127,12 @@ public function getNestedPlaceholders(): array
}

/**
* @return list<array<string, array{placeholder: string, crs_period_end_mail_placeholder: string}>>
* @return array<string, array{
* placeholder: string,
* label: string,
* requiresRecipient?: bool,
* supportsNestedPlaceholders?: bool
* }>
*/
final public function getPlaceholders(): array
{
Expand All @@ -122,10 +143,62 @@ final public function getPlaceholders(): array
}

/**
* @return list<array<string, array{placeholder: string, crs_period_end_mail_placeholder: string}>>
* @return array<string, array{
* placeholder: string,
* label: string,
* requiresRecipient?: bool,
* supportsNestedPlaceholders?: bool
* }>
*/
abstract public function getSpecificPlaceholders(): array;

public function requiresRecipientByPlaceholderName(string $placeholder_name): bool
{
$found = $this->findPlaceholderByMustacheName($placeholder_name);
if ($found === null) {
return false;
}

return $this->placeholderDefinitionRequiresRecipient($found['definition']);
}

/**
* @return array{key: string, definition: array{
* placeholder: string,
* label: string,
* requiresRecipient?: bool,
* supportsNestedPlaceholders?: bool
* }}|null
*/
private function findPlaceholderByMustacheName(string $placeholder_name): ?array
{
foreach ($this->getPlaceholders() as $key => $placeholder_definition) {
if (strtoupper($placeholder_definition['placeholder']) !== strtoupper($placeholder_name)) {
continue;
}

return [
'key' => (string) $key,
'definition' => $placeholder_definition,
];
}

return null;
}

/**
* @param array{
* placeholder: string,
* label: string,
* requiresRecipient?: bool,
* supportsNestedPlaceholders?: bool
* } $placeholder_definition
*/
private function placeholderDefinitionRequiresRecipient(array $placeholder_definition): bool
{
return $placeholder_definition['requiresRecipient'] ?? false;
}

/**
* @param array<string, mixed> $context_parameters
*/
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion components/ILIAS/Mail/tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
declare(strict_types=1);

require_once __DIR__ . '/../../../../vendor/composer/vendor/autoload.php';
require_once __DIR__ . '/ilMailBaseTest.php';
require_once __DIR__ . '/ilMailBaseTestCase.php';
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

use OrgUnit\PublicApi\OrgUnitUserService;

class ilMailTemplatePlaceholderToEmptyResolverTest extends ilMailBaseTestCase
{
public function testNullRecipientResolvesContextPlaceholdersAndKeepsRecipientDependentNames(): void
{
$lng = $this->getMockBuilder(ilLanguage::class)
->disableOriginalConstructor()
->onlyMethods(['txt', 'loadLanguageModule'])
->getMock();
$lng->method('txt')->willReturnArgument(0);
$this->setGlobalVariable('lng', $lng);
ilDatePresentation::setLanguage($lng);

$env_helper = $this->createMock(ilMailEnvironmentHelper::class);
$env_helper->method('getClientId')->willReturn('phpunit_client');
$env_helper->method('getHttpPath')->willReturn('https://ilias.example/');

$lng_helper = $this->createMock(ilMailLanguageHelper::class);
$lng_helper->method('getCurrentLanguage')->willReturn($lng);

$context = new class (
new OrgUnitUserService(),
$env_helper,
new ilMailUserHelper(),
$lng_helper
) extends ilMailTemplateContext {
public function getId(): string
{
return 'phpunit_context';
}

public function getTitle(): string
{
return 'phpunit';
}

public function getDescription(): string
{
return 'phpunit';
}

public function getSpecificPlaceholders(): array
{
return [
'course_title' => [
'placeholder' => 'COURSE_TITLE',
'label' => 'Course Title',
],
];
}

public function resolveSpecificPlaceholder(
string $placeholder_id,
array $context_parameters,
ilObjUser $recipient = null
): string {
if ('course_title' === $placeholder_id) {
return 'My Course';
}

return '';
}
};

$resolver = new ilMailTemplatePlaceholderResolver(new Mustache_Engine());
$message = 'Hello {{FIRST_NAME}} {{LAST_NAME}}, welcome to {{COURSE_TITLE}} at {{ILIAS_URL}}{{INSTALLATION_NAME}}';

$resolved = $resolver->resolve($context, $message, null, ['ref_id' => 123]);

$this->assertStringContainsString('FIRST_NAME', $resolved);
$this->assertStringContainsString('LAST_NAME', $resolved);
$this->assertStringNotContainsString('{{FIRST_NAME}}', $resolved);
$this->assertStringNotContainsString('{{LAST_NAME}}', $resolved);
$this->assertStringContainsString('My Course', $resolved);
$this->assertStringContainsString('https://ilias.example/', $resolved);
$this->assertStringContainsString('phpunit_client', $resolved);
}
}
1 change: 0 additions & 1 deletion components/ILIAS/Mail/tests/ilMailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,6 @@ static function (string $login): int {
$this->getMockBuilder(ilObjUser::class)->disableOriginalConstructor()->getMock(),
$this->getMockBuilder(ilMailTemplatePlaceholderResolver::class)->disableOriginalConstructor()->getMock(),
null,
null,
$this->getMockBuilder(MailSignatureService::class)->disableOriginalConstructor()->getMock(),
);
}
Expand Down
Loading