add file selections to allow for ordered pool selections
authorTony Cook <tony@develop-help.com>
Wed, 20 Apr 2011 09:42:02 +0000 (19:42 +1000)
committerTony Cook <tony@develop-help.com>
Wed, 20 Apr 2011 09:42:02 +0000 (19:42 +1000)
MANIFEST
schema/bse.sql
site/cgi-bin/modules/BSE/TB/Files.pm
site/cgi-bin/modules/BSE/TB/SelectedFile.pm [new file with mode: 0644]
site/cgi-bin/modules/BSE/TB/SelectedFiles.pm [new file with mode: 0644]
site/data/db/sql_statements.data
site/util/mysql.str

index 092213f..6cf93dd 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -191,6 +191,8 @@ site/cgi-bin/modules/BSE/TB/ProductOption.pm
 site/cgi-bin/modules/BSE/TB/ProductOptions.pm
 site/cgi-bin/modules/BSE/TB/ProductOptionValue.pm
 site/cgi-bin/modules/BSE/TB/ProductOptionValues.pm
+site/cgi-bin/modules/BSE/TB/SelectedFile.pm
+site/cgi-bin/modules/BSE/TB/SelectedFiles.pm
 site/cgi-bin/modules/BSE/TB/Seminar.pm
 site/cgi-bin/modules/BSE/TB/SeminarBooking.pm
 site/cgi-bin/modules/BSE/TB/SeminarBookings.pm
index c488bf2..0483e6d 100644 (file)
@@ -1210,4 +1210,20 @@ create table bse_files (
   description text not null,
 
   index owner(file_type, owner_id)
-);
\ No newline at end of file
+);
+
+-- a generic selection of files from a pool
+create table bse_selected_files (
+  id integer not null auto_increment primary key,
+
+  -- who owns this selection of files
+  owner_id integer not null,
+  owner_type varchar(20) not null,
+
+  -- one of the files
+  file_id integer not null,
+
+  display_order integer not null default -1,
+
+  unique only_one(owner_id, owner_type, file_id)
+) type = InnoDB;
\ No newline at end of file
index b4454c8..d308d08 100644 (file)
@@ -6,7 +6,7 @@ use vars qw(@ISA $VERSION);
 use BSE::TB::File;
 use Carp ();
 
-our $VERSION = "1.001";
+our $VERSION = "1.002";
 
 sub rowClass {
   return 'BSE::TB::File';
@@ -215,4 +215,107 @@ sub save_cgi_file {
   return $file;
 }
 
+=item selected_files
+
+Return the files selected out of a pool for a particular owner.
+
+Returns them in order.
+
+=cut
+
+sub selected_files {
+  my ($self, $owner_type, $owner_id) = @_;
+
+  my %files = map { $_->id() => $_ }
+    $self->getSpecial(selected_files => $owner_type, $owner_id);
+
+  my @order = $self->selected_file_ids($owner_type, $owner_id);
+
+  return map { $files{$_} ? $files{$_} : () } @order;
+}
+
+=item selected_file_ids
+
+Return the id of the files selected out of a pool for a particular
+owner.
+
+Returns them in order.
+
+=cut
+
+sub selected_file_ids {
+  my ($self, $owner_type, $owner_id) = @_;
+
+  require BSE::TB::SelectedFiles;
+  my @order = BSE::TB::SelectedFiles->getColumnBy
+    (
+     "file_id",
+     [ "and" =>
+       [ "=", "owner_id", $owner_id ],
+       [ "=", "owner_type", $owner_type ]
+     ],
+     { order => "display_order" }
+    );
+
+  return @order;
+}
+
+=item set_selected_files
+
+Set the list of selected files for a particular owner.
+
+Also sets the order.
+
+=cut
+
+sub set_selected_files {
+  my ($self, $owner_type, $owner_id, $files) = @_;
+
+  require BSE::TB::SelectedFiles;
+  BSE::DB->do_txn
+      (sub {
+        my @ids = map { ref() ? $_->id : $_ } @$files;
+
+        my %current = map { $_->file_id() => $_ }
+          BSE::TB::SelectedFiles->getBy2
+              (
+               [ "and" =>
+                 [ "=", "owner_id", $owner_id ],
+                 [ "=", "owner_type", $owner_type ],
+               ]
+              );
+
+        my $display_order = 1;
+        for my $id (@ids) {
+          my $sel = delete $current{$id};
+          if ($sel) {
+            $sel->set_display_order($display_order);
+            $sel->save;
+          }
+          else {
+            $sel = BSE::TB::SelectedFiles->make
+              (
+               owner_type => $owner_type,
+               owner_id => $owner_id,
+               file_id => $id,
+               display_order => $display_order,
+              );
+          }
+          ++$display_order;
+        }
+
+        $_->remove for values %current;
+       });
+}
+
+=item selection_owner_removed
+
+=cut
+
+sub selection_owner_removed {
+  my ($self, $owner_type, $owner_id) = @_;
+
+  BSE::DB->single->run("SelectedFiles.remove_owner" => $owner_type, $owner_id);
+}
+
 1;
diff --git a/site/cgi-bin/modules/BSE/TB/SelectedFile.pm b/site/cgi-bin/modules/BSE/TB/SelectedFile.pm
new file mode 100644 (file)
index 0000000..2ed61cc
--- /dev/null
@@ -0,0 +1,15 @@
+package BSE::TB::SelectedFile;
+use strict;
+use base "Squirrel::Row";
+
+our $VERSION = "0.001";
+
+sub columns {
+  qw/id owner_id owner_type file_id display_order/;
+}
+
+sub table {
+  return "bse_selected_files";
+}
+
+1;
diff --git a/site/cgi-bin/modules/BSE/TB/SelectedFiles.pm b/site/cgi-bin/modules/BSE/TB/SelectedFiles.pm
new file mode 100644 (file)
index 0000000..695f5b1
--- /dev/null
@@ -0,0 +1,10 @@
+package BSE::TB::SelectedFiles;
+use strict;
+use base "Squirrel::Table";
+use BSE::TB::SelectedFile;
+
+our $VERSION = "0.001";
+
+sub rowClass { "BSE::TB::SelectedFile" }
+
+1;
index 1905d79..0ddcbc2 100644 (file)
@@ -1,5 +1,5 @@
 --
-# VERSION=1.000
+# VERSION=1.001
 name: bse_siteuserSeminarBookingsDetail
 sql_statement: <<SQL
 select ar.*, pr.*, se.*, ss.*, sb.*,
@@ -440,3 +440,20 @@ where af.id = ?
   and od.paidFor <> 0
   and od.siteuser_id = ?
 SQL
+
+name: Files.selected_files
+sql_statement: <<SQL
+select bf.*
+from bse_files bf, bse_selected_files sf
+where sf.owner_type = ?
+  and sf.owner_id = ?
+  and sf.file_id = bf.id
+SQL
+
+name: SelectedFiles.remove_owner
+sql_statement: <<SQL
+delete from bse_selected_files
+ where owner_type = ?
+   and owner_id = ?
+SQL
+
index a583360..afbadf0 100644 (file)
@@ -291,6 +291,14 @@ Column enabled;int(11);NO;0;
 Column default_value;int(11);YES;NULL;
 Index PRIMARY;1;[id]
 Index product_order;0;[product_id;display_order]
+Table bse_selected_files
+Column id;int(11);NO;NULL;auto_increment
+Column owner_id;int(11);NO;NULL;
+Column owner_type;varchar(20);NO;NULL;
+Column file_id;int(11);NO;NULL;
+Column display_order;int(11);NO;-1;
+Index PRIMARY;1;[id]
+Index only_one;1;[owner_id;owner_type;file_id]
 Table bse_seminar_bookings
 Column id;int(11);NO;NULL;auto_increment
 Column session_id;int(11);NO;NULL;