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