| 1 |
<?php
|
| 2 |
// $Id: jgrowl.module,v 1.6 2008/12/12 22:10:45 stevemckenzie Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
*
|
| 6 |
* @file
|
| 7 |
* jGrowl is a jQuery plugin that raises unobtrusive messages within the browser, similar to the way that OS X's Growl Framework works.
|
| 8 |
*
|
| 9 |
* - Override status messages to be displayed on jGrowl.
|
| 10 |
* - An API to use with Drupal
|
| 11 |
* - A settings page for easy configuration of all options available in jgrowl.
|
| 12 |
* - A hook_jgrowl_variable_alter(&$variables) to alter / add variables on the settings form and on runtime.
|
| 13 |
*
|
| 14 |
*/
|
| 15 |
|
| 16 |
define('JGROWL_SYSTEM_MESSAGES', variable_get('jgrowl_system_messages', TRUE));
|
| 17 |
define('JGROWL_STICKY_ERRORS', variable_get('jgrowl_sticky_errors', TRUE));
|
| 18 |
define('JGROWL_STICKY_STATUSES', variable_get('jgrowl_sticky_statuses', FALSE));
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Implementation of hook_perm().
|
| 22 |
*/
|
| 23 |
function jgrowl_perm() {
|
| 24 |
return array('access jgrowl', 'administer jgrowl');
|
| 25 |
}
|
| 26 |
|
| 27 |
/**
|
| 28 |
* Implementation of hook_menu().
|
| 29 |
*/
|
| 30 |
function jgrowl_menu() {
|
| 31 |
$items = array();
|
| 32 |
|
| 33 |
$items['admin/settings/jgrowl'] = array(
|
| 34 |
'title' => t('jGrowl'),
|
| 35 |
'page callback' => 'drupal_get_form',
|
| 36 |
'page arguments' => array('jgrowl_settings_form'),
|
| 37 |
'access callback' => 'user_access',
|
| 38 |
'access arguments' => array('administer jgrowl'),
|
| 39 |
);
|
| 40 |
|
| 41 |
return $items;
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
* Implementation of hook_theme().
|
| 46 |
*/
|
| 47 |
function jgrowl_theme() {
|
| 48 |
return array(
|
| 49 |
'jgrowl_message' => array(
|
| 50 |
'arguments' => array('message' => NULL, 'options' => array()),
|
| 51 |
),
|
| 52 |
);
|
| 53 |
}
|
| 54 |
|
| 55 |
/**
|
| 56 |
* Implementation of hook_footer().
|
| 57 |
*/
|
| 58 |
function jgrowl_footer($main = 0) {
|
| 59 |
jgrowl_system_messages();
|
| 60 |
}
|
| 61 |
|
| 62 |
/**
|
| 63 |
* Includes all that is needed.
|
| 64 |
*
|
| 65 |
* @return void - uses drupal_dd_js () / drupal_add_css().
|
| 66 |
*/
|
| 67 |
function jgrowl_include() {
|
| 68 |
static $include;
|
| 69 |
|
| 70 |
if (!$include) {
|
| 71 |
// Handle the various ways jgrowl comes packaged.
|
| 72 |
// There are 3 types:
|
| 73 |
// 1) full (dev version, full source)
|
| 74 |
// 2) compressed
|
| 75 |
// 3) minimized
|
| 76 |
$file = 'jquery.jgrowl';
|
| 77 |
$path = drupal_get_path('module', 'jgrowl');
|
| 78 |
$type = variable_get('jgrowl_file_type', 'compressed');
|
| 79 |
if ($type != 'full') {
|
| 80 |
$file .= '_'. $type;
|
| 81 |
}
|
| 82 |
drupal_add_js($path. '/jgrowl/'. $file .'.js', 'module', 'footer');
|
| 83 |
|
| 84 |
// TODO: make theme-able.
|
| 85 |
drupal_add_css($path .'/jgrowl/jquery.jgrowl.css');
|
| 86 |
drupal_add_css($path .'/jgrowl.css');
|
| 87 |
|
| 88 |
$include = TRUE;
|
| 89 |
}
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
* Themable jQuery jGrowl snippet.
|
| 94 |
*
|
| 95 |
* @param $message
|
| 96 |
* The message string that is being displayed in jGrowl.
|
| 97 |
*
|
| 98 |
* @param $options
|
| 99 |
* Extra jGrowl options to pass to the JS layer.
|
| 100 |
*
|
| 101 |
* @return string - JavaScript snippet of a jGrowl instance.
|
| 102 |
*/
|
| 103 |
function theme_jgrowl_message($message, $options = array()) {
|
| 104 |
$variables = array_merge(jgrowl_variables(), $options);
|
| 105 |
$defaults = jgrowl_default_variables();
|
| 106 |
|
| 107 |
// No need to add items that are already using their default values.
|
| 108 |
foreach ($defaults as $name => $default) {
|
| 109 |
if ($name && $variables[$name] === $default) {
|
| 110 |
unset($variables[$name]);
|
| 111 |
}
|
| 112 |
}
|
| 113 |
|
| 114 |
return '$.jGrowl("'. str_replace("\n", "<br />", addslashes($message)) .'", '. jgrowl_to_js($variables) .'); ';
|
| 115 |
}
|
| 116 |
|
| 117 |
/**
|
| 118 |
* Module settings form.
|
| 119 |
*
|
| 120 |
* This form is also setup to provide a configurable settings area for other settings that modules implement via the hook_jgrowl_variable_alter().
|
| 121 |
*/
|
| 122 |
function jgrowl_settings_form() {
|
| 123 |
$form['jgrowl_file_type'] = array(
|
| 124 |
'#title' => t('File type'),
|
| 125 |
'#description' => t('The file type provided by jGrowl that you wish to use as the included .js for jGrowl.'),
|
| 126 |
'#type' => 'select',
|
| 127 |
'#options' => array('full' => t('Full'), 'compressed' => t('Compressed'), 'minimized' => t('Minimized')),
|
| 128 |
'#default_value' => variable_get('jgrowl_file_type', 'compressed'),
|
| 129 |
);
|
| 130 |
|
| 131 |
$form['jgrowl_system_messages'] = array(
|
| 132 |
'#title' => t('Override system messages'),
|
| 133 |
'#description' => t('Override all system messages that drupal usually prints inline and display them in jGrowl notifications.'),
|
| 134 |
'#type' => 'checkbox',
|
| 135 |
'#default_value' => JGROWL_SYSTEM_MESSAGES,
|
| 136 |
);
|
| 137 |
|
| 138 |
$form['jgrowl_sticky_errors'] = array(
|
| 139 |
'#title' => t('Sticky error messages'),
|
| 140 |
'#description' => t('Automatically sticky error messages even if sticky is not enabled below.'),
|
| 141 |
'#type' => 'checkbox',
|
| 142 |
'#default_value' => JGROWL_STICKY_ERRORS,
|
| 143 |
);
|
| 144 |
|
| 145 |
$form['jgrowl_sticky_statuses'] = array(
|
| 146 |
'#title' => t('Sticky status messages'),
|
| 147 |
'#description' => t('Automatically sticky status messages even if sticky is not enabled below.'),
|
| 148 |
'#type' => 'checkbox',
|
| 149 |
'#default_value' => JGROWL_STICKY_STATUSES,
|
| 150 |
);
|
| 151 |
|
| 152 |
$form['variables'] = array('#title' => t('Configuration'), '#type' => 'fieldset', '#collapsible' => TRUE, '#description' => t('For more information on the variables available and what they do, visit the !site.', array('!site' => l(t('plugin\'s site'), 'http://stanlemon.net/projects/jgrowl.html'))));
|
| 153 |
|
| 154 |
foreach (jgrowl_variables('settings') as $variable => $value) {
|
| 155 |
// Leave room for more custom fields.
|
| 156 |
// TODO: make the settings $op of the hook able to control this?
|
| 157 |
switch ($variable) {
|
| 158 |
case 'position':
|
| 159 |
$type = 'select';
|
| 160 |
$options = array('top-left' => t('Top left'), 'top-right' => t('Top right'), 'bottom-left' => t('Bottom left'), 'bottom-right' => t('Bottom right'), 'center' => t('Center'));
|
| 161 |
$options = array('#options' => $options);
|
| 162 |
break;
|
| 163 |
|
| 164 |
default:
|
| 165 |
$type = 'textfield';
|
| 166 |
$options = array();
|
| 167 |
break;
|
| 168 |
}
|
| 169 |
|
| 170 |
$form['variables']['jgrowl__'. $variable] = array_merge(array(
|
| 171 |
'#title' => $variable,
|
| 172 |
'#type' => $type,
|
| 173 |
'#default_value' => $value,
|
| 174 |
), $options);
|
| 175 |
}
|
| 176 |
|
| 177 |
return system_settings_form($form);
|
| 178 |
}
|
| 179 |
|
| 180 |
/**
|
| 181 |
* Handle altering the system messages and displaying them in jgrowl.
|
| 182 |
*
|
| 183 |
* @return void - uses drupal_add_js() to change Drupal's core messages to jGrowl.
|
| 184 |
*/
|
| 185 |
function jgrowl_system_messages() {
|
| 186 |
if (JGROWL_SYSTEM_MESSAGES && !empty($_SESSION['messages'])) {
|
| 187 |
jgrowl_include();
|
| 188 |
|
| 189 |
// Remove messages from the session and display them with growl.
|
| 190 |
$js = '';
|
| 191 |
foreach ($_SESSION['messages'] as $type => $messages) {
|
| 192 |
foreach ($messages as $key => $message) {
|
| 193 |
$options = array('theme' => $type);
|
| 194 |
switch ($type) {
|
| 195 |
case 'status':
|
| 196 |
if (JGROWL_STICKY_STATUSES) {
|
| 197 |
$options['sticky'] = TRUE;
|
| 198 |
}
|
| 199 |
break;
|
| 200 |
case 'error':
|
| 201 |
if (JGROWL_STICKY_ERRORS) {
|
| 202 |
$options['sticky'] = TRUE;
|
| 203 |
}
|
| 204 |
break;
|
| 205 |
}
|
| 206 |
|
| 207 |
$js .= theme('jgrowl_message', $message, $options);
|
| 208 |
unset($_SESSION['messages'][$type][$key]);
|
| 209 |
}
|
| 210 |
unset($_SESSION['messages'][$type]);
|
| 211 |
}
|
| 212 |
drupal_add_js("$(document).ready(function() { $js });", 'inline', 'footer');
|
| 213 |
}
|
| 214 |
}
|
| 215 |
|
| 216 |
/**
|
| 217 |
* Converts a PHP variable into its Javascript equivalent.
|
| 218 |
*
|
| 219 |
* This is a clone of drupal_to_js() but we removed a few extra security measures to avoid JS errors.
|
| 220 |
*
|
| 221 |
* @param variable
|
| 222 |
* Whatever variable to convert.
|
| 223 |
*
|
| 224 |
* @return string - json output.
|
| 225 |
*/
|
| 226 |
function jgrowl_to_js($var) {
|
| 227 |
// Force a numeric check because of how forgiving PHP can be.
|
| 228 |
if (is_numeric($var)) {
|
| 229 |
$type = 'integer';
|
| 230 |
} else {
|
| 231 |
$type = gettype($var);
|
| 232 |
}
|
| 233 |
|
| 234 |
switch ($type) {
|
| 235 |
case 'boolean':
|
| 236 |
return $var ? 'true' : 'false'; // Lowercase necessary!
|
| 237 |
|
| 238 |
case 'integer':
|
| 239 |
case 'double':
|
| 240 |
return $var;
|
| 241 |
|
| 242 |
case 'resource':
|
| 243 |
case 'string':
|
| 244 |
return '"'. $var .'"';
|
| 245 |
case 'array':
|
| 246 |
// Arrays in JSON can't be associative. If the array is empty or if it
|
| 247 |
// has sequential whole number keys starting with 0, it's not associative
|
| 248 |
// so we can go ahead and convert it as an array.
|
| 249 |
if (empty ($var) || array_keys($var) === range(0, sizeof($var) - 1)) {
|
| 250 |
$output = array();
|
| 251 |
foreach ($var as $v) {
|
| 252 |
$output[] = jgrowl_to_js($v);
|
| 253 |
}
|
| 254 |
return '[ '. implode(', ', $output) .' ]';
|
| 255 |
}
|
| 256 |
// Otherwise, fall through to convert the array as an object.
|
| 257 |
case 'object':
|
| 258 |
$output = array();
|
| 259 |
foreach ($var as $k => $v) {
|
| 260 |
$output[] = jgrowl_to_js(strval($k)) .': '. jgrowl_to_js($v);
|
| 261 |
}
|
| 262 |
return '{ '. implode(', ', $output) .' }';
|
| 263 |
|
| 264 |
// Handle items already in JS format.
|
| 265 |
case substr($var, 0, 1) == '{' && substr($var, (strlen($var) - 1)) == '}':
|
| 266 |
return $var;
|
| 267 |
|
| 268 |
// Handle custom JS functions.
|
| 269 |
case substr($var, 0, strlen('function')) == 'function':
|
| 270 |
return $var;
|
| 271 |
|
| 272 |
default:
|
| 273 |
return 'null';
|
| 274 |
}
|
| 275 |
}
|
| 276 |
|
| 277 |
/**
|
| 278 |
* Function to retrieve the jgrowl config variables.
|
| 279 |
*
|
| 280 |
* @param: $op
|
| 281 |
* A string of the operation the variables are currently on.
|
| 282 |
* - load - on load time.
|
| 283 |
* - settings - when the settings form is presented.
|
| 284 |
*
|
| 285 |
* @return: struct of variables.
|
| 286 |
*/
|
| 287 |
function jgrowl_variables($op = 'load') {
|
| 288 |
$variables = array();
|
| 289 |
|
| 290 |
$defaults = jgrowl_default_variables();
|
| 291 |
|
| 292 |
// TODO: i added the extra __ for safety for now. is this how i want to handle having them 'separate' as specific variables to pass to JS?
|
| 293 |
foreach ($defaults as $name => $value) {
|
| 294 |
$variables[$name] = variable_get('jgrowl__'. $name, $value);
|
| 295 |
}
|
| 296 |
|
| 297 |
// A hook to alter or add to the variables array.
|
| 298 |
foreach (module_implements('jgrowl_variable_alter') as $module) {
|
| 299 |
$function = $module .'_jgrowl_variable_alter';
|
| 300 |
$function($op, $variables);
|
| 301 |
}
|
| 302 |
|
| 303 |
return $variables;
|
| 304 |
}
|
| 305 |
|
| 306 |
/**
|
| 307 |
* A function to store all the default variables available in a jgrowl instance.
|
| 308 |
* TODO: redo this to be more dynamic instead of hardcoded in?
|
| 309 |
*
|
| 310 |
* @return struct of default variables.
|
| 311 |
*/
|
| 312 |
function jgrowl_default_variables() {
|
| 313 |
return array(
|
| 314 |
'header' => '',
|
| 315 |
'sticky' => 'false',
|
| 316 |
'glue' => 'after',
|
| 317 |
'position' => 'top-right',
|
| 318 |
'theme' => 'default',
|
| 319 |
'corners' => '10px',
|
| 320 |
'check' => '1000',
|
| 321 |
'life' => '3000',
|
| 322 |
'speed' => 'normal',
|
| 323 |
'easing' => 'swing',
|
| 324 |
'closer' => 'true',
|
| 325 |
'closerTemplate' => '<div>[ close all ]</div>',
|
| 326 |
'log' => 'function(e,m,o) {}',
|
| 327 |
'beforeOpen' => 'function(e,m,o) {}',
|
| 328 |
'open' => 'function(e,m,o) {}',
|
| 329 |
'beforeClose' => 'function(e,m,o) {}',
|
| 330 |
'close' => 'function(e,m,o) {}',
|
| 331 |
'animateOpen' => '{ opacity: "show" }',
|
| 332 |
'animateClose' => '{ opacity: "hide" }',
|
| 333 |
);
|
| 334 |
}
|
| 335 |
|