tattali / CalendarBundle

Provides event calendar for your Symfony project. Compatible with API like Google Calendar.
https://packagist.org/packages/tattali/calendar-bundle
MIT License
147 stars 21 forks source link

Filters getting #6

Closed dubitoph closed 5 years ago

dubitoph commented 5 years ago

Hello,

I use Symfony 4..2.1.

I meet a problem when I try to get the filters. I implement the filter variable in the javascript like this : ` eventSources: [ {

                            url: "/fc-load-events",
                            type: "POST",
                            data: {

                                    filters: {"vehicle": 1},

                                },
                            error: () => {

                                            alert("There was an error while fetching FullCalendar!");

                                         },

                        },
                      ],`

In my listener, I get the filters with $filters = $calendar->getFilters();. When I try to get the specific filter with $vehicle = $filters['vehicle'];, I have this error :

"Notice: Undefined index: vehicle"

tattali commented 5 years ago

Hello, I am sending a new release to solve your problem. Fullcalendar has changed its syntax to get events.

https://fullcalendar.io/docs/events-json-feed

- type: "POST",
- data: {
-     filters: {},
- },
- error: () => {},
+ method: "POST",
+ extraParams: {
+     filters: JSON.stringify({}),
+ },
+ failure: () => {},

do not forget to update to 1.1.4

tattali commented 5 years ago

@dubitoph which fullcalendar version do you have ?

dubitoph commented 5 years ago

Thank you very much for your reactivity.

I'm using version 4.

dubitoph commented 5 years ago

I updated to the 1.1.4 version and I changed the javascript like you said.

However, I have a new error in \vendor/tattali/calendar-bundle/src/Controller/CalendarController.php (line 37). It's this line code :

$start = new \DateTime($request->get('start'));

There is my javascipt :

` import '@fullcalendar/core/main.css'; import '@fullcalendar/daygrid/main.css'; import '@fullcalendar/timegrid/main.css'; import '@fullcalendar/list/main.css';

import { Calendar } from '@fullcalendar/core'; import dayGridPlugin from '@fullcalendar/daygrid'; import timeGridPlugin from '@fullcalendar/timegrid'; import listPlugin from '@fullcalendar/list';

export default class ShowCalendar {

static init () 
{

    var calendarEl = document.getElementById('calendar-holder');

    if (calendarEl === null) 
    {

        return;

    }

    var calendar = new FullCalendar.Calendar(calendarEl, {

        defaultView: 'dayGridMonth',
        editable: true,
        eventSources: [
                        {

                            url: "/fc-load-events",
                            method: "POST",
                            extraParams: {
                            filters: JSON.stringify({ vehicle: 1 }),
                            },
                            failure: () => {},

                        },
                      ],
        header: {

                    left: 'prev,next today',
                    center: 'title',
                    right: 'dayGridMonth,timeGridWeek,timeGridDay',

                },
        plugins: [ dayGridPlugin, timeGridPlugin, listPlugin ], // https://fullcalendar.io/docs/plugin-index
        timeZone: 'UTC',

    });

    calendar.render();

}

} `

My listener :

`<?php

namespace App\Listener;

use App\Entity\Booking; use CalendarBundle\Entity\Event; use App\Repository\BookingRepository; use CalendarBundle\Event\CalendarEvent; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class CalendarListener { private $bookingRepository; private $router; private $sessionManager; private $securityContext;

public function __construct(BookingRepository $bookingRepository, UrlGeneratorInterface $router) 
{

    $this->bookingRepository = $bookingRepository;
    $this->router = $router; 

}

public function load(CalendarEvent $calendar): void
{

    $start = $calendar->getStart()->format('Y-m-d H:i:s');
    $end = $calendar->getEnd()->format('Y-m-d H:i:s');

    $filters = $calendar->getFilters();

// $vehicle = null;

    $vehicle = $filters['vehicle']; 

// if (!empty($filters)) // { // if (array_key_exists('vehicule', $filters)) // {

// $vehicule = $filters['vehicule'];

// } // }

    $bookings = $this->bookingRepository->findBetweenDates($start, $end, $vehicle);

    foreach ($bookings as $booking) 
    {

        // this create the events with your data (here booking data) to fill calendar
        $bookingEvent = new Event(
                                    $booking->getTitle(),
                                    $booking->getBeginAt(),
                                    $booking->getEndAt() // If the end date is null or not defined, a all day event is created.
                                 )
        ;

        /*
         * Add custom options to events
         *
         * For more information see: https://fullcalendar.io/docs/event-object
         * and: https://github.com/fullcalendar/fullcalendar/blob/master/src/core/options.ts
         */

        $bookingEvent->setOptions([
                                    'backgroundColor' => 'red',
                                    'borderColor' => 'red',
                                  ]
                                 )
        ;

        $bookingEvent->addOption(
                                    'url',
                                    $this->router->generate('admin.booking.show', ['id' => $booking->getId(),])
                                )
        ;

        // finally, add the event to the CalendarEvent to fill the calendar
        $calendar->addEvent($bookingEvent);

    }

}

}`

And my BookingRepository :

`<?php

namespace App\Repository;

use App\Entity\Booking; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Symfony\Bridge\Doctrine\RegistryInterface;

/**

If I remove the setting vehicleId parameter in the bookingRepository function, I don't have any error :

` /**

tattali commented 5 years ago

What is the error message and which parameters are sended in the POST request

tattali commented 5 years ago

You should probably

- var calendar = new FullCalendar.Calendar(calendarEl, {
+ var calendar = new Calendar(calendarEl, {

because else import { Calendar } from '@fullcalendar/core'; is not used

dubitoph commented 5 years ago

I tried this :

In the js :

` var calendar = new Calendar(calendarEl, {

        defaultView: 'dayGridMonth',
        editable: true,
        eventSources: [
                        {

                            url: "/fc-load-events",
                            method: "POST",
                            extraParams: {
                            filters: JSON.stringify({ vehicle: vehicleId }),
                            },
                            failure: () => {},

                        },
                      ],

...`

In the listener :

` $filters = $calendar->getFilters();

    $vehicle = null;

    if (!empty($filters))
    {
        if (array_key_exists('vehicle', $filters))
        {

            $vehiculeId = $filters['vehicle']; 

            $vehicle = $this->bookingRepository->find($vehiculeId);

        }
    } 

    $bookings = $this->bookingRepository->findBetweenDates($start, $end, $vehicle);`

In the repository :

` /**

I have this error :

DateTime::__construct() expects parameter 1 to be string, null given

Attached, 3 files with console logs : Capture Capture2 Capture3

tattali commented 5 years ago

do you have empty start date in your database?

dubitoph commented 5 years ago

Currently, I have only one recording in the table booking. There is a screen shot. Capture

tattali commented 5 years ago

ok show me the params in the network panel https://github.com/tattali/CalendarBundle#troubleshoot-ajax-requests

dubitoph commented 5 years ago

I replaced $vehicle = $this->bookingRepository->find($vehiculeId); by $vehicle = $this->vehicleRepository->find($vehiculeId); because that was a mistake.

However, I alwayshave the same message and there is he message in the network panel. Capture

In the repository, I set 3 parameters.

` $query->setParameter('start', $start) ->setParameter('end', $end);

    if (!empty($vehicule))
    {

        $query->setParameter('vehicle', $vehicle);

    }`

There is certainly a problem with the parameter from filters.

dubitoph commented 5 years ago

Sorry, the mistake comes from this : if (!empty($vehicule)). This is if (!empty($vehicle))

tattali commented 5 years ago

ok you still have another error?

dubitoph commented 5 years ago

All is in order now. Sorry for my inattention and thank your very much for your help.

tattali commented 5 years ago

It was a pleasure if you want thank do not forget to star the bundle.

and if I may you should use a linter to facilitate reading

dubitoph commented 5 years ago

Star given ;).

What's a linter?

tattali commented 5 years ago

This is a software that automatically "beautify" your code to follow language standards. This is very useful when you share code with other people.

Here are some interesting links for PHP:

dubitoph commented 5 years ago

Ok, thank you!