| 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 | $bao = new CRM_Core_BAO_Domain(); |
| 32 | if (!empty($params['current_domain'])) { |
| 33 | $domainBAO = CRM_Core_Config::domainID(); |
| 34 | $params['id'] = $domainBAO; |
| 35 | } |
| 36 | if (!empty($params['options']) && !empty($params['options']['is_count'])) { |
| 37 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 38 | } |
| 39 | |
| 40 | _civicrm_api3_dao_set_filter($bao, $params, TRUE); |
| 41 | $domains = _civicrm_api3_dao_to_array($bao, $params, TRUE, 'Domain'); |
| 42 | |
| 43 | foreach ($domains as $domain) { |
| 44 | if (!empty($domain['contact_id'])) { |
| 45 | $values = []; |
| 46 | $locparams = [ |
| 47 | 'contact_id' => $domain['contact_id'], |
| 48 | ]; |
| 49 | $values['location'] = CRM_Core_BAO_Location::getValues($locparams, TRUE); |
| 50 | $address_array = [ |
| 51 | 'street_address', 'supplemental_address_1', 'supplemental_address_2', 'supplemental_address_3', |
| 52 | 'city', 'state_province_id', 'postal_code', 'country_id', |
| 53 | 'geo_code_1', 'geo_code_2', |
| 54 | ]; |
| 55 | |
| 56 | if (!empty($values['location']['email'])) { |
| 57 | $domain['domain_email'] = $values['location']['email'][1]['email'] ?? NULL; |
| 58 | } |
| 59 | |
| 60 | if (!empty($values['location']['phone'])) { |
| 61 | $domain['domain_phone'] = [ |
| 62 | 'phone_type' => CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_Phone', 'phone_type_id', |
| 63 | CRM_Utils_Array::value('phone_type_id', $values['location']['phone'][1])), |
| 64 | 'phone' => $values['location']['phone'][1]['phone'] ?? NULL, |
| 65 | ]; |
| 66 | } |
| 67 | |
| 68 | if (!empty($values['location']['address'])) { |
| 69 | foreach ($address_array as $value) { |
| 70 | $domain['domain_address'][$value] = CRM_Utils_Array::value($value, |
| 71 | $values['location']['address'][1] |
| 72 | ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | list($domain['from_name'], |
| 77 | $domain['from_email'] |
| 78 | ) = CRM_Core_BAO_Domain::getNameAndEmail(TRUE); |
| 79 | |
| 80 | // Rename version to domain_version, see CRM-17430. |
| 81 | $domain['domain_version'] = $domain['version']; |
| 82 | unset($domain['version']); |
| 83 | $domains[$domain['id']] = array_merge($domains[$domain['id']], $domain); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return civicrm_api3_create_success($domains, $params, 'Domain', 'get', $bao); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Adjust Metadata for Get action. |
| 92 | * |
| 93 | * The metadata is used for setting defaults, documentation & validation. |
| 94 | * |
| 95 | * @param array $params |
| 96 | * Array of parameters determined by getfields. |
| 97 | */ |
| 98 | function _civicrm_api3_domain_get_spec(&$params) { |
| 99 | $params['current_domain'] = [ |
| 100 | 'title' => "Current Domain", |
| 101 | 'description' => "get loaded domain", |
| 102 | ]; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Create a new Domain. |
| 107 | * |
| 108 | * @param array $params |
| 109 | * |
| 110 | * @return array |
| 111 | */ |
| 112 | function civicrm_api3_domain_create($params) { |
| 113 | if (isset($params['domain_version'])) { |
| 114 | $params['version'] = $params['domain_version']; |
| 115 | } |
| 116 | else { |
| 117 | unset($params['version']); |
| 118 | } |
| 119 | $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Domain'); |
| 120 | |
| 121 | $result_value = CRM_Utils_Array::first($result['values']); |
| 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 | } |
| 128 | return $result; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Adjust Metadata for Create action. |
| 133 | * |
| 134 | * The metadata is used for setting defaults, documentation & validation. |
| 135 | * |
| 136 | * @param array $params |
| 137 | * Array of parameters determined by getfields. |
| 138 | */ |
| 139 | function _civicrm_api3_domain_create_spec(&$params) { |
| 140 | $params['domain_version'] = [ |
| 141 | 'title' => "CiviCRM Version", |
| 142 | 'description' => "The civicrm version this instance is running", |
| 143 | 'type' => CRM_Utils_Type::T_STRING, |
| 144 | ]; |
| 145 | $params['domain_version']['api.required'] = 1; |
| 146 | unset($params['version']); |
| 147 | $params['name']['api.required'] = 1; |
| 148 | } |