You are here

function hansel_import_config in Hansel breadcrumbs 7

Same name and namespace in other branches
  1. 8 hansel.export.inc \hansel_import_config()

Import hansel configuration.

Parameters

array $config:

3 calls to hansel_import_config()
hansel_export_import_form_submit in export/hansel_export.module
Form submit callback.
hansel_features_rebuild in ./hansel.features.inc
Implements hook_features_rebuild().
hansel_features_revert in ./hansel.features.inc
Implements hook_features_revert().

File

./hansel.export.inc, line 49

Code

function hansel_import_config($config) {

  // Delete old configuration.
  db_query("DELETE FROM {hansel_rule}");
  db_query("DELETE FROM {hansel_rule_action_goto}");
  db_query("DELETE FROM {hansel_rule_action_leave}");
  db_query("DELETE FROM {hansel_rule_action_switch}");

  // Mapping with old id's as keys and new id's as values.
  $mapping = array();

  // Store all rules without actions and parent id's first, we will update
  // those later cause we don't have a complete mapping here.
  foreach ($config['rules'] as $rule) {
    if (is_array($rule)) {

      // The input can be an array because we use features_var_export().
      $rule = (object) $rule;
    }
    $r = new stdClass();
    $r->name = $rule->name;
    $r->crumb_action = $rule->crumb_action;
    $r->crumb_action_arguments = serialize($rule->crumb_action_arguments);
    drupal_write_record('hansel_rule', $r);
    $mapping[$rule->rid] = $r->rid;
  }

  // Store all actions and update parent id's.
  foreach ($config['rules'] as $rule) {
    if (is_array($rule)) {

      // The input can be an array because we use features_var_export().
      $rule = (object) $rule;
    }
    if (isset($mapping[$rule->pid])) {
      db_query("UPDATE {hansel_rule} SET pid = :pid WHERE rid = :rid", array(
        ':pid' => $mapping[$rule->pid],
        ':rid' => $mapping[$rule->rid],
      ));
    }
    $action = new stdClass();
    $action->rid = $mapping[$rule->rid];
    switch ($rule->action) {
      case 'goto':
        if (isset($mapping[$rule->destination])) {
          $action->destination = $mapping[$rule->destination];
          drupal_write_record('hansel_rule_action_goto', $action);
        }
        break;
      case 'switch':
        $action->handler = $rule->handler;
        $action->arguments = serialize($rule->arguments);
        drupal_write_record('hansel_rule_action_switch', $action);
        break;
      case 'leave':
        $action->restore_original = $rule->restore ? 1 : 0;
        drupal_write_record('hansel_rule_action_leave', $action);
        break;
    }
  }
  $nodetypes = empty($config['nodetypes']) ? array() : $config['nodetypes'];
  variable_set('hansel_nodetypes', $nodetypes);
  cache_clear_all('hansel_config', 'cache');
}