better_messages.module 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /*
  3. Implementation of hook_init
  4. */
  5. function better_messages_init() {
  6. drupal_add_css(drupal_get_path('module', 'better_messages') . '/better_messages_admin.css');
  7. }
  8. /*
  9. Implementation of hook_menu
  10. */
  11. function better_messages_menu() {
  12. $items = array();
  13. $items['admin/config/user-interface/better-messages'] = array(
  14. 'title' => t('Better Messages settings'),
  15. 'description' => t('Better handling of Drupal messages UI'),
  16. 'page callback' => 'drupal_get_form',
  17. 'page arguments' => array('better_messages_admin'),
  18. 'access arguments' => array('administer better messages'),
  19. 'file' => 'better_messages.inc',
  20. );
  21. return $items;
  22. }
  23. /**
  24. * Implementation of hook_perm().
  25. */
  26. function better_messages_permission() {
  27. return array(
  28. 'access better messages' => array(
  29. 'title' => t('access better messages'),
  30. 'description' => t('access better messages.'),
  31. ),
  32. 'administer better messages' => array(
  33. 'title' => t('administer better messages'),
  34. 'description' => t('administer better messages.'),
  35. ),
  36. );
  37. }
  38. /*
  39. Implementation of hook_theme
  40. */
  41. function better_messages_theme($existing, $type, $theme, $path) {
  42. return array(
  43. 'better_messages_content' => array(
  44. 'arguments' => array('display' => NULL),
  45. ),
  46. 'better_messages' => array(
  47. 'template' => 'better_messages',
  48. 'arguments' => array('content' => NULL),
  49. ),
  50. );
  51. }
  52. /*
  53. Implementation of hook_theme_registry_alter to add better_messages theme function
  54. */
  55. function better_messages_theme_registry_alter(&$theme_registry) {
  56. $theme_registry['status_messages']['function'] = "theme_better_messages";
  57. }
  58. function theme_better_messages($display = NULL) {
  59. $output = '';
  60. $better_messages = better_messages_process_visibility();
  61. $access = user_access('access better messages');
  62. $message = drupal_get_messages($display['display'], FALSE);
  63. if ($better_messages && $access && !empty($message)) {
  64. $disable_autoclose = array_key_exists('error', $message);
  65. better_messages_add_files($disable_autoclose);
  66. $message = theme('better_messages_content', $display['display']);
  67. // We save the intial output to SESSION so we can put it in <noscript> on page_preprocess
  68. $_SESSION['original_messages'] = $message;
  69. $message_skin = theme('better_messages', array('content' => $message));
  70. // We add a special ID so that JavaScript knows what to handle. Changing the ID will break JavaScript handling code.
  71. $output = "<div id=\"better-messages-wrapper\" style=\"display:none;overflow:visible;position:absolute;z-index:9999;\">\n".
  72. $message_skin ."\n</div>\n";
  73. }
  74. else { $output = theme('better_messages_content', $display['display']);}
  75. return $output;
  76. }
  77. function theme_better_messages_content($display = NULL) {
  78. $output = '';
  79. $first = TRUE;
  80. foreach (drupal_get_messages($display) as $type => $messages) {
  81. $class = $first ? 'first' : '';
  82. $first = FALSE;
  83. $output .= "<h2 class=\"messages-label $type\">" . t(drupal_ucfirst($type)) . "</h2>\n";
  84. $output .= "<div class=\"messages $type\">\n";
  85. if (count($messages) > 1) {
  86. $output .= " <ul>\n";
  87. foreach ($messages as $k => $message) {
  88. if ($k == 0) { $output .= "<li class='message-item first'>$message</li>"; }
  89. else if ($k == count($messages) - 1) { $output .= "<li class='message-item last'>$message</li>"; }
  90. else { $output .= "<li class='message-item'>$message</li>"; }
  91. }
  92. $output .= " </ul>\n";
  93. }
  94. else { $output .= $messages[0]; }
  95. $output .= "</div>\n";
  96. }
  97. return $output;
  98. }
  99. /*
  100. Preprocess the default status_messages so we add our template file here
  101. */
  102. function better_messages_preprocess_page(&$vars) {
  103. if (isset($vars['messages'])) {
  104. $settings = better_messages_get_settings();
  105. // Check if path Better Messages is enabled for this path
  106. $better_messages = better_messages_process_visibility();
  107. $access = user_access('access better messages');
  108. if ($better_messages && $access) {
  109. $message = $vars['messages'];
  110. // Here comes the graceful degrade for browsers with JS disabled
  111. $vars['messages'] = "<noscript>\n" . $_SESSION['original_messages'] . "\n</noscript>\n";
  112. unset($_SESSION['original_messages']);
  113. $vars['closure'] .= $message;
  114. }
  115. }
  116. }
  117. /*
  118. Helper function to get the settings
  119. */
  120. function better_messages_get_settings() {
  121. $settings = array(
  122. 'position' => 'center', 'vertical' => 0, 'horizontal' => 10, 'fixed' => 1,
  123. 'width' => '400px', 'autoclose' => 0, 'opendelay' => 0.3, 'disable_autoclose' => 0,
  124. 'show_countdown' => 1, 'hover_autoclose' => 1,
  125. 'popin' => array('effect' => 'fadeIn', 'duration' => 'slow'),
  126. 'popout' => array('effect' => 'fadeOut', 'duration' => 'slow'),
  127. 'jquery_ui' => array('draggable' => 0, 'resizable' => 0),
  128. 'extra' => array(
  129. 'pages' => '',
  130. 'visibility' => 0,
  131. )
  132. );
  133. return variable_get('better_messages', $settings);
  134. }
  135. /*
  136. Helper function to check where to popup Better Messages relative to path
  137. */
  138. function better_messages_process_visibility() {
  139. $settings = better_messages_get_settings();
  140. if ($settings['extra']['pages']) {
  141. $path = drupal_get_path_alias($_GET['q']);
  142. $page_match = drupal_match_path($path, $settings['extra']['pages']);
  143. if ($path != $_GET['q']) {
  144. $page_match = $page_match || drupal_match_path($_GET['q'], $settings['extra']['pages']);
  145. }
  146. $page_match = !($settings['extra']['visibility'] xor $page_match);
  147. }
  148. else { $page_match = TRUE; }
  149. return $page_match;
  150. }
  151. /*
  152. Helper function to add required JS and CSS only when required
  153. */
  154. function better_messages_add_files($disable_autoclose){
  155. $settings = better_messages_get_settings();
  156. drupal_add_css(drupal_get_path('module', 'better_messages') . '/skins/default/better_messages.css');
  157. drupal_add_js(drupal_get_path('module', 'better_messages') . '/better_messages.js');
  158. // Remove extra info from JS
  159. if (!module_exists('jquery_ui')) {
  160. unset($settings['jquery_ui']);
  161. }
  162. else {
  163. foreach ($settings['jquery_ui'] as $jquery_ui_setting => $value) {
  164. if ($value == 1) {
  165. jquery_ui_add(array('ui.' . $jquery_ui_setting));
  166. }
  167. }
  168. }
  169. unset($settings['extra']);
  170. if ($disable_autoclose) {
  171. $settings['autoclose'] = 0;
  172. }
  173. drupal_add_js(array('betterMessages' => $settings), 'setting');
  174. }