Skip to content

Commit

Permalink
Merge pull request #501 from colemanw/defaultCategory
Browse files Browse the repository at this point in the history
Switch newsletter category to one-time insert instead of managed
  • Loading branch information
mattwire authored Mar 30, 2022
2 parents 1e1e3ec + c6cf258 commit f802be0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 37 deletions.
85 changes: 78 additions & 7 deletions CRM/Mosaico/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<version>2.9</version>
<develStage>stable</develStage>
<compatibility>
<ver>5.46</ver>
<ver>5.48</ver>
</compatibility>
<requires>
<ext>org.civicrm.flexmailer</ext>
Expand Down
31 changes: 2 additions & 29 deletions managed/Mailing_Template_Category.mgd.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[
'name' => 'OptionGroup_mailing_template_category',
'entity' => 'OptionGroup',
'cleanup' => 'unused',
'cleanup' => 'always',
'update' => 'unmodified',
'params' => [
'version' => 4,
Expand All @@ -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'],
],
],
];

0 comments on commit f802be0

Please sign in to comment.