Index: openacs-4/packages/attachments/attachments.info
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/attachments.info,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/attachments/attachments.info	1 Jul 2002 20:16:23 -0000	1.1
+++ openacs-4/packages/attachments/attachments.info	2 Jul 2002 19:41:20 -0000	1.2
@@ -1,7 +1,7 @@
 <?xml version="1.0"?>
 <!-- Generated by the OpenACS Package Manager -->
 
-<package key="attachments" url="&quot;http://software.openforce.net/openacs/packages/attachments" type="apm_service">
+<package key="attachments" url="&quot;http://software.openforce.net/openacs/packages/attachments" type="apm_application">
     <package-name>attachments</package-name>
     <pretty-plural>attachments</pretty-plural>
     <initial-install-p>f</initial-install-p>
@@ -24,10 +24,34 @@
             <file type="package_spec" path="attachments.info"/>
             <file type="data_model_create" db_type="oracle" path="sql/oracle/attachments-create.sql"/>
             <file type="data_model_drop" db_type="oracle" path="sql/oracle/attachments-drop.sql"/>
+            <file type="query_file" db_type="oracle" path="tcl/attachments-procs-oracle.xql"/>
             <file type="tcl_procs" path="tcl/attachments-procs.tcl"/>
+            <file type="query_file" path="tcl/attachments-procs.xql"/>
+            <file type="content_page" path="www/admin/new-root-folder-map-2.tcl"/>
+            <file type="content_page" path="www/admin/new-root-folder-map.adp"/>
+            <file type="content_page" path="www/admin/new-root-folder-map.tcl"/>
+            <file type="content_page" path="www/admin/redirect.tcl"/>
+            <file type="content_page" path="www/attach-2.tcl"/>
+            <file type="content_page" path="www/attach.adp"/>
+            <file type="content_page" path="www/attach.tcl"/>
+            <file type="content_page" path="www/download/index.vuh"/>
+            <file type="query_file" path="www/download/index.xql"/>
+            <file type="query_file" db_type="oracle" path="www/file-add-2-oracle.xql"/>
+            <file type="query_file" db_type="postgresql" path="www/file-add-2-postgresql.xql"/>
+            <file type="content_page" path="www/file-add-2.tcl"/>
+            <file type="query_file" path="www/file-add-2.xql"/>
+            <file type="content_page" path="www/file-add.adp"/>
+            <file type="content_page" path="www/file-add.tcl"/>
+            <file type="content_page" path="www/go-to-attachment.tcl"/>
+            <file type="query_file" path="www/go-to-attachment.xql"/>
+            <file type="content_page" path="www/graphics/file.gif"/>
+            <file type="content_page" path="www/graphics/folder.gif"/>
+            <file type="content_page" path="www/simple-add-2.tcl"/>
+            <file type="content_page" path="www/simple-add.adp"/>
+            <file type="content_page" path="www/simple-add.tcl"/>
         </files>
         <parameters>
-        <!-- No version parameters -->
+            <parameter datatype="number"  min_n_values="1"  max_n_values="1"  name="MaximumFileSize"  default="4096000" description="maximum file size for upload"/>
         </parameters>
 
     </version>
Index: openacs-4/packages/attachments/sql/oracle/attachments-create.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/sql/oracle/attachments-create.sql,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/attachments/sql/oracle/attachments-create.sql	1 Jul 2002 20:16:23 -0000	1.1
+++ openacs-4/packages/attachments/sql/oracle/attachments-create.sql	2 Jul 2002 19:41:20 -0000	1.2
@@ -35,7 +35,8 @@
                                 references acs_objects(object_id)
                                 on delete cascade,
     item_id                     constraint attachments_item_id_fk
-                                references cr_items(item_id),
+                                references acs_objects(object_id)
+                                on delete cascade,
     constraint                  attachments_pk
                                 primary key (object_id, item_id)
 );
Index: openacs-4/packages/attachments/tcl/attachments-procs-oracle.xql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/tcl/Attic/attachments-procs-oracle.xql,v
diff -u
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ openacs-4/packages/attachments/tcl/attachments-procs-oracle.xql	2 Jul 2002 19:41:20 -0000	1.1
@@ -0,0 +1,12 @@
+<?xml version="1.0"?>
+<queryset>
+<rdbms><type>oracle</type><version>8.1.6</version></rdbms>
+
+<fullquery name="attachments::get_attachments.select_attachments">
+<querytext>
+select item_id, acs_object.name(item_id) from attachments
+where object_id= :object_id
+</querytext>
+</fullquery>
+ 
+</queryset>
Index: openacs-4/packages/attachments/tcl/attachments-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/tcl/attachments-procs.tcl,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/attachments/tcl/attachments-procs.tcl	1 Jul 2002 20:16:23 -0000	1.1
+++ openacs-4/packages/attachments/tcl/attachments-procs.tcl	2 Jul 2002 19:41:20 -0000	1.2
@@ -29,10 +29,16 @@
     }
 
     ad_proc -public get_root_folder {
-        {-package_id:required}
+        {-package_id ""}
     } {
     } {
-        return [db_1row get_root_folder_select {}]
+        if {[empty_string_p $package_id]} {
+            # Get the package ID from the parent URL
+            array set parent_node [site_node::get_parent -node_id [ad_conn node_id]]
+            set package_id $parent_node(object_id)
+        }
+
+        return [db_string get_root_folder_select {} -default {}]
     }
 
     ad_proc -public root_folder_map_p {
@@ -59,5 +65,87 @@
     } {
         db_dml unmap_root_folder_delete {}
     }
+
+    ad_proc -public attach {
+        {-object_id:required}
+        {-attachment_id:required}
+    } {
+        perform the attachment
+    } {
+        db_dml insert_attachment {}
+    }
+
+    ad_proc -public unattach {
+        {-object_id:required}
+        {-attachment_id:required}
+    } {
+        undo the attachment
+    } {
+        db_dml delete_attachment {}
+    }
+
+    ad_proc -public add_attachment_url {
+        {-package_id ""}
+        {-object_id:required}
+        {-return_url ""}
+        {-pretty_name ""}
+    } {
+        # FIXME: absolute URL here!!
+        return "attach/attach?pretty_object_name=[ns_urlencode $pretty_name]&object_id=$object_id&return_url=[ns_urlencode $return_url]"
+    }
+
+    ad_proc -public goto_attachment_url {
+        {-package_id ""}
+        {-object_id:required}
+        {-attachment_id:required}
+    } {
+        # FIXME: absolute URL!
+        return "attach/go-to-attachment?object_id=$object_id&attachment_id=$attachment_id"
+    }
+
+    ad_proc -public graphic_url {
+        {-package_id ""}
+    } {
+        # FIXME: absolute URL!
+        return "<img valign=bottom src=\"attach/graphics/file.gif\">"
+    }
+
+    ad_proc -public get_attachments {
+        {-object_id:required}
+    } {
+        returns a list of attachment ids and names
+    } {
+        set lst [db_list_of_lists select_attachments {}]
+        set lst_with_urls [list]
+
+        foreach el $lst {
+            set append_lst [list [goto_attachment_url -object_id $object_id -attachment_id [lindex $el 0]]]
+            lappend lst_with_urls [concat $el $append_lst]
+        }
+
+        return $lst_with_urls
+    }
+
+    ad_proc -public context_bar {
+        {-folder_id:required}
+        {-final ""}
+        {-extra_vars ""}
+    } {
+        set root_folder_id [attachments::get_root_folder]
+
+        set cbar_list [fs_context_bar_list -extra_vars $extra_vars -folder_url "attach" -file_url "attach" -root_folder_id $root_folder_id -final $final $folder_id]
+
+        set cbar_html "<a href=\"attach?${extra_vars}&folder_id=$root_folder_id\">Top</a> &gt; "
+        
+        foreach el $cbar_list {
+            if {[llength $el] < 2} {
+                append cbar_html "$el"
+            } else {
+                append cbar_html "<a href=\"[lindex $el 0]\">[lindex $el 1]</a> &gt; "
+            }
+        }
+
+        return $cbar_html
+    }
     
 }
Index: openacs-4/packages/attachments/tcl/attachments-procs.xql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/tcl/attachments-procs.xql,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/attachments/tcl/attachments-procs.xql	1 Jul 2002 20:16:23 -0000	1.1
+++ openacs-4/packages/attachments/tcl/attachments-procs.xql	2 Jul 2002 19:41:20 -0000	1.2
@@ -3,21 +3,21 @@
 
 <fullquery name="attachments::root_folder_p.root_folder_p_select">
 <querytext>
-select 1 from attachments_folder_map
+select 1 from attachments_fs_root_folder_map
 where package_id = :package_id
 </querytext>
 </fullquery>
 
 <fullquery name="attachments::get_root_folder.get_root_folder_select">
 <querytext>
-select folder_id from attachments_folder_map
+select folder_id from attachments_fs_root_folder_map
 where package_id = :package_id
 </querytext>
 </fullquery>
 
 <fullquery name="attachments::map_root_folder.map_root_folder_insert">
 <querytext>
-insert into attachments_folder_map 
+insert into attachments_fs_root_folder_map 
 (package_id, folder_id)
 values
 (:package_id, :folder_id)
@@ -26,10 +26,33 @@
 
 <fullquery name="attachments::unmap_root_folder.unmap_root_folder_delete">
 <querytext>
-delete from attachments_folder_map where
+delete from attachments_fs_root_folder_map where
 package_id = :package_id and
 folder_id = :folder_id
 </querytext>
 </fullquery>
+
+<fullquery name="attachments::attach.insert_attachment">
+<querytext>
+insert into attachments
+(object_id, item_id) values
+(:object_id, :attachment_id)
+</querytext>
+</fullquery>
+
+<fullquery name="attachments::unattach.delete_attachment">
+<querytext>
+delete from attachments
+where object_id = :object_id and
+item_id = :attachment_id
+</querytext>
+</fullquery>
+
+<fullquery name="attachments::get_attachments.select_attachments">
+<querytext>
+select item_id, acs_object.name(item_id) from attachments
+where object_id= :object_id
+</querytext>
+</fullquery>
  
 </queryset>
Index: openacs-4/packages/attachments/www/attach.adp
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/www/attach.adp,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/attachments/www/attach.adp	2 Jul 2002 04:18:54 -0000	1.1
+++ openacs-4/packages/attachments/www/attach.adp	2 Jul 2002 19:41:20 -0000	1.2
@@ -1,18 +1,20 @@
-<master>
+<master src="master">
+<property name="title">Attach A File to @pretty_object_name@</property>
+<property name="context_bar">@context_bar@</property>
+
 You are attaching a document to @pretty_object_name@.<p>
 
 Choose a location for your attachment. If your attachment already
-exists in the file storage folder, simply select it. Otherwise, you
+exists in the file storage folder, simply select it.<br>
+Otherwise, you
 may upload a new attachment.
 
 <p>
-@fs_context_bar@
+@fs_context_bar_html@
 <p>
-      <a href="file-add?folder_id=@folder_id@&@passthrough_vars@">Upload a new attachment
-      file</a>
+Attach a <b>new</b>:  &nbsp;&nbsp;     <a href="file-add?folder_id=@folder_id@&@passthrough_vars@">File</a>
       &nbsp;&nbsp;|&nbsp;&nbsp;
-      <a href="simple-add?folder_id=@folder_id@&@passthrough_vars@">Create a new URL
-      attachment</a>
+      <a href="simple-add?folder_id=@folder_id@&@passthrough_vars@">URL</a>
 <p>
 <if @contents:rowcount@ gt 0>
   <table width="85%" cellpadding="5" cellspacing="5">
Index: openacs-4/packages/attachments/www/attach.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/www/attach.tcl,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/attachments/www/attach.tcl	2 Jul 2002 04:18:54 -0000	1.1
+++ openacs-4/packages/attachments/www/attach.tcl	2 Jul 2002 19:41:20 -0000	1.2
@@ -25,8 +25,9 @@
 
 # Load up file storage information
 
+set root_folder_id [attachments::get_root_folder]
 if {[empty_string_p $folder_id]} {
-    set folder_id [attachments::get_root_folder]
+    set folder_id $root_folder_id
 }
 
 # Check permission
@@ -45,8 +46,14 @@
 
 template::util::list_of_ns_sets_to_multirow -rows $rows -var_name "contents"
 
-set fs_context_bar [fs_context_bar_list -root_folder_id [attachments::get_root_folder] -final "Attach" $folder_id]
-
 set passthrough_vars "object_id=$object_id&return_url=[ns_urlencode $return_url]&pretty_object_name=[ns_urlencode $pretty_object_name]"
 
+if {$folder_id == $root_folder_id} {
+    set fs_context_bar_html "Top"
+} else {
+    set fs_context_bar_html [attachments::context_bar -extra_vars $passthrough_vars -folder_id $folder_id]
+}
+
+set context_bar {Attach}
+
 ad_return_template
Index: openacs-4/packages/attachments/www/file-add.adp
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/www/file-add.adp,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/attachments/www/file-add.adp	2 Jul 2002 04:18:54 -0000	1.1
+++ openacs-4/packages/attachments/www/file-add.adp	2 Jul 2002 19:41:20 -0000	1.2
@@ -1,7 +1,9 @@
-<master>
-<property name="title">Upload New File</property>
+<master src="master">
+<property name="title">Upload New Attachment</property>
 <property name="context_bar">@context_bar@</property>
 
+You are attaching a document to @pretty_object_name@.<p>
+
 <form enctype=multipart/form-data method=POST action="file-add-2">
 <input type=hidden name="folder_id" value="@folder_id@">
 <input type=hidden name="object_id" value="@object_id@">
Index: openacs-4/packages/attachments/www/file-add.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/www/file-add.tcl,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/attachments/www/file-add.tcl	2 Jul 2002 04:18:54 -0000	1.1
+++ openacs-4/packages/attachments/www/file-add.tcl	2 Jul 2002 19:41:20 -0000	1.2
@@ -8,6 +8,7 @@
     folder_id:integer,notnull
     object_id:integer,notnull
     return_url:notnull
+    pretty_object_name:notnull
     {title ""}
     {lock_title_p 0}
 } -validate {
@@ -29,7 +30,8 @@
 
 # set templating datasources
 
-set context_bar [fs_context_bar_list -final "Add File" $folder_id]
+set context_bar {{Attach File}}
+set fs_context_bar [fs_context_bar_list -final "Add File" $folder_id]
 
 # Should probably generate the item_id and version_id now for
 # double-click protection
Index: openacs-4/packages/attachments/www/go-to-attachment.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/www/go-to-attachment.tcl,v
diff -u
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ openacs-4/packages/attachments/www/go-to-attachment.tcl	2 Jul 2002 19:41:20 -0000	1.1
@@ -0,0 +1,38 @@
+
+ad_page_contract {
+    Go to an attachment
+    
+    @author Ben Adida (ben@openforce)
+} {
+    {object_id:integer,notnull}
+    {attachment_id:integer,notnull}
+}
+
+# We check permissions on the object
+permission::require_permission -object_id $object_id -privilege read
+
+# Get information about attachment
+if {![db_0or1row select_attachment_data {}]} {
+    ad_return_complaint "No such attachment for this object"
+    return
+}
+
+switch $object_type {
+    fs_url {
+        set url [db_string select_url {}]
+        ad_returnredirect $url
+        ad_script_abort
+        return
+    }
+
+    content_item {
+        ad_returnredirect "download/$object_id?object_id=$object_id&attachment_id=$attachment_id"
+        ad_script_abort
+        return
+    }
+    
+    default {
+        ad_return_complaint "don't know how to deal with this attachment type"
+        return
+    }
+}
Index: openacs-4/packages/attachments/www/go-to-attachment.xql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/www/go-to-attachment.xql,v
diff -u
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ openacs-4/packages/attachments/www/go-to-attachment.xql	2 Jul 2002 19:41:20 -0000	1.1
@@ -0,0 +1,27 @@
+<?xml version="1.0"?>
+<queryset>
+
+<fullquery name="select_attachment_data">      
+<querytext>
+select 
+object_type
+from attachments, acs_objects
+where
+attachments.object_id = :object_id and
+attachments.item_id = :attachment_id and
+attachments.item_id = acs_objects.object_id
+</querytext>
+</fullquery>
+
+<fullquery name="select_url">      
+<querytext>
+select 
+url
+from fs_urls
+where
+url_id = :attachment_id
+</querytext>
+</fullquery>
+
+ 
+</queryset>
Index: openacs-4/packages/attachments/www/master.adp
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/www/Attic/master.adp,v
diff -u
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ openacs-4/packages/attachments/www/master.adp	2 Jul 2002 19:41:20 -0000	1.1
@@ -0,0 +1,10 @@
+<master>
+<property name="title">@title@</property>
+
+<h2>@title@</h2>
+
+@context_bar@
+
+<hr noshade>
+
+<slave>
Index: openacs-4/packages/attachments/www/master.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/www/Attic/master.tcl,v
diff -u
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ openacs-4/packages/attachments/www/master.tcl	2 Jul 2002 19:41:20 -0000	1.1
@@ -0,0 +1 @@
+set context_bar [eval ad_context_bar $context_bar]
Index: openacs-4/packages/attachments/www/simple-add.adp
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/www/simple-add.adp,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/attachments/www/simple-add.adp	2 Jul 2002 04:18:54 -0000	1.1
+++ openacs-4/packages/attachments/www/simple-add.adp	2 Jul 2002 19:41:20 -0000	1.2
@@ -1,7 +1,9 @@
-<master>
-<property name="title">Create @pretty_name@</property>
+<master src="master">
+<property name="title">Attach URL</property>
 <property name="context_bar">@context_bar@</property>
 
+You are attaching a document to @pretty_object_name@.<p>
+
 <form method=POST action="simple-add-2">
 <input type=hidden name="folder_id" value="@folder_id@">
 <input type=hidden name="type" value="@type@">
Index: openacs-4/packages/attachments/www/simple-add.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/www/simple-add.tcl,v
diff -u -r1.1 -r1.2
--- openacs-4/packages/attachments/www/simple-add.tcl	2 Jul 2002 04:18:54 -0000	1.1
+++ openacs-4/packages/attachments/www/simple-add.tcl	2 Jul 2002 19:41:20 -0000	1.2
@@ -9,6 +9,7 @@
     folder_id:integer,notnull
     object_id:integer,notnull
     return_url:notnull
+    pretty_object_name:notnull
     {type "fs_url"}
     {title ""}
     {lock_title_p 0}
@@ -34,7 +35,8 @@
     return -code error "No such type"
 }
 
-set context_bar [fs_context_bar_list -final "Add $pretty_name" $folder_id]
+set context_bar {{Attach URL}}
+set fs_context_bar [fs_context_bar_list -final "Add $pretty_name" $folder_id]
 
 # Should probably generate the item_id and version_id now for
 # double-click protection
Index: openacs-4/packages/attachments/www/download/index.vuh
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/www/download/index.vuh,v
diff -u
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ openacs-4/packages/attachments/www/download/index.vuh	2 Jul 2002 19:41:20 -0000	1.1
@@ -0,0 +1,28 @@
+# packages/attachments/www/download/index.vuh
+
+ad_page_contract {
+
+    Virtual URL handler for file downloads
+
+    @author Ben Adida
+    @creation-date 2 July 2002
+    @cvs-id $Id: index.vuh,v 1.1 2002/07/02 19:41:20 ben Exp $
+} {
+    object_id:integer,notnull
+    attachment_id:integer,notnull
+}
+
+# Permission check
+permission::require_permission -object_id $object_id -privilege read
+
+# Get the live version 
+# (and check that this is the right attachment while we're at it)
+set version_id [db_string select_version_id {} -default {}]
+
+if {[empty_string_p $version_id]} {
+    ad_return_complaint "bad attachment"
+    ad_script_abort
+    return
+}
+
+cr_write_content -revision_id $version_id
Index: openacs-4/packages/attachments/www/download/index.xql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/www/download/index.xql,v
diff -u
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ openacs-4/packages/attachments/www/download/index.xql	2 Jul 2002 19:41:20 -0000	1.1
@@ -0,0 +1,14 @@
+<?xml version="1.0"?>
+<queryset>
+
+<fullquery name="select_version_id">      
+<querytext>
+select live_revision
+from cr_items, attachments
+where cr_items.item_id = attachments.item_id
+and attachments.object_id = :object_id
+</querytext>
+</fullquery>
+
+ 
+</queryset>
Index: openacs-4/packages/attachments/www/graphics/file.gif
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/www/graphics/file.gif,v
diff -u
Binary files differ
Index: openacs-4/packages/attachments/www/graphics/folder.gif
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/attachments/www/graphics/folder.gif,v
diff -u
Binary files differ