Merge pull request #23892 from eileenmcnaughton/order
[civicrm-core.git] / api / v3 / Domain.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM Domain configuration settings.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Get CiviCRM Domain details.
20 *
21 * @param array $params
22 *
23 * @return array
24 * @throws \API_Exception
25 */
26 function civicrm_api3_domain_get($params) {
27
28 $params['version'] = $params['domain_version'] ?? NULL;
29 unset($params['version']);
30
31 if (!empty($params['current_domain'])) {
32 $params['id'] = CRM_Core_Config::domainID();
33 }
34 if (!empty($params['options']) && !empty($params['options']['is_count'])) {
35 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
36 }
37
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 }
48
49 foreach ($domains as $domain) {
50 if (!empty($domain['contact_id'])) {
51 $values = [];
52 $locparams = [
53 'contact_id' => $domain['contact_id'],
54 ];
55 $values['location'] = CRM_Core_BAO_Location::getValues($locparams, TRUE);
56 $address_array = [
57 'street_address', 'supplemental_address_1', 'supplemental_address_2', 'supplemental_address_3',
58 'city', 'state_province_id', 'postal_code', 'country_id',
59 'geo_code_1', 'geo_code_2',
60 ];
61
62 if (!empty($values['location']['email'])) {
63 $domain['domain_email'] = $values['location']['email'][1]['email'] ?? NULL;
64 }
65
66 if (!empty($values['location']['phone'])) {
67 $domain['domain_phone'] = [
68 'phone_type' => CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_Phone', 'phone_type_id',
69 CRM_Utils_Array::value('phone_type_id', $values['location']['phone'][1])),
70 'phone' => $values['location']['phone'][1]['phone'] ?? NULL,
71 ];
72 }
73
74 if (!empty($values['location']['address'])) {
75 foreach ($address_array as $value) {
76 $domain['domain_address'][$value] = CRM_Utils_Array::value($value,
77 $values['location']['address'][1]
78 );
79 }
80 }
81
82 list($domain['from_name'],
83 $domain['from_email']
84 ) = CRM_Core_BAO_Domain::getNameAndEmail(TRUE);
85
86 // Rename version to domain_version, see CRM-17430.
87 $domain['domain_version'] = $domain['version'];
88 unset($domain['version']);
89 $domains[$domain['id']] = array_merge($domains[$domain['id']], $domain);
90 }
91 }
92
93 return civicrm_api3_create_success($domains, $params, 'Domain', 'get', $bao);
94 }
95
96 /**
97 * Adjust Metadata for Get action.
98 *
99 * The metadata is used for setting defaults, documentation & validation.
100 *
101 * @param array $params
102 * Array of parameters determined by getfields.
103 */
104 function _civicrm_api3_domain_get_spec(&$params) {
105 $params['current_domain'] = [
106 'title' => "Current Domain",
107 'description' => "get loaded domain",
108 ];
109 }
110
111 /**
112 * Create a new Domain.
113 *
114 * @param array $params
115 *
116 * @return array
117 */
118 function civicrm_api3_domain_create($params) {
119 if (isset($params['domain_version'])) {
120 $params['version'] = $params['domain_version'];
121 }
122 else {
123 unset($params['version']);
124 }
125 $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Domain');
126
127 $result_value = CRM_Utils_Array::first($result['values']);
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 }
134 return $result;
135 }
136
137 /**
138 * Adjust Metadata for Create action.
139 *
140 * The metadata is used for setting defaults, documentation & validation.
141 *
142 * @param array $params
143 * Array of parameters determined by getfields.
144 */
145 function _civicrm_api3_domain_create_spec(&$params) {
146 $params['domain_version'] = [
147 'title' => "CiviCRM Version",
148 'description' => "The civicrm version this instance is running",
149 'type' => CRM_Utils_Type::T_STRING,
150 ];
151 $params['domain_version']['api.required'] = 1;
152 unset($params['version']);
153 $params['name']['api.required'] = 1;
154 }