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