Skip to content

Commit

Permalink
Mail translations 2nd attempt
Browse files Browse the repository at this point in the history
* Remove *-custom.tpl from repo - should only be provided by the user
* Ignore custom translation files
* defaultPath is a fallback path - The name is misleading, as it's not the default.language setting
* Remove dead code
  * DisplayLocalized in SmartyPage was never called
  * DisplayLocalized in Page was only a copy, which is still called, but the languageCode parameter is not used.
* Fix save custom email template for selected language - fixes #424
* Fallback to default lang for missing custom templates ..but only when custom template is available for default lang.
* Add config description
* Change ReservationShareMail property name - It conflicts with the now protected email property
* Fix unit test for updating email template
* closes #416
  • Loading branch information
Gerd Katzenbeisser committed Nov 2, 2024
1 parent d565509 commit 507817e
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 188 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# Customization Files
/Web/custom-favicon.png
/Web/img/custom-logo.png
lang/*/*-custom.tpl

# Upload Directories
/uploads/*
Expand Down
7 changes: 7 additions & 0 deletions Pages/Admin/ManageEmailTemplatesPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function SetSaveResult($saveResult);
* @return string
*/
public function GetUpdatedTemplateName();

public function GetUpdatedLanguage(): string;
}

class ManageEmailTemplatesPage extends ActionPage implements IManageEmailTemplatesPage
Expand Down Expand Up @@ -75,6 +77,11 @@ public function ProcessPageLoad()
$this->Display('Admin/Configuration/manage_email_templates.tpl');
}

public function GetUpdatedLanguage(): string
{
return $this->GetForm(FormKeys::EMAIL_TEMPLATE_LANGUAGE);
}

public function GetLanguage()
{
return $this->GetQuerystring(QueryStringKeys::LANGUAGE);
Expand Down
15 changes: 0 additions & 15 deletions Pages/HelpPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,5 @@ public function PageLoad()
$this->Set('ServerTimezone', date_default_timezone_get());

$this->DisplayLocalized('support-and-credits.tpl');

// $helpType = $this->GetQuerystring('ht');
//
// if ($helpType == 'about')
// {
//
// }
// else if ($helpType == 'admin')
// {
// $this->DisplayLocalized('help-admin.tpl');
// }
// else
// {
// $this->DisplayLocalized('help.tpl');
// }
}
}
7 changes: 2 additions & 5 deletions Pages/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,10 @@ protected function DisplayCsv($templateName, $fileName)

/**
* @param string $templateName
* @param null $languageCode uses current language is nothing is passed in
*/
protected function DisplayLocalized($templateName, $languageCode = null)
protected function DisplayLocalized($templateName)
{
if (empty($languageCode)) {
$languageCode = $this->GetVar('CurrentLanguage');
}
$languageCode = $this->GetVar('CurrentLanguage');
$localizedPath = ROOT_DIR . 'lang/' . $languageCode;
$defaultPath = ROOT_DIR . 'lang/en_us/';

Expand Down
2 changes: 1 addition & 1 deletion Presenters/Admin/ManageEmailTemplatesPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function UpdateEmailTemplate()
try {
Log::Debug('Updating email template. Template=%s', $templateName);

$templatePath = Paths::EmailTemplates($this->GetSelectedLanguage());
$templatePath = Paths::EmailTemplates($this->page->GetUpdatedLanguage());
$saveResult = $this->filesystem->Save($templatePath, str_replace('.tpl', '-custom.tpl', $templateName), $this->page->GetTemplateContents());

$this->filesystem->FlushSmartyCache();
Expand Down
3 changes: 2 additions & 1 deletion config/config.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
$conf['settings']['default.page.size'] = '50'; // number of records per page
$conf['settings']['enable.email'] = 'true'; // global configuration to enable if any emails will be sent
$conf['settings']['default.language'] = 'en_us'; // find your language in the lang directory
$conf['settings']['enforce.custom.mail.template'] = 'false'; // Fallback to default.language for missing custom templates, bu only when custom template is available for default.language
$conf['settings']['script.url'] = ''; // public URL to the Web directory of this instance. this is the URL that appears when you are logging in. leave http: or https: off to auto-detect
$conf['settings']['image.upload.directory'] = 'Web/uploads/images'; // full or relative path to where images will be stored
$conf['settings']['image.upload.url'] = 'uploads/images'; // full or relative path to show uploaded images from
Expand Down Expand Up @@ -212,7 +213,7 @@
* Error logging
*/
$conf['settings']['logging']['folder'] = '/var/log/librebooking/log'; //Absolute path to folder were the log will be written, writing permissions to the folder are required
$conf['settings']['logging']['level'] = 'none'; //Set to none disable logs, error to only log errors or debug to log all messages to the app.log file
$conf['settings']['logging']['level'] = 'none'; //Set to none disable logs, error to only log errors or debug to log all messages to the app.log file
$conf['settings']['logging']['sql'] = 'false'; //Set to true no enable the creation of and sql.log file

// IN THE REDIRECT URI'S (OF THE AUTHENTICATION YOU ARE USING) YOU NEED TO ADD THE PATH FROM THE WEBSITE DOMAIN TO THE
Expand Down
11 changes: 0 additions & 11 deletions lang/du_nl/AccountCreation-custom.tpl

This file was deleted.

11 changes: 0 additions & 11 deletions lang/en_us/AccountCreation-custom.tpl

This file was deleted.

11 changes: 0 additions & 11 deletions lang/es/AccountCreation-custom.tpl

This file was deleted.

11 changes: 0 additions & 11 deletions lang/fr_fr/AccountCreation-custom.tpl

This file was deleted.

18 changes: 0 additions & 18 deletions lang/pt_br/AccountCreation-custom.tpl

This file was deleted.

53 changes: 24 additions & 29 deletions lib/Common/SmartyPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,46 +76,41 @@ public function AddTemplateDirectory($templateDirectory)
}

/**
* Fetches template in a specific language. A custom template file might override the default template.
* In case the template is not available in the target language it will fall back to en_us.
* @param string $templateName
* @param null $languageCode uses current language is nothing is passed in
*/
public function DisplayLocalized($templateName, $languageCode = null)
{
if (empty($languageCode)) {
$languageCode = $this->getTemplateVars('CurrentLanguage');
}
$localizedPath = ROOT_DIR . 'lang/' . $languageCode;
$defaultPath = ROOT_DIR . 'lang/en_us/' . $templateName;

if (file_exists($localizedPath . '/' . $templateName)) {
$this->AddTemplateDirectory($localizedPath);
} else {
$this->AddTemplateDirectory($defaultPath);
}

$this->Display($templateName);
}

/**
* @param string $templateName
* @param null $languageCode uses current language is nothing is passed in
* @param string $languageCode Will be set to template var CurrentLanguage if null
* @param bool $enforceCustomTemplate if true uses custom language from default language
* if custom template of the target language is not available.
* @return string
*/
public function FetchLocalized($templateName, $languageCode = null)
public function FetchLocalized($templateName, bool $enforceCustomTemplate, string $languageCode = null)
{
if (empty($languageCode)) {
if ($languageCode == null) {
$languageCode = $this->getTemplateVars('CurrentLanguage');
}
$localizedPath = ROOT_DIR . 'lang/' . $languageCode;
$defaultPath = ROOT_DIR . 'lang/en_us/';
$langPath = ROOT_DIR . 'lang/';
$localizedPath = $langPath . $languageCode;
$customTemplateName = str_replace('.tpl', '-custom.tpl', $templateName);
$hasCustomTemplate = file_exists($localizedPath . '/' . $customTemplateName);

if ($enforceCustomTemplate && !$hasCustomTemplate) {
$defaultLanguageCode = Configuration::Instance()->GetKey(ConfigKeys::LANGUAGE);
$defaultLocalizedPath = $langPath . $defaultLanguageCode;
$hasCustomDefaultTemplate = file_exists($defaultLocalizedPath . '/' . $customTemplateName);
if ($languageCode != $defaultLanguageCode && $hasCustomDefaultTemplate) {
$hasCustomTemplate = true;
$localizedPath = $defaultLocalizedPath;
}
}

if (file_exists($localizedPath . '/' . $templateName) || file_exists($localizedPath . '/' . $customTemplateName)) {
if (file_exists($localizedPath . '/' . $templateName) || $hasCustomTemplate) {
$path = $localizedPath;
$this->AddTemplateDirectory($localizedPath);
} else {
$path = $defaultPath;
$this->AddTemplateDirectory($defaultPath);
// Fallback path
$path = ROOT_DIR . 'lang/en_us/';
$this->AddTemplateDirectory($path);
}

if (file_exists($path . '/' . $customTemplateName)) {
Expand Down
1 change: 1 addition & 0 deletions lib/Config/ConfigKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ConfigKeys
public const HOME_URL = 'home.url';
public const INACTIVITY_TIMEOUT = 'inactivity.timeout';
public const LANGUAGE = 'default.language';
public const ENFORCE_CUSTOM_MAIL_TEMPLATE = 'enforce.custom.mail.template';
public const LOGOUT_URL = 'logout.url';
public const NAME_FORMAT = 'name.format';
public const SCRIPT_URL = 'script.url';
Expand Down
7 changes: 5 additions & 2 deletions lib/Email/EmailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ abstract class EmailMessage implements IEmailMessage
/**
* @var SmartyPage
*/
private $email;
protected $email;
/**
* @var string|null
*/
Expand All @@ -15,8 +15,11 @@ abstract class EmailMessage implements IEmailMessage
*/
private $attachmentFileName;

protected bool $enforceCustomTemplate;

protected function __construct($languageCode = null)
{
$this->enforceCustomTemplate = Configuration::Instance()->GetKey(ConfigKeys::ENFORCE_CUSTOM_MAIL_TEMPLATE, new BooleanConverter());
$this->email = new SmartyPage($resources);
$resources = Resources::GetInstance();
if (!empty($languageCode)) {
Expand All @@ -37,7 +40,7 @@ protected function Set($var, $value)
protected function FetchTemplate($templateName, $includeHeaders = true)
{
$header = $includeHeaders ? $this->email->fetch('Email/emailheader.tpl') : '';
$body = $this->email->FetchLocalized($templateName);
$body = $this->email->FetchLocalized($templateName, $this->enforceCustomTemplate);
$footer = $includeHeaders ? $this->email->fetch('Email/emailfooter.tpl') : '';

return $header . $body . $footer;
Expand Down
2 changes: 1 addition & 1 deletion lib/Email/Messages/ReportEmailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct($report, $definition, $toAddress, $reportUser, $sele
$this->Set('Definition', $definition);
$this->Set('Report', $report);
$this->Set('ReportCsvColumnView', new ReportCsvColumnView($selectedColumns));
$contents = $this->FetchTemplate('Reports/custom-csv.tpl', false);
$contents = $this->email->FetchLocalized('Reports/custom-csv.tpl', false);

$name = $report->ReportName();
if (!empty($name)) {
Expand Down
8 changes: 4 additions & 4 deletions lib/Email/Messages/ReservationSeriesEndingEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ReservationSeriesEndingEmail extends EmailMessage
/**
* @var string
*/
private $email;
private $to;

/**
* @var ExistingReservationSeries
Expand All @@ -24,19 +24,19 @@ class ReservationSeriesEndingEmail extends EmailMessage
*/
private $currentInstance;

public function __construct(ExistingReservationSeries $reservationSeries, $language, $timezone, $email)
public function __construct(ExistingReservationSeries $reservationSeries, $language, $timezone, $to)
{
parent::__construct($language);

$this->reservationSeries = $reservationSeries;
$this->timezone = $timezone;
$this->email = $email;
$this->to = $to;
$this->currentInstance = $this->reservationSeries->CurrentInstance();
}

public function To()
{
return [new EmailAddress($this->email)];
return [new EmailAddress($this->to)];
}

public function Subject()
Expand Down
6 changes: 3 additions & 3 deletions lib/Email/Messages/ReservationShareEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ReservationShareEmail extends ReservationEmailMessage
/**
* @var string
*/
private $email;
private $emailToShare;

public function __construct(User $reservationOwner, $emailToShare, ReservationSeries $reservationSeries, IAttributeRepository $attributeRepository, IUserRepository $userRepository)
{
Expand All @@ -16,12 +16,12 @@ public function __construct(User $reservationOwner, $emailToShare, ReservationSe
$this->reservationOwner = $reservationOwner;
$this->reservationSeries = $reservationSeries;
$this->timezone = $reservationOwner->Timezone();
$this->email = $emailToShare;
$this->emailToShare = $emailToShare;
}

public function To()
{
return [new EmailAddress($this->email)];
return [new EmailAddress($this->emailToShare)];
}

public function Subject()
Expand Down
10 changes: 5 additions & 5 deletions lib/Email/Messages/ResourceStatusChangeEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@

class ResourceStatusChangeEmail extends EmailMessage
{
private $email;
private $to;
/**
* @var BookableResource
*/
private $resource;
private $message;

/**
* @param string $email
* @param string $to
* @param BookableResource $resource
* @param string $message
* @param string $language
*/
public function __construct($email, BookableResource $resource, $message, $language)
public function __construct($to, BookableResource $resource, $message, $language)
{
parent::__construct($language);
$this->email = $email;
$this->to = $to;
$this->resource = $resource;
$this->message = $message;
}

public function To()
{
return new EmailAddress($this->email);
return new EmailAddress($this->to);
}

public function Subject()
Expand Down
1 change: 1 addition & 0 deletions lib/Server/FormKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ private function __construct()
public const ENABLE_AUTO_RELEASE = 'ENABLE_AUTO_RELEASE';
public const EMAIL_CONTENTS = 'EMAIL_CONTENTS';
public const EMAIL_TEMPLATE_NAME = 'EMAIL_TEMPLATE_NAME';
public const EMAIL_TEMPLATE_LANGUAGE = 'EMAIL_TEMPLATE_LANGUAGE';

public const FIRST_NAME = 'fname';
public const FAVICON_FILE = 'FAVICON_FILE';
Expand Down
Loading

0 comments on commit 507817e

Please sign in to comment.