--- workflow.module 2007-01-02 19:50:09.000000000 +0200 +++ workflow-new.module 2007-01-10 15:14:15.062500000 +0200 @@ -5,6 +5,9 @@ define('WORKFLOW_CREATION_DEFAULT_WEIGHT', -50); define('WORKFLOW_ARROW', '→'); +require_once( 'php_api.php' ); +require_once( 'graphviz_api.php' ); + /** * Implementation of hook_help(). */ @@ -73,6 +76,18 @@ function workflow_menu($may_cache) { 'type' => MENU_CALLBACK, 'callback' => 'workflow_delete'); + $items[] = array('path' => 'admin/workflow/graph', + 'title' => t('Graph workflow'), + 'access' => $access, + 'type' => MENU_CALLBACK, + 'callback' => 'workflow_graph'); + + $items[] = array('path' => 'admin/workflow/graphimg', + 'title' => t('Generate workflow graph image'), + 'access' => $access, + 'type' => MENU_CALLBACK, + 'callback' => 'workflow_graph_img'); + $items[] = array('path' => 'admin/workflow/actions', 'title' => t('Workflow actions'), 'access' => $access, @@ -1043,7 +1058,7 @@ function workflow_transition_allowed($ti */ function workflow_overview() { $result = db_query("SELECT * FROM {workflows}"); - $header = array(array('data' => t('Name')), array('data' => 'Operations', 'colspan' => '4')); + $header = array(array('data' => t('Name')), array('data' => 'Operations', 'colspan' => '5')); $row = array(); while ($data = db_fetch_object($result)) { @@ -1053,7 +1068,8 @@ function workflow_overview() { array('data' => l(t('add state'), "admin/workflow/state/$data->wid")), array('data' => $states && module_exist('actions') ? l(t('actions'), "admin/workflow/actions/$data->wid") : ''), array('data' => l(t('edit'), "admin/workflow/edit/$data->wid")), - array('data' => l(t('delete'), "admin/workflow/delete/$data->wid"))); + array('data' => l(t('delete'), "admin/workflow/delete/$data->wid")), + array('data' => l(t('graph'), "admin/workflow/graph/$data->wid"))); if ($states) { foreach ($states as $sid => $state) { @@ -1062,6 +1078,7 @@ function workflow_overview() { array('data' => $state, 'colspan' => 3), array('data' => l(t('edit'), "admin/workflow/state/$data->wid/$sid")), array('data' => l(t('delete'), "admin/workflow/delete/$data->wid/$sid")), + array('data' => ' '), ); } } @@ -1810,3 +1827,70 @@ function workflow_cron() { cache_clear_all(); } } + +/** + * Create a graph out of the given workflow. + * The initial function returns an image tag that invokes the second function that computes the graph image using Graphviz. + * + * @param $wid + * integer The ID of the workflow. + */ +function workflow_graph($wid) { + $output = ''; + if (variable_get('workflow_graph_debug', false)) { + ob_start(); + echo '
';
+    $states = workflow_get_states($wid);
+    var_dump($states);
+    $G = new Digraph();
+    foreach ($states as $sid => $state) {  
+      $G->add_node($state);
+      echo "$state: \r\n";
+      $result = db_query("SELECT t.roles, t.tid, t.target_sid as state_id, s.state as state_name FROM "
+        . "{workflow_transitions} t INNER JOIN {workflow_states} s ON s.sid = "
+        . "t.target_sid WHERE t.sid = %d", $sid);
+      while ($t = db_fetch_array($result)) {
+        $attributes = array();
+        $attributes['label'] = _workflow_format_roles_label($t['roles']);
+        $G->add_edge($state, $t['state_name'], $attributes);
+        var_dump($t);
+      }
+    }
+    $G->generate();
+    echo '
'; + $output = ob_get_contents(); + ob_end_clean(); + } + $output .= ""; + return $output; +} + +function workflow_graph_img($wid) { + $states = workflow_get_states($wid); + $G = new Digraph(); + foreach ($states as $sid => $state) { + $G->add_node($state); + $result = db_query("SELECT t.roles, t.tid, t.target_sid as state_id, s.state as state_name FROM " + . "{workflow_transitions} t INNER JOIN {workflow_states} s ON s.sid = " + . "t.target_sid WHERE t.sid = %d", $sid); + while ($t = db_fetch_array($result)) { + $attributes = array(); + $attributes['label'] = _workflow_format_roles_label($t['roles']); + $G->add_edge($state, $t['state_name'], $attributes); + } + } + $G->output('png', true); +} + +function _workflow_format_roles_label($roles_string) { + $roles = explode(',', $roles_string); + $label = array(); + foreach ($roles as $role) { + if (is_numeric($role)) { + $label[] = db_result(db_query("SELECT name FROM {role} WHERE rid=$role")); + } else { + $label[] = $role; + } + } + return implode(',\l', $label); +}