Gossamer Forum
Home : Products : DBMan : Customization :

small mod: add duplicate record

Quote Reply
small mod: add duplicate record
I don't know whether this has been done before, but here it is anyway -
How to add a duplicate record

This small mod allows you to add a duplicate record upon search output.
I have found this useful in handling a bibliography database, where I often have to add
several articles contained in one book. Using this function, I don't have to enter
those fields which remain the same for all articles again and again.

Presupposition: Your database must contain a unique numeric identifier field which counts
the records and serves as the database key. In this mod, this field has the name "ID".

The mod does the following things:
- next to each record in the search output (obtained via searches or "list all"),
it prints out an "add duplicate" button
- upon pressing the button, dbman creates the add form and fills in the data from the record
you want to duplicate
- in the process, dbman calculates the total number of all records in the database and
automatically moves the record ID one up.

You will have to modify the html-code that does the formatting according to your setup.

MODIFICATIONS:

a) changes to html.pl:

1) in sub html_view: [NOTE: This is the search form; it may have a different name in
your html.pl]

add "<input type=hidden name="button" value="$db_key">" to the hidden input fields in the
beginning of the sub.

2) in sub html_record, add the following code:

if ($in{'button'}) {
print qq|
<form action="$db_script_url" METHOD="POST" name="form1">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<input type=hidden name="dbkey" value="$rec{$db_key}">|;
}

print qq|here goes your code for the search results|;

if ($in{'button'} && $per_add) {
print qq|<input type="SUBMIT" name="add_duplicate" value="Add duplicate">|;
}
print qq|</form>|;

3) ad the following sub:

sub html_add_duplicate {
&html_page_top; # this prints out html headers,
# replace it by whatever code you use
$page_title = "$html_title: Add duplicate"; # this goes together with &html_page_top
print qq|<form action="$db_script_url" method="GET">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<td>$html_title: Add duplicate</td> # modify formatting code as necessary
</tr><tr>
<td>Add duplicate</td>
</tr><tr>
<td>
|;

$in{'dbkey'} =~ s/<.?B>//g;
my (%rec) = &get_record($in{'dbkey'});
html_record_form(%rec);
print qq|</td>
</tr><tr>
<td><center>
<INPUT TYPE="SUBMIT" NAME="preview_record" VALUE="Preview Record"> # if you don't have the
# "preview"-mod installed,
# use "add_record" instead
<INPUT TYPE="RESET" VALUE="Reset Form"></center></td></tr><tr>
<td>
|;
&html_page_bottom; # this prints out the footer; replace by whatever code you use
}

4) in sub html_record_form:

First, add the following code at the top:

my $dupl;
my $key_value;
open (ID, "<$db_id_file_name") or &cgierr("error. unable to open id file: $db_id_file_name.\nReason: $!");
if ($db_use_flock) { flock(ID, 1); } $key_value = <ID>;close ID;
$dupl = $key_value +1;

Next, assuming that you have an ID field which is printed in the form using code like
"<input type="text" name="ID" size="2" value="$rec{'ID'}" maxlength="10">",
you should replace this field code by the following:

if ($in{'add_duplicate'}) {
print qq|<input type="text" name="ID" size="2" value="$dupl" maxlength="10">|;
} ### if the form is called from the "add duplicate" button, the ID is moved up
else {
print qq| <input type="text" name="ID" size="2" value="$rec{'ID'}" maxlength="10">|;
} ### if the form is accessed from a different sub, we don't need to fiddle with the
### ID counter

b) in db.cgi:

scroll down to the list with the many elsif-conditions and add the following:

"elsif ($in{'add_duplicate'}) { if ($per_add) { &html_add_duplicate; } else { &html_unauth; } }"




kellner
Quote Reply
Re: small mod: add duplicate record In reply to
oops, I just realized that the routine relied upon for moving the ID counter up doesn't really do that - it counts the number of records in the database, which, if you've already deleted records in between, is NOT the same as the ID number! I'll try to figure out how to get the highest ID number and then correct the code - .... code is corrected now and works (apparently) fine.

kellner
Quote Reply
Re: small mod: add duplicate record In reply to
Kellner:

It's always good to have alternative ways of doing things, thanks for your mod. Just wanted to mention that there are a few other variations of making a copy of a record in the FAQ noted below under the section "Files / Records".

I'll add your version to the selections Smile

Unoffical DBMan FAQ
http://webmagic.hypermart.net/dbman/
Quote Reply
Re: small mod: add duplicate record In reply to
Thanks LoisC, I had actually looked at the unofficial FAQ (I always do before I sit down and start coding myself!), but somehow the "COPY" mod must have escaped my attention.

kellner
Quote Reply
Re: [kellner] small mod: add duplicate record In reply to
It is not working and it should be great if it works! I think the problem is here:



In Reply To:

Next, assuming that you have an ID field which is printed in the form using code like
"<input type="text" name="ID" size="2" value="$rec{'ID'}" maxlength="10">",
you should replace this field code by the following:

if ($in{'add_duplicate'}) {
print qq|<input type="text" name="ID" size="2" value="$dupl" maxlength="10">|;
} ### if the form is called from the "add duplicate" button, the ID is moved up
else {
print qq| <input type="text" name="ID" size="2" value="$rec{'ID'}" maxlength="10">|;
} ### if the form is accessed from a different sub, we don't need to fiddle with the
### ID counter

__________________________ the debugger sais there is a problem near "text" name ... but I don't know what the problem is. thanks Luk