+ //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();
+ }
+ }