Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | |
6a488035 TO |
49 | */ |
50 | function _civicrm_api3_deprecated_participant_formatted_param($params, &$values, $create = FALSE) { | |
51 | $fields = CRM_Event_DAO_Participant::fields(); | |
52 | _civicrm_api3_store_values($fields, $params, $values); | |
53 | ||
54 | require_once 'CRM/Core/OptionGroup.php'; | |
55 | $customFields = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE); | |
56 | ||
57 | foreach ($params as $key => $value) { | |
58 | // ignore empty values or empty arrays etc | |
59 | if (CRM_Utils_System::isNull($value)) { | |
60 | continue; | |
61 | } | |
62 | ||
63 | //Handling Custom Data | |
64 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
65 | $values[$key] = $value; | |
66 | $type = $customFields[$customFieldID]['html_type']; | |
67 | if ($type == 'CheckBox' || $type == 'Multi-Select') { | |
91bb24a7 | 68 | $mulValues = explode(',', $value); |
6a488035 TO |
69 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); |
70 | $values[$key] = array(); | |
71 | foreach ($mulValues as $v1) { | |
72 | foreach ($customOption as $customValueID => $customLabel) { | |
73 | $customValue = $customLabel['value']; | |
74 | if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) || | |
75 | (strtolower(trim($customValue)) == strtolower(trim($v1))) | |
76 | ) { | |
77 | if ($type == 'CheckBox') { | |
78 | $values[$key][$customValue] = 1; | |
79 | } | |
80 | else { | |
81 | $values[$key][] = $customValue; | |
82 | } | |
83 | } | |
84 | } | |
85 | } | |
86 | } | |
87 | elseif ($type == 'Select' || $type == 'Radio') { | |
88 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
89 | foreach ($customOption as $customFldID => $customValue) { | |
91bb24a7 | 90 | $val = CRM_Utils_Array::value('value', $customValue); |
6a488035 TO |
91 | $label = CRM_Utils_Array::value('label', $customValue); |
92 | $label = strtolower($label); | |
93 | $value = strtolower(trim($value)); | |
94 | if (($value == $label) || ($value == strtolower($val))) { | |
95 | $values[$key] = $val; | |
96 | } | |
97 | } | |
98 | } | |
99 | } | |
100 | ||
101 | switch ($key) { | |
102 | case 'participant_contact_id': | |
103 | if (!CRM_Utils_Rule::integer($value)) { | |
104 | return civicrm_api3_create_error("contact_id not valid: $value"); | |
105 | } | |
91bb24a7 | 106 | $dao = new CRM_Core_DAO(); |
6a488035 | 107 | $qParams = array(); |
91bb24a7 | 108 | $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value", |
6a488035 TO |
109 | $qParams |
110 | ); | |
111 | if (!$svq) { | |
112 | return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); | |
113 | } | |
114 | $values['contact_id'] = $values['participant_contact_id']; | |
115 | unset($values['participant_contact_id']); | |
116 | break; | |
117 | ||
118 | case 'participant_register_date': | |
119 | if (!CRM_Utils_Rule::dateTime($value)) { | |
120 | return civicrm_api3_create_error("$key not a valid date: $value"); | |
121 | } | |
122 | break; | |
123 | ||
124 | case 'event_title': | |
125 | $id = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $value, 'id', 'title'); | |
126 | $values['event_id'] = $id; | |
127 | break; | |
128 | ||
129 | case 'event_id': | |
130 | if (!CRM_Utils_Rule::integer($value)) { | |
131 | return civicrm_api3_create_error("Event ID is not valid: $value"); | |
132 | } | |
91bb24a7 | 133 | $dao = new CRM_Core_DAO(); |
6a488035 | 134 | $qParams = array(); |
91bb24a7 | 135 | $svq = $dao->singleValueQuery("SELECT id FROM civicrm_event WHERE id = $value", |
6a488035 TO |
136 | $qParams |
137 | ); | |
138 | if (!$svq) { | |
139 | return civicrm_api3_create_error("Invalid Event ID: There is no event record with event_id = $value."); | |
140 | } | |
141 | break; | |
142 | ||
143 | case 'participant_status_id': | |
144 | if (!CRM_Utils_Rule::integer($value)) { | |
145 | return civicrm_api3_create_error("Event Status ID is not valid: $value"); | |
146 | } | |
147 | break; | |
148 | ||
149 | case 'participant_status': | |
150 | $status = CRM_Event_PseudoConstant::participantStatus(); | |
151 | $values['participant_status_id'] = CRM_Utils_Array::key($value, $status);; | |
152 | break; | |
153 | ||
154 | case 'participant_role_id': | |
155 | case 'participant_role': | |
156 | $role = CRM_Event_PseudoConstant::participantRole(); | |
157 | $participantRoles = explode(",", $value); | |
158 | foreach ($participantRoles as $k => $v) { | |
159 | $v = trim($v); | |
160 | if ($key == 'participant_role') { | |
161 | $participantRoles[$k] = CRM_Utils_Array::key($v, $role); | |
162 | } | |
163 | else { | |
164 | $participantRoles[$k] = $v; | |
165 | } | |
166 | } | |
167 | require_once 'CRM/Core/DAO.php'; | |
168 | $values['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $participantRoles); | |
169 | unset($values[$key]); | |
170 | break; | |
171 | ||
172 | default: | |
173 | break; | |
174 | } | |
175 | } | |
176 | ||
177 | if (array_key_exists('participant_note', $params)) { | |
178 | $values['participant_note'] = $params['participant_note']; | |
179 | } | |
180 | ||
181 | if ($create) { | |
182 | // CRM_Event_BAO_Participant::create() handles register_date, | |
183 | // status_id and source. So, if $values contains | |
184 | // participant_register_date, participant_status_id or participant_source, | |
185 | // convert it to register_date, status_id or source | |
186 | $changes = array( | |
187 | 'participant_register_date' => 'register_date', | |
188 | 'participant_source' => 'source', | |
189 | 'participant_status_id' => 'status_id', | |
190 | 'participant_role_id' => 'role_id', | |
191 | 'participant_fee_level' => 'fee_level', | |
192 | 'participant_fee_amount' => 'fee_amount', | |
193 | 'participant_id' => 'id', | |
194 | ); | |
195 | ||
196 | foreach ($changes as $orgVal => $changeVal) { | |
197 | if (isset($values[$orgVal])) { | |
198 | $values[$changeVal] = $values[$orgVal]; | |
199 | unset($values[$orgVal]); | |
200 | } | |
201 | } | |
202 | } | |
203 | ||
204 | return NULL; | |
205 | } | |
206 | ||
207 | /** | |
208 | * take the input parameter list as specified in the data model and | |
209 | * convert it into the same format that we use in QF and BAO object | |
210 | * | |
f4aaa82a | 211 | * @param array $params Associative array of property name/value |
6a488035 | 212 | * pairs to insert in new contact. |
f4aaa82a | 213 | * @param array $values The reformatted properties that we can use internally |
6a488035 TO |
214 | * ' |
215 | * | |
f4aaa82a EM |
216 | * @param bool $create |
217 | * @param null $onDuplicate | |
218 | * | |
6a488035 | 219 | * @return array|CRM_Error |
6a488035 | 220 | */ |
1221efe9 | 221 | function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = FALSE, $onDuplicate = Null) { |
6a488035 TO |
222 | // copy all the contribution fields as is |
223 | ||
224 | $fields = CRM_Contribute_DAO_Contribution::fields(); | |
225 | ||
226 | _civicrm_api3_store_values($fields, $params, $values); | |
227 | ||
228 | require_once 'CRM/Core/OptionGroup.php'; | |
229 | $customFields = CRM_Core_BAO_CustomField::getFields('Contribution', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE); | |
230 | ||
231 | foreach ($params as $key => $value) { | |
232 | // ignore empty values or empty arrays etc | |
233 | if (CRM_Utils_System::isNull($value)) { | |
234 | continue; | |
235 | } | |
236 | ||
237 | //Handling Custom Data | |
238 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
239 | $values[$key] = $value; | |
240 | $type = $customFields[$customFieldID]['html_type']; | |
241 | if ($type == 'CheckBox' || $type == 'Multi-Select') { | |
91bb24a7 | 242 | $mulValues = explode(',', $value); |
6a488035 TO |
243 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); |
244 | $values[$key] = array(); | |
245 | foreach ($mulValues as $v1) { | |
246 | foreach ($customOption as $customValueID => $customLabel) { | |
247 | $customValue = $customLabel['value']; | |
248 | if ((strtolower($customLabel['label']) == strtolower(trim($v1))) || | |
249 | (strtolower($customValue) == strtolower(trim($v1))) | |
250 | ) { | |
251 | if ($type == 'CheckBox') { | |
252 | $values[$key][$customValue] = 1; | |
253 | } | |
254 | else { | |
255 | $values[$key][] = $customValue; | |
256 | } | |
257 | } | |
258 | } | |
259 | } | |
260 | } | |
261 | elseif ($type == 'Select' || $type == 'Radio' || | |
262 | ($type == 'Autocomplete-Select' && | |
263 | $customFields[$customFieldID]['data_type'] == 'String' | |
264 | ) | |
265 | ) { | |
266 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
267 | foreach ($customOption as $customFldID => $customValue) { | |
91bb24a7 | 268 | $val = CRM_Utils_Array::value('value', $customValue); |
6a488035 TO |
269 | $label = CRM_Utils_Array::value('label', $customValue); |
270 | $label = strtolower($label); | |
271 | $value = strtolower(trim($value)); | |
272 | if (($value == $label) || ($value == strtolower($val))) { | |
273 | $values[$key] = $val; | |
274 | } | |
275 | } | |
276 | } | |
277 | } | |
278 | ||
279 | switch ($key) { | |
280 | case 'contribution_contact_id': | |
281 | if (!CRM_Utils_Rule::integer($value)) { | |
282 | return civicrm_api3_create_error("contact_id not valid: $value"); | |
283 | } | |
91bb24a7 | 284 | $dao = new CRM_Core_DAO(); |
6a488035 | 285 | $qParams = array(); |
7bc2eb38 | 286 | $svq = $dao->singleValueQuery("SELECT is_deleted FROM civicrm_contact WHERE id = $value", |
6a488035 TO |
287 | $qParams |
288 | ); | |
7bc2eb38 | 289 | if (!isset($svq)) { |
6a488035 | 290 | return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); |
7bc2eb38 KW |
291 | } else if ($svq == 1) { |
292 | return civicrm_api3_create_error("Invalid Contact ID: contact_id $value is a soft-deleted contact."); | |
6a488035 TO |
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 | /** | |
c490a46a | 597 | * check duplicate contacts based on de-deupe parameters |
6a488035 TO |
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 | |
6a488035 TO |
679 | */ |
680 | function _civicrm_api3_deprecated_activity_formatted_param(&$params, &$values, $create = FALSE) { | |
681 | // copy all the activity fields as is | |
682 | $fields = CRM_Activity_DAO_Activity::fields(); | |
683 | _civicrm_api3_store_values($fields, $params, $values); | |
684 | ||
685 | require_once 'CRM/Core/OptionGroup.php'; | |
686 | $customFields = CRM_Core_BAO_CustomField::getFields('Activity'); | |
687 | ||
688 | foreach ($params as $key => $value) { | |
689 | // ignore empty values or empty arrays etc | |
690 | if (CRM_Utils_System::isNull($value)) { | |
691 | continue; | |
692 | } | |
693 | ||
694 | //Handling Custom Data | |
695 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
696 | $values[$key] = $value; | |
697 | $type = $customFields[$customFieldID]['html_type']; | |
698 | if ($type == 'CheckBox' || $type == 'Multi-Select') { | |
91bb24a7 | 699 | $mulValues = explode(',', $value); |
6a488035 TO |
700 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); |
701 | $values[$key] = array(); | |
702 | foreach ($mulValues as $v1) { | |
703 | foreach ($customOption as $customValueID => $customLabel) { | |
704 | $customValue = $customLabel['value']; | |
705 | if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) || | |
706 | (strtolower(trim($customValue)) == strtolower(trim($v1))) | |
707 | ) { | |
708 | if ($type == 'CheckBox') { | |
709 | $values[$key][$customValue] = 1; | |
710 | } | |
711 | else { | |
712 | $values[$key][] = $customValue; | |
713 | } | |
714 | } | |
715 | } | |
716 | } | |
717 | } | |
718 | elseif ($type == 'Select' || $type == 'Radio') { | |
719 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); | |
720 | foreach ($customOption as $customFldID => $customValue) { | |
91bb24a7 | 721 | $val = CRM_Utils_Array::value('value', $customValue); |
6a488035 TO |
722 | $label = CRM_Utils_Array::value('label', $customValue); |
723 | $label = strtolower($label); | |
724 | $value = strtolower(trim($value)); | |
725 | if (($value == $label) || ($value == strtolower($val))) { | |
726 | $values[$key] = $val; | |
727 | } | |
728 | } | |
729 | } | |
730 | } | |
731 | ||
732 | if ($key == 'target_contact_id') { | |
733 | if (!CRM_Utils_Rule::integer($value)) { | |
734 | return civicrm_api3_create_error("contact_id not valid: $value"); | |
735 | } | |
736 | $contactID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value"); | |
737 | if (!$contactID) { | |
738 | return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); | |
739 | } | |
740 | } | |
741 | } | |
742 | return NULL; | |
743 | } | |
744 | ||
745 | /** | |
746 | * This function adds the contact variable in $values to the | |
747 | * parameter list $params. For most cases, $values should have length 1. If | |
748 | * the variable being added is a child of Location, a location_type_id must | |
749 | * also be included. If it is a child of phone, a phone_type must be included. | |
750 | * | |
751 | * @param array $values The variable(s) to be added | |
752 | * @param array $params The structured parameter list | |
753 | * | |
754 | * @return bool|CRM_Utils_Error | |
6a488035 TO |
755 | */ |
756 | function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { | |
757 | /* Crawl through the possible classes: | |
758 | * Contact | |
759 | * Individual | |
760 | * Household | |
761 | * Organization | |
762 | * Location | |
763 | * Address | |
764 | ||
765 | * Phone | |
766 | * IM | |
767 | * Note | |
768 | * Custom | |
769 | */ | |
770 | ||
6a488035 | 771 | /* Cache the various object fields */ |
6a488035 TO |
772 | static $fields = NULL; |
773 | ||
774 | if ($fields == NULL) { | |
775 | $fields = array(); | |
776 | } | |
777 | ||
778 | //first add core contact values since for other Civi modules they are not added | |
779 | require_once 'CRM/Contact/BAO/Contact.php'; | |
780 | $contactFields = CRM_Contact_DAO_Contact::fields(); | |
781 | _civicrm_api3_store_values($contactFields, $values, $params); | |
782 | ||
783 | if (isset($values['contact_type'])) { | |
784 | /* we're an individual/household/org property */ | |
785 | ||
6a488035 TO |
786 | $fields[$values['contact_type']] = CRM_Contact_DAO_Contact::fields(); |
787 | ||
788 | _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params); | |
789 | return TRUE; | |
790 | } | |
791 | ||
792 | if (isset($values['individual_prefix'])) { | |
a7488080 | 793 | if (!empty($params['prefix_id'])) { |
91bb24a7 | 794 | $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
6a488035 TO |
795 | $params['prefix'] = $prefixes[$params['prefix_id']]; |
796 | } | |
797 | else { | |
798 | $params['prefix'] = $values['individual_prefix']; | |
799 | } | |
800 | return TRUE; | |
801 | } | |
802 | ||
803 | if (isset($values['individual_suffix'])) { | |
a7488080 | 804 | if (!empty($params['suffix_id'])) { |
91bb24a7 | 805 | $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); |
6a488035 TO |
806 | $params['suffix'] = $suffixes[$params['suffix_id']]; |
807 | } | |
808 | else { | |
809 | $params['suffix'] = $values['individual_suffix']; | |
810 | } | |
811 | return TRUE; | |
812 | } | |
813 | ||
814 | //CRM-4575 | |
815 | if (isset($values['email_greeting'])) { | |
a7488080 | 816 | if (!empty($params['email_greeting_id'])) { |
91bb24a7 | 817 | $emailGreetingFilter = array( |
818 | 'contact_type' => CRM_Utils_Array::value('contact_type', $params), | |
6a488035 TO |
819 | 'greeting_type' => 'email_greeting', |
820 | ); | |
821 | $emailGreetings = CRM_Core_PseudoConstant::greeting($emailGreetingFilter); | |
822 | $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']]; | |
823 | } | |
824 | else { | |
825 | $params['email_greeting'] = $values['email_greeting']; | |
826 | } | |
827 | ||
828 | return TRUE; | |
829 | } | |
830 | ||
831 | if (isset($values['postal_greeting'])) { | |
a7488080 | 832 | if (!empty($params['postal_greeting_id'])) { |
91bb24a7 | 833 | $postalGreetingFilter = array( |
834 | 'contact_type' => CRM_Utils_Array::value('contact_type', $params), | |
6a488035 TO |
835 | 'greeting_type' => 'postal_greeting', |
836 | ); | |
837 | $postalGreetings = CRM_Core_PseudoConstant::greeting($postalGreetingFilter); | |
838 | $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']]; | |
839 | } | |
840 | else { | |
841 | $params['postal_greeting'] = $values['postal_greeting']; | |
842 | } | |
843 | return TRUE; | |
844 | } | |
845 | ||
846 | if (isset($values['addressee'])) { | |
a7488080 | 847 | if (!empty($params['addressee_id'])) { |
91bb24a7 | 848 | $addresseeFilter = array( |
849 | 'contact_type' => CRM_Utils_Array::value('contact_type', $params), | |
6a488035 TO |
850 | 'greeting_type' => 'addressee', |
851 | ); | |
852 | $addressee = CRM_Core_PseudoConstant::addressee($addresseeFilter); | |
853 | $params['addressee'] = $addressee[$params['addressee_id']]; | |
854 | } | |
855 | else { | |
856 | $params['addressee'] = $values['addressee']; | |
857 | } | |
858 | return TRUE; | |
859 | } | |
860 | ||
861 | if (isset($values['gender'])) { | |
a7488080 | 862 | if (!empty($params['gender_id'])) { |
91bb24a7 | 863 | $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); |
6a488035 TO |
864 | $params['gender'] = $genders[$params['gender_id']]; |
865 | } | |
866 | else { | |
867 | $params['gender'] = $values['gender']; | |
868 | } | |
869 | return TRUE; | |
870 | } | |
871 | ||
df5ad245 | 872 | if (!empty($values['preferred_communication_method'])) { |
91bb24a7 | 873 | $comm = array(); |
874 | $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER); | |
6a488035 TO |
875 | |
876 | $preffComm = explode(',', $values['preferred_communication_method']); | |
877 | foreach ($preffComm as $v) { | |
878 | $v = strtolower(trim($v)); | |
879 | if (array_key_exists($v, $pcm)) { | |
880 | $comm[$pcm[$v]] = 1; | |
881 | } | |
882 | } | |
883 | ||
884 | $params['preferred_communication_method'] = $comm; | |
885 | return TRUE; | |
886 | } | |
887 | ||
888 | //format the website params. | |
a7488080 | 889 | if (!empty($values['url'])) { |
6a488035 TO |
890 | static $websiteFields; |
891 | if (!is_array($websiteFields)) { | |
892 | require_once 'CRM/Core/DAO/Website.php'; | |
893 | $websiteFields = CRM_Core_DAO_Website::fields(); | |
894 | } | |
895 | if (!array_key_exists('website', $params) || | |
896 | !is_array($params['website']) | |
897 | ) { | |
898 | $params['website'] = array(); | |
899 | } | |
900 | ||
901 | $websiteCount = count($params['website']); | |
902 | _civicrm_api3_store_values($websiteFields, $values, | |
903 | $params['website'][++$websiteCount] | |
904 | ); | |
905 | ||
906 | return TRUE; | |
907 | } | |
908 | ||
909 | // get the formatted location blocks into params - w/ 3.0 format, CRM-4605 | |
a7488080 | 910 | if (!empty($values['location_type_id'])) { |
6a488035 TO |
911 | _civicrm_api3_deprecated_add_formatted_location_blocks($values, $params); |
912 | return TRUE; | |
913 | } | |
914 | ||
915 | if (isset($values['note'])) { | |
916 | /* add a note field */ | |
6a488035 TO |
917 | if (!isset($params['note'])) { |
918 | $params['note'] = array(); | |
919 | } | |
920 | $noteBlock = count($params['note']) + 1; | |
921 | ||
922 | $params['note'][$noteBlock] = array(); | |
923 | if (!isset($fields['Note'])) { | |
924 | $fields['Note'] = CRM_Core_DAO_Note::fields(); | |
925 | } | |
926 | ||
927 | // get the current logged in civicrm user | |
928 | $session = CRM_Core_Session::singleton(); | |
929 | $userID = $session->get('userID'); | |
930 | ||
931 | if ($userID) { | |
932 | $values['contact_id'] = $userID; | |
933 | } | |
934 | ||
935 | _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]); | |
936 | ||
937 | return TRUE; | |
938 | } | |
939 | ||
940 | /* Check for custom field values */ | |
941 | ||
a7488080 | 942 | if (empty($fields['custom'])) { |
6a488035 TO |
943 | $fields['custom'] = &CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $values), |
944 | FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE | |
945 | ); | |
946 | } | |
947 | ||
948 | foreach ($values as $key => $value) { | |
949 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
950 | /* check if it's a valid custom field id */ | |
951 | ||
6a488035 TO |
952 | if (!array_key_exists($customFieldID, $fields['custom'])) { |
953 | return civicrm_api3_create_error('Invalid custom field ID'); | |
954 | } | |
955 | else { | |
956 | $params[$key] = $value; | |
957 | } | |
958 | } | |
959 | } | |
960 | } | |
961 | ||
962 | /** | |
963 | * This function format location blocks w/ v3.0 format. | |
964 | * | |
965 | * @param array $values The variable(s) to be added | |
966 | * @param array $params The structured parameter list | |
967 | * | |
968 | * @return bool | |
6a488035 TO |
969 | */ |
970 | function _civicrm_api3_deprecated_add_formatted_location_blocks(&$values, &$params) { | |
971 | static $fields = NULL; | |
972 | if ($fields == NULL) { | |
973 | $fields = array(); | |
974 | } | |
975 | ||
976 | foreach (array( | |
977 | 'Phone', 'Email', 'IM', 'OpenID','Phone_Ext') as $block) { | |
978 | $name = strtolower($block); | |
979 | if (!array_key_exists($name, $values)) { | |
980 | continue; | |
981 | } | |
982 | ||
983 | if($name == 'phone_ext'){ | |
984 | $block = 'Phone'; | |
985 | } | |
986 | ||
987 | // block present in value array. | |
988 | if (!array_key_exists($name, $params) || !is_array($params[$name])) { | |
989 | $params[$name] = array(); | |
990 | } | |
991 | ||
992 | if (!array_key_exists($block, $fields)) { | |
0e6e8724 DL |
993 | $className = "CRM_Core_DAO_$block"; |
994 | $fields[$block] =& $className::fields( ); | |
6a488035 TO |
995 | } |
996 | ||
997 | $blockCnt = count($params[$name]); | |
998 | ||
999 | // copy value to dao field name. | |
1000 | if ($name == 'im') { | |
1001 | $values['name'] = $values[$name]; | |
1002 | } | |
1003 | ||
1004 | _civicrm_api3_store_values($fields[$block], $values, | |
1005 | $params[$name][++$blockCnt] | |
1006 | ); | |
1007 | ||
a7488080 | 1008 | if (empty($params['id']) && ($blockCnt == 1)) { |
6a488035 TO |
1009 | $params[$name][$blockCnt]['is_primary'] = TRUE; |
1010 | } | |
1011 | ||
1012 | // we only process single block at a time. | |
1013 | return TRUE; | |
1014 | } | |
1015 | ||
1016 | // handle address fields. | |
1017 | if (!array_key_exists('address', $params) || !is_array($params['address'])) { | |
1018 | $params['address'] = array(); | |
1019 | } | |
1020 | ||
1021 | $addressCnt = 1; | |
1022 | foreach ($params['address'] as $cnt => $addressBlock) { | |
1023 | if (CRM_Utils_Array::value('location_type_id', $values) == | |
1024 | CRM_Utils_Array::value('location_type_id', $addressBlock) | |
1025 | ) { | |
1026 | $addressCnt = $cnt; | |
1027 | break; | |
1028 | } | |
1029 | $addressCnt++; | |
1030 | } | |
1031 | ||
1032 | if (!array_key_exists('Address', $fields)) { | |
1033 | require_once 'CRM/Core/DAO/Address.php'; | |
1034 | $fields['Address'] = CRM_Core_DAO_Address::fields(); | |
1035 | } | |
1036 | ||
1037 | // Note: we doing multiple value formatting here for address custom fields, plus putting into right format. | |
1038 | // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving | |
1039 | // the address in CRM_Core_BAO_Address::create method | |
a7488080 | 1040 | if (!empty($values['location_type_id'])) { |
6a488035 TO |
1041 | static $customFields = array(); |
1042 | if (empty($customFields)) { | |
1043 | $customFields = CRM_Core_BAO_CustomField::getFields('Address'); | |
1044 | } | |
1045 | // make a copy of values, as we going to make changes | |
1046 | $newValues = $values; | |
1047 | foreach ($values as $key => $val) { | |
1048 | $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key); | |
1049 | if ($customFieldID && array_key_exists($customFieldID, $customFields)) { | |
1050 | // mark an entry in fields array since we want the value of custom field to be copied | |
1051 | $fields['Address'][$key] = null; | |
1052 | ||
1053 | $htmlType = CRM_Utils_Array::value( 'html_type', $customFields[$customFieldID] ); | |
1054 | switch ( $htmlType ) { | |
1055 | case 'CheckBox': | |
1056 | case 'AdvMulti-Select': | |
1057 | case 'Multi-Select': | |
1058 | if ( $val ) { | |
1059 | $mulValues = explode( ',', $val ); | |
1060 | $customOption = CRM_Core_BAO_CustomOption::getCustomOption( $customFieldID, true ); | |
1061 | $newValues[$key] = array( ); | |
1062 | foreach ( $mulValues as $v1 ) { | |
1063 | foreach ( $customOption as $v2 ) { | |
1064 | if ( ( strtolower( $v2['label'] ) == strtolower( trim( $v1 ) ) ) || | |
1065 | ( strtolower( $v2['value'] ) == strtolower( trim( $v1 ) ) ) ) { | |
1066 | if ( $htmlType == 'CheckBox' ) { | |
1067 | $newValues[$key][$v2['value']] = 1; | |
1068 | } else { | |
1069 | $newValues[$key][] = $v2['value']; | |
1070 | } | |
1071 | } | |
1072 | } | |
1073 | } | |
1074 | } | |
1075 | break; | |
1076 | } | |
1077 | } | |
1078 | } | |
1079 | // consider new values | |
1080 | $values = $newValues; | |
1081 | } | |
1082 | ||
1083 | _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]); | |
1084 | ||
1085 | $addressFields = array( | |
1086 | 'county', 'country', 'state_province', | |
1087 | 'supplemental_address_1', 'supplemental_address_2', | |
1088 | 'StateProvince.name', | |
1089 | ); | |
1090 | ||
1091 | foreach ($addressFields as $field) { | |
1092 | if (array_key_exists($field, $values)) { | |
1093 | if (!array_key_exists('address', $params)) { | |
1094 | $params['address'] = array(); | |
1095 | } | |
1096 | $params['address'][$addressCnt][$field] = $values[$field]; | |
1097 | } | |
1098 | } | |
1099 | ||
1100 | if ($addressCnt == 1) { | |
1101 | ||
1102 | $params['address'][$addressCnt]['is_primary'] = TRUE; | |
1103 | } | |
1104 | ||
1105 | return TRUE; | |
1106 | } | |
1107 | ||
1108 | /** | |
1109 | * | |
1110 | * @param <type> $params | |
1111 | * | |
f4aaa82a | 1112 | * @return array <type> |
6a488035 TO |
1113 | */ |
1114 | function _civicrm_api3_deprecated_duplicate_formatted_contact($params) { | |
1115 | $id = CRM_Utils_Array::value('id', $params); | |
1116 | $externalId = CRM_Utils_Array::value('external_identifier', $params); | |
1117 | if ($id || $externalId) { | |
1118 | $contact = new CRM_Contact_DAO_Contact(); | |
1119 | ||
1120 | $contact->id = $id; | |
1121 | $contact->external_identifier = $externalId; | |
1122 | ||
1123 | if ($contact->find(TRUE)) { | |
1124 | if ($params['contact_type'] != $contact->contact_type) { | |
1125 | return civicrm_api3_create_error("Mismatched contact IDs OR Mismatched contact Types"); | |
1126 | } | |
1127 | ||
1128 | $error = CRM_Core_Error::createError("Found matching contacts: $contact->id", | |
1129 | CRM_Core_Error::DUPLICATE_CONTACT, | |
1130 | 'Fatal', $contact->id | |
1131 | ); | |
1132 | return civicrm_api3_create_error($error->pop()); | |
1133 | } | |
1134 | } | |
1135 | else { | |
1136 | require_once 'CRM/Dedupe/Finder.php'; | |
1137 | $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); | |
1138 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised'); | |
1139 | ||
1140 | if (!empty($ids)) { | |
1141 | $ids = implode(',', $ids); | |
1142 | $error = CRM_Core_Error::createError("Found matching contacts: $ids", | |
1143 | CRM_Core_Error::DUPLICATE_CONTACT, | |
1144 | 'Fatal', $ids | |
1145 | ); | |
1146 | return civicrm_api3_create_error($error->pop()); | |
1147 | } | |
1148 | } | |
1149 | return civicrm_api3_create_success(TRUE); | |
1150 | } | |
1151 | ||
1152 | /** | |
1153 | * Validate a formatted contact parameter list. | |
1154 | * | |
1155 | * @param array $params Structured parameter list (as in crm_format_params) | |
1156 | * | |
1157 | * @return bool|CRM_Core_Error | |
6a488035 TO |
1158 | */ |
1159 | function _civicrm_api3_deprecated_validate_formatted_contact(&$params) { | |
1160 | /* Look for offending email addresses */ | |
1161 | ||
6a488035 TO |
1162 | if (array_key_exists('email', $params)) { |
1163 | foreach ($params['email'] as $count => $values) { | |
1164 | if (!is_array($values)) { | |
1165 | continue; | |
1166 | } | |
1167 | if ($email = CRM_Utils_Array::value('email', $values)) { | |
1168 | //validate each email | |
1169 | if (!CRM_Utils_Rule::email($email)) { | |
1170 | return civicrm_api3_create_error('No valid email address'); | |
1171 | } | |
1172 | ||
1173 | //check for loc type id. | |
a7488080 | 1174 | if (empty($values['location_type_id'])) { |
6a488035 TO |
1175 | return civicrm_api3_create_error('Location Type Id missing.'); |
1176 | } | |
1177 | } | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | /* Validate custom data fields */ | |
6a488035 TO |
1182 | if (array_key_exists('custom', $params) && is_array($params['custom'])) { |
1183 | foreach ($params['custom'] as $key => $custom) { | |
1184 | if (is_array($custom)) { | |
1185 | foreach ($custom as $fieldId => $value) { | |
1186 | $valid = CRM_Core_BAO_CustomValue::typecheck(CRM_Utils_Array::value('type', $value), | |
1187 | CRM_Utils_Array::value('value', $value) | |
1188 | ); | |
70dd449f | 1189 | if (!$valid && $value['is_required']) { |
6a488035 TO |
1190 | return civicrm_api3_create_error('Invalid value for custom field \'' . |
1191 | CRM_Utils_Array::value('name', $custom) . '\'' | |
1192 | ); | |
1193 | } | |
1194 | if (CRM_Utils_Array::value('type', $custom) == 'Date') { | |
1195 | $params['custom'][$key][$fieldId]['value'] = str_replace('-', '', $params['custom'][$key][$fieldId]['value']); | |
1196 | } | |
1197 | } | |
1198 | } | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | return civicrm_api3_create_success(TRUE); | |
1203 | } | |
1204 | ||
6a488035 | 1205 | |
6a488035 TO |
1206 | /** |
1207 | * @deprecated - this is part of the import parser not the API & needs to be moved on out | |
1208 | * | |
c490a46a | 1209 | * @param array $params |
f4aaa82a | 1210 | * @param $onDuplicate |
6a488035 | 1211 | * |
f4aaa82a | 1212 | * @return array|bool <type> |
6a488035 TO |
1213 | */ |
1214 | function _civicrm_api3_deprecated_create_participant_formatted($params, $onDuplicate) { | |
1215 | require_once 'CRM/Event/Import/Parser.php'; | |
a05662ef | 1216 | if ($onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK) { |
6a488035 TO |
1217 | CRM_Core_Error::reset(); |
1218 | $error = _civicrm_api3_deprecated_participant_check_params($params, TRUE); | |
1219 | if (civicrm_error($error)) { | |
1220 | return $error; | |
1221 | } | |
1222 | } | |
1223 | require_once "api/v3/Participant.php"; | |
1224 | return civicrm_api3_participant_create($params); | |
1225 | } | |
1226 | ||
1227 | /** | |
1228 | * | |
1229 | * @param <type> $params | |
1230 | * | |
f4aaa82a EM |
1231 | * @param bool $checkDuplicate |
1232 | * | |
1233 | * @return array|bool <type> | |
6a488035 TO |
1234 | */ |
1235 | function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplicate = FALSE) { | |
1236 | ||
1237 | //check if participant id is valid or not | |
a7488080 | 1238 | if (!empty($params['id'])) { |
6a488035 TO |
1239 | $participant = new CRM_Event_BAO_Participant(); |
1240 | $participant->id = $params['id']; | |
1241 | if (!$participant->find(TRUE)) { | |
1242 | return civicrm_api3_create_error(ts('Participant id is not valid')); | |
1243 | } | |
1244 | } | |
1245 | require_once 'CRM/Contact/BAO/Contact.php'; | |
1246 | //check if contact id is valid or not | |
a7488080 | 1247 | if (!empty($params['contact_id'])) { |
6a488035 TO |
1248 | $contact = new CRM_Contact_BAO_Contact(); |
1249 | $contact->id = $params['contact_id']; | |
1250 | if (!$contact->find(TRUE)) { | |
1251 | return civicrm_api3_create_error(ts('Contact id is not valid')); | |
1252 | } | |
1253 | } | |
1254 | ||
1255 | //check that event id is not an template | |
a7488080 | 1256 | if (!empty($params['event_id'])) { |
6a488035 TO |
1257 | $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template'); |
1258 | if (!empty($isTemplate)) { | |
3770b22a | 1259 | return civicrm_api3_create_error(ts('Event templates are not meant to be registered.')); |
6a488035 TO |
1260 | } |
1261 | } | |
1262 | ||
1263 | $result = array(); | |
1264 | if ($checkDuplicate) { | |
1265 | if (CRM_Event_BAO_Participant::checkDuplicate($params, $result)) { | |
1266 | $participantID = array_pop($result); | |
1267 | ||
1268 | $error = CRM_Core_Error::createError("Found matching participant record.", | |
1269 | CRM_Core_Error::DUPLICATE_PARTICIPANT, | |
1270 | 'Fatal', $participantID | |
1271 | ); | |
1272 | ||
1273 | return civicrm_api3_create_error($error->pop(), | |
1274 | array( | |
1275 | 'contactID' => $params['contact_id'], | |
1276 | 'participantID' => $participantID, | |
1277 | ) | |
1278 | ); | |
1279 | } | |
1280 | } | |
1281 | return TRUE; | |
1282 | } | |
1283 | ||
1284 | /** | |
1285 | * Ensure that we have the right input parameters for custom data | |
1286 | * | |
1287 | * @param array $params Associative array of property name/value | |
1288 | * pairs to insert in new contact. | |
1289 | * @param string $csType contact subtype if exists/passed. | |
1290 | * | |
1291 | * @return null on success, error message otherwise | |
6a488035 TO |
1292 | */ |
1293 | function _civicrm_api3_deprecated_contact_check_custom_params($params, $csType = NULL) { | |
1294 | empty($csType) ? $onlyParent = TRUE : $onlyParent = FALSE; | |
1295 | ||
1296 | require_once 'CRM/Core/BAO/CustomField.php'; | |
1297 | $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], | |
1298 | FALSE, | |
1299 | FALSE, | |
1300 | $csType, | |
1301 | NULL, | |
1302 | $onlyParent, | |
1303 | FALSE, | |
1304 | FALSE | |
1305 | ); | |
1306 | ||
1307 | foreach ($params as $key => $value) { | |
1308 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { | |
1309 | /* check if it's a valid custom field id */ | |
6a488035 TO |
1310 | if (!array_key_exists($customFieldID, $customFields)) { |
1311 | ||
1312 | $errorMsg = "Invalid Custom Field Contact Type: {$params['contact_type']}"; | |
1313 | if (!empty($csType)) { | |
1314 | $errorMsg .= " or Mismatched SubType: " . implode(', ', (array)$csType); | |
1315 | } | |
1316 | return civicrm_api3_create_error($errorMsg); | |
1317 | } | |
1318 | } | |
1319 | } | |
1320 | } | |
1321 | ||
5bc392e6 | 1322 | /** |
c490a46a | 1323 | * @param array $params |
5bc392e6 EM |
1324 | * @param bool $dupeCheck |
1325 | * @param bool $dupeErrorArray | |
1326 | * @param bool $requiredCheck | |
100fef9d | 1327 | * @param int $dedupeRuleGroupID |
5bc392e6 EM |
1328 | * |
1329 | * @return array|null | |
1330 | */ | |
109ff2ac DL |
1331 | function _civicrm_api3_deprecated_contact_check_params( |
1332 | &$params, | |
1333 | $dupeCheck = TRUE, | |
1334 | $dupeErrorArray = FALSE, | |
1335 | $requiredCheck = TRUE, | |
1336 | $dedupeRuleGroupID = NULL) { | |
6a488035 TO |
1337 | if (isset($params['id']) && is_numeric($params['id'])) { |
1338 | $requiredCheck = FALSE; | |
1339 | } | |
1340 | if ($requiredCheck) { | |
1341 | if (isset($params['id'])) { | |
1342 | $required = array('Individual', 'Household', 'Organization'); | |
1343 | } | |
1344 | $required = array( | |
1345 | 'Individual' => array( | |
1346 | array('first_name', 'last_name'), | |
1347 | 'email', | |
1348 | ), | |
1349 | 'Household' => array( | |
1350 | 'household_name', | |
1351 | ), | |
1352 | 'Organization' => array( | |
1353 | 'organization_name', | |
1354 | ), | |
1355 | ); | |
1356 | ||
1357 | ||
1358 | // contact_type has a limited number of valid values | |
81fc1677 E |
1359 | if(empty($params['contact_type'])) { |
1360 | return civicrm_api3_create_error("No Contact Type"); | |
1361 | } | |
6a488035 TO |
1362 | $fields = CRM_Utils_Array::value($params['contact_type'], $required); |
1363 | if ($fields == NULL) { | |
1364 | return civicrm_api3_create_error("Invalid Contact Type: {$params['contact_type']}"); | |
1365 | } | |
1366 | ||
1367 | if ($csType = CRM_Utils_Array::value('contact_sub_type', $params)) { | |
1368 | if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($csType, $params['contact_type']))) { | |
b8f96eb8 | 1369 | return civicrm_api3_create_error("Invalid or Mismatched Contact Subtype: " . implode(', ', (array)$csType)); |
6a488035 TO |
1370 | } |
1371 | } | |
1372 | ||
8cc574cf | 1373 | if (empty($params['contact_id']) && !empty($params['id'])) { |
6a488035 TO |
1374 | $valid = FALSE; |
1375 | $error = ''; | |
1376 | foreach ($fields as $field) { | |
1377 | if (is_array($field)) { | |
1378 | $valid = TRUE; | |
1379 | foreach ($field as $element) { | |
a7488080 | 1380 | if (empty($params[$element])) { |
6a488035 TO |
1381 | $valid = FALSE; |
1382 | $error .= $element; | |
1383 | break; | |
1384 | } | |
1385 | } | |
1386 | } | |
1387 | else { | |
a7488080 | 1388 | if (!empty($params[$field])) { |
6a488035 TO |
1389 | $valid = TRUE; |
1390 | } | |
1391 | } | |
1392 | if ($valid) { | |
1393 | break; | |
1394 | } | |
1395 | } | |
1396 | ||
1397 | if (!$valid) { | |
1398 | return civicrm_api3_create_error("Required fields not found for {$params['contact_type']} : $error"); | |
1399 | } | |
1400 | } | |
1401 | } | |
1402 | ||
1403 | if ($dupeCheck) { | |
1404 | // check for record already existing | |
1405 | require_once 'CRM/Dedupe/Finder.php'; | |
1406 | $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); | |
1407 | ||
1408 | // CRM-6431 | |
1409 | // setting 'check_permission' here means that the dedupe checking will be carried out even if the | |
1410 | // person does not have permission to carry out de-dupes | |
1411 | // this is similar to the front end form | |
1412 | if (isset($params['check_permission'])) { | |
1413 | $dedupeParams['check_permission'] = $params['check_permission']; | |
1414 | } | |
1415 | ||
1416 | $ids = implode(',', CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised', array(), $dedupeRuleGroupID)); | |
1417 | ||
1418 | if ($ids != NULL) { | |
1419 | if ($dupeErrorArray) { | |
1420 | $error = CRM_Core_Error::createError("Found matching contacts: $ids", | |
1421 | CRM_Core_Error::DUPLICATE_CONTACT, | |
1422 | 'Fatal', $ids | |
1423 | ); | |
1424 | return civicrm_api3_create_error($error->pop()); | |
1425 | } | |
1426 | ||
1427 | return civicrm_api3_create_error("Found matching contacts: $ids"); | |
1428 | } | |
1429 | } | |
1430 | ||
1431 | //check for organisations with same name | |
a7488080 | 1432 | if (!empty($params['current_employer'])) { |
6a488035 TO |
1433 | $organizationParams = array(); |
1434 | $organizationParams['organization_name'] = $params['current_employer']; | |
1435 | ||
1436 | require_once 'CRM/Dedupe/Finder.php'; | |
1437 | $dedupParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization'); | |
1438 | ||
1439 | $dedupParams['check_permission'] = FALSE; | |
1440 | $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupParams, 'Organization', 'Supervised'); | |
1441 | ||
1442 | // check for mismatch employer name and id | |
a7488080 | 1443 | if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds) |
6a488035 TO |
1444 | ) { |
1445 | return civicrm_api3_create_error('Employer name and Employer id Mismatch'); | |
1446 | } | |
1447 | ||
1448 | // show error if multiple organisation with same name exist | |
a7488080 | 1449 | if (empty($params['employer_id']) && (count($dupeIds) > 1) |
6a488035 TO |
1450 | ) { |
1451 | return civicrm_api3_create_error('Found more than one Organisation with same Name.'); | |
1452 | } | |
1453 | } | |
1454 | ||
1455 | return NULL; | |
1456 | } | |
1457 | ||
109ff2ac DL |
1458 | /** |
1459 | * | |
f4aaa82a | 1460 | * @param $result |
100fef9d | 1461 | * @param int $activityTypeID |
f4aaa82a | 1462 | * |
f4aaa82a | 1463 | * @return array <type> $params |
109ff2ac DL |
1464 | */ |
1465 | function _civicrm_api3_deprecated_activity_buildmailparams($result, $activityTypeID) { | |
1466 | // get ready for collecting data about activity to be created | |
1467 | $params = array(); | |
1468 | ||
1469 | $params['activity_type_id'] = $activityTypeID; | |
1470 | ||
1471 | $params['status_id'] = 2; | |
1472 | $params['source_contact_id'] = $params['assignee_contact_id'] = $result['from']['id']; | |
1473 | $params['target_contact_id'] = array(); | |
1474 | $keys = array('to', 'cc', 'bcc'); | |
1475 | foreach ($keys as $key) { | |
1476 | if (is_array($result[$key])) { | |
1477 | foreach ($result[$key] as $key => $keyValue) { | |
1478 | if (!empty($keyValue['id'])) { | |
1479 | $params['target_contact_id'][] = $keyValue['id']; | |
1480 | } | |
1481 | } | |
1482 | } | |
1483 | } | |
1484 | $params['subject'] = $result['subject']; | |
1485 | $params['activity_date_time'] = $result['date']; | |
1486 | $params['details'] = $result['body']; | |
1487 | ||
1488 | for ($i = 1; $i <= 5; $i++) { | |
1489 | if (isset($result["attachFile_$i"])) { | |
1490 | $params["attachFile_$i"] = $result["attachFile_$i"]; | |
1491 | } | |
1492 | } | |
1493 | ||
1494 | return $params; | |
1495 | } |