cobalt.module 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Implementation of hook_init().
  4. */
  5. function cobalt_init() {
  6. if (user_access('use cobalt')) {
  7. global $user;
  8. if (variable_get('cobalt_jquery_hotkeys', 1)) {
  9. drupal_add_library('cobalt', 'jquery.hotkeys');
  10. }
  11. drupal_add_js(drupal_get_path('module', 'cobalt') . '/js/cobalt.js');
  12. drupal_add_js(drupal_get_path('module', 'cobalt') . '/js/cobalt.menu.js');
  13. $settings = array(
  14. 'state' => $user->uid,
  15. 'path' => $_GET['q'],
  16. 'bindings' => preg_split('/,\s*/', variable_get('cobalt_shortcuts', 'Alt+space, Ctrl+space')),
  17. );
  18. drupal_add_js(array('cobalt' => $settings), 'setting');
  19. $themes = cobalt_themes();
  20. $theme_key = variable_get('cobalt_theme', '');
  21. foreach ($themes as $key => $info) {
  22. if ($key==$theme_key) {
  23. $theme = $info;
  24. break;
  25. }
  26. }
  27. if (!isset($theme) || !isset($theme['css']) || !isset($theme['replace']) || !$theme['replace']) {
  28. drupal_add_css(drupal_get_path('module', 'cobalt') . '/css/cobalt.css');
  29. }
  30. if (isset($theme) && isset($theme['css'])) {
  31. if (is_array($theme['css'])) {
  32. foreach ($theme['css'] as $css) {
  33. drupal_add_css($css);
  34. }
  35. }
  36. else {
  37. drupal_add_css($theme['css']);
  38. }
  39. }
  40. }
  41. }
  42. /**
  43. * Implements hook_library().
  44. */
  45. function cobalt_library() {
  46. // Use the original version by John Resig since the latest version at
  47. // https://github.com/tzuryby/jquery.hotkeys doesn't work, probably due
  48. // to incompatibility with jQuery 1.4.4.
  49. $libraries['jquery.hotkeys'] = array(
  50. 'title' => 'jQuery Hotkeys',
  51. 'website' => 'https://github.com/jeresig/jquery.hotkeys',
  52. 'version' => '0.8',
  53. 'js' => array(
  54. drupal_get_path('module', 'cobalt') . '/js/jquery.hotkeys.js' => array(),
  55. ),
  56. );
  57. return $libraries;
  58. }
  59. function cobalt_themes() {
  60. static $themes;
  61. if (!$themes) {
  62. $themes = array(
  63. '' => array(
  64. 'name' => t('Default theme'),
  65. ),
  66. );
  67. drupal_alter('cobalt_themes', $themes);
  68. }
  69. return $themes;
  70. }
  71. function cobalt_cobalt_themes_alter(&$themes) {
  72. $themes['cobalt_blue'] = array(
  73. 'name' => t('Cobalt blue'),
  74. 'css' => drupal_get_path('module', 'cobalt') . '/css/cobalt_blue.css',
  75. 'replace' => FALSE,
  76. );
  77. }
  78. /**
  79. * Implementation of hook_perm().
  80. */
  81. function cobalt_permissions() {
  82. return array(
  83. 'use cobalt' => array(
  84. 'title' => t('Enables Cobalt for the user'),
  85. ),
  86. );
  87. }
  88. /**
  89. * Implementation of hook_menu().
  90. */
  91. function cobalt_menu() {
  92. $items = array();
  93. $items['cobalt/alias'] = array(
  94. 'title' => 'Forward to the correct alias',
  95. 'page callback' => 'cobalt_forward_to_alias',
  96. 'access arguments' => array('use cobalt'),
  97. 'type' => MENU_CALLBACK,
  98. );
  99. $items['cobalt/data/menu_json'] = array(
  100. 'title' => 'Serialized menu',
  101. 'page callback' => 'cobalt_menu_json',
  102. 'access arguments' => array('use cobalt'),
  103. 'type' => MENU_CALLBACK,
  104. );
  105. $items['admin/config/system/cobalt'] = array(
  106. 'title' => 'Cobalt',
  107. 'description' => 'Configure Cobalt appearance and behaviour.',
  108. 'page callback' => 'drupal_get_form',
  109. 'page arguments' => array('cobalt_settings'),
  110. 'access arguments' => array('administer site configuration'),
  111. 'file' => 'cobalt_admin.inc.php',
  112. 'type' => MENU_NORMAL_ITEM,
  113. 'weight' => 0,
  114. );
  115. $items['cobalt/update'] = array(
  116. 'title' => 'Cobalt update',
  117. 'page callback' => 'cobalt_js_update',
  118. 'page arguments' => array('cobalt_settings'),
  119. 'access arguments' => array('use cobalt'),
  120. 'file' => 'cobalt_update.inc.php',
  121. 'type' => MENU_CALLBACK,
  122. 'weight' => 0,
  123. );
  124. // Menu callback for clearing the cache even when the devel module
  125. // isn't installed.
  126. $items['cobalt/clear-cache'] = array(
  127. 'title' => t('Clear cache'),
  128. 'page callback' => '_cobalt_clear_cache',
  129. 'file' => 'cobalt_admin.inc.php',
  130. 'access arguments' => array('administer site configuration'),
  131. 'type' => MENU_CALLBACK,
  132. );
  133. // Menuitem for rebuilding content permissions.
  134. $items['cobalt/rebuild-permissions'] = array(
  135. 'title' => t('Rebuild permissions'),
  136. 'page callback' => '_cobalt_rebuild_permissions',
  137. 'file' => 'cobalt_admin.inc.php',
  138. 'access arguments' => array('administer site configuration'),
  139. 'type' => MENU_CALLBACK,
  140. );
  141. return $items;
  142. }
  143. function cobalt_forward_to_alias() {
  144. $url_parts = array();
  145. $i = 2;
  146. while (arg($i)) {
  147. $url_parts[] = arg($i);
  148. $i++;
  149. }
  150. $path = join('/', $url_parts);
  151. drupal_goto($path);
  152. }
  153. function cobalt_menu_json() {
  154. $menu_names = array_keys(menu_get_menus());
  155. $tree = array();
  156. foreach ($menu_names as $name) {
  157. $tree += menu_tree_all_data($name);
  158. }
  159. $data = array();
  160. _cobalt_menu_data_recursive($tree, $data);
  161. drupal_alter('cobalt_menu', $data);
  162. print drupal_json_output($data);
  163. exit;
  164. }
  165. function _cobalt_menu_data_recursive($tree, &$data) {
  166. foreach ($tree as $key => $item) {
  167. $link = $item['link'];
  168. if ($link['hidden'] !== 1 && $link['access'] && !($link['type'] & MENU_LINKS_TO_PARENT) && $link['type'] !== MENU_VISIBLE_IN_BREADCRUMB) {
  169. $data[$link['mlid']] = array($link['href'], $link['title']);
  170. if ($item['below']) {
  171. _cobalt_menu_data_recursive($item['below'], $data);
  172. }
  173. }
  174. }
  175. }
  176. /**
  177. * Implementation of hook_cobalt_menu_alter().
  178. */
  179. function cobalt_cobalt_menu_alter(&$menuitems) {
  180. if (!module_exists('devel') && user_access('administer site configuration')) {
  181. $menuitems['cobalt-clear-cache'] = array('cobalt/clear-cache', t('Clear cache'));
  182. }
  183. if (user_access('administer site configuration')) {
  184. $menuitems['cobalt-rebuild-permissions'] = array('cobalt/rebuild-permissions', t('Rebuild permissions'));
  185. }
  186. }