Browse Source

Se modifica el gráfico del workflow, ahora se genera por goJS. Se
agregan algunas cositas para hacerlo posible.

Maximiliano Schvindt 8 years ago
parent
commit
eeb450eeba

+ 6 - 1
Admin/WorkflowAdmin.php

@@ -89,7 +89,7 @@ transitions:
         if(is_null($this->getSubject()->getId())) {
             $tempate_options['data'] = $template_example;
         } else {
-            $this->parameters = array('workflow_name' => $this->getSubject()->getName());
+            $this->parameters = array('workflow_name' => $this->getSubject()->getName(), 'workflow_subject' => $this->getSubject()->getSubject());
         }
         $_supports = WorkFlowEntityClasses::getClass();
         $supports = array();
@@ -117,6 +117,11 @@ transitions:
                'template' => $this->trans("helps.workflow_label_template"),
             ))
             ;
+            
+            if(is_null($this->getSubject()->getId())) {
+                $formMapper
+                    ->add('subject',null, array('template' => 'WorkflowBundle:Workflow:'));
+            }
     }
 
     /**

+ 18 - 2
Entity/Workflow.php

@@ -431,14 +431,14 @@ class Workflow
         $console = $rootDir."/../bin/console";
         exec("php {$console} cache:clear --env=prod");
 
-        $web_workflows = $rootDir."/../web/workflows_png/";
+        /*$web_workflows = $rootDir."/../web/workflows_png/";
         foreach($workflows as $k => $workflow) {
             $file_locate = "{$web_workflows}/{$workflow->getName()}";
             exec("php {$console} workflow:dump {$workflow->getName()} > {$file_locate}.dot");
             if(file_exists("{$file_locate}.dot")) {
                 exec("dot -Tpng {$file_locate}.dot -o {$file_locate}.png");
             }
-        }
+        }*/
     }
 
     public function getDefinition($subject) {
@@ -492,4 +492,20 @@ class Workflow
         return $this->support;
     }
 
+    /**
+     * Get subject
+     *
+     * @return object
+     * Para obtener un workflow desde el registry ya que localiza por object y workflowName
+     */
+    public function getSubject() 
+    {  
+        if($this->support) {
+           $className =  "\\".$this->support[0];
+           return new $className();
+        }
+
+        return null;
+    }
+
 }

File diff suppressed because it is too large
+ 2060 - 0
Resources/public/gojs_graph/go.js


+ 3 - 1
Resources/views/Workflow/show_template.html.twig

@@ -27,5 +27,7 @@
             editor.getSession().setUseWrapMode(true);
         });
     </script>
-    <img src="http://200.50.175.17/ftth/workflows_png/{{object.getName()}}.png?nocache={{ date().timestamp }}" />
+
+    <br />
+    {% include 'WorkflowBundle:Workflow:show_workflow_graph_gojs.html.twig' with {'object': object.getSubject(), 'name': object.getName() } %}
 {% endblock %}

+ 8 - 3
Resources/views/Workflow/show_workflow.html.twig

@@ -1,6 +1,7 @@
 {% extends 'WorkflowBundle:Workflow:layout.html.twig' %}
 
 {% block main %}
+    {#<script src="https://cdnjs.cloudflare.com/ajax/libs/gojs/1.7.11/go-debug.js"></script>#}
     <h1>Object "{{ object}}"</h1>
     {#<div class="row">#}
         {% if object.getWorkflow() is null %}
@@ -34,7 +35,6 @@
             <div class="col-md-6">
                 <h2>What can you do?</h2>
                     <div class="btn-group-vertical" role="group">
-                        {#<code style="color:black;text-align:center">current_state</code> #}
                         {% if workflow_transitions(object, object.getWorkflowName())|length > 0 %}
                             {% for transition in workflow_transitions(object, object.getWorkflowName()) %}
                                 <a  href="{{path('admin_workflow_workflow_apply_transition',{class: get_class(object),id:object.getId(),transition:transition.name})}}" name="transition" value="{{ transition.name }}"
@@ -44,7 +44,7 @@
                                         class="btn btn-danger" disabled="disabled"
                                     {%- endif -%}
                                 >
-                                    {% set label = 'workflow.' ~ object.getWorkflowName ~ '.transitions.' ~ transition.name %}
+                                    {% set label = 'workflow.' ~ object.getWorkflowName() ~ '.transitions.' ~ transition.name %}
                                     {{ transition.name }} | [trans: {{ workflow_translate_label(label, transition.name,{},"WorkflowLabel") }}]
                                 </a>
                             {% endfor %}
@@ -57,7 +57,9 @@
         </div>
         <div>
             <br />
-            <img src="{{ app.request.schemeAndHttpHost ~ app.request.baseUrl }}/../workflows_png/{{object.getWorkflowName()}}.png?nocache={{ date().timestamp }}" />
+
+            {% include 'WorkflowBundle:Workflow:show_workflow_graph_gojs.html.twig' with {'object': object, 'name': object.getWorkflowName() } %}
+            
         </div>
         <br />
     {% endif %}
@@ -66,5 +68,8 @@
             Volver al dashboard
         </a>
     </div>
+    <br />
+
     
+
 {% endblock %}

+ 34 - 0
Resources/views/Workflow/show_workflow_graph_gojs.html.twig

@@ -0,0 +1,34 @@
+<script src="{{ asset('bundles/workflow') }}/gojs_graph/go.js" type="text/javascript" charset="utf-8"></script>
+
+<div id="myDiagramDiv" style="width:800px; height:400px; background-color: #DAE4E4;"></div>
+
+<script type="text/javascript">
+
+var $gojs = go.GraphObject.make;
+var myDiagram =
+    $gojs(go.Diagram, "myDiagramDiv",
+        {initialContentAlignment: go.Spot.Center,"undoManager.isEnabled": false,layout: $gojs(go.Layout,{ })
+    });
+myDiagram.linkTemplate =
+    $gojs(go.Link,{ curve: go.Link.Bezier  },
+    $gojs(go.Shape), //From
+    $gojs(go.Shape, //To
+      { toArrow: "OpenTriangle", stroke: "#303B45", strokeWidth: 1 }),
+    $gojs(go.TextBlock, "middle",
+      {segmentOffset: new go.Point(0, 10),segmentOrientation: go.Link.OrientUpright, background: "white"},
+      new go.Binding("text", "text"))
+    );
+  
+myDiagram.nodeTemplate =
+    $gojs(go.Node, "Auto",
+        $gojs(go.Shape, "Circle", 
+            { strokeWidth: 1,  width: 100, height: 100, margin: 100},
+            new go.Binding("fill", "color")),
+        $gojs(go.TextBlock,
+            { margin: 8 },
+            new go.Binding("text", "key"))
+    );
+myDiagram.model = new go.GraphLinksModel({{ workflow_go_json(object, name)|raw }});
+myDiagram.isEnabled = false; 
+
+</script>

+ 12 - 2
Resources/views/Workflow/workflow_edit.html.twig

@@ -41,14 +41,24 @@
         
         
         {% if admin.parameters.workflow_name is defined %}
+
+            //lastDiv = $("#sonata-ba-field-container-{{ admin.uniqid }}_template").after();
+
+
             editor = $( ".ace_editor" );
-            image = "{{ app.request.schemeAndHttpHost ~ app.request.baseUrl }}/../workflows_png/{{admin.parameters.workflow_name}}.png?nocache={{ date().timestamp }}";
-            editor.parent("div").append("<div> <br /> <img src='"+ image +"'</div>");
+            editor.parent("div").append($("#gojs").html());
+            $("#gojs").remove();
+
         {% endif %}
         
     });
     </script>
 
+    {% if admin.parameters.workflow_name is defined %}
+    <div id="gojs" style="display:none">
+        {% include 'WorkflowBundle:Workflow:show_workflow_graph_gojs.html.twig' with {'object': admin.parameters.workflow_subject, 'name': admin.parameters.workflow_name } %}
+    <div>
+    {% endif %}
     
 
 

+ 50 - 1
Twig/WorkflowExtension.php

@@ -23,7 +23,8 @@ class WorkflowExtension extends \Twig_Extension
             new \Twig_SimpleFunction('workflow_transitions', array($this, 'getEnabledTransitions')),
             new \Twig_SimpleFunction('workflow_correct_state', array($this, 'isCorrectState')),
             new \Twig_SimpleFunction('get_class', array($this, 'getClass')),
-            new \Twig_SimpleFunction('workflow_translate_label', array($this, 'getLabelTranslate'))
+            new \Twig_SimpleFunction('workflow_translate_label', array($this, 'getLabelTranslate')),
+            new \Twig_SimpleFunction('workflow_go_json', array($this, 'getGoJson')),
         );
     }
 
@@ -82,4 +83,52 @@ class WorkflowExtension extends \Twig_Extension
 
         return $trans;
     }
+
+    public function getGoJson($object, $name = null)
+    {
+        $data = array();
+        $nodes = array();
+        $links = array();
+
+        $c = "FTTHBundle\Entity\ONU";
+        $class = "\\$c";
+        $object = new $class();
+        //print_r(get_class($object));
+        //die;
+
+        $definition = $this->workflowRegistry->get($object, $name)->getDefinition();
+        $places = $definition->getPlaces();
+        $initial = $definition->getInitialPlace();
+        $transitions = $definition->getTransitions();
+
+        foreach($places as $p => $place) {
+            
+            $label = $p;
+            
+            $color = "white";
+            if($p == $initial) $color = "lightblue";
+            $nodes[] = array('key' => $label, 'color' => $color);
+        }
+        
+        foreach($transitions as $transition) {
+            
+            $label = $transition->getName();
+            $from = $transition->getFroms()[0];
+            $to = $transition->getTos()[0];
+            
+            $links[] = array('from' => $from, 'to' => $to, 'text' => $label);
+        }
+
+        $data[] = $nodes;
+        $data[] = $links;
+
+        //return $data;
+
+        $return = json_encode($nodes).",".json_encode($links);
+        
+
+        return $return;
+
+
+    }
 }