PHP code example of markocupic / resource-booking-bundle
1. Go to this page and download the library: Download markocupic/resource-booking-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
markocupic / resource-booking-bundle example snippets
declare(strict_types=1);
namespace App\EventSubscriber;
use Contao\Date;
use Markocupic\ResourceBookingBundle\Event\PostBookingEvent;
use Markocupic\ResourceBookingBundle\Event\PreBookingEvent;
use Markocupic\ResourceBookingBundle\Exception\StopBookingProcessException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class BookingEventSubscriber implements EventSubscriberInterface
{
const priority = 10000;
public static function getSubscribedEvents(): array
{
return [
PreBookingEvent::NAME => ['onPreBooking', self::PRIORITY],
PostBookingEvent::NAME => ['onPostBooking', self::PRIORITY],
];
}
public function onPreBooking(PostBookingEvent $objPostBookingEvent): void
{
// E.g. do some checks
// Abort the booking process and throw a StopBookingProcessException.
throw new StopBookingProcessException('You are not old enough to book the room.');
}
public function onPostBooking(PostBookingEvent $objPostBookingEvent): void
{
// For demo usage only
$objBookingCollection = $objPostBookingEvent->getBookingCollection();
$objUser = $objPostBookingEvent->getUser();
while ($objBookingCollection->next()) {
if (null !== $objUser) {
// Send notifications, manipulate database
// or do some other insane stuff
$strMessage = sprintf(
'Dear %s %s'."\n".'You have successfully booked %s on %s from %s to %s.',
$objUser->firstname,
$objUser->lastname,
$objBookingCollection->getRelated('pid')->title,
Date::parse('d.m.Y', $objBookingCollection->startTime),
Date::parse('H:i', $objBookingCollection->startTime),
Date::parse('H:i', $objBookingCollection->endTime)
);
mail(
$objUser->email,
utf8_decode((string) $objBookingCollection->title),
utf8_decode((string) $strMessage)
);
}
}
// ...
// or revert the booking and throw a StopBookingProcessException.
throw new StopBookingProcessException('You are not old enough to book the room.');
}
}
declare(strict_types=1);
namespace App\EventSubscriber;
use Markocupic\ResourceBookingBundle\Event\AjaxRequestEvent;
use Markocupic\ResourceBookingBundle\Exception\StopBookingProcessException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
final class AjaxRequestEventSubscriber implements EventSubscriberInterface
{
const priority = 1010;
public function __construct(private readonly RequestStack $requestStack)
{
}
public static function getSubscribedEvents(): array
{
return [
AjaxRequestEvent::NAME => ['onXmlHttpRequest', self::PRIORITY],
];
}
public function onXmlHttpRequest(AjaxRequestEvent $ajaxRequestEvent): void
{
$request = $this->requestStack->getCurrentRequest();
if ($request->isXmlHttpRequest()) {
$action = $request->request->get('action', null);
if (null !== $action) {
if (\is_callable([self::class, 'on'.ucfirst($action)])) {
$this->{'on'.ucfirst($action)}($ajaxRequestEvent);
}
}
}
}
/**
* @throws \Exception
*/
protected function onFetchDataRequest(AjaxRequestEvent $ajaxRequestEvent): void
{
// Stop propagation and do not run original event handler
// Works only if the priority is > 10
$ajaxRequestEvent->stopPropagation();
// Get response object
$ajaxResponse = $ajaxRequestEvent->getAjaxResponse();
// Add some custom data to the response object
$ajaxResponse->setData('foo', 'bar');
$ajaxResponse->setStatus(AjaxResponse::STATUS_SUCCESS);
}
protected function onMyCustomRequest(AjaxRequestEvent $ajaxRequestEvent): void
{
// Respond to custom ajax requests
}
protected function onApplyFilterRequest(AjaxRequestEvent $ajaxRequestEvent): void
{
// Do some stuff here
}
protected function onJumpWeekRequest(AjaxRequestEvent $ajaxRequestEvent): void
{
// Do some stuff here
}
protected function onBookingRequest(AjaxRequestEvent $ajaxRequestEvent): void
{
// Do some stuff here
}
protected function onBookingFormValidationRequest(AjaxRequestEvent $ajaxRequestEvent): void
{
// Do some stuff here
}
protected function onCancelBookingRequest(AjaxRequestEvent $ajaxRequestEvent): void
{
// Do some stuff here
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.