Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

defstat
Copy link
Contributor

@defstat defstat commented Sep 2, 2024

No description provided.

})

->when(!is_null($this->keys), function (Builder $q) {
->selectRaw($this->contextId !== null ? $this->contextId . ' as context_id' : 'NULL as context_id')
Copy link
Member

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:

$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 NULLs 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.

Copy link
Contributor Author

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:

  1. When contextId is provided: In this case, we would join the email_templates table within the Collector's query to retrieve data specific to that context.

  2. When contextId is not provided: Here, we would use Application::SITE_CONTEXT_ID as the contextId. In this scenario, we retrieve default data from email_templates_default_data, bypassing any need for contextId in the Collector'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.

Copy link
Member

@asmecher asmecher left a 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!

Copy link
Collaborator

@Vitaliy-1 Vitaliy-1 left a 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants