update all is_reserved templates each time
authordemeritcowboy <demeritcowboy@hotmail.com>
Mon, 22 Aug 2022 20:09:31 +0000 (16:09 -0400)
committerdemeritcowboy <demeritcowboy@hotmail.com>
Tue, 23 Aug 2022 19:31:51 +0000 (15:31 -0400)
CRM/Upgrade/Form.php
CRM/Upgrade/Incremental/MessageTemplates.php

index 7f4903ec44cb5fb0975f473674ad5893a412ab01..9839582e169760adf469a99ec8d5638a577f8708 100644 (file)
@@ -581,6 +581,13 @@ SET    version = '$version'
       $queue->createItem($task, ['weight' => 0]);
     }
 
+    $task = new CRM_Queue_Task(
+      ['CRM_Upgrade_Incremental_MessageTemplates', 'updateReservedAndMaybeDefaultTemplates'],
+      [],
+      "Update all reserved message templates"
+    );
+    $queue->createItem($task, ['weight' => 990]);
+
     $task = new CRM_Queue_Task(
       ['CRM_Upgrade_Form', 'doCoreFinish'],
       [$rev, $latestVer, $latestVer, $postUpgradeMessageFile],
index e85585a5960a1abf7b3884238e2c3531a4d70060..dca70ec0ca093a703f9d6d63ce2b9c923675427f 100644 (file)
@@ -528,4 +528,81 @@ class CRM_Upgrade_Incremental_MessageTemplates {
     }
   }
 
+  /**
+   * Make sure *all* reserved ones get updated. Might be inefficient because we either already updated or
+   * there were no changes to a given template, but there's only about 30.
+   * This runs near the final steps of the upgrade, otherwise the earlier checks that run during the
+   * individual revisions wouldn't accurately be checking against the right is_reserved version to see
+   * if it had changed.
+   * @todo - do we still need those earlier per-version runs? e.g. the token replacement functions should still work as-is?
+   *
+   * @param CRM_Queue_TaskContext $ctx
+   * @return bool
+   */
+  public static function updateReservedAndMaybeDefaultTemplates(CRM_Queue_TaskContext $ctx): bool {
+    // This has to come first otherwise it would be checking against is_reserved we already updated.
+    $uneditedTemplates = self::getUneditedTemplates();
+
+    $dao = CRM_Core_DAO::executeQuery("SELECT id, workflow_id, workflow_name FROM civicrm_msg_template WHERE is_reserved=1");
+    while ($dao->fetch()) {
+      foreach (['html', 'text', 'subject'] as $type) {
+        $content = file_get_contents(\Civi::paths()->getPath('[civicrm.root]/xml/templates/message_templates/' . $dao->workflow_name . '_' . $type . '.tpl'));
+        if ($content) {
+          CRM_Core_DAO::executeQuery(
+            "UPDATE civicrm_msg_template SET msg_{$type} = %1 WHERE id = %2", [
+              1 => [$content, 'String'],
+              2 => [$dao->id, 'Integer'],
+            ]
+          );
+
+          // If the same workflow_id and type appears in our list of unedited templates, update it too.
+          // There's probably a more efficient way to look this up but simple for now.
+          foreach ($uneditedTemplates as $uneditedTemplate) {
+            if ($uneditedTemplate['workflow_id'] === $dao->workflow_id && $uneditedTemplate['type'] === $type) {
+              CRM_Core_DAO::executeQuery(
+                "UPDATE civicrm_msg_template SET msg_{$type} = %1 WHERE id = %2", [
+                  1 => [$content, 'String'],
+                  2 => [$uneditedTemplate['id'], 'Integer'],
+                ]
+              );
+              break;
+            }
+          }
+        }
+      }
+    }
+    return TRUE;
+  }
+
+  /**
+   * Get all the is_default templates that are the unchanged from their is_reserved counterpart, which
+   * at the time this runs was the version shipped with core when it was last changed.
+   *
+   * @todo have pulled this out since want to re-use it to get the preUpgrade message right. Currently
+   * it always tells you all the ones in the hardcoded per-version list have been customized.
+   *
+   * @return array
+   */
+  public static function getUneditedTemplates(): array {
+    $templates = [];
+    foreach (['html', 'text', 'subject'] as $type) {
+      $dao = CRM_Core_DAO::executeQuery("
+        SELECT default_template.id, default_template.workflow_id FROM civicrm_msg_template reserved
+        LEFT JOIN civicrm_msg_template default_template
+          ON reserved.workflow_id = default_template.workflow_id
+        WHERE reserved.is_reserved = 1 AND default_template.is_default = 1 AND reserved.id <> default_template.id
+        AND reserved.msg_{$type} = default_template.msg_{$type}
+      ");
+      while ($dao->fetch()) {
+        // Note the same id can appear multiple times, e.g. you might change the html but not the subject.
+        $templates[] = [
+          'id' => $dao->id,
+          'type' => $type,
+          'workflow_id' => $dao->workflow_id,
+        ];
+      }
+    }
+    return $templates;
+  }
+
 }