public function onDeleteUser(GetEntityResponseEvent $event) { $entity = $event->getEntity(); if (!$entity instanceof UserInterface) { return; } if ($this->user->getUsername() === $entity->getUsername()) { $response = new JsonResponse(array('status' => false, 'message' => $this->translator->trans('message.cant_delete_your_self', array(), $this->container->getParameter('ihsan.simple_admin.translation_domain')))); $event->setResponse($response); } }
protected function handle(Request $request, $data, $template, $action = 'new') { $translator = $this->container->get('translator'); $translationDomain = $this->container->getParameter('ihsan.simple_admin.translation_domain'); $dispatcher = $this->container->get('event_dispatcher'); $form = $this->getForm($data); $event = new GetFormResponseEvent(); $event->setController($this); $event->setFormData($data); $dispatcher->dispatch(Event::PRE_FORM_SUBMIT_EVENT, $event); $response = $event->getResponse(); if ($response) { return $response; } $form->handleRequest($request); $this->outputParameter['page_title'] = $translator->trans($this->pageTitle, array(), $translationDomain); $this->outputParameter['action_method'] = $translator->trans('page.' . $action, array(), $translationDomain); $this->outputParameter['page_description'] = $translator->trans($this->pageDescription, array(), $translationDomain); $this->outputParameter['form'] = $form->createView(); $this->outputParameter['form_theme'] = $this->container->getParameter('ihsan.simple_admin.themes.form_theme'); $this->outputParameter['menu'] = $this->container->getParameter('ihsan.simple_admin.menu'); if ($request->isMethod('POST')) { $preFormValidationEvent = new GetResponseEvent(); $preFormValidationEvent->setRequest($request); $dispatcher->dispatch(Event::PRE_FORM_VALIDATION_EVENT, $preFormValidationEvent); if (!$form->isValid()) { $this->outputParameter['errors'] = true; } else { if ($form->isValid()) { $entity = $form->getData(); $entityManager = $this->getDoctrine()->getManager(); $preSaveEvent = new GetEntityResponseEvent(); $preSaveEvent->setRequest($request); $preSaveEvent->setEntity($entity); $preSaveEvent->setEntityMeneger($entityManager); $postSaveEvent = new GetEntityEvent(); $postSaveEvent->setEntityMeneger($entityManager); $postSaveEvent->setEntity($entity); $dispatcher->dispatch(Event::PRE_SAVE_EVENT, $preSaveEvent); $entityManager->persist($entity); $entityManager->flush(); $dispatcher->dispatch(Event::POST_SAVE_EVENT, $postSaveEvent); $this->outputParameter['success'] = $translator->trans('message.data_saved', array(), $translationDomain); } } } return $this->render($template, $this->outputParameter); }