|
@@ -0,0 +1,128 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace WorkflowBundle\Controller;
|
|
|
+
|
|
|
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
|
+use Symfony\Component\Workflow\Exception\ExceptionInterface;
|
|
|
+use Symfony\Component\HttpFoundation\Request;
|
|
|
+use Symfony\Component\HttpFoundation\Response;
|
|
|
+use Sonata\AdminBundle\Admin\Pool;
|
|
|
+
|
|
|
+class WorkflowController extends Controller
|
|
|
+{
|
|
|
+ var $class;
|
|
|
+ var $object;
|
|
|
+ var $admin;
|
|
|
+
|
|
|
+ public function applyTransitionAction($id, $transition, $workflow = null, Request $request)
|
|
|
+ {
|
|
|
+ $class = $request->get('class');
|
|
|
+ $em = $this->get('doctrine')->getManager();
|
|
|
+
|
|
|
+ if(!$this->validate($class,$id)) {
|
|
|
+ return $this->redirect($this->generateUrl("sonata_admin_dashboard"));
|
|
|
+ }
|
|
|
+
|
|
|
+ $object = $this->object;
|
|
|
+ $admin = $this->admin;
|
|
|
+ $back_route = $admin->getBaseRouteName()."_list";
|
|
|
+
|
|
|
+ if(is_null($workflow)) {$workflow = "{$object->getWorkflowType()}.{$object->getWorkflow()}";}
|
|
|
+
|
|
|
+ try {
|
|
|
+ $this->get("{$workflow}")->apply($object, $transition);
|
|
|
+ $em->persist($object);
|
|
|
+ $em->flush();
|
|
|
+ } catch (ExceptionInterface $e) {
|
|
|
+ $this->get('session')->getFlashBag()->add('danger', $e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->redirect($this->generateUrl($back_route));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function showWorkflowAction($id, Request $request)
|
|
|
+ {
|
|
|
+ $class = $request->get('class');
|
|
|
+
|
|
|
+ if(!$this->validate($class,$id)) {
|
|
|
+ return $this->redirect($this->generateUrl("sonata_admin_dashboard"));
|
|
|
+ }
|
|
|
+
|
|
|
+ $object = $this->object;
|
|
|
+ $admin = $this->admin;
|
|
|
+ $back_route = $admin->getBaseRouteName()."_list";
|
|
|
+
|
|
|
+ return $this->render('WorkflowBundle:Workflow:show_workflow.html.twig', ['object' => $object,'back_route' => $back_route]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function resetStateAction($id, Request $request)
|
|
|
+ {
|
|
|
+ $class = $request->get('class');
|
|
|
+ $em = $this->get('doctrine')->getManager();
|
|
|
+
|
|
|
+ if(!$this->validate($class,$id)) {
|
|
|
+ return $this->redirect($this->generateUrl("sonata_admin_dashboard"));
|
|
|
+ }
|
|
|
+
|
|
|
+ $object = $this->object;
|
|
|
+ $admin = $this->admin;
|
|
|
+ $back_route = $admin->getBaseRouteName()."_list";
|
|
|
+
|
|
|
+ if(is_null($object->getWorkflow())) {
|
|
|
+ $object->setCurrentState(null);
|
|
|
+ $em->persist($object);
|
|
|
+ $em->flush();
|
|
|
+
|
|
|
+ return $this->redirect($this->generateUrl($back_route));
|
|
|
+ }
|
|
|
+
|
|
|
+ $workflow = $object->getServiceWorkflow();
|
|
|
+
|
|
|
+ if($workflow) {
|
|
|
+ $definition = $workflow->getDefinition();
|
|
|
+ if($definition) {
|
|
|
+ $object->setCurrentState($definition->getInitialPlace($object));
|
|
|
+ } else {
|
|
|
+ $object->setCurrentState(null);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $object->setCurrentState(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ $em->persist($object);
|
|
|
+ $em->flush();
|
|
|
+
|
|
|
+ return $this->redirect($this->generateUrl($back_route));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function validate($class, $id) {
|
|
|
+
|
|
|
+ if(!class_exists($class)) {
|
|
|
+ $this->get('session')->getFlashBag()->add('danger', $this->get('translator')->trans('Class not found',array(),'WorkflowBundle'));
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* @var $adminPool \Sonata\AdminBundle\Admin\Pool */
|
|
|
+ $adminPool = $this->get('sonata.admin.pool');
|
|
|
+
|
|
|
+ $em = $this->get('doctrine')->getManager();
|
|
|
+ try {
|
|
|
+ $object = $em->getRepository($class)->findOneById($id);
|
|
|
+ $admin = $adminPool->getAdminByClass($class);
|
|
|
+ } catch (ExceptionInterface $e) {
|
|
|
+ $this->get('session')->getFlashBag()->add('danger', $e->getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(is_null($object) || is_null($admin)) {
|
|
|
+ $this->get('session')->getFlashBag()->add('danger', $this->get('translator')->trans('Object not found',array(),'WorkflowBundle'));
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->object = $object;
|
|
|
+ $this->class = $class;
|
|
|
+ $this->admin = $admin;
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|