src/Controller/SubdivisionController.php line 287

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Subdivision;
  4. use App\Form\SubdivisionType;
  5. use App\Repository\SubdivisionRepository;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use App\Form\SubUploadType;
  11. use App\Entity\City;
  12. use App\Entity\Region;
  13. use App\Entity\Municipality;
  14. use App\Entity\Department;
  15. /**
  16. * @Route("/subdivision")
  17. */
  18. class SubdivisionController extends AbstractController
  19. {
  20. /**
  21. * @Route("/", name="subdivision_index", methods={"GET"})
  22. */
  23. public function index(SubdivisionRepository $subdivisionRepository): Response
  24. {
  25. return $this->render('subdivision/index.html.twig', [
  26. 'subdivisions' => $subdivisionRepository->findAll(),
  27. ]);
  28. }
  29. /**
  30. * @Route("/new", name="subdivision_new", methods={"GET","POST"})
  31. */
  32. public function new(Request $request): Response
  33. {
  34. $subdivision = new Subdivision();
  35. $form = $this->createForm(SubdivisionType::class, $subdivision);
  36. $form->handleRequest($request);
  37. if ($form->isSubmitted() && $form->isValid()) {
  38. $entityManager = $this->getDoctrine()->getManager();
  39. $entityManager->persist($subdivision);
  40. $entityManager->flush();
  41. return $this->redirectToRoute('subdivision_index');
  42. }
  43. return $this->render('subdivision/new.html.twig', [
  44. 'subdivision' => $subdivision,
  45. 'form' => $form->createView(),
  46. ]);
  47. }
  48. /**
  49. * @Route("/{id}/edit", name="subdivision_edit", methods={"GET","POST"})
  50. */
  51. public function edit(Request $request, Subdivision $subdivision): Response
  52. {
  53. $form = $this->createForm(SubdivisionType::class, $subdivision);
  54. $form->handleRequest($request);
  55. if ($form->isSubmitted() && $form->isValid()) {
  56. $this->getDoctrine()->getManager()->flush();
  57. return $this->redirectToRoute('subdivision_index');
  58. }
  59. return $this->render('subdivision/edit.html.twig', [
  60. 'subdivision' => $subdivision,
  61. 'form' => $form->createView(),
  62. ]);
  63. }
  64. /**
  65. * @Route("/{id}", name="subdivision_delete", methods={"DELETE"})
  66. */
  67. public function delete(Request $request, Subdivision $subdivision): Response
  68. {
  69. if ($this->isCsrfTokenValid('delete'.$subdivision->getId(), $request->request->get('_token'))) {
  70. $entityManager = $this->getDoctrine()->getManager();
  71. $entityManager->remove($subdivision);
  72. $entityManager->flush();
  73. }
  74. return $this->redirectToRoute('subdivision_index');
  75. }
  76. /**
  77. * @Route("/upload_subdivision",name="upload_sub")
  78. * @param Request $request
  79. * @return type
  80. */
  81. public function upload(Request $request) {
  82. $form = $this->createForm(SubUploadType::class);
  83. $form->handleRequest($request);
  84. if ($form->isSubmitted()){
  85. $dir= __DIR__."/../../public/uploads/media/";
  86. $media1 = $form['file']->getData();
  87. $name= $media1->getClientOriginalName();
  88. $media1->move($dir,$name);
  89. dump($media1);
  90. var_dump($media1);
  91. }
  92. if(isset($media1) && !empty($name)){
  93. // require_once __DIR__.'../../vendor/autoload.php';
  94. // require_once __DIR__.'/../../../web/uploads/media/'.''.$media1->getFilename();
  95. if (($handle = fopen($dir.''.$name, "r")) !== FALSE) {
  96. $i = 0;
  97. $em1 = $this->getDoctrine()->getManager();
  98. $em2 = $this->getDoctrine()->getManager();
  99. $em3 = $this->getDoctrine()->getManager();
  100. while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
  101. $num = count($data);
  102. // echo "<p> $num champs à la ligne $i: <br /></p>\n";
  103. /*
  104. echo $data[$c] . "<br />\n";
  105. }
  106. */
  107. if($i==0){
  108. $enTetes = $data;
  109. }
  110. if($i>0)
  111. {
  112. $subdivision = new Subdivision();
  113. $j = 0;
  114. $donnees = array_combine($enTetes, $data);
  115. // var_dump($donnees);
  116. foreach ($enTetes as $key => $value) {
  117. $city = $this->getDoctrine()->getManager()->getRepository("App:City")->findOneBy(array( 'name' => $donnees['city'],
  118. ));
  119. if (!empty($city))
  120. {
  121. $subdivision->setCity($city);
  122. }
  123. else{
  124. $department = $this->getDoctrine()->getManager()->getRepository('App:Department')->findOneBy([
  125. 'name'=> strval(utf8_decode($donnees['department']))
  126. ]);
  127. if (!empty($department)){
  128. $method = 'set'.ucfirst('department');
  129. if(method_exists( $city , $method))
  130. {
  131. $city->$method($department);
  132. }
  133. }
  134. else {
  135. $region = $this->getDoctrine()->getManager()->getRepository('App:Region')->findOneBy([
  136. 'name'=> strval(utf8_decode($donnees['region']))
  137. ]);
  138. if(empty($region)){
  139. $region=new \App\Entity\Region();
  140. $region->setName(strval(utf8_decode($donnees['region'])));
  141. $country = $em1->getRepository(\App\Entity\Country::class)->findOneBy([
  142. 'name'=>'Cameroun'
  143. ]);
  144. $region->setCountry($country);
  145. $em1->persist($region);
  146. $em1->flush();
  147. }
  148. $department = new Department();
  149. $department->setName($donnees['department']);
  150. $department->setRegion($region);
  151. $method = 'set'.ucfirst('department');
  152. if(method_exists($city, $method))
  153. {
  154. $city->$method($department);
  155. }
  156. }
  157. $city = new City();
  158. if (!empty($donnees['city']))
  159. {
  160. $city->setName(strval(utf8_decode($donnees['city'])));
  161. }
  162. else{
  163. $city->setName(strval(utf8_decode($donnees['name'])));
  164. }
  165. $city->setDepartment($department);
  166. $em1->persist($city);
  167. $em1->flush();
  168. }
  169. foreach ($donnees as $cle => $val) {
  170. $method = 'set'.ucfirst($cle);
  171. $method2 = 'get'.ucfirst($cle);
  172. if(method_exists($subdivision , $method) and empty($subdivision->$method2()))
  173. {
  174. if($cle == 'city'){
  175. $subdivision->$method($city);
  176. }
  177. else{
  178. $subdivision->$method(strval(utf8_decode($val)));
  179. }
  180. }
  181. }
  182. $j++;
  183. }
  184. if(!(empty($subdivision->getName())))
  185. {
  186. $sub = $this->getDoctrine()->getRepository(Subdivision::class)->findOneBy([
  187. 'name'=>$subdivision->getName()]);
  188. if(empty($sub)){
  189. $em1->persist($subdivision);
  190. $em1->flush();
  191. }
  192. $mun = $this->getDoctrine()->getRepository(Municipality::class)->findOneBy([
  193. 'name'=>$subdivision->getName()]);
  194. if(empty($mun)){
  195. $municipality = new Municipality();
  196. $municipality->setName($subdivision->getName());
  197. $municipality->setSubdivision($subdivision);
  198. $municipality->setLocation($subdivision->getName());
  199. $em1->persist($municipality);
  200. $em1->flush();
  201. }
  202. }
  203. }else
  204. {
  205. $em1->flush();
  206. }
  207. ;
  208. $i++; }
  209. fclose($handle);
  210. }
  211. // $em->remove($media1);
  212. }
  213. return $this->render('subdivision/new.html.twig',[
  214. 'form'=>$form->createView()
  215. ]);
  216. }
  217. /**
  218. * @Route("/{id}", name="subdivision_show", methods={"GET"})
  219. */
  220. public function show(Subdivision $subdivision): Response
  221. {
  222. return $this->render('subdivision/show.html.twig', [
  223. 'subdivision' => $subdivision,
  224. ]);
  225. }
  226. }