-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortcut.module
489 lines (448 loc) · 13 KB
/
shortcut.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
/**
* @file
* Provides keyboard shortcuts to the pages.
*
* Develop by Nestor Mata [email protected].
* AchieveInternet http://www.achieveinternet.com.
*/
define('SHORTCUT_TYPE_PATH', 0);
define('SHORTCUT_TYPE_FUNCTION', 1);
define('SHORTCUT_TYPE_EXTERNAL', 2);
define('SHORTCUT_MASK_ALT', 1);
define('SHORTCUT_MASK_CTRL', 2);
define('SHORTCUT_MASK_SHIFT', 4);
define('SHORTCUT_MASK_WIN', 8);
/**
* Node info Hook
*/
function shortcut_node_info() {
return array(
'shortcut' => array(
'name' => t('Keyboard Shortcut'),
'module' => 'shortcut',
'has_title' => true,
'has_body' => false,
'title_label' => t('Description'),
'description' => t("A keyboard shortcut to an internal or external page or a javascript function."),
)
);
}
/**
* Permissions hook
*/
function shortcut_perm() {
return array('admin shortcuts', 'view shortcuts');
}
/**
* Help hook
*/
function shortcut_help($section) {
switch($section) {
case 'admin/settings/shortcut':
return t('Shortcuts enable the posibility to assign keyboard shortcuts to an action or a link.');
}
}
/**
* Access hook
*/
function shortcut_access($op, $node, $account) {
if ($op == 'create') {
return user_access('admin shortcuts', $account);
}
return (user_access('view shortcuts', $account));
}
/**
* Menu Hook
*/
function shortcut_menu() {
// Menu definitions
$items = array();
$items['admin/settings/shortcut'] = array(
'title' => 'Keyboard Shortcuts',
'page callback' => 'shortcut_page_form',
'access arguments' => array('admin shortcuts'),
'description' => t('Administer the keyboard shortcuts that can be used in the site.'),
'type' => MENU_NORMAL_ITEM
);
$items['admin/settings/shortcut/list'] = array(
'title' => 'Keyboard Shortcuts',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/settings/shortcut/add'] = array(
'title' => 'Add new shortcut',
'type' => MENU_LOCAL_TASK,
'access arguments' => array('admin shortcuts'),
'page callback' => 'send_to_node_shortcut'
);
return $items;
}
function send_to_node_shortcut() {
$destination = drupal_get_destination();
drupal_goto('node/add/shortcut',$destination);
}
function shortcut_init() {
// Load scripts
shortcut_load_scripts();
}
/**
* Create the shortcuts page form
*/
function shortcut_page_form() {
$shortcuts = shortcut_get_list();
return theme('shortcut_admin_list', $shortcuts);
}
/**
* Form Hook.
* Definition of the customized form to add/edit shortcuts
*/
function shortcut_form($node) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#required' => TRUE,
'#default_value' => $node->title,
'#description' => t('A description about what this shortcut does.'),
);
$form['key_masks_values'] = array(
'#type' => 'checkboxes',
'#title' => t('Meta Keys'),
'#default_value' => array(
$node->keys_mask & SHORTCUT_MASK_ALT,
$node->keys_mask & SHORTCUT_MASK_CTRL,
$node->keys_mask & SHORTCUT_MASK_SHIFT,
),
'#required' => FALSE,
'#options' => array(
SHORTCUT_MASK_ALT => t('ALT Key'),
SHORTCUT_MASK_CTRL => t('CTRL Key'),
SHORTCUT_MASK_SHIFT => t('SHIFT Key')
),
'#description' => t('You can specify which meta keys are part of the shortcut.'),
);
$form['key'] = array(
'#type' => 'fieldset',
'#title' => t('Key'),
'#description' => t('Key letter or code to use. Only one can be used at the same time.'),
);
$form['key']['char_text'] = array(
'#type' => 'textfield',
'#title' => t('Letter'),
'#default_value' => $node->char_text,
'#maxlength' => 1,
'#size' => 1,
'#required' => FALSE,
'#attributes' => array('style' => 'width: 1em'),
'#description' => t('If the shortcut uses a letter enter it here'),
);
$form['key']['char_code'] = array(
'#type' => 'select',
'#title' => t('Character Code'),
'#default_value' => $node->char_code,
'#required' => FALSE,
'#options' => get_key_codes_array(),
'#description' => t('If the shortcut uses a key code selected it here'),
);
$form['s_type'] = array(
'#type' => 'select',
'#title' => t('Type of action or URL'),
'#default_value' => $node->s_type,
'#required' => TRUE,
'#options' => array(SHORTCUT_TYPE_FUNCTION => t('Call a JS function'), SHORTCUT_TYPE_PATH => t('Go to an internal URL'), SHORTCUT_TYPE_EXTERNAL => t('Go to an external URL')),
'#description' => t('The type of action that will be executed when the shortcut get raised, it can be a call to a javascript function, or go to an internal drupal address or go to an externa internet address.'),
);
$form['s_action'] = array(
'#type' => 'textfield',
'#title' => t('Action to take'),
'#default_value' => $node->s_action,
'#required' => TRUE,
'#description' => t('The action to be taken depends on the type of action, if the type is "Call a JS function" then enter here the name of the function, if the type is an internal address entered here (for example "node/25") and if the type is an external address entered here and include the http://.'),
);
return $form;
}
/**
* Hook Insert
*/
function shortcut_insert($node) {
db_query('
INSERT INTO {shortcut}
(nid, keys_mask, char_text, char_code, s_type, s_action)
VALUES
(%d, %d, \'%s\', %s, %d, \'%s\')
',
$node->nid,
array_sum($node->key_masks_values),
$node->char_text,
(empty($node->char_code) ? ord( strtoupper($node->char_text)) :$node->char_code),
$node->s_type,
$node->s_action
);
}
/**
* Hook update
*/
function shortcut_update($node) {
db_query('
UPDATE {shortcut}
SET keys_mask = %d,
char_text = \'%s\',
char_code = %s,
s_type = %d,
s_action = \'%s\'
WHERE nid = %d
'
, array_sum($node->key_masks_values),
$node->char_text
, (empty($node->char_code)? ord( strtoupper($node->char_code)) : $node->char_code)
, $node->s_type, $node->s_action, $node->nid);
}
/**
* Hook delete
*/
function shortcut_delete($node) {
db_query("DELETE FROM {shortcut} WHERE nid = %d", $node->nid);
}
/**
* Hook load
*/
function shortcut_load($node) {
return db_fetch_object(
db_query('SELECT keys_mask, char_text, char_code, s_type, s_action FROM {shortcut} WHERE nid = %d',
$node->nid));
}
/**
* Hook Block.
* There is a block to show the actual shortcuts.
*/
function shortcut_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0] = array(
'info' => t('The list of shortcuts'),
'weight' => 0,
'enabled' => 1,
'region' => 'right',
);
return $blocks;
break;
case 'view':
switch ($delta) {
case 0:
$block = array(
'subject' => t('Shortcuts'),
'content' => shortcut_display_block_shortcuts(),
);
break;
}
return $block;
break;
}
}
function shortcut_theme(){
$functions = array(
'shortcut_block' => array('arguments' => array('shortcuts' => array())),
'shortcut_admin_list' => array('arguments' => array('shortcuts' => array())),
);
return $functions;
}
/**
* Theme for the shortcuts block.
*/
function theme_shortcut_block($shortcuts = array()) {
$output = '<div class=\'shortcuts-block\'> <ul>';
foreach ($shortcuts as $shortcut) {
$shortcut_string = shortcut_shortcut_to_string($shortcut);
$output .= '<li><span class=\'shortcuts-keys\'>'
.$shortcut_string.':</span> <span class=\'shortcuts-description\'>'
.$shortcut['title'].'</span></li>';
}
$output .= '</ul> </div>';
return $output;
}
function theme_shortcut_admin_list($shortcuts = array()) {
$destination = drupal_get_destination();
if(empty($shortcuts)) {
drupal_set_message(
t('There is not shortcuts created, create one !here',
array('!here' => l('here','node/add/shortcut', array('query' => $destination)))
)
);
}
$headers = array(t('Shortcut'), t('Description'), t('Actions'));
$rows = array();
foreach ($shortcuts as $shortcut) {
$shortcut_string = shortcut_shortcut_to_string($shortcut);
$rows[] = array(
$shortcut_string,
$shortcut['title'],
l(t('Edit'), 'node/' . $shortcut['nid'] . '/edit', array('query' => $destination))
. ' | '
. l(t('Remove'), 'node/' . $shortcut['nid'] . '/delete', array('query' => $destination))
);
}
$output .= theme('table', $headers, $rows, array('class' => 'shortcut_admin_list'));
return $output;
}
/**
* This function return the output for the shortcuts block.
*/
function shortcut_display_block_shortcuts() {
$shortcuts = shortcut_get_list();
$out = theme('shortcut_block', $shortcuts);
return $out;
}
/**
* Get an array of arrays containing the actual shortcuts.
*/
function shortcut_get_list() {
$shortcuts = array();
$res = db_query('
SELECT s.nid, s.keys_mask, s.char_text, s.char_code, s.s_type, s.s_action, n.title
FROM {shortcut} s
INNER JOIN {node_revisions} n ON s.nid = n.nid
');
while ($row = db_fetch_array($res)) {
$shortcuts[] = $row;
}
return $shortcuts;
}
/**
* Loads the javascript required for the shortcut to work on the page.
*/
function shortcut_load_scripts() {
// Load the javascript code
$use_clean_url = variable_get('clean_url', 0);
$path = drupal_get_path('module', 'shortcut');
$shortcuts = array();
// Generate the javascript with the shortcut info.
$shortcuts_script = '';
$res = db_query('
SELECT nid, keys_mask, char_text, char_code, s_type, s_action
FROM {shortcut}
');
while ($row = db_fetch_array($res)) {
$masks = (int) $row['keys_mask'];
$function_call_path = ($row['s_type'] == SHORTCUT_TYPE_EXTERNAL) ? 'shortcut_call_external_path' : 'shortcut_call_internal_path';
$shortcuts[] = array(
'alt' => ( $masks & SHORTCUT_MASK_ALT ) ? 'true' : 'false',
'ctrl' => ( $masks & SHORTCUT_MASK_CTRL) ? 'true' : 'false',
'shift' => ( $masks & SHORTCUT_MASK_SHIFT) ? 'true' : 'false',
'win' => ( $masks & SHORTCUT_MASK_WIN) ? 'true' : 'false',
'char_code' => $row['char_code'],
'func_name' => ($row['s_type'] == SHORTCUT_TYPE_FUNCTION) ? $row['s_action'] : $function_call_path,
'param' => ($row['s_type'] == SHORTCUT_TYPE_FUNCTION) ? '' : ($row['s_type'] == SHORTCUT_TYPE_PATH && !$use_clean_url) ?'?q=':'' . $row['s_action']
);
}
//adding javascript files
drupal_add_js( array('shortcuts' => $shortcuts), 'setting');
drupal_add_js($path . '/shortcut.js', 'core');
}
/**
* Get an associated array with the key code and name for the keys.
*
* @return array key codes and name array
*/
function get_key_codes_array() {
return array(
'' => '',
8 => 'BACKSPACE',
9 => 'TAB',
13 => 'RETURN',
27 => 'ESC',
33 => 'PAGE UP',
34 => 'PAGE DOWN',
35 => 'END',
36 => 'HOME',
112 => 'F1',
113 => 'F2',
114 => 'F3',
115 => 'F4',
116 => 'F5',
117 => 'F6',
118 => 'F7',
119 => 'F8',
120 => 'F9',
121 => 'F10',
122 => 'F11',
123 => 'F12',
65 => 'A',
66 => 'B',
67 => 'C',
68 => 'D',
69 => 'E',
70 => 'F',
71 => 'G',
72 => 'H',
73 => 'I',
74 => 'J',
75 => 'K',
76 => 'L',
77 => 'M',
78 => 'N',
79 => 'O',
80 => 'P',
81 => 'Q',
82 => 'R',
83 => 'S',
84 => 'T',
85 => 'U',
86 => 'V',
87 => 'W',
88 => 'X',
89 => 'Y',
90 => 'Z',
);
}
/**
* This function returns a string with the shortcut formated.
* The format is if any meta key then printed and at the end the key all
* separated with a + sign. For example CTRL+ALT+C.
*/
function shortcut_shortcut_to_string($shortcut = array()) {
$codes_array = get_key_codes_array();
$masks_string = shortcut_get_mask_array($shortcut['keys_mask']);
if ($shortcut['char_code']) {
if (array_key_exists($shortcut['char_code'], $codes_array)) {
$char = $codes_array[$shortcut['char_code']];
}
else {
$char = chr($shortcut['char_code']);
}
}
else {
$char = $shortcut['char_text'];
}
return $masks_string.$char;
}
/**
* Return a string of the meta keys formated from a shortcut.
*/
function shortcut_get_mask_array($mask) {
$masks_string = '';
$masks_array = array(
SHORTCUT_MASK_ALT,
SHORTCUT_MASK_CTRL,
SHORTCUT_MASK_SHIFT,
SHORTCUT_MASK_WIN
);
foreach ($masks_array as $actual_mask) {
switch ($mask & $actual_mask) {
case SHORTCUT_MASK_ALT:
$masked_text = t('ALT') . '+';
break;
case SHORTCUT_MASK_CTRL:
$masked_text = t('CTRL') . '+';
break;
case SHORTCUT_MASK_WIN:
$masked_text = t('WIN') . '+';
break;
case SHORTCUT_MASK_SHIFT:
$masked_text = t('SHIFT') . '+';
break;
default:
$masked_text = '';
}
$masks_string .= $masked_text;
}
return $masks_string;
}