CRM-16490 make permissioned relationship ajax call work for other permissioned relati...
[civicrm-core.git] / CRM / Contact / BAO / Relationship.php
index e0f036e707ddecaaeea6d17c22c7f3faef419b84..e40378157b33e57007c7c871223ef14b7a5759d6 100644 (file)
@@ -1768,28 +1768,36 @@ WHERE id IN ( {$contacts} )
    *
    * @param int $contactID
    *   contact id whose permissioned contacts are to be found.
-   * @param string $relTypeId
+   * @param int $relTypeId
    *   one or more relationship type id's.
    * @param string $name
    *
    * @return array
    *   Array of contacts
    */
-  public static function getPermissionedContacts($contactID, $relTypeId, $name = NULL) {
+  public static function getPermissionedContacts($contactID, $relTypeId = NULL, $name = NULL) {
     $contacts = array();
+    $args = array(1 => array($contactID, 'Integer'));
+    $relationshipTypeClause = '';
 
     if ($relTypeId) {
+      // @todo relTypeId is only ever passed in as an int. Change this to reflect that -
+      // probably being overly conservative by not doing so but working on stable release.
+      $relationshipTypeClause = 'AND cr.relationship_type_id IN (%2) ';
+      $args[2] = array($relTypeId, 'String');
+    }
       $query = "
 SELECT cc.id as id, cc.sort_name as name
 FROM civicrm_relationship cr, civicrm_contact cc
 WHERE
 cr.contact_id_a         = %1 AND
-cr.relationship_type_id IN (%2) AND
 cr.is_permission_a_b    = 1 AND
 IF(cr.end_date IS NULL, 1, (DATEDIFF( CURDATE( ), cr.end_date ) <= 0)) AND
 cr.is_active = 1 AND
 cc.id = cr.contact_id_b AND
-cc.is_deleted = 0";
+cc.is_deleted = 0
+$relationshipTypeClause
+";
 
       if (!empty($name)) {
         $name = CRM_Utils_Type::escape($name, 'String');
@@ -1797,7 +1805,6 @@ cc.is_deleted = 0";
 AND cc.sort_name LIKE '%$name%'";
       }
 
-      $args = array(1 => array($contactID, 'Integer'), 2 => array($relTypeId, 'String'));
       $dao = CRM_Core_DAO::executeQuery($query, $args);
 
       while ($dao->fetch()) {
@@ -1806,7 +1813,7 @@ AND cc.sort_name LIKE '%$name%'";
           'value' => $dao->id,
         );
       }
-    }
+
     return $contacts;
   }