/** * @param Request $request * @param string $uri * @return Response * @throws \Exception * * @ApiDoc( * description="Update the calendar with the given uri", * requirements={ * { * "name"="uri", * "dataType"="string", * "description"="The uri of the calendar", * }, * }, * parameters={ * { * "name"="displayname", * "dataType"="string", * "required"=false, * "description"="The name of the calendar" * }, * { * "name"="description", * "dataType"="string", * "required"=false, * "description"="The description of the calendar" * } * } * ) */ public function updateCalendarAction(Request $request, $uri) { $where = Where::create('uri = $*', [$uri]); $calendars = $this->get('pmanager')->findWhere('public', 'calendar', $where); if ($calendars->count() == 0) { return $this->buildError('404', 'The calendar with the given uri could not be found.'); } $calendar = $calendars->get(0); $params = array(); $content = $request->getContent(); if (!empty($content)) { $params = json_decode($content, true); } $previousName = $calendar->displayname; foreach ($params as $name => $value) { $calendar->{$name} = $value; } if ($previousName != $calendar->displayname) { $calendarBackend = new CalendarBackend($this->get('pmanager'), null, $this->get('slugify')); $calendar->slug = $calendarBackend->generateSlug($calendar->displayname, 'calendar'); } $this->get('pmanager')->updateOne('public', 'calendar', $calendar, ['slug', 'displayname', 'description']); return $this->buildResponse(['calendar' => 'updated']); }
/** * @param Request $request * @param string $slug * * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response */ public function calendarUpdateAction(Request $request, $slug) { $this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!'); $usr = $this->get('security.token_storage')->getToken()->getUser(); $where = Where::create('slug = $*', [$slug]); $calendars = $this->get('pmanager')->findWhere('public', 'calendar', $where); if ($calendars->count() == 0) { return $this->redirectToRoute('calendar_home'); } $calendar = $calendars->get(0); if ($calendar->principaluri != 'principals/' . $usr->getUsernameCanonical()) { $this->addFlash('danger', 'Ce calendrier ne vous appartient pas.'); return $this->redirectToRoute('calendar_read', ['slug' => $slug]); } $previousName = $calendar->displayname; $form = $this->createForm(new CalendarType(), $calendar, ['csrf_protection' => false]); $form->handleRequest($request); if ($form->isValid()) { if ($previousName != $calendar->displayname) { $calendarBackend = new Calendar($this->get('pmanager'), null, $this->get('slugify')); $calendar->slug = $calendarBackend->generateSlug($calendar->displayname, 'calendar'); } $this->get('pmanager')->updateOne('public', 'calendar', $calendar, ['slug', 'displayname', 'description']); $this->addFlash('success', 'Le calendrier "' . $calendar->displayName . '" a bien été modifié.'); return $this->redirectToRoute('calendar_read', ['slug' => $calendar->slug]); } return $this->render('browser/calendar_update.html.twig', array('form' => $form->createView(), 'slug' => $slug)); }