1<?php
2/**
3 * Action Plugin: Inserts a button into the toolbar to add file tags
4 *
5 * @author Heiko Barth
6 */
7
8if (!defined('DOKU_INC')) die();
9if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
10require_once (DOKU_PLUGIN . 'action.php');
11
12class action_plugin_translatebutton extends DokuWiki_Action_Plugin {
13
14    /**
15     * Return some info
16     */
17    function getInfo() {
18        return array (
19            'author' => 'Jumangee',
20            'date' => '2011-10-11',
21            'name' => 'One-click Toolbar Google Translate Button',
22            'desc' => 'Opens a window with google translation of selected words (en-ru)',
23            'url' => '',
24        );
25    }
26
27    /**
28     * Register the eventhandlers
29     */
30    function register(&$controller) {
31        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'translate_button', array ());
32        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_hookjs');
33    }
34
35    /**
36     * Inserts the toolbar button
37     */
38    function translate_button(& $event, $param) {
39        $event->data[] = array (
40            'type' => 'openurlbyselection',
41            'title' => $this->getLang('translate'),
42            'icon' => '../../plugins/translatebutton/pix/translate.png',
43            'url'    => 'http://translate.google.com/#en|ru|{selection}',
44            'name'   => 'translate',
45            'options'=> 'width=750,height=500,left=20,top=20,scrollbars=yes,resizable=yes'
46		);
47    }
48
49     /**
50     * Hook js script into page headers.
51     *
52     * @author Jumangee <zdes@jumangee.net>
53     */
54    function _hookjs(&$event, $param) {
55        $event->data['script'][] = array(
56                     'type'    => 'text/javascript',
57                     'charset' => 'utf-8',
58                     '_data'   => "function tb_openurlbyselection(btn, props, edid) {
59    var sample = props['title'];
60    if(props['sample']){
61        sample = props['sample'];
62    }
63
64    // is something selected?
65    var opts;
66    var selection = getSelection($(edid));
67    if(selection.getLength()){
68        sample = selection.getText();
69        opts = {nosel: true};
70    }else{
71		return false
72    }
73
74    window.open(
75        props['url'].replace(new RegExp('{selection}','g'), sample),
76        props['name'],
77        props['options']);
78    return false;
79}");
80    }
81
82}
83