Function Below shows Following When Participating for an event Using custom data :
1) Get the custom data contact .
2) If not Get then Create new contact .
3) Create New Participant for Event .
4) Check for Organization & create if organization not exist .
5) Check for Relationship & create if previous not available .
function eventType_customPath_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {
if ( $objectName == 'Participant' && $op == 'create' ) {
require_once 'api/api.php';
$config =& CRM_Core_Config::singleton( );
$participantParams = $config->_customParams;
$participantFirstNameId = array( 'custom_22', 'custom_60', 'custom_62' ); /* add custom first name Id */
$participantLastNameId = array( 'custom_59', 'custom_61', 'custom_63' ); /* add custom last name Id */
$participantEmailId = array( 'custom_64', 'custom_65', 'custom_66' ); /* add custom email Id */
$orgFieldName = array( 'custom_73', 'custom_74', 'custom_75' ); /* add custom Organization name Id */
$participant = array( );
$date = date("Ymd His");
$eventId = $objectRef->event_id;
$participantStatusId = $objectRef->status_id;
$primaryContactId = $objectRef->contact_id;
$participantRoleId = $objectRef->role_id;
foreach( $participantFirstNameId as $participantKey => $participantValue ) {
if( !empty( $participantParams[0][ $participantValue ] ) && !empty( $participantParams[0][ $participantLastNameId[$participantKey] ] ) ) {
$searchContact = array ( 'first_name' => $participantParams[0][ $participantValue ],
'last_name' => $participantParams[0][ $participantLastNameId[$participantKey] ],
'email' => $participantParams[0][ $participantEmailId[$participantKey] ],
'contact_type' => 'Individual',
'check_permissions' => false,
'version' => 3 );
$contact = civicrm_api( 'Contact', 'Get', $searchContact );
if( $contact['count'] ) {
foreach( $contact[ 'values' ] as $contactKey => $contactValue ){
$contactId[]['contact_id_a'] = $contactValue['contact_id'];
break;
}
}else {
$createContact = array ( 'first_name' => $participantParams[0][ $participantValue ],
'last_name' => $participantParams[0][ $participantLastNameId[$participantKey] ],
'email' => $participantParams[0][ $participantEmailId[$participantKey] ],
'contact_type' => 'Individual',
'check_permissions' => false,
'version' =>3 );
$contact = civicrm_api( 'Contact', 'Create', $createContact );
$contactId[]['contact_id_a'] = $contact['id'];
}
}
if( $contactId[ $participantKey ][ 'contact_id_a' ] ) {
$createParticipant = array(
'contact_id' => $contactId [ $participantKey ][ 'contact_id_a' ],
'event_id' => $eventId,
'status_id' => $participantStatusId,
'role_id' => $participantRoleId,
'registered_by_id' => $objectRef->id,
'register_date' => $date,
'check_permissions' => false,
'version' => 3
);
$secondaryParticipant = gstAddParticipant( $createParticipant );
}
}
$participantIndex = 0;
foreach( $orgFieldName as $orgKey => $orgValue ) {
$params = array( 'version' => 3,
'check_permissions' => false,
'contact_type' => 'Organization',
'organization_name' => $participantParams[0][ $orgValue ] );
$result = civicrm_api( 'contact', 'get', $params );
if( !array_key_exists( 'id', $result ) ){
foreach( $result['values'] as $resultKey => $resultValue ){
$result['id'] = $resultValue['contact_id'];
break;
}
}
if( !$result['count'] ){
$individualParams = array( 'version' => 3,
'check_permissions' => false,
'contact_type' => 'Organization',
'organization_name' => $participantParams[0][ $orgValue ] );
$result = civicrm_api( 'contact', 'create', $params );
}
if($contactId[ $participantIndex ]['contact_id_a']){
$contactId[ $participantIndex ]['contact_id_b'] = $result['id'];
//CRM_Core_Error::debug( '$contactId', $contactId );
}
$participantIndex++;
}
foreach ( $contactId as $key => $value ) {
$params = array(
'contact_id' => $value['contact_id_a'],
'relationship_type_id' => 4,
'is_active' => 1,
'version' => 3,
'check_permissions' => false,
);
$result = civicrm_api( 'relationship','get',$params );
$flag = 0;
if( !empty( $result['values'] ) ) {
foreach( $result['values'] as $resultKey => $resultValues ) {
if( $resultValues['contact_id_b'] != $value['contact_id_b'] ){
$flag = 1;
} else {
$flag = 0;
break;
}
}
if( $flag ){
$result['is_error'] = 0;
}
} else {
$result['is_error'] = 0;
}
$relationDate = date("Ymd");
if ( !$result['is_error'] ) {
$param = array(
'contact_id_a' => $value['contact_id_a'],
'contact_id_b' => $value['contact_id_b'],
'relationship_type_id' => '4',
'start_date' => $relationDate,
'check_permissions' => false,
'end_date' => null,
'is_active' => '1',
'version' => '3',
);
$relationship = civicrm_api( 'relationship','create', $param );
}
}
}
}
function gstAddParticipant( $params ) {
if ( CRM_Utils_Array::value( 'register_date', $params ) ) {
$params['register_date'] = CRM_Utils_Date::isoToMysql($params['register_date']);
}
if ( CRM_Utils_Array::value( 'participant_fee_amount', $params ) ) {
$params['participant_fee_amount'] = CRM_Utils_Rule::cleanMoney( $params['participant_fee_amount'] );
}
if ( CRM_Utils_Array::value( 'participant_fee_amount', $params ) ) {
$params['fee_amount'] = CRM_Utils_Rule::cleanMoney( $params['fee_amount'] );
}
$participantBAO = new CRM_Event_BAO_Participant;
if (CRM_Utils_Array::value('id', $params)) {
$participantBAO->id = CRM_Utils_Array::value('id', $params);
$participantBAO->find(true);
$participantBAO->register_date = CRM_Utils_Date::isoToMysql($participantBAO->register_date);
}
$participantBAO->copyValues($params);
//CRM-6910
//1. If currency present, it should be valid one.
//2. We should have currency when amount is not null.
require_once 'CRM/Utils/Rule.php';
$currency = $participantBAO->fee_currency;
if ( $currency ||
!CRM_Utils_System::isNull( $participantBAO->fee_amount ) ) {
if ( !CRM_Utils_Rule::currencyCode( $currency ) ) {
$config = CRM_Core_Config::singleton();
$currency = $config->defaultCurrency;
}
}
$participantBAO->fee_currency = $currency;
$participantBAO->save();
$participantBAO->free();
}
1) Get the custom data contact .
2) If not Get then Create new contact .
3) Create New Participant for Event .
4) Check for Organization & create if organization not exist .
5) Check for Relationship & create if previous not available .
function eventType_customPath_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {
if ( $objectName == 'Participant' && $op == 'create' ) {
require_once 'api/api.php';
$config =& CRM_Core_Config::singleton( );
$participantParams = $config->_customParams;
$participantFirstNameId = array( 'custom_22', 'custom_60', 'custom_62' ); /* add custom first name Id */
$participantLastNameId = array( 'custom_59', 'custom_61', 'custom_63' ); /* add custom last name Id */
$participantEmailId = array( 'custom_64', 'custom_65', 'custom_66' ); /* add custom email Id */
$orgFieldName = array( 'custom_73', 'custom_74', 'custom_75' ); /* add custom Organization name Id */
$participant = array( );
$date = date("Ymd His");
$eventId = $objectRef->event_id;
$participantStatusId = $objectRef->status_id;
$primaryContactId = $objectRef->contact_id;
$participantRoleId = $objectRef->role_id;
foreach( $participantFirstNameId as $participantKey => $participantValue ) {
if( !empty( $participantParams[0][ $participantValue ] ) && !empty( $participantParams[0][ $participantLastNameId[$participantKey] ] ) ) {
$searchContact = array ( 'first_name' => $participantParams[0][ $participantValue ],
'last_name' => $participantParams[0][ $participantLastNameId[$participantKey] ],
'email' => $participantParams[0][ $participantEmailId[$participantKey] ],
'contact_type' => 'Individual',
'check_permissions' => false,
'version' => 3 );
$contact = civicrm_api( 'Contact', 'Get', $searchContact );
if( $contact['count'] ) {
foreach( $contact[ 'values' ] as $contactKey => $contactValue ){
$contactId[]['contact_id_a'] = $contactValue['contact_id'];
break;
}
}else {
$createContact = array ( 'first_name' => $participantParams[0][ $participantValue ],
'last_name' => $participantParams[0][ $participantLastNameId[$participantKey] ],
'email' => $participantParams[0][ $participantEmailId[$participantKey] ],
'contact_type' => 'Individual',
'check_permissions' => false,
'version' =>3 );
$contact = civicrm_api( 'Contact', 'Create', $createContact );
$contactId[]['contact_id_a'] = $contact['id'];
}
}
if( $contactId[ $participantKey ][ 'contact_id_a' ] ) {
$createParticipant = array(
'contact_id' => $contactId [ $participantKey ][ 'contact_id_a' ],
'event_id' => $eventId,
'status_id' => $participantStatusId,
'role_id' => $participantRoleId,
'registered_by_id' => $objectRef->id,
'register_date' => $date,
'check_permissions' => false,
'version' => 3
);
$secondaryParticipant = gstAddParticipant( $createParticipant );
}
}
$participantIndex = 0;
foreach( $orgFieldName as $orgKey => $orgValue ) {
$params = array( 'version' => 3,
'check_permissions' => false,
'contact_type' => 'Organization',
'organization_name' => $participantParams[0][ $orgValue ] );
$result = civicrm_api( 'contact', 'get', $params );
if( !array_key_exists( 'id', $result ) ){
foreach( $result['values'] as $resultKey => $resultValue ){
$result['id'] = $resultValue['contact_id'];
break;
}
}
if( !$result['count'] ){
$individualParams = array( 'version' => 3,
'check_permissions' => false,
'contact_type' => 'Organization',
'organization_name' => $participantParams[0][ $orgValue ] );
$result = civicrm_api( 'contact', 'create', $params );
}
if($contactId[ $participantIndex ]['contact_id_a']){
$contactId[ $participantIndex ]['contact_id_b'] = $result['id'];
//CRM_Core_Error::debug( '$contactId', $contactId );
}
$participantIndex++;
}
foreach ( $contactId as $key => $value ) {
$params = array(
'contact_id' => $value['contact_id_a'],
'relationship_type_id' => 4,
'is_active' => 1,
'version' => 3,
'check_permissions' => false,
);
$result = civicrm_api( 'relationship','get',$params );
$flag = 0;
if( !empty( $result['values'] ) ) {
foreach( $result['values'] as $resultKey => $resultValues ) {
if( $resultValues['contact_id_b'] != $value['contact_id_b'] ){
$flag = 1;
} else {
$flag = 0;
break;
}
}
if( $flag ){
$result['is_error'] = 0;
}
} else {
$result['is_error'] = 0;
}
$relationDate = date("Ymd");
if ( !$result['is_error'] ) {
$param = array(
'contact_id_a' => $value['contact_id_a'],
'contact_id_b' => $value['contact_id_b'],
'relationship_type_id' => '4',
'start_date' => $relationDate,
'check_permissions' => false,
'end_date' => null,
'is_active' => '1',
'version' => '3',
);
$relationship = civicrm_api( 'relationship','create', $param );
}
}
}
}
function gstAddParticipant( $params ) {
if ( CRM_Utils_Array::value( 'register_date', $params ) ) {
$params['register_date'] = CRM_Utils_Date::isoToMysql($params['register_date']);
}
if ( CRM_Utils_Array::value( 'participant_fee_amount', $params ) ) {
$params['participant_fee_amount'] = CRM_Utils_Rule::cleanMoney( $params['participant_fee_amount'] );
}
if ( CRM_Utils_Array::value( 'participant_fee_amount', $params ) ) {
$params['fee_amount'] = CRM_Utils_Rule::cleanMoney( $params['fee_amount'] );
}
$participantBAO = new CRM_Event_BAO_Participant;
if (CRM_Utils_Array::value('id', $params)) {
$participantBAO->id = CRM_Utils_Array::value('id', $params);
$participantBAO->find(true);
$participantBAO->register_date = CRM_Utils_Date::isoToMysql($participantBAO->register_date);
}
$participantBAO->copyValues($params);
//CRM-6910
//1. If currency present, it should be valid one.
//2. We should have currency when amount is not null.
require_once 'CRM/Utils/Rule.php';
$currency = $participantBAO->fee_currency;
if ( $currency ||
!CRM_Utils_System::isNull( $participantBAO->fee_amount ) ) {
if ( !CRM_Utils_Rule::currencyCode( $currency ) ) {
$config = CRM_Core_Config::singleton();
$currency = $config->defaultCurrency;
}
}
$participantBAO->fee_currency = $currency;
$participantBAO->save();
$participantBAO->free();
}
Comments
Post a Comment