Cleanup cities.pl and .tmpl - major
authorJoe Atzberger <joe.atzberger@liblime.com>
Fri, 5 Jun 2009 17:18:50 +0000 (12:18 -0500)
committerGalen Charlton <galen.charlton@liblime.com>
Fri, 5 Jun 2009 22:15:05 +0000 (17:15 -0500)
80% of the javascript was junk.
Bad check failed on non-existant field borrowers.select_city.
Enabled warnings.  Reduced "StringSearch" to 3 lines, removed unused args and vars.
Fixed row highlighting and removed "toggle" code.  Etc.

TODO: Stop redirecting to the same page.  Instead get the correct data and show it
on THIS pass.

Signed-off-by: Galen Charlton <galen.charlton@liblime.com>
admin/cities.pl
koha-tmpl/intranet-tmpl/prog/en/modules/admin/cities.tmpl

index fe26caa..1e7e877 100755 (executable)
 # Suite 330, Boston, MA  02111-1307 USA
 
 use strict;
+use warnings;
 use CGI;
 use C4::Context;
 use C4::Auth;
 use C4::Output;
 
-sub StringSearch  {
-       my ($searchstring,$type)=@_;
-       my $dbh = C4::Context->dbh;
-       $searchstring=~ s/\'/\\\'/g;
-       my @data=split(' ',$searchstring);
-       my $count=@data;
-       my $sth=$dbh->prepare("Select * from cities where (city_name like ?)");
-       $sth->execute("$data[0]%");
-       my @results;
-       while (my $data=$sth->fetchrow_hashref){
-       push(@results,$data);
-       }
-       #  $sth->execute;
-       $sth->finish;
-       return (scalar(@results),\@results);
+sub StringSearch {
+       my $sth = C4::Context->dbh->prepare("SELECT * FROM cities WHERE (city_name LIKE ?)");
+       $sth->execute("%" . (shift || '') . "%");
+    return $sth->fetchall_arrayref({});
 }
 
 my $input = new CGI;
-my $searchfield=$input->param('city_name');
-my $script_name="/cgi-bin/koha/admin/cities.pl";
-my $cityid=$input->param('cityid');
-my $op = $input->param('op');
+my $script_name = "/cgi-bin/koha/admin/cities.pl";
+my $searchfield = $input->param('city_name');
+my $cityid      = $input->param('cityid');
+my $op          = $input->param('op') || '';
 
 my ($template, $loggedinuser, $cookie)
     = get_template_and_user({template_name => "admin/cities.tmpl",
@@ -55,12 +45,11 @@ my ($template, $loggedinuser, $cookie)
                             debug => 1,
                             });
 
-
 $template->param(      script_name => $script_name,
                        cityid     => $cityid ,
                        searchfield => $searchfield);
 
-
+my $dbh = C4::Context->dbh;
 ################## ADD_FORM ##################################
 # called by default. Used to create form to add or  modify a record
 if ($op eq 'add_form') {
@@ -69,95 +58,57 @@ if ($op eq 'add_form') {
        #---- if primkey exists, it's a modify action, so read values to modify...
        my $data;
        if ($cityid) {
-               my $dbh = C4::Context->dbh;
                my $sth=$dbh->prepare("select cityid,city_name,city_zipcode from cities where  cityid=?");
                $sth->execute($cityid);
                $data=$sth->fetchrow_hashref;
-               $sth->finish;
        }
 
        $template->param(       
                                city_name       => $data->{'city_name'},
                                city_zipcode    => $data->{'city_zipcode'});
-##############ICI#####################
 # END $OP eq ADD_FORM
 ################## ADD_VALIDATE ##################################
 # called by add_form, used to insert/modify data in DB
 } elsif ($op eq 'add_validate') {
-       my $dbh = C4::Context->dbh;
        my $sth;
        
        if ($input->param('cityid') ){
                $sth=$dbh->prepare("UPDATE cities SET city_name=?,city_zipcode=? WHERE cityid=?");
                $sth->execute($input->param('city_name'),$input->param('city_zipcode'),$input->param('cityid'));
-       
        }
        else{   
                $sth=$dbh->prepare("INSERT INTO cities (city_name,city_zipcode) values (?,?)");
                $sth->execute($input->param('city_name'),$input->param('city_zipcode'));
        }
-       $sth->finish;
-       print $input->redirect("/cgi-bin/koha/admin/cities.pl");
+       print $input->redirect($script_name);
        exit;
-# END $OP eq ADD_VALIDATE
 ################## DELETE_CONFIRM ##################################
 # called by default form, used to confirm deletion of data in DB
 } elsif ($op eq 'delete_confirm') {
        $template->param(delete_confirm => 1);
-
-       my $dbh = C4::Context->dbh;
-       my $sth=$dbh->prepare("select count(*) as total from borrowers,cities where borrowers.select_city=cities.cityid and cityid=?");
+       my $sth=$dbh->prepare("select count(*) as total from borrowers,cities where borrowers.city=cities.city_name and cityid=?");
+    # FIXME: this check used to pretend there was a FK "select_city" in borrowers.
        $sth->execute($cityid);
        my $total = $sth->fetchrow_hashref;
-       $sth->finish;
-       $template->param(total => $total->{'total'});   
        my $sth2=$dbh->prepare("select cityid,city_name,city_zipcode from cities where  cityid=?");
        $sth2->execute($cityid);
        my $data=$sth2->fetchrow_hashref;
-       $sth2->finish;
-       if ($total->{'total'} >0) {
-               $template->param(totalgtzero => 1);
-       }
-
-        $template->param(      
-                               city_name       =>      ( $data->{'city_name'}),
-                               city_zipcode    =>       $data->{'city_zipcode'});
-
-
-                                                                                                       # END $OP eq DELETE_CONFIRM
+    $template->param(
+        total        => $total->{'total'},
+        city_name    =>        $data->{'city_name'},
+        city_zipcode => $data->{'city_zipcode'},
+    );
 ################## DELETE_CONFIRMED ##################################
 # called by delete_confirm, used to effectively confirm deletion of data in DB
 } elsif ($op eq 'delete_confirmed') {
-       my $dbh = C4::Context->dbh;
-       my $categorycode=uc($input->param('cityid'));
        my $sth=$dbh->prepare("delete from cities where cityid=?");
        $sth->execute($cityid);
-       $sth->finish;
-       print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=cities.pl\"></html>";
-       exit;
+       print $input->redirect($script_name);
+       exit;   # FIXME: what's the point of redirecting to this same page?
                                                                                                        # END $OP eq DELETE_CONFIRMED
 } else { # DEFAULT
        $template->param(else => 1);
-       my @loop;
-       my ($count,$results)=StringSearch($searchfield,'web');
-       my $toggle = 0;
-       for (my $i=0; $i < $count; $i++){
-               my %row = (cityid => $results->[$i]{'cityid'},
-                               city_name => $results->[$i]{'city_name'},
-                               city_zipcode => $results->[$i]{'city_zipcode'},
-                               toggle => $toggle );    
-               push @loop, \%row;
-               if ( $toggle eq 0 )
-               {
-                       $toggle = 1;
-               }
-               else
-               {
-                       $toggle = 0;
-               }
-       }
-       $template->param(loop => \@loop);
-
+       $template->param(loop => StringSearch($searchfield));
 
 } #---- END $OP eq DEFAULT
 output_html_with_http_headers $input, $cookie, $template->output;
index 6d8b0df..6a96abf 100644 (file)
@@ -3,50 +3,16 @@
 <!-- TMPL_INCLUDE NAME="doc-head-close.inc" -->
 <script type="text/javascript">
 //<![CDATA[
-               /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-               function isNotNull(f,noalert) {
-                       if (f.value.length == 0) {
-               return false;
-                       }
-               return true;
-               }
-               /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-               function toUC(f) {
-                       var x=f.value.toUpperCase();
-                       f.value=x;
-                       return true;
-               }
-               /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-               function isNum(v,maybenull) {
-               var n = new Number(v.value);
-               if (isNaN(n)) {
-                       return false;
-                       }
-               if (maybenull == 0 && v.value =='') {
-                       return false;
-               }
-               return true;
-               }
-               /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-               function isDate(f) {
-                       var t = Date.parse(f.value);
-                       if (isNaN(t)) {
-                               return false;
-                       }
-               }
-               /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-               function Check(f) {
-                       var ok=1;
-                       var _alertString="";
-                       var alertString2;
-                       if (f.city_zipcode.value.length == 0 && f.city_name.value.length == 0 ) {
-                               _alertString += "\n- " + _("City name & zipcode missing");
-                               alert(_alertString);    
-                       }
-                       else{
-                       document.Aform.submit();
-                       }
-               }
+    function Check(f) {
+        if (f.city_zipcode.value.length == 0 && f.city_name.value.length == 0 ) {
+            alert("City name & zipcode missing");
+        } else{
+            document.Aform.submit();
+        }
+    }
+    $(document).ready(function() {
+        new YAHOO.widget.Button("newcity");
+    });
 //]]>
 </script>
 </head>
 <!-- TMPL_INCLUDE NAME="header.inc" -->
 <!-- TMPL_INCLUDE NAME="cities-admin-search.inc" -->
 
-<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a> &rsaquo; <!-- TMPL_IF NAME="add_form" --><a href="/cgi-bin/koha/admin/cities.pl">Cities</a> &rsaquo; <!-- TMPL_IF NAME="cityid" --> Modify City<!-- TMPL_ELSE --> New City<!-- /TMPL_IF --><!-- TMPL_ELSE --><!-- TMPL_IF NAME="delete_confirm" --><a href="/cgi-bin/koha/admin/cities.pl">Cities</a> &rsaquo; Confirm Deletion of City<!-- TMPL_ELSE --> Cities<!-- /TMPL_IF --><!-- /TMPL_IF --></div>
+<div id="breadcrumbs">
+    <a href="/cgi-bin/koha/mainpage.pl">Home</a>
+    &rsaquo; <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a>
+    &rsaquo; <a href="/cgi-bin/koha/admin/cities.pl">Cities</a>
+    <!-- TMPL_IF NAME="add_form" -->
+    &rsaquo; <!-- TMPL_IF NAME="cityid" -->Modify<!-- TMPL_ELSE -->New<!-- /TMPL_IF --> City
+    <!-- TMPL_ELSIF NAME="delete_confirm" -->
+    &rsaquo; Confirm Deletion of City
+    <!-- /TMPL_IF -->
+</div>
 
 <div id="doc3" class="yui-t2">
    
@@ -80,7 +55,7 @@
        <!-- /TMPL_IF -->
        <li>
        <label for="city_name">City name: </label>
-       <input  type="text" name="city_name" id="city_name" size="40" maxlength="80" value="<!-- TMPL_VAR NAME="city_name" ESCAPE="HTML" -->" />
+       <input type="text" name="city_name" id="city_name" size="40" maxlength="80" value="<!-- TMPL_VAR NAME="city_name" ESCAPE="HTML" -->" />
        </li>
        <li>                            
        <label for="city_zipcode">City zipcode: </label>
 
 <!-- /TMPL_IF -->
 <!-- TMPL_IF NAME="delete_confirm" -->
-               <!-- TMPL_IF NAME="totalgtzero" -->
-<div class="dialog message">
-               <h3>Cannot Delete City "<!-- TMPL_VAR NAME="city_name" -->"</h3>
-               <p>This record is used <!-- TMPL_VAR NAME="total" --> times. Impossible to delete it</p>
-               <!-- TMPL_ELSE -->
-<div class="dialog alert">
-<h3>Delete City "<!-- TMPL_VAR NAME="city_name" -->?"</h3>
-<!-- /TMPL_IF -->
-       <table>
-               <tr>
-                       <th>City id
-                       </th>
-                       <td>
-                               <!-- TMPL_VAR NAME="cityid" -->
-                       </td>
-               </tr>
-                       
-               <tr>
-                       <th>City name</th>
-                       <td><!-- TMPL_VAR NAME="city_name" --></td>
-               </tr>
-               <tr>
-                       <th>City zipcode</th>
-                       <td><!-- TMPL_VAR NAME="city_zipcode" --></td>
-               </tr>
-</table>
-               <!-- TMPL_IF NAME="totalgtzero" -->
-                               <form action="<!-- TMPL_VAR NAME="script_name" -->" method="post">
-                               <input type="submit" class="approv" value="OK" />
-                               </form>
-               <!-- TMPL_ELSE -->
-<form action="<!-- TMPL_VAR NAME="script_name" -->" method="post">
-                       <input type="hidden" name="op" value="delete_confirmed" />
-                       <input type="hidden" name="cityid" value="<!-- TMPL_VAR NAME="cityid" -->" /><input type="submit" class="approve" value="Yes, Delete" /></form> <form action="<!-- TMPL_VAR NAME="script_name" -->" method="post">
-                       <input type="submit" class="deny" value="No, do not Delete" /></form>
-               <!-- /TMPL_IF -->
+    <!-- TMPL_IF NAME="total" -->
+    <div class="dialog message">
+    <h3>Cannot Delete City "<!-- TMPL_VAR NAME="city_name" -->"</h3>
+    <p>This record is used <!-- TMPL_VAR NAME="total" --> times. Impossible to delete it</p>
+    <!-- TMPL_ELSE -->
+    <div class="dialog alert">
+    <h3>Delete City "<!-- TMPL_VAR NAME="city_name" -->?"</h3>
+    <!-- /TMPL_IF -->
+    <table>
+        <tr><th>City id</th>
+            <td><!-- TMPL_VAR NAME="cityid" --></td>
+        </tr>
+        <tr><th>City name</th>
+            <td><!-- TMPL_VAR NAME="city_name" --></td>
+        </tr>
+        <tr><th>City zipcode</th>
+            <td><!-- TMPL_VAR NAME="city_zipcode" --></td>
+        </tr>
+    </table>
+    <form action="<!-- TMPL_VAR NAME="script_name" -->" method="post">
+    <!-- TMPL_IF NAME="total" -->
+        <input type="submit" class="approv" value="OK" />
+    <!-- TMPL_ELSE -->
+        <input type="hidden" name="op" value="delete_confirmed" />
+        <input type="hidden" name="cityid" value="<!-- TMPL_VAR NAME="cityid" -->" />
+        <input type="submit" class="approve" value="Yes, Delete" />
+    </form>
+    <form action="<!-- TMPL_VAR NAME="script_name" -->" method="get">
+        <input type="submit" class="deny" value="No, do not Delete" />
+    <!-- /TMPL_IF -->
+    </form>
 </div>
 <!-- /TMPL_IF -->
 
 <!-- TMPL_IF NAME="else" -->
 
 <div id="toolbar">
-       <script type="text/javascript">
-       //<![CDATA[
-
-       // prepare DOM for YUI Toolbar
-
-        $(document).ready(function() {
-           yuiToolbar();
-        });
-
-       // YUI Toolbar Functions
-
-       function yuiToolbar() {
-           new YAHOO.widget.Button("newcity");
-       }
-
-       //]]>
-       </script>
        <ul class="toolbar">
-       <li><a id="newcity" href="/cgi-bin/koha/admin/cities.pl?op=add_form">New City</a></li>
+       <li><a id="newcity" href="<!-- TMPL_VAR NAME="script_name" -->?op=add_form">New City</a></li>
 </ul></div>
 
        <h2>Cities</h2>
                        <th>City zipcode</th>
                        <th>&nbsp;</th>
                        <th>&nbsp;</th>
-                       
                </tr>
                <!-- TMPL_LOOP NAME="loop" -->
-               <!-- TMPL_IF NAME="toggle" -->
-               <tr class="hilighted">
+               <!-- TMPL_UNLESS NAME="__odd__" -->
+               <tr class="highlight">
                <!-- TMPL_ELSE -->
                <tr>
-               <!-- /TMPL_IF -->
+               <!-- /TMPL_UNLESS -->
                        <td><!-- TMPL_VAR NAME="cityid" --></td>
                        <td><!-- TMPL_VAR NAME="city_name" --></td>
                        <td><!-- TMPL_VAR NAME="city_zipcode" --></td>