Bug 25279: (QA follow-up) Original behaviour
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 5 May 2020 21:55:49 +0000 (18:55 -0300)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Tue, 12 May 2020 10:39:55 +0000 (11:39 +0100)
This patch does the following things:
- Uses a call to Koha::Cities->search(*)->count to display the same
  message we displayed before when no city was found/defined (datatable
  not rendered at all).
- Restores the main search box functionality, the passed param is used
  to query on the city name with wildcards on both sides, for (a)
  counting results and for (b) apending to the API call with the same
  behaviour.

The only missing bit from QA is HTML/URI escaping values from cells, but
this is going to happen at DT level most sure.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

admin/cities.pl
koha-tmpl/intranet-tmpl/prog/en/modules/admin/cities.tt

index f25ec18..03bce54 100755 (executable)
@@ -27,7 +27,7 @@ use C4::Output;
 use Koha::Cities;
 
 my $input       = new CGI;
-my $searchfield = $input->param('city_name') // q||;
+my $city_name   = $input->param('city_name') // q||;
 my $cityid      = $input->param('cityid');
 my $op          = $input->param('op') || 'list';
 my @messages;
@@ -84,8 +84,8 @@ if ( $op eq 'add_form' ) {
             push @messages, { type => 'message', code => 'success_on_insert' };
         }
     }
-    $searchfield = q||;
-    $op          = 'list';
+    $city_name = q||;
+    $op        = 'list';
 } elsif ( $op eq 'delete_confirm' ) {
     my $city = Koha::Cities->find($cityid);
     $template->param( city => $city, );
@@ -101,9 +101,16 @@ if ( $op eq 'add_form' ) {
     $op = 'list';
 }
 
+if ( $op eq 'list' ) {
+    my $filter = {};
+    $filter->{city_name} = { -like => '%'.$city_name.'%' }
+        if $city_name;
+    $template->param( cities_count => Koha::Cities->search($filter)->count );
+}
+
 $template->param(
     cityid      => $cityid,
-    searchfield => $searchfield,
+    city_name_filter => $city_name,
     messages    => \@messages,
     op          => $op,
 );
index bce24c5..445d839 100644 (file)
     </div>
 
     <h2>Cities</h2>
-    [% IF searchfield %]
-        Searching: [% searchfield | html %]
+    [% IF city_name_filter %]
+        Searching: [% city_name_filter | html %]
     [% END %]
 
-    <table id="table_cities">
-        <thead>
-            <tr>
-                <th>City ID</th>
-                <th>City</th>
-                <th>State</th>
-                <th>ZIP/Postal code</th>
-                <th>Country</th>
-                <th>Actions</th>
-            </tr>
-        </thead>
-    </table>
+    [% IF cities_count > 0 %]
+        <table id="table_cities">
+            <thead>
+                <tr>
+                    <th>City ID</th>
+                    <th>City</th>
+                    <th>State</th>
+                    <th>ZIP/Postal code</th>
+                    <th>Country</th>
+                    <th data-class-name="actions">Actions</th>
+                </tr>
+            </thead>
+        </table>
+    [% ELSE %]
+        <div class="dialog message">
+            There are no cities defined. <a href="/cgi-bin/koha/admin/cities.pl?op=add_form">Create a new city</a>.
+        </div>
+    [% END %]
 [% END %]
 
             </main>
     [% INCLUDE 'datatables.inc' %]
     <script>
         $(document).ready(function() {
-            var cities_table_url = '/api/v1/cities';
+            var cities_table_url = '/api/v1/cities?';
+        [% IF city_name_filter %]
+            var city_name_filter = {
+                'name': {
+                    "like": '%[%- city_name_filter | html -%]%'
+                }
+            };
+            cities_table_url += 'q='+ encodeURI(JSON.stringify(city_name_filter));
+        [% END %]
             var cities_table = $("#table_cities").api({
                 "ajax": {
                     "url": cities_table_url
                 },
                 'dom': 'C<"top pager"ilpfB><"#filter_c">tr<"bottom pager"ip>',
+                "order": [[ 1, "asc" ]],
                 "columns": [
                     {
                         "data": "city_id",
                     {
                         "data": function( row, type, val, meta ) {
 
-                            var result = '<a class="btn btn-default btn-xs" role="button" href="/cgi-bin/koha/admin/cities.pl?op=add_form&amp;cityid='+ row.city_id +'"><i class="fa fa-pencil" aria-hidden="true"></i> '+_("Edit")+'</a>';
+                            var result = '<a class="btn btn-default btn-xs" role="button" href="/cgi-bin/koha/admin/cities.pl?op=add_form&amp;cityid='+ row.city_id +'"><i class="fa fa-pencil" aria-hidden="true"></i> '+_("Edit")+'</a>'+"\n";
                             result += '<a class="btn btn-default btn-xs" role="button" href="/cgi-bin/koha/admin/cities.pl?op=delete_confirm&amp;cityid='+ row.city_id +'"><i class="fa fa-trash" aria-hidden="true"></i> '+_("Delete")+'</a>';
                             return result;