Skip to content

Commit 8b9266f

Browse files
defstatGaziYucel
authored andcommitted
[pkp-lib][main] #10357 Allow null contextId for emailTemplate Repository (#10358)
* #10357 Repository changes to allow null in the contextId * #10357 Specific fix for ValidateRegisteredEmail
1 parent 5035a0f commit 8b9266f

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

Diff for: classes/emailTemplate/Collector.php

+15-12
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Collector implements CollectorInterface
3232
* default.
3333
*/
3434
public ?bool $isModified = null;
35-
public int $contextId;
35+
public ?int $contextId;
3636
public ?array $keys = null;
3737
public ?string $searchPhrase = null;
3838
public ?int $count = null;
@@ -41,7 +41,7 @@ class Collector implements CollectorInterface
4141

4242
public const EMAIL_TEMPLATE_STAGE_DEFAULT = 0;
4343

44-
public function __construct(DAO $dao, int $contextId)
44+
public function __construct(DAO $dao, ?int $contextId)
4545
{
4646
$this->dao = $dao;
4747
$this->contextId = $contextId;
@@ -162,16 +162,19 @@ protected function getDefaultQueryBuilder(): Builder
162162
$q = DB::table('email_templates_default_data as etddata')
163163
->select('email_key')
164164
->selectRaw('NULL as email_id')
165-
->selectRaw($this->contextId . ' as context_id')
166-
->selectRaw('NULL as alternate_to')
167-
168-
->whereNotIn('etddata.email_key', function (Builder $q) {
169-
$q->select('et.email_key')
170-
->from('email_templates as et')
171-
->where('et.context_id', $this->contextId);
172-
})
173-
174-
->when(!is_null($this->keys), function (Builder $q) {
165+
->selectRaw($this->contextId !== null ? $this->contextId . ' as context_id' : 'NULL as context_id')
166+
->selectRaw('NULL as alternate_to');
167+
168+
// Handle case when contextId is provided
169+
if ($this->contextId !== null) {
170+
$q->whereNotIn('etddata.email_key', function (Builder $q) {
171+
$q->select('et.email_key')
172+
->from('email_templates as et')
173+
->where('et.context_id', $this->contextId);
174+
});
175+
}
176+
177+
$q->when(!is_null($this->keys), function (Builder $q) {
175178
return $q->whereIn('email_key', $this->keys);
176179
})
177180

Diff for: classes/emailTemplate/DAO.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function getMany(Collector $query): LazyCollection
130130
/**
131131
* Get a single email template that matches the given key
132132
*/
133-
public function getByKey(int $contextId, string $key): ?EmailTemplate
133+
public function getByKey(?int $contextId = null, string $key): ?EmailTemplate
134134
{
135135
$results = Repo::emailTemplate()->getCollector($contextId)
136136
->filterByKeys([$key])

Diff for: classes/emailTemplate/Repository.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ public function newDataObject(array $params = []): Emailtemplate
5050
}
5151

5252
/** @copydoc DAO::getByKey() */
53-
public function getByKey(int $contextId, string $key): ?EmailTemplate
53+
public function getByKey(?int $contextId = null, string $key): ?EmailTemplate
5454
{
5555
return $this->dao->getByKey($contextId, $key);
5656
}
5757

5858
/** @copydoc DAO::getCollector() */
59-
public function getCollector(int $contextId): Collector
59+
public function getCollector(?int $contextId = null): Collector
6060
{
6161
return app(Collector::class, ['contextId' => $contextId]);
6262
}

Diff for: classes/observers/listeners/ValidateRegisteredEmail.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace PKP\observers\listeners;
1818

19+
use APP\core\Application;
1920
use APP\facades\Repo;
2021
use Illuminate\Events\Dispatcher;
2122
use Illuminate\Support\Facades\Mail;
@@ -70,7 +71,7 @@ protected function manageEmail($event): void
7071
return;
7172
}
7273

73-
$contextId = null;
74+
$contextId = Application::SITE_CONTEXT_ID;
7475

7576
// Create and compile email template
7677
if (get_class($event) === UserRegisteredContext::class) {

0 commit comments

Comments
 (0)