api test for CRM-16796
[civicrm-core.git] / api / v3 / Address.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
244bbdd8 29 * This api exposes CiviCRM Address records.
6a488035
TO
30 *
31 * @package CiviCRM_APIv3
6a488035
TO
32 */
33
6a488035 34/**
22242c87 35 * Add an Address for a contact.
6a488035 36 *
c490a46a 37 * @param array $params
2e66abf8 38 * Array per getfields metadata.
77b97be7 39 *
a6c01b45 40 * @return array
6a488035
TO
41 */
42function civicrm_api3_address_create(&$params) {
43 /**
100fef9d 44 * If street_parsing, street_address has to be parsed into
6a488035
TO
45 * separate parts
46 */
47 if (array_key_exists('street_parsing', $params)) {
48 if ($params['street_parsing'] == 1) {
49 if (array_key_exists('street_address', $params)) {
50 if (!empty($params['street_address'])) {
51 $parsedItems = CRM_Core_BAO_Address::parseStreetAddress(
52 $params['street_address']
53 );
54 if (array_key_exists('street_name', $parsedItems)) {
55 $params['street_name'] = $parsedItems['street_name'];
56 }
57 if (array_key_exists('street_unit', $parsedItems)) {
58 $params['street_unit'] = $parsedItems['street_unit'];
59 }
60 if (array_key_exists('street_number', $parsedItems)) {
61 $params['street_number'] = $parsedItems['street_number'];
62 }
63 if (array_key_exists('street_number_suffix', $parsedItems)) {
64 $params['street_number_suffix'] = $parsedItems['street_number_suffix'];
65 }
66 }
67 }
68 }
69 }
70
71 /**
c490a46a
CW
72 * Create array for BAO (expects address params in as an
73 * element in array 'address'
74 */
6a488035
TO
75 $addressBAO = CRM_Core_BAO_Address::add($params, TRUE);
76 if (empty($addressBAO)) {
77 return civicrm_api3_create_error("Address is not created or updated ");
78 }
79 else {
6a488035 80 $values = _civicrm_api3_dao_to_array($addressBAO, $params);
244bbdd8 81 return civicrm_api3_create_success($values, $params, 'Address', $addressBAO);
6a488035
TO
82 }
83}
84
11e09c59 85/**
0aa0303c 86 * Adjust Metadata for Create action.
6a488035 87 *
cf470720 88 * @param array $params
b081365f 89 * Array of parameters determined by getfields.
6a488035
TO
90 */
91function _civicrm_api3_address_create_spec(&$params) {
92 $params['location_type_id']['api.required'] = 1;
93 $params['contact_id']['api.required'] = 1;
b2ed8e73
CW
94 $params['street_parsing'] = array(
95 'title' => 'Street Address Parsing',
96 'description' => 'Optional param to indicate you want the street_address field parsed into individual params',
d142432b 97 'type' => CRM_Utils_Type::T_BOOLEAN,
b2ed8e73 98 );
c7130c3e
CW
99 $params['world_region'] = array(
100 'title' => ts('World Region'),
101 'name' => 'world_region',
b8326bd2 102 'type' => CRM_Utils_Type::T_TEXT,
c7130c3e
CW
103 );
104}
105/**
2e66abf8 106 * Adjust Metadata for Get action.
c7130c3e 107 *
cf470720 108 * @param array $params
2e66abf8 109 * Array of parameters determined by getfields.
c7130c3e
CW
110 */
111function _civicrm_api3_address_get_spec(&$params) {
112 $params['world_region'] = array(
113 'title' => ts('World Region'),
114 'name' => 'world_region',
b8326bd2 115 'type' => CRM_Utils_Type::T_TEXT,
c7130c3e 116 );
6a488035
TO
117}
118
119/**
2e66abf8 120 * Delete an existing Address.
6a488035 121 *
cf470720 122 * @param array $params
2e66abf8 123 * Array per getfields metadata.
6a488035 124 *
a6c01b45 125 * @return array
72b3a70c 126 * api result array
6a488035 127 */
7c285037 128function civicrm_api3_address_delete($params) {
6a488035
TO
129 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
130}
131
132/**
2e66abf8 133 * Retrieve one or more addresses.
6a488035 134 *
cf470720 135 * @param array $params
2e66abf8 136 * Array per getfields metadata.
6a488035 137 *
a6c01b45 138 * @return array
72b3a70c 139 * details of found addresses else error
6a488035
TO
140 */
141function civicrm_api3_address_get(&$params) {
142 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Address');
143}