delta.module 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * @file Provide contextual theme settings via context module
  4. */
  5. define('DELTA_PRESERVE', 'preserve');
  6. define('DELTA_OVERRIDE', 'override');
  7. /**
  8. * Implements hook_menu_alter().
  9. */
  10. function delta_menu_alter(&$items) {
  11. foreach (list_themes() as $theme) {
  12. $items['admin/appearance/settings/' . $theme->name]['page arguments'] = array($theme->name);
  13. $items['admin/appearance/settings/' . $theme->name]['page callback'] = 'delta_theme_settings';
  14. }
  15. }
  16. /**
  17. * Implements hook_ctools_plugin_type().
  18. */
  19. function delta_ctools_plugin_type() {
  20. return array(
  21. 'plugins' => array(
  22. 'cache' => TRUE,
  23. 'use hooks' => TRUE,
  24. 'classes' => array('handler'),
  25. 'load themes' => TRUE,
  26. ),
  27. );
  28. }
  29. /**
  30. * Implements hook_delta_plugins().
  31. */
  32. function delta_delta_plugins() {
  33. return array(
  34. 'delta_injection' => array(
  35. 'handler' => array(
  36. 'path' => drupal_get_path('module', 'delta') . '/plugins/delta',
  37. 'file' => 'delta_injection.inc',
  38. 'class' => 'delta_injection',
  39. ),
  40. ),
  41. 'delta_injection_theme_settings' => array(
  42. 'handler' => array(
  43. 'path' => drupal_get_path('module', 'delta') . '/plugins/delta',
  44. 'file' => 'delta_injection_theme_settings.inc',
  45. 'class' => 'delta_injection_theme_settings',
  46. 'parent' => 'delta_injection',
  47. ),
  48. ),
  49. );
  50. }
  51. /**
  52. * Implements hook_context_plugins().
  53. */
  54. function delta_context_plugins() {
  55. return array(
  56. 'context_reaction_delta' => array(
  57. 'handler' => array(
  58. 'path' => drupal_get_path('module', 'delta') . '/plugins/context',
  59. 'file' => 'context_reaction_delta.inc',
  60. 'class' => 'context_reaction_delta',
  61. 'parent' => 'context_reaction',
  62. ),
  63. ),
  64. );
  65. }
  66. /**
  67. * Implements hook_context_regiswaittry().
  68. */
  69. function delta_context_registry() {
  70. return array(
  71. 'reactions' => array(
  72. 'delta' => array(
  73. 'title' => t('Delta'),
  74. 'description' => t('Apply contextual theme settings via the Delta module.'),
  75. 'plugin' => 'context_reaction_delta',
  76. ),
  77. ),
  78. );
  79. }
  80. /**
  81. * Implements hook_context_page_reaction().
  82. */
  83. function delta_context_page_reaction(){
  84. if ($plugin = context_get_plugin('reaction', 'delta')) {
  85. $plugin->execute();
  86. }
  87. }
  88. /**
  89. * Implements hook_load().
  90. */
  91. function delta_load($name) {
  92. ctools_include('export');
  93. if ($template = ctools_export_crud_load('delta', $name)) {
  94. return $template;
  95. }
  96. }
  97. /**
  98. * @todo
  99. */
  100. function delta_load_all($reset = FALSE) {
  101. return ctools_export_crud_load_all('delta', $reset);
  102. }
  103. /**
  104. * @todo
  105. */
  106. function delta_enabled($name) {
  107. $template = delta_load($name);
  108. return $template && (!isset($template->disabled) || !$template->disabled);
  109. }
  110. /**
  111. * @todo
  112. */
  113. function delta_reset($theme) {
  114. if ($current = delta_get_current($theme)) {
  115. foreach (delta_get_plugins($current) as $plugin) {
  116. $plugin->revoke();
  117. }
  118. $pointer = &drupal_static('delta_inject');
  119. $pointer[$theme] = NULL;
  120. module_invoke_all('delta_reset', $theme, $current);
  121. return $current;
  122. }
  123. }
  124. /**
  125. * @todo
  126. */
  127. function delta_get_current($theme) {
  128. $pointer = drupal_static('delta_inject');
  129. if (isset($pointer[$theme])) {
  130. return $pointer[$theme];
  131. }
  132. }
  133. /**
  134. * @todos
  135. */
  136. function delta_inject($name) {
  137. if ($name && $delta = delta_load($name)) {
  138. $pointer = &drupal_static(__FUNCTION__);
  139. $before = delta_reset($delta->theme);
  140. foreach (delta_get_plugins($name) as $plugin) {
  141. $plugin->inject();
  142. }
  143. $pointer[$delta->theme] = $name;
  144. module_invoke_all('delta_inject', $delta->theme, $name);
  145. return $before;
  146. }
  147. }
  148. /**
  149. * @todo
  150. */
  151. function delta_get_plugin($name, $plugin) {
  152. if ($plugins = delta_get_plugins($name)) {
  153. if (isset($plugins[$plugin])) {
  154. return $plugins[$plugin];
  155. }
  156. }
  157. return FALSE;
  158. }
  159. /**
  160. * @todo
  161. */
  162. function delta_get_plugins($name) {
  163. $cache = &drupal_static(__FUNCTION__);
  164. if (!isset($cache[$name])) {
  165. $cache[$name] = array();
  166. ctools_include('plugins');
  167. foreach (ctools_get_plugins('delta', 'plugins') as $plugin) {
  168. if ($plugin['name'] != 'delta_injection') {
  169. if ($class = ctools_plugin_get_class($plugin, 'handler')) {
  170. $cache[$name][$plugin['name']] = new $class($plugin, $name);
  171. }
  172. }
  173. }
  174. drupal_alter('delta_plugins', $cache[$name], $name);
  175. }
  176. return $cache[$name];
  177. }
  178. /**
  179. * @todo
  180. */
  181. function delta_variable_set($name, $value) {
  182. $original = isset($GLOBALS['conf'][$name]) ? $GLOBALS['conf'][$name] : NULL;
  183. $GLOBALS['conf'][$name] = $value;
  184. return $original;
  185. }
  186. /**
  187. * @todo
  188. */
  189. function delta_ancestors($name) {
  190. $ancestors = array();
  191. if ($template = delta_load($name)) {
  192. $ancestors[$template->machine_name] = $template;
  193. if (!empty($template->parent)) {
  194. $ancestors = array_merge($ancestors, delta_ancestors($template->parent));
  195. }
  196. }
  197. return $ancestors;
  198. }
  199. /**
  200. * @todo
  201. */
  202. function delta_merge($first, $second) {
  203. if (is_array($first) && is_array($second)) {
  204. return array_merge($first, $second);
  205. }
  206. if (is_object($first) && is_object($second)) {
  207. return (object) array_merge((array) $first, (array) $second);
  208. }
  209. return $second;
  210. }
  211. /**
  212. * @todo
  213. */
  214. function delta_reduce(array $first, array $second) {
  215. if ($first === $second) {
  216. return NULL;
  217. }
  218. foreach ($first as $name => $value) {
  219. if (($first[$name] === NULL && !isset($second[$name])) || (isset($second[$name]) && $value === $second[$name])) {
  220. unset($first[$name]);
  221. }
  222. }
  223. return $first;
  224. }
  225. /**
  226. * @todo
  227. */
  228. function delta_theme_settings($key) {
  229. $backup = delta_reset($key);
  230. $form = drupal_get_form('system_theme_settings', $key);
  231. delta_inject($backup);
  232. return $form;
  233. }