Introduce "civi.dao.preUpdate" and "civi.dao.preInsert" events
authorChristian Wach <needle@haystack.co.uk>
Mon, 9 Mar 2020 11:01:14 +0000 (11:01 +0000)
committerChristian Wach <needle@haystack.co.uk>
Mon, 9 Mar 2020 11:01:14 +0000 (11:01 +0000)
CRM/Core/DAO.php
Civi/Core/DAO/Event/PostUpdate.php
Civi/Core/DAO/Event/PreUpdate.php [new file with mode: 0644]

index 53503b1ee2619a0d7c90042acc9cbc2628a552c0..9cf405ce1be95e079d27e355ad3e0fd108c35e3f 100644 (file)
@@ -543,19 +543,29 @@ class CRM_Core_DAO extends DB_DataObject {
    */
   public function save($hook = TRUE) {
     if (!empty($this->id)) {
-      $this->update();
+      if ($hook) {
+        $preEvent = new \Civi\Core\DAO\Event\PreUpdate($this);
+        \Civi::service('dispatcher')->dispatch("civi.dao.preUpdate", $preEvent);
+      }
+
+      $result = $this->update();
 
       if ($hook) {
-        $event = new \Civi\Core\DAO\Event\PostUpdate($this);
+        $event = new \Civi\Core\DAO\Event\PostUpdate($this, $result);
         \Civi::service('dispatcher')->dispatch("civi.dao.postUpdate", $event);
       }
       $this->clearDbColumnValueCache();
     }
     else {
-      $this->insert();
+      if ($hook) {
+        $preEvent = new \Civi\Core\DAO\Event\PreUpdate($this);
+        \Civi::service('dispatcher')->dispatch("civi.dao.preInsert", $preEvent);
+      }
+
+      $result = $this->insert();
 
       if ($hook) {
-        $event = new \Civi\Core\DAO\Event\PostUpdate($this);
+        $event = new \Civi\Core\DAO\Event\PostUpdate($this, $result);
         \Civi::service('dispatcher')->dispatch("civi.dao.postInsert", $event);
       }
     }
index 8f07957e384adf26c91a7c4303ac746d2724a754..5e75c8a125a34a073c263d92c781927f5d19edca 100644 (file)
@@ -22,11 +22,18 @@ class PostUpdate extends \Symfony\Component\EventDispatcher\Event {
    */
   public $object;
 
+  /**
+   * @var mixed
+   */
+  public $result;
+
   /**
    * @param $object
+   * @param $result
    */
-  public function __construct($object) {
+  public function __construct($object, $result) {
     $this->object = $object;
+    $this->result = $result;
   }
 
 }
diff --git a/Civi/Core/DAO/Event/PreUpdate.php b/Civi/Core/DAO/Event/PreUpdate.php
new file mode 100644 (file)
index 0000000..e6d4716
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Core\DAO\Event;
+
+/**
+ * Class PreDelete
+ * @package Civi\Core\DAO\Event
+ */
+class PreUpdate extends \Symfony\Component\EventDispatcher\Event {
+
+  /**
+   * @var \CRM_Core_DAO
+   */
+  public $object;
+
+  /**
+   * @param $object
+   */
+  public function __construct($object) {
+    $this->object = $object;
+  }
+
+}