Bug 15451: Rewrite the csv profile tool script
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 31 Dec 2015 10:23:24 +0000 (10:23 +0000)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 22 Jul 2016 17:18:34 +0000 (17:18 +0000)
This page was inconsistent with the other admin and tool pages.
The 2 tabs 'New profile' and 'Edit existing profile' were useless, the
ergonomic needs to be revisited.
This patch applies the same script/page structure as others: by default
a table containing all csv profiles is displayed with 2 action links:
edit and delete.

Test plan:
1/ Create 1+ CSV profiles, with different types (marc and sql)
2/ Update some values using the Edit link
3/ Delete a CSV profile

Note: When deleting a CSV profile, it would be great to warn the user if it is
used.

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
No problems on create, update and delete.
No errors

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
koha-tmpl/intranet-tmpl/prog/en/modules/tools/csv-profiles.tt
tools/csv-profiles.pl

index 35b7b46..5fe6a91 100644 (file)
 <script type="text/javascript">
 //<![CDATA[
 function reloadPage(p) {
-       var id = p.value;
-       if (id != 0) { document.location = "/cgi-bin/koha/tools/csv-profiles.pl?id=" + id; }
+    var id = p.value;
+    if (id != 0) { document.location = "/cgi-bin/koha/tools/csv-profiles.pl?op=add_form&amp;export_format_id=" + id; }
 }
      $(document).ready(function() {
-        $('#csvexporttabs').tabs();
-
-        $("#profile_type").find("option:first").attr("selected", "selected");
-        $("#csvnew").find("li.marc_specific").show();
-        $("#csvnew").find("li.sql_specific").hide();
-
-        $("#profile_type").change(function(){
-            if ( $(this).find("option:selected").val() == "marc" ) {
-                $("#csvnew li.marc_specific").show();
-                $("#new_profile_marc_content").attr("required", "required");
-                $("#csvnew li.sql_specific").hide();
-                $("#new_profile_sql_content").removeAttr("required");
-            } else {
-                $("#csvnew li.marc_specific").hide();
-                $("#new_profile_marc_content").removeAttr("required");
-                $("#csvnew li.sql_specific").show();
-                $("#new_profile_sql_content").attr("required", "required");
-            }
-        });
-        $("#modify_profile_type").change(function(){
+        $("#type").change(function(){
             if ( $(this).find("option:selected").val() == "marc" ) {
-                $("#csvedit li.marc_specific").show();
-                $("#modify_profile_marc_content").attr("required", "required");
-                $("#csvedit li.sql_specific").hide();
-                $("#modify_profile_sql_content").removeAttr("required");
+                $("li.marc_specific").show();
+                $("#marc_content").attr("required", "required");
+                $("li.sql_specific").hide();
+                $("#sql_content").removeAttr("required");
             } else {
-                $("#csvedit li.marc_specific").hide();
-                $("#modify_profile_marc_content").removeAttr("required");
-                $("#csvedit li.sql_specific").show();
-                $("#modify_profile_sql_content").attr("required", "required");
+                $("li.marc_specific").hide();
+                $("#marc_content").removeAttr("required");
+                $("li.sql_specific").show();
+                $("#sql_content").attr("required", "required");
             }
         });
-        $("#profile_type").change();
-        $("#modify_profile_type").change();
+        $("#type").change();
      });
 //]]>
 </script>
 </head>
 
 <body id="tools_csv-profiles" class="tools">
-       [% INCLUDE 'header.inc' %]
-       [% INCLUDE 'cat-search.inc' %]
+[% INCLUDE 'header.inc' %]
+[% INCLUDE 'cat-search.inc' %]
 
-       <div id="breadcrumbs">
-               <a href="/cgi-bin/koha/mainpage.pl">Home</a>
-               &rsaquo; <a href="/cgi-bin/koha/tools/tools-home.pl">Tools</a>
-               &rsaquo; CSV export profiles
-       </div>
+<div id="breadcrumbs">
+    <a href="/cgi-bin/koha/mainpage.pl">Home</a>
+    &rsaquo; <a href="/cgi-bin/koha/tools/tools-home.pl">Tools</a>
+    &rsaquo; CSV export profiles
+</div>
 
-       <div id="doc3" class="yui-t2">
-               <div id="bd">
-                       <div id="yui-main">
-                               <div class="yui-b">
-                                   [% IF ( success || error ) %]
-                                           [% IF ( success ) %]
-        [% IF ( action == 'create' ) %]<div class="dialog message">The new CSV profile "[% profile_name %]" has been successfully created.</div>[% END %]
-        [% IF ( action == 'edit' ) %]<div class="dialog message">The CSV profile has been successfully modified.</div>[% END %]
-        [% IF ( action == 'delete' ) %]<div class="dialog message">The CSV profile has been successfully deleted.</div>[% END %]
+<div id="doc3" class="yui-t2">
+   <div id="bd">
+    <div id="yui-main">
+    <div class="yui-b">
+
+[% FOR m IN messages %]
+    <div class="dialog [% m.type %]">
+        [% SWITCH m.code %]
+        [% CASE 'error_on_update' %]
+            An error occurred when updating this CSV profile. Perhaps it already exists.
+        [% CASE 'error_on_insert' %]
+            An error occurred when adding this CSV profile.
+        [% CASE 'error_on_delete' %]
+            An error occurred when deleting this CSV profile. Check the logs.
+        [% CASE 'success_on_update' %]
+            CSV profile updated successfully.
+        [% CASE 'success_on_insert' %]
+            CSV profile added successfully.
+        [% CASE 'success_on_delete' %]
+            CSV profile deleted successfully.
+        [% CASE 'already_exists' %]
+            This CSV profile already exists.
+        [% CASE %]
+            [% m.code %]
+        [% END %]
+    </div>
+[% END %]
+
+[% BLOCK list_separator_options %]
+    [% IF selected_separator == ',' %]
+        <option value="," selected="selected">Comma (,)</option>
+    [% ELSE %]
+        <option value=",">Comma (,)</option>
+    [% END %]
+    [% IF selected_separator == '|' %]
+        <option value="|" selected="selected">Pipe (|)</option>
+    [% ELSE %]
+        <option value="|">Pipe (|)</option>
+    [% END %]
+    [% IF selected_separator == ';' %]
+        <option value=";" selected="selected">Semi-colon (;)</option>
+    [% ELSE %]
+        <option value=";">Semi-colon (;)</option>
+    [% END %]
+    [% IF selected_separator == '#' %]
+        <option value="#" selected="selected">Sharp (#)</option>
+    [% ELSE %]
+        <option value="#">Sharp (#)</option>
+    [% END %]
+    [% IF selected_separator == ' ' %]
+        <option value=" " selected="selected">Space ( )</option>
+    [% ELSE %]
+        <option value=" ">Space ( )</option>
+    [% END %]
+    [% IF selected_separator == '\t' %]
+        <option value="\t" selected="selected">Tabulation (\t)</option>
+    [% ELSE %]
+        <option value="\t">Tabulation (\t)</option>
+    [% END %]
+    [% IF selected_separator == '\n' %]
+        <option value="\n" selected="selected">New line (\n)</option>
+    [% ELSE %]
+        <option value="\n">New line (\n)</option>
+    [% END %]
+[% END %]
+
+[% IF op == 'add_form' %]
+    [% IF csv_profile %]
+        <h1>Modify a CSV profile</h1>
+    [% ELSE %]
+        <h1>New CSV profile</h1>
+    [% END %]
+
+    <form action="/cgi-bin/koha/tools/csv-profiles.pl" class="validated" method="post">
+        <input type="hidden" name="op" value="add_validate" />
+        <input type="hidden" name="export_format_id" value="[% csv_profile.export_format_id %]" />
+        <fieldset class="rows">
+            <ol>
+                [% IF csv_profile %]
+                    <li><span class="label">Profile ID: </span>[% csv_profile.export_format_id %]</li>
+                [% END %]
+                <li>
+                    <label for="profile" class="required">Profile name: </label>
+                    <input type="text" name="profile" id="profile" value="[% csv_profile.profile %]" class="required">
+                    <span class="required">Required</span>
+                </li>
+                <li>
+                    <label for="type" class="required">Profile type: </label>
+                    <select id="type" name="type">
+                        [% IF csv_profile.type == 'sql' %]
+                            <option value="marc">MARC</option>
+                            <option value="sql" selected="selected">SQL</option>
                         [% ELSE %]
-        [% IF ( action == 'create' ) %]<div class="dialog alert">The new CSV profile "[% profile_name %]" has not been created.</div>[% END %]
-        [% IF ( action == 'edit' ) %]<div class="dialog alert">The CSV profile has not been modified.</div>[% END %]
-        [% IF ( action == 'delete' ) %]<div class="dialog alert">The CSV profile has not been deleted.</div>[% END %]
-                                           [% END %]
-                                   [% END %]
-<div id="csvexporttabs" class="toptabs">
-<ul>
-    <li><a href="#csvnew">New profile</a></li>
-       [% IF ( existing_profiles ) %]
-       [% IF ( selected_profile_id ) %]
-    <li class="ui-tabs-active">
-       [% ELSE %]
-       <li>
-        [% END %]
-    <a href="#csvedit">Edit existing profile</a></li> [% END %]
-</ul>
-       <div id="csvnew">
-                                       <h3>New CSV export profile</h3>
-
-                        <form action="/cgi-bin/koha/tools/csv-profiles.pl" class="validated" method="post">
-                                               <fieldset class="rows">
-                          <ol>
-                            <li>
-                                <label for="profile_name" class="required">Profile name: </label>
-                                <input type="text" id="profile_name" name="profile_name" required="required" />
-                                <span class="required">Required</span>
-                            </li>
-
-                            <li>
-                              <label for="profile_type" class="required">Profile type: </label>
-                              <select id="profile_type" name="profile_type">
-                                <option value="marc" selected="selected">MARC</option>
-                                <option value="sql">SQL</option>
-                              </select>
-                              <span class="required">Required</span>
-                            </li>
-
-                                                   <li><label for="profile_description">Profile description: </label>
-                                                   <textarea cols="50" rows="2" name="profile_description" id="profile_description"></textarea></li>
-
-                                                   <li><label for="new_csv_separator">CSV separator: </label>
-                                                   <select name="csv_separator" id="new_csv_separator">
-                                                       <option value=":">Colon (:)</option>
-                                                       <option value="," selected="selected">Comma (,)</option>
-                                                       <option value="|">Pipe (|)</option>
-                                                       <option value=";">Semi-colon (;)</option>
-                                                       <option value="#">Sharp (#)</option>
-                                                       <option value=" ">Space ( )</option>
-                                                       <option value="\t">Tabulation (\t)</option>
-                                                       <option value="\n">New line (\n)</option>
-                                                   </select>
-                            </li>
+                            <option value="marc" selected="selected">MARC</option>
+                            <option value="sql">SQL</option>
+                        [% END %]
+                    </select>
+                    <span class="required">Required</span>
+                </li>
+                <li>
+                    <label for="description">Profile description: </label>
+                    <textarea cols="50" rows="2" name="description" id="description">[% csv_profile.description %]</textarea>
+                </li>
+                <li>
+                    <label for="csv_separator">CSV separator: </label>
+                    <select name="csv_separator" id="csv_separator">
+                        [% PROCESS list_separator_options selected_separator=csv_profile.csv_separator || ',' %]
+                    </select>
+                </li>
+
+                <li class="marc_specific">
+                    <label for="field_separator">Field separator: </label>
+                    <select name="field_separator" id="field_separator">
+                        [% PROCESS list_separator_options selected_separator=csv_profile.field_separator || '#' %]
+                    </select>
+                </li>
+
+                <li class="marc_specific"><label for="subfield_separator">Subfield separator: </label>
+                    <select name="subfield_separator" id="subfield_separator">
+                        [% PROCESS list_separator_options selected_separator=csv_profile.subfield_separator || '|'%]
+                    </select>
+                </li>
+
+                <li class="marc_specific"><label for="encoding">Encoding: </label>
+                    <select name="encoding" id="encoding">
+                        [% FOREACH encoding IN encodings %]
+                            [% IF csv_profile.encoding == encoding
+                                OR NOT csv_profile AND encoding == 'utf8' %]
+                                <option selected="selected">[% encoding %]</option>
+                            [% ELSE %]
+                                <option>[% encoding %]</option>
+                            [% END %]
+                        [% END %]
+                    </select>
+                </li>
+
+                <li class="marc_specific">
+                    <label for="marc_content" class="required">Profile MARC fields: </label>
+                    <textarea cols="50" rows="2" name="marc_content" id="marc_content">[% csv_profile.content %]</textarea>
+                    <span class="required">Required</span>
+                    <p>You have to define which fields or subfields you want to export, separated by pipes.</p>
+                    <p>You can also use your own headers (instead of the ones from Koha) by prefixing the field number with an header, followed by the equal sign.</p>
+                    <p>Example: Personal name=200|Entry element=210$a|300|009</p>
+                    <p>You can use Template Toolkit tags too. See the help page for more information.</p>
+                </li>
+
+                <li class="sql_specific">
+                  <label for="sql_content" class="required">Profile SQL fields: </label>
+                  <textarea cols="50" rows="2" name="sql_content" id="sql_content">[% csv_profile.content %]</textarea>
+                  <p>You have to define which fields you want to export, separated by pipes.</p>
+                  <p>You can also use your own headers (instead of the ones from Koha) by prefixing the field name with an header, followed by the equal sign.</p>
+                  <p>Example: Name=subscription.name|Title=subscription.title|Issue number=serial.serialseq</p>
+                </li>
+            </ol>
+        </fieldset>
+        <fieldset class="action">
+            <input type="submit" value="Submit" />
+            <a class="cancel" href="/cgi-bin/koha/tools/csv-profiles.pl">Cancel</a>
+        </fieldset>
+    </form>
+[% END %]
+
+[% IF op == 'delete_confirm' %]
+    <div class="dialog alert">
+        <h3>Delete CSV Profile "[% csv_profile.profile %]?"</h3>
+        <form action="/cgi-bin/koha/tools/csv-profiles.pl" method="post">
+            <input type="hidden" name="op" value="delete_confirmed" />
+            <input type="hidden" name="export_format_id" value="[% csv_profile.export_format_id %]" />
+            <input type="submit" class="approve" value="Yes, delete" />
+        </form>
+        <form action="/cgi-bin/koha/tools/csv-profiles.pl" method="get">
+            <input type="submit" class="deny" value="No, do not Delete" />
+        </form>
+    </div>
+[% END %]
 
-                            <li class="marc_specific"><label for="new_field_separator">Field separator: </label>
-                                                   <select name="field_separator" id="new_field_separator">
-                                                       <option value=":">Colon (:)</option>
-                                                       <option value=",">Comma (,)</option>
-                                                       <option value="|">Pipe (|)</option>
-                                                       <option value=";">Semi-colon (;)</option>
-                                                       <option value="#" selected="selected">Sharp (#)</option>
-                                                       <option value=" ">Space ( )</option>
-                                                       <option value="\t">Tabulation (\t)</option>
-                                                       <option value="\n">New line (\n)</option>
-                                                   </select>
-                            </li>
+[% IF op == 'list' %]
 
-                            <li class="marc_specific"><label for="new_subfield_separator">Subfield separator: </label>
-                                                   <select name="subfield_separator" id="new_subfield_separator">
-                                                       <option value=":">Colon (:)</option>
-                                                       <option value=",">Comma (,)</option>
-                                                       <option value="|" selected="selected">Pipe (|)</option>
-                                                       <option value=";">Semi-colon (;)</option>
-                                                       <option value="#">Sharp (#)</option>
-                                                       <option value=" ">Space ( )</option>
-                                                       <option value="\t">Tabulation (\t)</option>
-                                                       <option value="\n">New line (\n)</option>
-                                                   </select>
-                            </li>
-                                       
-                            <li class="marc_specific"><label for="new_encoding">Encoding: </label>
-                                                   <select name="encoding" id="new_encoding">
-                                                       [% FOREACH encoding IN encodings %]
-                                [% IF ( encoding.encoding == 'utf8' ) %]
-                                                                       <option selected="selected">[% encoding.encoding %]</option>
-                                                               [% ELSE %]
-                                                                       <option>[% encoding.encoding %]</option>
-                                                               [% END %]
-                                                       [% END %]
-                                                   </select></li>
+    <div id="toolbar" class="btn-toolbar">
+        <a class="btn btn-small" id="newcsvprofile" href="/cgi-bin/koha/tools/csv-profiles.pl?op=add_form"><i class="fa fa-plus"></i> New CSV profile</a>
+    </div>
 
+    <h2>CSV profiles</h2>
+
+    [% IF csv_profiles%]
+        <table id="table_csv_profiles">
+            <thead>
+                <th>CSV profile ID</th>
+                <th>Name</th>
+                <th>Description</th>
+                <th>Content</th>
+                <th>CSV separator</th>
+                <th>CSV type</th>
+                <th>&nbsp;</th>
+                <th>&nbsp;</th>
+            </thead>
+            <tbody>
+                [% FOREACH csv_profile IN csv_profiles %]
+                <tr>
+                    <td>[% csv_profile.export_format_id %]</td>
+                    <td>[% csv_profile.profile %]</td>
+                    <td>[% csv_profile.description %]</td>
+                    <td>[% csv_profile.content %]</td>
+                    <td>[% csv_profile.csv_separator %]</td>
+                    [% IF csv_profile.type == 'sql' %]
+                        <td>SQL</td>
+                    [% ELSE %]
+                        <td>MARC</td>
+                    [% END %]
+                    <td><a href="/cgi-bin/koha/tools/csv-profiles.pl?op=add_form&amp;export_format_id=[% csv_profile.export_format_id %]">Edit</a></td>
+                    <td><a href="/cgi-bin/koha/tools/csv-profiles.pl?op=delete_confirm&amp;export_format_id=[% csv_profile.export_format_id %]">Delete</a></td>
+                </tr>
+                [% END %]
+            </tbody>
+        </table>
+    [% ELSE %]
+        There is no CSV profile defined. <a href="/cgi-bin/koha/tools/csv-profiles.pl?op=add_form">Create a new CSV profile</a>.
+    [% END %]
+[% END %]
 
-                            <li class="marc_specific">
-                                <label for="new_profile_marc_content" class="required">Profile MARC fields: </label>
-                                <textarea cols="50" rows="2" name="profile_marc_content" id="new_profile_marc_content"></textarea>
-                                <span class="required">Required</span>
-                                                   <p>You have to define which fields or subfields you want to export, separated by pipes.</p>
-                            <p>You can also use your own headers (instead of the ones from Koha) by prefixing the field number with an header, followed by the equal sign.</p>
-                                                   <p>Example: Personal name=200|Entry element=210$a|300|009</p>
-                            <p>You can use Template Toolkit tags too. See the help page for more information.</p>
-                                                   </li>
-                            <li class="sql_specific">
-                              <label for="new_profile_sql_content" class="required">Profile SQL fields: </label>
-                              <textarea cols="50" rows="2" name="profile_sql_content" id="new_profile_sql_content"></textarea>
-                              <p>You have to define which fields you want to export, separated by pipes.</p>
-                              <p>You can also use your own headers (instead of the ones from Koha) by prefixing the field name with an header, followed by the equal sign.</p>
-                              <p>Example: Name=subscription.name|Title=subscription.title|Issue number=serial.serialseq</p>
-                            </li>
-                                                   </ol>
-                                               </fieldset>
-                                               <fieldset class="action"><input type="hidden" name="action" value="create" />
-                                               <input type="submit" value="Submit" /></fieldset>
-                                           </form>
-                                           <br style="clear:both;" />
 </div>
-                                       [% IF ( existing_profiles ) %]
-<div id="csvedit">
-                                       <h3>Modify or delete a CSV export profile</h3>
-
-                        <form action="/cgi-bin/koha/tools/csv-profiles.pl" class="validated" method="post">
-                                               <fieldset class="rows">
-                            <ol>
-                            <li><label for="modify_profile_name" class="required">Profile name: </label>
-                                <select id="modify_profile_name" name="profile_name" required="required" class="required" onchange="reloadPage(this)">
-                                                       <option value="0">-- Choose One --</option>
-                                                       [% FOREACH existing_profile IN existing_profiles %]
-                                                       [% IF ( existing_profile.export_format_id == selected_profile_id ) %]
-                                                       <option value="[% existing_profile.export_format_id %]" selected="selected">[% existing_profile.profile %]</option>
-                                                       [% ELSE %]
-                                                       <option value="[% existing_profile.export_format_id %]">[% existing_profile.profile %]</option>
-                                                       [% END %]
-                                                       [% END %]
-                            </select>
-                            <span class="required">Required</span>
-                            </li>
-
-                            <li>
-                              <label for="modify_profile_type" class="required">Profile type: </label>
-                              <select id="modify_profile_type" name="profile_type" class="required" required="required">
-                                <option value="marc">MARC</option>
-                                [% IF selected_profile_type == "sql" %]
-                                  <option value="sql" selected="selected">SQL</option>
-                                [% ELSE %]
-                                  <option value="sql">SQL</option>
-                                [% END %]
-                              </select>
-                                <span class="required">Required</span>
-                            </li>
-
-
-                                                   <li><label for="modify_profile_description">Profile description: </label>
-                                                   <textarea cols="50" rows="2" name="profile_description" id="modify_profile_description">[% selected_profile_description %]</textarea></li>
-
-                                                   <li><label for="csv_separator">CSV separator: </label>
-                                                   <select name="csv_separator" id="csv_separator">
-                                                               <option value=":">Colon (:)</option>
-                                                               [% IF ( selected_csv_separator == ',' ) %]
-                                                               <option value="," selected="selected">Comma (,)</option>
-                                                               [% ELSE %]
-                                                               <option value=",">Comma (,)</option>
-                                                               [% END %]
-                                                               [% IF ( selected_csv_separator == '|' ) %]
-                                                               <option value="|" selected="selected">Pipe (|)</option>
-                                                               [% ELSE %]
-                                                               <option value="|">Pipe (|)</option>
-                                                               [% END %]
-                                                               [% IF ( selected_csv_separator == ';' ) %]
-                                                               <option value=";" selected="selected">Semi-colon (;)</option>
-                                                               [% ELSE %]
-                                                               <option value=";">Semi-colon (;)</option>
-                                                               [% END %]
-                                                               [% IF ( selected_csv_separator == '#' ) %]
-                                                               <option value="#" selected="selected">Sharp (#)</option>
-                                                               [% ELSE %]
-                                                               <option value="#">Sharp (#)</option>
-                                                               [% END %]
-                                                               [% IF ( selected_csv_separator == ' ' ) %]
-                                                               <option value=" " selected="selected">Space ( )</option>
-                                                               [% ELSE %]
-                                                               <option value=" ">Space ( )</option>
-                                                               [% END %]
-                                                               [% IF ( selected_csv_separator == '\t' ) %]
-                                                               <option value="\t" selected="selected">Tabulation (\t)</option>
-                                                               [% ELSE %]
-                                                               <option value="\t">Tabulation (\t)</option>
-                                                               [% END %]
-                                                       [% IF ( selected_csv_separator == '\n' ) %]
-                                                               <option value="\n" selected="selected">New line (\n)</option>
-                                                               [% ELSE %]
-                                                               <option value="\n">New line (\n)</option>
-                                                               [% END %]
-                            </select></li>
-
-                            <li class="marc_specific"><label for="field_separator">Field separator: </label>
-                                                   <select name="field_separator" id="field_separator">
-                                <option value=":">Colon (:)</option>
-
-                                [% IF ( selected_field_separator == ',' ) %]
-                                <option value="," selected="selected">Comma (,)</option>
-                                [% ELSE %]
-                                <option value=",">Comma (,)</option>
-                                                               [% END %]
-
-                                [% IF ( selected_field_separator == '|' ) %]
-                                <option value="|" selected="selected">Pipe (|)</option>
-                                [% ELSE %]
-                                <option value="|">Pipe (|)</option>
-                                [% END %]
-
-                                [% IF ( selected_field_separator == ';' ) %]
-                                <option value=";" selected="selected">Semi-colon (;)</option>
-                                [% ELSE %]
-                                <option value=";">Semi-colon (;)</option>
-                                [% END %]
-
-                                [% IF ( selected_field_separator == '#' ) %]
-                                <option value="#" selected="selected">Sharp (#)</option>
-                                [% ELSE %]
-                                <option value="#">Sharp (#)</option>
-                                [% END %]
-
-                                [% IF ( selected_field_separator == ' ' ) %]
-                                <option value=" " selected="selected">Space ( )</option>
-                                [% ELSE %]
-                                <option value=" ">Space ( )</option>
-                                [% END %]
-
-                                [% IF ( selected_field_separator == '\t' ) %]
-                                <option value="\t" selected="selected">Tabulation (\t)</option>
-                                [% ELSE %]
-                                <option value="\t">Tabulation (\t)</option>
-                                [% END %]
-                                                       [% IF ( selected_field_separator == '\n' ) %]
-                                                               <option value="\n" selected="selected">New line (\n)</option>
-                                                               [% ELSE %]
-                                                               <option value="\n">New line (\n)</option>
-                                                               [% END %]
-                                                   </select></li>
-
-
-                            <li class="marc_specific"><label for="subfield_separator">Subfield separator: </label>
-                                                   <select name="subfield_separator" id="subfield_separator">
-                                <option value=":">Colon (:)</option>
-
-                                [% IF ( selected_subfield_separator == ',' ) %]
-                                <option value="," selected="selected">Comma (,)</option>
-                                [% ELSE %]
-                                <option value=",">Comma (,)</option>
-                                [% END %]
-
-                                [% IF ( selected_subfield_separator == '|' ) %]
-                                <option value="|" selected="selected">Pipe (|)</option>
-                                [% ELSE %]
-                                <option value="|">Pipe (|)</option>
-                                [% END %]
-
-                                                               [% IF ( selected_subfield_separator == ';' ) %]
-                                <option value=";" selected="selected">Semi-colon (;)</option>
-                                [% ELSE %]
-                                <option value=";">Semi-colon (;)</option>
-                                [% END %]
-
-                                                               [% IF ( selected_subfield_separator == '#' ) %]
-                                <option value="#" selected="selected">Sharp (#)</option>
-                                [% ELSE %]
-                                <option value="#">Sharp (#)</option>
-                                [% END %]
-
-                                                               [% IF ( selected_subfield_separator == ' ' ) %]
-                                <option value=" " selected="selected">Space ( )</option>
-                                [% ELSE %]
-                                <option value=" ">Space ( )</option>
-                                [% END %]
-
-                                                               [% IF ( selected_subfield_separator == '\t' ) %]
-                                <option value="\t" selected="selected">Tabulation (\t)</option>
-                                [% ELSE %]
-                                <option value="\t">Tabulation (\t)</option>
-                                [% END %]
-                                                       [% IF ( selected_subfield_separator == '\n' ) %]
-                                                               <option value="\n" selected="selected">New line (\n)</option>
-                                                               [% ELSE %]
-                                                               <option value="\n">New line (\n)</option>
-                                [% END %]
-       
-                                                   </select></li>
-
-                            <li class="marc_specific"><label for="encoding">Encoding: </label>
-                                                   <select name="encoding" id="encoding">
-                                                       [% FOREACH encoding IN encodings %]
-                                [% IF ( selected_encoding == encoding.encoding ) %]
-                                                           <option selected="selected">[% encoding.encoding %]</option>
-                                                           [% ELSE %]
-                                                           <option>[% encoding.encoding %]</option>
-                                                           [% END %]
-                                                       [% END %]
-                                                   </select></li>
-
-                            <li class="marc_specific">
-                                <label for="modify_profile_marc_content" class="required">Profile MARC fields: </label>
-                                <textarea cols="50" rows="2" name="profile_marc_content" id="modify_profile_marc_content" class="required" required="required">[% selected_profile_content %]</textarea>
-                                <span class="required">Required</span>
-                            </li>
-
-                            <li class="sql_specific">
-                              <label for="modify_profile_sql_content" class="required">Profile SQL fields: </label>
-                              <textarea cols="50" rows="2" name="profile_sql_content" id="modify_profile_sql_content">[% selected_profile_content %]</textarea>
-                            </li>
-
-                                                  <li class="radio"> <label for="delete">Delete selected profile?</label>
-                                                   <input type="checkbox" name="delete" id="delete" /></li>
-                                                   </ol>
-
-                                               </fieldset>
-
-                                               <fieldset class="action"><input type="hidden" name="modify_profile_id" value="[% selected_profile_id %]" />
-                                               <input type="hidden" name="action" value="edit" />
-                                               <input type="submit" value="Submit" /> <a href="/cgi-bin/koha/tools/csv-profiles.pl" class="cancel">Cancel</a></fieldset>
-                                           </form>
 </div>
-                                       [% END %]
-
-                               </div>
-                       </div>
-                       </div>
-                   <div class="yui-b noprint">
-        [% INCLUDE 'tools-menu.inc' %]
-    </div>
-       </div>
-       [% INCLUDE 'intranet-bottom.inc' %]
+<div class="yui-b noprint">
+[% INCLUDE 'tools-menu.inc' %]
+</div>
+</div>
+[% INCLUDE 'intranet-bottom.inc' %]
index 7323a6e..7db0f67 100755 (executable)
@@ -1,6 +1,7 @@
 #!/usr/bin/perl
 
 # Copyright 2009 BibLibre
+# Copyright 2015 Koha Development Team
 #
 # This file is part of Koha.
 #
@@ -33,9 +34,7 @@ This script allow the user to define a new profile for CSV export
 
 =cut
 
-use strict;
-#use warnings; FIXME - Bug 2505
-use Data::Dumper;
+use Modern::Perl;
 use Encode;
 
 use C4::Auth;
@@ -43,15 +42,15 @@ use C4::Context;
 use C4::Output;
 use CGI qw ( -utf8 );
 use C4::Koha;
-use C4::Csv;
+use Koha::CsvProfiles;
 
-my $input        = new CGI;
-my $dbh          = C4::Context->dbh;
+my $input            = new CGI;
+my $export_format_id = $input->param('export_format_id');
+my $op               = $input->param('op') || 'list';
+my @messages;
 
-# open template
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
-    {
-        template_name   => "tools/csv-profiles.tt",
+    {   template_name   => "tools/csv-profiles.tt",
         query           => $input,
         type            => "intranet",
         authnotrequired => 0,
@@ -61,78 +60,87 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 );
 
 # Getting available encodings list
-my @encodings = Encode->encodings();
-my @encodings_loop = map{{encoding => $_}} @encodings;
-$template->param(encodings => \@encodings_loop);
-
-my $profile_name        = $input->param("profile_name");
-my $profile_description = $input->param("profile_description");
-my $csv_separator       = $input->param("csv_separator");
-my $field_separator     = $input->param("field_separator");
-my $subfield_separator  = $input->param("subfield_separator");
-my $encoding            = $input->param("encoding");
-my $type                = $input->param("profile_type");
-my $action              = $input->param("action");
-my $delete              = $input->param("delete");
-my $id                  = $input->param("id");
-if ($delete) { $action = "delete"; }
-
-my $profile_content = $type eq "marc"
-    ? $input->param("profile_marc_content")
-    : $input->param("profile_sql_content");
-
-if ($profile_name && $profile_content && $action) {
-    my $rows;
-
-    if ($action eq "create") {
-    my $query = "INSERT INTO export_format(export_format_id, profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?)";
-       my $sth   = $dbh->prepare($query);
-    $rows  = $sth->execute($profile_name, $profile_description, $profile_content, $csv_separator, $field_separator, $subfield_separator, $encoding, $type);
-    
-    }
+$template->param( encodings => [ Encode->encodings ] );
 
-    if ($action eq "edit") {
-    my $query = "UPDATE export_format SET description=?, content=?, csv_separator=?, field_separator=?, subfield_separator=?, encoding=?, type=? WHERE export_format_id=? LIMIT 1";
-       my $sth   = $dbh->prepare($query);
-    $rows  = $sth->execute($profile_description, $profile_content, $csv_separator, $field_separator, $subfield_separator, $encoding, $type, $profile_name);
+if ( $op eq 'add_form' ) {
+    my $csv_profile;
+    if ($export_format_id) {
+        $csv_profile = Koha::CsvProfiles->find($export_format_id);
     }
-
-    if ($action eq "delete") {
-       my $query = "DELETE FROM export_format WHERE export_format_id=? LIMIT 1";
-       my $sth   = $dbh->prepare($query);
-       $rows  = $sth->execute($profile_name);
-
+    $template->param( csv_profile => $csv_profile, );
+} elsif ( $op eq 'add_validate' ) {
+    my $profile     = $input->param("profile");
+    my $description = $input->param("description");
+    my $type        = $input->param("type");
+    my $content =
+        $type eq "marc"
+      ? $input->param("marc_content")
+      : $input->param("sql_content");
+    my $csv_separator      = $input->param("csv_separator");
+    my $field_separator    = $input->param("field_separator");
+    my $subfield_separator = $input->param("subfield_separator");
+    my $encoding           = $input->param("encoding");
+
+    if ($export_format_id) {
+        my $csv_profile = Koha::CsvProfiles->find($export_format_id);
+        $csv_profile->profile($profile);
+        $csv_profile->description($description);
+        $csv_profile->content($content);
+        $csv_profile->csv_separator($csv_separator);
+        $csv_profile->field_separator($field_separator);
+        $csv_profile->subfield_separator($subfield_separator);
+        $csv_profile->encoding($encoding);
+        $csv_profile->type($type);
+        eval { $csv_profile->store; };
+
+        if ($@) {
+            push @messages, { type => 'error', code => 'error_on_update' };
+        } else {
+            push @messages, { type => 'message', code => 'success_on_update' };
+        }
+    } else {
+        my $csv_profile = Koha::CsvProfile->new(
+            {   profile            => $profile,
+                description        => $description,
+                content            => $content,
+                csv_separator      => $csv_separator,
+                field_separator    => $field_separator,
+                subfield_separator => $subfield_separator,
+                encoding           => $encoding,
+                type               => $type,
+            }
+        );
+        eval { $csv_profile->store; };
+        if ($@) {
+            push @messages, { type => 'error', code => 'error_on_insert' };
+        } else {
+            push @messages, { type => 'message', code => 'success_on_insert' };
+        }
     }
-
-    $rows ? $template->param(success => 1) : $template->param(error => 1);
-    $template->param(profile_name => $profile_name);
-    $template->param(action => $action);
-
+    $op = 'list';
+} elsif ( $op eq 'delete_confirm' ) {
+    my $csv_profile = Koha::CsvProfiles->find($export_format_id);
+    $template->param( csv_profile => $csv_profile, );
+} elsif ( $op eq 'delete_confirmed' ) {
+    my $csv_profile = Koha::CsvProfiles->find($export_format_id);
+    my $deleted = eval { $csv_profile->delete; };
+
+    if ( $@ or not $deleted ) {
+        push @messages, { type => 'error', code => 'error_on_delete' };
+    } else {
+        push @messages, { type => 'message', code => 'success_on_delete' };
+    }
+    $op = 'list';
 }
 
-    # If a profile has been selected for modification
-    if ($id) {
-    my $query = "SELECT export_format_id, profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type FROM export_format WHERE export_format_id = ?";
-       my $sth;
-       $sth = $dbh->prepare($query);
-
-       $sth->execute($id);
-       my $selected_profile = $sth->fetchrow_arrayref();
-       $template->param(
-           selected_profile_id          => $selected_profile->[0],
-           selected_profile_name        => $selected_profile->[1],
-           selected_profile_description => $selected_profile->[2],
-        selected_profile_content     => $selected_profile->[3],
-           selected_csv_separator       => $selected_profile->[4],
-           selected_field_separator     => $selected_profile->[5],
-           selected_subfield_separator  => $selected_profile->[6],
-        selected_encoding            => $selected_profile->[7],
-        selected_profile_type        => $selected_profile->[8]
-       );
-
-    }
+if ( $op eq 'list' ) {
+    my $csv_profiles = Koha::CsvProfiles->search;
+    $template->param( csv_profiles => $csv_profiles, );
+}
 
-    # List of existing profiles
-    $template->param(existing_profiles => GetCsvProfilesLoop());
+$template->param(
+    messages => \@messages,
+    op       => $op,
+);
 
 output_html_with_http_headers $input, $cookie, $template->output;