src/Controller/ChildController.php line 84

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Child;
  4. use App\Form\ChildType;
  5. use App\Form\Child1Type;
  6. use App\Repository\ChildRepository;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use App\Form\ChildType_1;
  12. use App\Form\ChildType_2;
  13. use App\Form\TestType;
  14. /**
  15. * @Route("{_locale?fr}/child",locale="fr", requirements={
  16. * "_locale": "en|fr",
  17. * "_format": "html|xml",
  18. * })")
  19. */
  20. class ChildController extends AbstractController
  21. {
  22. /**
  23. * @Route("/", name="child_index", methods={"GET"})
  24. */
  25. public function index(ChildRepository $childRepository): Response
  26. {
  27. return $this->render('child/index.html.twig', [
  28. 'children' => $childRepository->findAll(),
  29. ]);
  30. }
  31. /**
  32. * @Route("/hospital/{id}", name="child_hospital_index", methods={"GET"})
  33. */
  34. public function indexHospital(\App\Entity\Hospital $id): Response
  35. {
  36. $children = $this->getDoctrine()->getManager()->getRepository(Child::class)->findBy([
  37. 'hospital'=>$id
  38. ]);
  39. return $this->render('child/index.html_2.twig', [
  40. 'children' => $children,
  41. ]);
  42. }
  43. /**
  44. * @Route("/new_dec", name="child_dec", methods={"GET","POST"})
  45. */
  46. public function new(Request $request): Response
  47. {
  48. $child = new Child();
  49. $form = $this->createForm(Child1Type::class, $child);
  50. $form->handleRequest($request);
  51. if ($form->isSubmitted() && $form->isValid()) {
  52. $entityManager = $this->getDoctrine()->getManager();
  53. $entityManager->persist($child);
  54. $entityManager->flush();
  55. $id = $child->getId();
  56. $this->VerifyForm($form,$child);
  57. $entityManager->persist($child);
  58. $entityManager->flush();
  59. return $this->redirectToRoute('chid_show', ['id'=>$child->getId()]);
  60. }
  61. return $this->render('child/new.html.twig', [
  62. 'child' => $child,
  63. 'form' => $form->createView(),
  64. ]);
  65. }
  66. /**
  67. * @Route("/test_child", name="child_test", methods={"GET","POST"})
  68. */
  69. public function test(Request $request){
  70. $form = $this->createForm(TestType::class);
  71. $form->handleRequest($request);
  72. if ($form->isSubmitted() && $form->isValid()) {
  73. $id = $form['id']->getData();
  74. $date = $form['dateOfBirth']->getData();
  75. $hospital = $form['hospital']->getData();
  76. $val = intval($id);
  77. /*
  78. * param Child $child
  79. */
  80. $child = $this->getDoctrine()->getManager()->getRepository(Child::class)->find($id);
  81. if($child->getHospital() == $hospital && $child->getDateOfBirth() == $date)
  82. {
  83. return $this->redirectToRoute('child_edit', [
  84. 'id' => $val
  85. ]);
  86. }
  87. else{
  88. return $this->render('test/new.html.twig',[
  89. 'form'=>$form->createView(),
  90. 'message'=>'Verify your informations'
  91. ]
  92. );
  93. }
  94. }
  95. return $this->render('test/new.html.twig',[
  96. 'form'=>$form->createView(),
  97. 'message'=>''
  98. ]
  99. );
  100. }
  101. /**
  102. * @Route("/new", name="child_new", methods={"GET","POST"})
  103. */
  104. public function childDec(Request $request): Response
  105. {
  106. $child = new Child();
  107. $form = $this->createForm(ChildType::class, $child);
  108. $form->handleRequest($request);
  109. if ($form->isSubmitted() && $form->isValid()) {
  110. $entityManager = $this->getDoctrine()->getManager();
  111. $child->setPlaceOfBirth('');
  112. $entityManager->persist($child);
  113. $entityManager->flush();
  114. $this->VerifyForm($form,$child);
  115. $entityManager->persist($child);
  116. $entityManager->flush();
  117. return $this->redirectToRoute('child_show',[
  118. 'id'=>$child->getId()
  119. ]);
  120. }
  121. return $this->render('child/new.html.twig', [
  122. 'child' => $child,
  123. 'form' => $form->createView(),
  124. ]);
  125. }
  126. /**
  127. * @Route("/child_ok/{id}", name="child_ok", methods={"GET","POST"})
  128. */
  129. public function childok(Request $request, $id): Response
  130. {
  131. return $this->render('child/index.html_1.twig', [
  132. 'id' => $id,
  133. ]);
  134. }
  135. /**
  136. * @Route("/{id}", name="child_show", methods={"GET"})
  137. */
  138. public function show(Child $child): Response
  139. {
  140. return $this->render('child/show.html.twig', [
  141. 'child' => $child,
  142. ]);
  143. }
  144. /**
  145. * @Route("/{id}/edit", name="child_edit", methods={"GET","POST"})
  146. */
  147. public function edit(Request $request, Child $child): Response
  148. {
  149. $form = $this->createForm(ChildType::class, $child);
  150. $form->handleRequest($request);
  151. if ($form->isSubmitted() && $form->isValid()) {
  152. $this->VerifyForm($form,$child);
  153. $this->getDoctrine()->getManager()->flush();
  154. return $this->redirectToRoute('child_show',['id'=>$child->getId()]);
  155. }
  156. return $this->render('child/edit.html.twig', [
  157. 'child' => $child,
  158. 'form' => $form->createView(),
  159. ]);
  160. }
  161. /**
  162. * @Route("/del/{id}", name="child_delete", methods={"DELETE"})
  163. */
  164. public function delete(Request $request, Child $child): Response
  165. {
  166. if ($this->isCsrfTokenValid('delete'.$child->getId(), $request->request->get('_token'))) {
  167. $entityManager = $this->getDoctrine()->getManager();
  168. $entityManager->remove($child);
  169. $entityManager->flush();
  170. }
  171. return $this->redirectToRoute('child_index');
  172. }
  173. public function uploadImg($data,$id)
  174. {
  175. // ...
  176. $file = $data->getData();
  177. $extension = $file->guessExtension();
  178. if (!$extension) {
  179. // extension cannot be guessed
  180. $extension = 'bin';
  181. }
  182. $file->move(__DIR__.'/../../public/uploads/images/child/image', 'file'.$id.'.'.$extension);
  183. // ...
  184. }
  185. public function uploadNic($data,$id)
  186. {
  187. // ...
  188. $file = $data;
  189. $extension = $file->guessExtension();
  190. if (!$extension) {
  191. // extension cannot be guessed
  192. $extension = 'bin';
  193. }
  194. var_dump($data);
  195. dump($data);
  196. $file->move(__DIR__.'/../../public/uploads/nic/child', 'file_'.$id.'.'.$extension);
  197. return 'file'.$id.'.'.$extension;
  198. // ...
  199. }
  200. public function VerifyFormFile($formElt,$name,Child $child) {
  201. if(!empty($formElt)){
  202. $cni_id = $name.'_'.$child->getId();
  203. $filename = $this->uploadNic($formElt,$cni_id);
  204. $method = 'set'.ucfirst($name);
  205. $child->$method($filename);
  206. }
  207. }
  208. public function VerifyForm($form,$child) {
  209. $fatherNic = $form['fatherNic']->getData();
  210. $motherNic = $form['motherNic']->getData();
  211. $firstWitnessNic = $form['firstWitnessNic']->getData();
  212. $SecondWitnessNic = $form['secondWitnessNic']->getData();
  213. $birthDeclaration = $form['birthDeclarationFile']->getData();
  214. $this->VerifyFormFile($fatherNic, 'fatherNic', $child);
  215. $this->VerifyFormFile($motherNic, 'motherNic', $child);
  216. $this->VerifyFormFile($firstWitnessNic, 'firstWitnessNic', $child);
  217. $this->VerifyFormFile($firstWitnessNic, 'secondWitnessNic', $child);
  218. $this->VerifyFormFile($birthDeclaration,'birthDeclarationFile', $child);
  219. }
  220. }