View source  
  <?php
function themekey_features_features_api() {
  return array(
    'themekey_features_rule_chain' => array(
      'name' => t('ThemeKey Rule Chain'),
      'default_hook' => 'themekey_features_rule_chain_import',
    ),
  );
}
function themekey_features_rule_chain_features_export_options() {
  $options = array();
  $rules = themekey_features_load_rule_childs();
  if (!empty($rules)) {
    foreach ($rules as $rule) {
      $options[md5(serialize($rule))] = $rule['string'];
    }
  }
  return $options;
}
function themekey_features_rule_chain_features_export($data, &$export, $module_name = '') {
  $export['dependencies']['features'] = 'features';
  $export['dependencies']['themekey_features'] = 'themekey_features';
  
  foreach ($data as $rule_md5) {
    $export['features']['themekey_features_rule_chain'][$rule_md5] = $rule_md5;
  }
  return array();
}
function themekey_features_rule_chain_features_export_render($module_name, $data, $export = NULL) {
  $rules = themekey_features_load_rule_childs();
  $keep_rules = array();
  foreach ($rules as $rule) {
    if (in_array(md5(serialize($rule)), $data)) {
      $keep_rules[] = $rule;
    }
  }
  $code = array();
  $code[] = "if (!defined('THEMEKEY_PAGECACHE_UNSUPPORTED')) {\n    define('THEMEKEY_PAGECACHE_UNSUPPORTED', 0);\n    define('THEMEKEY_PAGECACHE_SUPPORTED', 1);\n    define('THEMEKEY_PAGECACHE_TIMEBASED', 2);\n  }";
  $code[] = '$rules = ' . features_var_export($keep_rules) . ';';
  $code[] = '';
  $code[] = 'return $rules;';
  return array(
    'themekey_features_rule_chain_import' => implode("\n", $code),
  );
}
function themekey_features_rule_chain_features_rebuild($module) {
  module_load_include('inc', 'themekey', 'themekey_build');
  db_truncate('themekey_properties');
  themekey_rebuild();
  $rules = module_invoke($module, 'themekey_features_rule_chain_import');
  themekey_features_save_rule_childs($rules);
  
  cache_clear_all('*', 'cache_page', TRUE);
  return TRUE;
}
function themekey_features_rule_chain_features_revert($module) {
  return themekey_features_rule_chain_features_rebuild($module);
}
function themekey_features_load_rule_childs($parent = 0) {
  module_load_include('inc', 'themekey', 'themekey_base');
  module_load_include('inc', 'themekey', 'themekey_build');
  $rules = array();
  if ($result = db_select('themekey_properties', 'tp')
    ->fields('tp', array(
    'id',
  ))
    ->condition('parent', $parent)
    ->orderBy('weight', 'ASC')
    ->execute()) {
    foreach ($result as $record) {
      
      $rule = themekey_rule_get($record->id);
      if ('drupal:path' != $rule->property) {
        themekey_complete_path($rule);
      }
      unset($rule->id);
      unset($rule->parent);
      unset($rule->weight);
      $rules[] = array(
        'rule' => $rule,
        'string' => themekey_format_rule_as_string($record->id),
        'childs' => themekey_features_load_rule_childs($record->id),
      );
    }
  }
  return $rules;
}
function themekey_features_save_rule_childs($childs, $parent = 0) {
  module_load_include('inc', 'themekey', 'themekey_build');
  foreach ($childs as $child) {
    $child['rule']['parent'] = $parent;
    themekey_rule_set($child['rule']);
    themekey_features_save_rule_childs($child['childs'], $child['rule']['id']);
  }
}