+ //Iterate on locations
+ foreach($this->context['locations'] as $locationId => $location) {
+ //With existing snippet
+ if (isset($snippets[$location['id']])) {
+ //Set existing in current
+ $current = $snippets[$location['id']];
+ //Without existing snippet
+ } else {
+ //Init snippet
+ $current = new Snippet();
+
+ //Set default locale
+ $current->setLocale($this->locale);
+
+ //Set default user
+ $current->setUser($user);
+
+ //Set default location
+ $current->setLocation($this->doctrine->getRepository(Location::class)->findOneById($location['id']));
+ }
+
+ //Create SnippetType form
+ $form = $this->factory->createNamed(
+ //Set form id
+ 'snippet_'.$locationId.'_'.$id.'_'.$this->locale,
+ //Set form type
+ 'Rapsys\AirBundle\Form\SnippetType',
+ //Set form data
+ $current
+ );
+
+ //Refill the fields in case of invalid form
+ $form->handleRequest($request);
+
+ //Handle submitted and valid form
+ //TODO: add a delete snippet ?
+ if ($form->isSubmitted() && $form->isValid()) {
+ //Get snippet
+ $snippet = $form->getData();
+
+ //Queue snippet save
+ $this->manager->persist($snippet);
+
+ //Flush to get the ids
+ $this->manager->flush();
+
+ //Add notice
+ $this->addFlash('notice', $this->translator->trans('Snippet for %user% %location% updated', ['%location%' => $location['at'], '%user%' => $this->context['user']['pseudonym']]));
+
+ //Redirect to cleaned url
+ return $this->redirectToRoute('rapsys_air_user_view', ['id' => $id, 'user' => $this->context['user']['slug']]);
+ }
+
+ //Add form to context
+ $this->context['forms']['snippets'][$locationId] = $form->createView();
+
+ //With location user source image
+ if (($isFile = is_file($source = $this->config['path'].'/location/'.$location['id'].'/'.$id.'.png')) && ($mtime = stat($source)['mtime'])) {
+ //Set location image
+ $this->context['locations'][$locationId]['image'] = $this->image->getThumb($location['miniature'], $mtime, $source);
+ }
+
+ //Create ImageType form
+ $form = $this->factory->createNamed(
+ //Set form id
+ 'image_'.$locationId.'_'.$id,
+ //Set form type
+ 'Rapsys\AirBundle\Form\ImageType',
+ //Set form data
+ [
+ //Set location
+ 'location' => $location['id'],
+ //Set user
+ 'user' => $id
+ ],
+ //Set form attributes
+ [
+ //Enable delete with image
+ 'delete' => isset($this->context['locations'][$locationId]['image'])
+ ]
+ );
+
+ //Refill the fields in case of invalid form
+ $form->handleRequest($request);
+
+ //Handle submitted and valid form
+ if ($form->isSubmitted() && $form->isValid()) {
+ //With delete
+ if ($form->has('delete') && $form->get('delete')->isClicked()) {
+ //With source and mtime
+ if ($isFile && !empty($source) && !empty($mtime)) {
+ //Clear thumb
+ $this->image->remove($mtime, $source);
+
+ //Unlink file
+ unlink($this->config['path'].'/location/'.$location['id'].'/'.$id.'.png');
+
+ //Add notice
+ $this->addFlash('notice', $this->translator->trans('Image for %user% %location% deleted', ['%location%' => $location['at'], '%user%' => $this->context['user']['pseudonym']]));
+
+ //Redirect to cleaned url
+ return $this->redirectToRoute('rapsys_air_user_view', ['id' => $id, 'user' => $this->context['user']['slug']]);
+ }
+ }
+
+ //With image
+ if ($image = $form->get('image')->getData()) {
+ //Check source path
+ if (!is_dir($dir = dirname($source))) {
+ //Create filesystem object
+ $filesystem = new Filesystem();
+
+ try {
+ //Create dir
+ //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
+ $filesystem->mkdir($dir, 0775);
+ } catch (IOExceptionInterface $e) {
+ //Throw error
+ throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
+ }
+ }
+
+ //Set source
+ $source = realpath($dir).'/'.basename($source);
+
+ //Create imagick object
+ $imagick = new \Imagick();
+
+ //Read image
+ $imagick->readImage($image->getRealPath());
+
+ //Save image
+ if (!$imagick->writeImage($source)) {
+ //Throw error
+ throw new \Exception(sprintf('Unable to write image "%s"', $source));
+ }
+
+ //Add notice
+ $this->addFlash('notice', $this->translator->trans('Image for %user% %location% updated', ['%location%' => $location['at'], '%user%' => $this->context['user']['pseudonym']]));
+
+ //Redirect to cleaned url
+ return $this->redirectToRoute('rapsys_air_user_view', ['id' => $id, 'user' => $this->context['user']['slug']]);
+ }
+ }
+
+ //Add form to context
+ $this->context['forms']['images'][$locationId] = $form->createView();
+ }
+ }
+
+ //Render the view
+ return $this->render('@RapsysAir/user/view.html.twig', ['id' => $id]+$this->context);