getfields api - ensure 'name' property is set for every field
[civicrm-core.git] / api / v3 / Domain.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 * File for the CiviCRM APIv3 domain functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Domain
33 *
34 * @copyright CiviCRM LLC (c) 2004-2014
35 * @version $Id: Domain.php 30171 2010-10-14 09:11:27Z mover $
36 */
37
38 /**
39 * Get CiviCRM domain details.
40 *
41 * @param array $params
42 *
43 * @return array
44 * @throws \API_Exception
45 */
46 function civicrm_api3_domain_get($params) {
47
48 $params['version'] = CRM_Utils_Array::value('domain_version', $params);
49 unset($params['version']);
50
51 $bao = new CRM_Core_BAO_Domain();
52 if (!empty($params['current_domain'])) {
53 $domainBAO = CRM_Core_Config::domainID();
54 $params['id'] = $domainBAO;
55 }
56 if (!empty($params['options']) && !empty($params['options']['is_count'])) {
57 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
58 }
59
60 _civicrm_api3_dao_set_filter($bao, $params, TRUE, 'domain');
61 $domains = _civicrm_api3_dao_to_array($bao, $params, TRUE, 'domain');
62
63 foreach ($domains as $domain) {
64 if (!empty($domain['contact_id'])) {
65 $values = array();
66 $locparams = array(
67 'contact_id' => $domain['contact_id'],
68 );
69 $values['location'] = CRM_Core_BAO_Location::getValues($locparams, TRUE);
70
71 $address_array = array(
72 'street_address', 'supplemental_address_1', 'supplemental_address_2',
73 'city', 'state_province_id', 'postal_code', 'country_id',
74 'geo_code_1', 'geo_code_2',
75 );
76
77 if (!empty($values['location']['email'])) {
78 $domain['domain_email'] = CRM_Utils_Array::value('email', $values['location']['email'][1]);
79 }
80
81 if (!empty($values['location']['phone'])) {
82 $domain['domain_phone'] = array(
83 'phone_type' => CRM_Core_OptionGroup::getLabel(
84 'phone_type',
85 CRM_Utils_Array::value(
86 'phone_type_id',
87 $values['location']['phone'][1]
88 )
89 ),
90 'phone' => CRM_Utils_Array::value(
91 'phone',
92 $values['location']['phone'][1]
93 ),
94 );
95 }
96
97 if (!empty($values['location']['address'])) {
98 foreach ($address_array as $value) {
99 $domain['domain_address'][$value] = CRM_Utils_Array::value($value,
100 $values['location']['address'][1]
101 );
102 }
103 }
104
105 list($domain['from_name'],
106 $domain['from_email']
107 ) = CRM_Core_BAO_Domain::getNameAndEmail(TRUE);
108 $domains[$domain['id']] = array_merge($domains[$domain['id']], $domain);
109 }
110 }
111
112 return civicrm_api3_create_success($domains, $params, 'domain', 'get', $bao);
113 }
114
115 /**
116 * Adjust Metadata for Get action.
117 *
118 * The metadata is used for setting defaults, documentation & validation.
119 *
120 * @param array $params
121 * Array or parameters determined by getfields.
122 */
123 function _civicrm_api3_domain_get_spec(&$params) {
124 $params['current_domain'] = array('title' => "get loaded domain");
125 }
126
127 /**
128 * Create a new domain.
129 *
130 * @param array $params
131 *
132 * @return array
133 */
134 function civicrm_api3_domain_create($params) {
135 $params['version'] = $params['domain_version'];
136 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
137 }
138
139 /**
140 * Adjust Metadata for Create action.
141 *
142 * The metadata is used for setting defaults, documentation & validation.
143 *
144 * @param array $params
145 * Array or parameters determined by getfields.
146 */
147 function _civicrm_api3_domain_create_spec(&$params) {
148 $params['domain_version'] = $params['version'];
149 $params['domain_version']['api.required'] = 1;
150 unset($params['version']);
151 $params['name']['api.required'] = 1;
152 }