Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-02-09-11-44-07
[civicrm-core.git] / api / v3 / Address.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 * @param array $params
38 * Array per getfields metadata.
39 *
40 * @return array
41 * Array of newly created tag property values.
42 */
43 function civicrm_api3_address_create(&$params) {
44 /**
45 * If street_parsing, street_address has to be parsed into
46 * separate parts
47 */
48 if (array_key_exists('street_parsing', $params)) {
49 if ($params['street_parsing'] == 1) {
50 if (array_key_exists('street_address', $params)) {
51 if (!empty($params['street_address'])) {
52 $parsedItems = CRM_Core_BAO_Address::parseStreetAddress(
53 $params['street_address']
54 );
55 if (array_key_exists('street_name', $parsedItems)) {
56 $params['street_name'] = $parsedItems['street_name'];
57 }
58 if (array_key_exists('street_unit', $parsedItems)) {
59 $params['street_unit'] = $parsedItems['street_unit'];
60 }
61 if (array_key_exists('street_number', $parsedItems)) {
62 $params['street_number'] = $parsedItems['street_number'];
63 }
64 if (array_key_exists('street_number_suffix', $parsedItems)) {
65 $params['street_number_suffix'] = $parsedItems['street_number_suffix'];
66 }
67 }
68 }
69 }
70 }
71
72 /**
73 * Create array for BAO (expects address params in as an
74 * element in array 'address'
75 */
76 $addressBAO = CRM_Core_BAO_Address::add($params, TRUE);
77 if (empty($addressBAO)) {
78 return civicrm_api3_create_error("Address is not created or updated ");
79 }
80 else {
81 $values = _civicrm_api3_dao_to_array($addressBAO, $params);
82 return civicrm_api3_create_success($values, $params, 'address', $addressBAO);
83 }
84 }
85
86 /**
87 * Adjust Metadata for Create action.
88 *
89 * @param array $params
90 * Array of parameters determined by getfields.
91 */
92 function _civicrm_api3_address_create_spec(&$params) {
93 $params['location_type_id']['api.required'] = 1;
94 $params['contact_id']['api.required'] = 1;
95 $params['country'] = array('title' => 'Name or 2-letter abbreviation of country. Looked up in civicrm_country table');
96 $params['street_parsing'] = array('title' => 'optional param to indicate you want the street_address field parsed into individual params');
97 $params['world_region'] = array(
98 'title' => ts('World Region'),
99 'name' => 'world_region',
100 'type' => CRM_Utils_Type::T_TEXT,
101 );
102 }
103 /**
104 * Adjust Metadata for Get action.
105 *
106 * @param array $params
107 * Array of parameters determined by getfields.
108 */
109 function _civicrm_api3_address_get_spec(&$params) {
110 $params['world_region'] = array(
111 'title' => ts('World Region'),
112 'name' => 'world_region',
113 'type' => CRM_Utils_Type::T_TEXT,
114 );
115 }
116
117 /**
118 * Delete an existing Address.
119 *
120 * @param array $params
121 * Array per getfields metadata.
122 *
123 * @return array
124 * api result array
125 */
126 function civicrm_api3_address_delete($params) {
127 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
128 }
129
130 /**
131 * Retrieve one or more addresses.
132 *
133 * @param array $params
134 * Array per getfields metadata.
135 *
136 * @return array
137 * details of found addresses else error
138 */
139 function civicrm_api3_address_get(&$params) {
140 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Address');
141 }