Index: openacs-4/packages/acs-content-repository/sql/postgresql/content-item.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-content-repository/sql/postgresql/content-item.sql,v
diff -u -r1.39.2.4 -r1.39.2.5
--- openacs-4/packages/acs-content-repository/sql/postgresql/content-item.sql	20 Mar 2003 22:11:07 -0000	1.39.2.4
+++ openacs-4/packages/acs-content-repository/sql/postgresql/content-item.sql	7 Nov 2003 02:05:36 -0000	1.39.2.5
@@ -1565,7 +1565,6 @@
   return 0; 
 end;' language 'plpgsql';
 
-
 create function content_item__set_release_period (integer, timestamptz, timestamptz)
 returns integer as '
 declare
@@ -1648,16 +1647,31 @@
 --   to the target folder
 -- 3) update the parent_id for the item
 
-create function content_item__move (integer,integer)
+create or replace function content_item__move (integer,integer)
 returns integer as '
 declare
   move__item_id                alias for $1;  
-  move__target_folder_id       alias for $2;  
+  move__target_folder_id       alias for $2;
 begin
+  perform content_item__move(
+	move__item_id,
+	move__targer_folder_id,
+	move__name
+	);
+return null;
+end;' language 'plpgsql';
 
+create or replace function content_item__move (integer,integer,varchar)
+returns integer as '
+declare
+  move__item_id                alias for $1;  
+  move__target_folder_id       alias for $2;
+  move__name                   alias for $3;
+begin
+
   if content_folder__is_folder(move__item_id) = ''t'' then
 
-    PERFORM content_folder__move(move__item_id, move__target_folder_id);
+    PERFORM content_folder__move(move__item_id, move__target_folder_id,move__name);
 
   else if content_folder__is_folder(move__target_folder_id) = ''t'' then
    
@@ -1667,10 +1681,14 @@
        content_folder__is_registered(move__target_folder_id,
           content_item__get_content_type(content_symlink__resolve(move__item_id)),''f'') = ''t''
       then
-
     -- update the parent_id for the item
+    if move__name = '''' then
+	move__name := NULL;
+    end if;
+
     update cr_items 
-      set parent_id = move__target_folder_id
+      set parent_id = move__target_folder_id,
+          name = coalesce(move__name, name)
       where item_id = move__item_id;
     end if;
 
@@ -1680,7 +1698,7 @@
 end;' language 'plpgsql';
 
 
-create function content_item__copy (integer,integer,integer,varchar)
+create or replace function content_item__copy (integer,integer,integer,varchar)
 returns integer as '
 declare
   item_id                alias for $1;  
@@ -1704,7 +1722,7 @@
 -- 3) create a new item with no revisions in the target folder
 -- 4) copy the latest revision from the original item to the new item (if any)
 
-create function content_item__copy2 (integer,integer,integer,varchar)
+create or replace function content_item__copy2 (integer,integer,integer,varchar)
 returns integer as '
 declare
   copy2__item_id                alias for $1;  
@@ -1724,32 +1742,72 @@
   v_storage_type                cr_items.storage_type%TYPE;
 begin
 
+	perform content_item__copy (
+		copy2__item_id,
+		copy2__target_folder_id,
+		copy2__creation_user,
+		copy2__creation_ip,
+		''''
+		);
+	return copy2__item_id;
+
+end;' language 'plpgsql';
+
+create or replace function content_item__copy (
+	integer,
+	integer,
+	integer,
+	varchar,
+	varchar
+) returns integer as '
+declare
+  copy__item_id                alias for $1;  
+  copy__target_folder_id       alias for $2;  
+  copy__creation_user          alias for $3;  
+  copy__creation_ip            alias for $4;  -- default null  
+  copy__name                   alias for $5; -- default null
+  v_current_folder_id           cr_folders.folder_id%TYPE;
+  v_num_revisions               integer;       
+  v_name                        cr_items.name%TYPE;
+  v_content_type                cr_items.content_type%TYPE;
+  v_locale                      cr_items.locale%TYPE;
+  v_item_id                     cr_items.item_id%TYPE;
+  v_revision_id                 cr_revisions.revision_id%TYPE;
+  v_is_registered               boolean;       
+  v_old_revision_id             cr_revisions.revision_id%TYPE;
+  v_new_revision_id             cr_revisions.revision_id%TYPE;
+  v_storage_type                cr_items.storage_type%TYPE;
+begin
+
   -- call content_folder.copy if the item is a folder
-  if content_folder__is_folder(copy2__item_id) = ''t'' then
+  if content_folder__is_folder(copy__item_id) = ''t'' then
     PERFORM content_folder__copy(
-        copy2__item_id,
-        copy2__target_folder_id,
-        copy2__creation_user,
-        copy2__creation_ip
-    );
+        copy__item_id,
+        copy__target_folder_id,
+        copy__creation_user,
+        copy__creation_ip,
+	copy__name
+    ); 
   -- call content_symlink.copy if the item is a symlink
-  else if content_symlink__is_symlink(copy2__item_id) = ''t'' then
+  else if content_symlink__is_symlink(copy__item_id) = ''t'' then
     PERFORM content_symlink__copy(
-        copy2__item_id,
-        copy2__target_folder_id,
-        copy2__creation_user,
-        copy2__creation_ip
+        copy__item_id,
+        copy__target_folder_id,
+        copy__creation_user,
+        copy__creation_ip,
+	copy__name
     );
   -- call content_extlink.copy if the item is an url
-  else if content_extlink__is_extlink(copy2__item_id) = ''t'' then
+  else if content_extlink__is_extlink(copy__item_id) = ''t'' then
     PERFORM content_extlink__copy(
-        copy2__item_id,
-        copy2__target_folder_id,
-        copy2__creation_user,
-        copy2__creation_ip
+        copy__item_id,
+        copy__target_folder_id,
+        copy__creation_user,
+        copy__creation_ip,
+	copy__name
     );
   -- make sure the target folder is really a folder
-  else if content_folder__is_folder(copy2__target_folder_id) = ''t'' then
+  else if content_folder__is_folder(copy__target_folder_id) = ''t'' then
 
     select
       parent_id
@@ -1758,39 +1816,40 @@
     from
       cr_items
     where
-      item_id = copy2__item_id;
+      item_id = copy__item_id;
 
-    -- can''t copy to the same folder
-    if copy2__target_folder_id != v_current_folder_id then
+    select
+      content_type, name, locale,
+      coalesce(live_revision, latest_revision), storage_type
+    into
+      v_content_type, v_name, v_locale, v_revision_id, v_storage_type
+    from
+      cr_items
+    where
+      item_id = copy__item_id;
 
-      select
-        content_type, name, locale,
-        coalesce(live_revision, latest_revision), storage_type
-      into
-        v_content_type, v_name, v_locale, v_revision_id, v_storage_type
-      from
-        cr_items
-      where
-        item_id = copy2__item_id;
+-- copy to a different folder, or allow copy to the same folder
+-- with a different name
 
+    if copy__target_folder_id != v_current_folder_id  or ( v_name != copy__name and copy__name is not null ) then
       -- make sure the content type of the item is registered to the folder
       v_is_registered := content_folder__is_registered(
-          copy2__target_folder_id,
+          copy__target_folder_id,
           v_content_type,
           ''f''
       );
 
       if v_is_registered = ''t'' then
         -- create the new content item
         v_item_id := content_item__new(
-            v_name,
-            copy2__target_folder_id,
+            coalesce (copy__name, v_name),
+            copy__target_folder_id,
             null,
             v_locale,
             now(),
-            copy2__creation_user,
+            copy__creation_user,
             null,
-            copy2__creation_ip,
+            copy__creation_ip,
             ''content_item'',            
             v_content_type,
             null,
@@ -1801,27 +1860,25 @@
             v_storage_type
         );
 
-        -- get the latest revision of the old item
-        select
+	select
           latest_revision into v_old_revision_id
         from
-          cr_items
+       	  cr_items
         where
-          item_id = copy2__item_id;
+       	  item_id = copy__item_id;
+	end if;
 
         -- copy the latest revision (if any) to the new item
-        if v_old_revision_id is not null then
+	if v_old_revision_id is not null then
           v_new_revision_id := content_revision__copy (
               v_old_revision_id,
               null,
               v_item_id,
-              copy2__creation_user,
-              copy2__creation_ip
+              copy__creation_user,
+              copy__creation_ip
           );
         end if;
-      end if;
 
-
     end if;
   end if; end if; end if; end if;
 
