(NFC) (dev/core#878) Simplify copyright header (tests/*)
[civicrm-core.git] / api / v3 / Domain.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
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/**
244bbdd8 29 * This api exposes CiviCRM Domain configuration settings.
6a488035
TO
30 *
31 * @package CiviCRM_APIv3
6a488035
TO
32 */
33
34/**
244bbdd8 35 * Get CiviCRM Domain details.
9d32e6f7 36 *
d0997921 37 * @param array $params
9d32e6f7 38 *
645ee340
EM
39 * @return array
40 * @throws \API_Exception
6a488035
TO
41 */
42function civicrm_api3_domain_get($params) {
43
395d8dc6 44 $params['version'] = CRM_Utils_Array::value('domain_version', $params);
6a488035
TO
45 unset($params['version']);
46
47 $bao = new CRM_Core_BAO_Domain();
a7488080 48 if (!empty($params['current_domain'])) {
6a488035
TO
49 $domainBAO = CRM_Core_Config::domainID();
50 $params['id'] = $domainBAO;
51 }
461c9a60
EM
52 if (!empty($params['options']) && !empty($params['options']['is_count'])) {
53 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
54 }
6a488035 55
244bbdd8
CW
56 _civicrm_api3_dao_set_filter($bao, $params, TRUE);
57 $domains = _civicrm_api3_dao_to_array($bao, $params, TRUE, 'Domain');
6a488035
TO
58
59 foreach ($domains as $domain) {
9b873358 60 if (!empty($domain['contact_id'])) {
cf8f0fff
CW
61 $values = [];
62 $locparams = [
21dfd5f5 63 'contact_id' => $domain['contact_id'],
cf8f0fff 64 ];
6a488035 65 $values['location'] = CRM_Core_BAO_Location::getValues($locparams, TRUE);
cf8f0fff 66 $address_array = [
207f62c6 67 'street_address', 'supplemental_address_1', 'supplemental_address_2', 'supplemental_address_3',
6a488035
TO
68 'city', 'state_province_id', 'postal_code', 'country_id',
69 'geo_code_1', 'geo_code_2',
cf8f0fff 70 ];
6a488035 71
481a74f4 72 if (!empty($values['location']['email'])) {
6a488035
TO
73 $domain['domain_email'] = CRM_Utils_Array::value('email', $values['location']['email'][1]);
74 }
75
481a74f4 76 if (!empty($values['location']['phone'])) {
cf8f0fff 77 $domain['domain_phone'] = [
678bb176 78 'phone_type' => CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_Phone', 'phone_type_id',
7c31ae57
SL
79 CRM_Utils_Array::value('phone_type_id', $values['location']['phone'][1])),
80 'phone' => CRM_Utils_Array::value('phone', $values['location']['phone'][1]),
81 ];
35671d00 82 }
6a488035 83
481a74f4 84 if (!empty($values['location']['address'])) {
35671d00
TO
85 foreach ($address_array as $value) {
86 $domain['domain_address'][$value] = CRM_Utils_Array::value($value,
6a488035 87 $values['location']['address'][1]
35671d00
TO
88 );
89 }
6a488035 90 }
6a488035
TO
91
92 list($domain['from_name'],
93 $domain['from_email']
94 ) = CRM_Core_BAO_Domain::getNameAndEmail(TRUE);
73156a79
JV
95
96 // Rename version to domain_version, see CRM-17430.
97 $domain['domain_version'] = $domain['version'];
98 unset($domain['version']);
6a488035
TO
99 $domains[$domain['id']] = array_merge($domains[$domain['id']], $domain);
100 }
101 }
102
244bbdd8 103 return civicrm_api3_create_success($domains, $params, 'Domain', 'get', $bao);
6a488035 104}
11e09c59
TO
105
106/**
9d32e6f7 107 * Adjust Metadata for Get action.
6a488035 108 *
0aa0303c
EM
109 * The metadata is used for setting defaults, documentation & validation.
110 *
cf470720 111 * @param array $params
b081365f 112 * Array of parameters determined by getfields.
6a488035
TO
113 */
114function _civicrm_api3_domain_get_spec(&$params) {
cf8f0fff 115 $params['current_domain'] = [
b2ed8e73
CW
116 'title' => "Current Domain",
117 'description' => "get loaded domain",
cf8f0fff 118 ];
6a488035
TO
119}
120
121/**
244bbdd8 122 * Create a new Domain.
6a488035
TO
123 *
124 * @param array $params
125 *
126 * @return array
6a488035
TO
127 */
128function civicrm_api3_domain_create($params) {
753f36b3
JV
129 if (isset($params['domain_version'])) {
130 $params['version'] = $params['domain_version'];
131 }
132 else {
35a07880
JV
133 unset($params['version']);
134 }
ddaf2161 135 $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Domain');
73156a79 136
73156a79 137 $result_value = CRM_Utils_Array::first($result['values']);
9f954e37
JV
138 if (isset($result_value['version'])) {
139 // Rename version to domain_version, see CRM-17430.
140 $result_value['domain_version'] = $result_value['version'];
141 unset($result_value['version']);
142 $result['values'][$result['id']] = $result_value;
143 }
73156a79 144 return $result;
6a488035 145}
11e09c59
TO
146
147/**
0aa0303c
EM
148 * Adjust Metadata for Create action.
149 *
150 * The metadata is used for setting defaults, documentation & validation.
6a488035 151 *
cf470720 152 * @param array $params
b081365f 153 * Array of parameters determined by getfields.
6a488035
TO
154 */
155function _civicrm_api3_domain_create_spec(&$params) {
cf8f0fff 156 $params['domain_version'] = [
3301016d
JV
157 'title' => "CiviCRM Version",
158 'description' => "The civicrm version this instance is running",
5fe3f641 159 'type' => CRM_Utils_Type::T_STRING,
cf8f0fff 160 ];
6a488035
TO
161 $params['domain_version']['api.required'] = 1;
162 unset($params['version']);
163 $params['name']['api.required'] = 1;
164}