loader = $twig->getLoader(); $this->entityManager = $entityManager; $this->paginator = $paginator; $this->documentService = $documentService; $this->fileService = $fileService; } #[Route('/cds/document', name: 'document')] public function index(): Response { return $this->redirectToRoute('document-category-list'); } #[Route('/cds/document-category/list', name: 'document-category-list')] public function documentCategoryList(Request $request): Response { $user = $this->getUser(); $validation = []; //$v = $request->query->get('validation'); //symfony shits itself if array is in get param... if (isset($_GET['validation']) && is_array($_GET['validation'])) { $validation = $_GET['validation']; } $repository = $this->entityManager->getRepository(DocumentCategory::class); $criteria = []; $orderBy = [ 'rank' => 'ASC', 'category_name' => 'ASC' ]; $documentCategories = $repository->findBy( $criteria, $orderBy ); return $this->render( 'document-category-list.html.twig', [ 'user' => $user, 'page' => [ 'title' => 'Documents', 'section' => 'CDS', 'section_link' => '/cds', ], 'documentCategories' => $documentCategories, 'validation' => $validation ] ); } #[Route('/cds/document/list/{documentCategoryId}', name: 'document-list')] public function documentList(Request $request, int $documentCategoryId): Response { $user = $this->getUser(); $repository = $this->entityManager->getRepository(DocumentCategory::class); $documentCategory = $repository->find($documentCategoryId); $page = $request->query->getInt('page', 1); $query = $this->entityManager ->createQuery( 'SELECT d FROM App\Entity\Document d WHERE d.document_category = :id ORDER BY d.rank, d.title, d.upload_date ASC' ) ->setParameter('id', $documentCategoryId) ->setMaxResults($this->pageLimit) ->setFirstResult($this->pageLimit * $page); $pagination = $this->paginator->paginate( $query, $page, $this->pageLimit ); $pagination->setCustomParameters([ 'ajax_func' => 'buildDocumentList', 'ajax_param1' => "'document-list-" . $documentCategoryId ."'", 'ajax_param2' => $documentCategoryId ]); $documents = $query->execute(); return $this->render( 'partials/document/document-list.html.twig', [ 'user' => $user, 'page' => [ 'title' => 'Documents', 'section' => 'CDS', 'section_link' => '/cds', ], 'documentCategory' => $documentCategory, 'documents' => $documents, 'pagination' => $pagination ] ); // $dql = "SELECT a FROM AcmeMainBundle:Article a"; // $query = $em->createQuery($dql); // $pagination = $paginator->paginate( // $query, /* query NOT result */ // $request->query->getInt('page', 1), /* page number */ // 10 /* limit per page */ // ); // // parameters to template // return $this->render('article/list.html.twig', ['pagination' => $pagination]); } #[Route('/cds/document-category/add', name: 'document-category-add')] public function addDocumentCategory(Request $request): Response { $user = $this->getUser(); $data = []; $validation = []; $post = $request->getPayload()->all(); if (count($post) > 0) { $data = $this->documentService->sanitiseDocumentCategoryData($post); $validation = $this->documentService->validateDocumentCategoryData($data); if ($validation['success']) { $___documentCategory = $this->documentService->createDocumentCategoryFromData($data); $this->entityManager->persist($___documentCategory); $this->entityManager->flush(); return $this->redirectToRoute( 'document-category-list', [ 'validation' => [ 'success' => 'Document Category added successfully.' ] ] ); } } else { $data = new DocumentCategory(); } return $this->render( 'document-category-add.html.twig', [ 'user' => $user, 'page' => [ 'title' => 'Add Document Category', 'section' => 'Documents', 'section_link' => '/cds/document' ], 'data' => $data, 'validation' => $validation ] ); } #[Route('/cds/document-category/edit', name: 'document-category-edit')] public function editDocumentCategory(Request $request): Response { $user = $this->getUser(); $data = []; $validation = []; $repository = $this->entityManager->getRepository(DocumentCategory::class); $post = $request->getPayload()->all(); if (count($post) > 0 && isset($post['id'])) { $documentCategory = $repository->find($post['id']); if ($documentCategory != null) { $data = $this->documentService->sanitiseDocumentCategoryData($post); $validation = $this->documentService->validateDocumentCategoryData($data); if ($validation['success']) { $this->documentService->updateDocumentCategoryFromData($documentCategory, $data); $this->entityManager->persist($documentCategory); $this->entityManager->flush(); return $this->redirectToRoute( 'document-category-list', [ 'validation' => [ 'success' => 'Document Category edited successfully.' ] ] ); } } else { //document category not found return $this->redirectToRoute('document-category-list'); } } else { $id = (int)$request->query->get('id'); $documentCategory = $repository->find($id); if ($documentCategory != null) { $data = $documentCategory; } else { return $this->redirectToRoute('document-category-list'); } } return $this->render( 'document-category-edit.html.twig', [ 'user' => $user, 'page' => [ 'title' => 'Edit Document Category', 'section' => 'Documents', 'section_link' => '/cds/document' ], 'data' => $data, 'validation' => $validation ] ); } #[Route('/cds/document-category/delete', name: 'document-category-delete')] public function deleteDocumentCategory(Request $request): Response { $user = $this->getUser(); $data = []; $validation = []; $repository = $this->entityManager->getRepository(DocumentCategory::class); $post = $request->getPayload()->all(); if (count($post) > 0 && isset($post['id'])) { $documentCategory = $repository->find($post['id']); if ($documentCategory != null) { $data = $documentCategory; if (isset($post['confirm'])) { $this->documentService->deleteDocumentCategory($documentCategory); return $this->redirectToRoute( 'document-category-list', [ 'validation' => [ 'success' => 'Document Category deleted successfully.' ] ] ); } else { $validation['errors'] = [ 'Please confirm deletion, or press cancel.' ]; } } else { //document category not found return $this->redirectToRoute('document-category-list'); } } else { $id = (int)$request->query->get('id'); $documentCategory = $repository->find($id); if ($documentCategory != null) { $data = $documentCategory; } else { return $this->redirectToRoute('document-category-list'); } } return $this->render( 'document-category-delete.html.twig', [ 'user' => $user, 'page' => [ 'title' => 'Delete Document Category', 'section' => 'Documents', 'section_link' => '/cds/document' ], 'data' => $data, 'validation' => $validation ] ); } #[Route('/cds/document/add', name: 'document-add')] public function addDocument(Request $request): Response { $user = $this->getUser(); $data = []; $validation = []; $documentCategoryRepository = $this->entityManager->getRepository(DocumentCategory::class); $post = $request->getPayload()->all(); if (count($post) > 0) { $documentCategoryId = (int)$post['document_category_id']; $documentCategory = $documentCategoryRepository->find($documentCategoryId); if ($documentCategory == null) { return $this->redirectToRoute( 'document-category-list', [ 'validation' => [ 'errors' => [ 'Unknown Document Category' ] ] ] ); } $data = $this->documentService->sanitiseDocumentData($post); $validation = $this->documentService->validateDocumentData($data); if ($validation['success']) { $___document = $this->documentService->createDocumentFromData($data); //FILES $file = $request->files->get('document'); if ($file) { $filename = $this->fileService->upload('documentInternal', $file); if ($filename != null) { $___document->setFilename($filename); } } ////// $this->entityManager->persist($___document); $this->entityManager->flush(); return $this->redirectToRoute( 'document-category-list', [ 'validation' => [ 'success' => 'Document added successfully.' ] ] ); } } else { $documentCategoryId = (int)$request->query->get('document_category_id'); $documentCategory = $documentCategoryRepository->find($documentCategoryId); if ($documentCategory != null) { $data = new Document($documentCategory); } else { //document category not found return $this->redirectToRoute( 'document-category-list', [ 'validation' => [ 'errors' => [ 'Unknown Document Category' ] ] ] ); } } return $this->render( 'document-add.html.twig', [ 'user' => $user, 'page' => [ 'title' => 'Add Document', 'section' => 'Documents', 'section_link' => '/cds/document' ], 'data' => $data, 'validation' => $validation ] ); } #[Route('/cds/document/edit', name: 'document-edit')] public function editDocument(Request $request): Response { $user = $this->getUser(); $data = []; $validation = []; $repository = $this->entityManager->getRepository(Document::class); $post = $request->getPayload()->all(); if (count($post) > 0 && isset($post['id'])) { $document= $repository->find($post['id']); if ($document != null) { $data = $this->documentService->sanitiseDocumentData($post); $validation = $this->documentService->validateDocumentData($data); if ($validation['success']) { $this->documentService->updateDocumentFromData($document, $data); $this->entityManager->persist($document); $this->entityManager->flush(); return $this->redirectToRoute( 'document-category-list', [ 'validation' => [ 'success' => 'Document edited successfully.' ] ] ); } } else { //document not found return $this->redirectToRoute('document-category-list'); } } else { $id = (int)$request->query->get('id'); $document = $repository->find($id); if ($document != null) { $data = $document; } else { return $this->redirectToRoute('document-category-list'); } } return $this->render( 'document-edit.html.twig', [ 'user' => $user, 'page' => [ 'title' => 'Edit Document', 'section' => 'Documents', 'section_link' => '/cds/document' ], 'data' => $data, 'validation' => $validation ] ); } #[Route('/cds/document/delete', name: 'document-delete')] public function deleteDocument(Request $request): Response { $user = $this->getUser(); $data = []; $validation = []; $repository = $this->entityManager->getRepository(Document::class); $post = $request->getPayload()->all(); if (count($post) > 0 && isset($post['id'])) { $document= $repository->find($post['id']); if ($document!= null) { $data = $document; if (isset($post['confirm'])) { $this->documentService->deleteDocument($document); return $this->redirectToRoute( 'document-category-list', [ 'validation' => [ 'success' => 'Document deleted successfully.' ] ] ); } else { $validation['errors'] = [ 'Please confirm deletion, or press cancel.' ]; } } else { //document not found return $this->redirectToRoute('document-category-list'); } } else { $id = (int)$request->query->get('id'); $document = $repository->find($id); if ($document != null) { $data = $document; } else { return $this->redirectToRoute('document-category-list'); } } return $this->render( 'document-delete.html.twig', [ 'user' => $user, 'page' => [ 'title' => 'Delete Document', 'section' => 'Documents', 'section_link' => '/cds/document' ], 'data' => $data, 'validation' => $validation ] ); } }