Skip to content

Commit

Permalink
Add flag for to increase method usage compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
taslangraham committed Nov 20, 2024
1 parent 02159c5 commit 3cb222d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
27 changes: 16 additions & 11 deletions classes/emailTemplate/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,16 @@ public function getMainEmailTemplatesFilename()
* skipping others
* @param bool $skipExisting If true, do not install email templates
* that already exist in the database
*
* @param bool $recordTemplateGroupAccess - If true, records the templates as unrestricted.
* By default, it is set to false to ensure compatibility with older processes (e.g., migrations)
* where the `email_template_user_group_access` table may not exist at the time of execution.
*/
public function installEmailTemplates(
string $templatesFile,
array $locales = [],
?string $emailKey = null,
bool $skipExisting = false
bool $skipExisting = false,
bool $recordTemplateGroupAccess = false
): bool {
$xmlDao = new XMLDAO();
$data = $xmlDao->parseStruct($templatesFile, ['email']);
Expand Down Expand Up @@ -282,16 +285,18 @@ public function installEmailTemplates(
}
}

// Default to true if `isUnrestricted` is not set.
$isUnrestricted = $attrs['isUnrestricted'] ?? '1';
if ($recordTemplateGroupAccess) {
// Default to true if `isUnrestricted` is not set.
$isUnrestricted = $attrs['isUnrestricted'] ?? '1';

if ($isUnrestricted !== '1' && $isUnrestricted !== '0') {
throw new Exception('Invalid value given for the `isUnrestricted` attribute on the ' . $attrs['key'] . ' template.');
}
if ($isUnrestricted !== '1' && $isUnrestricted !== '0') {
throw new Exception('Invalid value given for the `isUnrestricted` attribute on the ' . $attrs['key'] . ' template.');
}

$contextIds = app()->get('context')->getIds();
foreach ($contextIds as $contextId) {
Repo::emailTemplate()->markTemplateAsUnrestricted($attrs['key'], (bool)$isUnrestricted, $contextId);
$contextIds = app()->get('context')->getIds();
foreach ($contextIds as $contextId) {
Repo::emailTemplate()->markTemplateAsUnrestricted($attrs['key'], (bool)$isUnrestricted, $contextId);
}
}
}
return true;
Expand Down Expand Up @@ -421,7 +426,7 @@ public function installAlternateEmailTemplates(int $contextId, ?string $emailKey
'Tried to install email template as an alternate to `' . $alternateTo . '`, but no default template exists with this key. Installing ' . $alternateTo . ' email template first',
E_USER_WARNING
);
$this->installEmailTemplates(Repo::emailTemplate()->dao->getMainEmailTemplatesFilename(), [], $alternateTo);
$this->installEmailTemplates(Repo::emailTemplate()->dao->getMainEmailTemplatesFilename(), [], $alternateTo, false, true);
}

DB::table($this->table)->insert([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use PKP\context\Context;
use PKP\emailTemplate\EmailTemplateAccessGroup;
use PKP\migration\Migration;

Expand Down
2 changes: 1 addition & 1 deletion classes/plugins/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ public function installEmailTemplates($hookName, $args)
}
// Localized data is needed by the email installation
$this->addLocaleData();
$status = Repo::emailTemplate()->dao->installEmailTemplates($this->getInstallEmailTemplatesFile(), $locales, null, true);
$status = Repo::emailTemplate()->dao->installEmailTemplates($this->getInstallEmailTemplatesFile(), $locales, null, true, true);

if ($status === false) {
// The template file seems to be invalid.
Expand Down
4 changes: 3 additions & 1 deletion tools/installEmailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public function execute()
Repo::emailTemplate()->dao->installEmailTemplates(
Repo::emailTemplate()->dao->getMainEmailTemplatesFilename(),
$locales,
$this->_emailKey
$this->_emailKey,
false,
true
);
}
}
Expand Down

0 comments on commit 3cb222d

Please sign in to comment.