CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Can I Set/Update custom fields with civicrm_event_create ?
Pages: [1]

Author Topic: Can I Set/Update custom fields with civicrm_event_create ?  (Read 1069 times)

najoory

  • Guest
Can I Set/Update custom fields with civicrm_event_create ?
March 04, 2010, 06:07:43 am
Hi all
I need to create event with custom fields. Api's function civicrm_event_create creates event with empty custom fields. I've tried such array of $params:
Code: [Select]
Array
(
    [title] => My title
    [summary] => My summary
    [event_type_id] => 2
    [participant_listing_id] => 2
    [is_public] => 1
    [is_online_registration] => 0
    [max_participants] => 100
    [event_full_text] => My description
    [is_monetary] => 0
    [contribution_type_id] => 0
    [is_map] => 0
    [is_active] => 1
    [is_show_location] => 1
    [default_role_id] => 1
    [is_email_confirm] => 0
    [is_pay_later] => 0
    [is_multiple_registrations] => 0
    [allow_same_participant_emails] => 0
    [has_waitlist] => 0
    [template_title] => My title
    [created_id] => 1
    [custom_13_-1] => medium
    [custom_8_-1] => SS
    [custom_11_-1] => Tuesday
    [custom_9_-1] => 16:00
    [custom_10_-1] => 60
    [event_start_date] => 20100309160000
    [start_date] => 20100309160000
    [event_end_date] => 20100309165959
    [end_date] => 20100309165959
)
And like this:
Code: [Select]
Array
(
    [title] => My title
    [summary] => My summary
    [event_type_id] => 2
    [participant_listing_id] => 2
    [is_public] => 1
    [is_online_registration] => 0
    [max_participants] => 100
    [event_full_text] => Full text
    [is_monetary] => 0
    [contribution_type_id] => 0
    [is_map] => 0
    [is_active] => 1
    [is_show_location] => 1
    [default_role_id] => 1
    [is_email_confirm] => 0
    [is_pay_later] => 0
    [is_multiple_registrations] => 0
    [allow_same_participant_emails] => 0
    [has_waitlist] => 0
    [template_title] => My title
    [created_id] => 1
    [custom_13] => medium
    [custom_8] => SS
    [custom_11] => Tuesday
    [custom_9] => 16:00
    [custom_10] => 60
    [event_start_date] => 20100309160000
    [start_date] => 20100309160000
    [event_end_date] => 20100309165959
    [end_date] => 20100309165959
)
I've tried to debug it and I think issue is in input format of CRM_Event_BAO_Event::create()

Code: [Select]
     
if ( CRM_Utils_Array::value( 'custom', $params ) &&
             is_array( $params['custom'] ) ) {
            require_once 'CRM/Core/BAO/CustomValueTable.php';
            CRM_Core_BAO_CustomValueTable::store( $params['custom'], 'civicrm_event', $event->id );
}

How I can format my array to use API?
« Last Edit: March 04, 2010, 06:09:45 am by najoory »

najoory

  • Guest
Re: Can I Set/Update custom fields with civicrm_event_create ?
March 04, 2010, 06:26:35 am
I can answer this question myself now. There is lines from CRM/Event/Form/ManageEvent/EventInfo.php
Code: [Select]
       
$customFields = CRM_Core_BAO_CustomField::getFields( 'Event', false, false,
                                                             CRM_Utils_Array::value( 'event_type_id', $params ) );
        $params['custom'] = CRM_Core_BAO_CustomField::postProcess( $params,
                                                                   $customFields,
                                                                   $this->_id,
                                                                   'Event' );

Maybe it will be usefull to put this lines into your API's civicrm_event_create before create call?
« Last Edit: March 04, 2010, 06:35:45 am by najoory »

najoory

  • Guest
Re: Can I Set/Update custom fields with civicrm_event_create ?
March 04, 2010, 06:44:41 am
Yeah, it works! My hacked function can take custom fields in usual format:
Code: [Select]

/**
 * Create a Event
 *
 * This API is used for creating a Event
 *
 * @param   array  $params  an associative array of title/value property values of civicrm_event
 *
 * @return array of newly created event property values.
 * @access public
 */
function civicrm_event_create( &$params )
{
    _civicrm_initialize();
    if ( ! is_array($params) ) {
        return civicrm_create_error('Params is not an array');
    }

    if (! isset( $params['title'] ) || ! isset( $params['event_type_id'] ) || ! isset( $params['start_date'] ) ) {
        return civicrm_create_error('Missing require fields ( title, event type id,start date)');
    }

    $error = _civicrm_check_required_fields( $params, 'CRM_Event_DAO_Event' );
    if ($error['is_error']) {
        return civicrm_create_error( $error['error_message'] );
    }

    // Do we really want $params[id], even if we have
    // $params[event_id]? if yes then please uncomment the below line

    //$ids['event'      ] = $params['id'];

    $ids['eventTypeId'] = $params['event_type_id'];
    $ids['startDate'  ] = $params['start_date'];
    $ids['event_id']    = CRM_Utils_Array::value( 'event_id', $params );

    /*hack*/
    $customFields = CRM_Core_BAO_CustomField::getFields( 'Event', false, false, CRM_Utils_Array::value( 'event_type_id', $params ) );
    $params['custom'] = CRM_Core_BAO_CustomField::postProcess( $params,
                                                                   $customFields,
                                                                   '',
                                                                   'Event' );
/*/hack*/
    require_once 'CRM/Event/BAO/Event.php';
    $eventBAO = CRM_Event_BAO_Event::create($params, $ids);

    if ( is_a( $eventBAO, 'CRM_Core_Error' ) ) {
        return civicrm_create_error( "Event is not created" );
    } else {
        $event = array();
        _civicrm_object_to_array($eventBAO, $event);
        $values = array( );
        $values['event_id'] = $event['id'];
        $values['is_error']   = 0;
    }

    return $values;
}

Also it will be usefull to process dates input formats, e.g.
Code: [Select]
$params['start_date'] = date('YmdHis', strtotime($params['start_date']));

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Can I Set/Update custom fields with civicrm_event_create ?

This forum was archived on 2017-11-26.