3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * This api exposes CiviCRM Address records.
15 * @package CiviCRM_APIv3
19 * Add an Address for a contact.
21 * FIXME: Should be using basic_create util
23 * @param array $params
24 * Array per getfields metadata.
29 function civicrm_api3_address_create($params) {
30 _civicrm_api3_check_edit_permissions('CRM_Core_BAO_Address', $params);
32 * If street_parsing, street_address has to be parsed into
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']
42 if (array_key_exists('street_name', $parsedItems)) {
43 $params['street_name'] = $parsedItems['street_name'];
45 if (array_key_exists('street_unit', $parsedItems)) {
46 $params['street_unit'] = $parsedItems['street_unit'];
48 if (array_key_exists('street_number', $parsedItems)) {
49 $params['street_number'] = $parsedItems['street_number'];
51 if (array_key_exists('street_number_suffix', $parsedItems)) {
52 $params['street_number_suffix'] = $parsedItems['street_number_suffix'];
59 if (!isset($params['check_permissions'])) {
60 $params['check_permissions'] = 0;
63 if (!isset($params['fix_address'])) {
64 $params['fix_address'] = TRUE;
68 * Create array for BAO (expects address params in as an
69 * element in array 'address'
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 ");
76 $values = _civicrm_api3_dao_to_array($addressBAO, $params);
77 return civicrm_api3_create_success($values, $params, 'Address', $addressBAO);
82 * Adjust Metadata for Create action.
84 * @param array $params
85 * Array of parameters determined by getfields.
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
,
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
,
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,
107 $params['world_region'] = [
108 'title' => ts('World Region'),
109 'name' => 'world_region',
110 'type' => CRM_Utils_Type
::T_TEXT
,
112 $defaultLocation = CRM_Core_BAO_LocationType
::getDefault();
113 if ($defaultLocation) {
114 $params['location_type_id']['api.default'] = $defaultLocation->id
;
119 * Adjust Metadata for Get action.
121 * @param array $params
122 * Array of parameters determined by getfields.
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
,
133 * Delete an existing Address.
135 * @param array $params
136 * Array per getfields metadata.
141 function civicrm_api3_address_delete($params) {
142 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
146 * Retrieve one or more addresses.
148 * @param array $params
149 * Array per getfields metadata.
154 function civicrm_api3_address_get($params) {
155 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, TRUE, 'Address');