Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions _build/data/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
]
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* @package commerce_cursus
* @subpackage plugin
*/

namespace modmore\Commerce_Cursus\Plugins\DemoEvents;

use CursusEventParticipants;
use CursusEvents;
use CursusParticipants;
use TreehillStudio\Agenda\Agenda;
use TreehillStudio\Cursus\Cursus;
use TreehillStudio\Cursus\Helper\Mail;
use modmore\Commerce_Cursus\Plugins\Plugin;
use xPDO;

class OnCursusEventParticipantBooked 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;

/** @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');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* @package ferienprogramm
* @subpackage plugin
*/

namespace modmore\Commerce_Cursus\Plugins\DemoEvents;

use comOrder;
use CursusEvents;
use modMail;
use TreehillStudio\Agenda\Agenda;
use TreehillStudio\Cursus\Cursus;
use TreehillStudio\Cursus\Helper\Parse;
use modmore\Commerce_Cursus\Plugins\Plugin;
use xPDO;

class OnCursusEventParticipantRestored extends Plugin
{
/**
* A reference to the Cursus instance
* @var Cursus $cursus
*/
private $cursus;

/**
* {@inheritDoc}
* @return void
*/
public function init()
{
$corePath = $this->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();
}
}
2 changes: 1 addition & 1 deletion core/components/commerce_cursus/src/Plugins/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace modmore\Commerce_Cursus\Plugins;

use modmore\Commerce_Cursus\Commerce_Cursus;
use modX;
use Commerce_Cursus;

/**
* Class Plugin
Expand Down