Commit | Line | Data |
---|---|---|
dcef11bd TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
dcef11bd | 5 | +--------------------------------------------------------------------+ |
39de6fd5 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
dcef11bd 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 | /** | |
33 | * This class determines what fields are allowed for a request | |
34 | * and validates that the fields are provided correctly. | |
35 | */ | |
36 | class APIv3SchemaAdapter implements EventSubscriberInterface { | |
6550386a EM |
37 | /** |
38 | * @return array | |
39 | */ | |
dcef11bd TO |
40 | public static function getSubscribedEvents() { |
41 | return array( | |
42 | Events::PREPARE => array( | |
43 | array('onApiPrepare', Events::W_MIDDLE), | |
44 | array('onApiPrepare_validate', Events::W_LATE), | |
45 | ), | |
46 | ); | |
47 | } | |
48 | ||
6550386a EM |
49 | /** |
50 | * @param \Civi\API\Event\PrepareEvent $event | |
51 | * | |
52 | * @throws \API_Exception | |
53 | */ | |
dcef11bd TO |
54 | public function onApiPrepare(\Civi\API\Event\PrepareEvent $event) { |
55 | $apiRequest = $event->getApiRequest(); | |
c65db512 TO |
56 | if ($apiRequest['version'] > 3) { |
57 | return; | |
58 | } | |
dcef11bd TO |
59 | |
60 | $apiRequest['fields'] = _civicrm_api3_api_getfields($apiRequest); | |
61 | ||
62 | _civicrm_api3_swap_out_aliases($apiRequest, $apiRequest['fields']); | |
63 | if (strtolower($apiRequest['action']) != 'getfields') { | |
64 | if (empty($apiRequest['params']['id'])) { | |
65 | $apiRequest['params'] = array_merge($this->getDefaults($apiRequest['fields']), $apiRequest['params']); | |
66 | } | |
67 | //if 'id' is set then only 'version' will be checked but should still be checked for consistency | |
68 | civicrm_api3_verify_mandatory($apiRequest['params'], NULL, $this->getRequired($apiRequest['fields'])); | |
69 | } | |
70 | ||
71 | $event->setApiRequest($apiRequest); | |
72 | } | |
73 | ||
6550386a EM |
74 | /** |
75 | * @param \Civi\API\Event\Event $event | |
76 | * | |
77 | * @throws \Exception | |
78 | */ | |
dcef11bd TO |
79 | public function onApiPrepare_validate(\Civi\API\Event\Event $event) { |
80 | $apiRequest = $event->getApiRequest(); | |
81 | // Not sure why this is omitted for generic actions. It would make sense to omit 'getfields', but that's only one generic action. | |
82 | ||
83 | if (isset($apiRequest['function']) && !$apiRequest['is_generic'] && isset($apiRequest['fields'])) { | |
84 | _civicrm_api3_validate_fields($apiRequest['entity'], $apiRequest['action'], $apiRequest['params'], $apiRequest['fields']); | |
85 | $event->setApiRequest($apiRequest); | |
86 | } | |
87 | } | |
88 | ||
89 | /** | |
90 | * Return array of defaults for the given API (function is a wrapper on getfields) | |
91 | */ | |
92 | public function getDefaults($fields) { | |
93 | $defaults = array(); | |
94 | ||
95 | foreach ($fields as $field => $values) { | |
96 | if (isset($values['api.default'])) { | |
97 | $defaults[$field] = $values['api.default']; | |
98 | } | |
99 | } | |
100 | return $defaults; | |
101 | } | |
102 | ||
103 | /** | |
104 | * Return array of required fields for the given API (function is a wrapper on getfields) | |
105 | */ | |
106 | public function getRequired($fields) { | |
107 | $required = array('version'); | |
108 | ||
109 | foreach ($fields as $field => $values) { | |
110 | if (!empty($values['api.required'])) { | |
111 | $required[] = $field; | |
112 | } | |
113 | } | |
114 | return $required; | |
115 | } | |
6550386a | 116 | } |