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