Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-05-28-22-50-49
[civicrm-core.git] / Civi / API / Subscriber / ChainSubscriber.php
index e0a6209d45b22cbddf46aab17cd4babc8292b0c9..53566316a472ed5d7d2c2f1e765aec18f08e7723 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -90,7 +90,7 @@ class ChainSubscriber implements EventSubscriberInterface {
 
     // We don't need to worry about nested api in the getfields/getoptions
     // actions, so just return immediately.
-    if (in_array($action, array('getfields', 'getoptions'))) {
+    if (in_array($action, array('getfields', 'getfield', 'getoptions'))) {
       return;
     }
 
@@ -133,12 +133,29 @@ class ChainSubscriber implements EventSubscriberInterface {
             //set to the parent's id
             $subParams["entity_id"] = $parentAPIValues['id'];
             $subParams['entity_table'] = 'civicrm_' . $lowercase_entity;
-            $subParams[$lowercase_entity . "_id"] = $parentAPIValues['id'];
+
+            $crm16084 = FALSE;
+            if ($subEntity == 'relationship' && $lowercase_entity == 'contact') {
+              // if a relationship call is chained to a contact call, we need
+              // to check whether contact_id_a or contact_id_b for the
+              // relationship is given. If so, don't add an extra subParam
+              // "contact_id" => parent_id.
+              // See CRM-16084.
+              foreach (array_keys($newparams) as $key) {
+                if (substr($key, 0, 11) == 'contact_id_') {
+                  $crm16084 = TRUE;
+                  break;
+                }
+              }
+            }
+            if (!$crm16084) {
+              $subParams[$lowercase_entity . "_id"] = $parentAPIValues['id'];
+            }
           }
           if ($entity != 'Contact' && \CRM_Utils_Array::value(strtolower($subEntity . "_id"), $parentAPIValues)) {
             //e.g. if event_id is in the values returned & subentity is event
             //then pass in event_id as 'id' don't do this for contact as it
-            //does some wierd things like returning primary email &
+            //does some weird things like returning primary email &
             //thus limiting the ability to chain email
             //TODO - this might need the camel treatment
             $subParams['id'] = $parentAPIValues[$subEntity . "_id"];