Skip to main content

For Custom Data In Event Participant

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();
}

Comments

Popular posts from this blog

Create Hooks in Civicrm in Joomla 1.5

  Create Hooks in Civicrm in Joomla 1.5 Create folder in any directory like /Joomla/media/civicrm_hook . Create file civicrmHooks.php in this directory. Go to Administer CiviCRM > Global Settings > Directories Set the path for custom php here. Use the civicrm hooks with Joomla_civicrm_hookName format. Ex. function Joomla_civicrm_buildForm( ){ // Write your code here. }

New Services to the world

Different government services & Online Services GST Registration Company Registration ITR PAN Card Aadhar Card E-way Bill Registration Food License Accounting Service Website designing Website development Website Hosting Website Domain Website Testing (Manual) Website SCO Optimization Lead Generation Add Creation and many more...

Views3 Integration ( Add Db_prefix in settings.php for drupal 7 )

These instructions assume CiviCRM and Drupal are on separate databases located on the same physical host , and that the same database user has access to both the CiviCRM and Drupal databases. Before making any changes, it is  strongly  advised that you make backups of your mysql databases and settings.php file. Make sure you are logged into your Drupal site as the administrator. Visit  http://yourdomain.com/civicrm/admin/setting/uf?reset=1  Replace with the domain of your Drupal site/installation. You should see a page with output that begins with: "$databases['default']['default']['prefix']= array(" Copy everything inside this box, including the trailing ");" Open /sites/default/settings.php for editing from the root directory of the site, typically "public_html" on most servers. ( NOTE: This assumes you have a single site installation. The location of the settings file might be different depending on whether or ...