Update Copywrite year to be 2019
[civicrm-core.git] / CRM / Contact / BAO / GroupContact.php
index 3ac3b2b0eb1986ed3af59eff947021ce5a1b7813..20290e2c690150f6ed2472516c773d609a568e34 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2018                                |
+ | Copyright CiviCRM LLC (c) 2004-2019                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2018
+ * @copyright CiviCRM LLC (c) 2004-2019
  */
 class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
 
@@ -51,17 +51,27 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
    *
    * @return CRM_Contact_BAO_Group
    */
-  public static function add(&$params) {
+  public static function add($params) {
+    $hook = empty($params['id']) ? 'create' : 'edit';
+    CRM_Utils_Hook::pre($hook, 'GroupContact', CRM_Utils_Array::value('id', $params), $params);
 
-    $dataExists = self::dataExists($params);
-    if (!$dataExists) {
+    if (!self::dataExists($params)) {
       return NULL;
     }
 
     $groupContact = new CRM_Contact_BAO_GroupContact();
     $groupContact->copyValues($params);
-    CRM_Contact_BAO_SubscriptionHistory::create($params);
     $groupContact->save();
+
+    // Lookup existing info for the sake of subscription history
+    if (!empty($params['id'])) {
+      $groupContact->find(TRUE);
+      $params = $groupContact->toArray();
+    }
+    CRM_Contact_BAO_SubscriptionHistory::create($params);
+
+    CRM_Utils_Hook::post($hook, 'GroupContact', $groupContact->id, $groupContact);
+
     return $groupContact;
   }
 
@@ -74,12 +84,7 @@ class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact {
    * @return bool
    */
   public static function dataExists(&$params) {
-    // return if no data present
-    if ($params['group_id'] == 0) {
-      return FALSE;
-    }
-
-    return TRUE;
+    return (!empty($params['id']) || (!empty($params['group_id']) && !empty($params['contact_id'])));
   }
 
   /**