Skip to content

Commit

Permalink
pkp#6432 Retrieve emailTemplates data for supported locales only
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy-1 committed Jan 30, 2023
1 parent 7514066 commit 81b4a38
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 45 deletions.
6 changes: 2 additions & 4 deletions api/v1/emailTemplates/PKPEmailTemplateHandler.php
Expand Up @@ -107,7 +107,7 @@ public function getMany(SlimRequest $slimRequest, Response $response, array $arg
{
$request = $this->getRequest();

$collector = Repo::emailTemplate()->getCollector();
$collector = Repo::emailTemplate()->getCollector($request->getContext()->getId());

// Process query params to format incoming data as needed
foreach ($slimRequest->getQueryParams() as $param => $val) {
Expand All @@ -132,9 +132,7 @@ public function getMany(SlimRequest $slimRequest, Response $response, array $arg

Hook::call('API::emailTemplates::params', [$collector, $slimRequest]);

$emailTemplates = $collector
->filterByContext($request->getContext()->getId())
->getMany();
$emailTemplates = $collector->getMany();

return $response->withJson([
'itemsMax' => $collector->limit(null)->offset(null)->getCount(),
Expand Down
3 changes: 1 addition & 2 deletions classes/decision/steps/Email.php
Expand Up @@ -132,8 +132,7 @@ protected function getEmailTemplates(): array
$emailTemplates->add($emailTemplate);
}
Repo::emailTemplate()
->getCollector()
->filterByContext($context->getId())
->getCollector($context->getId())
->alternateTo([$this->mailable::getEmailTemplateKey()])
->getMany()
->each(fn(EmailTemplate $e) => $emailTemplates->add($e));
Expand Down
27 changes: 7 additions & 20 deletions classes/emailTemplate/Collector.php
Expand Up @@ -29,7 +29,7 @@ class Collector implements CollectorInterface
* default.
*/
public ?bool $isModified = null;
public ?int $contextId = null;
public int $contextId;
public ?array $keys = null;
public ?string $searchPhrase = null;
public ?int $count = null;
Expand All @@ -38,9 +38,10 @@ class Collector implements CollectorInterface

public const EMAIL_TEMPLATE_STAGE_DEFAULT = 0;

public function __construct(DAO $dao)
public function __construct(DAO $dao, int $contextId)
{
$this->dao = $dao;
$this->contextId = $contextId;
}

public function getMany(): LazyCollection
Expand All @@ -59,15 +60,6 @@ public function isModified(?bool $isModified): self
return $this;
}

/**
* Set context filter
*/
public function filterByContext(?int $contextId): self
{
$this->contextId = $contextId;
return $this;
}

/**
* Set email keys filter
*/
Expand Down Expand Up @@ -162,16 +154,13 @@ protected function getDefaultQueryBuilder(): Builder
$q = DB::table('email_templates_default_data as etddata')
->select('email_key')
->selectRaw('NULL as email_id')
->selectRaw('NULL as context_id')
->selectRaw($this->contextId . ' as context_id')
->selectRaw('NULL as alternate_to')

->whereNotIn('etddata.email_key', function (Builder $q) {
$q
->select('et.email_key')
$q->select('et.email_key')
->from('email_templates as et')
->when(!is_null($this->contextId), function (Builder $q) {
$q->where('et.context_id', $this->contextId);
});
->where('et.context_id', $this->contextId);
})

->when(!is_null($this->keys), function (Builder $q) {
Expand Down Expand Up @@ -215,9 +204,7 @@ protected function getCustomQueryBuilder(): Builder
'et.alternate_to',
])

->when(!is_null($this->contextId), function (Builder $q) {
return $q->where('et.context_id', $this->contextId);
})
->where('et.context_id', $this->contextId)

->when(!is_null($this->keys), function (Builder $q) {
return $q->whereIn('et.email_key', $this->keys);
Expand Down
15 changes: 12 additions & 3 deletions classes/emailTemplate/DAO.php
Expand Up @@ -13,16 +13,17 @@

namespace PKP\emailTemplate;

use APP\core\Application;
use APP\core\Services;
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\LazyCollection;
use PKP\core\EntityDAO;
use PKP\core\PKPApplication;
use PKP\db\DAORegistry;
use PKP\db\XMLDAO;
use PKP\facades\Locale;
use APP\facades\Repo;
use PKP\context\Context;
use PKP\site\Site;
use PKP\site\SiteDAO;
use Stringy\Stringy;
Expand Down Expand Up @@ -124,8 +125,7 @@ public function getMany(Collector $query): LazyCollection
*/
public function getByKey(int $contextId, string $key): ?EmailTemplate
{
$results = Repo::emailTemplate()->getCollector()
->filterByContext($contextId)
$results = Repo::emailTemplate()->getCollector($contextId)
->filterByKeys([$key])
->getMany();

Expand Down Expand Up @@ -153,9 +153,18 @@ public function fromRow(object $row): EmailTemplate
/** @var EmailTemplate $emailTemplate */
$emailTemplate = parent::fromRow($row);
$schema = $this->schemaService->get($this->schema);
$contextDao = Application::getContextDAO();

$supportedLocalesJson = $row->context_id === PKPApplication::CONTEXT_SITE ?
DB::table('site')->first()->supported_locales :
DB::table($contextDao->settingsTableName)
->where($contextDao->primaryKeyColumn, $row->context_id)
->where('setting_name', 'supportedLocales')
->value('setting_value');

$rows = DB::table($this->defaultTable)
->where('email_key', '=', $emailTemplate->getData('key'))
->whereIn('locale', json_decode($supportedLocalesJson))
->get();

$props = ['name', 'subject', 'body'];
Expand Down
7 changes: 3 additions & 4 deletions classes/emailTemplate/Repository.php
Expand Up @@ -57,9 +57,9 @@ public function getByKey(int $contextId, string $key): ?EmailTemplate
}

/** @copydoc DAO::getCollector() */
public function getCollector(): Collector
public function getCollector(int $contextId): Collector
{
return app(Collector::class);
return app(Collector::class, ['contextId' => $contextId]);
}

/**
Expand Down Expand Up @@ -189,8 +189,7 @@ public function deleteMany(Collector $collector)
*/
public function restoreDefaults($contextId): array
{
$results = $this->getCollector()
->filterByContext($contextId)
$results = $this->getCollector($contextId)
->isModified(true)
->getMany();

Expand Down
3 changes: 1 addition & 2 deletions classes/mail/Repository.php
Expand Up @@ -115,8 +115,7 @@ public function describeMailable(string $class, int $contextId): array
$data['emailTemplates'] = [];
} else {
$templates = Repo::emailTemplate()
->getCollector()
->filterByContext($contextId)
->getCollector($contextId)
->alternateTo([$class::getEmailTemplateKey()])
->getMany();

Expand Down
3 changes: 1 addition & 2 deletions controllers/grid/queries/form/QueryForm.php
Expand Up @@ -266,8 +266,7 @@ public function fetch($request, $template = null, $display = false, $actionArgs
$data = $mailable->getData();
$defaultTemplate = Repo::emailTemplate()->getByKey($context->getId(), $mailable::getEmailTemplateKey());
$templateKeySubjectPairs = [$mailable::getEmailTemplateKey() => $defaultTemplate->getLocalizedData('name')];
$alternateTemplates = Repo::emailTemplate()->getCollector()
->filterByContext($context->getId())
$alternateTemplates = Repo::emailTemplate()->getCollector($context->getId())
->alternateTo([$mailable::getEmailTemplateKey()])
->getMany();
foreach ($alternateTemplates as $alternateTemplate) {
Expand Down
Expand Up @@ -72,9 +72,8 @@ public function initData()
$context = $request->getContext();
$mailable = $this->getMailable();

$templates = Repo::emailTemplate()->getCollector()
$templates = Repo::emailTemplate()->getCollector($context->getId())
->filterByKeys([ReviewRequest::getEmailTemplateKey(), ReviewRequestSubsequent::getEmailTemplateKey()])
->filterByContext($context->getId())
->getMany()
->mapWithKeys(function(EmailTemplate $item, int $key) use ($mailable) {
return [$item->getData('key') => Mail::compileParams($item->getLocalizedData('body'), $mailable->viewData)];
Expand Down Expand Up @@ -241,8 +240,7 @@ protected function getEmailTemplates(): array
ReviewRequestSubsequent::getEmailTemplateKey()
);

$alternateTemplates = Repo::emailTemplate()->getCollector()
->filterByContext(Application::get()->getRequest()->getContext()->getId())
$alternateTemplates = Repo::emailTemplate()->getCollector(Application::get()->getRequest()->getContext()->getId())
->alternateTo([ReviewRequestSubsequent::getEmailTemplateKey()])
->getMany();

Expand Down
3 changes: 1 addition & 2 deletions controllers/grid/users/reviewer/form/ReviewerForm.php
Expand Up @@ -476,8 +476,7 @@ protected function getEmailTemplates(): array
);
$templateKeys = [ReviewRequest::getEmailTemplateKey() => $defaultTemplate->getLocalizedData('name')];

$alternateTemplates = Repo::emailTemplate()->getCollector()
->filterByContext(Application::get()->getRequest()->getContext()->getId())
$alternateTemplates = Repo::emailTemplate()->getCollector(Application::get()->getRequest()->getContext()->getId())
->alternateTo([ReviewRequest::getEmailTemplateKey()])
->getMany();

Expand Down
Expand Up @@ -100,8 +100,7 @@ public function fetch($request, $template = null, $display = false)
$data = $mailable->getData();
$defaultTemplate = Repo::emailTemplate()->getByKey($context->getId(), $mailable::getEmailTemplateKey());
$templates = [$mailable::getEmailTemplateKey() => $defaultTemplate->getLocalizedData('name')];
$alternateTemplates = Repo::emailTemplate()->getCollector()
->filterByContext($context->getId())
$alternateTemplates = Repo::emailTemplate()->getCollector($context->getId())
->alternateTo([$mailable::getEmailTemplateKey()])
->getMany();
foreach ($alternateTemplates as $alternateTemplate) {
Expand Down

0 comments on commit 81b4a38

Please sign in to comment.