| 1 |
<?php // -*-php-*-
|
| 2 |
// $Id: workflow_graph.module,v 1.1 2007/07/21 08:02:40 kratib Exp $
|
| 3 |
/**
|
| 4 |
* @file Produces workflow graphs using Graphviz.
|
| 5 |
* Uses PEAR::Image_GraphViz (http://pear.php.net/package/Image_GraphViz)
|
| 6 |
*/
|
| 7 |
|
| 8 |
require_once('Image/GraphViz.php');
|
| 9 |
|
| 10 |
function workflow_graph_help($section) {
|
| 11 |
switch ($section) {
|
| 12 |
case 'admin/modules#description':
|
| 13 |
return t('Produces workflow graphs using Graphviz. <em>Note: Requires workflow.module and PEAR::Image_GraphViz</em>.');
|
| 14 |
}
|
| 15 |
}
|
| 16 |
|
| 17 |
function workflow_graph_menu($may_cache) {
|
| 18 |
$items = array();
|
| 19 |
$access = user_access('administer workflow');
|
| 20 |
|
| 21 |
if ($may_cache) {
|
| 22 |
$items[] = array('path' => 'admin/build/workflow/graph',
|
| 23 |
'title' => t('Graph workflow'),
|
| 24 |
'access' => $access,
|
| 25 |
'type' => MENU_CALLBACK,
|
| 26 |
'callback' => 'workflow_graph_definition');
|
| 27 |
$items[] = array('path' => 'admin/build/workflow/graph/img',
|
| 28 |
'title' => t('Generate workflow graph image'),
|
| 29 |
'access' => $access,
|
| 30 |
'type' => MENU_CALLBACK,
|
| 31 |
'callback' => 'workflow_graph_definition_image');
|
| 32 |
$items[] = array('path' => 'node/workflow/graph/img',
|
| 33 |
'title' => t('Generate node workflow history graph image'),
|
| 34 |
'access' => TRUE,
|
| 35 |
'type' => MENU_CALLBACK,
|
| 36 |
'callback' => 'workflow_graph_history_image');
|
| 37 |
}
|
| 38 |
else {
|
| 39 |
if (arg(0) == 'node' && is_numeric(arg(1))) {
|
| 40 |
$node = node_load(arg(1));
|
| 41 |
$wid = workflow_get_workflow_for_type($node->type);
|
| 42 |
if ($wid) { // workflow exists for this type
|
| 43 |
global $user;
|
| 44 |
$roles = array_keys($user->roles);
|
| 45 |
if ($node->uid == $user->uid) {
|
| 46 |
$roles = array_merge(array('author'), $roles);
|
| 47 |
}
|
| 48 |
$workflow = db_fetch_object(db_query("SELECT * FROM {workflows} WHERE wid = %d", $wid));
|
| 49 |
$allowed_roles = $workflow->tab_roles ? explode(',', $workflow->tab_roles) : array();
|
| 50 |
$items[] = array('path' => "node/$node->nid/graph",
|
| 51 |
'title' => t('Graph'),
|
| 52 |
'access' => array_intersect($roles, $allowed_roles) || user_access('administer nodes'),
|
| 53 |
'type' => MENU_LOCAL_TASK,
|
| 54 |
'weight' => 3,
|
| 55 |
'callback' => 'workflow_graph_history',
|
| 56 |
'callback arguments' => arg(1),
|
| 57 |
);
|
| 58 |
}
|
| 59 |
}
|
| 60 |
}
|
| 61 |
|
| 62 |
return $items;
|
| 63 |
}
|
| 64 |
|
| 65 |
function workflow_graph_history($nid) {
|
| 66 |
return theme('workflow_graph_history', $nid);
|
| 67 |
}
|
| 68 |
|
| 69 |
function theme_workflow_graph_history($nid) {
|
| 70 |
$node = node_load($nid);
|
| 71 |
drupal_set_title(check_plain($node->title));
|
| 72 |
if (variable_get('workflow_graph.debug', false)) {
|
| 73 |
$G = _workflow_graph_history_generate($nid);
|
| 74 |
$output = '<pre>'.$G->fetch('dot').'</pre>';
|
| 75 |
}
|
| 76 |
$output .= "<img src='?q=node/workflow/graph/img/$nid'/>";
|
| 77 |
return $output;
|
| 78 |
}
|
| 79 |
|
| 80 |
function workflow_graph_history_image($nid) {
|
| 81 |
$G = _workflow_graph_history_generate($nid);
|
| 82 |
$G->image('png');
|
| 83 |
}
|
| 84 |
|
| 85 |
function _workflow_graph_history_generate($nid) {
|
| 86 |
$node = node_load($nid);
|
| 87 |
$wid = workflow_get_workflow_for_type($node->type);
|
| 88 |
$states = workflow_get_states($wid) + array(t('(creation)'));
|
| 89 |
$G = new Image_GraphViz(true, array(
|
| 90 |
'bgcolor' => 'transparent',
|
| 91 |
));
|
| 92 |
$result = db_query("SELECT h.*, u.name FROM {workflow_node_history} h LEFT JOIN {users} u ON h.uid = u.uid WHERE nid = %d ORDER BY stamp ASC", $nid);
|
| 93 |
$transition = 1;
|
| 94 |
while ($history = db_fetch_object($result)) {
|
| 95 |
if ($transition == 1) {
|
| 96 |
$G->addNode($states[$history->old_sid].'0', array('label' => $states[$history->old_sid]));
|
| 97 |
}
|
| 98 |
$G->addNode($states[$history->sid].$transition, array('label' => $states[$history->sid]));
|
| 99 |
$G->addEdge(array($states[$history->old_sid].($transition-1) => $states[$history->sid].$transition), array(
|
| 100 |
'label' => $history->name.'\l'.format_date($history->stamp).'\l'.$history->comment.'\l',
|
| 101 |
));
|
| 102 |
$transition++;
|
| 103 |
}
|
| 104 |
return $G;
|
| 105 |
}
|
| 106 |
|
| 107 |
function workflow_graph_definition($wid) {
|
| 108 |
return theme('workflow_graph_definition', $wid);
|
| 109 |
}
|
| 110 |
|
| 111 |
function theme_workflow_graph_definition($wid) {
|
| 112 |
drupal_set_title(t('Graph workflow %name', array('%name' => workflow_get_name($wid))));
|
| 113 |
if (variable_get('workflow_graph.debug', false)) {
|
| 114 |
$G = _workflow_graph_definition_generate($wid);
|
| 115 |
$output = '<pre>'.$G->fetch('dot').'</pre>';
|
| 116 |
}
|
| 117 |
$output .= "<img src='?q=admin/build/workflow/graph/img/$wid'/>";
|
| 118 |
return $output;
|
| 119 |
}
|
| 120 |
|
| 121 |
function workflow_graph_definition_image($wid) {
|
| 122 |
$G = _workflow_graph_definition_generate($wid);
|
| 123 |
$G->image('png');
|
| 124 |
}
|
| 125 |
|
| 126 |
function _workflow_graph_definition_generate($wid) {
|
| 127 |
$states = workflow_get_states($wid);
|
| 128 |
$G = new Image_GraphViz(true, array(
|
| 129 |
'bgcolor' => 'transparent'
|
| 130 |
));
|
| 131 |
foreach ($states as $sid => $state) {
|
| 132 |
$G->addNode($state);
|
| 133 |
$result = db_query("SELECT t.roles, t.tid, t.target_sid as state_id, s.state as state_name FROM "
|
| 134 |
. "{workflow_transitions} t INNER JOIN {workflow_states} s ON s.sid = "
|
| 135 |
. "t.target_sid WHERE t.sid = %d", $sid);
|
| 136 |
while ($t = db_fetch_array($result)) {
|
| 137 |
$G->addEdge(array($state => $t['state_name']), array(
|
| 138 |
'label' => _workflow_graph_format_roles_label($t['roles']),
|
| 139 |
));
|
| 140 |
}
|
| 141 |
}
|
| 142 |
return $G;
|
| 143 |
}
|
| 144 |
|
| 145 |
function _workflow_graph_format_roles_label($roles_string) {
|
| 146 |
$roles = explode(',', $roles_string);
|
| 147 |
$label = array();
|
| 148 |
foreach ($roles as $role) {
|
| 149 |
if (is_numeric($role)) {
|
| 150 |
$label[] = db_result(db_query("SELECT name FROM {role} WHERE rid=$role"));
|
| 151 |
} else {
|
| 152 |
$label[] = $role;
|
| 153 |
}
|
| 154 |
}
|
| 155 |
return implode(',\l', $label).'\l';
|
| 156 |
}
|
| 157 |
|
| 158 |
?>
|