3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
28 namespace Civi\API\Subscriber
;
31 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
34 * The ChainSubscriber looks for API parameters which specify a nested or
35 * chained API call. For example:
38 * $result = civicrm_api('Contact', 'create', array(
40 * 'first_name' => 'Amy',
41 * 'api.Email.create' => array(
42 * 'email' => 'amy@example.com',
43 * 'location_type_id' => 123,
48 * The ChainSubscriber looks for any parameters of the form "api.Email.create";
49 * if found, it issues the nested API call (and passes some extra context --
50 * eg Amy's contact_id).
52 class ChainSubscriber
implements EventSubscriberInterface
{
56 public static function getSubscribedEvents() {
58 Events
::RESPOND
=> array('onApiRespond', Events
::W_EARLY
),
63 * @param \Civi\API\Event\RespondEvent $event
68 public function onApiRespond(\Civi\API\Event\RespondEvent
$event) {
69 $apiRequest = $event->getApiRequest();
70 $result = $event->getResponse();
71 if (\CRM_Utils_Array
::value('is_error', $result, 0) == 0) {
72 $this->callNestedApi($apiRequest['params'], $result, $apiRequest['action'], $apiRequest['entity'], $apiRequest['version']);
73 $event->setResponse($result);
78 * Call any nested api calls
80 * TODO: We don't really need this to be a separate function.
82 protected function callNestedApi(&$params, &$result, $action, $entity, $version) {
83 $entity = _civicrm_api_get_entity_name_from_camel($entity);
85 // We don't need to worry about nested api in the getfields/getoptions
86 // actions, so just return immediately.
87 if (in_array(strtolower($action), array('getfields', 'getoptions'))) {
91 if (strtolower($action) == 'getsingle') {
92 // I don't understand the protocol here, but we don't want
93 // $result to be a recursive array
94 // $result['values'][0] = $result;
96 $result = array('values' => array(0 => $oldResult));
98 foreach ($params as $field => $newparams) {
99 if ((is_array($newparams) ||
$newparams === 1) && $field <> 'api.has_parent' && substr($field, 0, 3) == 'api') {
101 // 'api.participant.delete' => 1 is a valid options - handle 1
102 // instead of an array
103 if ($newparams === 1) {
104 $newparams = array('version' => $version);
106 // can be api_ or api.
107 $separator = $field[3];
108 if (!($separator == '.' ||
$separator == '_')) {
111 $subAPI = explode($separator, $field);
113 $subaction = empty($subAPI[2]) ?
$action : $subAPI[2];
115 'debug' => \CRM_Utils_Array
::value('debug', $params),
117 $subEntity = $subAPI[1];
119 foreach ($result['values'] as $idIndex => $parentAPIValues) {
121 if (strtolower($subEntity) != 'contact') {
122 //contact spits the dummy at activity_id so what else won't it like?
123 //set entity_id & entity table based on the parent's id & entity.
124 //e.g for something like note if the parent call is contact
125 //'entity_table' will be set to 'contact' & 'id' to the contact id
126 //from the parent call. in this case 'contact_id' will also be
127 //set to the parent's id
128 $subParams["entity_id"] = $parentAPIValues['id'];
129 $subParams['entity_table'] = 'civicrm_' . _civicrm_api_get_entity_name_from_camel($entity);
130 $subParams[strtolower($entity) . "_id"] = $parentAPIValues['id'];
132 if (strtolower($entity) != 'contact' && \CRM_Utils_Array
::value(strtolower($subEntity . "_id"), $parentAPIValues)) {
133 //e.g. if event_id is in the values returned & subentity is event
134 //then pass in event_id as 'id' don't do this for contact as it
135 //does some wierd things like returning primary email &
136 //thus limiting the ability to chain email
137 //TODO - this might need the camel treatment
138 $subParams['id'] = $parentAPIValues[$subEntity . "_id"];
141 if (\CRM_Utils_Array
::value('entity_table', $result['values'][$idIndex]) == $subEntity) {
142 $subParams['id'] = $result['values'][$idIndex]['entity_id'];
144 // if we are dealing with the same entity pass 'id' through
145 // (useful for get + delete for example)
146 if (strtolower($entity) == strtolower($subEntity)) {
147 $subParams['id'] = $result['values'][$idIndex]['id'];
150 $subParams['version'] = $version;
151 if (!empty($params['check_permissions'])) {
152 $subParams['check_permissions'] = $params['check_permissions'];
154 $subParams['sequential'] = 1;
155 $subParams['api.has_parent'] = 1;
156 if (array_key_exists(0, $newparams)) {
157 $genericParams = $subParams;
158 // it is a numerically indexed array - ie. multiple creates
159 foreach ($newparams as $entityparams) {
160 $subParams = array_merge($genericParams, $entityparams);
161 _civicrm_api_replace_variables($subAPI[1], $subaction, $subParams, $result['values'][$idIndex], $separator);
162 $result['values'][$result['id']][$field][] = civicrm_api($subEntity, $subaction, $subParams);
163 if ($result['is_error'] === 1) {
164 throw new \
Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']);
170 $subParams = array_merge($subParams, $newparams);
171 _civicrm_api_replace_variables($subAPI[1], $subaction, $subParams, $result['values'][$idIndex], $separator);
172 $result['values'][$idIndex][$field] = civicrm_api($subEntity, $subaction, $subParams);
173 if (!empty($result['is_error'])) {
174 throw new \
Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']);
180 if (strtolower($action) == 'getsingle') {
181 $result = $result['values'][0];