Skip to content

Commit

Permalink
Revert " PICKME Refactor Email system to builder pattern"
Browse files Browse the repository at this point in the history
  • Loading branch information
effgarces authored Oct 16, 2024
1 parent a7adc75 commit 0cdfdbc
Show file tree
Hide file tree
Showing 19 changed files with 134 additions and 70 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
# Customization Files
/Web/custom-favicon.png
/Web/img/custom-logo.png
lang/*/*-custom.tpl

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

public function GetUpdatedLanguage(): string;
}

class ManageEmailTemplatesPage extends ActionPage implements IManageEmailTemplatesPage
Expand Down Expand Up @@ -77,11 +75,6 @@ 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: 15 additions & 0 deletions Pages/HelpPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,20 @@ 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: 5 additions & 2 deletions Pages/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,13 @@ protected function DisplayCsv($templateName, $fileName)

/**
* @param string $templateName
* @param null $languageCode uses current language is nothing is passed in
*/
protected function DisplayLocalized($templateName)
protected function DisplayLocalized($templateName, $languageCode = null)
{
$languageCode = $this->GetVar('CurrentLanguage');
if (empty($languageCode)) {
$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->page->GetUpdatedLanguage());
$templatePath = Paths::EmailTemplates($this->GetSelectedLanguage());
$saveResult = $this->filesystem->Save($templatePath, str_replace('.tpl', '-custom.tpl', $templateName), $this->page->GetTemplateContents());

$this->filesystem->FlushSmartyCache();
Expand Down
1 change: 0 additions & 1 deletion config/config.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
$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
11 changes: 11 additions & 0 deletions lang/du_nl/AccountCreation-custom.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p>{$To},</p>

<p>Een nieuwe gebruiker is geregistreerd met de volgende informatie:<br/>
Email: {$EmailAddress}<br/>
Naam: {$FullName}<br/>
Telefoonnummer: {$Phone}<br/>
Organisatie: {$Organization}<br/>
Positie: {$Position}</p>
{if !empty($CreatedBy)}
Gemaakt door: {$CreatedBy}
{/if}
11 changes: 11 additions & 0 deletions lang/en_us/AccountCreation-custom.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p>{$To},</p>

<p>A new user has registered with the following information:<br/>
Email: {$EmailAddress}<br/>
Name: {$FullName}<br/>
Phone: {$Phone}<br/>
Organization: {$Organization}<br/>
Position: {$Position}</p>
{if !empty($CreatedBy)}
Created by: {$CreatedBy}
{/if}
11 changes: 11 additions & 0 deletions lang/es/AccountCreation-custom.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p>{$To},</p>

<p>Se ha registrado un nuevo usuario con la siguiente información:<br/>
Correo electrónico: {$EmailAddress}<br/>
Nombre: {$FullName}<br/>
Teléfono: {$Phone}<br/>
Organización: {$Organization}<br/>
Cargo: {$Position}</p>
{if !empty($CreatedBy)}
Creado por: {$CreatedBy}
{/if}
11 changes: 11 additions & 0 deletions lang/fr_fr/AccountCreation-custom.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p>{$To},</p>

<p>Un nouvel utilisateur s'est enregistré avec les informations suivantes:<br/>
Email: {$EmailAddress}<br/>
Nom: {$FullName}<br/>
Téléphone: {$Phone}<br/>
Organisation: {$Organization}<br/>
Position: {$Position}</p>
{if !empty($CreatedBy)}
Créé par: {$CreatedBy}
{/if}
18 changes: 18 additions & 0 deletions lang/pt_br/AccountCreation-custom.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<p>
{$To},
<br />
</p>

<p>
Um novo usuário se registrou com as seguintes informações:
<br />
E-mail: {$EmailAddress}
<br />
Nome: {$FullName}
<br />
Organização: {$Organization}
{if !empty($CreatedBy)}
<br />
Criada por: {$CreatedBy}
{/if}
</p>
53 changes: 29 additions & 24 deletions lib/Common/SmartyPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,41 +76,46 @@ 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 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.
* @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
* @return string
*/
public function FetchLocalized($templateName, bool $enforceCustomTemplate, string $languageCode = null)
public function FetchLocalized($templateName, $languageCode = null)
{
if ($languageCode == null) {
if (empty($languageCode)) {
$languageCode = $this->getTemplateVars('CurrentLanguage');
}
$langPath = ROOT_DIR . 'lang/';
$localizedPath = $langPath . $languageCode;
$localizedPath = ROOT_DIR . 'lang/' . $languageCode;
$defaultPath = ROOT_DIR . 'lang/en_us/';
$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) || $hasCustomTemplate) {
if (file_exists($localizedPath . '/' . $templateName) || file_exists($localizedPath . '/' . $customTemplateName)) {
$path = $localizedPath;
$this->AddTemplateDirectory($localizedPath);
} else {
// Fallback path
$path = ROOT_DIR . 'lang/en_us/';
$this->AddTemplateDirectory($path);
$path = $defaultPath;
$this->AddTemplateDirectory($defaultPath);
}

if (file_exists($path . '/' . $customTemplateName)) {
Expand Down
1 change: 0 additions & 1 deletion lib/Config/ConfigKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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: 2 additions & 5 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
*/
protected $email;
private $email;
/**
* @var string|null
*/
Expand All @@ -15,11 +15,8 @@ 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 @@ -40,7 +37,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, $this->enforceCustomTemplate);
$body = $this->email->FetchLocalized($templateName);
$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->email->FetchLocalized('Reports/custom-csv.tpl', false);
$contents = $this->FetchTemplate('Reports/custom-csv.tpl', false);

$name = $report->ReportName();
if (!empty($name)) {
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 $emailToShare;
private $email;

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->emailToShare = $emailToShare;
$this->email = $emailToShare;
}

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

public function Subject()
Expand Down
1 change: 0 additions & 1 deletion lib/Server/FormKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ 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
9 changes: 1 addition & 8 deletions tests/Presenters/Admin/ManageEmailTemplatesPresenterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ public function testUpdatesEmailTemplate()
$this->page->_UpdatedTemplateName = $templateName;
$this->page->_TemplateContents = $contents;
$this->page->_Language = 'en_us';
$this->page->_UpdatedLanguage = 'cz';

$this->presenter->UpdateEmailTemplate();

$this->assertEquals($contents, $this->fileSystem->_AddedFileContents);
$this->assertEquals(Paths::EmailTemplates('cz'), $this->fileSystem->_AddedFilePath);
$this->assertEquals(Paths::EmailTemplates('en_us'), $this->fileSystem->_AddedFilePath);
$this->assertEquals('template-custom.tpl', $this->fileSystem->_AddedFileName);
}
}
Expand All @@ -92,7 +91,6 @@ class FakeManageEmailTemplatesPage extends ManageEmailTemplatesPage
public $_BoundTemplateContents;
public $_TemplateContents;
public $_Language;
public $_UpdatedLanguage;
public $_SaveResult = true;

public function BindTemplateNames($templates)
Expand Down Expand Up @@ -125,11 +123,6 @@ public function GetLanguage()
return $this->_Language;
}

public function GetUpdatedLanguage(): string
{
return $this->_UpdatedLanguage;
}

public function SetSaveResult($saveResult)
{
$this->_SaveResult = $saveResult;
Expand Down
30 changes: 15 additions & 15 deletions tpl/Admin/Configuration/manage_email_templates.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

</div>

<form role="form" id="updateEmailForm" ajaxAction="{EmailTemplatesActions::Update}" method="post">
<div class="form-group col-sm-4 col-xs-6">
<select id="languageOpts" {formname key=EMAIL_TEMPLATE_LANGUAGE} title="{translate key=Language}" class="form-select">
{foreach from=$Languages item=language}
<option value="{$language->LanguageCode}" {if $Language==$language->LanguageCode} selected="selected"
{/if}>{$language->DisplayName}</option>
{/foreach}
</select>
</div>
<div class="form-group col-sm-4 col-6">
<select id="languageOpts" title="{translate key=Language}" class="form-select">
{foreach from=$Languages item=language}
<option value="{$language->LanguageCode}" {if $Language==$language->LanguageCode}selected="selected"
{/if}>{$language->DisplayName}</option>
{/foreach}
</select>
</div>

<div id="editEmailSection" class="d-none">
<div class="mb-2">
<div class="form-group">
<div id="editEmailSection" class="d-none">
<div class="mb-2">
<form role="form" id="updateEmailForm" ajaxAction="{EmailTemplatesActions::Update}" method="post">
<div class="form-group">
<textarea id="templateContents" {formname key=EMAIL_CONTENTS}
title="{translate key=EmailTemplate}" class="form-control mb-2" rows="20"
style="width:100%"></textarea>
Expand All @@ -41,10 +41,10 @@
value="{translate key=ReloadOriginalContents}" />
</div>

<input type="hidden" id="templatePath" {formname key=EMAIL_TEMPLATE_NAME} />
{csrf_token}
<input type="hidden" id="templatePath" {formname key=EMAIL_TEMPLATE_NAME} />
{csrf_token}
</form>
</div>
</form>

<div id="updateSuccess" class="alert alert-success" style="display:none;">
<i class="bi bi-check-circle me-1"></i> {translate key=UpdateEmailTemplateSuccess}
Expand Down

0 comments on commit 0cdfdbc

Please sign in to comment.