Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
function create_parent_and_contribution() {
$api_params = array(
'sequential' => 1,
'contact_type' => 'Individual',
'first_name' => CRM_Utils_Type::validate($_POST['first_name'],'String'),
'last_name' => CRM_Utils_Type::validate($_POST['last_name'],'String'),
'email' => CRM_Utils_Type::validate($_POST['email'],'String'),
'api.Phone.create' => array(
'sequential' => 1,
'contact_id' => '$value.id',
'phone' => CRM_Utils_Type::validate($_POST['phone'],'String'),
),
'api.Address.create' => array(
'sequential' => 1,
'contact_id' => '$value.id',
'location_type_id' => "Home",
'street_address' => CRM_Utils_Type::validate($_POST['street_address'],'String'),
'city' => CRM_Utils_Type::validate($_POST['city'],'String'),
'state_province_id' => CRM_Utils_Type::validate($_POST['state_province_id'],'String'),
'postal_code' => CRM_Utils_Type::validate($_POST['postal_code'],'String'),
),
'api.Contribution.create' => array(
'sequential' => 1,
'contact_id' => '$value.id',
'receive_date' => date('m/d/Y g:i a'),
'total_amount' => '0',
'financial_type_id' => 'Invoice',
'invoice_id' => uniqid(),
'contribution_status_id' => 'Pending',
),
);
try {
$api_result = civicrm_api3('Contact', 'create', $api_params);
return $api_result;
}
catch (CiviCRM_API3_Exception $e) {
// handle error here
$errorMessage = $e->getMessage();
$errorCode = $e->getErrorCode();
$errorData = $e->getExtraParams();
return array(
'error' => $errorMessage,
'error_code' => $errorCode,
'error_data' => $errorData,
);
}
}
function create_and_register_students($parent_result) {
$student_result = [];
foreach ($_POST['student_details'] AS $student_record){
$student_details = array(
'sequential' => 1,
'debug' => 1,
'contact_type' => 'Individual',
'contact_sub_type' => 'Student',
'first_name' => CRM_Utils_Type::validate($student_record['student_first_name'],'String'),
'last_name' => CRM_Utils_Type::validate($student_record['student_last_name'],'String'),
'gender_id' => CRM_Utils_Type::validate($student_record['gender_id'],'String'),
'birth_date' => CRM_Utils_Type::validate(date("Ymd",strtotime($student_record['birth_date'])),'Date'), //CRM_Utils_Rule::mysqlDate requires this date format
// 'custom_1' => CRM_Utils_Type::validate($student_record['custom_1'],'String'), //Latest Grade Completed
// 'custom_2' => CRM_Utils_Type::validate($student_record['custom_2'],'String'), //T-Shirt Size
// 'custom_3' => CRM_Utils_Type::validate($student_record['custom_3'],'String'), //Allergies
// 'custom_14' => CRM_Utils_Type::validate($student_record['custom_14'],'String'), //Seating/Grouping Requests
'api.Relationship.create' => array (
'sequential' => 1,
'contact_id_a' => '$value.id',
'contact_id_b' => $parent_result['id'],
'relationship_type_id' => 1,
'is_permission_a_b' => 0,
'is_permission_b_a' => 1,
),
);
$student_event_list = [];
foreach($student_record['event_id'] AS $student_event) {
$student_event_list[] = array(
'sequential' => 1,
'event_id' => CRM_Utils_Type::validate($student_event,'Positive'),
'contact_id' => '$value.id',
'status_id' => "Partially paid",
'role_id' => "Attendee",
'register_date' => date("m/d/Y g:i a"),
'fee_level' => "Single",
'api.ParticipantPayment.create' => array (
'sequential' => 1,
'participant_id' => '$value.id',
'contribution_id' => $parent_result['values'][0]['api.Contribution.create']['id'],
)
)
;
}
$student_details['api.Participant.create'] = $student_event_list;
try {
$student_result[] = civicrm_api3('Contact', 'create', $student_details);
}
catch (CiviCRM_API3_Exception $e) {
$errorMessage = $e->getMessage();
$errorCode = $e->getErrorCode();
$errorData = $e->getExtraParams();
return array(
'error' => $errorMessage,
'error_code' => $errorCode,
'error_data' => $errorData,
);
}
}
return $student_result;
}
function civi_custom_form_submit() {
try {
$parent_result = create_parent_and_contribution();
if (!$parent_result['error']) {
$student_result = create_and_register_students($parent_result);
if (!$student_result['error']) {
// header("Location:page-camp_registration_confirm.php");
// exit;
return print_r($student_result);
}
else
{
return print_r($student_result);
}
}
else
{
return print_r($parent_result);
}
}
catch (CiviCRM_API3_Exception $e) {
$errorMessage = $e->getMessage();
$errorCode = $e->getErrorCode();
return $errorCode . ' : ' . $errorMessage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment