Bug 26922: Regression tests
[koha.git] / cataloguing / value_builder / EXAMPLE.pl
blob2e13d6ad86261c38e560e9c57937751e8b57aed5
1 #!/usr/bin/perl
3 # Copyright 2014 Rijksmuseum
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use C4::Auth;
23 use C4::Output;
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.
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.
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!
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.
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.
53 # Comments in JavaScript must be multi-line style ( /* ... */ ) in case
54 # the JavaScript block is collapsed by the template
56 # Do not use the perl variable $id to extract the field value. Use variable
57 # event.data.id. This makes a difference when the field is cloned or has
58 # been created dynamically (as in additem.js).
60 my $builder= sub {
61 my $params = shift;
62 my $id = $params->{id};
64 return qq|
65 <script>
66 function Focus$id(event) {
67 if( \$('#'+event.data.id).val()=='' ) {
68 \$('#'+event.data.id).val('EXAMPLE:');
72 function MouseOver$id(event) {
73 return Focus$id(event);
74 /* just redirecting it to Focus for the same effect */
77 function KeyPress$id(event) {
78 if( event.which == 64 ) { /* at character */
79 var f= \$('#'+event.data.id).val();
80 \$('#'+event.data.id).val( f + 'AT' );
81 return false; /* prevents getting the @ character back too */
85 function Change$id(event) {
86 var colors= [ 'rgb(0, 0, 255)', 'rgb(0, 128, 0)', 'rgb(255, 0, 0)' ];
87 var curcol= \$('#'+event.data.id).css('color');
88 var i= Math.floor( Math.random() * 3 );
89 if( colors[i]==curcol ) {
90 i= (i + 1)%3;
92 var f= \$('#'+event.data.id).css('color',colors[i]);
95 function Click$id(event) {
96 var fieldvalue=\$('#'+event.data.id).val();
97 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');
98 return false; /* prevents scrolling */
100 </script>|;
102 # NOTE: Did you see the last semicolon? This was just an assignment!
104 # We continue now with the example launcher.
105 # It receives a CGI object via the parameter hashref (from plugin_launcher.pl).
106 # It also receives index (the html id of the input field) and result (the
107 # value of the input field). See also the URL in the Click function above.
109 # In this example we just pass those two fields to the template and call
110 # the output_html routine. But you could do some processing in perl before
111 # showing the template output.
112 # When you look at the template EXAMPLE.tt, you can see that the javascript
113 # code there puts a new value back into the input field (referenced by index).
115 my $launcher= sub {
116 my $params = shift;
117 my $cgi = $params->{cgi};
118 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
119 template_name => "cataloguing/value_builder/EXAMPLE.tt",
120 query => $cgi,
121 type => "intranet",
122 flagsrequired => {editcatalogue => '*'},
124 $template->param(
125 index => scalar $cgi->param('index'),
126 result => scalar $cgi->param('result'),
128 output_html_with_http_headers $cgi, $cookie, $template->output;
131 # Return the hashref with the builder and launcher to FrameworkPlugin object.
132 # NOTE: If you do not need a popup but only use e.g. Focus, Blur etc. for a
133 # particular plugin, you only need to define and return the builder.
134 return { builder => $builder, launcher => $launcher };