Merge pull request #16755 from eileenmcnaughton/et
[civicrm-core.git] / CRM / Core / ManagedEntities.php
index f5ee0c38cabce0fff141284064a5869aba9b488a..434c7a33760de3ca9496e03692ec246c319f927f 100644 (file)
@@ -112,10 +112,15 @@ class CRM_Core_ManagedEntities {
   /**
    * Identify any enabled/disabled modules. Add new entities, update
    * existing entities, and remove orphaned (stale) entities.
+   * @param bool $ignoreUpgradeMode
    *
    * @throws Exception
    */
-  public function reconcile() {
+  public function reconcile($ignoreUpgradeMode = FALSE) {
+    // Do not reconcile whilst we are in upgrade mode
+    if (CRM_Core_Config::singleton()->isUpgradeMode() && !$ignoreUpgradeMode) {
+      return;
+    }
     if ($error = $this->validate($this->getDeclarations())) {
       throw new Exception($error);
     }
@@ -246,7 +251,7 @@ class CRM_Core_ManagedEntities {
   }
 
   /**
-   * Update an entity which (a) is believed to exist and which (b) ought to be active.
+   * Update an entity which is believed to exist.
    *
    * @param CRM_Core_DAO_Managed $dao
    * @param array $todo
@@ -257,11 +262,26 @@ class CRM_Core_ManagedEntities {
     $doUpdate = ($policy == 'always');
 
     if ($doUpdate) {
-      $defaults = [
-        'id' => $dao->entity_id,
-        'is_active' => 1, // FIXME: test whether is_active is valid
-      ];
+      $defaults = ['id' => $dao->entity_id, 'is_active' => 1];
       $params = array_merge($defaults, $todo['params']);
+
+      $manager = CRM_Extension_System::singleton()->getManager();
+      if ($dao->entity_type === 'Job' && !$manager->extensionIsBeingInstalledOrEnabled($dao->module)) {
+        // Special treatment for scheduled jobs:
+        //
+        // If we're being called as part of enabling/installing a module then
+        // we want the default behaviour of setting is_active = 1.
+        //
+        // However, if we're just being called by a normal cache flush then we
+        // should not re-enable a job that an administrator has decided to disable.
+        //
+        // Without this logic there was a problem: site admin might disable
+        // a job, but then when there was a flush op, the job was re-enabled
+        // which can cause significant embarrassment, depending on the job
+        // ("Don't worry, sending mailings is disabled right now...").
+        unset($params['is_active']);
+      }
+
       $result = civicrm_api($dao->entity_type, 'create', $params);
       if ($result['is_error']) {
         $this->onApiError($dao->entity_type, 'create', $params, $result);