[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb:[3492] trunk



Revision: 3492
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=3492&view=rev
Author:   einhverfr
Date:     2011-07-11 11:19:11 +0000 (Mon, 11 Jul 2011)

Log Message:
-----------
more file attachment fixes and code

Modified Paths:
--------------
    trunk/LedgerSMB/File.pm
    trunk/scripts/file.pl
    trunk/sql/modules/Files.sql

Modified: trunk/LedgerSMB/File.pm
===================================================================
--- trunk/LedgerSMB/File.pm	2011-07-11 09:42:10 UTC (rev 3491)
+++ trunk/LedgerSMB/File.pm	2011-07-11 11:19:11 UTC (rev 3492)
@@ -154,6 +154,23 @@
     }
 }
 
+=item get_mime_type
+
+Sends the textual representation of the MIME type.  If not set, retrieves and
+sets it.
+
+=cut
+
+sub get_mime_type {
+    my ($self) = @_;
+    if ($self->mime_type_text){
+       return $self->mime_type_text;
+    } else {
+       my ($ref) = $self->exec_method({funcname => 'file__mime_type_text'});
+       $self->mime_type_text($ref->{mime_type});
+       return $self->mime_type_text;
+    }
+}
 =item get
 
 Retrives a file.  ID and file_class properties must be set.

Modified: trunk/scripts/file.pl
===================================================================
--- trunk/scripts/file.pl	2011-07-11 09:42:10 UTC (rev 3491)
+++ trunk/scripts/file.pl	2011-07-11 11:19:11 UTC (rev 3492)
@@ -2,7 +2,7 @@
 
 =head1 NAME
 
-LedgerSMB:Scripts::file
+LedgerSMB::Scripts::file
 
 =head1 SYNOPSIS
 
@@ -16,18 +16,71 @@
 
 Retrieves a file and sends it to the web browser.
 
+Requires that id and file_class be set.
+
 =cut
 
+package LedgerSMB::Scripts::file;
+use strict;
+use CGI::Simple;
+
+our $fileclassmap = {
+   1   => 'LedgerSMB::File::Transaction',
+   2   => 'LedgerSMB::File::Order',
+};
+
+sub get {
+    my ($request) = @_;
+    my $file = LedgerSMB::File->new();
+    $file->dbobject(LedgerSMB::DBObject->new({base => $request}));
+    $file->id($request->{id});
+    $file->file_class($request->{file_class});
+    $file->get();
+
+    my $cgi = CGI::Simple->new();
+
+    print $cgi->header(
+          -type       => $file->get_mime_type,
+          -status     => '200',
+          -charset    => 'utf-8',
+          -attachment => $file->file_name,
+    );
+    print $file->content;
+}
+
 =item show_attachment_screen
 
 Show the attachment or upload screen.
 
 =cut
 
+sub show_attachment_screen {
+    my ($request) = @_;
+    my $template = LedgerSMB::Template(
+        user     => $request->{_user},
+        locale   => $request->{_locale},
+        path     => 'UI/file',
+        template => 'attachment_screen',
+        format   => 'HTML'
+    );
+    $template->render($request);
+}
+
 =item attach
+
+Attaches a file to an object
         
 =cut
 
+sub attach {
+    my ($request) = @_;
+    my $file = $fileclassmap->{$request->{file_class}}->new;
+    $file->merge($request);
+    $file->attach;
+    my $cgi = LedgerSMB::CGI->new;
+    print $cgi->redirect($request->{callback});
+}
+
 =back
 
 =head1 COPYRIGHT

Modified: trunk/sql/modules/Files.sql
===================================================================
--- trunk/sql/modules/Files.sql	2011-07-11 09:42:10 UTC (rev 3491)
+++ trunk/sql/modules/Files.sql	2011-07-11 11:19:11 UTC (rev 3492)
@@ -1,4 +1,13 @@
-create or replace function file__attach_to_tx,
+CREATE OR REPLACE FUNCTION file__get_mime_type(in_mime_type_id int)
+RETURNS mime_type AS
+$$
+select * from mime_type where id = $1;
+$$ language sql;
+
+COMMENT ON FUNCTION file__get_mime_type(in_mime_type_id int) IS
+$$Retrieves mime type information associated with a file object.$$;
+
+CREATE OR REPLACE FUNCTION file__attach_to_tx
 (in_content bytea, in_mime_type_id int, in_file_name text,
 in_description text, in_id int, in_ref_key int, in_file_class int)
 RETURNS file_base
@@ -32,8 +41,15 @@
 END;
 $$ LANGUAGE PLPGSQL;
 
-create or replace function file__attach_to_order
+COMMENT ON FUNCTION file__attach_to_tx
 (in_content bytea, in_mime_type_id int, in_file_name text,
+in_description text, in_id int, in_ref_key int, in_file_class int) IS
+$$ Attaches or links a file to a transaction.  in_content OR id can be set.
+Setting both raises an exception.$$;
+
+
+CREATE OR REPLACE FUNCTION file__attach_to_order
+(in_content bytea, in_mime_type_id int, in_file_name text,
 in_description text, in_id int, ref_key int, file_class int)
 RETURNS file_base
 AS
@@ -74,6 +90,13 @@
 END;
 $$ LANGUAGE PLPGSQL;
 
+COMMENT ON FUNCTION file__attach_to_order
+(in_content bytea, in_mime_type_id int, in_file_name text,
+in_description text, in_id int, in_ref_key int, in_file_class int) IS
+$$ Attaches or links a file to an order.  in_content OR id can be set.
+Setting both raises an exception.$$;
+
+
 CREATE TYPE file_list_item AS
        mime_type text,
        file_name text,
@@ -85,7 +108,8 @@
        ref_key int,
        file_class int
 );
-create or replace function file__list_by(in_ref_key int, in_file_class int)
+
+CREATE OR REPLACE FUNCTION file__list_by(in_ref_key int, in_file_class int)
 RETURNS SETOF file_base AS
 $$
 
@@ -98,12 +122,18 @@
 
 $$ language sql;
 
-create or replace function file__get(in_id int, in_file_class int)
+COMMENT ON FUNCTION file__list_by(in_ref_key int, in_file_class int) IS
+$$ Returns a list of files attached to a database object.  No content is 
+retrieved.$$;
+
+CREATE OR REPLACE FUNCTION file__get(in_id int, in_file_class int)
 RETURNS file_base AS
 $$
 SELECT * FROM file_base where id = $1 and file_class = $2;
 $$ language sql;
 
+COMMENT ON FUNCTION file__get(in_id int, in_file_class int) IS
+$$ Retrieves the file information specified including content.$$;
 
 DROP VIEW IF EXISTS file_links;
 DROP VIEW IF EXISTS file_tx_links;
@@ -170,3 +200,11 @@
 
 select * from file_links_vrebuild();
 
+
+CREATE OR REPLACE FUNCTION file__list_links(in_ref_key int, in_file_class int)
+RETURNS setof file_links AS
+$$ select * from file_links where ref_key = $1 and file_class = $2;
+$$ language sql;
+
+COMMENT ON FUNCTION file__list_links(in_ref_key int, in_file_class int) IS
+$$ This function retrieves a list of file attachments on a specified object.$$;


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.