| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
|
| 5 |
/**
|
| 6 |
* @file
|
| 7 |
* Double Click module adds behaviors for double clicking on links in Drupal
|
| 8 |
*
|
| 9 |
* by Jeff Robbins | Lullabot.com
|
| 10 |
*
|
| 11 |
* @todo
|
| 12 |
* - submit ideas to http://drupal.org/project/dblclick
|
| 13 |
*
|
| 14 |
* @bugs
|
| 15 |
* - does a complete page reload when (single) clicking on anchor links - browser bug?
|
| 16 |
*/
|
| 17 |
|
| 18 |
/**
|
| 19 |
* Implementation of hook_help().
|
| 20 |
*/
|
| 21 |
function dblclick_help($section) {
|
| 22 |
switch ($section) {
|
| 23 |
case 'admin/help#dblclick':
|
| 24 |
return t('<p>This simple module has no configuration settings. It only implements
|
| 25 |
<a href="@access">one permission</a>, but it allows users with proper permissions
|
| 26 |
to double-click on a node, user, or comment link to "jump" to the edit page for that item.
|
| 27 |
The javascript "magic" will not be added to the page if the user does not have "double click"
|
| 28 |
permission. The module also checks to see that they have access to edit the node, user, or
|
| 29 |
comment before it redirects them to edit form.</p>
|
| 30 |
<p>This module also implements an API hook to allow other modules to create their own double-click
|
| 31 |
behaviors.</p>
|
| 32 |
', array('@access' => url('admin/user/access')));
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Implementation of hook_init().
|
| 38 |
*/
|
| 39 |
function dblclick_init() {
|
| 40 |
if ($_GET['dbl'] == 'true' && user_access('double click')) {
|
| 41 |
// modules can create a "hook_dblclick"
|
| 42 |
$results = module_invoke_all('dblclick');
|
| 43 |
if (!in_array(FALSE, $results)) {
|
| 44 |
// if we get back here, then none of the other modules
|
| 45 |
// have done a drupal_goto() and none have returned FALSE
|
| 46 |
// So we get rid of the "muck" in the URL
|
| 47 |
// by redirecting to the current page.
|
| 48 |
// I'm not sure if this is a good idea or not...
|
| 49 |
$dest = $_REQUEST['destination'] ? 'destination='. urlencode($_REQUEST['destination']) : NULL;
|
| 50 |
unset($_REQUEST['destination']);
|
| 51 |
drupal_goto($_GET['q'], $dest);
|
| 52 |
}
|
| 53 |
}
|
| 54 |
}
|
| 55 |
|
| 56 |
function dblclick_perm() {
|
| 57 |
return array('double click');
|
| 58 |
}
|
| 59 |
|
| 60 |
|
| 61 |
/**
|
| 62 |
* Implementation of hook_menu().
|
| 63 |
*/
|
| 64 |
function dblclick_menu($may_cache) {
|
| 65 |
$items = array();
|
| 66 |
|
| 67 |
if (!$may_cache) {
|
| 68 |
if (user_access('double click')) {
|
| 69 |
// add the dblclick.js file
|
| 70 |
$path = drupal_get_path('module', 'dblclick');
|
| 71 |
drupal_add_js($path .'/dblclick.js');
|
| 72 |
|
| 73 |
// add a little bit of "extra" information for javascript to use
|
| 74 |
$data = array(
|
| 75 |
'dblClick' => array(
|
| 76 |
'dblDest' => '&dbl'. drupal_get_destination(),
|
| 77 |
'dblTime' => variable_get('dblclick_time', 300)
|
| 78 |
)
|
| 79 |
);
|
| 80 |
drupal_add_js($data, 'setting');
|
| 81 |
}
|
| 82 |
}
|
| 83 |
return $items;
|
| 84 |
}
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Implementation of hook_dblclick()
|
| 88 |
*
|
| 89 |
* @abstract
|
| 90 |
* Double clicking on an link simply adds a few $_GET arguments to the URL.
|
| 91 |
* For this reason, we need to interpret the behavior *after* the link has
|
| 92 |
* already been followed. This hook gets fired when the dbl=true value is
|
| 93 |
* found. This hook takes no arguments and returns no value. Modules
|
| 94 |
* implementing this hook should examine the current path to determine what
|
| 95 |
* was double-clicked on and then (most likely) use drupal_goto() to send
|
| 96 |
* the user somewhere else.
|
| 97 |
*
|
| 98 |
* Implementations should examine the path arguments to figure out the current page
|
| 99 |
* and on and then do a drupal_goto (or some other behavior)
|
| 100 |
* as the double-click behavior.
|
| 101 |
*
|
| 102 |
* If you are NOT doing a drupal_goto() in your implementation
|
| 103 |
* you should return FALSE from this function so that dblclick.module
|
| 104 |
* does not do it's URL "clean up" refresh of the page
|
| 105 |
*
|
| 106 |
* Some helpful variables include:
|
| 107 |
* - $_GET['shift']
|
| 108 |
* This value is 'true' if shift key was held down when user was double-clicking
|
| 109 |
* - $_GET['alt']
|
| 110 |
* This value is 'true' if alt key was held down when user was double-clicking
|
| 111 |
* - $_GET['frag']
|
| 112 |
* If the link contained a fragment (#), then this variable contains it.
|
| 113 |
* Useful to make sure that the user hasn't clicked on a comment link.
|
| 114 |
* - $_GET['dbldestination']
|
| 115 |
* The on which the double-click happened. Good to return users here after editing.
|
| 116 |
*
|
| 117 |
* And if you're using this, I'd love to know about it! Send me a note:
|
| 118 |
* http://drupal.org/user/17190/contact
|
| 119 |
*
|
| 120 |
*/
|
| 121 |
function dblclick_dblclick() {
|
| 122 |
global $user;
|
| 123 |
if (!$_GET['frag']) { // don't do any of this if they've clicked on a comment
|
| 124 |
switch (TRUE) {
|
| 125 |
// double clicked on node/123
|
| 126 |
case (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2) && (user_access('administer nodes') || node_access('update', node_load(arg(1))))) :
|
| 127 |
// double clicked on 'My account'
|
| 128 |
case (arg(0) == 'user' && arg(1) == $user->uid && !arg(2)) :
|
| 129 |
// double clicked on user/123
|
| 130 |
case (arg(0) == 'user' && is_numeric(arg(1)) && !arg(2) && user_access('administer users')) :
|
| 131 |
// the redirect for all of these uses the same scheme
|
| 132 |
$dest = isset($_GET['dbldestination']) ? 'destination='. urlencode($_GET['dbldestination']) : NULL;
|
| 133 |
drupal_goto(arg(0) .'/'. arg(1) . '/edit', $dest);
|
| 134 |
break;
|
| 135 |
}
|
| 136 |
}
|
| 137 |
else {
|
| 138 |
// okay, there's a fragment... is it a comment?
|
| 139 |
$frag = $_GET['frag'];
|
| 140 |
if (strpos($frag, 'comment-') === 0) {
|
| 141 |
$cid = substr($frag, 8);
|
| 142 |
if (is_numeric($cid)) {
|
| 143 |
$comment = _comment_load($cid);
|
| 144 |
if (comment_access('edit', $comment)) {
|
| 145 |
$dest = isset($_GET['dbldestination']) ? 'destination='. urlencode($_GET['dbldestination']) : NULL;
|
| 146 |
drupal_goto('comment/edit/'. $cid, $dest);
|
| 147 |
break;
|
| 148 |
}
|
| 149 |
}
|
| 150 |
}
|
| 151 |
}
|
| 152 |
}
|