tkrebs / ep3-bs

Online booking system for courts
http://bs.hbsys.de/
MIT License
180 stars 108 forks source link

Datepicker and initial date format problem #165

Open pongraczi opened 6 years ago

pongraczi commented 6 years ago

As I investigated the issue #138 I found the problem is a little bit complex.
Take a look here: kijeloles_756

My idea, after playing with modifying the phtml and controller, to be able to determine the used format and using the convertFromFormat method to convert the input value into a valid date.

pongraczi commented 6 years ago

Any progress regarding the date format anomaly? Thnx

tkrebs commented 6 years ago

I am currently somewhat busy, but I think I will invest some time today to fix this.

pongraczi commented 6 years ago

Thank you.

I can see that, there are some inconsistency how the date displayed/represented and this one could cause headache, at least for me :)

1st, Date label will contain a date in IntlDateFormatter::MEDIUM format. If the user will not change a datepicker, this format will be passed to the bookingcontroller.php (or other processors).
2nd, If the user click on the date and got the datepicker, after selecting a date the format will be the format the user set in jquery-ui/i18n/....js and this format will be inserted into the label and passed to the controller.

The 1st and 2nd case will be different, but the controller has no idea, which one passed to the controller. We neither. Force using IntlDateFormatter::MEDIUM or format from jquery-ui is not a real solution, until we can guarantee, all the date labels and datepickers using exactly the same format, or determine the format passed to the controller.

This is not a solution, but I try to figure out, what happens.

pongraczi commented 6 years ago

Hi Tobias, did you have some time to find a workaround for this issue? Thank you!

tkrebs commented 6 years ago

I have created a new development branch dev/improved-date-i18n where I have switched from the IntlDateFormatter to a more predictable date formatting according to the translations for the datepickers.

tkrebs commented 6 years ago

It doesn't yet solve all date related issues but it's a start.

pongraczi commented 6 years ago

Ah, ok, thanks, I missed the new branch, sorry.

wes45 commented 3 years ago

Hi there, This problem still exists for me, even after implementing the new branch. If the admin uses nl language, and tries to add a singletime booking in the calendar, the invalid date error is shown. I found the lines below which probably cause the error, but my coding skills are too limited to convert these lines to working ones. Could anyone please help converting these lines to the Y-m-d format? Thanks in advance and best regards, Wes

/httpdocs/module/Backend/src/Backend/Controller/BookingController.php

change line 184 from: 'bf-date-start' => $this->dateFormat($booking->getMeta('date_start', $reservation->get('date')), \IntlDateFormatter::MEDIUM), into:

change line 185 from: 'bf-date-end' => $this->dateFormat($booking->getMeta('date_end', $reservation->get('date')), \IntlDateFormatter::MEDIUM), into:

change line 192 from: 'bf-date-start' => $this->dateFormat($reservation->get('date'), \IntlDateFormatter::MEDIUM), into:

change line 193 from: 'bf-date-end' => $this->dateFormat($reservation->get('date'), \IntlDateFormatter::MEDIUM), into:

change line 205 from: 'bf-date-start' => $this->dateFormat($params['dateTimeStart'], \IntlDateFormatter::MEDIUM), into:

change line 206 from: 'bf-date-end' => $this->dateFormat($params['dateTimeEnd'], \IntlDateFormatter::MEDIUM), into:

change line 357 from: 'bf-date-start' => $this->dateFormat($booking->needMeta('date_start'), \IntlDateFormatter::MEDIUM), into:

change line 358 from: 'bf-date-end' => $this->dateFormat($booking->needMeta('date_end'), \IntlDateFormatter::MEDIUM), into:

fstaccone commented 2 years ago

Hi @wes45 , did you solve your issue in the end?

wes45 commented 2 years ago

Hi, here is the contents of the BookingController.php that worked for me. I am not using the script anymore, but it worked A-OK in the end.

`<?php

namespace Backend\Controller;

use Booking\Entity\Booking; use Booking\Table\BookingTable; use Booking\Table\ReservationTable; use Zend\Db\Adapter\Adapter; use Zend\Mvc\Controller\AbstractActionController;

class BookingController extends AbstractActionController {

public function indexAction()
{
    $this->authorize('admin.booking');

    $serviceManager = @$this->getServiceLocator();
    $bookingManager = $serviceManager->get('Booking\Manager\BookingManager');
    $reservationManager = $serviceManager->get('Booking\Manager\ReservationManager');
    $userManager = $serviceManager->get('User\Manager\UserManager');

    $bookings = array();
    $reservations = array();

    $dateStart = $this->params()->fromQuery('date-start');
    $dateEnd = $this->params()->fromQuery('date-end');
    $search = $this->params()->fromQuery('search');

    if ($dateStart) {
        $dateStart = new \DateTime($dateStart);
    }

    if ($dateEnd) {
        $dateEnd = new \DateTime($dateEnd);
    }

    if (($dateStart && $dateEnd) || $search) {
        $filters = $this->backendBookingDetermineFilters($search);

        try {
            $limit = 1000;

            if ($dateStart && $dateEnd) {
                $reservations = $reservationManager->getInRange($dateStart, $dateEnd, $limit);
                $bookings = $bookingManager->getByReservations($reservations, $filters['filters']);
            } else {
                $bookings = $bookingManager->getBy($filters['filters'], null, $limit);
            }

            $bookings = $this->complexFilterBookings($bookings, $filters);
            $reservations = $reservationManager->getByBookings($bookings);

            $userManager->getByBookings($bookings);
        } catch (\RuntimeException $e) {
            $bookings = array();
            $reservations = array();
        }
    }

    return array(
        'bookings' => $bookings,
        'reservations' => $reservations,
        'dateStart' => $dateStart,
        'dateEnd' => $dateEnd,
        'search' => $search,
    );
}

protected function complexFilterBookings($bookings, $filters)
{
    $serviceManager = @$this->getServiceLocator();

    foreach ($filters['filterParts'] as $filterPart) {

        // Filter for billing total
        if ($filterPart[0] == str_replace(' ', '_', strtolower($this->t('Billing total')))) {
            $bookingBillManager = $serviceManager->get('Booking\Manager\Booking\BillManager');
            $bookingBillManager->getByBookings($bookings);

            $bookings = array_filter($bookings, function(Booking $booking) use ($filterPart) {
                switch ($filterPart[1]) {
                    case '=':
                        return $booking->getExtra('bills_total') == (int) $filterPart[2];
                    case '>':
                        return $booking->getExtra('bills_total') > (int) $filterPart[2];
                    case '<':
                        return $booking->getExtra('bills_total') < (int) $filterPart[2];
                    default:
                        return false;
                }
            });
        }
    }

    return $bookings;
}

public function editAction()
{
    $sessionUser = $this->authorize('admin.booking, calendar.see-data');

    $params = $this->backendBookingDetermineParams(true);

    $reservation = $booking = null;

    $editMode = null;      

    if (! ($this->getRequest()->isPost() || $this->params()->fromQuery('force') == 'new')) {
        switch (count($params['reservations'])) {
            case 0:
                break;
            case 1:
                $reservation = current($params['reservations']);
                $booking = $reservation->getExtra('booking');

                if ($booking->get('status') == 'subscription' && $sessionUser->can(['calendar.create-subscription-bookings', 'calendar.cancel-subscription-bookings', 'calendar.delete-subscription-bookings'])) {
                    if (! $params['editMode']) {
                        return $this->forward()->dispatch('Backend\Controller\Booking', ['action' => 'editMode', 'params' => $params]);
                    }
                }
                break;
            default:
                return $this->forward()->dispatch('Backend\Controller\Booking', ['action' => 'editChoice', 'params' => $params]);
        }
    }

    $serviceManager = @$this->getServiceLocator();
    $formElementManager = $serviceManager->get('FormElementManager');

    $editForm = $formElementManager->get('Backend\Form\Booking\EditForm');

    if ($this->getRequest()->isPost()) {
        $editForm->setData($this->params()->fromPost());

        if ($editForm->isValid()) {
            $d = $editForm->getData();

            /* Process form (note, that reservation and booking are not available here) */

            if ($d['bf-rid']) {

                /* Update booking/reservation */

                $savedBooking = $this->backendBookingUpdate($d['bf-rid'], $d['bf-user'], $d['bf-time-start'], $d['bf-time-end'], $d['bf-date-start'],
                    $d['bf-sid'], $d['bf-status-billing'], $d['bf-quantity'], $d['bf-notes'], $params['editMode']);

            } else {

                /* Create booking/reservation */

                $savedBooking = $this->backendBookingCreate($d['bf-user'], $d['bf-time-start'], $d['bf-time-end'], $d['bf-date-start'], $d['bf-date-end'],
                    $d['bf-repeat'], $d['bf-sid'], $d['bf-status-billing'], $d['bf-quantity'], $d['bf-notes'], $sessionUser->get('alias'));
            }

            $this->flashMessenger()->addSuccessMessage('Booking has been saved');

            if ($this->params()->fromPost('bf-edit-user')) {
                return $this->redirect()->toRoute('backend/user/edit', ['uid' => $savedBooking->get('uid')]);
            } else if ($this->params()->fromPost('bf-edit-bills')) {
                return $this->redirect()->toRoute('backend/booking/bills', ['bid' => $savedBooking->get('bid')]);
            } else {
                return $this->redirect()->toRoute('frontend');
            }
        }
    } else {
        if ($booking) {
            $user = $booking->needExtra('user');

            $editForm->setData(array(
                'bf-rid' => $reservation->get('rid'),
                'bf-user' => $user->need('alias') . ' (' . $user->need('uid') . ')',
                'bf-sid' => $booking->get('sid'),
                'bf-status-billing' => $booking->get('status_billing'),
                'bf-quantity' => $booking->get('quantity'),
                'bf-notes' => $booking->getMeta('notes'),
            ));

            if ($booking->get('status') == 'subscription' && $params['editMode'] == 'booking') {
                $editForm->setData(array(
                    'bf-time-start' => substr($booking->getMeta('time_start', $reservation->get('time_start')), 0, 5),
                    'bf-time-end' => substr($booking->getMeta('time_end', $reservation->get('time_end')), 0, 5),
                    'bf-date-start' => $this->dateFormat($booking->getMeta('date_start', $reservation->get('date'))),
                    'bf-date-end' => $this->dateFormat($booking->getMeta('date_end', $reservation->get('date'))),
                    'bf-repeat' => $booking->getMeta('repeat'),
                ));
            } else {
                $editForm->setData(array(
                    'bf-time-start' => substr($reservation->get('time_start'), 0, 5),
                    'bf-time-end' => substr($reservation->get('time_end'), 0, 5),
                    'bf-date-start' => $reservation->get('date', $reservation->get('date')),
                    'bf-date-end' => $reservation->get('date', $reservation->get('date')),
                ));
            }
        } else {
            $timeEnd = $params['dateTimeEnd']->format('H:i');

            if ($timeEnd == '00:00') {
                $timeEnd = '24:00';
            }

            $editForm->setData(array(
                'bf-sid' => $params['square']->get('sid'),
                'bf-date-start' => $params['dateTimeStart']->format($this->t('Y-m-d')),
                'bf-date-end' => $params['dateTimeEnd']->format($this->t('Y-m-d')),
                'bf-time-start' => $params['dateTimeStart']->format('H:i'),
                'bf-time-end' => $timeEnd,
            ));
        }
    }

    if ($booking && $booking->getMeta('player-names')) {
        $editForm->get('bf-quantity')->setLabel(sprintf('%s (<a href="%s">%s</a>)',
            $this->translate('Number of players'),
            $this->url()->fromRoute('backend/booking/players', ['bid' => $booking->need('bid')]),
            $this->translate('Who?')));
        $editForm->get('bf-quantity')->setLabelOption('disable_html_escape', true);

        $playerNameNotes = '';
        $playerNames = $booking->getMeta('player-names');

        if ($playerNames) {
            $playerNamesUnserialized = @unserialize($booking->getMeta('player-names'));

            if ($playerNamesUnserialized && is_array($playerNamesUnserialized)) {
                foreach ($playerNamesUnserialized as $i => $playerName) {
                    $playerNameNotes .= sprintf('<div>%s. %s</div>',
                        $i + 1, $playerName['value']);
                }
            }
        }

        $editForm->get('bf-quantity')->setOption('notes', $playerNameNotes);
    }

    if (! $sessionUser->can(['calendar.create-subscription-bookings'])) {
        return $this->ajaxViewModel(array_merge($params, array(
        'editMode' => 'no_subscr',
        'editForm' => $editForm,
        'booking' => $booking,
        'reservation' => $reservation,
        'sessionUser' => $sessionUser,
        )));
    }

    return $this->ajaxViewModel(array_merge($params, array(
        'editForm' => $editForm,
        'booking' => $booking,
        'reservation' => $reservation,
        'sessionUser' => $sessionUser,
    )));
}

public function editChoiceAction()
{
    $params = $this->getEvent()->getRouteMatch()->getParam('params');

    return $this->ajaxViewModel($params);
}

public function editModeAction()
{
    $params = $this->getEvent()->getRouteMatch()->getParam('params');

    return $this->ajaxViewModel($params);
}

public function editRangeAction()
{
    $this->authorize('admin.booking, calendar.create-subscription-bookings + calendar.cancel-subscription-bookings');

    $serviceManager = @$this->getServiceLocator();
    $bookingManager = $serviceManager->get('Booking\Manager\BookingManager');
    $reservationManager = $serviceManager->get('Booking\Manager\ReservationManager');
    $formElementManager = $serviceManager->get('FormElementManager');

    $bid = $this->params()->fromRoute('bid');

    $booking = $bookingManager->get($bid);

    if ($booking->get('status') != 'subscription') {
        throw new \RuntimeException('Time and date range can only be edited on subscription bookings');
    }

    $editTimeRangeForm = $formElementManager->get('Backend\Form\Booking\Range\EditTimeRangeForm');
    $editDateRangeForm = $formElementManager->get('Backend\Form\Booking\Range\EditDateRangeForm');

    if ($this->getRequest()->isPost()) {
        $db = $serviceManager->get('Zend\Db\Adapter\Adapter');

        $mode = $this->params()->fromQuery('mode');

        if ($mode == 'time') {
            $editTimeRangeForm->setData($this->params()->fromPost());

            if ($editTimeRangeForm->isValid()) {
                $data = $editTimeRangeForm->getData();

                $res = $db->query(
                    sprintf('UPDATE %s SET time_start = "%s", time_end = "%s" WHERE bid = %s AND time_start = "%s" AND time_end = "%s"',
                        ReservationTable::NAME,
                        $data['bf-time-start'], $data['bf-time-end'], $bid, $booking->needMeta('time_start'), $booking->needMeta('time_end')),
                    Adapter::QUERY_MODE_EXECUTE);

                if ($res->getAffectedRows() > 0) {
                    $booking->setMeta('time_start', $data['bf-time-start']);
                    $booking->setMeta('time_end', $data['bf-time-end']);

                    $bookingManager->save($booking);
                }

                $this->flashMessenger()->addSuccessMessage('Booking has been saved');

                return $this->redirect()->toRoute('frontend');
            }
        } else if ($mode == 'date') {
            $editDateRangeForm->setData($this->params()->fromPost());

            if ($editDateRangeForm->isValid()) {
                $data = $editDateRangeForm->getData();

                $dateStart = new \DateTime($data['bf-date-start']);
                $dateEnd = new \DateTime($data['bf-date-end']);
                $repeat = $data['bf-repeat'];

                $res = $db->query(
                    sprintf('DELETE FROM %s WHERE bid = %s',
                        ReservationTable::NAME, $bid),
                    Adapter::QUERY_MODE_EXECUTE);

                if ($res->getAffectedRows() > 0) {
                    $reservationManager->createByRange($booking, $dateStart, $dateEnd,
                        $booking->needMeta('time_start'), $booking->needMeta('time_end'), $repeat);

                    $booking->setMeta('date_start', $dateStart->format('Y-m-d'));
                    $booking->setMeta('date_end', $dateEnd->format('Y-m-d'));
                    $booking->setMeta('repeat', $repeat);

                    $bookingManager->save($booking);
                }

                $this->flashMessenger()->addSuccessMessage('Booking has been saved');

                return $this->redirect()->toRoute('frontend');
            }
        } else {
            throw new \RuntimeException('Invalid edit mode received');
        }
    } else {
        $editTimeRangeForm->setData(array(
            'bf-time-start' => substr($booking->needMeta('time_start'), 0, 5),
            'bf-time-end' => substr($booking->needMeta('time_end'), 0, 5),
        ));

        $editDateRangeForm->setData(array(
            'bf-date-start' => $booking->needMeta('date_start')->format($this->t('Y-m-d')),
            'bf-date-end' => $booking->needMeta('date_end')->format($this->t('Y-m-d')),
            'bf-repeat' => $booking->needMeta('repeat'),
        ));
    }

    return $this->ajaxViewModel(array(
        'booking' => $booking,
        'editTimeRangeForm' => $editTimeRangeForm,
        'editDateRangeForm' => $editDateRangeForm,
    ));
}

public function deleteAction()
{
    $sessionUser = $this->authorize([
        'calendar.cancel-single-bookings', 'calendar.delete-single-bookings',
        'calendar.cancel-subscription-bookings', 'calendar.delete-subscription-bookings']);

    $serviceManager = @$this->getServiceLocator();
    $bookingManager = $serviceManager->get('Booking\Manager\BookingManager');
    $reservationManager = $serviceManager->get('Booking\Manager\ReservationManager');

    $rid = $this->params()->fromRoute('rid');
    $editMode = $this->params()->fromQuery('edit-mode');

    $reservation = $reservationManager->get($rid);
    $booking = $bookingManager->get($reservation->get('bid'));

    switch ($booking->get('status')) {
        case 'single':
            $this->authorize(['calendar.cancel-single-bookings', 'calendar.delete-single-bookings']);
            break;
        case 'subscription':
            $this->authorize(['calendar.cancel-subscription-bookings', 'calendar.delete-subscription-bookings']);
            break;
    }

    if ($this->params()->fromQuery('confirmed') == 'true') {

        if ($editMode == 'reservation') {
            $this->authorize(['calendar.delete-single-bookings', 'calendar.delete-subscription-bookings']);

            $reservationManager->delete($reservation);

            $this->flashMessenger()->addSuccessMessage('Reservation has been deleted');
        } else {

            if ($this->params()->fromQuery('cancel') == 'true') {
                $this->authorize(['calendar.cancel-single-bookings', 'calendar.cancel-subscription-bookings']);

                $booking->set('status', 'cancelled');
                $booking->setMeta('cancellor', $sessionUser->get('alias'));
                $booking->setMeta('cancelled', date('Y-m-d H:i:s'));
                $bookingManager->save($booking);

                $this->flashMessenger()->addSuccessMessage('Booking has been cancelled');
            } else {
                $this->authorize(['calendar.delete-single-bookings', 'calendar.delete-subscription-bookings']);

                $bookingManager->delete($booking);

                $this->flashMessenger()->addSuccessMessage('Booking has been deleted');
            }
        }

        return $this->redirect()->toRoute('frontend');
    }

    if ($editMode == 'reservation') {
        $template = 'backend/booking/delete.reservation.phtml';
    } else {
        $template = null;
    }

    return $this->ajaxViewModel(array(
        'rid' => $rid,
    ), null, $template);
}

public function statsAction()
{
    $this->authorize('admin.booking');

    $db = @$this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');

    $stats = $db->query(sprintf('SELECT status, COUNT(status) AS count FROM %s GROUP BY status', BookingTable::NAME),
        Adapter::QUERY_MODE_EXECUTE)->toArray();

    return array(
        'stats' => $stats,
    );
}

public function billsAction()
{
    $this->authorize('admin.booking');

    $bid = $this->params()->fromRoute('bid');

    $serviceManager = @$this->getServiceLocator();

    $bookingManager = $serviceManager->get('Booking\Manager\BookingManager');
    $bookingBillManager = $serviceManager->get('Booking\Manager\Booking\BillManager');
    $bookingStatusService = $serviceManager->get('Booking\Service\BookingStatusService');
    $userManager = $serviceManager->get('User\Manager\UserManager');

    $booking = $bookingManager->get($bid);
    $bills = $bookingBillManager->getBy(array('bid' => $bid), 'bbid ASC');
    $user = $userManager->get($booking->need('uid'));

    if ($this->getRequest()->isGet()) {
        $create = $this->params()->fromQuery('create');

        if ($create == 'default-bill') {
            $reservationManager = $serviceManager->get('Booking\Manager\ReservationManager');
            $squareManager = $serviceManager->get('Square\Manager\SquareManager');
            $squarePricingManager = $serviceManager->get('Square\Manager\SquarePricingManager');

            $square = $squareManager->get($booking->get('sid'));
            $squareType = $this->option('subject.square.type');
            $squareName = $this->t($square->need('name'));

            $dateRangeHelper = $serviceManager->get('ViewHelperManager')->get('DateRange');

            $created = false;

            foreach ($reservationManager->getBy(['bid' => $bid]) as $reservation) {

                $dateTimeStart = new \DateTime($reservation->get('date') . ' ' . $reservation->get('time_start'));
                $dateTimeEnd = new \DateTime($reservation->get('date') . ' ' . $reservation->get('time_end'));

                $pricing = $squarePricingManager->getFinalPricingInRange($dateTimeStart, $dateTimeEnd, $square, $booking->get('quantity'));

                if ($pricing) {

                    $description = sprintf('%s %s, %s',
                        $squareType, $squareName,
                        $dateRangeHelper($dateTimeStart, $dateTimeEnd));

                    $bookingBillManager->save(new Booking\Bill(array(
                        'bid' => $bid,
                        'description' => $description,
                        'quantity' => $booking->get('quantity'),
                        'time' => $pricing['seconds'],
                        'price' => $pricing['price'],
                        'rate' => $pricing['rate'],
                        'gross' => $pricing['gross'],
                    )));

                    $created = true;
                }
            }

            if ($created) {
                $this->flashMessenger()->addSuccessMessage('Booking-Bill position has been created');
            } else {
                $this->flashMessenger()->addErrorMessage('No Booking-Bill position has been created');
            }

            return $this->redirect()->toRoute('backend/booking/bills', ['bid' => $bid]);
        }

        $delete = $this->params()->fromQuery('delete');

        if ($delete && is_numeric($delete) && isset($bills[$delete])) {
            $bookingBillManager->delete($delete);

            $this->flashMessenger()->addSuccessMessage('Booking-Bill position has been deleted');
            return $this->redirect()->toRoute('backend/booking/bills', ['bid' => $bid]);
        }
    }

    if ($this->getRequest()->isPost()) {

        /* Check and save billing status */

        $billingStatus = $this->params()->fromPost('ebf-status');

        if ($bookingStatusService->checkStatus($billingStatus)) {
            $booking->set('status_billing', $billingStatus);
            $bookingManager->save($booking);
        } else {
            $this->flashMessenger()->addErrorMessage('Invalid billing status selected');
        }

        /* Check and save known (and new) bills */

        $bills[] = new Booking\Bill(['bid' => $bid]);

        foreach ($bills as $bill) {

            $bbid = $bill->get('bbid', 'new');

            $description = $this->params()->fromPost('ebf-' . $bbid . '-description');
            $description = trim(strip_tags($description));

            if ($description) {
                $bill->set('description', $description);
            }

            $time = $this->params()->fromPost('ebf-' . $bbid . '-time');

            if ($time && is_numeric($time)) {
                $bill->set('time', $time * 60);
            }

            $quantity = $this->params()->fromPost('ebf-' . $bbid . '-quantity');

            if ($quantity && is_numeric($quantity)) {
                $bill->set('quantity', $quantity);
            }

            $price = $this->params()->fromPost('ebf-' . $bbid . '-price');

            if ($price && is_numeric($price)) {
                $bill->set('price', $price);
            }

            $vatGross = $this->params()->fromPost('ebf-' . $bbid . '-vat-gross');
            $vatRate = $this->params()->fromPost('ebf-' . $bbid . '-vat-rate');

            if (is_numeric($vatGross) && is_numeric($vatRate)) {
                $bill->set('gross', $vatGross);
                $bill->set('rate', $vatRate);
            }

            if ($description && $price && is_numeric($vatRate) && is_numeric($vatGross)) {
                $bookingBillManager->save($bill);
            }
        }

        $save = $this->params()->fromPost('ebf-save');
        $saveAndBack = $this->params()->fromPost('ebf-save-and-back');

        $this->flashMessenger()->addSuccessMessage('Booking-Bill has been saved');

        if ($save) {
            return $this->redirect()->toRoute('backend/booking/bills', ['bid' => $bid]);
        } else if ($saveAndBack) {
            return $this->redirect()->toRoute('user/bookings/bills', ['bid' => $bid]);
        }
    }

    return array(
        'booking' => $booking,
        'bookingStatusService' => $bookingStatusService,
        'bills' => $bills,
        'user' => $user,
    );
}

public function playersAction()
{
    $this->authorize('admin.booking, calendar.see-data');

    $bid = $this->params()->fromRoute('bid');

    $serviceManager = @$this->getServiceLocator();
    $bookingManager = $serviceManager->get('Booking\Manager\BookingManager');
    $userManager = $serviceManager->get('User\Manager\UserManager');

    $booking = $bookingManager->get($bid);
    $user = $userManager->get($booking->need('uid'));

    $playerNames = $booking->getMeta('player-names');

    if (! $playerNames) {
        throw new \RuntimeException('This booking has no additional player names');
    }

    $playerNames = @unserialize($playerNames);

    if (! $playerNames) {
        throw new \RuntimeException('Invalid player names data stored in database');
    }

    $players = array();

    foreach ($playerNames as $playerData) {
        $nameData = explode('-', $playerData['name']);
        $playerNumber = $nameData[count($nameData) - 1];

        if (! isset($players[$playerNumber])) {
            $players[$playerNumber] = array();
        }

        $playerDataKey = $nameData[count($nameData) - 2];
        $playerDataValue = $playerData['value'];

        if ($playerDataKey == 'email') {
            $respectiveUser = $userManager->getBy(['email' => $playerDataValue]);

            if ($respectiveUser) {
                $players[$playerNumber]['user'] = current($respectiveUser);
                $players[$playerNumber]['userMatch'] = $playerDataKey;
            }
        }

        if ($playerDataKey == 'phone') {
            $respectiveUser = $userManager->getByPhoneNumber($playerDataValue);

            if ($respectiveUser) {
                $players[$playerNumber]['user'] = $respectiveUser;
                $players[$playerNumber]['userMatch'] = $playerDataKey;
            }
        }

        $players[$playerNumber][$playerDataKey] = $playerDataValue;
    }

    return array(
        'booking' => $booking,
        'user' => $user,
        'players' => $players,
    );
}

} `

wes45 commented 2 years ago

and I used this fork from Holger by the way, he helped a lot; https://github.com/hschiebold/ep3-bs