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