/**
  * @param Request $request
  * @return Response
  *
  * @ApiDoc(
  *  description="Create a new calendar",
  *  requirements={
  *      {
  *          "name"="displayname",
  *          "dataType"="string",    
  *          "description"="The name of the calendar",
  *      },
  *      {
  *          "name"="username",
  *          "dataType"="string",
  *          "description"="The name of the owner of the calendar"
  *      }
  *  },
  *  parameters={
  *      {
  *          "name"="description",
  *          "dataType"="string",
  *          "required"=false,
  *          "description"="The description of the calendar"
  *      },
  *  }
  * )
  */
 public function createCalendarAction(Request $request)
 {
     $params = array();
     $content = $request->getContent();
     if (!empty($content)) {
         $params = json_decode($content, true);
     }
     if (!isset($params['displayname']) || !isset($params['description']) || !isset($params['username'])) {
         return $this->buildError(400, 'Missing parameters');
     }
     $calendarBackend = new CalendarBackend($this->get('pmanager'), null, $this->get('slugify'));
     $calendarUri = $calendarBackend->generateCalendarUri();
     $raw = ['{DAV:}displayname' => $params['displayname'], '{urn:ietf:params:xml:ns:caldav}calendar-description' => $params['description'] ?: ""];
     $principalUri = 'principals/' . $params['username'];
     $calendarUid = $calendarBackend->createCalendar($principalUri, $calendarUri, $raw);
     return $this->buildResponse(['created' => $calendarUri]);
 }
 /**
  * @param Request $request
  *
  * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
  * @throws AccessDeniedException
  */
 public function calendarCreateAction(Request $request)
 {
     $this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
     $usr = $this->get('security.token_storage')->getToken()->getUser();
     $form = $this->createForm(new CalendarType(), null, ['csrf_protection' => false]);
     $form->handleRequest($request);
     if ($form->isValid()) {
         $values = $form->getData();
         $calendarBackend = new Calendar($this->get('pmanager'), null, $this->get('slugify'));
         $calendarUri = $calendarBackend->generateCalendarUri();
         $raw = ['{DAV:}displayname' => $values['displayname'], '{urn:ietf:params:xml:ns:caldav}calendar-description' => $values['description']];
         $principalUri = 'principals/' . $usr->getUsernameCanonical();
         $calendarBackend->createCalendar($principalUri, $calendarUri, $raw);
         $this->addFlash('success', 'Le calendrier "' . $values['displayname'] . '" a bien été créé.');
         $where = Where::create('uri = $*', [$calendarUri]);
         $rawCalendars = $this->get('pmanager')->findWhere('public', 'calendar', $where);
         return $this->redirectToRoute('calendar_read', ['slug' => $rawCalendars->get(0)->slug]);
     }
     return $this->render('browser/calendar_create.html.twig', array('form' => $form->createView()));
 }