Merge pull request #17656 from civicrm/5.27
[civicrm-core.git] / api / v3 / Domain.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
a30c801b
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035
TO
9 +--------------------------------------------------------------------+
10 */
11
12/**
244bbdd8 13 * This api exposes CiviCRM Domain configuration settings.
6a488035
TO
14 *
15 * @package CiviCRM_APIv3
6a488035
TO
16 */
17
18/**
244bbdd8 19 * Get CiviCRM Domain details.
9d32e6f7 20 *
d0997921 21 * @param array $params
9d32e6f7 22 *
645ee340
EM
23 * @return array
24 * @throws \API_Exception
6a488035
TO
25 */
26function civicrm_api3_domain_get($params) {
27
f748c073 28 $params['version'] = $params['domain_version'] ?? NULL;
6a488035
TO
29 unset($params['version']);
30
31 $bao = new CRM_Core_BAO_Domain();
a7488080 32 if (!empty($params['current_domain'])) {
6a488035
TO
33 $domainBAO = CRM_Core_Config::domainID();
34 $params['id'] = $domainBAO;
35 }
461c9a60
EM
36 if (!empty($params['options']) && !empty($params['options']['is_count'])) {
37 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
38 }
6a488035 39
244bbdd8
CW
40 _civicrm_api3_dao_set_filter($bao, $params, TRUE);
41 $domains = _civicrm_api3_dao_to_array($bao, $params, TRUE, 'Domain');
6a488035
TO
42
43 foreach ($domains as $domain) {
9b873358 44 if (!empty($domain['contact_id'])) {
cf8f0fff
CW
45 $values = [];
46 $locparams = [
21dfd5f5 47 'contact_id' => $domain['contact_id'],
cf8f0fff 48 ];
6a488035 49 $values['location'] = CRM_Core_BAO_Location::getValues($locparams, TRUE);
cf8f0fff 50 $address_array = [
207f62c6 51 'street_address', 'supplemental_address_1', 'supplemental_address_2', 'supplemental_address_3',
6a488035
TO
52 'city', 'state_province_id', 'postal_code', 'country_id',
53 'geo_code_1', 'geo_code_2',
cf8f0fff 54 ];
6a488035 55
481a74f4 56 if (!empty($values['location']['email'])) {
f748c073 57 $domain['domain_email'] = $values['location']['email'][1]['email'] ?? NULL;
6a488035
TO
58 }
59
481a74f4 60 if (!empty($values['location']['phone'])) {
cf8f0fff 61 $domain['domain_phone'] = [
678bb176 62 'phone_type' => CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_Phone', 'phone_type_id',
7c31ae57 63 CRM_Utils_Array::value('phone_type_id', $values['location']['phone'][1])),
6b409353 64 'phone' => $values['location']['phone'][1]['phone'] ?? NULL,
7c31ae57 65 ];
35671d00 66 }
6a488035 67
481a74f4 68 if (!empty($values['location']['address'])) {
35671d00
TO
69 foreach ($address_array as $value) {
70 $domain['domain_address'][$value] = CRM_Utils_Array::value($value,
6a488035 71 $values['location']['address'][1]
35671d00
TO
72 );
73 }
6a488035 74 }
6a488035
TO
75
76 list($domain['from_name'],
77 $domain['from_email']
78 ) = CRM_Core_BAO_Domain::getNameAndEmail(TRUE);
73156a79
JV
79
80 // Rename version to domain_version, see CRM-17430.
81 $domain['domain_version'] = $domain['version'];
82 unset($domain['version']);
6a488035
TO
83 $domains[$domain['id']] = array_merge($domains[$domain['id']], $domain);
84 }
85 }
86
244bbdd8 87 return civicrm_api3_create_success($domains, $params, 'Domain', 'get', $bao);
6a488035 88}
11e09c59
TO
89
90/**
9d32e6f7 91 * Adjust Metadata for Get action.
6a488035 92 *
0aa0303c
EM
93 * The metadata is used for setting defaults, documentation & validation.
94 *
cf470720 95 * @param array $params
b081365f 96 * Array of parameters determined by getfields.
6a488035
TO
97 */
98function _civicrm_api3_domain_get_spec(&$params) {
cf8f0fff 99 $params['current_domain'] = [
b2ed8e73
CW
100 'title' => "Current Domain",
101 'description' => "get loaded domain",
cf8f0fff 102 ];
6a488035
TO
103}
104
105/**
244bbdd8 106 * Create a new Domain.
6a488035
TO
107 *
108 * @param array $params
109 *
110 * @return array
6a488035
TO
111 */
112function civicrm_api3_domain_create($params) {
753f36b3
JV
113 if (isset($params['domain_version'])) {
114 $params['version'] = $params['domain_version'];
115 }
116 else {
35a07880
JV
117 unset($params['version']);
118 }
ddaf2161 119 $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Domain');
73156a79 120
73156a79 121 $result_value = CRM_Utils_Array::first($result['values']);
9f954e37
JV
122 if (isset($result_value['version'])) {
123 // Rename version to domain_version, see CRM-17430.
124 $result_value['domain_version'] = $result_value['version'];
125 unset($result_value['version']);
126 $result['values'][$result['id']] = $result_value;
127 }
73156a79 128 return $result;
6a488035 129}
11e09c59
TO
130
131/**
0aa0303c
EM
132 * Adjust Metadata for Create action.
133 *
134 * The metadata is used for setting defaults, documentation & validation.
6a488035 135 *
cf470720 136 * @param array $params
b081365f 137 * Array of parameters determined by getfields.
6a488035
TO
138 */
139function _civicrm_api3_domain_create_spec(&$params) {
cf8f0fff 140 $params['domain_version'] = [
3301016d
JV
141 'title' => "CiviCRM Version",
142 'description' => "The civicrm version this instance is running",
5fe3f641 143 'type' => CRM_Utils_Type::T_STRING,
cf8f0fff 144 ];
6a488035
TO
145 $params['domain_version']['api.required'] = 1;
146 unset($params['version']);
147 $params['name']['api.required'] = 1;
148}