Merge pull request #3216 from xurizaemon/CRM-14596
[civicrm-core.git] / CRM / Activity / BAO / ActivityContact.php
index 0b8694e9f6022f5444e82f8fe89515f2704b646d..1a21e3d2738cf773727528c3203fa06f62f6f10c 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -59,21 +59,27 @@ class CRM_Activity_BAO_ActivityContact extends CRM_Activity_DAO_ActivityContact
     $activityContact = new CRM_Activity_DAO_ActivityContact();
 
     $activityContact->copyValues($params);
-    return $activityContact->save();
+    if (!$activityContact->find(TRUE)) {
+      return $activityContact->save();
+    }
+    return $activityContact;
   }
 
   /**
    * function to retrieve names of contact by activity_id
    *
-   * @param int    $id   ID of the activity
-   * @param string $type type of interaction
+   * @param $activityID
+   * @param $recordTypeID
+   * @param bool $alsoIDs
+   *
+   * @internal param int $id ID of the activity
+   * @internal param string $type type of interaction
    *
    * @return array
    *
    * @access public
-   *
    */
-  static function getNames($activityID, $recordType, $alsoIDs = FALSE) {
+  static function getNames($activityID, $recordTypeID, $alsoIDs = FALSE) {
     $names = array();
     $ids   = array();
 
@@ -86,12 +92,12 @@ SELECT     contact_a.id, contact_a.sort_name
 FROM       civicrm_contact contact_a
 INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
 WHERE      civicrm_activity_contact.activity_id = %1
-AND        civicrm_activity_contact.record_type = %2
+AND        civicrm_activity_contact.record_type_id = %2
 AND        contact_a.is_deleted = 0
 ";
     $params = array(
       1 => array($activityID, 'Integer'),
-      2 => array($recordType, 'String')
+      2 => array($recordTypeID, 'Integer')
     );
 
     $dao = CRM_Core_DAO::executeQuery($query, $params);
@@ -103,4 +109,46 @@ AND        contact_a.is_deleted = 0
     return $alsoIDs ? array($names, $ids) : $names;
   }
 
+  /**
+   * function to retrieve id of target contact by activity_id
+   *
+   * @param $activityID
+   * @param $recordTypeID
+   *
+   * @internal param int $id ID of the activity
+   *
+   * @return mixed
+   *
+   * @access public
+   */
+  static function retrieveContactIdsByActivityId($activityID, $recordTypeID) {
+    $activityContact = array();
+    if (!CRM_Utils_Rule::positiveInteger($activityID) ||
+        !CRM_Utils_Rule::positiveInteger($recordTypeID)) {
+      return $activityContact;
+    }
+
+    $sql = "                                                                                                                                                                                             SELECT     contact_id
+FROM       civicrm_activity_contact
+INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
+WHERE      activity_id = %1
+AND        record_type_id = %2
+AND        civicrm_contact.is_deleted = 0
+";
+    $params = array(
+      1 => array($activityID, 'Integer'),
+      2 => array($recordTypeID, 'Integer')
+    );
+
+    $dao = CRM_Core_DAO::executeQuery($sql, $params);
+    while ($dao->fetch()) {
+      $activityContact[] = $dao->contact_id;
+    }
+    return $activityContact;
+  }
+
+  function links() {
+    $link = array('activity_id' => 'civicrm_activity:id');
+    return $link;
+  }
 }