Bug 27643: Add CodeMirror custom syntax highlighting for SQL runtime parameters
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / modules / reports / guided_reports_start.tt
1 [% USE raw %]
2 [% USE Asset %]
3 [% USE KohaDates %]
4 [% USE Koha %]
5 [% USE TablesSettings %]
6 [% USE JSON.Escape %]
7 [% SET footerjs = 1 %]
8
9 [%- BLOCK area_name -%]
10     [%- SWITCH area -%]
11         [%- CASE 'CIRC' -%]Circulation
12         [%- CASE 'CAT'  -%]Catalog
13         [%- CASE 'PAT'  -%]Patrons
14         [%- CASE 'ACQ'  -%]Acquisitions
15         [%- CASE 'ACC'  -%]Accounts
16         [%- CASE 'SER'  -%]Serials
17     [%- END -%]
18 [%- END -%]
19
20 [% INCLUDE 'doc-head-open.inc' %]
21
22 <title>
23     Koha &rsaquo; Reports &rsaquo; Guided reports wizard
24     [%- IF ( saved1 ) -%]
25         &rsaquo; Saved reports
26     [%- ELSIF ( create ) -%]
27         &rsaquo; Create from SQL
28     [%- ELSIF ( showsql ) -%]
29         &rsaquo; Saved reports &rsaquo; SQL view
30     [%- ELSIF ( execute ) -%]
31         &rsaquo; Saved reports &rsaquo; Report [% name | html %] ([% id | html %])
32     [%- ELSIF ( editsql ) -%]
33         &rsaquo; Saved reports &rsaquo; Edit report [% reportname | html %] ([% id | html %])
34     [%- END -%]
35     [%- IF ( build1 ) -%]
36         &rsaquo; Build a report, step 1 of 6: Choose a module
37     [%- ELSIF ( build2 ) -%]
38         &rsaquo; Build a report, step 2 of 6: Pick a report type
39     [%- ELSIF ( build3 ) -%]
40         &rsaquo; Build a report, step 3 of 6: Select columns for display
41     [%- ELSIF ( build4 ) -%]
42         &rsaquo; Build a report, step 4 of 6: Select criteria to limit on
43     [%- ELSIF ( build5 ) -%]
44         &rsaquo; Build a report, step 5 of 6: Pick which columns to total
45     [%- ELSIF ( build6 ) -%]
46         &rsaquo; Build a report, step 6 of 6: Select how you want the report ordered
47     [%- END -%]
48 </title>
49
50 [% INCLUDE 'doc-head-close.inc' %]
51 [% Asset.css("lib/codemirror/codemirror.min.css") | $raw %]
52 <style>
53     .CodeMirror {
54         resize:  vertical;
55     }
56     .cm-sqlParams {
57         color: #11917B;
58     }
59     #mana_search_errortext { font-family: monospace; font-weight: bold; }
60 </style>
61 [% Asset.css("css/reports.css") | $raw %]
62 [% Asset.css("lib/d3c3/c3.min.css") | $raw %]
63 </head>
64
65 <body id="rep_guided_reports_start" class="rep">
66 [% INCLUDE 'header.inc' %]
67 [% INCLUDE 'circ-search.inc' %]
68
69 <div id="breadcrumbs">
70     <a href="/cgi-bin/koha/mainpage.pl">Home</a>
71     &rsaquo; <a href="/cgi-bin/koha/reports/reports-home.pl">Reports</a>
72     &rsaquo; <a href="/cgi-bin/koha/reports/guided_reports.pl">Guided reports wizard</a>
73
74     [% IF ( saved1 ) %]
75         &rsaquo; Saved reports
76     [% ELSIF ( create ) %]
77         &rsaquo; Create from SQL
78     [% ELSIF ( showsql ) %]
79         &rsaquo; <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved">Saved reports</a> &rsaquo; [% reportname | html %] ([% id | html %])
80     [% ELSIF ( editsql ) %]
81         &rsaquo; <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved">Saved reports</a> &rsaquo; <a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% id | uri %]&amp;phase=Show%20SQL">[% reportname | html %] ([% id | html %])</a> &rsaquo; Edit
82     [% ELSIF ( execute ) %]
83         &rsaquo; <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved">Saved reports</a> &rsaquo; <a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% id | uri %]&amp;phase=Show%20SQL">[% name | html %] ([% id | html %])</a> &rsaquo; Run
84     [% ELSIF ( build1 || build2 || build3 || build4 || build5 || build6 ) %]
85         &rsaquo; <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Build%20new">Build a report</a>
86         [% IF ( build1 ) %]
87             &rsaquo; Step 1 of 6: Choose a module
88         [% ELSIF ( build2 ) %]
89             &rsaquo; Step 2 of 6: Pick a report type
90         [% ELSIF ( build3 ) %]
91             &rsaquo; Step 3 of 6: Select columns for display
92         [% ELSIF ( build4 ) %]
93             &rsaquo; Step 4 of 6: Select criteria to limit on
94         [% ELSIF ( build5 ) %]
95             &rsaquo; Step 5 of 6: Pick which columns to total
96         [% ELSIF ( build6 ) %]
97             &rsaquo; Step 6 of 6: Select how you want the report ordered
98         [% END %]
99     [% END %]
100 </div> <!-- /#breadcrumbs -->
101
102 <div id="update_sql" class="modal" tabindex="-1" role="dialog" aria-labelledby="update_sql_label" aria-hidden="true">
103     <div class="modal-dialog">
104         <div class="modal-content">
105             <div class="modal-header">
106                 <button type="button" class="closebtn" data-dismiss="modal" aria-hidden="true">×</button>
107                 <h3 id="update_sql_label">Update SQL</h3>
108             </div>
109             <div class="modal-body">
110                 <div id="loading"> <img src="[% interface | html %]/[% theme | html %]/img/spinner-small.gif" alt="" /> Loading </div>
111             </div>
112             <div class="modal-footer">
113                 <a href="#" class="btn btn-default" id="update_sql_button" role="button" data-toggle="modal">Update</a>
114                 <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Close</button>
115             </div>
116         </div> <!-- /.modal-content -->
117     </div> <!-- /.modal-dialog -->
118 </div> <!-- #update_sql -->
119
120 <div class="main container-fluid">
121     <div class="row">
122         <div class="col-sm-10 col-sm-push-2">
123             <main>
124
125                 [% INCLUDE "reports-toolbar.inc" %]
126
127                 [% IF ( start ) %]
128                     <h2>Guided reports</h2>
129                     <p>Use the guided reports engine to create non standard reports. This feature aims to provide some middle ground between the built in canned reports and writing custom SQL reports.</p>
130
131                     <h3>Build and run reports</h3>
132
133                     [% IF ( CAN_user_reports_create_reports ) %]
134                         <form action="/cgi-bin/koha/reports/guided_reports.pl">
135                             <input type="hidden" name="phase" value="Build new" />
136                             <input type="submit" name="submit" value="Build new"/>
137                         </form>
138                     [% END %]
139
140                     [% IF ( CAN_user_reports_execute_reports ) %]
141                         <form action="/cgi-bin/koha/reports/guided_reports.pl">
142                             <input type="hidden" name="phase" value="Use saved"/>
143                             <input type="submit" name="submit" value="Use saved"/>
144                         </form>
145                     [% END %]
146
147                     [% IF ( CAN_user_reports_create_reports ) %]
148                         <form action="/cgi-bin/koha/reports/guided_reports.pl">
149                             <input type="hidden" name="phase" value="Create report from SQL"/>
150                             <input type="submit" name="submit" value="Create report from SQL"/>
151                         </form>
152                     [% END %]
153
154                     <h3>Reports Dictionary</h3>
155                     <p>Use the reports dictionary to define custom criteria to use in your reports</p>
156                     <form action="/cgi-bin/koha/reports/dictionary.pl">
157                         <input type="hidden" name="phase" value="View Dictionary"/>
158                         <input type="submit" name="submit" value="View dictionary"/>
159                     </form>
160                 [% END # /IF (start) %]
161
162                 [% IF report_converted %]
163                     <div class="dialog message">
164                         The report "[% report_converted | html %]" has been converted.
165                     </div>
166                 [% END %]
167
168                 [% IF report_converted %]
169                     <div class="dialog message">
170                         The report "[% report_converted | html %]" has been converted.
171                     </div>
172                 [% END %]
173
174                 [% IF ( saved1 ) %]
175                     [% IF ( savedreports ) %]
176                         <h1>Saved reports</h1>
177
178                         [% IF ( filters.date || filters.author || filters.keyword ) %]
179                             <p>Filtered by:
180                                 <span class="filter">
181                                     [% IF ( filters.date ) %]
182                                         <span class="filter_date"><strong>Date:</strong> [% filters.date | html %]</span>
183                                     [% END %]
184                                     [% IF ( filters.author ) %]
185                                         <span class="filter_author"><strong>Author:</strong> [% filters.author | html %]</span>
186                                     [% END %]
187                                     [% IF ( filters.keyword ) %]
188                                         <span class="filter_keyword"><strong>Keyword:</strong> [% filters.keyword | html %]</span>
189                                     [% END %]
190                                     <a class="clear_filter" href="/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved&clear_filters=1"><i class="fa fa-remove"></i> Clear</a>
191                                 </span>
192                             </p>
193                         [% END %]
194
195                         <div id="tabs" class="toptabs">
196                             <ul>
197                                 <li><a href="#reports">All</a></li>
198                                 [% FOREACH group IN groups_with_subgroups %]
199                                     <li><a id="[% group.id | html %]" href="#reports">[% group.name | html %]</a></li>
200                                 [% END %]
201                             </ul>
202                             <div id="reports">
203                                 <div id="subgroup_filter_block">
204                                     <label for="subgroup_filter">Subgroup:</label>
205                                     <select id="subgroup_filter">
206                                         <option value="">All</option>
207                                     </select>
208                                 </div>
209
210                                 [% IF (Koha.Preference('Mana') == 1) %]
211                                     [% IF manamsg %]
212                                         <div id="mana_search_message" class="dialog message">
213                                             <p> [% manamsg | html %] </p>
214                                         </div>
215                                     [% END %]
216                                 [% END %]
217
218                                 <form action="/cgi-bin/koha/reports/guided_reports.pl" id="reports_form" method="post">
219                                     <input type="hidden" name="phase" value="Delete Multiple" />
220                                     <table id="table_reports">
221                                         <thead>
222                                             <tr>
223                                                 <th class="NoSort">&nbsp;</th>
224                                                 <th>ID</th>
225                                                 <th>Report name</th>
226                                                 <th>Type</th>
227                                                 <th>Group</th>
228                                                 <th>Subgroup</th>
229                                                 <th>Notes</th>
230                                                 <th>Author</th>
231                                                 <th class="title-string">Creation date</th>
232                                                 <th class="title-string">Last edit</th>
233                                                 <th class="title-string">Last run</th>
234                                                 <th class="report_public">Public</th>
235                                                 <th class="report_json_url">JSON URL</th>
236                                                 [% IF (usecache) %]
237                                                     <th>Cache expiry (seconds)</th>
238                                                 [% ELSE %]
239                                                     <th class="hidden">&nbsp;</th>
240                                                 [% END %]
241                                                 <th>Saved results</th>
242                                                 [% IF has_obsolete_reports %]
243                                                     <th>Update</th>
244                                                 [% ELSE %]
245                                                     <th class="hidden">&nbsp;</th>
246                                                 [% END %]
247                                                 <th class="NoSort noExport">Actions</th>
248                                             </tr>
249                                         </thead>
250                                         <tbody>
251                                             [% FOREACH savedreport IN savedreports %]
252                                                 [% UNLESS ( loop.odd ) %]<tr class="odd">[% ELSE %]<tr>[% END %]
253                                                     <td class="report_checkbox">
254                                                         [% IF ( CAN_user_reports_delete_reports ) %] <!-- not break CSS -->
255                                                             <input type="checkbox" name="ids" id="ids[% savedreport.id | html %]" value="[% savedreport.id | html %]" />
256                                                         [% END %]
257                                                         <input type="hidden" class="report_sql" value="[% savedreport.savedsql |html %]">
258                                                     </td>
259                                                     <td class="report_id">
260                                                         <label for="ids[% savedreport.id | html %]">[% savedreport.id | html %]</label>
261                                                     </td>
262                                                     <td class="report_name">
263                                                         [% IF ( savedreport.report_name ) %]
264                                                             [% savedreport.report_name | html %]
265                                                         [% ELSE %]
266                                                             [ no name ]
267                                                         [% END %]
268                                                     </td>
269                                                     <td class="report_type">
270                                                         [% savedreport.type | html %]
271                                                     </td>
272                                                     <td class="report_group">
273                                                         [% savedreport.groupname | html %]
274                                                     </td>
275                                                     <td>
276                                                         [% savedreport.subgroupname | html %]
277                                                     </td>
278                                                     <td class="report_notes">
279                                                         [% savedreport.notes | html %]
280                                                     </td>
281                                                     <td>
282                                                         [% savedreport.borrowersurname | html %]
283                                                         [% IF ( savedreport.borrowerfirstname ) %]
284                                                             , [% savedreport.borrowerfirstname | html %]
285                                                         [% END %]
286                                                         ([% savedreport.borrowernumber | html %])
287                                                     </td>
288                                                     <td>
289                                                         <span title="[% savedreport.date_created | html %]">[% savedreport.date_created | $KohaDates %]</span>
290                                                     </td>
291                                                     <td>
292                                                         <span title="[% savedreport.last_modified | html %]">[% savedreport.last_modified | $KohaDates  with_hours => 1 %]</span>
293                                                     </td>
294                                                     <td>
295                                                         <span title="[% savedreport.last_run | html %]">[% savedreport.last_run | $KohaDates  with_hours => 1 %]</span>
296                                                     </td>
297                                                     <td class="report_public">
298                                                         [% IF (savedreport.public) %]
299                                                             Yes
300                                                         [% ELSE %]
301                                                             No
302                                                         [% END %]
303                                                     </td>
304                                                     <td class="report_json_url">
305                                                         [% IF (savedreport.public) %]
306                                                             <a href="[% OPACBaseURL | url %]/cgi-bin/koha/svc/report?id=[% savedreport.id | uri %]">[% OPACBaseURL | html %]/cgi-bin/koha/svc/report?id=[% savedreport.id | html %]</a>
307                                                         [% ELSE %]
308                                                             <a href="/cgi-bin/koha/svc/report?id=[% savedreport.id | uri %]">[% Koha.Preference('staffClientBaseURL') | html %]/cgi-bin/koha/svc/report?id=[% savedreport.id | html %]</a>
309                                                         [% END %]
310                                                     </td>
311                                                     <td>
312                                                         [% savedreport.cache_expiry | html %]
313                                                     </td>
314                                                     <td>
315                                                         [% FOR result IN savedreport.results %]
316                                                             <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&amp;id=[% result.id | uri %]">[% result.date_run | html %]</a>
317                                                             <br/>
318                                                         [% END %]
319                                                     </td>
320                                                     <td>
321                                                         [% IF savedreport.seems_obsolete %]
322                                                             This report seems obsolete, it uses biblioitems.marcxml field.
323                                                             <a href="/cgi-bin/koha/svc/convert_report?report_id=[% savedreport.id | uri %]" data-report_id="[% savedreport.id | html %]" class="update_sql btn btn-default btn-xs" title="Update SQL"><i class="fa fa-eye"></i> Update SQL</a>
324                                                         [% END %]
325                                                     </td>
326                                                     <td>
327                                                         <div class="btn-group dropup">
328                                                             [%# There should be no space between these two buttons, it would render badly %]
329                                                             <a class="btn btn-default btn-xs" role="button"
330                                                                href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id | html %]&amp;phase=Run%20this%20report"><i
331                                                                class="fa fa-play"></i> Run</a><a
332                                                                class="btn btn-default btn-xs dropdown-toggle" id="reportactions[% savedreport.id | html %]" role="button" data-toggle="dropdown"
333                                                                href="#"><b class="caret"></b></a>
334                                                             <ul class="dropdown-menu pull-right" role="menu" aria-labelledby="reportactions[% savedreport.id | html %]">
335                                                                 <li><a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id | uri %]&amp;phase=Show%20SQL"><i class="fa fa-search"></i> View</a></li>
336                                                                 <li>
337                                                                     <a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id | uri %]&phase=Show%20SQL" class="preview_sql" data-reportid="[% savedreport.id | html %]">
338                                                                         <i class="fa fa-eye"></i> Preview SQL
339                                                                     </a>
340                                                                 </li>
341                                                                 [% IF ( CAN_user_reports_create_reports ) %]
342                                                                     <li><a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id | uri %]&amp;phase=Edit%20SQL"><i class="fa fa-pencil"></i> Edit</a></li>
343                                                                     <li><a title="Duplicate this saved report" href="/cgi-bin/koha/reports/guided_reports.pl?phase=Create report from existing&amp;report_id=[% savedreport.id | uri %]"><i class="fa fa-copy"></i> Duplicate</a></li>
344                                                                 [% END %]
345                                                                 [% IF (Koha.Preference('Mana') == 1) %]
346                                                                     <li><a class="ShareButton" data-toggle="modal" href="#mana_share_report" title="Share your report with Mana Knowledge Base"><i class="fa fa-share-alt"></i> Share</a></li>
347                                                                 [% END %]
348                                                                 <li><a href="/cgi-bin/koha/tools/scheduler.pl?id=[% savedreport.id | uri %]"><i class="fa fa-clock-o"></i> Schedule</a></li>
349                                                                 [% IF ( CAN_user_reports_delete_reports ) %]
350                                                                     <li><a class="confirmdelete" title="Delete this saved report" href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id | html %]&amp;phase=Delete%20Saved"><i class="fa fa-trash"></i> Delete</a></li>
351                                                                 [% END %]
352                                                             </ul>
353                                                         </div>
354                                                         <input type="hidden" id="previewSql[% savedreport.id | html %]" value="[% savedreport.savedsql | html %]" data-title="[% savedreport.report_name | html %]" />
355                                                     </td>
356                                                 </tr>
357                                             [% END %]
358                                         </tbody>
359                                     </table>
360
361                                     [% IF ( CAN_user_reports_delete_reports ) %]
362                                         <fieldset class="action">
363                                             <input type="submit" value="Delete selected" />
364                                         </fieldset>
365                                     [% END %]
366                                 </form>
367                             </div>
368                         </div>
369                     [% ELSE # IF ( savedreports ) %]
370                         <div class="dialog message">
371                             [% IF (filter_set || filters.date || filters.author || filters.keyword) %]
372                                 <h4>No saved reports match your criteria. </h4>
373                                 [% IF ( CAN_user_reports_create_reports ) %]
374                                     <form action="/cgi-bin/koha/reports/guided_reports.pl" method="get">
375                                         <input type="hidden" name="phase" value="Build new" />
376                                         <button type="submit" class="new"><i class="fa fa-plus"></i> New guided report</button>
377                                     </form>
378
379                                     <form action="/cgi-bin/koha/reports/guided_reports.pl" method="get">
380                                         <input type="hidden" name="phase" value="Create report from SQL" />
381                                         <button type="submit" class="new"><i class="fa fa-plus"></i> New SQL report</button>
382                                     </form>
383
384                                     <form action="/cgi-bin/koha/reports/guided_reports.pl" method="get">
385                                         <input type="hidden" name="phase" value="Use saved" />
386                                         <input type="hidden" name="filter_set" value="1" />
387                                         <input type="hidden" name="filter_keyword" value="" />
388                                         <button type="submit" class="deny"><i class="fa fa-fw fa-remove"></i> Cancel filter</button>
389                                     </form>
390                                 [% END %]
391                             [% ELSE %]
392                                 <h4>There are no saved reports. </h4>
393                                 [% IF ( CAN_user_reports_create_reports ) %]
394                                     <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Build%20new">Build a new report?</a>
395                                 [% END %]
396                             [% END # IF (filter_set || filters.date || filters.author || filters.keyword) %]
397                         </div> <!-- /.dialog.message -->
398                     [% END # /IF ( savedreports ) %]
399                 [% END # /IF ( saved1 ) %]
400
401                 [% INCLUDE 'mana/mana-share-report.inc' %]
402
403                 [% IF ( build1 ) %]
404                     [% IF ( cache_error) %]
405                         <div class="dialog alert">
406                             <strong> Please choose a cache_expiry less than 30 days </strong>
407                         </div>
408                     [% END %]
409
410                     <h1>Build a report</h1>
411                     <form action="/cgi-bin/koha/reports/guided_reports.pl">
412                         <fieldset class="rows">
413                             <legend>Step 1 of 6: Choose a module to report on,[% IF (usecache) %] Set cache expiry, [% END %] and choose report visibility </legend>
414                             <ol>
415                                 <li>
416                                     <label for="area">Choose: </label>
417                                     <select name="area" id="area">
418                                         [%- FOREACH area IN areas -%]
419                                             <option value="[% area | html %]">[%- PROCESS area_name area=area -%]</option>
420                                         [%- END -%]
421                                     </select>
422                                 </li>
423                                 [% IF (public) %]
424                                     <li>
425                                         <label for="public">Report is public:</label>
426                                         <select id="public" name="public">
427                                             <option value="0">No (default)</option>
428                                             <option value="1" selected="selected">Yes</option>
429                                         </select>
430                                     </li>
431                                 [% ELSE %]
432                                     <li>
433                                         <label for="public">Report is public:</label>
434                                         <select id="public" name="public">
435                                             <option value="0" selected="selected">No (default)</option>
436                                             <option value="1">Yes</option>
437                                         </select>
438                                     </li>
439                                 [% END %]
440
441                                 [% IF (usecache) %]
442                                     <li>
443                                         <label for="cache_expiry">Cache expiry:</label>
444                                         <input type="text" id="cache_expiry" name="cache_expiry" value="[% cache_expiry | html %]"></input>
445                                         <select id="cache_expiry_units" name="cache_expiry_units">
446                                             <option value="seconds">Seconds (default)</option>
447                                             <option value="minutes">Minutes</option>
448                                             <option value="hours">Hours</option>
449                                             <option value="days">Days</option>
450                                         </select>
451                                     </li>
452                                 [% END %]
453                             </ol>
454                         </fieldset> <!-- /.rows -->
455                         <fieldset class="action">
456                             <input type="hidden" name="phase" value="Report on this Area" />
457                             <input type="submit" name="submit" value="Next &gt;&gt;" />
458                         </fieldset>
459                     </form>
460                 [% END # /build1 %]
461
462                 [% IF ( build2 ) %]
463                     <h1>Build a report</h1>
464                     <form action="/cgi-bin/koha/reports/guided_reports.pl" method="post">
465                         <input type="hidden" name="area" value="[% area | html %]" />
466                         <input type="hidden" name="public" value="[% public | html %]" />
467                         <input type="hidden" name="cache_expiry" value="[% cache_expiry | html %]" />
468                         <fieldset class="rows">
469                             <legend>Step 2 of 6: Pick a report type</legend>
470                             <ol>
471                                 <li>
472                                     <label for="types">Choose: </label>
473                                     <select id="types" name="types">
474                                         <option value="1">Tabular</option>
475                                         <option value="2" disabled="disabled">Summary</option>
476                                         <option value="3" disabled="disabled">Matrix</option>
477                                     </select>
478                                 </li>
479                             </ol>
480                         </fieldset>
481
482                         <fieldset class="action">
483                             <input type="hidden" name="phase" value="Choose this type" />
484                             <input type="button" name="back" value="&lt;&lt; Back" class="goback" />
485                             <input type="submit" name="submit" value="Next &gt;&gt;" />
486                         </fieldset>
487                     </form>
488                 [% END # /IF (build2 ) %]
489
490                 [% IF ( build3 ) %]
491                     <h1>Build a report</h1>
492                     <h3>Step 3 of 6: Select columns for display</h3>
493                     <p>Note: Be careful selecting when selecting columns. If your choice is too broad it could result in a very large report that will either not complete, or slow your system down.</p>
494
495                     <form id="column_submit" action="/cgi-bin/koha/reports/guided_reports.pl" method="post">
496                         <input type="hidden" name="area" value="[% area | html %]" />
497                         <input type="hidden" name="type" value="[% type | html %]" />
498                         <input type="hidden" name="public" value="[% public | html %]" />
499                         <input type="hidden" name="cache_expiry" value="[% cache_expiry | html %]" />
500                         <fieldset>
501                             <div class="row">
502                                 <div class="col-sm-6">
503                                     <div style="float: left;">
504                                         <select id="availableColumns" name="oldcolumns2" multiple="multiple" size="25" style="min-width: 200px;height:300px;">
505                                             [% FOREACH column IN columns %]
506                                                 [% IF ( column.table ) %]
507                                                     [% IF ( loop.first ) %]
508                                                     [% ELSE %]
509                                                         </optgroup>
510                                                     [% END %]
511                                                     <optgroup label="[% column.table | html %]">
512                                                 [% ELSE %]
513                                                     <option value="[% column.name | html %]">
514                                                         [% IF ( column.description ) %]
515                                                             [% column.description | html %] &nbsp; / &nbsp; [% column.name | html %]
516                                                         [% ELSE %]
517                                                             [% column.name | html %]
518                                                         [% END %]
519                                                     </option>
520                                                 [% END %]
521                                             [% END %]
522                                             </optgroup>
523                                         </select>
524                                     </div>
525                                     <div style="width: 6.3em; float: right; margin-top: 100px">
526                                         <input type="button" name="Add" value="Add" class="button" style="width:6em;" id="addColumn" /><br />
527                                         <input type="button" name="delete" value="&lt;&lt; Delete" class="button" style="width: 6em; margin: 1em 0;" id="delColumn" />
528                                     </div>
529                                 </div> <!-- /.col-sm-6 -->
530
531                                 <div class="col-sm-6">
532                                     <select id="selectedColumns" name="columns" multiple="multiple" size="25" style="width:200px; height:300px;">
533                                     </select>
534                                 </div>
535                             </div> <!-- /.row -->
536                         </fieldset>
537                         <fieldset class="action">
538                             <input type="hidden" name="phase" value="Choose these columns" />
539                             <input type="button" name="back" value="&lt;&lt; Back" class="goback" />
540                             <input type="submit" name="submit" value="Next &gt;&gt;" />
541                         </fieldset>
542                     </form> <!-- /#column_submit -->
543                 [% END # /IF ( build3 ) %]
544
545                 [% IF ( build4 ) %]
546                     <h1>Build a report</h1>
547                     <form action="/cgi-bin/koha/reports/guided_reports.pl" method="post" >
548                         <input type="hidden" name="area" value="[% area | html %]" />
549                         <input type="hidden" name="type" value="[% type | html %]" />
550                         <input type="hidden" name="column" value="[% column | html %]" />
551                         <input type="hidden" name="public" value="[% public | html %]" />
552                         <input type="hidden" name="cache_expiry" value="[% cache_expiry | html %]" />
553                         <fieldset>
554                             <legend>Step 4 of 6: Select criteria to limit on</legend>
555                             <table>
556                                 [% FOREACH criteri IN criteria %]
557                                     <tr>
558                                         <td>
559                                             <input type="checkbox" name="criteria_column" id="[% criteri.name | html %]" value="[% criteri.name | html %]" />
560                                             <label for="[% criteri.name | html %]">[% criteri.description | html %] </label>
561                                         </td>
562                                         [% IF ( criteri.date ) %]
563                                             <td>
564                                                 <input type="text" size="10" id="[% criteri.name | html %]_value" name="[% criteri.name | html %]_value" value="" class="datepicker" />
565                                                 <span class="hint">[% INCLUDE 'date-format.inc' %]</span>
566                                             </td>
567                                         </tr>
568                                         [% ELSE %]
569                                             [% IF ( criteri.textrange ) %]
570                                                 <td>
571                                                     from
572                                                     <input type="text" size="10" id="[% criteri.from | html %]_value" name="[% criteri.from | html %]_value" value="" /> to
573                                                     <input type="text" size="10" id="[% criteri.to | html %]_value" name="[% criteri.to | html %]_value" value="" />
574                                                 </td>
575                                             </tr>
576                                             [% ELSE %]
577                                                 [% IF ( criteri.daterange ) %]
578                                                     <td>
579                                                         from
580                                                         <input type="text" size="10" id="from_[% criteri.name | html %]_value" name="from_[% criteri.name | html %]_value" value="" class="datepickerfrom" />
581                                                         to
582                                                         <input type="text" size="10" id="to_[% criteri.name | html %]_value" name="to_[% criteri.name | html %]_value" value="" class="datepickerto" />
583                                                         <span class="hint">[% INCLUDE 'date-format.inc' %]</span>
584                                                     </td>
585                                                 </tr>
586                                                 [% ELSE %]
587                                                     <td>
588                                                         <select name="[% criteri.name | html %]_value">
589                                                            [% FOREACH value IN criteri.values %]
590                                                                 <option value="[% value.availablevalues | html %]">
591                                                                     [% IF ( value.default ) %]
592                                                                         Default
593                                                                     [% ELSE %]
594                                                                         [% value.display_value | html %]
595                                                                     [% END %]
596                                                                 </option>
597                                                             [% END %]
598                                                         </select>
599                                                     </td>
600                                                 </tr>
601                                                 [% END %]
602                                             [% END %]
603                                         [% END %]
604                                 [% END %]
605                             </table>
606                         </fieldset>
607
608                         [% IF ( definitions ) %]
609                             <fieldset>
610                                 <legend>Dictionary definitions</legend>
611                                 <table>
612                                     [% FOREACH definition IN definitions %]
613                                         <tr>
614                                             <td>
615                                                 <input type="checkbox" name="definition" value="[% definition.id | html %]" />
616                                                 [% definition.name | html %]
617                                             </td>
618                                         </tr>
619                                     [% END %]
620                                 </table>
621                             </fieldset>
622                         [% END %]
623
624                         <fieldset class="action">
625                             <input type="hidden" name="phase" value="Choose these criteria" />
626                             <input type="button" name="back" value="&lt;&lt; Back" class="goback" />
627                             <input type="submit" name="submit" value="Next &gt;&gt;" />
628                         </fieldset>
629                     </form>
630                 [% END # /IF ( build4 ) %]
631
632                 [% IF ( build5 ) %]
633                     <h1>Build a report</h1>
634                     <h3>Step 5 of 6: Pick which columns to total</h3>
635                     <form action="/cgi-bin/koha/reports/guided_reports.pl" method="post">
636                         <input type="hidden" name="area" value="[% area | html %]" />
637                         <input type="hidden" name="type" value="[% type | html %]" />
638                         <input type="hidden" name="column" value="[% column | html %]" />
639                         <input type="hidden" name="definition" value="[% definition | html %]" />
640                         <input type="hidden" name="criteria" value="[% criteriastring | html %]" />
641                         <input type="hidden" name="public" value="[% public | html %]" />
642                         <input type="hidden" name="cache_expiry" value="[% cache_expiry | html %]" />
643                         <fieldset>
644                             <table>
645                                 [% FOREACH total_b IN total_by %]
646                                     <tr>
647                                         <td>
648                                             <input type="checkbox" name="total_by" id="[% total_b.name | html %]" value="[% total_b.name | html %]" />
649                                             <label for="[% total_b.name | html %]">[% total_b.name | html %]</label>
650                                         </td>
651                                         <td>
652                                             <select name="[% total_b.name | html %]_tvalue">
653                                                 [% FOREACH selec IN total_b.select %]
654                                                     <option value="[% selec.value | html %]">[% selec.value | html %]</option>
655                                                 [% END %]
656                                             </select>
657                                         </td>
658                                     </tr>
659                                 [% END %]
660                             </table>
661                         </fieldset>
662
663                         <fieldset class="action"><input type="hidden" name="phase" value="Choose these operations" />
664                             <input type="button" name="back" value="&lt;&lt; Back" class="goback" />
665                             <input type="submit" name="submit" value="Next &gt;&gt;" />
666                         </fieldset>
667                     </form>
668                 [% END # /IF ( build5 ) %]
669
670                 [% IF ( build6 ) %]
671                     <h1>Build a report</h1>
672                     <h3>Step 6 of 6: Choose how you want the report ordered</h3>
673                     <form action="/cgi-bin/koha/reports/guided_reports.pl" method="post">
674                         <input type="hidden" name="area" value="[% area | html %]" />
675                         <input type="hidden" name="type" value="[% type | html %]" />
676                         <input type="hidden" name="column" value="[% column | html %]" />
677                         <input type="hidden" name="criteria" value="[% criteriastring | html %]" />
678                         <input type="hidden" name="definition" value="[% definition | html %]" />
679                         <input type="hidden" name="totals" value="[% totals | html %]" />
680                         <input type="hidden" name="public" value="[% public | html %]" />
681                         <input type="hidden" name="cache_expiry" value="[% cache_expiry | html %]" />
682                         <fieldset>
683                             <table>
684                                 [% FOREACH order_b IN order_by %]
685                                     <tr>
686                                         <td>
687                                             <input type="checkbox" id="[% order_b.name | html %]" name="order_by" value="[% order_b.name | html %]" />
688                                             <label for="[% order_b.name | html %]">[% order_b.name | html %]</label>
689                                         </td>
690                                         <td>
691                                             <select name="[% order_b.name | html %]_ovalue">
692                                                 [% FOREACH selec IN order_b.select %]
693                                                     <option value="[% selec.value | html %]">[% selec.value | html %]</option>
694                                                 [% END %]
695                                             </select>
696                                         </td>
697                                     </tr>
698                                 [% END %]
699                             </table>
700                         </fieldset>
701                         <fieldset class="action">
702                             <input type="hidden" name="phase" value="Build report" />
703                             <input type="submit" name="submit" value="Finish" />
704                         </fieldset>
705                     </form>
706                 [% END #/ IF ( build6 ) %]
707
708                 [% IF ( showreport ) %]
709                     <h1>Confirm custom report</h1>
710                     <p>Your report will be generated with the following SQL statement.</p>
711                     <p>
712                         [% sql | html %]
713                     </p>
714
715                     <form action="/cgi-bin/koha/reports/guided_reports.pl" method="post">
716                         <input type="hidden" name="sql" value="[% sql | html %]" />
717                         <input type="hidden" name="type" value="[% type | html %]" />
718                         <input type="hidden" name="public" value="[% public | html %]" />
719                         <input type="hidden" name="cache_expiry" value="[% cache_expiry | html %]" />
720                         <p>You will need to save the report before you can execute it</p>
721                         <fieldset class="action"><input type="hidden" name="phase" value="Save" />
722                             <input type="submit" name="submit" value="Next" />
723                         </fieldset>
724                     </form>
725                 [% END #/ IF ( showreport ) %]
726
727                 [% IF ( save ) %]
728                     <form action="/cgi-bin/koha/reports/guided_reports.pl" method="post" id="sql_report_form">
729                         <input type="hidden" name="sql" value="[% sql | html %]" />
730                         <input type="hidden" name="type" value="[% type | html %]" />
731                         <input type="hidden" name="area" value="[% area | html %]" />
732                         <input type="hidden" name="public" value="[% public | html %]" />
733                         <input type="hidden" name="cache_expiry" value="[% cache_expiry | html %]" />
734                         <fieldset class="rows">
735                             <legend>Save your custom report</legend>
736                             <ol>
737                                 <li>
738                                     <label for="reportname" class="required">Report name: </label>
739                                     <input type="text" id="reportname" name="reportname" class="required" required="required" />
740                                     <span class="required">Required</span>
741                                 </li>
742                                 [% PROCESS group_and_subgroup_selection %]
743                                 <li>
744                                     <label for="notes">Notes:</label>
745                                     <textarea name="notes" id="notes"></textarea>
746                                 </li>
747                             </ol>
748                         </fieldset>
749                         <fieldset class="action"><input type="hidden" name="phase" value="Save Report" />
750                             <input type="submit" name="submit" value="Save report" />
751                         </fieldset>
752                     </form>
753                 [% END # /IF( save ) %]
754
755                 [% IF ( warn_authval_problem ) %]
756                     <div class="dialog alert">
757                         <h3>Errors found when processing parameters for report: [% name | html %]</h3>
758                         [% FOREACH problematic_authval IN problematic_authvals %]
759                             <p>
760                             <strong>[% problematic_authval.name | html %]:</strong> The authorized value category (<strong>[% problematic_authval.authval | html %]</strong>)
761                                 you selected does not exist.
762                             </p>
763                         [% END %]
764                         <!-- Save Anyway Form -->
765                         <form action='/cgi-bin/koha/reports/guided_reports.pl'>
766                         <!--Every parameter the user issued is provided as a hidden field for recovery-->
767                             <input type='hidden' name='id' value='[% id | html %]' />
768                             <input type='hidden' name='sql' value='[% sql | html %]' />
769                             <input type='hidden' name='reportname' value='[% reportname | html %]' />
770                             <input type='hidden' name='group' value='[% group | html %]' />
771                             <input type='hidden' name='subgroup' value='[% subgroup | html %]' />
772                             <input type='hidden' name='notes' value='[% notes | html %]' />
773                             <input type='hidden' name='cache_expiry' value='[% cache_expiry | html %]' />
774                             <input type='hidden' name='cache_expiry_units' value='[% cache_expiry_units | html %]' />
775                             <input type='hidden' name='public' value='[% public | html %]' />
776                         [% IF ( phase_update) %]
777                             <input type='hidden' name='phase' value='Update SQL' />
778                             <button type="submit" name="save_anyway" value="Save anyway" class="approve"><i class="fa fa-fw fa-check"></i> Save anyway</button>
779                         [% ELSIF ( phase_save) %]
780                             <input type='hidden' name='area' value='[% area | html %]' />
781                             <input type='hidden' name='phase' value='Save Report' />
782                             <button type="submit" name="save_anyway" value="Save anyway" class="approve"><i class="fa fa-fw fa-check"></i> Save anyway</button>
783                         [% END %]
784                         </form>
785                         <!-- Go back to editing -->
786                         <form action='/cgi-bin/koha/reports/guided_reports.pl'>
787                             <button type="button" class="new goback"><i class="fa fa-fw fa-pencil"></i> Edit SQL</button>
788                         </form>
789                     </div>
790                 [% END # /IF ( warn_authval_problem )%]
791
792                 [% IF ( enter_params ) %]
793                     <form action='/cgi-bin/koha/reports/guided_reports.pl'>
794                         <input type='hidden' name='reports' value="[% reports | html %]" />
795                         [% IF ( auth_val_error ) %]
796                             <input type='hidden' name='phase' value='Edit SQL' />
797                             <div class="dialog alert">
798                                 <h3>Errors found when processing parameters for report: [% name | html %]</h3>
799                                 [% FOREACH auth_val_error IN auth_val_errors %]
800                                     <p>
801                                         <strong>[% auth_val_error.entry | html %]:</strong> The authorized value category (<strong>[% auth_val_error.auth_val | html %]</strong>)
802                                         you selected does not exist.
803                                     </p>
804                                 [% END %]
805                             </div>
806                             <fieldset class="action"><input type="submit" value="Edit SQL" /></fieldset>
807                         [% ELSE #  IF ( auth_val_error ) %]
808                             <input type='hidden' name='phase' value='Run this report' />
809                             <h1>Enter parameters for report [% name | html %]:</h1>
810                             [% IF ( notes ) %]
811                                 <p>[% notes | html %]</p>
812                             [% END %]
813                             <fieldset class="rows">
814                                 <ol>
815                                     [% FOREACH sql_param IN sql_params %]
816                                         <input name="param_name" value="[% sql_param.name | html %]" type="hidden" />
817                                         [% IF sql_param.input == 'date' %]
818                                             <li>
819                                                 <label for="date_[% sql_param_entry | html %][% loop.count | html %]">[% sql_param.entry | html %]:</label> <input id="date_[% sql_param_entry | html %][% loop.count | html %]" type="text" value="" size="10" name="sql_params" class="datepicker" />
820                                             </li>
821                                         [% ELSIF ( sql_param.input == 'text' ) %]
822                                             <li>
823                                                 <label for="sql_params[% loop.count | html %]">[% sql_param.entry | html %]: </label>
824                                                 <input id="sql_params[% loop.count | html %]" type="text" name="sql_params" />
825                                             </li>
826                                         [% ELSE %]
827                                             <li>
828                                                 <label for="sql_params_[% sql_param.labelid | html %]">[% sql_param.entry | html %]:</label>
829                                                 <select name="[%- sql_param.input.name | html -%]" tabindex="1"  size="1" id="[%- sql_param.input.id | html -%]">
830                                                     [% IF (sql_param.include_all) %]
831                                                         <option value="%">All</option>
832                                                     [% END %]
833                                                     [% FOREACH value IN sql_param.input.values %]
834                                                         <option value="[%- value | html -%]">[%- sql_param.input.labels.$value | html -%]</option>
835                                                     [% END %]
836                                                 </select>
837                                             </li>
838                                         [% END # /IF sql_param.input == 'date' %]
839                                     [% END # /FOREACH sql_param %]
840                                 </ol>
841                             </fieldset> <!-- /.rows -->
842                             <fieldset class="action">
843                                 <input type="submit" value="Run the report" />
844                             </fieldset>
845                         [% END # / IF ( auth_val_error ) %]
846                     </form>
847                 [% END # /IF ( enter_params ) %]
848
849                 [% IF ( execute ) %]
850                     <h1>[% name | html %] <span id="report_id">([% id | html %])</span></h1>
851                     [% IF ( notes ) %]
852                         <p><span class="label">Notes:</span> [% notes | html %]</p>
853                     [% END %]
854                     [% IF ( unlimited_total ) %]
855                         <p>
856                             <span class="label">Total number of results:</span>
857                             [% unlimited_total | html %]
858                             [% IF unlimited_total > limit %]
859                                 ([% limit | html %] shown)
860                             [% END %].
861                         </p>
862                     [% END %]
863
864                     <div id="sql_output" style="display:none;">
865                         <span class="label">Report SQL:</span>
866                         <textarea id="sql" readonly="readonly">[% sql | html %]</textarea>
867                     </div>
868
869                     <div>
870                         <a href="#" id="toggle_chart_settings_hid" class="toggle_chart_settings" style="display:none"><i class="fa fa-eye-slash"></i> Hide chart</a>
871                         <a href="#" id="toggle_chart_settings_vis" class="toggle_chart_settings" style="display:none"><i class="fa fa fa-eye"></i> Show chart</a>
872                     </div>
873                     <div id="chart" class="clearfix"></div>
874
875                     [% IF ( execute ) %]
876                         [% UNLESS ( errors ) %]
877                             <form method="post" enctype="multipart/form-data" action="/cgi-bin/koha/tools/batch_record_modification.pl" id="batch_record_modification">
878                                 <input type="hidden" name="recordtype" value="biblio" />
879                                 <input type="hidden" name="op" value="list" />
880                                 [% # Preserve the whitespace of the following textarea in order to format the values correctly %]
881                                 <textarea style="display:none" name="recordnumber_list" id="recordnumber_list">
882                                     [%- recordnumbers = PROCESS batch_list results=results batch_type='biblionumber' | trim | html %][% IF recordnumbers %][% SET batch_biblionumbers = 1 %][% recordnumbers | html %][% END -%]
883                                 </textarea>
884                             </form>
885
886                             <form method="POST" action="/cgi-bin/koha/tools/batch_delete_records.pl" id="batch_record_deletion">
887                                 <input type="hidden" name="recordtype" value="biblio" />
888                                 <input type="hidden" name="op" value="list" />
889                                 [% # Preserve the whitespace of the following textarea in order to format the values correctly %]
890                                 <textarea style="display:none" name="recordnumber_list" id="recordnumber_list">
891                                     [%- recordnumbers = PROCESS batch_list results=results batch_type='biblionumber' | trim | html %][% IF recordnumbers %][% SET batch_biblionumbers = 1 %][% recordnumbers | html %][% END -%]
892                                 </textarea>
893                             </form>
894
895                             <form method="POST" action="/cgi-bin/koha/tools/batchMod.pl" id="batch_item_modification">
896                                 <input type="hidden" name="op" value="show" />
897                                 [% FOREACH result IN results %]
898                                     [% FOREACH cells IN result.cells %]
899                                         [% place = loop.index %]
900                                         [% NEXT UNLESS cells.cell.match('^(\d+)$') %]
901                                         [% IF header_row.$place.cell == 'itemnumber' || header_types.item(header_row.$place.cell) == 'itemnumber'  %]
902                                             [% SET batch_itemnumbers = 1 %]
903                                             [% SET header_row.$place.has_itemnumbers = 1 %]
904                                             <input type="hidden" name="[% header_row.$place.cell | html %]" value="[% cells.cell | html %]" />
905                                         [% END %]
906                                     [% END %]
907                                 [% END %]
908                             </form>
909
910                             <form method="POST" action="/cgi-bin/koha/tools/batchMod.pl" id="batch_item_deletion">
911                                 <input type="hidden" name="op" value="show" />
912                                 <input type="hidden" name="del" value="1" />
913                                 [% FOREACH result IN results %]
914                                     [% FOREACH cells IN result.cells %]
915                                         [% place = loop.index %]
916                                         [% NEXT UNLESS cells.cell.match('^(\d+)$') %]
917                                         [% IF header_row.$place.cell == 'itemnumber' || header_types.item(header_row.$place.cell) == 'itemnumber'  %]
918                                             [% SET batch_itemnumbers = 1 %]
919                                             [% SET header_row.$place.has_itemnumbers = 1 %]
920                                             <input type="hidden" name="[% header_row.$place.cell | html %]" value="[% cells.cell | html %]" />
921                                         [% END %]
922                                     [% END %]
923                                 [% END %]
924                             </form>
925
926                             <form method="get" action="/cgi-bin/koha/virtualshelves/addbybiblionumber.pl" id="batch_add_to_list">
927                                 [% FOREACH result IN results %]
928                                     [% FOREACH cells IN result.cells %]
929                                         [% place = loop.index %]
930                                         [% NEXT UNLESS cells.cell.match('^(\d+)$') %]
931                                         [% IF header_row.$place.cell == 'biblionumber' || header_types.item(header_row.$place.cell) == 'biblionumber' %]
932                                             [% SET batch_biblionumbers = 1 %]
933                                             [% SET header_row.$place.has_biblionumbers = 1 %]
934                                             <input type="hidden" class="bib_to_list" name="biblionumber" value="[% cells.cell | html %]" />
935                                         [% END %]
936                                     [% END %]
937                                 [% END %]
938                             </form>
939
940                             <form method="POST" action="/cgi-bin/koha/tools/modborrowers.pl" id="batch_patron_modification">
941                                 <input type="hidden" name="op" value="show" />
942                                 [% # Preserve the whitespace of the following textarea in order to format the values correctly %]
943                                 <textarea style="display:none" name="cardnumberlist" id="cardnumberlist">
944                                     [%- cardnumbers = PROCESS batch_list results=results batch_type='cardnumber' | trim | html %][% IF cardnumbers %][% SET batch_cardnumbers = 1 %][% cardnumbers | html %][% END -%]
945                                 </textarea>
946                             </form>
947
948                             [% BLOCK batch_list %]
949                                     [%- FOREACH result IN results %]
950                                         [%- FOREACH cells IN result.cells %]
951                                             [%- place = loop.index %]
952                                             [%- IF header_row.$place.cell == batch_type || header_types.item(header_row.$place.cell) == batch_type %]
953 [%# We must not add whitespace to the cardnumbers %][% cells.cell | html %]
954                                             [%- END %]
955                                         [%- END %]
956                                     [%- END -%]
957                             [% END %]
958                             [% IF ( batch_biblionumbers || batch_itemnumbers || batch_cardnumbers ) %]
959                                 <p>
960                                     <div class="btn-group">
961                                         <button class="btn btn-default dropdown-toggle" type="button" id="batch_mod_menu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
962                                             Batch operations with [% IF unlimited_total > limit %][% limit | html %][% ELSE %][% unlimited_total | html %][% END %] visible records
963                                             <span class="caret"></span>
964                                         </button>
965                                         <ul class="dropdown-menu" aria-labelledby="batch_mod_menu">
966                                             [% FOREACH header_ro IN header_row %]
967                                                 [% IF header_ro.has_biblionumbers && ( header_ro.cell == 'biblionumber' || header_types.item( header_ro.cell ) == 'biblionumber' ) %]
968                                                     <li class="dropdown-header">Bibliographic records</li>
969                                                     <li>
970                                                         <a href="#" data-submit="batch_record_modification" data-toggle="tooltip" data-placement="right" title="Send visible records to batch record modification" class="batch_op send_to_record_mod">Batch record modification</a>
971                                                     </li>
972                                                     <li>
973                                                         <a href="#" data-submit="batch_record_deletion" data-toggle="tooltip" data-placement="right" title="Send visible records to batch record deletion" class="batch_op send_to_record_del">Batch record deletion</a>
974                                                     </li>
975                                                     <li>
976                                                         <a href="#" data-submit="batch_add_to_list" data-toggle="tooltip" data-placement="right" title="Send visible records to a list" class="batch_op send_to_list">Add to list</a>
977                                                     </li>
978                                                 [% END %]
979                                                 [% IF header_ro.has_itemnumbers && ( header_ro.cell == 'itemnumber' || header_types.item( header_ro.cell ) == 'itemnumber' ) %]
980                                                     [% IF ( batch_biblionumbers ) %]
981                                                         <li role="separator" class="divider"></li>
982                                                     [% END %]
983                                                     <li class="dropdown-header">Item records</li>
984                                                     <li>
985                                                         <a href="#" data-submit="batch_item_modification" data-toggle="tooltip" data-placement="right" title="Send visible items to batch item modification" class="batch_op send_to_item_mod">Batch item modification</a>
986                                                     </li>
987                                                     <li>
988                                                         <a href="#" data-submit="batch_item_deletion" data-toggle="tooltip" data-placement="right" title="Send visible items to batch item deletion" class="batch_op send_to_item_del">Batch item deletion</a>
989                                                     </li>
990                                                 [% END %]
991                                                 [% IF header_ro.cell == 'cardnumber' || header_types.item( header_ro.cell ) == 'cardnumber' %]
992                                                     [% IF ( batch_biblionumbers || batch_itemnumbers ) %]
993                                                         <li role="separator" class="divider"></li>
994                                                     [% END %]
995                                                     <li class="dropdown-header">Patron records</li>
996                                                     <li>
997                                                         <a href="#" data-submit="batch_patron_modification" data-toggle="tooltip" data-placement="right" title="Send visible results to batch patron modification" class="batch_op send_to_patron_mod">Batch patron modification</a>
998                                                     </li>
999                                                 [% END %]
1000                                             [% END %]
1001                                         </ul> <!-- /.dropdown-menu -->
1002                                     </div> <!-- /.dropdown -->
1003                                 </p>
1004                             [% END # /IF ( batch_biblionumbers || batch_itemnumbers || batch_cardnumbers ) %]
1005                         [% END # UNLESS ( errors ) %]
1006                     [% END # IF ( execute ) %]
1007
1008                     <form action="/cgi-bin/koha/reports/guided_reports.pl" method="get" id="limitselect">
1009                         <input type="hidden" name="phase" value="Run this report"/>
1010                         <input type="hidden" name="reports" value="[% report_id | html %]"/>
1011
1012                         [% FOREACH p IN sql_params %]
1013                             <input type="hidden" name="sql_params" value="[% p | html %]"/>
1014                         [% END %]
1015                         [% FOREACH n IN param_names %]
1016                             <input type="hidden" name="param_name" value="[% n | html %]"/>
1017                         [% END %]
1018
1019                         <label for="limit">Rows per page: </label>
1020                         <select name="limit" id="limit">
1021                             [% limits = [ 10, 20, 50, 100, 200, 300, 400, 500, 1000 ] %]
1022                             [% FOREACH l IN limits %]
1023                                 [% IF l == limit %]
1024                                     <option value="[% l | html %]" selected="selected">[% l | html %]</option>
1025                                 [% ELSE %]
1026                                     <option value="[% l | html %]">[% l | html %]</option>
1027                                 [% END %]
1028                             [% END %]
1029                         </select>
1030                     </form> <!-- /#limitselect -->
1031
1032                     <div class="pages">[% pagination_bar | $raw %]</div>
1033                     [% UNLESS ( errors ) %]
1034                         <table>
1035                             <tr>
1036                                 [% FOREACH header_ro IN header_row %]
1037                                     <th>[% header_ro.cell | html %]</th>
1038                                 [% END %]
1039                             </tr>
1040                             [% FOREACH result IN results %]
1041                                 <tr>
1042                                     [% FOREACH cells IN result.cells %]
1043                                         <td>[% cells.cell | $raw %]</td>
1044                                     [% END %]
1045                                 </tr>
1046                             [% END %]
1047                         </table>
1048                     [% END %]
1049                     <div class="pages">[% pagination_bar | $raw %]</div>
1050
1051                     [% INCLUDE 'chart.inc' %]
1052
1053                 [% END #/IF ( execute ) %]
1054
1055                 [% IF ( create ) %]
1056                     <form action="/cgi-bin/koha/reports/guided_reports.pl" method="post" id="sql_report_form">
1057                         <fieldset class="rows">
1058                             <legend>Create report from SQL</legend>
1059                             <ol>
1060                                 <li>
1061                                     <label for="reportname" class="required">Report name:</label>
1062                                     [% IF ( reportname ) %]
1063                                         <input type="text" class="required" required="required" id="reportname" name="reportname" value="[% reportname | html %]" size="50"/>
1064                                     [% ELSE %]
1065                                         <input type="text" class="required" required="required" id="reportname" name="reportname" size="50" />
1066                                     [% END %]
1067                                     <span class="required">Required</span>
1068                                 </li>
1069                                 [% PROCESS group_and_subgroup_selection %]
1070
1071                                 [% IF (public) %]
1072                                     <li>
1073                                         <label for="public">Report is public:</label>
1074                                         <select id="public" name="public">
1075                                             <option value="0">No (default)</option>
1076                                             <option value="1" selected="selected">Yes</option>
1077                                         </select>
1078                                     </li>
1079                                 [% ELSE %]
1080                                     <li>
1081                                         <label for="public">Report is public:</label>
1082                                         <select id="public" name="public">
1083                                             <option value="0" selected="selected">No (default)</option>
1084                                             <option value="1">Yes</option>
1085                                         </select>
1086                                     </li>
1087                                 [% END # /IF (public) %]
1088
1089                                 [% IF (usecache) %]
1090                                     <li>
1091                                         <label for="cache_expiry">Cache expiry:</label>
1092                                         <input type="text" id="cache_expiry" name="cache_expiry" value="[% cache_expiry | html %]"></input>
1093                                         <select id="cache_expiry_units" name="cache_expiry_units">
1094                                             <option value="seconds" selected="selected">Seconds (default)</option>
1095                                             <option value="minutes">Minutes</option>
1096                                             <option value="hours">Hours</option>
1097                                             <option value="days">Days</option>
1098                                         </select>
1099                                     </li>
1100                                 [% END # /IF (usecache) %]
1101                                 <li>
1102                                     <label for="notes">Notes:</label>
1103                                     <textarea id="notes" name="notes" cols="50" rows="2">[% notes | html %]</textarea>
1104                                 </li>
1105                             </ol>
1106                         </fieldset> <!-- /.rows -->
1107
1108                         <fieldset class="rows">
1109                             <legend>SQL:</legend>
1110                             <div style="margin:1em;">
1111                                 <textarea id="sql" name="sql" class="required" required="required" cols="50" rows="10">[% sql | html %]</textarea>
1112                                 <span class="required">Required</span>
1113                             </div>
1114                         </fieldset>
1115
1116                         <fieldset class="action">
1117                             <input type="hidden" name="phase" value="Save Report" />
1118                             <input type="submit" name="submit" value="Save report" />
1119                             <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved" class="cancel">Cancel</a>
1120                         </fieldset>
1121                     </form>
1122                 [% END #/IF ( create ) %]
1123
1124                 [% IF saved_results %]
1125                     <h1>Saved report results</h1>
1126                     <h2>[% name | html %]</h2>
1127                     <p>[% notes | html %]</p>
1128                     <table>
1129                         [% FOREACH rows IN saved_results %]
1130                             <tr>
1131                                 [% FOREACH col IN rows %]
1132                                     <td>[% col | html %]</td>
1133                                 [% END %]
1134                             </tr>
1135                         [% END %]
1136                     </table>
1137                 [% END # /IF saved_results %]
1138
1139                 [% IF ( showsql ) %]
1140                     <fieldset class="rows">
1141                         <legend>[% reportname | html %]</legend>
1142                         <ol>
1143                             [% IF ( notes ) %]
1144                                 <li>
1145                                     <span class="label">Notes:</span>
1146                                     [% notes | html %]
1147                                 </li>
1148                             [% ELSE %]
1149                             [% END %]
1150                             <li>
1151                                 <textarea id="sql">[% sql | html %]</textarea>
1152                             </li>
1153                         </ol>
1154                     </fieldset>
1155                 [% END # /IF ( showsql ) %]
1156
1157                 [% IF ( save_successful ) %]
1158                     [% UNLESS ( errors ) %]
1159                         </br>
1160                         <div id="report_updated">
1161                             <div class="dialog message">
1162                                 <p>Your report "[% reportname | html %]" has been saved</p>
1163                             </div>
1164                         </div>
1165                     [% END %]
1166                 [% END %]
1167
1168                 [% IF ( editsql ) %]
1169                     <form action="/cgi-bin/koha/reports/guided_reports.pl" method="post" id="sql_report_form">
1170                         <input type="hidden" name="phase" value="Update SQL" />
1171                         <input type="hidden" name="id" value="[% id | html %]"/>
1172                         <fieldset class="rows">
1173                             <legend>Edit SQL report</legend>
1174                             <ol>
1175                                 <li>
1176                                     <label for="reportname" class="required">Report name: </label>
1177                                     <input type="text" id="reportname" name="reportname" value="[% reportname | html %]" size="50" class="required" required="required" />
1178                                     <span class="required">Required</span>
1179                                 </li>
1180                                 [% PROCESS group_and_subgroup_selection %]
1181                                 [% IF (public) %]
1182                                     <li>
1183                                         <label for="public">Report is public:</label>
1184                                         <select id="public" name="public">
1185                                             <option value="0">No (default)</option>
1186                                             <option value="1" selected="selected">Yes</option>
1187                                         </select>
1188                                     </li>
1189                                 [% ELSE %]
1190                                     <li>
1191                                         <label for="public">Report is public:</label>
1192                                         <select id="public" name="public">
1193                                             <option value="0" selected="selected">No (default)</option>
1194                                             <option value="1">Yes</option>
1195                                         </select>
1196                                     </li>
1197                                 [% END # /IF (public) %]
1198
1199                                 [% IF (usecache) %]
1200                                     <li>
1201                                         <label for="cache_expiry">Cache expiry:</label>
1202                                         <input type="text" id="cache_expiry" name="cache_expiry" value="[% cache_expiry | html %]" />
1203                                         <select id="cache_expiry_units" name="cache_expiry_units">
1204                                             <option value="seconds">Seconds (default)</option>
1205                                             <option value="minutes">Minutes</option>
1206                                             <option value="hours">Hours</option>
1207                                             <option value="days">Days</option>
1208                                         </select>
1209                                     </li>
1210                                 [% END %]
1211                                 <li>
1212                                     <label for="notes">Notes:</label>
1213                                     <textarea id="notes" name="notes" cols="50" rows="2">[% notes | html %]</textarea>
1214                                 </li>
1215                             </ol>
1216                         </fieldset> <!-- /.rows -->
1217
1218                         <fieldset class="rows">
1219                             <legend>SQL:</legend>
1220                             <div style="margin:1em;">
1221                                 <textarea id="sql" name="sql" class="required" required="required" cols="50" rows="10">[% sql | html %]</textarea>
1222                                 <span class="required">Required</span>
1223                             </div>
1224                         </fieldset>
1225
1226                         <fieldset class="action">
1227                             <input type="submit" name="submit" value="Update SQL" />
1228                             <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved" class="cancel">Cancel</a>
1229                         </fieldset>
1230                     </form>
1231                 [% END # /IF ( editsql ) %]
1232
1233                 [% IF ( errors ) %]
1234                     <form action="/cgi-bin/koha/reports/guided_reports.pl" method="post">
1235                         <div class="dialog alert">
1236                             <strong>The following error was encountered:</strong><br />
1237                             [% FOREACH error IN errors %]
1238                                 [% IF ( error.sqlerr ) %]
1239                                     This report contains the SQL keyword <strong>[% error.sqlerr | html %]</strong>.<br />
1240                                     Use of this keyword is not allowed in Koha reports due to security and data integrity risks. Only SELECT queries are allowed.<br />
1241                                     Please return to the &quot;Saved Reports&quot; screen and delete this report or retry creating a new one.
1242                                 [% ELSIF ( error.queryerr ) %]
1243                                     The database returned the following error: <br />
1244                                     [% error.queryerr | html %]<br />Please check the log for further details.
1245                                 [% ELSIF ( error.cache_expiry ) %]
1246                                     Please select a cache expiry less than 30 days.
1247                                 [% ELSE %]
1248                                 [% END %]
1249                                 <div id="onerror_actions">
1250                                     <a href="#" class="button goback">Return to previous page</a>
1251                                 </div>
1252                             [% END %]
1253                         </div>
1254                         <fieldset class="action">
1255                             <input type="hidden" name="phase" value="Use saved" />
1256                             <input type="submit" name="submit" value="Saved reports" />
1257                         </fieldset>
1258                     </form>
1259                 [% END # /IF ( errors ) %]
1260
1261             </main>
1262         </div> <!-- /.col-sm-10.col-sm-push-2 -->
1263
1264         <div class="col-sm-2 col-sm-pull-10">
1265             <aside>
1266                 [% IF ( saved1 ) %]
1267                     <div id="saved-reports-filter">
1268                         <form action="/cgi-bin/koha/reports/guided_reports.pl" method="get">
1269                             <input type="hidden" name="phase" value="Use saved" />
1270                             <input type="hidden" name="filter_set" value="1" />
1271                             <fieldset class="brief">
1272                                 <h3>Filter</h3>
1273                                 <ol>
1274                                     <li>
1275                                         <label for="filter_date">Date:</label>
1276                                         <input type="text" id="filter_date" name="filter_date" size="10" value="[% filters.date | html %]" class="datepicker" />
1277                                         <div class="hint">[% INCLUDE 'date-format.inc' %]</div>
1278                                     </li>
1279                                     <li>
1280                                         <label for="filter_author">Author:</label>
1281                                         <input type="text" id="filter_author" name="filter_author" value="[% filters.author | html %]" size="16" />
1282                                     </li>
1283                                     <li>
1284                                         <label for="filter_keyword">Keyword:</label>
1285                                         <input type="text" id="filter_keyword" name="filter_keyword" value="[% filters.keyword | html %]" size="16" />
1286                                     </li>
1287                                 </ol>
1288                             </fieldset> <!-- /.brief -->
1289                             <fieldset class="action">
1290                                 <input type="submit" value="Apply filter" />
1291                                 <a id="resetReportsFilter" href="/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved&clear_filters=1">Clear</a>
1292                             </fieldset>
1293                         </form>
1294                     </div> <!-- /#saved-reports-filter -->
1295                 [% END %]
1296                 [% INCLUDE 'guided-reports-view.inc' %]
1297             </aside>
1298         </div> <!-- /.col-sm-2.col-sm-pull-10 -->
1299     </div> <!-- /.row -->
1300
1301 [% MACRO jsinclude BLOCK %]
1302     [% Asset.js("js/charts.js") | $raw %]
1303     [% Asset.js("lib/d3c3/d3.min.js") | $raw %]
1304     [% Asset.js("lib/d3c3/c3.min.js") | $raw %]
1305     [% INCLUDE 'calendar.inc' %]
1306     [% INCLUDE 'datatables.inc' %]
1307     [% IF ( saved1 ) %]
1308         [% INCLUDE 'columns_settings.inc' %]
1309     [% END %]
1310     [% Asset.js( "lib/codemirror/codemirror.min.js" ) | $raw %]
1311     [% Asset.js( "lib/codemirror/overlay.min.js" ) | $raw %]
1312     [% Asset.js( "lib/codemirror/sql.min.js" ) | $raw %]
1313     [% Asset.js( "js/mana.js" ) | $raw %]
1314     <script>
1315
1316         function hide_bar_element() {
1317             $('#chart-column-horizontal').hide()
1318             $('.chart-column-group').each(function( index ) {
1319                 $( this ).hide();
1320             });
1321             $('.chart-column-line').each(function( index ) {
1322                 $( this ).hide()
1323             });
1324         }
1325
1326         function show_bar_element() {
1327             $('#chart-column-horizontal').show()
1328             $('.chart-column-group').each(function( index ) {
1329                 $( this ).show()
1330             });
1331             $('.chart-column-line').each(function( index ) {
1332                 $( this ).show()
1333             });
1334         }
1335
1336         function removeColumn(id) {
1337             $('#'+id).remove();
1338
1339             if ( $('.chart-column-conf').length == 1 ) {
1340                 $('.chart-column-delete').remove();
1341             }
1342         }
1343
1344         /* overlay a syntax-highlighting definition on top of the existing sql one */
1345         CodeMirror.defineMode("sqlParams", function(config, parserConfig) {
1346             var sqlParamsOverlay = {
1347                 token: function(stream, state) {
1348                 var ch;
1349                 if (stream.match("<<")) {
1350                     while ((ch = stream.next()) != null)
1351                     if (ch == ">" && stream.next() == ">") {
1352                         stream.eat(">");
1353                         return "sqlParams";
1354                     }
1355                 }
1356                 while (stream.next() != null && !stream.match("<<", false)) {}
1357                 return null;
1358                 }
1359             };
1360             return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || "text/x-sql"), sqlParamsOverlay);
1361         });
1362
1363         var MSG_CONFIRM_DELETE = _("Are you sure you want to delete this report? This cannot be undone.");
1364         var group_subgroups = {};
1365         [% FOREACH group IN groups_with_subgroups %]
1366             var gid = "[% group.id | html %]"
1367             group_subgroups[gid] = new Array();
1368             [% FOREACH subgroup IN group.subgroups %]
1369                 var sgid = "[% subgroup.id | html %]";
1370                 var sgname = "[% subgroup.name | html %]";
1371                 group_subgroups[gid].push([sgid, sgname]);
1372             [% END %]
1373         [% END %]
1374
1375         [% IF ( create || editsql || save ) %]
1376
1377             var editor = CodeMirror.fromTextArea(sql, {
1378                 lineNumbers: true,
1379                 mode: "sqlParams", /* text/x-sql plus custom sqlParams configuration */
1380                 lineWrapping: true,
1381                 smartIndent: false
1382             });
1383
1384             // https://stackoverflow.com/questions/2086287/how-to-clear-jquery-validation-error-messages#answer-16025232
1385             function clearValidation( formElement ){
1386                 // formElement should be a jQuery object
1387                 var validator = formElement.validate();
1388                 // Iterate through named elements inside of the form, and mark them as error free
1389                 $('[name]',formElement).each(function(){
1390                     validator.successList.push(this);//mark as error free
1391                     validator.showErrors();//remove error messages if present
1392                 });
1393                 validator.resetForm();//remove error class on name elements and clear history
1394                 validator.reset();//remove all error and success data
1395             }
1396         [% END %]
1397
1398         [% IF ( showsql ) %]
1399             var editor = CodeMirror.fromTextArea(sql, {
1400                 lineNumbers: false,
1401                 mode: "sqlParams", /* text/x-sql plus custom sqlParams configuration */
1402                 lineWrapping: true,
1403                 readOnly: true
1404             });
1405         [% END %]
1406
1407         function load_group_subgroups () {
1408             var group = $("#group_select").val();
1409             var sg = $("#subgroup");
1410             $(sg).find('option[value!=""]').each(function() {
1411                 $(this).remove();
1412             });
1413             $(sg).hide();
1414             if (group) {
1415                 var select = $(sg).find('select')[0];
1416                 $.each( group_subgroups[group], function(index, value) {
1417                     $('<option value="' + value[0] + '">' + value[1] + '</option>').appendTo(select);
1418                 } );
1419                 $("#subgroup, #subgroup *").show();
1420             }
1421         }
1422
1423         $(document).ready(function(){
1424
1425             var activeTab = localStorage.getItem("sql_reports_activetab");
1426
1427             $("body").on('click',".fetch_chart_data",function(){
1428                 if( [% unlimited_total || 0 | $raw %] > 1000 ){
1429                     if( confirm("Fetching full chart data for reports with many rows can cause performance issues. Are you sure you with to chart this report?") ){
1430                         return true;
1431                     } else {
1432                         return false;
1433                     }
1434                 }
1435             });
1436
1437             var showsql;
1438             hide_bar_element();
1439
1440             if ( $('.chart-column-conf').length == 1 ) {
1441                 $('.chart-column-delete').remove();
1442             }
1443
1444             $(".chart-column-delete").on('click', function(e){
1445                 e.preventDefault();
1446                 removeColumn('column_' + $(this).data('column'));
1447             })
1448
1449             $('#download-chart').click(function() {
1450                 var svg = '<svg>' + $('#chart svg').html() + '</svg>';
1451                 this.href = 'data:application/octet-stream;base64,' + btoa(svg);
1452                 this.setAttribute('download', 'chart.svg');
1453             });
1454
1455             $('#chart-type').change(function() {
1456                 if ($(this).val() == 'bar') {
1457                     show_bar_element();
1458                 }
1459                 else {
1460                     hide_bar_element();
1461                 }
1462             });
1463
1464             $('#download-chart').hide();
1465             var chart;
1466
1467             [% IF results && !errors %]
1468                 $('#draw-chart').click(function() {
1469
1470                     var x_elements = $('select[name="x"]').val();
1471                     var y_elements = [];
1472                     var groups = [];
1473                     var lines = [];
1474                     var options = {};
1475
1476                     headers = [% header_row.json | $raw %];
1477
1478                     var results;
1479                     [% IF allresults.size %]
1480                         if ($('input[name="chart-include-all"]').prop('checked')) {
1481                             results = [% allresults.json | $raw %]
1482                         }
1483                         else {
1484                             results = [% results.json | $raw %]
1485                         }
1486                     [% ELSE %]
1487                         results = [% results.json | $raw %];
1488                     [% END %]
1489
1490                     if ($('input[name="chart-exclude-last"]').prop('checked')) {
1491                         results.splice(-1, 1);
1492                     }
1493
1494                     $('select[name="y"]').each(function( index ) {
1495                         y_elements.push( $(this).val() );
1496                     });
1497                     $('select[name="group"]').each(function( index ) {
1498                         groups.push( $(this).val() );
1499                     });
1500                     $('.column-line').each(function( index ) {
1501                         if ($(this).prop('checked')) {
1502                             lines.push( $(this).attr('name') );
1503                         }
1504                     });
1505
1506                     // Remove deleted columns from headers and results.
1507                     var deleted_indexes = [];
1508                     var kept_headers = [];
1509                     $.each(headers, function(index, value) {
1510                         if (value.cell != x_elements && $.inArray(value.cell, y_elements) === -1) {
1511                             // This header is neither a x element nor in y elements. Don't need it.
1512                             deleted_indexes.push(index);
1513                         }
1514                         else {
1515                             kept_headers.push({cell: value.cell});
1516                         }
1517                     });
1518
1519                     // Remove coresponding cells.
1520                     var kept_results = [];
1521                     $.each(results, function(index, value) {
1522                         var line = {};
1523                         line['cells'] = [];
1524                         $.each(value.cells, function(i, val) {
1525                             if ($.inArray(i, deleted_indexes) === -1) {
1526                                 line['cells'].push({cell: val.cell});
1527                             }
1528                         });
1529                         kept_results.push(line);
1530                     });
1531
1532                     options.type = $('select[name="chart-type"]').val();
1533                     options.horizontal = $('input[name="column-horizontal"]').prop('checked');
1534                     options.lines = lines;
1535
1536                     chart = create_chart(kept_headers, kept_results, x_elements, y_elements, groups, options);
1537                     $("#download-chart,#toggle_chart_settings_hid,#chart").show();
1538                     $("#toggle_chart_settings_vis").hide();
1539                     $("#chartModal").modal("hide");
1540                 });
1541             [% END %]
1542             [% IF ( create ) %]
1543                 load_group_subgroups();
1544             [% END %]
1545
1546             $('[data-toggle="tooltip"]').tooltip();
1547             var columns_settings = [% TablesSettings.GetColumns( 'reports', 'saved-sql', 'table_reports', 'json' ) | $raw %];
1548
1549             $('#limit').change(function() {
1550                 $('#limitselect').submit();
1551             });
1552
1553             $(document).click(function() {
1554                 $('#report_updated').hide();
1555             });
1556
1557             $(".goback").on("click",function(e){
1558                 e.preventDefault();
1559                 window.history.back();
1560             });
1561
1562             $("body").on("click", ".mana_search_button", function(){
1563                 $("#mana-loading").show();
1564                 mana_search($("#mana_search_field").val());
1565             });
1566
1567             $(".ShareButton").on("click", function(){
1568                 $("#note-error").hide();
1569                 if($(this).closest("tr").find(".report_notes").text().length < 20 || $(this).closest("tr").find(".report_name").text().length < 20){
1570                     $(".shared_infos").hide();
1571                     $("#note-error").show();
1572                 }
1573                 else{
1574                     $("#reportid").val($(this).closest("tr").find(".report_id").text());
1575                     $("#shared_id").html($(this).closest("tr").find(".report_id").text());
1576                     $("#shared_name").html($(this).closest("tr").find(".report_name").text());
1577                     $("#shared_sql").html($(this).closest("tr").find(".report_sql").val());
1578                     $("#shared_type").html($(this).closest("tr").find(".report_type").text());
1579                     $("#shared_group").html($(this).closest("tr").find(".report_group").text());
1580                     $("#shared_notes").html($(this).closest("tr").find(".report_notes").text());
1581                 }
1582             });
1583
1584             $("#ManaCloseButton").on("click", function() {
1585                 $(".shared_infos").show();
1586             });
1587
1588             $("#addColumn").on("click",function(){
1589                 addColumn();
1590             });
1591
1592             $("#delColumn").on("click",function(){
1593                 delColumn();
1594             });
1595
1596             [% IF (saved1) %]
1597                 var rtable = KohaTable("table_reports", {
1598                     'iDisplayLength': [% Koha.Preference('NumSavedReports') | html %],
1599                     'bAutoWidth': false,
1600                     'sPaginationType': 'full',
1601                     'aaSorting': [[ 1, "asc" ]],
1602                     'aoColumnDefs': [
1603                         { 'bSearchable': false, 'aTargets': [3] },
1604                         { "aTargets": [ 1, 2 ], "sType": "natural"  },
1605                         { "visible": false, "aTargets" : [ "hidden" ] }
1606                     ],
1607                     'oLanguage': {
1608                         'sZeroRecords': _("No matching reports found")
1609                     },
1610                 }, columns_settings);
1611
1612                 var rtabs = $("#tabs").tabs({
1613                     create: function( e, ui ){
1614                         tabsInit( ui, rtable );
1615                     },
1616                     activate: function( e, ui ){
1617                         tabsInit( ui, rtable );
1618                     }
1619                 });
1620                 if( activeTab ){
1621                     $("#tabs").tabs("option","active", activeTab );
1622                 }
1623                 $("#subgroup_filter_block").hide();
1624
1625                 $("#subgroup_filter").change(function() {
1626                     var selected = $(this).find('option:selected');
1627                     var sg_id = $(selected).val();
1628                     var sg_name = $(selected).text();
1629                     if (sg_id.length > 0) {
1630                         rtable.fnFilter('^' + sg_name + '$', 5, true, true, true, false);
1631                         rtable.fnSetColumnVis(5, false);
1632                     } else {
1633                         rtable.fnFilter('', 5);
1634                         rtable.fnSetColumnVis(5, true);
1635                     }
1636                 });
1637
1638                 $("#reports_form").submit(function(){
1639                     var checkedItems = $("input[name=ids]:checked");
1640                     if ($(checkedItems).size() == 0) {
1641                         alert(_("You must select one or more reports to delete"));
1642                         return false;
1643                     }
1644                     $(checkedItems).parents('tr').addClass("warn");
1645                     if( confirm(_("Are you sure you want to delete the selected reports?")) ) {
1646                         return true;
1647                     } else {
1648                         $(checkedItems).parents('tr').removeClass("warn");
1649                         return false;
1650                     }
1651                 });
1652
1653                 $("body").on("click", ".update_sql", function(e){
1654                     e.preventDefault();
1655                     var ltitle = $(this).text();
1656                     var report_id = $(this).data("report_id");
1657                     var page = $(this).attr("href");
1658                     $("#update_sql .modal-body").load(page + " div", function(){
1659                         var diff1 = $("#col1 .show_sql").text();
1660                         var diff2 = $("#col2 .show_sql").text();
1661                         var diffs = diffString( escape(diff1), escape(diff2) );
1662                         $("#col1 .show_sql,#col2 .show_sql").html(diffs);
1663                     });
1664                     $('#update_sql').modal('show');
1665                     $("#update_sql_button").attr("href", "/cgi-bin/koha/reports/guided_reports.pl?phase=Use saved&op=convert&report_id=" + report_id);
1666                 });
1667
1668                 $("#update_sql").on("hidden.bs.modal", function(){
1669                     $("#update_sql_label").html("");
1670                     $("#update_sql .modal-body").html("<div id=\"loading\"><img src=\"[% interface | html %]/[% theme | html %]/img/spinner-small.gif\" alt=\"\" /> "+_("Loading")+"</div>");
1671                 });
1672             [% END %]
1673
1674             [% IF ( showsql ) %]
1675                 $("#sql").focus(function() {
1676                     $(this).select();
1677                 });
1678             [% END %]
1679
1680             $(".toggle_sql").click(function(){
1681                 $("#sql_output").toggle();
1682                 $("#toggle_sql_hid").toggle();
1683                 $("#toggle_sql_vis").toggle();
1684                 if( !showsql ){
1685                     showsql = CodeMirror.fromTextArea(sql, {
1686                         lineNumbers: false,
1687                         mode: "text/x-sql",
1688                         lineWrapping: true,
1689                         readOnly: true
1690                     });
1691                 }
1692             });
1693
1694             $(".toggle_chart_settings").click(function(){
1695                 $("#chart, #toggle_chart_settings_hid, #toggle_chart_settings_vis").toggle();
1696             });
1697
1698             $("#table_reports").delegate(".confirmdelete", 'click', function(){
1699                 $(this).parents('tr').attr("class","warn");
1700                 if(confirm(_("Are you sure you want to delete this saved report?"))){
1701                     return true;
1702                 } else {
1703                     $(this).parents('tr').attr("class","");
1704                     return false;
1705                 }
1706             });
1707
1708             [% IF (create || editsql || save) %]
1709
1710                 var validated_form = $("#sql_report_form").validate({
1711                     reportname: "required",
1712                     group_input: {
1713                         required: {
1714                             depends: function(element) {
1715                                 return $("#create_group").prop("checked") && $("#groupdesc_input").val() != '';
1716                             }
1717                         }
1718                     },
1719                     groupdesc_input: {
1720                         required: {
1721                             depends: function(element) {
1722                                 return $("#create_group").prop("checked") && $("#group_input").val() != '';
1723                             }
1724                         }
1725                     }
1726                 });
1727
1728                 $("#select_group").change(function() {
1729                     if($(this).prop('checked')) {
1730                         $("#group_input").attr("class","").prop('disabled', true).prop("required", false);
1731                         $("#groupdesc_input").attr("class","").prop('disabled', true).prop("required", false);
1732                         $("#group_select").prop('disabled', false);
1733                         clearValidation( $("#sql_report_form") );
1734                         if ($("#group_select").val().length > 0) {
1735                             $("#select_subgroup").prop('checked', true);
1736                             $("#select_subgroup").change();
1737                             $("#subgroup, #subgroup *").show();
1738                         } else {
1739                             $("#subgroup").hide();
1740                         }
1741                     }
1742                 });
1743                 $("#create_group").change(function() {
1744                     if($(this).prop('checked')) {
1745                         $("#group_input").prop('disabled', false).prop("required", true );
1746                         $("#groupdesc_input").prop('disabled', false).prop("required", true );
1747                         $("#group_select").prop('disabled', true);
1748                         // $("#create_subgroup").prop('checked', true).change();
1749                         $("#subgroup_select").hide();
1750                         $("#subgroup input[type='radio']").hide();
1751                         $("#subgroup label[for]").hide();
1752                         $("#subgroup_input").prop("required", false ).prop("disabled", false).show();
1753                         $("#subgroupdesc_input").prop("required", false ).prop("disabled", false).show();
1754                         $("#subgroup").show();
1755                         // Add validation rules for fields which were previously hidden
1756                         $("#subgroup_input").rules("add", {
1757                             required: {
1758                                 depends: function(element) {
1759                                     return $("#create_group").prop("checked") &&  $("#subgroupdesc_input").val() != '';
1760                                 }
1761                             }
1762                         });
1763                         $("#subgroupdesc_input").rules("add", {
1764                             required: {
1765                                 depends: function(element) {
1766                                     return $("#create_group").prop("checked") &&  $("#subgroup_input").val() != '';
1767                                 }
1768                             }
1769                         });
1770                     }
1771                 });
1772                 $("#select_subgroup").change(function() {
1773                     if($(this).prop('checked')) {
1774                         $("#subgroup_select").prop('disabled', false);
1775                         $("#subgroup_input").prop('disabled', true).prop("required", false );
1776                         $("#subgroupdesc_input").prop('disabled', true).prop("required", false );
1777                         clearValidation( $("#sql_report_form") );
1778                     }
1779                 });
1780                 $("#create_subgroup").change(function() {
1781                     if($(this).prop('checked')) {
1782                         $("#subgroup_input").prop('disabled', false).prop("required", true );
1783                         $("#subgroupdesc_input").prop('disabled', false).prop("required", true );
1784                         $("#subgroup_select").val("").prop('disabled', true);
1785                     }
1786                 });
1787                 $("#select_group").change();
1788                 $("#select_subgroup").change();
1789                 $("#group_select").on("change",function(){
1790                     load_group_subgroups();
1791                 });
1792             [% END %]
1793
1794             $(".delete").on("click",function(){
1795                 return confirmDelete(MSG_CONFIRM_DELETE);
1796             });
1797
1798             $("#mana_search_form").submit(function(e){
1799                 e.preventDefault();
1800             });
1801
1802             $("#column_submit").submit(function() {
1803                 if ($("#selectedColumns option").size() < 1) {
1804                     alert(_("No columns selected!"));
1805                     return false;
1806                 }
1807                 $("#selectedColumns option").attr("selected", "selected");  // Select everything still in #selectedColumns
1808                 return true;
1809             });
1810
1811             // Mana KB
1812             $("body").on("click", ".mana-use", function(e) {
1813                 e.preventDefault();
1814                 $(this).find("i").attr("class","fa fa-refresh fa-spin");
1815                 mana_use( $(this).data("report_id") );
1816             });
1817
1818             $("#mana_search_result").on("hide.bs.modal", function(){
1819                 $("#mana_result_content").html("");
1820                 $("#mana_search_field").val("");
1821             });
1822
1823             $(".batch_op").on("click", function(e){
1824                 e.preventDefault();
1825                 var target_form = $(this).data("submit");
1826                 $("#" + target_form ).submit();
1827             });
1828
1829             $("#batch_add_to_list").on("submit", function(e){
1830                 e.preventDefault();
1831                 addToList();
1832             });
1833
1834             $("body").on("click", ".preview_sql", function(e){
1835                 e.preventDefault();
1836                 var reportid = $(this).data("reportid");
1837                 previewSql( reportid );
1838             });
1839         });
1840
1841         function tabsInit( ui, rtable ){
1842             var thisTab = ui.newTab ? ui.newTab : ui.tab;
1843             var activeTab = thisTab.index();
1844             localStorage.setItem("sql_reports_activetab", activeTab );
1845
1846             $("#subgroup_filter option").each(function() {
1847                 if($(this).val().length > 0) {
1848                     $(this).remove();
1849                 }
1850             });
1851             rtable.fnFilter('', 4);
1852             rtable.fnFilter('', 5);
1853             rtable.fnSetColumnVis(4, true);
1854             rtable.fnSetColumnVis(5, true);
1855
1856             var g_id = $(ui.newTab).children().attr('id');
1857             var g_name = $(ui.newTab).text();
1858             if ( g_name == _("All") ) {
1859                 g_id = "";
1860                 g_name = "";
1861             }
1862
1863             if (g_id && g_id.length > 0) {
1864                 rtable.fnFilter('^' + g_name + '$', 4, true, true, true, false);
1865                 rtable.fnSetColumnVis(4, false);
1866                 for(var i in group_subgroups[g_id]) {
1867                     $("#subgroup_filter").append(
1868                         '<option value="' + group_subgroups[g_id][i][0] + '">'
1869                         + group_subgroups[g_id][i][1] + '</option>'
1870                     );
1871                 }
1872                 $("#subgroup_filter_block").show();
1873             } else {
1874                 $("#subgroup_filter_block").hide();
1875             }
1876         }
1877
1878         function addColumn() {
1879             $("#availableColumns option:selected").clone().appendTo("#selectedColumns").attr("selected", "selected");
1880         }
1881         function delColumn() {
1882             $("#selectedColumns option:selected").remove();
1883         }
1884
1885         // Mana KB
1886         function mana_use( mana_id ){
1887             $.ajax( {
1888                 type:"POST",
1889                 url: "/cgi-bin/koha/svc/mana/use",
1890                 data: {id:mana_id, resource: 'report', saveinbase: 1},
1891                 dataType: "json",
1892             })
1893             .done( function (result){
1894                 if ( result.errmsg ){
1895                     alert( result.errmsg );
1896                 }
1897                 else{
1898                     window.location = ("/cgi-bin/koha/reports/guided_reports.pl?reports=").concat(result.id).concat("&phase=Show%20SQL&mana_success=1");
1899                 }
1900             })
1901             .fail(function( error ) {
1902                 $(".mana_use_status").hide();
1903                 $("#mana_use_errortext").html( error.status + " " + error.statusText );
1904                 $("#mana_use_failed").show();
1905                 $(".mana-use i").attr("class","fa fa-download");
1906             });
1907         }
1908
1909         function mana_search( textquery ){
1910             $(".mana_search_status").hide();
1911             $("#mana_result_content").load("/cgi-bin/koha/svc/mana/search #mana_results", { resource: 'report', id: textquery, usecomments: 1 }, function( response, status, xhr ) {
1912
1913                     if ( status == "error" ) {
1914                         $("#mana_search_errortext").html( xhr.status + " " + xhr.statusText );
1915                         $("#mana_search_failed").show();
1916                         $("#mana-loading").hide();
1917                     } else {
1918                         $(".mana_search_status").hide();
1919                         $("#mana_search_result_label").text(_("Results from Mana Knowledge Base"));
1920                         $("#mana-loading").hide();
1921                         $("#mana_results_datatable").dataTable($.extend(true, {}, dataTablesDefaults,{
1922                             "sPaginationType":"full",
1923                             "autoWidth": false,
1924                             "columnDefs": [
1925                                 { "width": "35%", "targets": 1 }
1926                             ],
1927                             "aoColumnDefs": [
1928                                 { 'bSortable': false, "bSearchable": false, 'aTargets': [ 'NoSort' ] },
1929                                 { "sType": "title-string", "aTargets" : [ "title-string" ] },
1930                                 { 'sType': "anti-the", 'aTargets' : [ 'anti-the'] }
1931                             ]
1932                         }));
1933
1934                         $(".showbutton").on("click", function(e){
1935                             e.preventDefault();
1936                             $(this).parent().hide();
1937                             $(this).parent().next().show();
1938                         });
1939
1940                         $(".hidebutton").on("click", function(e){
1941                             e.preventDefault();
1942                             $(this).parent().hide();
1943                             $(this).parent().prev().show();
1944                         });
1945
1946                         if($("td.dataTables_empty").length == 0){
1947                             $("#mana_search_message").show();
1948                         }
1949                     }
1950                 });
1951         }
1952
1953         function addToList() {
1954             var biblionumbers = [];
1955             $(".bib_to_list").each(function() {
1956                 var biblionumber = Number( $(this).val() );
1957                 if( biblionumbers.indexOf( biblionumber ) < 0 ){
1958                     biblionumbers.push( biblionumber );
1959                 }
1960             });
1961             bibs = biblionumbers.join("/");
1962             var url = "/cgi-bin/koha/virtualshelves/addbybiblionumber.pl?biblionumbers=" + bibs;
1963             window.open(url, 'Add_to_virtualshelf', 'width=500, height=400, toolbar=false, scrollbars=yes');
1964             return false;
1965         }
1966
1967         // Adapted from https://gist.github.com/jnormore/7418776
1968         function previewSql(reportid) {
1969             var yes_label = "";
1970             var no_label = "";
1971             var message = $("#previewSql" + reportid ).val();
1972             var title = $("#previewSql" + reportid ).data("title");
1973             if( $("#preview-sql-modal").length > 0) {
1974                 $("#preview-sql-modal").remove();
1975             }
1976             $("body").append('<div id="preview-sql-modal" tabindex="-1" role="dialog" aria-hidden="true" class="modal">\
1977                 <div class="modal-dialog">\
1978                     <div class="modal-content">\
1979                         <div class="modal-header" style="min-height:40px;">\
1980                             <button type="button" class="closebtn" data-dismiss="modal" aria-label="Close">\
1981                                 <span aria-hidden="true">×</span>\
1982                             </button>\
1983                             <h4 class="modal-title">' + title + '</h4>\
1984                         </div>\
1985                         <div class="modal-body"><textarea id="code' + reportid + '">' + message + '</textarea>\
1986                         <div class="modal-footer">\
1987                             <a id="preview-modal-editreport" class="btn btn-default" href="/cgi-bin/koha/reports/guided_reports.pl?reports=' + reportid + '&amp;phase=Edit%20SQL"><i class="fa fa-pencil"></i> Edit</a>\
1988                             <a id="preview-modal-duplicate" class="btn btn-default" href="/cgi-bin/koha/reports/guided_reports.pl?phase=Create report from existing&amp;report_id=' + reportid + '"><i class="fa fa-copy"></i> Duplicate</a>\
1989                             <a id="preview-modal-duplicate" class="btn btn-default" href="/cgi-bin/koha/tools/scheduler.pl?id=' + reportid + '"><i class="fa fa-clock-o"></i> Schedule</a>\
1990                             <a id="preview-modal-delete" class="delete btn btn-default" href="/cgi-bin/koha/reports/guided_reports.pl?reports=' + reportid + '&amp;phase=Delete%20Saved"><i class="fa fa-trash"></i> Delete</a>\
1991                             <a id="preview-modal-runreport" class="btn btn-default" href="/cgi-bin/koha/reports/guided_reports.pl?reports=' + reportid + '&amp;phase=Run%20this%20report"><i class="fa fa-play"></i> Run report</a>\
1992                             <a href="#" id="preview-sql-modal-cancel" data-dismiss="modal" class="btn btn-default"><i class="fa fa-remove" aria-hidden="true"></i> Close</a>\
1993                         </div>\
1994                     </div>\
1995                 </div>\
1996             </div>');
1997
1998             $("#preview-sql-modal").modal('show');
1999             CodeMirror.fromTextArea( document.getElementById("code" + reportid ), {
2000                 lineNumbers: false,
2001                 mode: "text/x-sql",
2002                 lineWrapping: true,
2003                 readOnly: true
2004             });
2005         }
2006     </script>
2007 [% END %]
2008
2009 [% INCLUDE 'intranet-bottom.inc' %]
2010
2011 [% BLOCK group_and_subgroup_selection %]
2012     <li id="group">
2013         <label>Report group:</label>
2014         <input type="radio" name="select_or_create_group"
2015             id="select_group" checked="checked" />
2016         <label for="select_group" style="float:none">Select</label>
2017         <select name="group" id="group_select">
2018             <option value="">(None)</option>
2019             [% FOREACH group IN groups_with_subgroups %]
2020                 [% IF (group.selected) %]
2021                     <option value="[% group.id | html %]" selected="selected">
2022                 [% ELSE %]
2023                     <option value="[% group.id | html %]">
2024                 [% END %]
2025                     [% group.name | html %]
2026                 </option>
2027             [% END %]
2028         </select>
2029         <input type="radio" name="select_or_create_group" id="create_group" />
2030         <label for="create_group" style="float:none">or create:</label>
2031         <input type="text" name="group" id="group_input" title="Group code" placeholder="Code" />
2032         <input type="text" name="groupdesc" id="groupdesc_input" title="Group name" placeholder="Name" />
2033     </li>
2034     <li id="subgroup">
2035         <label>Report subgroup:</label>
2036         <input type="radio" name="select_or_create_subgroup"
2037             id="select_subgroup" checked="checked" />
2038         <label for="select_subgroup" style="float:none">Select</label>
2039         <select name="subgroup" id="subgroup_select">
2040             <option value="">(None)</option>
2041             [% FOREACH group IN groups_with_subgroups %]
2042                 [% IF (group.selected) %]
2043                     [% FOREACH subgroup IN group.subgroups %]
2044                         [% IF (subgroup.selected) %]
2045                             <option value="[% subgroup.id | html %]" selected="selected">
2046                         [% ELSE %]
2047                             <option value="[% subgroup.id | html %]">
2048                         [% END %]
2049                             [% subgroup.name | html %]
2050                         </option>
2051                     [% END %]
2052                 [% END %]
2053             [% END %]
2054         </select>
2055         <input type="radio" name="select_or_create_subgroup"
2056             id="create_subgroup" />
2057         <label for="create_subgroup" style="float:none">or create</label>
2058         <input type="text" name="subgroup" id="subgroup_input" title="Subgroup code" placeholder="Code" />
2059         <input type="text" name="subgroupdesc" id="subgroupdesc_input" title="Subgroup name" placeholder="Name" />
2060     </li>
2061 [% END %]