From c6cf25827d199186328919a9e42804033deefa6b Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 10 Mar 2022 12:58:10 -0500 Subject: [PATCH] Switch newsletter category to one-time insert instead of a managed entity --- CRM/Mosaico/Upgrader.php | 85 +++++++++++++++++++++-- info.xml | 2 +- managed/Mailing_Template_Category.mgd.php | 31 +-------- 3 files changed, 81 insertions(+), 37 deletions(-) diff --git a/CRM/Mosaico/Upgrader.php b/CRM/Mosaico/Upgrader.php index 46738ce04..9b4228860 100644 --- a/CRM/Mosaico/Upgrader.php +++ b/CRM/Mosaico/Upgrader.php @@ -5,14 +5,39 @@ */ class CRM_Mosaico_Upgrader extends CRM_Mosaico_Upgrader_Base { - // By convention, functions that look like "function upgrade_NNNN()" are - // upgrade tasks. They are executed in order (like Drupal's hook_update_N). - /** - * Example: Run an external SQL script when the module is installed. - * + * Install module + */ public function install() { - $this->executeSqlFile('sql/myinstall.sql'); + // This would normally be added by Mailing_Template_Category.mgd.php but needs to be present before we save the option value + // The 'match' param will prevent it from being double-inserted. + \Civi\Api4\OptionGroup::save(FALSE) + ->addRecord([ + 'name' => 'mailing_template_category', + 'title' => 'Mailing Template Category', + 'is_reserved' => TRUE, + 'is_active' => TRUE, + ]) + ->setMatch(['name']) + ->execute(); + + $existingCategories = \Civi\Api4\OptionValue::get() + ->selectRowCount() + ->addWhere('option_group_id.name', '=', 'mailing_template_category') + ->execute(); + + // If there are no categories, insert the default "Newsletter" + if (!$existingCategories->count()) { + \Civi\Api4\OptionValue::save(FALSE) + ->addRecord([ + 'option_group_id.name' => 'mailing_template_category', + 'label' => 'Newsletter', + 'value' => '1', + 'name' => 'newsletter', + 'is_default' => TRUE, + ]) + ->execute(); + } } /** @@ -120,7 +145,7 @@ public function upgrade_4704() { FOREIGN KEY (`msg_tpl_id`) REFERENCES `civicrm_msg_template`(`id`) ON DELETE SET NULL '); - + CRM_Core_Invoke::rebuildMenuAndCaches(TRUE); return TRUE; @@ -140,6 +165,52 @@ public function upgrade_4705() { return TRUE; } + /** + * Convert "Newsletter" category from a managed entity to a one-off insert. + */ + public function upgrade_4706() { + $this->ctx->log->info('Applying update 4706'); + + // Stop managing the "newsletter" category - it should only be inserted once as a default, + // but the "managed" thing was preveting the user from deleting it. + \Civi\Api4\Managed::delete(FALSE) + ->addWhere('module', '=', 'uk.co.vedaconsulting.mosaico') + ->addWhere('name', '=', 'OptionGroup_mailing_template_category_newsletter') + ->execute(); + + // This would normally be added by Mailing_Template_Category.mgd.php but needs to be present before we save the option value + // The 'match' param will prevent it from being double-inserted. + \Civi\Api4\OptionGroup::save(FALSE) + ->addRecord([ + 'name' => 'mailing_template_category', + 'title' => 'Mailing Template Category', + 'is_reserved' => TRUE, + 'is_active' => TRUE, + ]) + ->setMatch(['name']) + ->execute(); + + $existingCategories = \Civi\Api4\OptionValue::get() + ->selectRowCount() + ->addWhere('option_group_id.name', '=', 'mailing_template_category') + ->execute(); + + // If there are no categories, insert the default "Newsletter" + if (!$existingCategories->count()) { + \Civi\Api4\OptionValue::save(FALSE) + ->addRecord([ + 'option_group_id.name' => 'mailing_template_category', + 'label' => 'Newsletter', + 'value' => '1', + 'name' => 'newsletter', + 'is_default' => TRUE, + ]) + ->execute(); + } + + return TRUE; + } + /** * Example: Run an external SQL script. diff --git a/info.xml b/info.xml index baa6e2a69..c2cf456a7 100644 --- a/info.xml +++ b/info.xml @@ -19,7 +19,7 @@ 2.9 stable - 5.46 + 5.48 org.civicrm.flexmailer diff --git a/managed/Mailing_Template_Category.mgd.php b/managed/Mailing_Template_Category.mgd.php index e1d5c60ff..8c661f835 100644 --- a/managed/Mailing_Template_Category.mgd.php +++ b/managed/Mailing_Template_Category.mgd.php @@ -3,7 +3,7 @@ [ 'name' => 'OptionGroup_mailing_template_category', 'entity' => 'OptionGroup', - 'cleanup' => 'unused', + 'cleanup' => 'always', 'update' => 'unmodified', 'params' => [ 'version' => 4, @@ -16,34 +16,7 @@ 'is_active' => TRUE, 'is_locked' => FALSE, ], - ], - ], - [ - 'name' => 'OptionGroup_mailing_template_category_newsletter', - 'entity' => 'OptionValue', - 'cleanup' => 'unused', - 'update' => 'unmodified', - 'params' => [ - 'version' => 4, - 'values' => [ - 'option_group_id.name' => 'mailing_template_category', - 'label' => 'Newsletter', - 'value' => '1', - 'name' => 'newsletter', - 'grouping' => NULL, - 'filter' => 0, - 'is_default' => TRUE, - 'weight' => 1, - 'description' => NULL, - 'is_optgroup' => FALSE, - 'is_reserved' => FALSE, - 'is_active' => TRUE, - 'icon' => NULL, - 'color' => NULL, - 'component_id' => NULL, - 'visibility_id' => NULL, - 'domain_id' => NULL, - ], + 'match' => ['name'], ], ], ];