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',
'default_file' => FEATURES_DEFAULTS_INCLUDED_COMMON,
'feature_source' => TRUE,
),
);
}
function themekey_features_rule_chain_features_export_options() {
$options = array();
$rules = themekey_features_load_rule_childs();
if (!empty($rules)) {
foreach ($rules as $rule) {
$string = themekey_features_get_rule_cascade_string($rule);
$options[$string] = $string;
}
}
return $options;
}
function themekey_features_rule_chain_features_export($data, &$export, $module_name = '') {
$export['dependencies']['themekey'] = 'themekey';
$export['dependencies']['themekey_features'] = 'themekey_features';
foreach ($data as $rule_string) {
$export['features']['themekey_features_rule_chain'][$rule_string] = $rule_string;
}
return array();
}
function themekey_features_rule_chain_features_export_render($module_name, $data, $export = NULL) {
$rules = themekey_features_load_rule_childs($module_name);
$keep_rules = array();
foreach ($rules as $rule) {
if (in_array(themekey_features_get_rule_cascade_string($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' => implode("\n", $code),
);
}
function themekey_features_rule_chain_features_enable_feature($module) {
$rules = module_invoke($module, 'themekey_features_rule_chain');
themekey_features_save_rule_childs($module, $rules, TRUE);
}
function themekey_features_rule_chain_features_disable_feature($module) {
module_load_include('inc', 'themekey', 'themekey_build');
$rules = themekey_load_rules();
foreach ($rules as $id => $rule) {
if ($rule['module'] == $module) {
themekey_rule_disable($id);
}
}
}
function themekey_features_rule_chain_features_revert($module) {
themekey_features_graceful_rule_deletion($module);
themekey_features_rule_chain_features_disable_feature($module);
$rules = module_invoke($module, 'themekey_features_rule_chain');
themekey_features_save_rule_childs($module, $rules, TRUE);
}
function themekey_features_graceful_rule_deletion($module) {
module_load_include('inc', 'themekey', 'themekey_build');
$rules = themekey_load_rules();
$delete = TRUE;
while ($delete) {
$delete = FALSE;
foreach ($rules as $id => $rule) {
if ($rule['module'] == $module) {
try {
themekey_rule_del($id);
$delete = TRUE;
} catch (ThemeKeyRuleDeletionException $e) {
continue;
}
}
}
}
}
function themekey_features_load_rule_childs($module_name = '', $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)
->condition('enabled', 1)
->orderBy('weight', 'ASC')
->execute()) {
foreach ($result as $record) {
$rule = themekey_rule_get($record->id);
if (!empty($module_name)) {
$rule->module = $module_name;
}
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($module_name, $record->id),
);
}
}
return $rules;
}
function themekey_features_save_rule_childs($module, $childs, $force = FALSE, $parent = 0, $enabled = 1) {
module_load_include('inc', 'themekey', 'themekey_build');
foreach ($childs as $child) {
$child['rule']['parent'] = $parent;
$id = db_select('themekey_properties', 'tp')
->fields('tp', array(
'id',
))
->condition('property', $child['rule']['property'])
->condition('operator', $child['rule']['operator'])
->condition('value', $child['rule']['value'])
->condition('parent', $child['rule']['parent'])
->condition('module', $module)
->condition('enabled', 0)
->execute()
->fetchField();
if ($id) {
$child['rule']['id'] = $id;
}
try {
themekey_rule_set($child['rule'], $module);
} catch (ThemeKeyRuleConflictException $e) {
if ($force) {
themekey_rule_disable($e
->getCode());
}
else {
$child['rule']['enabled'] = 0;
}
themekey_rule_set($child['rule'], $module);
drupal_set_message(t('ThemeKey rule %rule has been disabled because it conflicts with an existing one.', array(
'%rule' => $child['string'],
)), 'warning');
}
themekey_features_save_rule_childs($module, $child['childs'], $force, $child['rule']['id'], $child['rule']['enabled']);
}
}
function themekey_features_get_rule_cascade_string($rule) {
$string = trim($rule['string'], '"');
if (!empty($rule['childs'])) {
return preg_replace('/>>>.+$/', '>>> ... ' . t('rule cascade'), $string);
}
return $string;
}