Merge pull request #4629 from systopia/CRM-15665
[civicrm-core.git] / api / v3 / Address.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * File for the CiviCRM APIv3 address functions
31 *
32 * @package CiviCRM_APIv3
33 * @subpackage API_Address
34 *
35 * @copyright CiviCRM LLC (c) 2004-2014
36 * @version $Id: Address.php 2011-02-16 ErikHommel $
37 */
38
39 /**
40 * Add an Address for a contact
41 *
42 * Allowed @params array keys are:
43 * {@getfields address_create}
44 * {@example AddressCreate.php}
45 *
46 * @param array $params
47 *
48 * @return array of newly created tag property values.
49 * @access public
50 */
51 function civicrm_api3_address_create(&$params) {
52 /**
53 * If street_parsing, street_address has to be parsed into
54 * separate parts
55 */
56 if (array_key_exists('street_parsing', $params)) {
57 if ($params['street_parsing'] == 1) {
58 if (array_key_exists('street_address', $params)) {
59 if (!empty($params['street_address'])) {
60 $parsedItems = CRM_Core_BAO_Address::parseStreetAddress(
61 $params['street_address']
62 );
63 if (array_key_exists('street_name', $parsedItems)) {
64 $params['street_name'] = $parsedItems['street_name'];
65 }
66 if (array_key_exists('street_unit', $parsedItems)) {
67 $params['street_unit'] = $parsedItems['street_unit'];
68 }
69 if (array_key_exists('street_number', $parsedItems)) {
70 $params['street_number'] = $parsedItems['street_number'];
71 }
72 if (array_key_exists('street_number_suffix', $parsedItems)) {
73 $params['street_number_suffix'] = $parsedItems['street_number_suffix'];
74 }
75 }
76 }
77 }
78 }
79
80 /**
81 * Create array for BAO (expects address params in as an
82 * element in array 'address'
83 */
84 $addressBAO = CRM_Core_BAO_Address::add($params, TRUE);
85 if (empty($addressBAO)) {
86 return civicrm_api3_create_error("Address is not created or updated ");
87 }
88 else {
89 $values = array();
90 $values = _civicrm_api3_dao_to_array($addressBAO, $params);
91 return civicrm_api3_create_success($values, $params, 'address', $addressBAO);
92 }
93 }
94
95 /**
96 * Adjust Metadata for Create action
97 *
98 * @param array $params array or parameters determined by getfields
99 */
100 function _civicrm_api3_address_create_spec(&$params) {
101 $params['location_type_id']['api.required'] = 1;
102 $params['contact_id']['api.required'] = 1;
103 $params['country'] = array('title' => 'Name or 2-letter abbreviation of country. Looked up in civicrm_country table');
104 $params['street_parsing'] = array('title' => 'optional param to indicate you want the street_address field parsed into individual params');
105 $params['world_region'] = array(
106 'title' => ts('World Region'),
107 'name' => 'world_region',
108 'type' => CRM_Utils_Type::T_TEXT,
109 );
110 }
111 /**
112 * Adjust Metadata for Get action
113 *
114 * @param array $params array or parameters determined by getfields
115 */
116 function _civicrm_api3_address_get_spec(&$params) {
117 $params['world_region'] = array(
118 'title' => ts('World Region'),
119 'name' => 'world_region',
120 'type' => CRM_Utils_Type::T_TEXT,
121 );
122 }
123
124 /**
125 * Deletes an existing Address
126 *
127 * @param array $params
128 *
129 * {@getfields address_delete}
130 * {@example AddressDelete.php 0}
131 *
132 * @return array api result array
133 * @access public
134 */
135 function civicrm_api3_address_delete($params) {
136 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
137 }
138
139 /**
140 * Retrieve one or more addresses on address_id, contact_id, street_name, city
141 * or a combination of those
142 *
143 * @param mixed[] (reference ) input parameters
144 *
145 * {@example AddressGet.php 0}
146 * @param array $params an associative array of name/value pairs.
147 *
148 * @return array details of found addresses else error
149 * {@getfields address_get}
150 * @access public
151 */
152 function civicrm_api3_address_get(&$params) {
153 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Address');
154 }
155