Skip to content

Commit

Permalink
Update migration logic
Browse files Browse the repository at this point in the history
  • Loading branch information
taslangraham committed Dec 19, 2024
1 parent 7d5922f commit 4a3e88e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 35 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace PKP\migration\upgrade\v3_5_0;

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;

class I10403_EmailTemplateUserGroupAccess extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$contextDao = \APP\core\Application::getContextDAO();
Schema::create('email_template_user_group_access', function (Blueprint $table) use ($contextDao) {
$table->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');
}
}

0 comments on commit 4a3e88e

Please sign in to comment.