-
Notifications
You must be signed in to change notification settings - Fork 445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[pkp-lib][main] #10357 Allow null contextId for emailTemplate Repository #10358
base: main
Are you sure you want to change the base?
Conversation
}) | ||
|
||
->when(!is_null($this->keys), function (Builder $q) { | ||
->selectRaw($this->contextId !== null ? $this->contextId . ' as context_id' : 'NULL as context_id') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather avoid string concatenation of queries as much as possible; Jonas had an approach that we should also follow for consistency. See:
pkp-lib/classes/userGroup/Collector.php
Line 302 in 1b6f1b5
$q->whereIn(DB::raw('COALESCE(ug.context_id, 0)'), array_map(intval(...), $this->contextIds)); |
We risk confusing null
(constraint not specified to the collector) with SITE_CONTEXT_ID
(used to be 0
, now null
). In cases like this it's better to have the caller provide a list of context IDs, with the SITE_ID_CONTEXT
constant being one of the available options. Then, per the link above, map the array to intval
when the query is being built and use COALESCE(context_id, 0)
in the SQL in order to map NULL
s to 0
there.
The idea is that calling code can use:
$collector->filterByContextIds([$journal->getId()]); // Filter by a single journal ID -- note the array syntax
...or...
$collector->filterByContextIds(null); // Do not filter by context IDs -- clear any pre-existing filter
...or...
$collector->filterByContextIds([SITE_ID_CONTEXT]); // Filter for templates that are site-wide -- note the array syntax
The need to specify the context ID in the constructor in this class is non-standard. I'd suggest not requiring any filters in the constructor, and instead using fluent functions to chain constraints like we do elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@asmecher, thank you for the feedback. I understand the importance of avoiding query string concatenation and aligning with Jonas’s approach for consistency. Here’s my take on handling contextId
in email templates:
I believe that when we’re dealing with emailTemplates
, we are specifically referring to context-level email templates. The user, as far as I know, cannot modify the default email template at the Site level. This means that the email_templates
table shouldn't typically contain rows with null
in the context_id
column.
Given this, we seem to have two primary cases to consider:
-
When
contextId
is provided: In this case, we would join theemail_templates
table within theCollector
's query to retrieve data specific to that context. -
When
contextId
is not provided: Here, we would useApplication::SITE_CONTEXT_ID
as thecontextId
. In this scenario, we retrieve default data fromemail_templates_default_data
, bypassing any need forcontextId
in theCollector
's query.
The root of this issue lies in the change of Application::SITE_CONTEXT_ID
from 0
to null
to align with updated database standards and ensure consistency with other cases. This shift led to back-office issues, as contextId
was previously non-nullable in both Repo
and Collector
. Consequently, we needed to adjust the code to handle null
values correctly.
However, while we could potentially introduce a specific Repo
function to handle Site-level email templates, I believe that would be more appropriate as part of a broader future task rather than within the scope of this issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment with some suggestions to standardize this against other similar cases!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Don't have anything to add here, thanks
No description provided.