dev/core#2390 Add hook support for Activity Contact
authoreileen <emcnaughton@wikimedia.org>
Thu, 18 Mar 2021 06:57:05 +0000 (19:57 +1300)
committereileen <emcnaughton@wikimedia.org>
Thu, 18 Mar 2021 06:57:05 +0000 (19:57 +1300)
Alternative to https://github.com/civicrm/civicrm-core/pull/19623
per comments on that PR.

CRM/Activity/BAO/ActivityContact.php
CRM/Contact/BAO/DashboardContact.php
CRM/Core/DAO.php

index 4ac6652f7c251374212e77590f01f1aa0fdf544d..b68260e1be64ca52a9be049e75f4ce439c5997da 100644 (file)
  */
 class CRM_Activity_BAO_ActivityContact extends CRM_Activity_DAO_ActivityContact {
 
-  /**
-   * Class constructor.
-   */
-  public function __construct() {
-    parent::__construct();
-  }
-
   /**
    * Function to add activity contact.
    *
    * @param array $params
    *   The values for this table: activity id, contact id, record type.
    *
-   * @return object
+   * @return CRM_Activity_DAO_ActivityContact
    *   activity_contact object
+   *
+   * @throws \CRM_Core_Exception
+   * @throws \PEAR_Exception
    */
-  public static function create($params) {
-    $activityContact = new CRM_Activity_DAO_ActivityContact();
-    $activityContact->copyValues($params);
+  public static function create(array $params): CRM_Activity_DAO_ActivityContact {
     try {
-      return $activityContact->save();
+      return self::writeRecord($params);
     }
     catch (PEAR_Exception $e) {
       // This check used to be done first, creating an extra query before each insert.
       // However, in none of the core tests was this ever called with values that already
       // existed, meaning that this line would never or almost never be hit.
       // hence it's better to save the select query here.
+      $activityContact = new CRM_Activity_DAO_ActivityContact();
+      $activityContact->copyValues($params);
       if ($activityContact->find(TRUE)) {
         return $activityContact;
       }
index c6ff99f6891a9cfd33421c01eab9a20843b64da6..145494860b58e8a8f0fcba5666da31a149cdc117 100644 (file)
@@ -17,10 +17,11 @@ class CRM_Contact_BAO_DashboardContact extends CRM_Contact_DAO_DashboardContact
 
   /**
    * @param array $record
+   *
    * @return CRM_Contact_DAO_DashboardContact
-   * @throws CRM_Core_Exception
+   * @throws \CRM_Core_Exception
    */
-  public static function writeRecord(array $record) {
+  public static function writeRecord(array $record): CRM_Core_DAO {
     self::checkEditPermission($record);
     return parent::writeRecord($record);
   }
index b80bc54575bad121f81d6e3b332e4ce10d9db28e..506a971ba72ada0069624873e40614d202ab14eb 100644 (file)
@@ -887,10 +887,11 @@ class CRM_Core_DAO extends DB_DataObject {
    * Otherwise a new record will be created.
    *
    * @param array $record
-   * @return CRM_Core_DAO
-   * @throws CRM_Core_Exception
+   *
+   * @return $this
+   * @throws \CRM_Core_Exception
    */
-  public static function writeRecord(array $record) {
+  public static function writeRecord(array $record): CRM_Core_DAO {
     $hook = empty($record['id']) ? 'create' : 'edit';
     $className = CRM_Core_DAO_AllCoreTables::getCanonicalClassName(static::class);
     if ($className === 'CRM_Core_DAO') {