Merge pull request #5520 from totten/4.6-cxn
[civicrm-core.git] / Civi / API / Subscriber / ChainSubscriber.php
index e0a6209d45b22cbddf46aab17cd4babc8292b0c9..944dc71fa01cd1f181a080a0c6fcc789f041aa61 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.                                    |
  |                                                                    |
@@ -69,7 +69,7 @@ class ChainSubscriber implements EventSubscriberInterface {
     $apiRequest = $event->getApiRequest();
     $result = $event->getResponse();
     if (\CRM_Utils_Array::value('is_error', $result, 0) == 0) {
-      $this->callNestedApi($apiRequest['params'], $result, $apiRequest['action'], $apiRequest['entity'], $apiRequest['version']);
+      $this->callNestedApi($event->getApiKernel(), $apiRequest['params'], $result, $apiRequest['action'], $apiRequest['entity'], $apiRequest['version']);
       $event->setResponse($result);
     }
   }
@@ -78,6 +78,7 @@ class ChainSubscriber implements EventSubscriberInterface {
    * Call any nested api calls.
    *
    * TODO: We don't really need this to be a separate function.
+   * @param \Civi\API\Kernel $apiKernel
    * @param $params
    * @param $result
    * @param $action
@@ -85,7 +86,7 @@ class ChainSubscriber implements EventSubscriberInterface {
    * @param $version
    * @throws \Exception
    */
-  protected function callNestedApi(&$params, &$result, $action, $entity, $version) {
+  protected function callNestedApi($apiKernel, &$params, &$result, $action, $entity, $version) {
     $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
 
     // We don't need to worry about nested api in the getfields/getoptions
@@ -133,7 +134,24 @@ 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
@@ -165,7 +183,7 @@ class ChainSubscriber implements EventSubscriberInterface {
             foreach ($newparams as $entityparams) {
               $subParams = array_merge($genericParams, $entityparams);
               _civicrm_api_replace_variables($subParams, $result['values'][$idIndex], $separator);
-              $result['values'][$result['id']][$field][] = civicrm_api($subEntity, $subaction, $subParams);
+              $result['values'][$result['id']][$field][] = $apiKernel->run($subEntity, $subaction, $subParams);
               if ($result['is_error'] === 1) {
                 throw new \Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']);
               }
@@ -175,7 +193,7 @@ class ChainSubscriber implements EventSubscriberInterface {
 
             $subParams = array_merge($subParams, $newparams);
             _civicrm_api_replace_variables($subParams, $result['values'][$idIndex], $separator);
-            $result['values'][$idIndex][$field] = civicrm_api($subEntity, $subaction, $subParams);
+            $result['values'][$idIndex][$field] = $apiKernel->run($subEntity, $subaction, $subParams);
             if (!empty($result['is_error'])) {
               throw new \Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']);
             }