CRM-20091 - Add customValue.gettree api
[civicrm-core.git] / api / v3 / Address.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 /**
29 * This api exposes CiviCRM Address records.
30 *
31 * @package CiviCRM_APIv3
32 */
33
34 /**
35 * Add an Address for a contact.
36 *
37 * FIXME: Should be using basic_create util
38 *
39 * @param array $params
40 * Array per getfields metadata.
41 *
42 * @return array
43 * API result array
44 */
45 function civicrm_api3_address_create(&$params) {
46 _civicrm_api3_check_edit_permissions('CRM_Core_BAO_Address', $params);
47 /**
48 * If street_parsing, street_address has to be parsed into
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
75 if (!isset($params['check_permissions'])) {
76 $params['check_permissions'] = 0;
77 }
78
79 /**
80 * Create array for BAO (expects address params in as an
81 * element in array 'address'
82 */
83 $addressBAO = CRM_Core_BAO_Address::add($params, TRUE);
84 if (empty($addressBAO)) {
85 return civicrm_api3_create_error("Address is not created or updated ");
86 }
87 else {
88 $values = _civicrm_api3_dao_to_array($addressBAO, $params);
89 return civicrm_api3_create_success($values, $params, 'Address', $addressBAO);
90 }
91 }
92
93 /**
94 * Adjust Metadata for Create action.
95 *
96 * @param array $params
97 * Array of parameters determined by getfields.
98 */
99 function _civicrm_api3_address_create_spec(&$params) {
100 $params['location_type_id']['api.required'] = 1;
101 $params['contact_id']['api.required'] = 1;
102 $params['street_parsing'] = array(
103 'title' => 'Street Address Parsing',
104 'description' => 'Optional param to indicate you want the street_address field parsed into individual params',
105 'type' => CRM_Utils_Type::T_BOOLEAN,
106 );
107 $params['skip_geocode'] = array(
108 'title' => 'Skip geocode',
109 'description' => 'Optional param to indicate you want to skip geocoding (useful when importing a lot of addresses
110 at once, the job \'Geocode and Parse Addresses\' can execute this task after the import)',
111 'type' => CRM_Utils_Type::T_BOOLEAN,
112 );
113 $params['world_region'] = array(
114 'title' => ts('World Region'),
115 'name' => 'world_region',
116 'type' => CRM_Utils_Type::T_TEXT,
117 );
118 }
119
120 /**
121 * Adjust Metadata for Get action.
122 *
123 * @param array $params
124 * Array of parameters determined by getfields.
125 */
126 function _civicrm_api3_address_get_spec(&$params) {
127 $params['world_region'] = array(
128 'title' => ts('World Region'),
129 'name' => 'world_region',
130 'type' => CRM_Utils_Type::T_TEXT,
131 );
132 }
133
134 /**
135 * Delete an existing Address.
136 *
137 * @param array $params
138 * Array per getfields metadata.
139 *
140 * @return array
141 * API result array
142 */
143 function civicrm_api3_address_delete($params) {
144 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
145 }
146
147 /**
148 * Retrieve one or more addresses.
149 *
150 * @param array $params
151 * Array per getfields metadata.
152 *
153 * @return array
154 * API result array
155 */
156 function civicrm_api3_address_get(&$params) {
157 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Address');
158 }