You are here

hansel_export.module in Hansel breadcrumbs 8

Same filename and directory in other branches
  1. 7 export/hansel_export.module

File

export/hansel_export.module
View source
<?php

/**
 * Implements hook_menu().
 */
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;
}

/**
 * Form providing the configuration dump.
 */
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;
}

/**
 * Form providing the export to dot format.
 */
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;
}

/**
 * Form submit handler. Provides the dot export as a download.
 */
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;
}

/**
 * Form for importing new configuration.
 */
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;
}

/**
 * Form validate callback.
 */
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.'));
  }
}

/**
 * Form submit callback.
 */
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';
}

/**
 * Get a dump of the actual Hansel configuration
 */
function _hansel_export_get() {
  $config = hansel_get_config();
  $config = serialize($config);
  return $config;
}

/**
 * Generate a graphiz node string
 *
 * node [
 *   label = "abc";
 *   color = "yellow";
 * ]
 * @param $id
 *   id is unique allready so we concat the id with node_
 * @param $label
 * @param $color
 * @param $type
 */
function _hansel_dot_node($id, $label, $color = 'yellow', $type = NULL) {
  if (isset($type)) {
    $label .= '\\n\\<\\<' . $type . '\\>\\>';
  }
  return "\n  node_{$id} [ label = \"{$label}\"; color = \"{$color}\"; ]";
}

/**
 * Generate a graphiz node string
 *
 * a -> b [
 *   label = "abc";
 *   color = "yellow";
 * ]
 */
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}\" ]" : '');
}

/**
 * Prepare for graphviz node and link strings
 *
 * A rule is like:
 *
 * stdClass Object
 * (
 *   [rid] => 21
 *   [pid] => 62
 *   [name] => <default>
 *   [crumb_action] =>
 *   [crumb_action_arguments] => Array
 *       (
 *       )
 *
 *   [action] => switch
 *   [handler] => node type
 *   [arguments] => Array
 *       (
 *       )
 * )
 *
 * We color the node depending on it's kind
 *
 * @param $rule
 */
function _hansel_export_dot_node($rule) {
  $fields = array();
  $fields[] = $rule->name . '\\n';

  // We first gather the crumb settings
  if (!empty($rule->crumb_action)) {
    $fields[] = $rule->crumb_action;
    foreach ($rule->crumb_action_arguments as $key => $value) {
      $fields[] = 'crumb-' . $key . ': ' . $value;
    }
  }

  // Next we add the action settings
  $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;
  }

  // Root nodes get different color
  if ($rule->pid == 0) {
    $color = "grey";
  }
  $label = join('\\n', $fields);
  $result = _hansel_dot_node($rule->rid, $label, $color);
  return $result;
}

/**
 * Generete an export to dot format for the current configuration.
 */
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;
      }
    }
  }

  // It is a directed graph
  $result = 'digraph {';

  // Generate dot-nodes
  foreach ($rules as $rule) {
    $result .= _hansel_export_dot_node($rule);
  }

  // Generate links
  foreach ($rules as $rule) {

    // A link from parent rule to rule
    if (isset($rule->pid) && $rule->pid != 0) {
      $result .= _hansel_dot_link($rule->pid, $rule->rid, 'p', 'black');
    }

    // A goto introduces an extra link from rule to destination
    if ($rule->action == 'goto') {
      $result .= _hansel_dot_link($rule->rid, $rule->destination, $rule->action, 'red');
    }
  }
  $result .= "\n}";
  return $result;
}

Functions

Namesort descending Description
hansel_export_dot Generete an export to dot format for the current configuration.
hansel_export_dot_form Form providing the export to dot format.
hansel_export_dot_form_submit Form submit handler. Provides the dot export as a download.
hansel_export_export_form Form providing the configuration dump.
hansel_export_import_form Form for importing new configuration.
hansel_export_import_form_submit Form submit callback.
hansel_export_import_form_validate Form validate callback.
hansel_export_menu Implements hook_menu().
_hansel_dot_link Generate a graphiz node string
_hansel_dot_node Generate a graphiz node string
_hansel_export_dot_node Prepare for graphviz node and link strings
_hansel_export_get Get a dump of the actual Hansel configuration