container = $container; $this->entityManager = $this->container->get('doctrine.orm.entity_manager'); $this->kernel = $this->container->get('kernel'); $this->photo_warehouse = $photo_warehouse; $this->translator = $this->container->get('translator'); $this->requestStack = $this->container->get('request_stack'); $this->local = $this->requestStack->getCurrentRequest()->getLocale(); } /** * @param PhotosBox $element * @return PhotosBox|null */ public function quickUpdate(PhotosBox $element): ?PhotosBox { if (!$this->entityManager->contains($element)) { try { $this->entityManager->persist($element); } catch (ORMException $e) { return null; } } try { $this->entityManager->flush(); } catch (OptimisticLockException $e) { return null; } catch (ORMException $e) { return null; } return $element; } /** * @param PhotosBox $element * @return bool */ public function delete(PhotosBox $element) { try { $this->entityManager->remove($element); } catch (ORMException $e) { return false; } try { $this->entityManager->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } return true; } public function getByName(string $name){ $photosBox = $this->entityManager->getRepository(PhotosBox::class)->findOneBy(['name'=>$name]); if($photosBox === null){ $photosBox = $this->create($name); } return $photosBox; } private function create(string $name){ if (!isset($name)) return null; $photoBox = new PhotosBox(); $photoBox->setDefault($name); if (!$this->quickUpdate($photoBox)) return null; return $photoBox; } /* * directories is children */ public function addDirectories(PhotosBox $photoBox, Directory $directory){ if (!$photoBox instanceof PhotosBox || !$directory instanceof Directory) return null; $photoBox->addDirectory($directory); return $this->update($photoBox); } public function addFile(PhotosBox $photoBox, FileItem $fileItem){ if (!$photoBox instanceof PhotosBox || !$fileItem instanceof FileItem) return null; $photoBox->addFile($fileItem); return $this->update($photoBox); } public function addFolderOwner(PhotosBox $photoBox, FolderOwner $folderOwner){ if (!$photoBox instanceof PhotosBox || !$folderOwner instanceof FolderOwner) return null; $photoBox->addFolderOwner($folderOwner); return $this->update($photoBox); } public function addFolderGroupOwner(PhotosBox $photoBox, FolderGroupPermission $folderGroupPermission){ if (!$photoBox instanceof PhotosBox || !$folderGroupPermission instanceof FolderGroupPermission) return null; $photoBox->addFolderGroupPermission($folderGroupPermission); return $this->update($photoBox); } public function update(PhotosBox $folder){ return $this->quickUpdate($folder); } public function remove(PhotosBox $photoBox){ $this->delete($photoBox); return true; } public function upload_file(UploadedFile $file, array $response){ $photos_box = $this->getByName("PhotosBox"); // if directory for upload is not exit we create it if (!is_dir($this->photo_warehouse)){ mkdir($this->photo_warehouse); } $mime_type = $file->getMimeType(); //upload tmp dir photo $file_name = $this->container->get('net15.file.upload.manager')->upload($file, $this->photo_warehouse); // Create fileItem if ($file_item = $this->container->get('net15.file.item.manager')->create($this->photo_warehouse.DIRECTORY_SEPARATOR.$file_name, $file->getClientOriginalName())){ $response['message'] = $this->translator->trans('file_manager.file.file_uploaded', [], null, $this->local); } // generate thumbnail try { $this->container->get('net15.files.thumbnail.main')->generate($file_item); } catch (\Exception $e) { } $photos_box->addFilesItm($file_item); if (!$this->update($photos_box)) return null; $response['photo_box'] = $photos_box; $response['success'] = true; return $response; } }