Bug 16154: CGI->multi_param - Force scalar context
[koha.git] / cataloguing / value_builder / EXAMPLE.pl
1 #!/usr/bin/perl
2
3 # Copyright 2014 Rijksmuseum
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use C4::Auth;
23 use C4::Output;
24
25 # Example of framework plugin new style.
26 # It should define and return at least one and normally two anynomous
27 # subroutines in a hash ref.
28 # REQUEST: If you copy this code to construct a new plugin, please REMOVE
29 # all comments copied from this file.
30
31 # The first one is the builder: it returns javascript code for the plugin.
32 # The second one is the launcher: it runs the popup and will normally have an
33 # associated HTML template.
34
35 # We start with the example builder:
36 # It contains code for five events: Focus, MouseOver, KeyPress, Change and Click
37 # You could also use: Blur. Or: keydown, keyup.
38 # Or: mouseout, mousedown, mouseup, mousemove.
39 # Only define what you actually need!
40
41 # The builder receives a parameters hashref from the calling plugin object.
42 # Available parameters are listed in FrameworkPlugin.pm, but by far the only
43 # one interesting is id: it contains the html id of the field controlled by
44 # this plugin.
45 #
46 # The plugin returns javascript code. Note that the function names are made
47 # unique by appending the id. You should use the event names as listed above
48 # (upper or lowercase does not matter). The plugin object takes care of
49 # binding the function to the actual event. When doing so, it passes the id
50 # into the event data parameter; Focus e.g. uses that one again by looking at
51 # the variable event.data.id.
52 #
53 # Do not use the perl variable $id to extract the field value. Use variable
54 # event.data.id. This makes a difference when the field is cloned or has
55 # been created dynamically (as in additem.js).
56
57 my $builder= sub {
58     my $params = shift;
59     my $id = $params->{id};
60
61     return qq|
62 <script type="text/javascript">
63 function Focus$id(event) {
64     if( \$('#'+event.data.id).val()=='' ) {
65         \$('#'+event.data.id).val('EXAMPLE:');
66     }
67 }
68
69 function MouseOver$id(event) {
70     return Focus$id(event);
71     // just redirecting it to Focus for the same effect
72 }
73
74 function KeyPress$id(event) {
75     if( event.which == 64 ) { // at character
76         var f= \$('#'+event.data.id).val();
77         \$('#'+event.data.id).val( f + 'AT' );
78         return false; // prevents getting the @ character back too
79     }
80 }
81
82 function Change$id(event) {
83     var colors= [ 'rgb(0, 0, 255)', 'rgb(0, 128, 0)', 'rgb(255, 0, 0)' ];
84     var curcol= \$('#'+event.data.id).css('color');
85     var i= Math.floor( Math.random() * 3 );
86     if( colors[i]==curcol ) {
87         i= (i + 1)%3;
88     }
89     var f= \$('#'+event.data.id).css('color',colors[i]);
90 }
91
92 function Click$id(event) {
93     var fieldvalue=\$('#'+event.data.id).val();
94     window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=EXAMPLE.pl&index=\"+event.data.id+\"&result=\"+fieldvalue,\"tag_editor\",'width=700,height=700,toolbar=false,scrollbars=yes');
95     return false; // prevents scrolling
96 }
97 </script>|;
98 };
99 # NOTE: Did you see the last semicolon? This was just an assignment!
100
101 # We continue now with the example launcher.
102 # It receives a CGI object via the parameter hashref (from plugin_launcher.pl).
103 # It also receives index (the html id of the input field) and result (the
104 # value of the input field). See also the URL in the Click function above.
105
106 # In this example we just pass those two fields to the template and call
107 # the output_html routine. But you could do some processing in perl before
108 # showing the template output.
109 # When you look at the template EXAMPLE.tt, you can see that the javascript
110 # code there puts a new value back into the input field (referenced by index).
111
112 my $launcher= sub {
113     my $params = shift;
114     my $cgi = $params->{cgi};
115     my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
116         template_name => "cataloguing/value_builder/EXAMPLE.tt",
117         query => $cgi,
118         type => "intranet",
119         authnotrequired => 0,
120         flagsrequired => {editcatalogue => '*'},
121     });
122     $template->param(
123         index => scalar $cgi->param('index'),
124         result => scalar $cgi->param('result'),
125     );
126     output_html_with_http_headers $cgi, $cookie, $template->output;
127 };
128
129 # Return the hashref with the builder and launcher to FrameworkPlugin object.
130 # NOTE: If you do not need a popup but only use e.g. Focus, Blur etc. for a
131 # particular plugin, you only need to define and return the builder.
132 return { builder => $builder, launcher => $launcher };