From b99c2d2b4d1e7fac5514a8e2abacb526d88794b8 Mon Sep 17 00:00:00 2001 From: Thomas Jakobi Date: Sun, 10 Aug 2025 18:26:11 +0200 Subject: [PATCH 1/3] Add Demo OnCursusEventParticipantBooked and OnCursusEventParticipantRestored plugin event code --- .../OnCursusEventParticipantBooked.php | 70 ++++++++++++ .../OnCursusEventParticipantRestored.php | 100 ++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 core/components/commerce_cursus/src/Plugins/DemoEvents/OnCursusEventParticipantBooked.php create mode 100644 core/components/commerce_cursus/src/Plugins/DemoEvents/OnCursusEventParticipantRestored.php diff --git a/core/components/commerce_cursus/src/Plugins/DemoEvents/OnCursusEventParticipantBooked.php b/core/components/commerce_cursus/src/Plugins/DemoEvents/OnCursusEventParticipantBooked.php new file mode 100644 index 0000000..639dbd9 --- /dev/null +++ b/core/components/commerce_cursus/src/Plugins/DemoEvents/OnCursusEventParticipantBooked.php @@ -0,0 +1,70 @@ +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; + + /** @var CursusEventParticipants $eventParticipant */ + $eventParticipant = $this->scriptProperties['event_participant']; + + // Send a mail to the eventparticipant + if ($eventParticipant) { + /** @var CursusParticipants $participant */ + $participant = $eventParticipant->getOne('Participant'); + if (!$participant) { + $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'Participant not found!', '', 'OnCursusEventParticipantBooked'); + return; + } + + if (!$this->modx->loadClass('cursus.CursusEvents', $this->cursus->getOption('modelPath'))) { + $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'Could not load CursusEvents class!', '', 'OnCursusEventParticipantBooked'); + return; + } + $eventClass = new CursusEvents($this->modx); + $c = $this->modx->newQuery('CursusEvents'); + $c = $eventClass->eventsQuery($c, [ + 'agenda_event_id' => $eventParticipant->get('event_id'), + 'all' => true, + ]); + /** @var CursusEvents $event */ + $event = $this->modx->getObject('CursusEvents', $c); + + if ($event) { + $mail = new Mail($this->modx); + $mail->sendParticipantMail($event, $participant, [], [ + 'userSubject' => 'tplCommerceCursusBookedMailUserSubject', + 'userText' => 'tplCommerceCursusBookedMailText', + 'sendParticipantMail' => true, + ]); + } + } else { + $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'EventParticipant not found!', '', 'OnCursusEventParticipantBooked'); + } + } +} diff --git a/core/components/commerce_cursus/src/Plugins/DemoEvents/OnCursusEventParticipantRestored.php b/core/components/commerce_cursus/src/Plugins/DemoEvents/OnCursusEventParticipantRestored.php new file mode 100644 index 0000000..5b7926a --- /dev/null +++ b/core/components/commerce_cursus/src/Plugins/DemoEvents/OnCursusEventParticipantRestored.php @@ -0,0 +1,100 @@ +modx->getOption('agenda.core_path', null, $this->modx->getOption('core_path') . 'components/agenda/'); + /** @var Agenda $agenda */ + $agenda = $this->modx->getService('agenda', 'Agenda', $corePath . 'model/agenda/', [ + 'core_path' => $corePath + ]); + /** @var Cursus $cursus */ + $this->cursus = &$agenda->cursus; + } + + /** + * {@inheritDoc} + * @return void + */ + public function process() + { + /** @var comOrder $order */ + $order = $this->scriptProperties['order']; + + $items = $order->getItems(); + foreach ($items as $item) { + $event = $this->getEvent([ + 'event_id' => $item->get('product'), + ]); + if ($event['remaining_booked'] < 0) { + $eventParticipants = $item->getProperty('cursus_event_participants') ? explode(',', $item->getProperty('cursus_event_participants')) : []; + $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'Expired event participants ' . implode(', ', $eventParticipants) . ' were set to booked during the payment and the event is currently overbooked.'); + + $participantNames = $item->getProperty('participant_names') ? explode(',', $item->getProperty('participant_names')) : []; + $this->sendMail('Event', 'The reservation of the participants ' . implode(', ', $participantNames) . ' expired before the payment and the event ' . $event['event_id'] . ' is currently overbooked.'); + } + } + } + + /** + * @param $eventsOptions + * @return array|null + */ + private function getEvent($eventsOptions) + { + $eventClass = new CursusEvents($this->modx); + $event = $eventClass->getEvent($eventsOptions); + if ($event) { + $eventArray = $event->toExtendedArray(); + return Parse::flattenArray($eventArray, '_'); + } + return null; + } + + /** + * @param $email + * @param $subject + * @param $message + * @return void + */ + private function sendMail($subject, $message) + { + $this->modx->getService('mail', 'mail.modPHPMailer'); + $this->modx->mail->set(modMail::MAIL_BODY, $message); + $this->modx->mail->set(modMail::MAIL_FROM, $this->cursus->getOption('email_from')); + $this->modx->mail->set(modMail::MAIL_FROM_NAME, $this->cursus->getOption('email_from_name')); + $this->modx->mail->set(modMail::MAIL_SUBJECT, $subject); + $this->modx->mail->address('to', $this->cursus->getOption('email_to')); + $this->modx->mail->setHTML(true); + if (!$this->modx->mail->send()) { + $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'An error occurred while trying to send the email: ' . $this->modx->mail->mailer->ErrorInfo); + } + $this->modx->mail->reset(); + } +} From 84e61e3b50787428da7ea38d01b3c5eee9c655a6 Mon Sep 17 00:00:00 2001 From: Thomas Jakobi Date: Mon, 11 Aug 2025 09:45:40 +0200 Subject: [PATCH 2/3] Activate the OnCursusEventParticipantBooked and OnCursusEventParticipantRestored events for the demo plugin --- _build/data/plugins.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/_build/data/plugins.php b/_build/data/plugins.php index 5984e33..6d4ce7b 100644 --- a/_build/data/plugins.php +++ b/_build/data/plugins.php @@ -2,12 +2,14 @@ return [ 'CommerceCursusDemo' => [ - 'description' => 'Cursus for Commerce Demo Plugin that runs on the OnAgendaBeforeRemove and the OnAgendaSave event to add and remove Commerce products when an Agenda event is created or deleted.', + 'description' => 'Cursus for Commerce that adds and removes Commerce products when an Agenda event is created or deleted. It also sends a mail, when the Cursus event participant is booked or restored from expired.', 'file' => 'commercecursusdemo.plugin.php', 'disabled' => true, 'events' => [ 'OnAgendaBeforeRemove', - 'OnAgendaSave' + 'OnAgendaSave', + 'OnCursusEventParticipantBooked', + 'OnCursusEventParticipantRestored', ], ] ]; From 5eb4962cab16979440c95fb3a5058a3442804c03 Mon Sep 17 00:00:00 2001 From: Thomas Jakobi Date: Wed, 13 Aug 2025 08:59:39 +0200 Subject: [PATCH 3/3] Fix the event query --- .../Plugins/DemoEvents/OnCursusEventParticipantBooked.php | 6 ++++-- core/components/commerce_cursus/src/Plugins/Plugin.php | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/core/components/commerce_cursus/src/Plugins/DemoEvents/OnCursusEventParticipantBooked.php b/core/components/commerce_cursus/src/Plugins/DemoEvents/OnCursusEventParticipantBooked.php index 639dbd9..9530eac 100644 --- a/core/components/commerce_cursus/src/Plugins/DemoEvents/OnCursusEventParticipantBooked.php +++ b/core/components/commerce_cursus/src/Plugins/DemoEvents/OnCursusEventParticipantBooked.php @@ -37,7 +37,7 @@ public function process() if ($eventParticipant) { /** @var CursusParticipants $participant */ $participant = $eventParticipant->getOne('Participant'); - if (!$participant) { + if (!$participant) { $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'Participant not found!', '', 'OnCursusEventParticipantBooked'); return; } @@ -49,7 +49,7 @@ public function process() $eventClass = new CursusEvents($this->modx); $c = $this->modx->newQuery('CursusEvents'); $c = $eventClass->eventsQuery($c, [ - 'agenda_event_id' => $eventParticipant->get('event_id'), + 'event_id' => $eventParticipant->get('event_id'), 'all' => true, ]); /** @var CursusEvents $event */ @@ -62,6 +62,8 @@ public function process() 'userText' => 'tplCommerceCursusBookedMailText', 'sendParticipantMail' => true, ]); + } else { + $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'Event not found!', '', 'OnCursusEventParticipantBooked'); } } else { $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'EventParticipant not found!', '', 'OnCursusEventParticipantBooked'); diff --git a/core/components/commerce_cursus/src/Plugins/Plugin.php b/core/components/commerce_cursus/src/Plugins/Plugin.php index c598e92..1fb94db 100644 --- a/core/components/commerce_cursus/src/Plugins/Plugin.php +++ b/core/components/commerce_cursus/src/Plugins/Plugin.php @@ -8,8 +8,8 @@ namespace modmore\Commerce_Cursus\Plugins; +use modmore\Commerce_Cursus\Commerce_Cursus; use modX; -use Commerce_Cursus; /** * Class Plugin