Bug 26703: reports folder
[koha-ffzg.git] / koha-tmpl / intranet-tmpl / prog / en / modules / reports / guided_reports_start.tt
index fee6ecc..a002e40 100644 (file)
@@ -2,7 +2,7 @@
 [% USE Asset %]
 [% USE KohaDates %]
 [% USE Koha %]
-[% USE ColumnsSettings %]
+[% USE TablesSettings %]
 [% USE JSON.Escape %]
 [% SET footerjs = 1 %]
 
 [% INCLUDE 'doc-head-open.inc' %]
 
 <title>
-    Koha &rsaquo; Reports &rsaquo; Guided reports wizard
     [%- IF ( saved1 ) -%]
-        &rsaquo; Saved reports
-    [%- ELSIF ( create ) -%]
-        &rsaquo; Create from SQL
-    [%- ELSIF ( showsql ) -%]
-        &rsaquo; Saved reports &rsaquo; SQL view
-    [%- ELSIF ( execute ) -%]
-        &rsaquo; Saved reports &rsaquo; [% name | html %] Report
-    [%- ELSIF ( editsql ) -%]
-        &rsaquo; Saved reports &rsaquo; Edit SQL report
-    [%- END -%]
+        Saved reports &rsaquo; [%- ELSIF ( create ) -%]
+        Create from SQL &rsaquo; [%- ELSIF ( showsql ) -%]
+        SQL view &rsaquo; Saved reports &rsaquo; [%- ELSIF ( execute ) -%]
+        Report [% name | html %] ([% id | html %]) &rsaquo; Saved reports &rsaquo; [%- ELSIF ( editsql ) -%]
+        Edit report [% reportname | html %] ([% id | html %]) &rsaquo; Saved reports &rsaquo; [%- END -%]
+
     [%- IF ( build1 ) -%]
-        &rsaquo; Build a report, step 1 of 6: Choose a module
-    [%- ELSIF ( build2 ) -%]
-        &rsaquo; Build a report, step 2 of 6: Pick a report type
-    [%- ELSIF ( build3 ) -%]
-        &rsaquo; Build a report, step 3 of 6: Select columns for display
-    [%- ELSIF ( build4 ) -%]
-        &rsaquo; Build a report, step 4 of 6: Select criteria to limit on
-    [%- ELSIF ( build5 ) -%]
-        &rsaquo; Build a report, step 5 of 6: Pick which columns to total
-    [%- ELSIF ( build6 ) -%]
-        &rsaquo; Build a report, step 6 of 6: Select how you want the report ordered
-    [%- END -%]
+        Build a report, step 1 of 6: Choose a module &rsaquo; [%- ELSIF ( build2 ) -%]
+        Build a report, step 2 of 6: Pick a report type &rsaquo; [%- ELSIF ( build3 ) -%]
+        Build a report, step 3 of 6: Select columns for display &rsaquo; [%- ELSIF ( build4 ) -%]
+        Build a report, step 4 of 6: Select criteria to limit on &rsaquo; [%- ELSIF ( build5 ) -%]
+        Build a report, step 5 of 6: Pick which columns to total &rsaquo; [%- ELSIF ( build6 ) -%]
+        Build a report, step 6 of 6: Select how you want the report ordered &rsaquo; [%- END -%]
+    Guided reports wizard &rsaquo; Reports &rsaquo; Koha
 </title>
 
 [% INCLUDE 'doc-head-close.inc' %]
@@ -53,6 +43,9 @@
     .CodeMirror {
         resize:  vertical;
     }
+    .cm-sqlParams {
+        color: #11917B;
+    }
     #mana_search_errortext { font-family: monospace; font-weight: bold; }
 </style>
 [% Asset.css("css/reports.css") | $raw %]
 [% INCLUDE 'header.inc' %]
 [% INCLUDE 'circ-search.inc' %]
 
-<div id="breadcrumbs">
-    <a href="/cgi-bin/koha/mainpage.pl">Home</a>
-    &rsaquo; <a href="/cgi-bin/koha/reports/reports-home.pl">Reports</a>
-    &rsaquo; <a href="/cgi-bin/koha/reports/guided_reports.pl">Guided reports wizard</a>
-
-    [% IF ( saved1 ) %]
-        &rsaquo; Saved reports
-    [% ELSIF ( create ) %]
-        &rsaquo; Create from SQL
-    [% ELSIF ( showsql ) %]
-        &rsaquo; <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved">Saved reports</a> &rsaquo; SQL view
-    [% ELSIF ( editsql ) %]
-        &rsaquo; <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved">Saved reports</a> &rsaquo; Edit SQL report
-    [% ELSIF ( execute ) %]
-        &rsaquo; <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved">Saved reports</a> &rsaquo; <em>[% name | html %]</em> Report
-    [% ELSIF ( build1 || build2 || build3 || build4 || build5 || build6 ) %]
-        &rsaquo; <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Build%20new">Build a report</a>
-        [% IF ( build1 ) %]
-            &rsaquo; Step 1 of 6: Choose a module
-        [% ELSIF ( build2 ) %]
-            &rsaquo; Step 2 of 6: Pick a report type
-        [% ELSIF ( build3 ) %]
-            &rsaquo; Step 3 of 6: Select columns for display
-        [% ELSIF ( build4 ) %]
-            &rsaquo; Step 4 of 6: Select criteria to limit on
-        [% ELSIF ( build5 ) %]
-            &rsaquo; Step 5 of 6: Pick which columns to total
-        [% ELSIF ( build6 ) %]
-            &rsaquo; Step 6 of 6: Select how you want the report ordered
+<nav id="breadcrumbs" aria-label="Breadcrumb" class="breadcrumb">
+    <ol>
+        <li>
+            <a href="/cgi-bin/koha/mainpage.pl">Home</a>
+        </li>
+        <li>
+            <a href="/cgi-bin/koha/reports/reports-home.pl">Reports</a>
+        </li>
+        <li>
+            <a href="/cgi-bin/koha/reports/guided_reports.pl">Guided reports wizard</a>
+        </li>
+
+        [% IF ( saved1 ) %]
+            <li>
+                <a href="#" aria-current="page">
+                    Saved reports
+                </a>
+            </li>
+        [% ELSIF ( create ) %]
+            <li>
+                <a href="#" aria-current="page">
+                    Create from SQL
+                </a>
+            </li>
+        [% ELSIF ( showsql ) %]
+            <li>
+                <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved">Saved reports</a>
+            </li>
+            <li>
+                <a href="#" aria-current="page">
+                    [% reportname | html %] ([% id | html %])
+                </a>
+            </li>
+        [% ELSIF ( editsql ) %]
+            <li>
+                <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved">Saved reports</a>
+            </li>
+            <li>
+                <a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% id | uri %]&amp;phase=Show%20SQL">[% reportname | html %] ([% id | html %])</a>
+            </li>
+            <li>
+                <a href="#" aria-current="page">
+                    Edit
+                </a>
+            </li>
+        [% ELSIF ( execute ) %]
+            <li>
+                <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved">Saved reports</a>
+            </li>
+            <li>
+                <a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% id | uri %]&amp;phase=Show%20SQL">[% name | html %] ([% id | html %])</a>
+            </li>
+            <li>
+                <a href="#" aria-current="page">
+                    Run
+                </a>
+            </li>
+        [% ELSIF ( build1 || build2 || build3 || build4 || build5 || build6 ) %]
+            <li>
+                <a href="/cgi-bin/koha/reports/guided_reports.pl?phase=Build%20new">Build a report</a>
+            </li>
+            <li>
+                <a href="#" aria-current="page">
+                    [% IF ( build1 ) %]
+                        Step 1 of 6: Choose a module
+                    [% ELSIF ( build2 ) %]
+                        Step 2 of 6: Pick a report type
+                    [% ELSIF ( build3 ) %]
+                        Step 3 of 6: Select columns for display
+                    [% ELSIF ( build4 ) %]
+                        Step 4 of 6: Select criteria to limit on
+                    [% ELSIF ( build5 ) %]
+                        Step 5 of 6: Pick which columns to total
+                    [% ELSIF ( build6 ) %]
+                        Step 6 of 6: Select how you want the report ordered
+                    [% END %]
+                </a>
         [% END %]
-    [% END %]
-</div> <!-- /#breadcrumbs -->
+    </ol>
+</nav> <!-- /#breadcrumbs -->
 
 <div id="update_sql" class="modal" tabindex="-1" role="dialog" aria-labelledby="update_sql_label" aria-hidden="true">
     <div class="modal-dialog">
                                     <table id="table_reports">
                                         <thead>
                                             <tr>
-                                                <th>&nbsp;</th>
+                                                <th class="NoSort">&nbsp;</th>
                                                 <th>ID</th>
                                                 <th>Report name</th>
                                                 <th>Type</th>
                                                 <th>Subgroup</th>
                                                 <th>Notes</th>
                                                 <th>Author</th>
-                                                <th class="title-string">Creation date</th>
-                                                <th class="title-string">Last edit</th>
-                                                <th class="title-string">Last run</th>
+                                                <th>Creation date</th>
+                                                <th>Last edit</th>
+                                                <th>Last run</th>
                                                 <th class="report_public">Public</th>
                                                 <th class="report_json_url">JSON URL</th>
                                                 [% IF (usecache) %]
                                                 [% ELSE %]
                                                     <th class="hidden">&nbsp;</th>
                                                 [% END %]
-                                                <th>Actions</th>
+                                                <th class="NoSort noExport">Actions</th>
                                             </tr>
                                         </thead>
                                         <tbody>
                                                 [% UNLESS ( loop.odd ) %]<tr class="odd">[% ELSE %]<tr>[% END %]
                                                     <td class="report_checkbox">
                                                         [% IF ( CAN_user_reports_delete_reports ) %] <!-- not break CSS -->
-                                                            <input type="checkbox" name="ids" value="[% savedreport.id | html %]" />
+                                                            <input type="checkbox" name="ids" id="ids[% savedreport.id | html %]" value="[% savedreport.id | html %]" />
                                                         [% END %]
                                                         <input type="hidden" class="report_sql" value="[% savedreport.savedsql |html %]">
                                                     </td>
                                                     <td class="report_id">
-                                                        <label for="ids">[% savedreport.id | html %]</label>
+                                                        <label for="ids[% savedreport.id | html %]">[% savedreport.id | html %]</label>
                                                     </td>
                                                     <td class="report_name">
                                                         [% IF ( savedreport.report_name ) %]
                                                         [% END %]
                                                         ([% savedreport.borrowernumber | html %])
                                                     </td>
-                                                    <td>
-                                                        <span title="[% savedreport.date_created | html %]">[% savedreport.date_created | $KohaDates %]</span>
+                                                    <td data-order="[% savedreport.date_created | html %]">
+                                                        [% savedreport.date_created | $KohaDates %]
                                                     </td>
-                                                    <td>
-                                                        <span title="[% savedreport.last_modified | html %]">[% savedreport.last_modified | $KohaDates  with_hours => 1 %]</span>
+                                                    <td data-order="[% savedreport.last_modified | html %]">
+                                                        [% savedreport.last_modified | $KohaDates  with_hours => 1 %]
                                                     </td>
-                                                    <td>
-                                                        <span title="[% savedreport.last_run | html %]">[% savedreport.last_run | $KohaDates  with_hours => 1 %]</span>
+                                                    <td data-order="[% savedreport.last_run | html %]">
+                                                        [% savedreport.last_run | $KohaDates  with_hours => 1 %]
                                                     </td>
                                                     <td class="report_public">
                                                         [% IF (savedreport.public) %]
                                                         [% END %]
                                                     </td>
                                                     <td>
-                                                        <div class="dropup">
-                                                            <div class="btn-group">
-                                                                [%# There should be no space between these two buttons, it would render badly %]
-                                                                <a class="btn btn-default btn-xs" role="button"
-                                                                   href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id | html %]&amp;phase=Run%20this%20report"><i
-                                                                   class="fa fa-play"></i> Run</a><a
-                                                                   class="btn btn-default btn-xs dropdown-toggle" id="reportactions[% savedreport.id | html %]" role="button" data-toggle="dropdown"
-                                                                   href="#"><b class="caret"></b></a>
-                                                                <ul class="dropdown-menu pull-right" role="menu" aria-labelledby="reportactions[% savedreport.id | html %]">
-                                                                    <li><a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id | uri %]&amp;phase=Show%20SQL"><i class="fa fa-search"></i> Show</a></li>
-                                                                    [% IF ( CAN_user_reports_create_reports ) %]
-                                                                        <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>
-                                                                        <li><a title="Duplicate this saved report" href="/cgi-bin/koha/reports/guided_reports.pl?phase=Create report from SQL&amp;sql=[% savedreport.savedsql |uri %]&amp;reportname=[% savedreport.report_name |uri %]&amp;notes=[% savedreport.notes |uri %]"><i class="fa fa-copy"></i> Duplicate</a></li>
-                                                                    [% END %]
-                                                                    [% IF (Koha.Preference('Mana') == 1) %]
-                                                                        <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>
-                                                                    [% END %]
-                                                                    <li><a href="/cgi-bin/koha/tools/scheduler.pl?id=[% savedreport.id | uri %]"><i class="fa fa-clock-o"></i> Schedule</a></li>
-                                                                    [% IF ( CAN_user_reports_delete_reports ) %]
-                                                                        <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>
-                                                                    [% END %]
-                                                                </ul>
-                                                            </div>
+                                                        <div class="btn-group dropup">
+                                                            [%# There should be no space between these two buttons, it would render badly %]
+                                                            <a class="btn btn-default btn-xs" role="button"
+                                                               href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id | html %]&amp;phase=Run%20this%20report"><i
+                                                               class="fa fa-play"></i> Run</a><a
+                                                               class="btn btn-default btn-xs dropdown-toggle" id="reportactions[% savedreport.id | html %]" role="button" data-toggle="dropdown"
+                                                               href="#"><b class="caret"></b></a>
+                                                            <ul class="dropdown-menu pull-right" role="menu" aria-labelledby="reportactions[% savedreport.id | html %]">
+                                                                <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>
+                                                                <li>
+                                                                    <a href="/cgi-bin/koha/reports/guided_reports.pl?reports=[% savedreport.id | uri %]&phase=Show%20SQL" class="preview_sql" data-reportid="[% savedreport.id | html %]">
+                                                                        <i class="fa fa-eye"></i> Preview SQL
+                                                                    </a>
+                                                                </li>
+                                                                [% IF ( CAN_user_reports_create_reports ) %]
+                                                                    <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>
+                                                                    <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>
+                                                                [% END %]
+                                                                [% IF (Koha.Preference('Mana') == 1) %]
+                                                                    <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>
+                                                                [% END %]
+                                                                <li><a href="/cgi-bin/koha/tools/scheduler.pl?id=[% savedreport.id | uri %]"><i class="fa fa-clock-o"></i> Schedule</a></li>
+                                                                [% IF ( CAN_user_reports_delete_reports ) %]
+                                                                    <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>
+                                                                [% END %]
+                                                            </ul>
                                                         </div>
+                                                        <input type="hidden" id="previewSql[% savedreport.id | html %]" value="[% savedreport.savedsql | html %]" data-title="[% savedreport.report_name | html %]" />
                                                     </td>
                                                 </tr>
                                             [% END %]
                 [% IF ( build1 ) %]
                     [% IF ( cache_error) %]
                         <div class="dialog alert">
-                            <b> Please choose a cache_expiry less than 30 days </b>
+                            <strong> Please choose a cache_expiry less than 30 days </strong>
                         </div>
                     [% END %]
 
                         <input type="hidden" name="cache_expiry" value="[% cache_expiry | html %]" />
                         <p>You will need to save the report before you can execute it</p>
                         <fieldset class="action"><input type="hidden" name="phase" value="Save" />
-                            <input type="submit" name="submit" value="Save" />
+                            <input type="submit" name="submit" value="Next" />
                         </fieldset>
                     </form>
                 [% END #/ IF ( showreport ) %]
                                                 <label for="sql_params[% loop.count | html %]">[% sql_param.entry | html %]: </label>
                                                 <input id="sql_params[% loop.count | html %]" type="text" name="sql_params" />
                                             </li>
+                                        [% ELSIF ( sql_param.input == 'textarea' ) %]
+                                            <li>
+                                                <label for="sql_params[% loop.count | html %]">[% sql_param.entry | html %]: </label>
+                                                <textarea id="sql_params[% loop.count | html %]" name="sql_params" rows="5"></textarea>
+                                            </li>
                                         [% ELSE %]
                                             <li>
                                                 <label for="sql_params_[% sql_param.labelid | html %]">[% sql_param.entry | html %]:</label>
-                                                <select name="[%- sql_param.input.name | html -%]" tabindex="1"  size="1" id="[%- sql_param.input.id | html -%]">
+                                                <select name="[%- sql_param.input.name | html -%]" tabindex="1"  id="[%- sql_param.input.id | html -%]">
                                                     [% IF (sql_param.include_all) %]
                                                         <option value="%">All</option>
                                                     [% END %]
                 [% END # /IF ( enter_params ) %]
 
                 [% IF ( execute ) %]
-                    <h1>[% name | html %]</h1>
+                    <h1>[% name | html %] <span id="report_id">([% id | html %])</span></h1>
                     [% IF ( notes ) %]
                         <p><span class="label">Notes:</span> [% notes | html %]</p>
                     [% END %]
                                 <input type="hidden" name="recordtype" value="biblio" />
                                 <input type="hidden" name="op" value="list" />
                                 [% # Preserve the whitespace of the following textarea in order to format the values correctly %]
-                                <textarea style="display:none" name="recordnumber_list" id="recordnumber_list">[% FOREACH result IN results %][% FOREACH cells IN result.cells %][% place = loop.index %][% IF header_row.$place.cell == 'biblionumber' || header_types.item(header_row.$place.cell) == 'biblionumber' %][% cells.cell | html %][% END %][% END %][% END %]</textarea>
+                                <textarea style="display:none" name="recordnumber_list" id="recordnumber_list">
+                                    [%- recordnumbers = PROCESS batch_list results=results batch_type='biblionumber' | trim | html %][% IF recordnumbers %][% SET batch_biblionumbers = 1 %][% recordnumbers | html %][% END -%]
+                                </textarea>
                             </form>
 
                             <form method="POST" action="/cgi-bin/koha/tools/batch_delete_records.pl" id="batch_record_deletion">
                                 <input type="hidden" name="recordtype" value="biblio" />
                                 <input type="hidden" name="op" value="list" />
                                 [% # Preserve the whitespace of the following textarea in order to format the values correctly %]
-                                <textarea style="display:none" name="recordnumber_list" id="recordnumber_list">[% FOREACH result IN results %][% FOREACH cells IN result.cells %][% place = loop.index %][% IF header_row.$place.cell == 'biblionumber' || header_types.item(header_row.$place.cell) == 'biblionumber' %][% cells.cell | html %][% END %][% END %][% END %]</textarea>
+                                <textarea style="display:none" name="recordnumber_list" id="recordnumber_list">
+                                    [%- recordnumbers = PROCESS batch_list results=results batch_type='biblionumber' | trim | html %][% IF recordnumbers %][% SET batch_biblionumbers = 1 %][% recordnumbers | html %][% END -%]
+                                </textarea>
                             </form>
 
                             <form method="POST" action="/cgi-bin/koha/tools/batchMod.pl" id="batch_item_modification">
                             <form method="POST" action="/cgi-bin/koha/tools/modborrowers.pl" id="batch_patron_modification">
                                 <input type="hidden" name="op" value="show" />
                                 [% # Preserve the whitespace of the following textarea in order to format the values correctly %]
-                                <textarea style="display:none" name="cardnumberlist" id="cardnumberlist">[% FOREACH result IN results %][% FOREACH cells IN result.cells %][% place = loop.index %][% IF header_row.$place.cell == 'cardnumber' || header_types.item(header_row.$place.cell) == 'cardnumber' %][% SET batch_cardnumbers = 1 %][% cells.cell | html %][% END %][% END %][% END %]</textarea>
+                                <textarea style="display:none" name="cardnumberlist" id="cardnumberlist">
+                                    [%- cardnumbers = PROCESS batch_list results=results batch_type='cardnumber' | trim | html %][% IF cardnumbers %][% SET batch_cardnumbers = 1 %][% cardnumbers | html %][% END -%]
+                                </textarea>
                             </form>
 
+                            [% BLOCK batch_list %]
+                                    [%- FOREACH result IN results %]
+                                        [%- FOREACH cells IN result.cells %]
+                                            [%- place = loop.index %]
+                                            [%- IF header_row.$place.cell == batch_type || header_types.item(header_row.$place.cell) == batch_type %]
+[%# We must not add whitespace to the cardnumbers %][% cells.cell | html %]
+                                            [%- END %]
+                                        [%- END %]
+                                    [%- END -%]
+                            [% END %]
                             [% IF ( batch_biblionumbers || batch_itemnumbers || batch_cardnumbers ) %]
                                 <p>
-                                    <div class="dropdown">
+                                    <div class="btn-group">
                                         <button class="btn btn-default dropdown-toggle" type="button" id="batch_mod_menu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                                             Batch operations with [% IF unlimited_total > limit %][% limit | html %][% ELSE %][% unlimited_total | html %][% END %] visible records
                                             <span class="caret"></span>
                             [% END %]
                         </table>
                     [% END %]
+                    <div class="pages">[% pagination_bar | $raw %]</div>
 
                     [% INCLUDE 'chart.inc' %]
 
                 [% IF ( errors ) %]
                     <form action="/cgi-bin/koha/reports/guided_reports.pl" method="post">
                         <div class="dialog alert">
-                            <b>The following error was encountered:</b><br />
+                            <strong>The following error was encountered:</strong><br />
                             [% FOREACH error IN errors %]
                                 [% IF ( error.sqlerr ) %]
-                                    This report contains the SQL keyword <b>[% error.sqlerr | html %]</b>.<br />
+                                    This report contains the SQL keyword <strong>[% error.sqlerr | html %]</strong>.<br />
                                     Use of this keyword is not allowed in Koha reports due to security and data integrity risks. Only SELECT queries are allowed.<br />
                                     Please return to the &quot;Saved Reports&quot; screen and delete this report or retry creating a new one.
                                 [% ELSIF ( error.queryerr ) %]
                 [% INCLUDE 'guided-reports-view.inc' %]
             </aside>
         </div> <!-- /.col-sm-2.col-sm-pull-10 -->
-     </div> <!-- /.row -->
+    </div> <!-- /.row -->
 
 [% MACRO jsinclude BLOCK %]
     [% Asset.js("js/charts.js") | $raw %]
         [% INCLUDE 'columns_settings.inc' %]
     [% END %]
     [% Asset.js( "lib/codemirror/codemirror.min.js" ) | $raw %]
+    [% Asset.js( "lib/codemirror/overlay.min.js" ) | $raw %]
     [% Asset.js( "lib/codemirror/sql.min.js" ) | $raw %]
     [% Asset.js( "js/mana.js" ) | $raw %]
     <script>
             }
         }
 
+        /* overlay a syntax-highlighting definition on top of the existing sql one */
+        CodeMirror.defineMode("sqlParams", function(config, parserConfig) {
+            var sqlParamsOverlay = {
+                token: function(stream, state) {
+                var ch;
+                if (stream.match("<<")) {
+                    while ((ch = stream.next()) != null)
+                    if (ch == ">" && stream.next() == ">") {
+                        stream.eat(">");
+                        return "sqlParams";
+                    }
+                }
+                while (stream.next() != null && !stream.match("<<", false)) {}
+                return null;
+                }
+            };
+            return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || "text/x-sql"), sqlParamsOverlay);
+        });
+
         var MSG_CONFIRM_DELETE = _("Are you sure you want to delete this report? This cannot be undone.");
         var group_subgroups = {};
         [% FOREACH group IN groups_with_subgroups %]
         [% END %]
 
         [% IF ( create || editsql || save ) %]
+
             var editor = CodeMirror.fromTextArea(sql, {
                 lineNumbers: true,
-                mode: "text/x-sql",
+                mode: "sqlParams", /* text/x-sql plus custom sqlParams configuration */
                 lineWrapping: true,
                 smartIndent: false
             });
         [% IF ( showsql ) %]
             var editor = CodeMirror.fromTextArea(sql, {
                 lineNumbers: false,
-                mode: "text/x-sql",
+                mode: "sqlParams", /* text/x-sql plus custom sqlParams configuration */
                 lineWrapping: true,
                 readOnly: true
             });
         }
 
         $(document).ready(function(){
+
+            var activeTab = localStorage.getItem("sql_reports_activetab");
+
             $("body").on('click',".fetch_chart_data",function(){
                 if( [% unlimited_total || 0 | $raw %] > 1000 ){
                     if( confirm("Fetching full chart data for reports with many rows can cause performance issues. Are you sure you with to chart this report?") ){
             [% END %]
 
             $('[data-toggle="tooltip"]').tooltip();
-            var columns_settings = [% ColumnsSettings.GetColumns( 'reports', 'saved-sql', 'table_reports', 'json' ) | $raw %];
+            var columns_settings = [% TablesSettings.GetColumns( 'reports', 'saved-sql', 'table_reports', 'json' ) | $raw %];
 
             $('#limit').change(function() {
                 $('#limitselect').submit();
                     'sPaginationType': 'full',
                     'aaSorting': [[ 1, "asc" ]],
                     'aoColumnDefs': [
-                        { 'bSortable': false, 'bSearchable':false, 'aTargets': [0, -1] },
-                        { 'bSearchable': false, 'aTargets': [3] },
-                        { "aTargets": [ 1, 2 ], "sType": "natural"  },
-                        { "sType": "title-string", "aTargets" : [ "title-string" ] },
                         { "visible": false, "aTargets" : [ "hidden" ] }
                     ],
                     'oLanguage': {
                     },
                 }, columns_settings);
 
-                var rtabs = $("#tabs").tabs();
-                rtabs.on("tabsactivate", function(e, ui) {
-                    $("#subgroup_filter option").each(function() {
-                        if($(this).val().length > 0) {
-                            $(this).remove();
-                        }
-                    });
-                    rtable.fnFilter('', 4);
-                    rtable.fnFilter('', 5);
-                    rtable.fnSetColumnVis(4, true);
-                    rtable.fnSetColumnVis(5, true);
-
-                    var g_id = $(ui.newTab).children().attr('id');
-                    var g_name = $(ui.newTab).text();
-                    if ( g_name == _("All") ) {
-                        g_id = "";
-                        g_name = "";
-                    }
-
-                    if (g_id && g_id.length > 0) {
-                        rtable.fnFilter('^' + g_name + '$', 4, true, true, true, false);
-                        rtable.fnSetColumnVis(4, false);
-                        for(var i in group_subgroups[g_id]) {
-                            $("#subgroup_filter").append(
-                                '<option value="' + group_subgroups[g_id][i][0] + '">'
-                                + group_subgroups[g_id][i][1] + '</option>'
-                            );
-                        }
-                        $("#subgroup_filter_block").show();
-                    } else {
-                        $("#subgroup_filter_block").hide();
+                var rtabs = $("#tabs").tabs({
+                    create: function( e, ui ){
+                        tabsInit( ui, rtable );
+                    },
+                    activate: function( e, ui ){
+                        tabsInit( ui, rtable );
                     }
                 });
+                if( activeTab ){
+                    $("#tabs").tabs("option","active", activeTab );
+                }
                 $("#subgroup_filter_block").hide();
 
                 $("#subgroup_filter").change(function() {
                 e.preventDefault();
                 addToList();
             });
+
+            $("body").on("click", ".preview_sql", function(e){
+                e.preventDefault();
+                var reportid = $(this).data("reportid");
+                previewSql( reportid );
+            });
         });
 
+        function tabsInit( ui, rtable ){
+            var thisTab = ui.newTab ? ui.newTab : ui.tab;
+            var activeTab = thisTab.index();
+            localStorage.setItem("sql_reports_activetab", activeTab );
+
+            $("#subgroup_filter option").each(function() {
+                if($(this).val().length > 0) {
+                    $(this).remove();
+                }
+            });
+            rtable.fnFilter('', 4);
+            rtable.fnFilter('', 5);
+            rtable.fnSetColumnVis(4, true);
+            rtable.fnSetColumnVis(5, true);
+
+            var g_id = $(ui.newTab).children().attr('id');
+            var g_name = $(ui.newTab).text();
+            if ( g_name == _("All") ) {
+                g_id = "";
+                g_name = "";
+            }
+
+            if (g_id && g_id.length > 0) {
+                rtable.fnFilter('^' + g_name + '$', 4, true, true, true, false);
+                rtable.fnSetColumnVis(4, false);
+                for(var i in group_subgroups[g_id]) {
+                    $("#subgroup_filter").append(
+                        '<option value="' + group_subgroups[g_id][i][0] + '">'
+                        + group_subgroups[g_id][i][1] + '</option>'
+                    );
+                }
+                $("#subgroup_filter_block").show();
+            } else {
+                $("#subgroup_filter_block").hide();
+            }
+        }
+
         function addColumn() {
             $("#availableColumns option:selected").clone().appendTo("#selectedColumns").attr("selected", "selected");
         }
                             ],
                             "aoColumnDefs": [
                                 { 'bSortable': false, "bSearchable": false, 'aTargets': [ 'NoSort' ] },
-                                { "sType": "title-string", "aTargets" : [ "title-string" ] },
                                 { 'sType': "anti-the", 'aTargets' : [ 'anti-the'] }
                             ]
                         }));
             window.open(url, 'Add_to_virtualshelf', 'width=500, height=400, toolbar=false, scrollbars=yes');
             return false;
         }
+
+        // Adapted from https://gist.github.com/jnormore/7418776
+        function previewSql(reportid) {
+            var yes_label = "";
+            var no_label = "";
+            var message = $("#previewSql" + reportid ).val();
+            var title = $("#previewSql" + reportid ).data("title");
+            if( $("#preview-sql-modal").length > 0) {
+                $("#preview-sql-modal").remove();
+            }
+            $("body").append('<div id="preview-sql-modal" tabindex="-1" role="dialog" aria-hidden="true" class="modal">\
+                <div class="modal-dialog">\
+                    <div class="modal-content">\
+                        <div class="modal-header" style="min-height:40px;">\
+                            <button type="button" class="closebtn" data-dismiss="modal" aria-label="Close">\
+                                <span aria-hidden="true">×</span>\
+                            </button>\
+                            <h4 class="modal-title">' + title + '</h4>\
+                        </div>\
+                        <div class="modal-body"><textarea id="code' + reportid + '">' + message + '</textarea>\
+                        <div class="modal-footer">\
+                            <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>\
+                            <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>\
+                            <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>\
+                            <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>\
+                            <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>\
+                            <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>\
+                        </div>\
+                    </div>\
+                </div>\
+            </div>');
+
+            $("#preview-sql-modal").modal('show');
+            CodeMirror.fromTextArea( document.getElementById("code" + reportid ), {
+                lineNumbers: false,
+                mode: "text/x-sql",
+                lineWrapping: true,
+                readOnly: true
+            });
+        }
     </script>
 [% END %]