better_messages.module 6.6 KB

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