Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
244bbdd8 | 29 | * This api exposes CiviCRM Address records. |
6a488035 TO |
30 | * |
31 | * @package CiviCRM_APIv3 | |
6a488035 TO |
32 | */ |
33 | ||
6a488035 | 34 | /** |
22242c87 | 35 | * Add an Address for a contact. |
6a488035 | 36 | * |
db83e3a3 CW |
37 | * FIXME: Should be using basic_create util |
38 | * | |
c490a46a | 39 | * @param array $params |
2e66abf8 | 40 | * Array per getfields metadata. |
77b97be7 | 41 | * |
a6c01b45 | 42 | * @return array |
00f8641b | 43 | * API result array |
6a488035 TO |
44 | */ |
45 | function civicrm_api3_address_create(&$params) { | |
db83e3a3 | 46 | _civicrm_api3_check_edit_permissions('CRM_Core_BAO_Address', $params); |
6a488035 | 47 | /** |
100fef9d | 48 | * If street_parsing, street_address has to be parsed into |
6a488035 TO |
49 | * separate parts |
50 | */ | |
51 | if (array_key_exists('street_parsing', $params)) { | |
52 | if ($params['street_parsing'] == 1) { | |
53 | if (array_key_exists('street_address', $params)) { | |
54 | if (!empty($params['street_address'])) { | |
55 | $parsedItems = CRM_Core_BAO_Address::parseStreetAddress( | |
56 | $params['street_address'] | |
57 | ); | |
58 | if (array_key_exists('street_name', $parsedItems)) { | |
59 | $params['street_name'] = $parsedItems['street_name']; | |
60 | } | |
61 | if (array_key_exists('street_unit', $parsedItems)) { | |
62 | $params['street_unit'] = $parsedItems['street_unit']; | |
63 | } | |
64 | if (array_key_exists('street_number', $parsedItems)) { | |
65 | $params['street_number'] = $parsedItems['street_number']; | |
66 | } | |
67 | if (array_key_exists('street_number_suffix', $parsedItems)) { | |
68 | $params['street_number_suffix'] = $parsedItems['street_number_suffix']; | |
69 | } | |
70 | } | |
71 | } | |
72 | } | |
73 | } | |
74 | ||
e9ff5391 | 75 | if (!isset($params['check_permissions'])) { |
76 | $params['check_permissions'] = 0; | |
77 | } | |
78 | ||
0b04b86f EE |
79 | if (!isset($params['fix_address'])) { |
80 | $params['fix_address'] = TRUE; | |
81 | } | |
82 | ||
6a488035 | 83 | /** |
c490a46a CW |
84 | * Create array for BAO (expects address params in as an |
85 | * element in array 'address' | |
86 | */ | |
0b04b86f | 87 | $addressBAO = CRM_Core_BAO_Address::add($params, $params['fix_address']); |
6a488035 TO |
88 | if (empty($addressBAO)) { |
89 | return civicrm_api3_create_error("Address is not created or updated "); | |
90 | } | |
91 | else { | |
6a488035 | 92 | $values = _civicrm_api3_dao_to_array($addressBAO, $params); |
244bbdd8 | 93 | return civicrm_api3_create_success($values, $params, 'Address', $addressBAO); |
6a488035 TO |
94 | } |
95 | } | |
96 | ||
11e09c59 | 97 | /** |
0aa0303c | 98 | * Adjust Metadata for Create action. |
6a488035 | 99 | * |
cf470720 | 100 | * @param array $params |
b081365f | 101 | * Array of parameters determined by getfields. |
6a488035 TO |
102 | */ |
103 | function _civicrm_api3_address_create_spec(&$params) { | |
104 | $params['location_type_id']['api.required'] = 1; | |
105 | $params['contact_id']['api.required'] = 1; | |
cf8f0fff | 106 | $params['street_parsing'] = [ |
b2ed8e73 CW |
107 | 'title' => 'Street Address Parsing', |
108 | 'description' => 'Optional param to indicate you want the street_address field parsed into individual params', | |
d142432b | 109 | 'type' => CRM_Utils_Type::T_BOOLEAN, |
cf8f0fff CW |
110 | ]; |
111 | $params['skip_geocode'] = [ | |
ef804737 WDC |
112 | 'title' => 'Skip geocode', |
113 | 'description' => 'Optional param to indicate you want to skip geocoding (useful when importing a lot of addresses | |
114 | at once, the job \'Geocode and Parse Addresses\' can execute this task after the import)', | |
115 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
cf8f0fff CW |
116 | ]; |
117 | $params['fix_address'] = [ | |
0b04b86f EE |
118 | 'title' => ts('Fix address'), |
119 | 'description' => ts('When true, apply various fixes to the address before insert. Default true.'), | |
120 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
121 | 'api.default' => TRUE, | |
cf8f0fff CW |
122 | ]; |
123 | $params['world_region'] = [ | |
c7130c3e CW |
124 | 'title' => ts('World Region'), |
125 | 'name' => 'world_region', | |
b8326bd2 | 126 | 'type' => CRM_Utils_Type::T_TEXT, |
cf8f0fff | 127 | ]; |
c7130c3e | 128 | } |
210737b6 | 129 | |
c7130c3e | 130 | /** |
2e66abf8 | 131 | * Adjust Metadata for Get action. |
c7130c3e | 132 | * |
cf470720 | 133 | * @param array $params |
2e66abf8 | 134 | * Array of parameters determined by getfields. |
c7130c3e CW |
135 | */ |
136 | function _civicrm_api3_address_get_spec(&$params) { | |
cf8f0fff | 137 | $params['world_region'] = [ |
c7130c3e CW |
138 | 'title' => ts('World Region'), |
139 | 'name' => 'world_region', | |
b8326bd2 | 140 | 'type' => CRM_Utils_Type::T_TEXT, |
cf8f0fff | 141 | ]; |
6a488035 TO |
142 | } |
143 | ||
144 | /** | |
2e66abf8 | 145 | * Delete an existing Address. |
6a488035 | 146 | * |
cf470720 | 147 | * @param array $params |
2e66abf8 | 148 | * Array per getfields metadata. |
6a488035 | 149 | * |
a6c01b45 | 150 | * @return array |
00f8641b | 151 | * API result array |
6a488035 | 152 | */ |
7c285037 | 153 | function civicrm_api3_address_delete($params) { |
6a488035 TO |
154 | return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
155 | } | |
156 | ||
157 | /** | |
2e66abf8 | 158 | * Retrieve one or more addresses. |
6a488035 | 159 | * |
cf470720 | 160 | * @param array $params |
2e66abf8 | 161 | * Array per getfields metadata. |
6a488035 | 162 | * |
a6c01b45 | 163 | * @return array |
00f8641b | 164 | * API result array |
6a488035 TO |
165 | */ |
166 | function civicrm_api3_address_get(&$params) { | |
167 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Address'); | |
168 | } |