diff --git a/classes/migration/upgrade/v3_5_0/I10403_EmailTemplateRoleAccess.php b/classes/migration/upgrade/v3_5_0/I10403_EmailTemplateRoleAccess.php deleted file mode 100644 index cbfc1186e74..00000000000 --- a/classes/migration/upgrade/v3_5_0/I10403_EmailTemplateRoleAccess.php +++ /dev/null @@ -1,35 +0,0 @@ -bigInteger('email_template_user_group_access_id')->autoIncrement(); - $table->string('email_key', 255); - $table->bigInteger('context_id'); - $table->bigInteger('user_group_id')->nullable(); - - $table->foreign('context_id')->references('journal_id')->on('journals')->onDelete('cascade'); - $table->foreign('user_group_id')->references('user_group_id')->on('user_groups')->onDelete('cascade'); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::drop('email_template_user_group_access'); - } -} diff --git a/classes/migration/upgrade/v3_5_0/I10403_EmailTemplateUserGroupAccess.php b/classes/migration/upgrade/v3_5_0/I10403_EmailTemplateUserGroupAccess.php new file mode 100644 index 00000000000..fb4b75a2e55 --- /dev/null +++ b/classes/migration/upgrade/v3_5_0/I10403_EmailTemplateUserGroupAccess.php @@ -0,0 +1,75 @@ +bigInteger('email_template_user_group_access_id')->autoIncrement(); + $table->string('email_key', 255); + $table->bigInteger('context_id'); + $table->bigInteger('user_group_id')->nullable(); + + $table->foreign('context_id')->references($contextDao->primaryKeyColumn)->on($contextDao->tableName)->onDelete('cascade'); + $table->foreign('user_group_id')->references('user_group_id')->on('user_groups')->onDelete('cascade'); + }); + + $contextIds = array_map(fn (Context $context) => $context->getId(), $contextDao->getAll()->toArray()); + + if (!empty($contextIds)) { + DB::table('email_template_user_group_access')->select('*')->get(); + + $defaultTemplateKeys = DB::table('email_templates_default_data')->select()->pluck('email_key')->all(); + $alternateTemplates = DB::table('email_templates')->select(['context_id', 'email_key'])->get()->toArray(); + + $data = []; + + // Record any existing default templates as unrestricted for existing Contexts + foreach ($defaultTemplateKeys as $defaultTemplateKey) { + foreach ($contextIds as $contextId) { + $data[] = [ + 'email_key' => $defaultTemplateKey, + 'context_id' => $contextId, + 'user_group_id' => null + ]; + } + } + + // For any existing alternate template, register it as unrestricted within its assigned context + foreach ($alternateTemplates as $template) { + foreach ($contextIds as $contextId) { + if ($contextId === $template->context_id) { + $data[] = [ + 'email_key' => $template->email_key, + 'context_id' => $contextId, + 'user_group_id' => null + ]; + } + } + } + + EmailTemplateAccessGroup::insert($data); + } + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::drop('email_template_user_group_access'); + } +}