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