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