Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /* | |
5 | +--------------------------------------------------------------------+ | |
6 | | CiviCRM version 4.3 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
9 | +--------------------------------------------------------------------+ | |
10 | | This file is a part of CiviCRM. | | |
11 | | | | |
12 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
13 | | under the terms of the GNU Affero General Public License | | |
14 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
15 | | | | |
16 | | CiviCRM is distributed in the hope that it will be useful, but | | |
17 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
19 | | See the GNU Affero General Public License for more details. | | |
20 | | | | |
21 | | You should have received a copy of the GNU Affero General Public | | |
22 | | License and the CiviCRM Licensing Exception along | | |
23 | | with this program; if not, contact CiviCRM LLC | | |
24 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
25 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
26 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
27 | +--------------------------------------------------------------------+ | |
28 | */ | |
29 | ||
30 | /** | |
31 | * File for the CiviCRM APIv3 activity profile functions | |
32 | * | |
33 | * @package CiviCRM_APIv3 | |
34 | * @subpackage API_ActivityProfile | |
35 | * @copyright CiviCRM LLC (c) 2004-2013 | |
36 | * @version $Id: ActivityProfile.php 30486 2011-05-20 16:12:09Z rajan $ | |
37 | * | |
38 | */ | |
39 | ||
40 | /** | |
41 | * Include common API util functions | |
42 | */ | |
43 | require_once 'api/v3/utils.php'; | |
44 | ||
6a488035 TO |
45 | /** |
46 | * Retrieve Profile field values. | |
47 | * | |
48 | * @param array $params Associative array of property name/value | |
49 | * pairs to get profile field values | |
50 | * | |
51 | * @return Profile field values|CRM_Error | |
52 | * | |
53 | * @todo add example | |
54 | * @todo add test cases | |
55 | * | |
56 | */ | |
57 | function civicrm_api3_profile_get($params) { | |
58 | ||
59 | civicrm_api3_verify_mandatory($params, NULL, array('profile_id', 'contact_id')); | |
60 | ||
61 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) { | |
62 | return civicrm_api3_create_error('Invalid value for profile_id'); | |
63 | } | |
64 | ||
65 | $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($params['profile_id']); | |
66 | ||
67 | if (CRM_Core_BAO_UFField::checkProfileType($params['profile_id']) && !$isContactActivityProfile) { | |
68 | return civicrm_api3_create_error('Can not retrieve values for profiles include fields for more than one record type.'); | |
69 | } | |
70 | ||
71 | $profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'], | |
72 | FALSE, | |
73 | NULL, | |
74 | NULL, | |
75 | NULL, | |
76 | FALSE, | |
77 | NULL, | |
78 | TRUE, | |
79 | NULL, | |
80 | CRM_Core_Permission::EDIT | |
81 | ); | |
82 | ||
83 | $values = array(); | |
84 | if ($isContactActivityProfile) { | |
85 | civicrm_api3_verify_mandatory($params, NULL, array('activity_id')); | |
86 | ||
6a488035 TO |
87 | $errors = CRM_Profile_Form::validateContactActivityProfile($params['activity_id'], |
88 | $params['contact_id'], | |
89 | $params['profile_id'] | |
90 | ); | |
91 | if (!empty($errors)) { | |
92 | return civicrm_api3_create_error(array_pop($errors)); | |
93 | } | |
94 | ||
95 | $contactFields = $activityFields = array(); | |
96 | foreach ($profileFields as $fieldName => $field) { | |
97 | if (CRM_Utils_Array::value('field_type', $field) == 'Activity') { | |
98 | $activityFields[$fieldName] = $field; | |
99 | } | |
100 | else { | |
101 | $contactFields[$fieldName] = $field; | |
102 | } | |
103 | } | |
104 | ||
105 | CRM_Core_BAO_UFGroup::setProfileDefaults($params['contact_id'], $contactFields, $values, TRUE); | |
106 | ||
107 | if ($params['activity_id']) { | |
108 | CRM_Core_BAO_UFGroup::setComponentDefaults($activityFields, $params['activity_id'], 'Activity', $values, TRUE); | |
109 | } | |
110 | } | |
111 | else { | |
112 | CRM_Core_BAO_UFGroup::setProfileDefaults($params['contact_id'], $profileFields, $values, TRUE); | |
113 | } | |
114 | ||
115 | $result = civicrm_api3_create_success(); | |
116 | $result['values'] = $values; | |
117 | ||
118 | return $result; | |
119 | } | |
120 | ||
121 | /** | |
122 | * Update Profile field values. | |
123 | * | |
124 | * @param array $params Associative array of property name/value | |
125 | * pairs to update profile field values | |
126 | * | |
127 | * @return Updated Contact/ Activity object|CRM_Error | |
128 | * | |
129 | * @todo add example | |
130 | * @todo add test cases | |
131 | * | |
132 | */ | |
133 | function civicrm_api3_profile_set($params) { | |
134 | ||
135 | civicrm_api3_verify_mandatory($params, NULL, array('profile_id')); | |
136 | ||
137 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) { | |
138 | return civicrm_api3_create_error('Invalid value for profile_id'); | |
139 | } | |
140 | ||
141 | $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($params['profile_id']); | |
142 | ||
143 | if (CRM_Core_BAO_UFField::checkProfileType($params['profile_id']) && !$isContactActivityProfile) { | |
144 | return civicrm_api3_create_error('Can not retrieve values for profiles include fields for more than one record type.'); | |
145 | } | |
146 | ||
147 | $contactParams = $activityParams = $missingParams = array(); | |
148 | ||
149 | $profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'], | |
150 | FALSE, | |
151 | NULL, | |
152 | NULL, | |
153 | NULL, | |
154 | FALSE, | |
155 | NULL, | |
156 | TRUE, | |
157 | NULL, | |
158 | CRM_Core_Permission::EDIT | |
159 | ); | |
160 | ||
161 | if ($isContactActivityProfile) { | |
162 | civicrm_api3_verify_mandatory($params, NULL, array('activity_id')); | |
163 | ||
6a488035 TO |
164 | $errors = CRM_Profile_Form::validateContactActivityProfile($params['activity_id'], |
165 | $params['contact_id'], | |
166 | $params['profile_id'] | |
167 | ); | |
168 | if (!empty($errors)) { | |
169 | return civicrm_api3_create_error(array_pop($errors)); | |
170 | } | |
171 | } | |
172 | ||
173 | foreach ($profileFields as $fieldName => $field) { | |
174 | if (CRM_Utils_Array::value('is_required', $field)) { | |
175 | if (!CRM_Utils_Array::value($fieldName, $params) || empty($params[$fieldName])) { | |
176 | $missingParams[] = $fieldName; | |
177 | } | |
178 | } | |
179 | ||
180 | if (!isset($params[$fieldName])) { | |
181 | continue; | |
182 | } | |
183 | ||
184 | $value = $params[$fieldName]; | |
185 | if ($params[$fieldName] && isset($params[$fieldName . '_id'])) { | |
186 | $value = $params[$fieldName . '_id']; | |
187 | } | |
188 | ||
189 | if ($isContactActivityProfile && CRM_Utils_Array::value('field_type', $field) == 'Activity') { | |
190 | $activityParams[$fieldName] = $value; | |
191 | } | |
192 | else { | |
193 | $contactParams[$fieldName] = $value; | |
194 | } | |
195 | } | |
196 | ||
197 | if (!empty($missingParams)) { | |
198 | return civicrm_api3_create_error("Missing required parameters for profile id {$params['profile_id']}: " . implode(', ', $missingParams)); | |
199 | } | |
200 | ||
201 | $contactParams['version'] = 3; | |
202 | $contactParams['contact_id'] = CRM_Utils_Array::value('contact_id', $params); | |
203 | $contactParams['profile_id'] = $params['profile_id']; | |
204 | $contactParams['skip_custom'] = 1; | |
205 | ||
206 | $contactProfileParams = civicrm_api3_profile_apply($contactParams); | |
207 | if (CRM_Utils_Array::value('is_error', $contactProfileParams)) { | |
208 | return $contactProfileParams; | |
209 | } | |
210 | ||
211 | // Contact profile fields | |
212 | $profileParams = $contactProfileParams['values']; | |
213 | ||
214 | // If profile having activity fields | |
215 | if ($isContactActivityProfile && !empty($activityParams)) { | |
216 | $activityParams['id'] = $params['activity_id']; | |
217 | $profileParams['api.activity.create'] = $activityParams; | |
218 | } | |
219 | ||
220 | $groups = $tags = array(); | |
221 | if (isset($profileParams['group'])) { | |
222 | $groups = $profileParams['group']; | |
223 | unset($profileParams['group']); | |
224 | } | |
225 | ||
226 | if (isset($profileParams['tag'])) { | |
227 | $tags = $profileParams['tag']; | |
228 | unset($profileParams['tag']); | |
229 | } | |
230 | ||
231 | $result = civicrm_api('contact', 'create', $profileParams); | |
232 | if (CRM_Utils_Array::value('is_error', $result)) { | |
233 | return $result; | |
234 | } | |
235 | ||
236 | $ufGroupDetails = array(); | |
237 | $ufGroupParams = array('id' => $params['profile_id']); | |
238 | CRM_Core_BAO_UFGroup::retrieve($ufGroupParams, $ufGroupDetails); | |
239 | ||
240 | if (isset($profileFields['group'])) { | |
241 | CRM_Contact_BAO_GroupContact::create($groups, | |
242 | $params['contact_id'], | |
243 | FALSE, | |
244 | 'Admin' | |
245 | ); | |
246 | } | |
247 | ||
248 | if (isset($profileFields['tag'])) { | |
6a488035 TO |
249 | CRM_Core_BAO_EntityTag::create($tags, |
250 | 'civicrm_contact', | |
251 | $params['contact_id'] | |
252 | ); | |
253 | } | |
254 | ||
255 | if (CRM_Utils_Array::value('add_to_group_id', $ufGroupDetails)) { | |
256 | $contactIds = array($params['contact_id']); | |
257 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, | |
258 | $ufGroupDetails['add_to_group_id'] | |
259 | ); | |
260 | } | |
261 | ||
262 | return $result; | |
263 | } | |
264 | ||
265 | /** | |
266 | * Provide formatted values for profile fields. | |
267 | * | |
268 | * @param array $params Associative array of property name/value | |
269 | * pairs to profile field values | |
270 | * | |
271 | * @return formatted profile field values|CRM_Error | |
272 | * | |
273 | * @todo add example | |
274 | * @todo add test cases | |
275 | * | |
276 | */ | |
277 | function civicrm_api3_profile_apply($params) { | |
278 | ||
279 | civicrm_api3_verify_mandatory($params, NULL, array('profile_id')); | |
6a488035 TO |
280 | |
281 | if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) { | |
282 | return civicrm_api3_create_error('Invalid value for profile_id'); | |
283 | } | |
284 | ||
285 | $profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'], | |
286 | FALSE, | |
287 | NULL, | |
288 | NULL, | |
289 | NULL, | |
290 | FALSE, | |
291 | NULL, | |
292 | TRUE, | |
293 | NULL, | |
294 | CRM_Core_Permission::EDIT | |
295 | ); | |
296 | ||
297 | list($data, $contactDetails) = CRM_Contact_BAO_Contact::formatProfileContactParams($params, | |
298 | $profileFields, | |
299 | CRM_Utils_Array::value('contact_id', $params), | |
300 | $params['profile_id'], | |
301 | CRM_Utils_Array::value('contact_type', $params), | |
302 | CRM_Utils_Array::value('skip_custom', $params, FALSE) | |
303 | ); | |
304 | ||
305 | if (empty($data)) { | |
306 | return civicrm_api3_create_error('Enable to format profile parameters.'); | |
307 | } | |
308 | ||
309 | return civicrm_api3_create_success($data); | |
310 | } | |
311 | ||
11e09c59 | 312 | /** |
6a488035 TO |
313 | * Return UFGroup fields |
314 | */ | |
315 | function civicrm_api3_profile_getfields($params) { | |
316 | $dao = _civicrm_api3_get_DAO('UFGroup'); | |
6a488035 TO |
317 | $d = new $dao(); |
318 | $fields = $d->fields(); | |
319 | return civicrm_api3_create_success($fields); | |
320 | } | |
321 |