onAfterSync

This handler fires immediately after an item is created or updated that originates from a synced source. Or, if the original event has not been changed, the handler is called with mode=unchanged. (At present, $type may only be ‘events’.) An example module is below.

Parameter Description
$type currently only works for $type = “events”
$subscription_id id of the linked calendar
$event_id id of the event just created/updated
$mode “create”, “update”, or “unchanged”
$item array of the original feed item values
<?php
     $_LW->REGISTERED_APPS['my_app']=[
       'title'=>'My App',
       'handlers'=>['onAfterSync'],
    ];

    class LiveWhaleApplicationMyApp {

        // perform additional form validations
        public function onAfterSync($type, $subscription_id, $event_id, $mode, $item) { 
            global $_LW;
                
            if ($type == 'events') {

                // use the $subscription_id to only apply this to a certain linked calendar
                if ($subscription_id === 17) {
                          
                    // check $item/$mode and take any desired actions

                }
            }
        }
    }
?>

Certain fields—like summary, event types, tags, and custom fields—are only synced when a Linked Calendar event is first loaded into LiveWhale. This is to prevent editor actions from being unintentionally overwritten on a future sync.

However, you can use onAfterSync to enforce certain values from the ICAL always get saved every hourly sync. Here’s an example of that approach:

public function onAfterSync($type, $subscription_id, $event_id, $mode, $item) { 
    global $_LW;

    if (strpos(@$_LW->linked_calendar_url, '://www.my-student-events.com/')!==false) { // if this is a Student Events feed
        if ($mode !== 'create') { // for updated or unchanged items
            $data_to_save=[
                'summary'=>(!empty($item['summary']) ? $item['summary'] : ''), // overwrite the summary field
                'categories'=>(!empty($item['categories']) ? $item['categories'] : ''), // overwrite the event types
                'associated_data'=>(!empty($item['tags']) ? ['tags'=>$item['tags']] : []), // overwrite the tags
                'custom_my_custom_field'=>(!empty($item['custom_my_custom_field']) ? $item['custom_my_custom_field'] : ''), // overwrite a custom field
            ];
            $_LW->update($type, $event_id, $data_to_save);
        };
    };

}