Skip to content

System Events

The following system events exists in Cursus:

'OnAgendaBeforeSave',
'OnAgendaSave',
'OnAgendaBeforeRemove'
'OnAgendaValidate'

The OnAgendaBeforeSave, OnAgendaSave and OnAgendaBeforeRemove system events were introduced in Agenda 1.5.0.

The OnAgendaValidate system event was introduced in Agenda 1.6.3.

The use of these events allows additional code to be executed before and after saving and before deleting Agenda database entries.

The following Agenda database objects invoke the system events: AgendaCalendars, AgendaCategories, AgendaEventCursus, AgendaEventDates, AgendaEventImages, AgendaEvents, AgendaEventVideos and AgendaLocations.

The following properties can be used in the plugin events:

Property Content
mode The state of the Agenda object. It can contain the following states: modSystemEvent::MODE_NEW or modSystemEvent::MODE_UPD. Not available in OnAgendaBeforeRemove
className The class name of the saved object.
id The identifier of the saved object.
object The Agenda database object.

The following properties are only available in OnAgendaBeforeSave and OnAgendaSave:

Property Content
dirty True if the object was modified before saving.

Skeleton classes for the events can be found in core/components/agenda/src/Plugins/Events. To use them, you have to remove the underscore prefix from the filename and activate the according event in the Agenda plugin.

Examples

The following two code examples use the AgendaBeforeRemove event to prevent removing Agenda events and repeating events, when they were already booked in Cursus. Thanks to Jens Wittmann for the idea of the plugin.

The first one contains the code of a plugin, that has to be created in the manager with an activated AgendaBeforeRemove system event.

AgendaCursusPreventRemove.plugin.php
<?php
/**
 * Prevent booked Cursus dates from being accidentally deleted in Agenda
 *
 * @var modX $modx
 * @var array $scriptProperties
 */

use TreehillStudio\Agenda\Agenda;

$eventName = $modx->event->name;

switch ($eventName) {
    case 'OnAgendaBeforeRemove':
        $corePath = $modx->getOption('agenda.core_path', null, $modx->getOption('core_path') . 'components/agenda/');
        $agenda = $modx->getService('agenda', 'Agenda', $corePath . 'model/agenda/', [
            'core_path' => $corePath
        ]);

        $eventId = $scriptProperties['id'];
        $className = $scriptProperties['className'];

        if (!$modx->loadClass('agenda.AgendaEvents', $agenda->getOption('modelPath'))) {
            $modx->log(xPDO::LOG_LEVEL_ERROR, 'Could not load AgendaEvents class!', '', 'AgendaCursusPreventRemove');
            return;
        }
        if (!$modx->loadClass('agenda.AgendaEventDates', $agenda->getOption('modelPath'))) {
            $modx->log(xPDO::LOG_LEVEL_ERROR, 'Could not load AgendaEventDates class!', '', 'AgendaCursusPreventRemove');
            return;
        }

        if ($eventId) {
            if ($className == 'AgendaEvents') {
                $c = $modx->newQuery('CursusEventParticipants');
                $c->leftJoin('AgendaEventDates', 'AgendaEventDates', 'CursusEventParticipants.event_id = AgendaEventDates.id');
                $c->where(['AgendaEventDates.event_id' => $eventId]);
                $count = $this->modx->getCount('CursusEventParticipants', $c);
                if ($count > 0) {
                    $modx->event->_output = 'There are already repeating events booked for this event. Therefore, it is not possible to delete the event.';
                }
            } elseif ($className == 'AgendaEventDates') {
                $participants = $modx->getObject('CursusEventParticipants', ['event_id' => $eventId]);
                if ($participants) {
                    $modx->event->_output = 'The event repeat has already been booked. Therefore, it is not possible to delete it.';
                }
            }
        }
        break;
}
return;

The second one contains the code of a plugin class, that has to be uploaded into the core/components/agenda/src/Plugins/Events folder. After that you have to activate the AgendaBeforeRemove system event in the Agenda Plugin.

OnAgendaBeforeRemove.php
<?php
/**
 * Prevent booked Cursus dates from being accidentally deleted in Agenda
 *
 * @package agenda
 * @subpackage plugin
 */

namespace TreehillStudio\Ferienprogramm\Plugins\Events;

use TreehillStudio\Agenda\Agenda;
use TreehillStudio\Agenda\Plugins\Plugin;
use TreehillStudio\Cursus\Cursus;
use xPDO;

class OnAgendaBeforeRemove extends Plugin
{
    /** @var Agenda $agenda */
    public $agenda;
    /** @var Cursus $cursus */
    public $cursus;

    public function process()
    {
        $corePath = $this->modx->getOption('agenda.core_path', null, $this->modx->getOption('core_path') . 'components/agenda/');
        $this->agenda = $this->modx->getService('agenda', 'Agenda', $corePath . 'model/agenda/', [
            'core_path' => $corePath
        ]);
        $this->cursus = &$this->agenda->cursus;

        $eventId = $this->scriptProperties['id'];
        $className = $this->scriptProperties['className'];

        if (!$this->modx->loadClass('agenda.AgendaEvents', $this->agenda->getOption('modelPath'))) {
            $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'Could not load AgendaEvents class!', '', 'OnAgendaBeforeRemove');
            return;
        }
        if (!$this->modx->loadClass('agenda.AgendaEventDates', $this->agenda->getOption('modelPath'))) {
            $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'Could not load AgendaEventDates class!', '', 'OnAgendaBeforeRemove');
            return;
        }

        if ($eventId) {
            if ($className == 'AgendaEvents') {
                $c = $this->modx->newQuery('CursusEventParticipants');
                $c->leftJoin('AgendaEventDates', 'AgendaEventDates', 'CursusEventParticipants.event_id = AgendaEventDates.id');
                $c->where(['AgendaEventDates.event_id' => $eventId]);
                $count = $this->modx->getCount('CursusEventParticipants', $c);
                if ($count > 0) {
                    $this->modx->event->_output = 'There are already repeating events booked for this event. Therefore, it is not possible to delete the event.';
                }
            }
            if ($className == 'AgendaEventDates') {
                $participants = $this->modx->getObject('CursusEventParticipants', ['event_id' => $eventId]);
                if ($participants) {
                    $this->modx->event->_output = 'The event repeat has already been booked. Therefore, it is not possible to delete it.';
                }
            }
        }
    }
}