INFRA-132 - Batch 14 (g)
[civicrm-core.git] / api / v3 / Address.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
731a0992 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 * File for the CiviCRM APIv3 address functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Address
33 *
731a0992 34 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
35 * @version $Id: Address.php 2011-02-16 ErikHommel $
36 */
37
6a488035
TO
38/**
39 * Add an Address for a contact
40 *
41 * Allowed @params array keys are:
42 * {@getfields address_create}
43 * {@example AddressCreate.php}
44 *
c490a46a 45 * @param array $params
77b97be7 46 *
a6c01b45 47 * @return array
16b10e64 48 * Array of newly created tag property values.
6a488035
TO
49 */
50function civicrm_api3_address_create(&$params) {
51 /**
100fef9d 52 * If street_parsing, street_address has to be parsed into
6a488035
TO
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 /**
c490a46a
CW
80 * Create array for BAO (expects address params in as an
81 * element in array 'address'
82 */
6a488035
TO
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 {
6a488035
TO
88 $values = _civicrm_api3_dao_to_array($addressBAO, $params);
89 return civicrm_api3_create_success($values, $params, 'address', $addressBAO);
90 }
91}
92
11e09c59 93/**
6a488035
TO
94 * Adjust Metadata for Create action
95 *
cf470720
TO
96 * @param array $params
97 * Array or parameters determined by getfields.
6a488035
TO
98 */
99function _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');
c7130c3e
CW
104 $params['world_region'] = array(
105 'title' => ts('World Region'),
106 'name' => 'world_region',
b8326bd2 107 'type' => CRM_Utils_Type::T_TEXT,
c7130c3e
CW
108 );
109}
110/**
111 * Adjust Metadata for Get action
112 *
cf470720
TO
113 * @param array $params
114 * Array or parameters determined by getfields.
c7130c3e
CW
115 */
116function _civicrm_api3_address_get_spec(&$params) {
117 $params['world_region'] = array(
118 'title' => ts('World Region'),
119 'name' => 'world_region',
b8326bd2 120 'type' => CRM_Utils_Type::T_TEXT,
c7130c3e 121 );
6a488035
TO
122}
123
124/**
125 * Deletes an existing Address
126 *
cf470720 127 * @param array $params
6a488035
TO
128 *
129 * {@getfields address_delete}
130 * {@example AddressDelete.php 0}
131 *
a6c01b45 132 * @return array
72b3a70c 133 * api result array
6a488035 134 */
7c285037 135function civicrm_api3_address_delete($params) {
6a488035
TO
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 *
cf470720 143 * @param mixed[] (reference ) input parameters
6a488035
TO
144 *
145 * {@example AddressGet.php 0}
cf470720
TO
146 * @param array $params
147 * An associative array of name/value pairs.
6a488035 148 *
a6c01b45 149 * @return array
72b3a70c 150 * details of found addresses else error
6a488035 151 * {@getfields address_get}
6a488035
TO
152 */
153function civicrm_api3_address_get(&$params) {
154 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Address');
155}