Merge pull request #17656 from civicrm/5.27
[civicrm-core.git] / api / v3 / Address.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM Address records.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Add an Address for a contact.
20 *
21 * FIXME: Should be using basic_create util
22 *
23 * @param array $params
24 * Array per getfields metadata.
25 *
26 * @return array
27 * API result array
28 */
29 function civicrm_api3_address_create($params) {
30 _civicrm_api3_check_edit_permissions('CRM_Core_BAO_Address', $params);
31 /**
32 * If street_parsing, street_address has to be parsed into
33 * separate parts
34 */
35 if (array_key_exists('street_parsing', $params)) {
36 if ($params['street_parsing'] == 1) {
37 if (array_key_exists('street_address', $params)) {
38 if (!empty($params['street_address'])) {
39 $parsedItems = CRM_Core_BAO_Address::parseStreetAddress(
40 $params['street_address']
41 );
42 if (array_key_exists('street_name', $parsedItems)) {
43 $params['street_name'] = $parsedItems['street_name'];
44 }
45 if (array_key_exists('street_unit', $parsedItems)) {
46 $params['street_unit'] = $parsedItems['street_unit'];
47 }
48 if (array_key_exists('street_number', $parsedItems)) {
49 $params['street_number'] = $parsedItems['street_number'];
50 }
51 if (array_key_exists('street_number_suffix', $parsedItems)) {
52 $params['street_number_suffix'] = $parsedItems['street_number_suffix'];
53 }
54 }
55 }
56 }
57 }
58
59 if (!isset($params['check_permissions'])) {
60 $params['check_permissions'] = 0;
61 }
62
63 if (!isset($params['fix_address'])) {
64 $params['fix_address'] = TRUE;
65 }
66
67 /**
68 * Create array for BAO (expects address params in as an
69 * element in array 'address'
70 */
71 $addressBAO = CRM_Core_BAO_Address::add($params, $params['fix_address']);
72 if (empty($addressBAO)) {
73 return civicrm_api3_create_error("Address is not created or updated ");
74 }
75 else {
76 $values = _civicrm_api3_dao_to_array($addressBAO, $params);
77 return civicrm_api3_create_success($values, $params, 'Address', $addressBAO);
78 }
79 }
80
81 /**
82 * Adjust Metadata for Create action.
83 *
84 * @param array $params
85 * Array of parameters determined by getfields.
86 */
87 function _civicrm_api3_address_create_spec(&$params) {
88 $params['location_type_id']['api.required'] = 1;
89 $params['contact_id']['api.required'] = 1;
90 $params['street_parsing'] = [
91 'title' => 'Street Address Parsing',
92 'description' => 'Optional param to indicate you want the street_address field parsed into individual params',
93 'type' => CRM_Utils_Type::T_BOOLEAN,
94 ];
95 $params['skip_geocode'] = [
96 'title' => 'Skip geocode',
97 'description' => 'Optional param to indicate you want to skip geocoding (useful when importing a lot of addresses
98 at once, the job \'Geocode and Parse Addresses\' can execute this task after the import)',
99 'type' => CRM_Utils_Type::T_BOOLEAN,
100 ];
101 $params['fix_address'] = [
102 'title' => ts('Fix address'),
103 'description' => ts('When true, apply various fixes to the address before insert. Default true.'),
104 'type' => CRM_Utils_Type::T_BOOLEAN,
105 'api.default' => TRUE,
106 ];
107 $params['world_region'] = [
108 'title' => ts('World Region'),
109 'name' => 'world_region',
110 'type' => CRM_Utils_Type::T_TEXT,
111 ];
112 $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
113 if ($defaultLocation) {
114 $params['location_type_id']['api.default'] = $defaultLocation->id;
115 }
116 }
117
118 /**
119 * Adjust Metadata for Get action.
120 *
121 * @param array $params
122 * Array of parameters determined by getfields.
123 */
124 function _civicrm_api3_address_get_spec(&$params) {
125 $params['world_region'] = [
126 'title' => ts('World Region'),
127 'name' => 'world_region',
128 'type' => CRM_Utils_Type::T_TEXT,
129 ];
130 }
131
132 /**
133 * Delete an existing Address.
134 *
135 * @param array $params
136 * Array per getfields metadata.
137 *
138 * @return array
139 * API result array
140 */
141 function civicrm_api3_address_delete($params) {
142 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
143 }
144
145 /**
146 * Retrieve one or more addresses.
147 *
148 * @param array $params
149 * Array per getfields metadata.
150 *
151 * @return array
152 * API result array
153 */
154 function civicrm_api3_address_get($params) {
155 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Address');
156 }