PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/VinUserBundle/Controller/BrokerController.php

https://gitlab.com/svinesh3691/symfony_curd
PHP | 217 lines | 123 code | 33 blank | 61 comment | 16 complexity | 4f4ce3d427527f2066fa8143a8e2046f MD5 | raw file
  1. <?php
  2. namespace VinUserBundle\Controller;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  4. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use AppBundle\Entity\User;
  8. use Symfony\Component\Form\Extension\Core\Type\TextType;
  9. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  10. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  11. use Symfony\Component\Form\Extension\Core\Type\ResetType;
  12. class BrokerController extends Controller
  13. {
  14. /**
  15. * Method to the main page of broker management
  16. *
  17. * @param $request the request object
  18. *
  19. * @return retuns a html page
  20. *
  21. */
  22. public function indexAction(Request $request)
  23. {
  24. // Checking privillage
  25. if(!$this->checkPrivilage()) {
  26. return false;
  27. }
  28. $logged_user = $this->getUser();
  29. $users = $this->getDoctrine()->getRepository('AppBundle:User')->findAll();
  30. return $this->render('VinUserBundle:Broker:main.html.twig', [
  31. 'users' => $users,
  32. 'username' => $logged_user->getFirstName() .' '. $logged_user->getLastName(),
  33. 'userrole' => $logged_user->getRoles(),
  34. 'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
  35. ]);
  36. }
  37. /**
  38. * Method to get the list of brokers
  39. *
  40. * @param $request the request object
  41. *
  42. * @return retuns a json response of employe list
  43. *
  44. */
  45. public function jsonListAction(Request $request)
  46. {
  47. $users = $this->getDoctrine()->getRepository('AppBundle:User')->getUserByRoles('ROLE_BROKER');
  48. return new Response(json_encode($users));
  49. }
  50. /**
  51. * Method to create broker
  52. *
  53. * @param $request the request object
  54. *
  55. * @return retuns a json reponse or the complete form based on the request
  56. *
  57. */
  58. public function addFormAction(Request $request)
  59. {
  60. // Checking privillage
  61. if(!$this->checkPrivilage()) {
  62. return false;
  63. }
  64. $entityManager = $this->get('doctrine')->getManager();
  65. // creating a new user object
  66. $user = new User();
  67. // creating the form
  68. $form = $this->createFormBuilder($user)
  69. ->add('firstName',TextType::class,['attr' => ['class' => 'form-control']])
  70. ->add('lastName',TextType::class,['attr' => ['class' => 'form-control']])
  71. ->add('email',EmailType::class,['attr' => ['class' => 'form-control']])
  72. ->add('plainPassword',TextType::class,['attr' => ['class' => 'form-control']])
  73. ->getForm();
  74. $form->handleRequest($request);
  75. // When we get the post request
  76. if ($request->getMethod() == 'POST') {
  77. // If the form is submitted
  78. if ($form->isSubmitted()) {
  79. // setting some defautts data for broker
  80. $user->setEnabled(1);
  81. $user->setUsername($form['email']->getData());
  82. $user->setRoles(['ROLE_BROKER']);
  83. // Checking for validation errors
  84. $errors = $this->get('validator')->validate($user);
  85. if (count($errors) > 0) { // If validation error exists
  86. $errors_array = $this->get('hase_main')->getFormErrors($form);
  87. $response = [
  88. 'status' => 'failed',
  89. 'error_type' => 'validation_error',
  90. 'errors' => $errors_array,
  91. ];
  92. return new Response(json_encode($response));
  93. } else { // If no validation error
  94. $entityManager->persist($user);
  95. $entityManager->flush();
  96. $response = ['status' => 'success'];
  97. return new Response(json_encode($response));
  98. }
  99. }
  100. }
  101. // If the request is a get request respond with the broker add form
  102. return $this->render('VinUserBundle:Broker:form.html.twig', [
  103. 'form' => $form->createView(),
  104. 'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
  105. ]);
  106. }
  107. /**
  108. * Method to edit broker
  109. *
  110. * @param $request the request object
  111. *
  112. * @return retuns a json reponse or the complete form based on the request
  113. *
  114. */
  115. public function editFormAction(Request $request)
  116. {
  117. // Checking privillage
  118. if(!$this->checkPrivilage()) {
  119. return false;
  120. }
  121. $entityManager = $this->get('doctrine')->getManager();
  122. // Getting the broker id from the request payload
  123. $broker_id = $request->attributes->get('broker_id');
  124. // Getting the user object to edit using the doctrine
  125. $user = $this->getDoctrine()->getRepository('AppBundle:User')->findOneById($broker_id);
  126. // Creating the form
  127. $form = $this->createFormBuilder($user)
  128. ->add('firstName',TextType::class,['attr' => ['class' => 'form-control']])
  129. ->add('lastName',TextType::class,['attr' => ['class' => 'form-control']])
  130. ->add('email',EmailType::class,['attr' => ['class' => 'form-control']])
  131. ->getForm();
  132. $form->handleRequest($request);
  133. // If the request is a post request
  134. if ($request->getMethod() == 'POST') {
  135. if ($form->isSubmitted() && $form->isValid()) {
  136. $user->setEnabled(1);
  137. $user->setUsername($form['email']->getData());
  138. $user->setRoles(['ROLE_BROKER']);
  139. $entityManager->persist($user);
  140. $entityManager->flush();
  141. return new Response(json_encode(array('status'=>'success')));
  142. }
  143. }
  144. return $this->render('VinUserBundle:Broker:form.html.twig', [
  145. 'form' => $form->createView(),
  146. 'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
  147. ]);
  148. }
  149. /**
  150. * Method to delete broker
  151. *
  152. * @param $request the request object
  153. *
  154. * @return retuns a json reponse of the delete status
  155. *
  156. */
  157. public function deleteAction(Request $request)
  158. {
  159. // Checking privillage
  160. if(!$this->checkPrivilage()) {
  161. return false;
  162. }
  163. $entityManager = $this->get('doctrine')->getManager();
  164. $broker_id = $request->request->get('id');
  165. $user = $this->getDoctrine()->getRepository('AppBundle:User')->findOneById($broker_id);
  166. $entityManager->remove($user);
  167. $entityManager->flush();
  168. return new Response(json_encode(array('status'=>'success')));
  169. }
  170. /**
  171. * Method to check privillege
  172. *
  173. * @return retuns a boolean
  174. *
  175. */
  176. protected function checkPrivilage() {
  177. $roles = $this->getUser()->getRoles();
  178. if (in_array('ROLE_SUPER_ADMIN', $roles) || in_array('ROLE_EMPLOYEE', $roles) ) {
  179. return true;
  180. } else {
  181. return false;
  182. }
  183. }
  184. }