Ajax call to approve a calendar event

Permalink
I am trying to add a new menu item in the calendar where admins can easily approve a calendar event. I copied the \concrete\single_pages\dashboard\calendar\events.php file into \application\single_pages\dashboard\calendar\events.php and I made the following addition to the code:

foreach ($results as $occurrence) {
  $menu = new \Concrete\Core\Calendar\Event\Menu\EventOccurrenceMenu($occurrence);
  $menuHelper = Core::make('helper/concrete/ui/menu');
  $newMenuItem = new Concrete\Core\Application\UserInterface\ContextMenu\Item\LinkItem('javascript:;', t('Approve Event'), ['onclick'=>'approveEvent('.$occurrence->getID().');']);
  $menu->addItem($newMenuItem);
...


For the life of me I can't figure out how to do a successful ajax call to approve the event. Initially I couldn't figure out how to create the link item as a clickable element that didn't actually go anywhere, so that's where I ended up adding the 'onclick' attribute. Then I think I've narrowed it down to calling /ccm/calendar/event/version/approve but I started running into issues with the ccm_token and doing a regular jQuery ajax call. Is this the wrong approach? Should I be doing something different here?

Thanks in advance!

derekairdrie
 
derekairdrie replied on at Permalink Reply
derekairdrie
I ended up getting it working with the code below, but this still feels like the wrong way to do this:

foreach ($results as $occurrence) {
                            $menu = new \Concrete\Core\Calendar\Event\Menu\EventOccurrenceMenu($occurrence);
                            $event = $occurrence->getEvent();
                            // Add the approve link if the event is pending approval
                            if ($event->isPending() && $permissions->canApproveCalendarEvent()){
                                $menuHelper = Core::make('helper/concrete/ui/menu');
                                $newMenuItem = new Concrete\Core\Application\UserInterface\ContextMenu\Item\LinkItem('javascript:;', t('Approve Event'), ['onclick'=>"approveEvent(".$occurrence->getVersion()->getID().",'" .\Core::make("token")->generate('calendar/event/version/approve/'. $occurrence->getVersion()->getID()) ."');"]);
                                $menu->addItem($newMenuItem);
                            }
...


and then the JS function:

function approveEvent(id,ccm_token){
        var formData = new FormData();
        formData.set('eventVersionID', id);
        formData.set('ccm_token', ccm_token);
        $.ajax({
            data: formData,
            type: 'post',
            url: "<?= \URL::to('ccm/calendar/event/version/approve') ?>",
            success: function (r) {
                window.location.href = window.location.href;
            },
            processData: false,
            contentType: false
        });
    }