themekey_features.module

Tracking 6.x-4.x branch
  1. drupal
    1. 6 contributions/themekey/themekey_features.module

Functions & methods

NameDescription
themekey_features_features_apiImplementation of hook_features_api().
themekey_features_load_rule_childsLoads current ThemeKey Rule Chain as array.
themekey_features_rule_chain_features_exportImplementation of hook_features_export().
themekey_features_rule_chain_features_export_optionsImplementation of hook_features_export_options().
themekey_features_rule_chain_features_export_renderImplementation of hook_features_export_render().
themekey_features_rule_chain_features_rebuildImplementation of hook_features_rebuild().
themekey_features_rule_chain_features_revertImplementation of hook_features_revert().
themekey_features_save_rule_childsTakes a serialized ThemeKey Rule Chain as created by themekey_features_load_rule_childs() and replaces the current one in the database eith it.

File

View source
  1. <?php
  2. /**
  3. * Implementation of hook_features_api().
  4. */
  5. function themekey_features_features_api() {
  6. return array(
  7. 'themekey_features_rule_chain' => array(
  8. 'name' => t('ThemeKey Rule Chain'),
  9. 'default_hook' => 'themekey_features_rule_chain_import'
  10. ),
  11. );
  12. }
  13. /**
  14. * Implementation of hook_features_export_options().
  15. */
  16. function themekey_features_rule_chain_features_export_options() {
  17. $options = array();
  18. $rules = themekey_features_load_rule_childs();
  19. if (!empty($rules)) {
  20. foreach ($rules as $rule) {
  21. $options[md5(serialize($rule))] = $rule['string'];
  22. }
  23. }
  24. return $options;
  25. }
  26. /**
  27. * Implementation of hook_features_export().
  28. */
  29. function themekey_features_rule_chain_features_export($data, &$export, $module_name = '') {
  30. $export['dependencies']['features'] = 'features';
  31. $export['dependencies']['themekey_features'] = 'themekey_features';
  32. // TODO set dependencies to providers of each single themekey property
  33. foreach ($data as $rule_md5) {
  34. $export['features']['themekey_features_rule_chain'][$rule_md5] = $rule_md5;
  35. }
  36. return array();
  37. }
  38. /**
  39. * Implementation of hook_features_export_render().
  40. */
  41. function themekey_features_rule_chain_features_export_render($module_name, $data, $export = NULL) {
  42. $rules = themekey_features_load_rule_childs();
  43. $keep_rules = array();
  44. foreach ($rules as $rule) {
  45. if (in_array(md5(serialize($rule)), $data)) {
  46. $keep_rules[] = $rule;
  47. }
  48. }
  49. $code = array();
  50. $code[] = "if (!defined('THEMEKEY_PAGECACHE_UNSUPPORTED')) {
  51. define('THEMEKEY_PAGECACHE_UNSUPPORTED', 0);
  52. define('THEMEKEY_PAGECACHE_SUPPORTED', 1);
  53. define('THEMEKEY_PAGECACHE_TIMEBASED', 2);
  54. }";
  55. $code[] = '$rules = '. features_var_export($keep_rules) .';';
  56. $code[] = '';
  57. $code[] = 'return $rules;';
  58. return array('themekey_features_rule_chain_import' => implode("\n", $code));
  59. }
  60. /**
  61. * Implementation of hook_features_rebuild().
  62. */
  63. function themekey_features_rule_chain_features_rebuild($module) {
  64. module_load_include('inc', 'themekey', 'themekey_build');
  65. db_query('TRUNCATE {themekey_properties}');
  66. themekey_rebuild();
  67. $rules = module_invoke($module, 'themekey_features_rule_chain_import');
  68. themekey_features_save_rule_childs($rules);
  69. // fast deletion of page cache (truncate)
  70. cache_clear_all('*', 'cache_page', TRUE);
  71. return TRUE;
  72. }
  73. /**
  74. * Implementation of hook_features_revert().
  75. */
  76. function themekey_features_rule_chain_features_revert($module) {
  77. return themekey_features_rule_chain_features_rebuild($module);
  78. }
  79. /**
  80. * Loads current ThemeKey Rule Chain as array.
  81. *
  82. * @param $parent
  83. * internal use in recursion
  84. *
  85. * @return
  86. * serialized ThemeKey Rule Chain as array
  87. */
  88. function themekey_features_load_rule_childs($parent = 0) {
  89. module_load_include('inc', 'themekey', 'themekey_base');
  90. module_load_include('inc', 'themekey', 'themekey_build');
  91. $rules = array();
  92. if ($result = db_query("SELECT id FROM {themekey_properties} WHERE parent = %d ORDER BY weight", $parent)) {
  93. while ($item = db_fetch_array($result)) {
  94. // we have to load the rule again using themekey_rule_get() which applies some transformations
  95. $rule = themekey_rule_get($item['id']);
  96. if ('drupal:path' != $rule['property']) {
  97. themekey_complete_path($rule);
  98. }
  99. unset($rule['id']);
  100. unset($rule['parent']);
  101. unset($rule['weight']);
  102. $rules[] = array('rule' => $rule, 'string' => themekey_format_rule_as_string($item['id']), 'childs' => themekey_features_load_rule_childs($item['id']));
  103. }
  104. }
  105. return $rules;
  106. }
  107. /**
  108. * Takes a serialized ThemeKey Rule Chain as created by
  109. * themekey_features_load_rule_childs() and replaces the current
  110. * one in the database eith it.
  111. *
  112. * @param $childs
  113. * serialized ThemeKey Rule Chain as array
  114. * @param $parent
  115. * internal use in recursion
  116. */
  117. function themekey_features_save_rule_childs($childs, $parent = 0) {
  118. module_load_include('inc', 'themekey', 'themekey_build');
  119. foreach ($childs as $child) {
  120. $child['rule']['parent'] = $parent;
  121. themekey_rule_set($child['rule']);
  122. themekey_features_save_rule_childs($child['childs'], $child['rule']['id']);
  123. }
  124. }