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', ], ] ]; 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..9530eac --- /dev/null +++ b/core/components/commerce_cursus/src/Plugins/DemoEvents/OnCursusEventParticipantBooked.php @@ -0,0 +1,72 @@ +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, [ + '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, '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/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(); + } +} 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