Commit | Line | Data |
---|---|---|
0a946de2 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
0a946de2 | 5 | +--------------------------------------------------------------------+ |
39de6fd5 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
0a946de2 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | namespace Civi\API\Subscriber; | |
29 | use Civi\API\Events; | |
30 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
31 | ||
32 | /** | |
8882ff5c TO |
33 | * The ChainSubscriber looks for API parameters which specify a nested or |
34 | * chained API call. For example: | |
0a946de2 TO |
35 | * |
36 | * @code | |
37 | * $result = civicrm_api('Contact', 'create', array( | |
38 | * 'version' => 3, | |
39 | * 'first_name' => 'Amy', | |
40 | * 'api.Email.create' => array( | |
41 | * 'email' => 'amy@example.com', | |
42 | * 'location_type_id' => 123, | |
43 | * ), | |
44 | * )); | |
45 | * @endcode | |
46 | * | |
8882ff5c TO |
47 | * The ChainSubscriber looks for any parameters of the form "api.Email.create"; |
48 | * if found, it issues the nested API call (and passes some extra context -- | |
49 | * eg Amy's contact_id). | |
0a946de2 TO |
50 | */ |
51 | class ChainSubscriber implements EventSubscriberInterface { | |
6550386a EM |
52 | /** |
53 | * @return array | |
54 | */ | |
0a946de2 TO |
55 | public static function getSubscribedEvents() { |
56 | return array( | |
57 | Events::RESPOND => array('onApiRespond', Events::W_EARLY), | |
58 | ); | |
59 | } | |
60 | ||
6550386a EM |
61 | /** |
62 | * @param \Civi\API\Event\RespondEvent $event | |
8882ff5c | 63 | * API response event. |
6550386a EM |
64 | * |
65 | * @throws \Exception | |
66 | */ | |
0a946de2 TO |
67 | public function onApiRespond(\Civi\API\Event\RespondEvent $event) { |
68 | $apiRequest = $event->getApiRequest(); | |
69 | $result = $event->getResponse(); | |
70 | if (\CRM_Utils_Array::value('is_error', $result, 0) == 0) { | |
8882ff5c | 71 | $this->callNestedApi($apiRequest['params'], $result, $apiRequest['action'], $apiRequest['entity'], $apiRequest['version']); |
0a946de2 TO |
72 | $event->setResponse($result); |
73 | } | |
74 | } | |
75 | ||
76 | /** | |
77 | * Call any nested api calls | |
78 | * | |
79 | * TODO: We don't really need this to be a separate function. | |
80 | */ | |
8882ff5c | 81 | protected function callNestedApi(&$params, &$result, $action, $entity, $version) { |
0a946de2 TO |
82 | $entity = _civicrm_api_get_entity_name_from_camel($entity); |
83 | ||
8882ff5c TO |
84 | // We don't need to worry about nested api in the getfields/getoptions |
85 | // actions, so just return immediately. | |
0a946de2 TO |
86 | if (in_array(strtolower($action), array('getfields', 'getoptions'))) { |
87 | return; | |
88 | } | |
89 | ||
90 | if (strtolower($action) == 'getsingle') { | |
91 | // I don't understand the protocol here, but we don't want | |
92 | // $result to be a recursive array | |
93 | // $result['values'][0] = $result; | |
94 | $oldResult = $result; | |
95 | $result = array('values' => array(0 => $oldResult)); | |
96 | } | |
97 | foreach ($params as $field => $newparams) { | |
98 | if ((is_array($newparams) || $newparams === 1) && $field <> 'api.has_parent' && substr($field, 0, 3) == 'api') { | |
99 | ||
8882ff5c TO |
100 | // 'api.participant.delete' => 1 is a valid options - handle 1 |
101 | // instead of an array | |
0a946de2 TO |
102 | if ($newparams === 1) { |
103 | $newparams = array('version' => $version); | |
104 | } | |
105 | // can be api_ or api. | |
106 | $separator = $field[3]; | |
107 | if (!($separator == '.' || $separator == '_')) { | |
108 | continue; | |
109 | } | |
110 | $subAPI = explode($separator, $field); | |
111 | ||
112 | $subaction = empty($subAPI[2]) ? $action : $subAPI[2]; | |
113 | $subParams = array( | |
114 | 'debug' => \CRM_Utils_Array::value('debug', $params), | |
115 | ); | |
116 | $subEntity = $subAPI[1]; | |
117 | ||
118 | foreach ($result['values'] as $idIndex => $parentAPIValues) { | |
119 | ||
120 | if (strtolower($subEntity) != 'contact') { | |
121 | //contact spits the dummy at activity_id so what else won't it like? | |
8882ff5c TO |
122 | //set entity_id & entity table based on the parent's id & entity. |
123 | //e.g for something like note if the parent call is contact | |
124 | //'entity_table' will be set to 'contact' & 'id' to the contact id | |
125 | //from the parent call. in this case 'contact_id' will also be | |
126 | //set to the parent's id | |
0a946de2 TO |
127 | $subParams["entity_id"] = $parentAPIValues['id']; |
128 | $subParams['entity_table'] = 'civicrm_' . _civicrm_api_get_entity_name_from_camel($entity); | |
129 | $subParams[strtolower($entity) . "_id"] = $parentAPIValues['id']; | |
130 | } | |
131 | if (strtolower($entity) != 'contact' && \CRM_Utils_Array::value(strtolower($subEntity . "_id"), $parentAPIValues)) { | |
8882ff5c TO |
132 | //e.g. if event_id is in the values returned & subentity is event |
133 | //then pass in event_id as 'id' don't do this for contact as it | |
134 | //does some wierd things like returning primary email & | |
0a946de2 TO |
135 | //thus limiting the ability to chain email |
136 | //TODO - this might need the camel treatment | |
137 | $subParams['id'] = $parentAPIValues[$subEntity . "_id"]; | |
138 | } | |
139 | ||
140 | if (\CRM_Utils_Array::value('entity_table', $result['values'][$idIndex]) == $subEntity) { | |
141 | $subParams['id'] = $result['values'][$idIndex]['entity_id']; | |
142 | } | |
8882ff5c TO |
143 | // if we are dealing with the same entity pass 'id' through |
144 | // (useful for get + delete for example) | |
0a946de2 TO |
145 | if (strtolower($entity) == strtolower($subEntity)) { |
146 | $subParams['id'] = $result['values'][$idIndex]['id']; | |
147 | } | |
148 | ||
0a946de2 TO |
149 | $subParams['version'] = $version; |
150 | if (!empty($params['check_permissions'])) { | |
151 | $subParams['check_permissions'] = $params['check_permissions']; | |
152 | } | |
153 | $subParams['sequential'] = 1; | |
154 | $subParams['api.has_parent'] = 1; | |
155 | if (array_key_exists(0, $newparams)) { | |
156 | $genericParams = $subParams; | |
157 | // it is a numerically indexed array - ie. multiple creates | |
158 | foreach ($newparams as $entityparams) { | |
159 | $subParams = array_merge($genericParams, $entityparams); | |
160 | _civicrm_api_replace_variables($subAPI[1], $subaction, $subParams, $result['values'][$idIndex], $separator); | |
161 | $result['values'][$result['id']][$field][] = civicrm_api($subEntity, $subaction, $subParams); | |
162 | if ($result['is_error'] === 1) { | |
163 | throw new \Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']); | |
164 | } | |
165 | } | |
166 | } | |
167 | else { | |
168 | ||
169 | $subParams = array_merge($subParams, $newparams); | |
170 | _civicrm_api_replace_variables($subAPI[1], $subaction, $subParams, $result['values'][$idIndex], $separator); | |
171 | $result['values'][$idIndex][$field] = civicrm_api($subEntity, $subaction, $subParams); | |
172 | if (!empty($result['is_error'])) { | |
173 | throw new \Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']); | |
174 | } | |
175 | } | |
176 | } | |
177 | } | |
178 | } | |
179 | if (strtolower($action) == 'getsingle') { | |
180 | $result = $result['values'][0]; | |
181 | } | |
182 | } | |
183 | ||
6550386a | 184 | } |