View source
<?php
function hansel_export_menu() {
$menu = array();
$menu['admin/config/search/hansel/export'] = array(
'title' => 'Export',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'hansel_export_export_form',
),
'access arguments' => array(
'administer hansel',
),
'type' => MENU_LOCAL_TASK,
'weight' => 8,
);
$menu['admin/config/search/hansel/export/dump'] = array(
'title' => 'Importable dump',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'hansel_export_export_form',
),
'access arguments' => array(
'administer hansel',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 1,
);
$menu['admin/config/search/hansel/export/dot'] = array(
'title' => 'Dot format',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'hansel_export_dot_form',
),
'access arguments' => array(
'administer hansel',
),
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
$menu['admin/config/search/hansel/import'] = array(
'title' => 'Import',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'hansel_export_import_form',
),
'access arguments' => array(
'administer hansel',
),
'type' => MENU_LOCAL_TASK,
'weight' => 9,
);
return $menu;
}
function hansel_export_export_form($form_state) {
$form = array();
$form['config'] = array(
'#type' => 'textarea',
'#title' => t('Hansel configuration export'),
'#default_value' => _hansel_export_get(),
'#rows' => 15,
);
return $form;
}
function hansel_export_dot_form($form_state) {
$form = array();
$form['config'] = array(
'#type' => 'textarea',
'#title' => t('Hansel dot export'),
'#description' => t('Install the !graphviz tools to render this output.', array(
'!graphviz' => l('Graphviz', 'http://graphviz.org/'),
)),
'#default_value' => hansel_export_dot(),
'#rows' => 15,
);
$form['download'] = array(
'#type' => 'submit',
'#value' => t('Download file'),
);
return $form;
}
function hansel_export_dot_form_submit($form, &$form_state) {
header('Content-Type: text/vnd.graphviz');
header('Content-Disposition: attachment; filename="hansel.dot"');
print hansel_export_dot();
module_invoke_all('exit');
exit;
}
function hansel_export_import_form($form_state) {
$form = array();
$form['config'] = array(
'#type' => 'textarea',
'#title' => t('Hansel configuration'),
'#rows' => 15,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Import'),
);
return $form;
}
function hansel_export_import_form_validate($form, &$form_state) {
$config = $form_state['values']['config'];
$config = trim($config);
if (!@unserialize($config)) {
form_set_error('config', t('Unable to read configuration.'));
}
if (!isset($config['version'])) {
form_set_error('config', t('Unable to read configuration.'));
}
if ($config['version'] > 1) {
form_set_error('config', t('Unable to import configuration. The export version is newer than the current version of Hansel.'));
}
}
function hansel_export_import_form_submit($form, &$form_state) {
$config = $form_state['values']['config'];
$config = trim($config);
$config = unserialize($config);
module_load_include('inc', 'hansel', 'hansel.export');
hansel_import_config($config);
drupal_set_message(t('The Hansel configuration was succesfully imported.'));
$form_state['redirect'] = 'admin/config/search/hansel';
}
function _hansel_export_get() {
$config = hansel_get_config();
$config = serialize($config);
return $config;
}
function _hansel_dot_node($id, $label, $color = 'yellow', $type = NULL) {
if (isset($type)) {
$label .= '\\n\\<\\<' . $type . '\\>\\>';
}
return "\n node_{$id} [ label = \"{$label}\"; color = \"{$color}\"; ]";
}
function _hansel_dot_link($from_id, $to_id, $label = '', $color = 'yellow', $type = NULL) {
if (isset($type)) {
$label = '\\n\\<\\<' . $type . '\\>\\>\\n' . $label;
}
return "\n node_{$from_id} -> node_{$to_id}" . (!empty($label) ? " [ label=\"{$label}\" ]" : '');
}
function _hansel_export_dot_node($rule) {
$fields = array();
$fields[] = $rule->name . '\\n';
if (!empty($rule->crumb_action)) {
$fields[] = $rule->crumb_action;
foreach ($rule->crumb_action_arguments as $key => $value) {
$fields[] = 'crumb-' . $key . ': ' . $value;
}
}
$fields[] = '\\n\\<\\<' . $rule->action . '\\>\\>';
switch ($rule->action) {
case 'switch':
$fields[] = $rule->handler;
$fields[] = join('\\n-', $rule->arguments);
$color = 'green';
break;
case 'leave':
$color = 'blue';
break;
default:
$color = 'red';
break;
}
if ($rule->pid == 0) {
$color = "grey";
}
$label = join('\\n', $fields);
$result = _hansel_dot_node($rule->rid, $label, $color);
return $result;
}
function hansel_export_dot() {
$rules = array();
$pids = array(
0,
);
while (count($pids)) {
$pid = array_shift($pids);
$new_rules = _hansel_get_rules($pid);
foreach ($new_rules as $rule) {
$rid = $rule->rid;
if (!isset($rules[$rid])) {
$rules[$rid] = $rule;
$pids[] = $rid;
}
}
}
$result = 'digraph {';
foreach ($rules as $rule) {
$result .= _hansel_export_dot_node($rule);
}
foreach ($rules as $rule) {
if (isset($rule->pid) && $rule->pid != 0) {
$result .= _hansel_dot_link($rule->pid, $rule->rid, 'p', 'black');
}
if ($rule->action == 'goto') {
$result .= _hansel_dot_link($rule->rid, $rule->destination, $rule->action, 'red');
}
}
$result .= "\n}";
return $result;
}