Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | ||
6a488035 TO |
28 | /* |
29 | * These functions have been deprecated out of API v3 Utils folder as they are not part of the | |
30 | * API. Calling API functions directly is not supported & these functions are not called by any | |
31 | * part of the API so are not really part of the api | |
32 | * | |
33 | */ | |
34 | ||
6a488035 TO |
35 | require_once 'api/v3/utils.php'; |
36 | ||
6a488035 TO |
37 | /** |
38 | * take the input parameter list as specified in the data model and | |
39 | * convert it into the same format that we use in QF and BAO object | |
40 | * | |
f4aaa82a | 41 | * @param array $params Associative array of property name/value |
6a488035 | 42 | * pairs to insert in new contact. |
f4aaa82a | 43 | * @param array $values The reformatted properties that we can use internally |
6a488035 | 44 | * |
f4aaa82a | 45 | * @param array|bool $create Is the formatted Values array going to |
109ff2ac | 46 | * be used for CRM_vent_BAO_Participant:create() |
6a488035 TO |
47 | * |
48 | * @return array|CRM_Error | |
49 | * @access public | |
50 | */ | |
51 | function _civicrm_api3_deprecated_participant_formatted_param($params, &$values, $create = FALSE) { | |
52 | $fields = CRM_Event_DAO_Participant::fields(); | |
53 | _civicrm_api3_store_values($fields, $params, $values); | |
54 | ||
55 | require_once 'CRM/Core/OptionGroup.php'; | |
56 | $customFields = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE); | |
57 | ||
58 | foreach ($params as $key => $value) { | |
59 | // ignore empty values or empty arrays etc | |
60 | if (CRM_Utils_System::isNull($value)) { | |
61 | continue; | |
62 | } | |
63 | ||
64 | //Handling Custom Data | |
65 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
66 | $values[$key] = $value; | |
67 | $type = $customFields[$customFieldID]['html_type']; | |
68 | if ($type == 'CheckBox' || $type == 'Multi-Select') { | |
91bb24a7 | 69 | $mulValues = explode(',', $value); |
6a488035 TO |
70 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); |
71 | $values[$key] = array(); | |
72 | foreach ($mulValues as $v1) { | |
73 | foreach ($customOption as $customValueID => $customLabel) { | |
74 | $customValue = $customLabel['value']; | |
75 | if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) || | |
76 | (strtolower(trim($customValue)) == strtolower(trim($v1))) | |
77 | ) { | |
78 | if ($type == 'CheckBox') { | |
79 | $values[$key][$customValue] = 1; | |
80 | } | |
81 | else { | |
82 | $values[$key][] = $customValue; | |
83 | } | |
84 | } | |
85 | } | |
86 | } | |
87 | } | |
88 | elseif ($type == 'Select' || $type == 'Radio') { | |
89 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
90 | foreach ($customOption as $customFldID => $customValue) { | |
91bb24a7 | 91 | $val = CRM_Utils_Array::value('value', $customValue); |
6a488035 TO |
92 | $label = CRM_Utils_Array::value('label', $customValue); |
93 | $label = strtolower($label); | |
94 | $value = strtolower(trim($value)); | |
95 | if (($value == $label) || ($value == strtolower($val))) { | |
96 | $values[$key] = $val; | |
97 | } | |
98 | } | |
99 | } | |
100 | } | |
101 | ||
102 | switch ($key) { | |
103 | case 'participant_contact_id': | |
104 | if (!CRM_Utils_Rule::integer($value)) { | |
105 | return civicrm_api3_create_error("contact_id not valid: $value"); | |
106 | } | |
91bb24a7 | 107 | $dao = new CRM_Core_DAO(); |
6a488035 | 108 | $qParams = array(); |
91bb24a7 | 109 | $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value", |
6a488035 TO |
110 | $qParams |
111 | ); | |
112 | if (!$svq) { | |
113 | return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); | |
114 | } | |
115 | $values['contact_id'] = $values['participant_contact_id']; | |
116 | unset($values['participant_contact_id']); | |
117 | break; | |
118 | ||
119 | case 'participant_register_date': | |
120 | if (!CRM_Utils_Rule::dateTime($value)) { | |
121 | return civicrm_api3_create_error("$key not a valid date: $value"); | |
122 | } | |
123 | break; | |
124 | ||
125 | case 'event_title': | |
126 | $id = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $value, 'id', 'title'); | |
127 | $values['event_id'] = $id; | |
128 | break; | |
129 | ||
130 | case 'event_id': | |
131 | if (!CRM_Utils_Rule::integer($value)) { | |
132 | return civicrm_api3_create_error("Event ID is not valid: $value"); | |
133 | } | |
91bb24a7 | 134 | $dao = new CRM_Core_DAO(); |
6a488035 | 135 | $qParams = array(); |
91bb24a7 | 136 | $svq = $dao->singleValueQuery("SELECT id FROM civicrm_event WHERE id = $value", |
6a488035 TO |
137 | $qParams |
138 | ); | |
139 | if (!$svq) { | |
140 | return civicrm_api3_create_error("Invalid Event ID: There is no event record with event_id = $value."); | |
141 | } | |
142 | break; | |
143 | ||
144 | case 'participant_status_id': | |
145 | if (!CRM_Utils_Rule::integer($value)) { | |
146 | return civicrm_api3_create_error("Event Status ID is not valid: $value"); | |
147 | } | |
148 | break; | |
149 | ||
150 | case 'participant_status': | |
151 | $status = CRM_Event_PseudoConstant::participantStatus(); | |
152 | $values['participant_status_id'] = CRM_Utils_Array::key($value, $status);; | |
153 | break; | |
154 | ||
155 | case 'participant_role_id': | |
156 | case 'participant_role': | |
157 | $role = CRM_Event_PseudoConstant::participantRole(); | |
158 | $participantRoles = explode(",", $value); | |
159 | foreach ($participantRoles as $k => $v) { | |
160 | $v = trim($v); | |
161 | if ($key == 'participant_role') { | |
162 | $participantRoles[$k] = CRM_Utils_Array::key($v, $role); | |
163 | } | |
164 | else { | |
165 | $participantRoles[$k] = $v; | |
166 | } | |
167 | } | |
168 | require_once 'CRM/Core/DAO.php'; | |
169 | $values['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $participantRoles); | |
170 | unset($values[$key]); | |
171 | break; | |
172 | ||
173 | default: | |
174 | break; | |
175 | } | |
176 | } | |
177 | ||
178 | if (array_key_exists('participant_note', $params)) { | |
179 | $values['participant_note'] = $params['participant_note']; | |
180 | } | |
181 | ||
182 | if ($create) { | |
183 | // CRM_Event_BAO_Participant::create() handles register_date, | |
184 | // status_id and source. So, if $values contains | |
185 | // participant_register_date, participant_status_id or participant_source, | |
186 | // convert it to register_date, status_id or source | |
187 | $changes = array( | |
188 | 'participant_register_date' => 'register_date', | |
189 | 'participant_source' => 'source', | |
190 | 'participant_status_id' => 'status_id', | |
191 | 'participant_role_id' => 'role_id', | |
192 | 'participant_fee_level' => 'fee_level', | |
193 | 'participant_fee_amount' => 'fee_amount', | |
194 | 'participant_id' => 'id', | |
195 | ); | |
196 | ||
197 | foreach ($changes as $orgVal => $changeVal) { | |
198 | if (isset($values[$orgVal])) { | |
199 | $values[$changeVal] = $values[$orgVal]; | |
200 | unset($values[$orgVal]); | |
201 | } | |
202 | } | |
203 | } | |
204 | ||
205 | return NULL; | |
206 | } | |
207 | ||
208 | /** | |
209 | * take the input parameter list as specified in the data model and | |
210 | * convert it into the same format that we use in QF and BAO object | |
211 | * | |
f4aaa82a | 212 | * @param array $params Associative array of property name/value |
6a488035 | 213 | * pairs to insert in new contact. |
f4aaa82a | 214 | * @param array $values The reformatted properties that we can use internally |
6a488035 TO |
215 | * ' |
216 | * | |
f4aaa82a EM |
217 | * @param bool $create |
218 | * @param null $onDuplicate | |
219 | * | |
6a488035 TO |
220 | * @return array|CRM_Error |
221 | * @access public | |
222 | */ | |
1221efe9 | 223 | function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = FALSE, $onDuplicate = Null) { |
6a488035 TO |
224 | // copy all the contribution fields as is |
225 | ||
226 | $fields = CRM_Contribute_DAO_Contribution::fields(); | |
227 | ||
228 | _civicrm_api3_store_values($fields, $params, $values); | |
229 | ||
230 | require_once 'CRM/Core/OptionGroup.php'; | |
231 | $customFields = CRM_Core_BAO_CustomField::getFields('Contribution', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE); | |
232 | ||
233 | foreach ($params as $key => $value) { | |
234 | // ignore empty values or empty arrays etc | |
235 | if (CRM_Utils_System::isNull($value)) { | |
236 | continue; | |
237 | } | |
238 | ||
239 | //Handling Custom Data | |
240 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
241 | $values[$key] = $value; | |
242 | $type = $customFields[$customFieldID]['html_type']; | |
243 | if ($type == 'CheckBox' || $type == 'Multi-Select') { | |
91bb24a7 | 244 | $mulValues = explode(',', $value); |
6a488035 TO |
245 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); |
246 | $values[$key] = array(); | |
247 | foreach ($mulValues as $v1) { | |
248 | foreach ($customOption as $customValueID => $customLabel) { | |
249 | $customValue = $customLabel['value']; | |
250 | if ((strtolower($customLabel['label']) == strtolower(trim($v1))) || | |
251 | (strtolower($customValue) == strtolower(trim($v1))) | |
252 | ) { | |
253 | if ($type == 'CheckBox') { | |
254 | $values[$key][$customValue] = 1; | |
255 | } | |
256 | else { | |
257 | $values[$key][] = $customValue; | |
258 | } | |
259 | } | |
260 | } | |
261 | } | |
262 | } | |
263 | elseif ($type == 'Select' || $type == 'Radio' || | |
264 | ($type == 'Autocomplete-Select' && | |
265 | $customFields[$customFieldID]['data_type'] == 'String' | |
266 | ) | |
267 | ) { | |
268 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
269 | foreach ($customOption as $customFldID => $customValue) { | |
91bb24a7 | 270 | $val = CRM_Utils_Array::value('value', $customValue); |
6a488035 TO |
271 | $label = CRM_Utils_Array::value('label', $customValue); |
272 | $label = strtolower($label); | |
273 | $value = strtolower(trim($value)); | |
274 | if (($value == $label) || ($value == strtolower($val))) { | |
275 | $values[$key] = $val; | |
276 | } | |
277 | } | |
278 | } | |
279 | } | |
280 | ||
281 | switch ($key) { | |
282 | case 'contribution_contact_id': | |
283 | if (!CRM_Utils_Rule::integer($value)) { | |
284 | return civicrm_api3_create_error("contact_id not valid: $value"); | |
285 | } | |
91bb24a7 | 286 | $dao = new CRM_Core_DAO(); |
6a488035 | 287 | $qParams = array(); |
91bb24a7 | 288 | $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value", |
6a488035 TO |
289 | $qParams |
290 | ); | |
291 | if (!$svq) { | |
292 | return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); | |
293 | } | |
294 | ||
295 | $values['contact_id'] = $values['contribution_contact_id']; | |
296 | unset($values['contribution_contact_id']); | |
297 | break; | |
298 | ||
299 | case 'contact_type': | |
300 | //import contribution record according to select contact type | |
301 | require_once 'CRM/Contact/DAO/Contact.php'; | |
302 | $contactType = new CRM_Contact_DAO_Contact(); | |
91bb24a7 | 303 | //when insert mode check contact id or external identifier |
304 | if (!empty($params['contribution_contact_id']) || !empty($params['external_identifier'])) { | |
305 | if (!empty($params['contribution_contact_id'])) { | |
306 | $contactType->id = CRM_Utils_Array::value('contribution_contact_id', $params); | |
6a488035 | 307 | } |
91bb24a7 | 308 | elseif (!empty($params['external_identifier'])) { |
6a488035 TO |
309 | $contactType->external_identifier = $params['external_identifier']; |
310 | } | |
311 | if ($contactType->find(TRUE)) { | |
312 | if ($params['contact_type'] != $contactType->contact_type) { | |
313 | return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type"); | |
314 | } | |
315 | } | |
316 | } | |
c9ea7d07 | 317 | elseif (!empty($params['contribution_id']) || !empty($params['trxn_id']) || !empty($params['invoice_id'])) { |
6a488035 TO |
318 | //when update mode check contribution id or trxn id or |
319 | //invoice id | |
320 | $contactId = new CRM_Contribute_DAO_Contribution(); | |
c411c28a | 321 | if (!empty($params['contribution_id'])) { |
6a488035 TO |
322 | $contactId->id = $params['contribution_id']; |
323 | } | |
c411c28a | 324 | elseif (!empty($params['trxn_id'])) { |
6a488035 TO |
325 | $contactId->trxn_id = $params['trxn_id']; |
326 | } | |
c411c28a | 327 | elseif (!empty($params['invoice_id'])) { |
6a488035 TO |
328 | $contactId->invoice_id = $params['invoice_id']; |
329 | } | |
330 | if ($contactId->find(TRUE)) { | |
331 | $contactType->id = $contactId->contact_id; | |
332 | if ($contactType->find(TRUE)) { | |
333 | if ($params['contact_type'] != $contactType->contact_type) { | |
334 | return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type"); | |
335 | } | |
336 | } | |
337 | } | |
338 | } | |
c9ea7d07 AS |
339 | else { |
340 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { | |
8b8e183b | 341 | return civicrm_api3_create_error("Empty Contribution and Invoice and Transaction ID. Row was skipped."); |
c9ea7d07 AS |
342 | } |
343 | else { | |
8b8e183b | 344 | return civicrm_api3_create_error("Empty Contact and External ID. Row was skipped."); |
c9ea7d07 AS |
345 | } |
346 | } | |
6a488035 TO |
347 | break; |
348 | ||
349 | case 'receive_date': | |
350 | case 'cancel_date': | |
351 | case 'receipt_date': | |
352 | case 'thankyou_date': | |
353 | if (!CRM_Utils_Rule::dateTime($value)) { | |
354 | return civicrm_api3_create_error("$key not a valid date: $value"); | |
355 | } | |
356 | break; | |
357 | ||
358 | case 'non_deductible_amount': | |
359 | case 'total_amount': | |
360 | case 'fee_amount': | |
361 | case 'net_amount': | |
362 | if (!CRM_Utils_Rule::money($value)) { | |
363 | return civicrm_api3_create_error("$key not a valid amount: $value"); | |
364 | } | |
365 | break; | |
366 | ||
367 | case 'currency': | |
368 | if (!CRM_Utils_Rule::currencyCode($value)) { | |
369 | return civicrm_api3_create_error("currency not a valid code: $value"); | |
370 | } | |
371 | break; | |
372 | ||
373 | case 'financial_type': | |
374 | require_once 'CRM/Contribute/PseudoConstant.php'; | |
375 | $contriTypes = CRM_Contribute_PseudoConstant::financialType(); | |
376 | foreach ($contriTypes as $val => $type) { | |
377 | if (strtolower($value) == strtolower($type)) { | |
378 | $values['financial_type_id'] = $val; | |
379 | break; | |
380 | } | |
381 | } | |
a7488080 | 382 | if (empty($values['financial_type_id'])) { |
6a488035 TO |
383 | return civicrm_api3_create_error("Financial Type is not valid: $value"); |
384 | } | |
385 | break; | |
386 | ||
387 | case 'payment_instrument': | |
388 | require_once 'CRM/Core/OptionGroup.php'; | |
389 | $values['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', $value); | |
a7488080 | 390 | if (empty($values['payment_instrument_id'])) { |
6a488035 TO |
391 | return civicrm_api3_create_error("Payment Instrument is not valid: $value"); |
392 | } | |
393 | break; | |
394 | ||
395 | case 'contribution_status_id': | |
396 | require_once 'CRM/Core/OptionGroup.php'; | |
397 | if (!$values['contribution_status_id'] = CRM_Core_OptionGroup::getValue('contribution_status', $value)) { | |
398 | return civicrm_api3_create_error("Contribution Status is not valid: $value"); | |
399 | } | |
400 | break; | |
401 | ||
6a488035 TO |
402 | case 'soft_credit': |
403 | //import contribution record according to select contact type | |
6a488035 | 404 | // validate contact id and external identifier. |
377fa510 | 405 | $value[$key] = $mismatchContactType = $softCreditContactIds = ''; |
1221efe9 | 406 | if (isset($params[$key]) && is_array($params[$key])) { |
407 | foreach ($params[$key] as $softKey => $softParam) { | |
408 | $contactId = CRM_Utils_Array::value('contact_id', $softParam); | |
409 | $externalId = CRM_Utils_Array::value('external_identifier', $softParam); | |
377fa510 | 410 | $email = CRM_Utils_Array::value('email', $softParam); |
1221efe9 | 411 | if ($contactId || $externalId) { |
412 | require_once 'CRM/Contact/DAO/Contact.php'; | |
413 | $contact = new CRM_Contact_DAO_Contact(); | |
414 | $contact->id = $contactId; | |
415 | $contact->external_identifier = $externalId; | |
1221efe9 | 416 | $errorMsg = NULL; |
417 | if (!$contact->find(TRUE)) { | |
7b99ead3 CW |
418 | $field = $contactId ? ts('Contact ID') : ts('External ID'); |
419 | $errorMsg = ts("Soft Credit %1 - %2 doesn't exist. Row was skipped.", | |
420 | array(1 => $field, 2 => $contactId ? $contactId : $externalId)); | |
1221efe9 | 421 | } |
6a488035 | 422 | |
1221efe9 | 423 | if ($errorMsg) { |
424 | return civicrm_api3_create_error($errorMsg, $value[$key]); | |
425 | } | |
6a488035 | 426 | |
1221efe9 | 427 | // finally get soft credit contact id. |
428 | $values[$key][$softKey] = $softParam; | |
429 | $values[$key][$softKey]['contact_id'] = $contact->id; | |
6a488035 | 430 | } |
377fa510 | 431 | elseif ($email) { |
432 | if (!CRM_Utils_Rule::email($email)) { | |
433 | return civicrm_api3_create_error("Invalid email address $email provided for Soft Credit. Row was skipped"); | |
434 | } | |
435 | ||
1221efe9 | 436 | // get the contact id from duplicate contact rule, if more than one contact is returned |
437 | // we should return error, since current interface allows only one-one mapping | |
377fa510 | 438 | $emailParams = array('email' => $email, 'contact_type' => $params['contact_type']); |
439 | $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams); | |
440 | if (!$checkDedupe['is_error']) { | |
441 | return civicrm_api3_create_error("Invalid email address(doesn't exist) $email for Soft Credit. Row was skipped"); | |
1221efe9 | 442 | } |
443 | else { | |
377fa510 | 444 | $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]); |
445 | if (count($matchingContactIds) > 1) { | |
3d4fb61c | 446 | return civicrm_api3_create_error("Invalid email address(duplicate) $email for Soft Credit. Row was skipped"); |
377fa510 | 447 | } |
448 | elseif (count($matchingContactIds) == 1) { | |
449 | $contactId = $matchingContactIds[0]; | |
450 | unset($softParam['email']); | |
451 | $values[$key][$softKey] = $softParam + array('contact_id' => $contactId); | |
452 | } | |
1221efe9 | 453 | } |
6a488035 TO |
454 | } |
455 | } | |
377fa510 | 456 | } |
457 | break; | |
6a488035 TO |
458 | |
459 | case 'pledge_payment': | |
460 | case 'pledge_id': | |
461 | ||
462 | //giving respect to pledge_payment flag. | |
a7488080 | 463 | if (empty($params['pledge_payment'])) { |
6a488035 TO |
464 | continue; |
465 | } | |
466 | ||
467 | //get total amount of from import fields | |
468 | $totalAmount = CRM_Utils_Array::value('total_amount', $params); | |
469 | ||
470 | $onDuplicate = CRM_Utils_Array::value('onDuplicate', $params); | |
471 | ||
472 | //we need to get contact id $contributionContactID to | |
473 | //retrieve pledge details as well as to validate pledge ID | |
474 | ||
475 | //first need to check for update mode | |
a05662ef | 476 | if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE && |
6a488035 TO |
477 | ($params['contribution_id'] || $params['trxn_id'] || $params['invoice_id']) |
478 | ) { | |
479 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
480 | if ($params['contribution_id']) { | |
481 | $contribution->id = $params['contribution_id']; | |
482 | } | |
483 | elseif ($params['trxn_id']) { | |
484 | $contribution->trxn_id = $params['trxn_id']; | |
485 | } | |
486 | elseif ($params['invoice_id']) { | |
487 | $contribution->invoice_id = $params['invoice_id']; | |
488 | } | |
489 | ||
490 | if ($contribution->find(TRUE)) { | |
491 | $contributionContactID = $contribution->contact_id; | |
492 | if (!$totalAmount) { | |
493 | $totalAmount = $contribution->total_amount; | |
494 | } | |
495 | } | |
496 | else { | |
497 | return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment'); | |
498 | } | |
499 | } | |
500 | else { | |
501 | // first get the contact id for given contribution record. | |
a7488080 | 502 | if (!empty($params['contribution_contact_id'])) { |
6a488035 TO |
503 | $contributionContactID = $params['contribution_contact_id']; |
504 | } | |
a7488080 | 505 | elseif (!empty($params['external_identifier'])) { |
6a488035 TO |
506 | require_once 'CRM/Contact/DAO/Contact.php'; |
507 | $contact = new CRM_Contact_DAO_Contact(); | |
508 | $contact->external_identifier = $params['external_identifier']; | |
509 | if ($contact->find(TRUE)) { | |
510 | $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $contact->id; | |
511 | } | |
512 | else { | |
513 | return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment'); | |
514 | } | |
515 | } | |
516 | else { | |
517 | // we need to get contribution contact using de dupe | |
518 | $error = _civicrm_api3_deprecated_check_contact_dedupe($params); | |
519 | ||
520 | if (isset($error['error_message']['params'][0])) { | |
521 | $matchedIDs = explode(',', $error['error_message']['params'][0]); | |
522 | ||
523 | // check if only one contact is found | |
524 | if (count($matchedIDs) > 1) { | |
525 | return civicrm_api3_create_error($error['error_message']['message'], 'pledge_payment'); | |
526 | } | |
527 | else { | |
528 | $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $matchedIDs[0]; | |
529 | } | |
530 | } | |
531 | else { | |
532 | return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment'); | |
533 | } | |
534 | } | |
535 | } | |
536 | ||
a7488080 | 537 | if (!empty($params['pledge_id'])) { |
6a488035 TO |
538 | if (CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $params['pledge_id'], 'contact_id') != $contributionContactID) { |
539 | return civicrm_api3_create_error('Invalid Pledge ID provided. Contribution row was skipped.', 'pledge_payment'); | |
540 | } | |
541 | $values['pledge_id'] = $params['pledge_id']; | |
542 | } | |
543 | else { | |
544 | //check if there are any pledge related to this contact, with payments pending or in progress | |
545 | require_once 'CRM/Pledge/BAO/Pledge.php'; | |
546 | $pledgeDetails = CRM_Pledge_BAO_Pledge::getContactPledges($contributionContactID); | |
547 | ||
548 | if (empty($pledgeDetails)) { | |
549 | return civicrm_api3_create_error('No open pledges found for this contact. Contribution row was skipped.', 'pledge_payment'); | |
550 | } | |
551 | elseif (count($pledgeDetails) > 1) { | |
552 | return civicrm_api3_create_error('This contact has more than one open pledge. Unable to determine which pledge to apply the contribution to. Contribution row was skipped.', 'pledge_payment'); | |
553 | } | |
554 | ||
555 | // this mean we have only one pending / in progress pledge | |
556 | $values['pledge_id'] = $pledgeDetails[0]; | |
557 | } | |
558 | ||
559 | //we need to check if oldest payment amount equal to contribution amount | |
560 | require_once 'CRM/Pledge/BAO/PledgePayment.php'; | |
561 | $pledgePaymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($values['pledge_id']); | |
562 | ||
563 | if ($pledgePaymentDetails['amount'] == $totalAmount) { | |
564 | $values['pledge_payment_id'] = $pledgePaymentDetails['id']; | |
565 | } | |
566 | else { | |
567 | return civicrm_api3_create_error('Contribution and Pledge Payment amount mismatch for this record. Contribution row was skipped.', 'pledge_payment'); | |
568 | } | |
569 | break; | |
570 | ||
571 | default: | |
572 | break; | |
573 | } | |
574 | } | |
575 | ||
576 | if (array_key_exists('note', $params)) { | |
577 | $values['note'] = $params['note']; | |
578 | } | |
579 | ||
580 | if ($create) { | |
581 | // CRM_Contribute_BAO_Contribution::add() handles contribution_source | |
582 | // So, if $values contains contribution_source, convert it to source | |
583 | $changes = array('contribution_source' => 'source'); | |
584 | ||
585 | foreach ($changes as $orgVal => $changeVal) { | |
586 | if (isset($values[$orgVal])) { | |
587 | $values[$changeVal] = $values[$orgVal]; | |
588 | unset($values[$orgVal]); | |
589 | } | |
590 | } | |
591 | } | |
592 | ||
593 | return NULL; | |
594 | } | |
595 | ||
596 | /** | |
597 | * Function to check duplicate contacts based on de-deupe parameters | |
598 | */ | |
599 | function _civicrm_api3_deprecated_check_contact_dedupe($params) { | |
600 | static $cIndieFields = NULL; | |
601 | static $defaultLocationId = NULL; | |
602 | ||
603 | $contactType = $params['contact_type']; | |
604 | if ($cIndieFields == NULL) { | |
605 | require_once 'CRM/Contact/BAO/Contact.php'; | |
606 | $cTempIndieFields = CRM_Contact_BAO_Contact::importableFields($contactType); | |
607 | $cIndieFields = $cTempIndieFields; | |
608 | ||
609 | require_once "CRM/Core/BAO/LocationType.php"; | |
610 | $defaultLocation = CRM_Core_BAO_LocationType::getDefault(); | |
611 | ||
612 | //set the value to default location id else set to 1 | |
613 | if (!$defaultLocationId = (int)$defaultLocation->id) { | |
614 | $defaultLocationId = 1; | |
615 | } | |
616 | } | |
617 | ||
618 | require_once 'CRM/Contact/BAO/Query.php'; | |
619 | $locationFields = CRM_Contact_BAO_Query::$_locationSpecificFields; | |
620 | ||
621 | $contactFormatted = array(); | |
622 | foreach ($params as $key => $field) { | |
623 | if ($field == NULL || $field === '') { | |
624 | continue; | |
625 | } | |
626 | if (is_array($field)) { | |
627 | foreach ($field as $value) { | |
628 | $break = FALSE; | |
629 | if (is_array($value)) { | |
630 | foreach ($value as $name => $testForEmpty) { | |
631 | if ($name !== 'phone_type' && | |
632 | ($testForEmpty === '' || $testForEmpty == NULL) | |
633 | ) { | |
634 | $break = TRUE; | |
635 | break; | |
636 | } | |
637 | } | |
638 | } | |
639 | else { | |
640 | $break = TRUE; | |
641 | } | |
642 | if (!$break) { | |
643 | _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted); | |
644 | } | |
645 | } | |
646 | continue; | |
647 | } | |
648 | ||
649 | $value = array($key => $field); | |
650 | ||
651 | // check if location related field, then we need to add primary location type | |
652 | if (in_array($key, $locationFields)) { | |
653 | $value['location_type_id'] = $defaultLocationId; | |
654 | } | |
655 | elseif (array_key_exists($key, $cIndieFields)) { | |
656 | $value['contact_type'] = $contactType; | |
657 | } | |
658 | ||
659 | _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted); | |
660 | } | |
661 | ||
662 | $contactFormatted['contact_type'] = $contactType; | |
663 | ||
664 | return _civicrm_api3_deprecated_duplicate_formatted_contact($contactFormatted); | |
665 | } | |
666 | ||
667 | /** | |
668 | * take the input parameter list as specified in the data model and | |
669 | * convert it into the same format that we use in QF and BAO object | |
670 | * | |
f4aaa82a | 671 | * @param array $params Associative array of property name/value |
6a488035 | 672 | * pairs to insert in new contact. |
f4aaa82a | 673 | * @param array $values The reformatted properties that we can use internally |
6a488035 | 674 | * |
f4aaa82a | 675 | * @param array|bool $create Is the formatted Values array going to |
6a488035 TO |
676 | * be used for CRM_Activity_BAO_Activity::create() |
677 | * | |
678 | * @return array|CRM_Error | |
679 | * @access public | |
680 | */ | |
681 | function _civicrm_api3_deprecated_activity_formatted_param(&$params, &$values, $create = FALSE) { | |
682 | // copy all the activity fields as is | |
683 | $fields = CRM_Activity_DAO_Activity::fields(); | |
684 | _civicrm_api3_store_values($fields, $params, $values); | |
685 | ||
686 | require_once 'CRM/Core/OptionGroup.php'; | |
687 | $customFields = CRM_Core_BAO_CustomField::getFields('Activity'); | |
688 | ||
689 | foreach ($params as $key => $value) { | |
690 | // ignore empty values or empty arrays etc | |
691 | if (CRM_Utils_System::isNull($value)) { | |
692 | continue; | |
693 | } | |
694 | ||
695 | //Handling Custom Data | |
696 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
697 | $values[$key] = $value; | |
698 | $type = $customFields[$customFieldID]['html_type']; | |
699 | if ($type == 'CheckBox' || $type == 'Multi-Select') { | |
91bb24a7 | 700 | $mulValues = explode(',', $value); |
6a488035 TO |
701 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); |
702 | $values[$key] = array(); | |
703 | foreach ($mulValues as $v1) { | |
704 | foreach ($customOption as $customValueID => $customLabel) { | |
705 | $customValue = $customLabel['value']; | |
706 | if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) || | |
707 | (strtolower(trim($customValue)) == strtolower(trim($v1))) | |
708 | ) { | |
709 | if ($type == 'CheckBox') { | |
710 | $values[$key][$customValue] = 1; | |
711 | } | |
712 | else { | |
713 | $values[$key][] = $customValue; | |
714 | } | |
715 | } | |
716 | } | |
717 | } | |
718 | } | |
719 | elseif ($type == 'Select' || $type == 'Radio') { | |
720 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
721 | foreach ($customOption as $customFldID => $customValue) { | |
91bb24a7 | 722 | $val = CRM_Utils_Array::value('value', $customValue); |
6a488035 TO |
723 | $label = CRM_Utils_Array::value('label', $customValue); |
724 | $label = strtolower($label); | |
725 | $value = strtolower(trim($value)); | |
726 | if (($value == $label) || ($value == strtolower($val))) { | |
727 | $values[$key] = $val; | |
728 | } | |
729 | } | |
730 | } | |
731 | } | |
732 | ||
733 | if ($key == 'target_contact_id') { | |
734 | if (!CRM_Utils_Rule::integer($value)) { | |
735 | return civicrm_api3_create_error("contact_id not valid: $value"); | |
736 | } | |
737 | $contactID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value"); | |
738 | if (!$contactID) { | |
739 | return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); | |
740 | } | |
741 | } | |
742 | } | |
743 | return NULL; | |
744 | } | |
745 | ||
746 | /** | |
747 | * This function adds the contact variable in $values to the | |
748 | * parameter list $params. For most cases, $values should have length 1. If | |
749 | * the variable being added is a child of Location, a location_type_id must | |
750 | * also be included. If it is a child of phone, a phone_type must be included. | |
751 | * | |
752 | * @param array $values The variable(s) to be added | |
753 | * @param array $params The structured parameter list | |
754 | * | |
755 | * @return bool|CRM_Utils_Error | |
756 | * @access public | |
757 | */ | |
758 | function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { | |
759 | /* Crawl through the possible classes: | |
760 | * Contact | |
761 | * Individual | |
762 | * Household | |
763 | * Organization | |
764 | * Location | |
765 | * Address | |
766 | ||
767 | * Phone | |
768 | * IM | |
769 | * Note | |
770 | * Custom | |
771 | */ | |
772 | ||
6a488035 | 773 | /* Cache the various object fields */ |
6a488035 TO |
774 | static $fields = NULL; |
775 | ||
776 | if ($fields == NULL) { | |
777 | $fields = array(); | |
778 | } | |
779 | ||
780 | //first add core contact values since for other Civi modules they are not added | |
781 | require_once 'CRM/Contact/BAO/Contact.php'; | |
782 | $contactFields = CRM_Contact_DAO_Contact::fields(); | |
783 | _civicrm_api3_store_values($contactFields, $values, $params); | |
784 | ||
785 | if (isset($values['contact_type'])) { | |
786 | /* we're an individual/household/org property */ | |
787 | ||
6a488035 TO |
788 | $fields[$values['contact_type']] = CRM_Contact_DAO_Contact::fields(); |
789 | ||
790 | _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params); | |
791 | return TRUE; | |
792 | } | |
793 | ||
794 | if (isset($values['individual_prefix'])) { | |
a7488080 | 795 | if (!empty($params['prefix_id'])) { |
91bb24a7 | 796 | $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
6a488035 TO |
797 | $params['prefix'] = $prefixes[$params['prefix_id']]; |
798 | } | |
799 | else { | |
800 | $params['prefix'] = $values['individual_prefix']; | |
801 | } | |
802 | return TRUE; | |
803 | } | |
804 | ||
805 | if (isset($values['individual_suffix'])) { | |
a7488080 | 806 | if (!empty($params['suffix_id'])) { |
91bb24a7 | 807 | $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); |
6a488035 TO |
808 | $params['suffix'] = $suffixes[$params['suffix_id']]; |
809 | } | |
810 | else { | |
811 | $params['suffix'] = $values['individual_suffix']; | |
812 | } | |
813 | return TRUE; | |
814 | } | |
815 | ||
816 | //CRM-4575 | |
817 | if (isset($values['email_greeting'])) { | |
a7488080 | 818 | if (!empty($params['email_greeting_id'])) { |
91bb24a7 | 819 | $emailGreetingFilter = array( |
820 | 'contact_type' => CRM_Utils_Array::value('contact_type', $params), | |
6a488035 TO |
821 | 'greeting_type' => 'email_greeting', |
822 | ); | |
823 | $emailGreetings = CRM_Core_PseudoConstant::greeting($emailGreetingFilter); | |
824 | $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']]; | |
825 | } | |
826 | else { | |
827 | $params['email_greeting'] = $values['email_greeting']; | |
828 | } | |
829 | ||
830 | return TRUE; | |
831 | } | |
832 | ||
833 | if (isset($values['postal_greeting'])) { | |
a7488080 | 834 | if (!empty($params['postal_greeting_id'])) { |
91bb24a7 | 835 | $postalGreetingFilter = array( |
836 | 'contact_type' => CRM_Utils_Array::value('contact_type', $params), | |
6a488035 TO |
837 | 'greeting_type' => 'postal_greeting', |
838 | ); | |
839 | $postalGreetings = CRM_Core_PseudoConstant::greeting($postalGreetingFilter); | |
840 | $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']]; | |
841 | } | |
842 | else { | |
843 | $params['postal_greeting'] = $values['postal_greeting']; | |
844 | } | |
845 | return TRUE; | |
846 | } | |
847 | ||
848 | if (isset($values['addressee'])) { | |
a7488080 | 849 | if (!empty($params['addressee_id'])) { |
91bb24a7 | 850 | $addresseeFilter = array( |
851 | 'contact_type' => CRM_Utils_Array::value('contact_type', $params), | |
6a488035 TO |
852 | 'greeting_type' => 'addressee', |
853 | ); | |
854 | $addressee = CRM_Core_PseudoConstant::addressee($addresseeFilter); | |
855 | $params['addressee'] = $addressee[$params['addressee_id']]; | |
856 | } | |
857 | else { | |
858 | $params['addressee'] = $values['addressee']; | |
859 | } | |
860 | return TRUE; | |
861 | } | |
862 | ||
863 | if (isset($values['gender'])) { | |
a7488080 | 864 | if (!empty($params['gender_id'])) { |
91bb24a7 | 865 | $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); |
6a488035 TO |
866 | $params['gender'] = $genders[$params['gender_id']]; |
867 | } | |
868 | else { | |
869 | $params['gender'] = $values['gender']; | |
870 | } | |
871 | return TRUE; | |
872 | } | |
873 | ||
df5ad245 | 874 | if (!empty($values['preferred_communication_method'])) { |
91bb24a7 | 875 | $comm = array(); |
876 | $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER); | |
6a488035 TO |
877 | |
878 | $preffComm = explode(',', $values['preferred_communication_method']); | |
879 | foreach ($preffComm as $v) { | |
880 | $v = strtolower(trim($v)); | |
881 | if (array_key_exists($v, $pcm)) { | |
882 | $comm[$pcm[$v]] = 1; | |
883 | } | |
884 | } | |
885 | ||
886 | $params['preferred_communication_method'] = $comm; | |
887 | return TRUE; | |
888 | } | |
889 | ||
890 | //format the website params. | |
a7488080 | 891 | if (!empty($values['url'])) { |
6a488035 TO |
892 | static $websiteFields; |
893 | if (!is_array($websiteFields)) { | |
894 | require_once 'CRM/Core/DAO/Website.php'; | |
895 | $websiteFields = CRM_Core_DAO_Website::fields(); | |
896 | } | |
897 | if (!array_key_exists('website', $params) || | |
898 | !is_array($params['website']) | |
899 | ) { | |
900 | $params['website'] = array(); | |
901 | } | |
902 | ||
903 | $websiteCount = count($params['website']); | |
904 | _civicrm_api3_store_values($websiteFields, $values, | |
905 | $params['website'][++$websiteCount] | |
906 | ); | |
907 | ||
908 | return TRUE; | |
909 | } | |
910 | ||
911 | // get the formatted location blocks into params - w/ 3.0 format, CRM-4605 | |
a7488080 | 912 | if (!empty($values['location_type_id'])) { |
6a488035 TO |
913 | _civicrm_api3_deprecated_add_formatted_location_blocks($values, $params); |
914 | return TRUE; | |
915 | } | |
916 | ||
917 | if (isset($values['note'])) { | |
918 | /* add a note field */ | |
6a488035 TO |
919 | if (!isset($params['note'])) { |
920 | $params['note'] = array(); | |
921 | } | |
922 | $noteBlock = count($params['note']) + 1; | |
923 | ||
924 | $params['note'][$noteBlock] = array(); | |
925 | if (!isset($fields['Note'])) { | |
926 | $fields['Note'] = CRM_Core_DAO_Note::fields(); | |
927 | } | |
928 | ||
929 | // get the current logged in civicrm user | |
930 | $session = CRM_Core_Session::singleton(); | |
931 | $userID = $session->get('userID'); | |
932 | ||
933 | if ($userID) { | |
934 | $values['contact_id'] = $userID; | |
935 | } | |
936 | ||
937 | _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]); | |
938 | ||
939 | return TRUE; | |
940 | } | |
941 | ||
942 | /* Check for custom field values */ | |
943 | ||
a7488080 | 944 | if (empty($fields['custom'])) { |
6a488035 TO |
945 | $fields['custom'] = &CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $values), |
946 | FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE | |
947 | ); | |
948 | } | |
949 | ||
950 | foreach ($values as $key => $value) { | |
951 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
952 | /* check if it's a valid custom field id */ | |
953 | ||
6a488035 TO |
954 | if (!array_key_exists($customFieldID, $fields['custom'])) { |
955 | return civicrm_api3_create_error('Invalid custom field ID'); | |
956 | } | |
957 | else { | |
958 | $params[$key] = $value; | |
959 | } | |
960 | } | |
961 | } | |
962 | } | |
963 | ||
964 | /** | |
965 | * This function format location blocks w/ v3.0 format. | |
966 | * | |
967 | * @param array $values The variable(s) to be added | |
968 | * @param array $params The structured parameter list | |
969 | * | |
970 | * @return bool | |
971 | * @access public | |
972 | */ | |
973 | function _civicrm_api3_deprecated_add_formatted_location_blocks(&$values, &$params) { | |
974 | static $fields = NULL; | |
975 | if ($fields == NULL) { | |
976 | $fields = array(); | |
977 | } | |
978 | ||
979 | foreach (array( | |
980 | 'Phone', 'Email', 'IM', 'OpenID','Phone_Ext') as $block) { | |
981 | $name = strtolower($block); | |
982 | if (!array_key_exists($name, $values)) { | |
983 | continue; | |
984 | } | |
985 | ||
986 | if($name == 'phone_ext'){ | |
987 | $block = 'Phone'; | |
988 | } | |
989 | ||
990 | // block present in value array. | |
991 | if (!array_key_exists($name, $params) || !is_array($params[$name])) { | |
992 | $params[$name] = array(); | |
993 | } | |
994 | ||
995 | if (!array_key_exists($block, $fields)) { | |
0e6e8724 DL |
996 | $className = "CRM_Core_DAO_$block"; |
997 | $fields[$block] =& $className::fields( ); | |
6a488035 TO |
998 | } |
999 | ||
1000 | $blockCnt = count($params[$name]); | |
1001 | ||
1002 | // copy value to dao field name. | |
1003 | if ($name == 'im') { | |
1004 | $values['name'] = $values[$name]; | |
1005 | } | |
1006 | ||
1007 | _civicrm_api3_store_values($fields[$block], $values, | |
1008 | $params[$name][++$blockCnt] | |
1009 | ); | |
1010 | ||
a7488080 | 1011 | if (empty($params['id']) && ($blockCnt == 1)) { |
6a488035 TO |
1012 | $params[$name][$blockCnt]['is_primary'] = TRUE; |
1013 | } | |
1014 | ||
1015 | // we only process single block at a time. | |
1016 | return TRUE; | |
1017 | } | |
1018 | ||
1019 | // handle address fields. | |
1020 | if (!array_key_exists('address', $params) || !is_array($params['address'])) { | |
1021 | $params['address'] = array(); | |
1022 | } | |
1023 | ||
1024 | $addressCnt = 1; | |
1025 | foreach ($params['address'] as $cnt => $addressBlock) { | |
1026 | if (CRM_Utils_Array::value('location_type_id', $values) == | |
1027 | CRM_Utils_Array::value('location_type_id', $addressBlock) | |
1028 | ) { | |
1029 | $addressCnt = $cnt; | |
1030 | break; | |
1031 | } | |
1032 | $addressCnt++; | |
1033 | } | |
1034 | ||
1035 | if (!array_key_exists('Address', $fields)) { | |
1036 | require_once 'CRM/Core/DAO/Address.php'; | |
1037 | $fields['Address'] = CRM_Core_DAO_Address::fields(); | |
1038 | } | |
1039 | ||
1040 | // Note: we doing multiple value formatting here for address custom fields, plus putting into right format. | |
1041 | // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving | |
1042 | // the address in CRM_Core_BAO_Address::create method | |
a7488080 | 1043 | if (!empty($values['location_type_id'])) { |
6a488035 TO |
1044 | static $customFields = array(); |
1045 | if (empty($customFields)) { | |
1046 | $customFields = CRM_Core_BAO_CustomField::getFields('Address'); | |
1047 | } | |
1048 | // make a copy of values, as we going to make changes | |
1049 | $newValues = $values; | |
1050 | foreach ($values as $key => $val) { | |
1051 | $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key); | |
1052 | if ($customFieldID && array_key_exists($customFieldID, $customFields)) { | |
1053 | // mark an entry in fields array since we want the value of custom field to be copied | |
1054 | $fields['Address'][$key] = null; | |
1055 | ||
1056 | $htmlType = CRM_Utils_Array::value( 'html_type', $customFields[$customFieldID] ); | |
1057 | switch ( $htmlType ) { | |
1058 | case 'CheckBox': | |
1059 | case 'AdvMulti-Select': | |
1060 | case 'Multi-Select': | |
1061 | if ( $val ) { | |
1062 | $mulValues = explode( ',', $val ); | |
1063 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption( $customFieldID, true ); | |
1064 | $newValues[$key] = array( ); | |
1065 | foreach ( $mulValues as $v1 ) { | |
1066 | foreach ( $customOption as $v2 ) { | |
1067 | if ( ( strtolower( $v2['label'] ) == strtolower( trim( $v1 ) ) ) || | |
1068 | ( strtolower( $v2['value'] ) == strtolower( trim( $v1 ) ) ) ) { | |
1069 | if ( $htmlType == 'CheckBox' ) { | |
1070 | $newValues[$key][$v2['value']] = 1; | |
1071 | } else { | |
1072 | $newValues[$key][] = $v2['value']; | |
1073 | } | |
1074 | } | |
1075 | } | |
1076 | } | |
1077 | } | |
1078 | break; | |
1079 | } | |
1080 | } | |
1081 | } | |
1082 | // consider new values | |
1083 | $values = $newValues; | |
1084 | } | |
1085 | ||
1086 | _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]); | |
1087 | ||
1088 | $addressFields = array( | |
1089 | 'county', 'country', 'state_province', | |
1090 | 'supplemental_address_1', 'supplemental_address_2', | |
1091 | 'StateProvince.name', | |
1092 | ); | |
1093 | ||
1094 | foreach ($addressFields as $field) { | |
1095 | if (array_key_exists($field, $values)) { | |
1096 | if (!array_key_exists('address', $params)) { | |
1097 | $params['address'] = array(); | |
1098 | } | |
1099 | $params['address'][$addressCnt][$field] = $values[$field]; | |
1100 | } | |
1101 | } | |
1102 | ||
1103 | if ($addressCnt == 1) { | |
1104 | ||
1105 | $params['address'][$addressCnt]['is_primary'] = TRUE; | |
1106 | } | |
1107 | ||
1108 | return TRUE; | |
1109 | } | |
1110 | ||
1111 | /** | |
1112 | * | |
1113 | * @param <type> $params | |
1114 | * | |
f4aaa82a | 1115 | * @return array <type> |
6a488035 TO |
1116 | */ |
1117 | function _civicrm_api3_deprecated_duplicate_formatted_contact($params) { | |
1118 | $id = CRM_Utils_Array::value('id', $params); | |
1119 | $externalId = CRM_Utils_Array::value('external_identifier', $params); | |
1120 | if ($id || $externalId) { | |
1121 | $contact = new CRM_Contact_DAO_Contact(); | |
1122 | ||
1123 | $contact->id = $id; | |
1124 | $contact->external_identifier = $externalId; | |
1125 | ||
1126 | if ($contact->find(TRUE)) { | |
1127 | if ($params['contact_type'] != $contact->contact_type) { | |
1128 | return civicrm_api3_create_error("Mismatched contact IDs OR Mismatched contact Types"); | |
1129 | } | |
1130 | ||
1131 | $error = CRM_Core_Error::createError("Found matching contacts: $contact->id", | |
1132 | CRM_Core_Error::DUPLICATE_CONTACT, | |
1133 | 'Fatal', $contact->id | |
1134 | ); | |
1135 | return civicrm_api3_create_error($error->pop()); | |
1136 | } | |
1137 | } | |
1138 | else { | |
1139 | require_once 'CRM/Dedupe/Finder.php'; | |
1140 | $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); | |
1141 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised'); | |
1142 | ||
1143 | if (!empty($ids)) { | |
1144 | $ids = implode(',', $ids); | |
1145 | $error = CRM_Core_Error::createError("Found matching contacts: $ids", | |
1146 | CRM_Core_Error::DUPLICATE_CONTACT, | |
1147 | 'Fatal', $ids | |
1148 | ); | |
1149 | return civicrm_api3_create_error($error->pop()); | |
1150 | } | |
1151 | } | |
1152 | return civicrm_api3_create_success(TRUE); | |
1153 | } | |
1154 | ||
1155 | /** | |
1156 | * Validate a formatted contact parameter list. | |
1157 | * | |
1158 | * @param array $params Structured parameter list (as in crm_format_params) | |
1159 | * | |
1160 | * @return bool|CRM_Core_Error | |
1161 | * @access public | |
1162 | */ | |
1163 | function _civicrm_api3_deprecated_validate_formatted_contact(&$params) { | |
1164 | /* Look for offending email addresses */ | |
1165 | ||
6a488035 TO |
1166 | if (array_key_exists('email', $params)) { |
1167 | foreach ($params['email'] as $count => $values) { | |
1168 | if (!is_array($values)) { | |
1169 | continue; | |
1170 | } | |
1171 | if ($email = CRM_Utils_Array::value('email', $values)) { | |
1172 | //validate each email | |
1173 | if (!CRM_Utils_Rule::email($email)) { | |
1174 | return civicrm_api3_create_error('No valid email address'); | |
1175 | } | |
1176 | ||
1177 | //check for loc type id. | |
a7488080 | 1178 | if (empty($values['location_type_id'])) { |
6a488035 TO |
1179 | return civicrm_api3_create_error('Location Type Id missing.'); |
1180 | } | |
1181 | } | |
1182 | } | |
1183 | } | |
1184 | ||
1185 | /* Validate custom data fields */ | |
6a488035 TO |
1186 | if (array_key_exists('custom', $params) && is_array($params['custom'])) { |
1187 | foreach ($params['custom'] as $key => $custom) { | |
1188 | if (is_array($custom)) { | |
1189 | foreach ($custom as $fieldId => $value) { | |
1190 | $valid = CRM_Core_BAO_CustomValue::typecheck(CRM_Utils_Array::value('type', $value), | |
1191 | CRM_Utils_Array::value('value', $value) | |
1192 | ); | |
70dd449f | 1193 | if (!$valid && $value['is_required']) { |
6a488035 TO |
1194 | return civicrm_api3_create_error('Invalid value for custom field \'' . |
1195 | CRM_Utils_Array::value('name', $custom) . '\'' | |
1196 | ); | |
1197 | } | |
1198 | if (CRM_Utils_Array::value('type', $custom) == 'Date') { | |
1199 | $params['custom'][$key][$fieldId]['value'] = str_replace('-', '', $params['custom'][$key][$fieldId]['value']); | |
1200 | } | |
1201 | } | |
1202 | } | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | return civicrm_api3_create_success(TRUE); | |
1207 | } | |
1208 | ||
6a488035 | 1209 | |
6a488035 TO |
1210 | /** |
1211 | * @deprecated - this is part of the import parser not the API & needs to be moved on out | |
1212 | * | |
f4aaa82a EM |
1213 | * @param $params |
1214 | * @param $onDuplicate | |
6a488035 | 1215 | * |
f4aaa82a EM |
1216 | * @internal param $ <type> $params |
1217 | * @internal param $ <type> $onDuplicate | |
1218 | * | |
1219 | * @return array|bool <type> | |
6a488035 TO |
1220 | */ |
1221 | function _civicrm_api3_deprecated_create_participant_formatted($params, $onDuplicate) { | |
1222 | require_once 'CRM/Event/Import/Parser.php'; | |
a05662ef | 1223 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK) { |
6a488035 TO |
1224 | CRM_Core_Error::reset(); |
1225 | $error = _civicrm_api3_deprecated_participant_check_params($params, TRUE); | |
1226 | if (civicrm_error($error)) { | |
1227 | return $error; | |
1228 | } | |
1229 | } | |
1230 | require_once "api/v3/Participant.php"; | |
1231 | return civicrm_api3_participant_create($params); | |
1232 | } | |
1233 | ||
1234 | /** | |
1235 | * | |
1236 | * @param <type> $params | |
1237 | * | |
f4aaa82a EM |
1238 | * @param bool $checkDuplicate |
1239 | * | |
1240 | * @return array|bool <type> | |
6a488035 TO |
1241 | */ |
1242 | function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplicate = FALSE) { | |
1243 | ||
1244 | //check if participant id is valid or not | |
a7488080 | 1245 | if (!empty($params['id'])) { |
6a488035 TO |
1246 | $participant = new CRM_Event_BAO_Participant(); |
1247 | $participant->id = $params['id']; | |
1248 | if (!$participant->find(TRUE)) { | |
1249 | return civicrm_api3_create_error(ts('Participant id is not valid')); | |
1250 | } | |
1251 | } | |
1252 | require_once 'CRM/Contact/BAO/Contact.php'; | |
1253 | //check if contact id is valid or not | |
a7488080 | 1254 | if (!empty($params['contact_id'])) { |
6a488035 TO |
1255 | $contact = new CRM_Contact_BAO_Contact(); |
1256 | $contact->id = $params['contact_id']; | |
1257 | if (!$contact->find(TRUE)) { | |
1258 | return civicrm_api3_create_error(ts('Contact id is not valid')); | |
1259 | } | |
1260 | } | |
1261 | ||
1262 | //check that event id is not an template | |
a7488080 | 1263 | if (!empty($params['event_id'])) { |
6a488035 TO |
1264 | $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template'); |
1265 | if (!empty($isTemplate)) { | |
1266 | return civicrm_api3_create_error(ts('Event templates are not meant to be registered')); | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | $result = array(); | |
1271 | if ($checkDuplicate) { | |
1272 | if (CRM_Event_BAO_Participant::checkDuplicate($params, $result)) { | |
1273 | $participantID = array_pop($result); | |
1274 | ||
1275 | $error = CRM_Core_Error::createError("Found matching participant record.", | |
1276 | CRM_Core_Error::DUPLICATE_PARTICIPANT, | |
1277 | 'Fatal', $participantID | |
1278 | ); | |
1279 | ||
1280 | return civicrm_api3_create_error($error->pop(), | |
1281 | array( | |
1282 | 'contactID' => $params['contact_id'], | |
1283 | 'participantID' => $participantID, | |
1284 | ) | |
1285 | ); | |
1286 | } | |
1287 | } | |
1288 | return TRUE; | |
1289 | } | |
1290 | ||
1291 | /** | |
1292 | * Ensure that we have the right input parameters for custom data | |
1293 | * | |
1294 | * @param array $params Associative array of property name/value | |
1295 | * pairs to insert in new contact. | |
1296 | * @param string $csType contact subtype if exists/passed. | |
1297 | * | |
1298 | * @return null on success, error message otherwise | |
1299 | * @access public | |
1300 | */ | |
1301 | function _civicrm_api3_deprecated_contact_check_custom_params($params, $csType = NULL) { | |
1302 | empty($csType) ? $onlyParent = TRUE : $onlyParent = FALSE; | |
1303 | ||
1304 | require_once 'CRM/Core/BAO/CustomField.php'; | |
1305 | $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], | |
1306 | FALSE, | |
1307 | FALSE, | |
1308 | $csType, | |
1309 | NULL, | |
1310 | $onlyParent, | |
1311 | FALSE, | |
1312 | FALSE | |
1313 | ); | |
1314 | ||
1315 | foreach ($params as $key => $value) { | |
1316 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
1317 | /* check if it's a valid custom field id */ | |
6a488035 TO |
1318 | if (!array_key_exists($customFieldID, $customFields)) { |
1319 | ||
1320 | $errorMsg = "Invalid Custom Field Contact Type: {$params['contact_type']}"; | |
1321 | if (!empty($csType)) { | |
1322 | $errorMsg .= " or Mismatched SubType: " . implode(', ', (array)$csType); | |
1323 | } | |
1324 | return civicrm_api3_create_error($errorMsg); | |
1325 | } | |
1326 | } | |
1327 | } | |
1328 | } | |
1329 | ||
5bc392e6 EM |
1330 | /** |
1331 | * @param $params | |
1332 | * @param bool $dupeCheck | |
1333 | * @param bool $dupeErrorArray | |
1334 | * @param bool $requiredCheck | |
1335 | * @param null $dedupeRuleGroupID | |
1336 | * | |
1337 | * @return array|null | |
1338 | */ | |
109ff2ac DL |
1339 | function _civicrm_api3_deprecated_contact_check_params( |
1340 | &$params, | |
1341 | $dupeCheck = TRUE, | |
1342 | $dupeErrorArray = FALSE, | |
1343 | $requiredCheck = TRUE, | |
1344 | $dedupeRuleGroupID = NULL) { | |
6a488035 TO |
1345 | if (isset($params['id']) && is_numeric($params['id'])) { |
1346 | $requiredCheck = FALSE; | |
1347 | } | |
1348 | if ($requiredCheck) { | |
1349 | if (isset($params['id'])) { | |
1350 | $required = array('Individual', 'Household', 'Organization'); | |
1351 | } | |
1352 | $required = array( | |
1353 | 'Individual' => array( | |
1354 | array('first_name', 'last_name'), | |
1355 | 'email', | |
1356 | ), | |
1357 | 'Household' => array( | |
1358 | 'household_name', | |
1359 | ), | |
1360 | 'Organization' => array( | |
1361 | 'organization_name', | |
1362 | ), | |
1363 | ); | |
1364 | ||
1365 | ||
1366 | // contact_type has a limited number of valid values | |
81fc1677 E |
1367 | if(empty($params['contact_type'])) { |
1368 | return civicrm_api3_create_error("No Contact Type"); | |
1369 | } | |
6a488035 TO |
1370 | $fields = CRM_Utils_Array::value($params['contact_type'], $required); |
1371 | if ($fields == NULL) { | |
1372 | return civicrm_api3_create_error("Invalid Contact Type: {$params['contact_type']}"); | |
1373 | } | |
1374 | ||
1375 | if ($csType = CRM_Utils_Array::value('contact_sub_type', $params)) { | |
1376 | if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($csType, $params['contact_type']))) { | |
1377 | return civicrm_api3_create_error("Invalid or Mismatched Contact SubType: " . implode(', ', (array)$csType)); | |
1378 | } | |
1379 | } | |
1380 | ||
8cc574cf | 1381 | if (empty($params['contact_id']) && !empty($params['id'])) { |
6a488035 TO |
1382 | $valid = FALSE; |
1383 | $error = ''; | |
1384 | foreach ($fields as $field) { | |
1385 | if (is_array($field)) { | |
1386 | $valid = TRUE; | |
1387 | foreach ($field as $element) { | |
a7488080 | 1388 | if (empty($params[$element])) { |
6a488035 TO |
1389 | $valid = FALSE; |
1390 | $error .= $element; | |
1391 | break; | |
1392 | } | |
1393 | } | |
1394 | } | |
1395 | else { | |
a7488080 | 1396 | if (!empty($params[$field])) { |
6a488035 TO |
1397 | $valid = TRUE; |
1398 | } | |
1399 | } | |
1400 | if ($valid) { | |
1401 | break; | |
1402 | } | |
1403 | } | |
1404 | ||
1405 | if (!$valid) { | |
1406 | return civicrm_api3_create_error("Required fields not found for {$params['contact_type']} : $error"); | |
1407 | } | |
1408 | } | |
1409 | } | |
1410 | ||
1411 | if ($dupeCheck) { | |
1412 | // check for record already existing | |
1413 | require_once 'CRM/Dedupe/Finder.php'; | |
1414 | $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); | |
1415 | ||
1416 | // CRM-6431 | |
1417 | // setting 'check_permission' here means that the dedupe checking will be carried out even if the | |
1418 | // person does not have permission to carry out de-dupes | |
1419 | // this is similar to the front end form | |
1420 | if (isset($params['check_permission'])) { | |
1421 | $dedupeParams['check_permission'] = $params['check_permission']; | |
1422 | } | |
1423 | ||
1424 | $ids = implode(',', CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised', array(), $dedupeRuleGroupID)); | |
1425 | ||
1426 | if ($ids != NULL) { | |
1427 | if ($dupeErrorArray) { | |
1428 | $error = CRM_Core_Error::createError("Found matching contacts: $ids", | |
1429 | CRM_Core_Error::DUPLICATE_CONTACT, | |
1430 | 'Fatal', $ids | |
1431 | ); | |
1432 | return civicrm_api3_create_error($error->pop()); | |
1433 | } | |
1434 | ||
1435 | return civicrm_api3_create_error("Found matching contacts: $ids"); | |
1436 | } | |
1437 | } | |
1438 | ||
1439 | //check for organisations with same name | |
a7488080 | 1440 | if (!empty($params['current_employer'])) { |
6a488035 TO |
1441 | $organizationParams = array(); |
1442 | $organizationParams['organization_name'] = $params['current_employer']; | |
1443 | ||
1444 | require_once 'CRM/Dedupe/Finder.php'; | |
1445 | $dedupParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization'); | |
1446 | ||
1447 | $dedupParams['check_permission'] = FALSE; | |
1448 | $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupParams, 'Organization', 'Supervised'); | |
1449 | ||
1450 | // check for mismatch employer name and id | |
a7488080 | 1451 | if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds) |
6a488035 TO |
1452 | ) { |
1453 | return civicrm_api3_create_error('Employer name and Employer id Mismatch'); | |
1454 | } | |
1455 | ||
1456 | // show error if multiple organisation with same name exist | |
a7488080 | 1457 | if (empty($params['employer_id']) && (count($dupeIds) > 1) |
6a488035 TO |
1458 | ) { |
1459 | return civicrm_api3_create_error('Found more than one Organisation with same Name.'); | |
1460 | } | |
1461 | } | |
1462 | ||
1463 | return NULL; | |
1464 | } | |
1465 | ||
109ff2ac DL |
1466 | /** |
1467 | * | |
f4aaa82a EM |
1468 | * @param $result |
1469 | * @param $activityTypeID | |
1470 | * | |
1471 | * @internal param $ <type> $result | |
1472 | * @internal param $ <type> $activityTypeID | |
109ff2ac | 1473 | * |
f4aaa82a | 1474 | * @return array <type> $params |
109ff2ac DL |
1475 | */ |
1476 | function _civicrm_api3_deprecated_activity_buildmailparams($result, $activityTypeID) { | |
1477 | // get ready for collecting data about activity to be created | |
1478 | $params = array(); | |
1479 | ||
1480 | $params['activity_type_id'] = $activityTypeID; | |
1481 | ||
1482 | $params['status_id'] = 2; | |
1483 | $params['source_contact_id'] = $params['assignee_contact_id'] = $result['from']['id']; | |
1484 | $params['target_contact_id'] = array(); | |
1485 | $keys = array('to', 'cc', 'bcc'); | |
1486 | foreach ($keys as $key) { | |
1487 | if (is_array($result[$key])) { | |
1488 | foreach ($result[$key] as $key => $keyValue) { | |
1489 | if (!empty($keyValue['id'])) { | |
1490 | $params['target_contact_id'][] = $keyValue['id']; | |
1491 | } | |
1492 | } | |
1493 | } | |
1494 | } | |
1495 | $params['subject'] = $result['subject']; | |
1496 | $params['activity_date_time'] = $result['date']; | |
1497 | $params['details'] = $result['body']; | |
1498 | ||
1499 | for ($i = 1; $i <= 5; $i++) { | |
1500 | if (isset($result["attachFile_$i"])) { | |
1501 | $params["attachFile_$i"] = $result["attachFile_$i"]; | |
1502 | } | |
1503 | } | |
1504 | ||
1505 | return $params; | |
1506 | } |