Extending branch categories : adding search domains. partial commit.
authorRyan Higgins <rch@liblime.com>
Sun, 21 Oct 2007 20:11:37 +0000 (15:11 -0500)
committerJoshua Ferraro <jmf@liblime.com>
Mon, 22 Oct 2007 00:18:36 +0000 (19:18 -0500)
Signed-off-by: Chris Cormack <crc@liblime.com>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
C4/Branch.pm
admin/branches.pl
installer/kohastructure.sql
koha-tmpl/intranet-tmpl/prog/en/modules/admin/branches.tmpl

index 561ec2c..08b10ae 100644 (file)
@@ -53,6 +53,9 @@ The functions in this module deal with branches.
    &ModBranch
    &CheckBranchCategorycode
    &GetBranchInfo
+   &GetCategoryTypes
+   &GetBranchCategories
+   &GetBranchesInCategory
    &ModBranchCategoryInfo
    &DelBranch
    &DelBranchCategory
@@ -64,6 +67,9 @@ The functions in this module deal with branches.
   returns informations about ALL branches.
   Create a branch selector with the following code
   IndependantBranches Insensitive...
+  GetBranchInfo() returns the same information without the problems of this function 
+  (namespace collision, mainly).  You should probably use that, and replace GetBranches()
+  with GetBranchInfo() where you see it in the code.
   
 =head3 in PERL SCRIPT
 
@@ -118,7 +124,8 @@ sub GetBranches {
             # you to list the categories that a branch belongs to:
             # you'd have to list keys %$branch, and remove those keys
             # that aren't fields in the "branches" table.
-            $branch->{$cat} = 1;
+         #   $branch->{$cat} = 1;
+            $branch->{category}{$cat} = 1;
         }
         $branches{ $branch->{'branchcode'} } = $branch;
     }
@@ -268,6 +275,23 @@ sub GetBranchCategory {
     return \@results;
 }
 
+=head2 GetCategoryTypes
+
+$categorytypes = GetCategoryTypes;
+returns a list of category types.
+Currently these types are HARDCODED.
+type: 'searchdomain' defines a group of agencies that the calling library may search in.
+Other usage of agency categories falls under type: 'properties'.
+       to allow for other uses of categories.
+The searchdomain bit may be better implemented as a separate module, but
+the categories were already here, and minimally used.
+=cut
+
+       #TODO  manage category types.  rename possibly to 'agency domains' ? as borrowergroups are called categories.
+sub GetCategoryTypes() {
+       return ( 'searchdomain','properties');
+}
+
 =head2 GetBranch
 
 $branch = GetBranch( $query, $branches );
@@ -302,7 +326,6 @@ sub GetBranchDetail {
     return $branchname;
 }
 
-
 =head2 get_branchinfos_of
 
   my $branchinfos_of = get_branchinfos_of(@branchcodes);
@@ -330,19 +353,75 @@ sub get_branchinfos_of {
     return C4::Koha::get_infos_of( $query, 'branchcode' );
 }
 
+=head2 GetBranchCategories
+
+  my $categories = GetBranchCategories($branchcode,$categorytype);
+
+Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
+i.e. categorycode, categorydescription, categorytype, categoryname.
+if $branchcode and/or $categorytype are passed, limit set to categories that
+$branchcode is a member of , and to $categorytype.
+
+=cut
+
+sub GetBranchCategories($$) {
+    my ($branchcode,$categorytype) = @_;
+       my $dbh = C4::Context->dbh();
+       my $select = "SELECT c.* FROM branchcategories c";
+       my (@where, @bind);
+       if($branchcode) {
+               $select .= ",branchrelations r, branches b ";
+               push @where, "c.categorycode=r.categorycode and r.branchcode=? ";  
+               push @bind , $branchcode;
+       }
+       if ($categorytype) {
+               push @where, " c.categorytype=? ";
+               push @bind, $categorytype;
+       }
+    my $sth=$dbh->prepare( $select . " where " . join(" and ", @where) );
+       $sth->execute(@bind);
+       
+       my $branchcats = $sth->fetchall_arrayref({});
+       $sth->finish();
+       return( $branchcats );
+}
+
+
+=head2 GetBranchesInCategory
+
+  my $branches = GetBranchesInCategory($categorycode);
+
+Returns a href:  keys %$branches eq (branchcode,branchname) .
+
+=cut
+
+sub GetBranchesInCategory($) {
+    my ($categorycode) = @_;
+       my $dbh = C4::context->dbh();
+       my $sth=$dbh->prepare( "SELECT branchcode, branchname FROM branchrelations r, branches b 
+                                                       where r.branchcode=b.branchcode and r.categorycode=?");
+    $sth->execute($categorycode);
+       my $branches = $sth->fetchall_hashref;
+       $sth->finish();
+       return( $branches );
+}
+
 =head2 GetBranchInfo
 
 $results = GetBranchInfo($branchcode);
 
 returns C<$results>, a reference to an array of hashes containing branches.
-
+if $branchcode, just this branch, with associated categories.
+if ! $branchcode && $categorytype, all branches in the category.
 =cut
 
 sub GetBranchInfo {
-    my ($branchcode) = @_;
+    my ($branchcode,$categorytype) = @_;
     my $dbh = C4::Context->dbh;
     my $sth;
-    if ($branchcode) {
+
+
+       if ($branchcode) {
         $sth =
           $dbh->prepare(
             "Select * from branches where branchcode = ? order by branchcode");
@@ -354,10 +433,16 @@ sub GetBranchInfo {
     }
     my @results;
     while ( my $data = $sth->fetchrow_hashref ) {
-        my $nsth =
-          $dbh->prepare(
-            "select categorycode from branchrelations where branchcode = ?");
-        $nsth->execute( $data->{'branchcode'} );
+               my @bind = ($data->{'branchcode'});
+        my $query= "select r.categorycode from branchrelations r";
+               $query .= ", branchcategories c " if($categorytype);
+               $query .= " where  branchcode=? ";
+               if($categorytype) { 
+                       $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
+                       push @bind, $categorytype;
+               }
+        my $nsth=$dbh->prepare($query);
+               $nsth->execute( @bind );
         my @cats = ();
         while ( my ($cat) = $nsth->fetchrow_array ) {
             push( @cats, $cat );
@@ -395,8 +480,8 @@ sub ModBranchCategoryInfo {
 
     my ($data) = @_;
     my $dbh    = C4::Context->dbh;
-    my $sth    = $dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)");
-    $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'} );
+    my $sth    = $dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription,categorytype) values (?,?,?,?)");
+    $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
     $sth->finish;
 }
 
index 0b7532c..4f314bd 100755 (executable)
@@ -282,6 +282,7 @@ sub editbranchform {
           {
             categoryname    => $cat->{'categoryname'},
             categorycode    => $cat->{'categorycode'},
+            categorytype    => $cat->{'categorytype'},
             codedescription => $cat->{'codedescription'},
             checked         => $checked,
           };
@@ -303,13 +304,19 @@ sub editcatform {
     my ($categorycode,$innertemplate) = @_;
     warn "cat : $categorycode";
     my $data;
-    if ($categorycode) {
+       my @cats;
+    $innertemplate->param( categorytype => \@cats);
+       if ($categorycode) {
         $data = GetBranchCategory($categorycode);
         $data = $data->[0];
-        $innertemplate->param( categorycode    => $data->{'categorycode'} );
-        $innertemplate->param( categoryname    => $data->{'categoryname'} );
-        $innertemplate->param( codedescription => $data->{'codedescription'} );
+        $innertemplate->param( categorycode    => $data->{'categorycode'} ,
+                                       categoryname    => $data->{'categoryname'},
+                                       codedescription => $data->{'codedescription'} ,
+                                               );
     }
+       for my $ctype (GetCategoryTypes()) {
+               push @cats , { type => $ctype , selected => ($data->{'categorytype'} eq $ctype) };
+       }
 }
 
 sub deleteconfirm {
@@ -328,7 +335,7 @@ sub branchinfotable {
         $branchinfo = GetBranchInfo($branchcode);
     }
     else {
-        $branchinfo = GetBranchInfo();
+        $branchinfo = GetBranchInfo(undef,'properties');
     }
     my $toggle;
     my $i;
@@ -408,20 +415,20 @@ sub branchinfotable {
         $i++;
     }
     my @branchcategories = ();
-    my $catinfo          = GetBranchCategory();
-    $i = 0;
-    foreach my $cat (@$catinfo) {
-        ( $i % 2 ) ? ( $toggle = 1 ) : ( $toggle = 0 );
-        push @branchcategories,
-          {
-            toggle          => $toggle,
-            categoryname    => $cat->{'categoryname'},
-            categorycode    => $cat->{'categorycode'},
-            codedescription => $cat->{'codedescription'},
-          };
-        $i++;
-    }
-
+       for my $ctype ( GetCategoryTypes() ) {
+       my $catinfo = GetBranchCategories(undef,$ctype);
+       my @categories;
+               foreach my $cat (@$catinfo) {
+               push @categories,
+                 {
+                   categoryname    => $cat->{'categoryname'},
+                   categorycode    => $cat->{'categorycode'},
+                   codedescription => $cat->{'codedescription'},
+                   categorytype => $cat->{'categorytype'},
+                 };
+       }
+       push @branchcategories, { categorytype => $ctype , $ctype => 1 , catloop => \@categories};
+       }
     $innertemplate->param(
         branches         => \@loop_data,
         branchcategories => \@branchcategories
@@ -429,7 +436,7 @@ sub branchinfotable {
 
 }
 
-# FIXME logic seems wrong
+# FIXME logic seems wrong   ##  sub is not used.
 sub branchcategoriestable {
     my $innertemplate = shift;
     #Needs to be implemented...
index 50d53c5..59aa4f7 100644 (file)
@@ -489,9 +489,10 @@ CREATE TABLE `borrowers` (
 
 DROP TABLE IF EXISTS `branchcategories`;
 CREATE TABLE `branchcategories` (
-  `categorycode` varchar(4) NOT NULL default '',
-  `categoryname` mediumtext,
+  `categorycode` char(10) NOT NULL default '',
+  `categoryname` varchar(32),
   `codedescription` mediumtext,
+  `categorytype` varchar(16),
   PRIMARY KEY  (`categorycode`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
@@ -522,7 +523,8 @@ CREATE TABLE `branches` (
 DROP TABLE IF EXISTS `branchrelations`;
 CREATE TABLE `branchrelations` (
   `branchcode` char(10) NOT NULL default '',
-  `categorycode` varchar(4) NOT NULL default '',
+  `categorycode` char(10) NOT NULL default '',
+  `relation`  char(10) default NULL,
   PRIMARY KEY  (`branchcode`,`categorycode`),
   KEY `branchcode` (`branchcode`),
   KEY `categorycode` (`categorycode`),
index 034458f..35bd1bc 100644 (file)
             <th>Name</th>
             <th>Code</th>
             <th>Address</th>
-            <th>Category</th>
-            <th>Library IP</th>
-            <th>Library Printer</th>
+            <th>Properties</th>
+            <th>IP</th>
+            <th>Printer</th>
             <th colspan="2">&nbsp;</th>
         </tr>
         <!-- TMPL_LOOP name="branches" -->
                 </td>
                 <td>
                     <!-- TMPL_IF name="no-categories-p" -->
-                        (no categories set)
                     <!-- TMPL_ELSE -->
                         <!-- TMPL_LOOP name="category_list" -->
                             <!-- TMPL_VAR name="categoryname" --><br />
     </form>
     
     <!-- TMPL_IF NAME="branchcategories" -->
-    <table>
-    <caption>Agency Categories</caption>
+   
+   <!-- TMPL_LOOP NAME="branchcategories" -->
+       <table>
+    <caption>Agency Categories:  <!-- TMPL_IF NAME="properties" -->Properties<!-- TMPL_ELSE --><!-- TMPL_IF NAME="searchdomain" -->Search Domain<!-- /TMPL_IF --><!-- /TMPL_IF --></caption>
     <tr>
         <th>Name</th>
         <th>Code</th>
         <th>&nbsp;</th>
         <th>&nbsp;</th>
     </tr>
-    <!-- TMPL_LOOP name="branchcategories" -->
+    <!-- TMPL_LOOP name="catloop" -->
         <tr>
         <td><!-- TMPL_VAR name="categoryname" --></td>
         <td><!-- TMPL_VAR name="categorycode" --></td>
         </tr>
     <!-- /TMPL_LOOP -->
     </table>
+    <!-- /TMPL_LOOP -->
+
     <!-- TMPL_ELSE -->
         <p>
             No Agency Categories defined.
                     <!-- TMPL_VAR name="categorycode" -->
                 <!-- TMPL_ELSE -->
             <label for="categorycode">Category code:</label>
-                    <input type="text" name="categorycode" id="categorycode" size="5" maxlength="5" value="<!-- TMPL_VAR name="categorycode" escape="HTML" -->" />
+                    <input type="text" name="categorycode" id="categorycode" size="11" maxlength="10" value="<!-- TMPL_VAR name="categorycode" escape="HTML" -->" />
                 <!-- /TMPL_IF -->
             </li>
         <li>
         <li>
             <label for="codedescription">Description: </label>
             <input type="text" name="codedescription" id="codedescription" size="70" maxlength="80" value="<!-- TMPL_VAR name="codedescription" escape="HTML" -->" />
-        </li></ol>
+        </li>
+               <li>
+               <label for="categorytype">Category Type</label>
+            <select id="categorytype" name="categorytype">
+            <!-- TMPL_LOOP NAME="categorytype" -->
+                <option value="<!-- TMPL_VAR NAME="type" -->" <!-- TMPL_IF NAME="selected" -->selected="selected"<!-- /TMPL_IF -->><!-- TMPL_VAR NAME="type" --></option>
+                <!-- /TMPL_LOOP -->
+            </select>
+               </li>
+               </ol>
     </fieldset><input type="submit" value="Update" />
     </form>
 <!-- /TMPL_IF -->