Copying a record into existing DB Table

D

Debbie S

Guest
Hello everyone!

The question about copying a record has been asked a few times and I have finally managed to get the code to accomplish this finished. The code changes are added primarily to business_logic.php and index.php -- optionally the config.php and language files should you wish to implement icon/text variables like the edit, delete and details buttons on the results view. Here we go (portions to copy are noted BETWEEN _startcopy_ and _endcopy_ lines .. additions are noted in bold) ...

***** business_logic.php *****
-- 1 --
Locate the function delete_record and insert either above or below the following:

_startcopy_
function copy_record ($table_name, $where_field, $where_value)
// goal: delete one record
{
global $conn, $quote;//INSERT INTO table_b SELECT * FROM table_a WHERE
$sql = "CREATE TEMPORARY TABLE ".$quote.$table_name."_".$where_value.$quote;
$sql .=" SELECT * FROM ".$quote.$table_name.$quote." WHERE ".$quote.$where_field.$quote." = '".$where_value."' LIMIT 1";
display_sql($sql);// INSERT INTO ".$quote.$table_name."_".$where_value.$quote."

// execute the select query
$res_contacts = execute_db($sql, $conn);

global $conn, $quote;//INSERT INTO table_b SELECT * FROM table_a WHERE
$sql1 = " UPDATE ".$quote.$table_name."_".$where_value.$quote." SET ".$quote.$where_field.$quote." = 'NULL'";
display_sql($sql1);

// execute the select query
$res_contacts = execute_db($sql1, $conn);

global $conn, $quote;//INSERT INTO table_b SELECT * FROM table_a WHERE
$sql2 = " INSERT INTO ".$quote.$table_name.$quote." SELECT * FROM ".$quote.$table_name."_".$where_value.$quote;
display_sql($sql2);

// execute the select query
$res_contacts = execute_db($sql2, $conn);

global $conn, $quote;//INSERT INTO table_b SELECT * FROM table_a WHERE
$sql3 = " DROP TABLE ".$quote.$table_name."_".$where_value.$quote;

display_sql($sql3);

// execute the select query
$res_contacts = execute_db($sql3, $conn);

} // end function copy_record
_endcopy_

-- 2 --
Locate the line 'if ($enable_edit == "1"){ // display the edit icon' inside the function build_results_table and insert after the //end if statement:

_startcopy_
if ($enable_insert == "1"){ // display the copy icon
$results_table .= "<a class=\"onlyscreen\"";
$results_table .= " href=\"".$dadabik_main_file."?table_name=".urlencode($table_name)."&function=copy";

if($results_type == "search") {
$results_table .= "&where_clause=".urlencode($where_clause)."&page=".$page."&order=".urlencode($order)."&order_type=".$order_type;
}

$results_table .= "&where_field=".urlencode($where_field)."&where_value=".urlencode($where_value)."\">Copy</a>";
} // end if
_endcopy_

-- 3 --
Add the bold text to the 'global' line of the function build_results_table:

global $submit_buttons_ar, $normal_messages_ar, $edit_target_window, $delete_icon, $edit_icon, $details_icon, $enable_edit, $enable_insert, $enable_delete, $enable_details, $conn, $quote, $ask_confirmation_delete, $word_wrap_col, $word_wrap_fix_width, $alias_prefix, $dadabik_main_file;

Again, if you wish to use an icon and specify it in you config.php as a variable, you will need to also add $copy_icon (if you named it as such in config.php) to the global line.


***** index.php *****
-- 1 --
Here we need to add the copy function as an option so DaDaBIK knows what it's supposedc to do with the function created above.
Locate case "delete": and insert after that case's break; line:

_startcopy_
case "copy":
if ($enable_insert == "1") {

$location_url = $site_url.$dadabik_main_file.'?table_name='.urlencode($table_name).'&function=search&where_clause='.urlencode($where_clause);
if(isset($page) && isset($order) && isset($order_type)) {
$location_url .= '&page='.$page.'&order='.urlencode($order).'&order_type='.$order_type;
}
else{
$location_url .= '&page=0';
}


if( $enable_authentication === 0 || $enable_insert_authorization === 0 || current_user_is_owner($where_field, $where_value, $table_name, $fields_labels_ar)){
copy_record ($table_name, $where_field, $where_value);
} // end if
else {
$location_url .= '&just_delete_no_authorization=1';
} // end else

header('Location: '.$location_url);
} // end if
break;
_endcopy_


I have this working on my test installation along with the Approval (move record) as outlined in this post >>> http://www.dadabik.org/forum/read.php?f=1&i=4126&t=4126 and all works great.

In addition, the copy function can be added to the details page (like the 'edit this record' can be added as described in this post >>> http://www.dadabik.org/forum/read.php?f=1&i=3078&t=2801 -- line numbers may not match so search for the details of record line in index.php). The code to copy when viewing a detail record is:

_startcopy_
if ($enable_insert == "1"){ // display the copy icon
txt_out("<a href=\"".$dadabik_main_file."?table_name=".urlencode($table_name)."&function=copy&where_field=".urlencode($where_field)."&where_value=".urlencode($where_value)."\">[Copy]</a>");
} // end if
_endcopy_

The only thing to mention is after selecting the copy in the details view, the view reverts back to the results view. I have not figured out how to get the display to automatically change to the new record if selected in the details view, so this may not be the most desirable loction for the copy function. When selecting copy from the results view, you are returned to the same page of results so you can see your dup record and select it for editing.


Hope this is useful to someone!
Debbie
(Latest version of DaDaBIK when this message was posted: 3.2 Beta)
 

rjrsinc

New member
Hi Debbie. I came across this post and new to dadabik. This code was created for ver 3.2 and I'm using 5.0. I'm going to go through it and see if it will work on 5.0, but thought you may have already tried it. Could you let me know if you have? Thanks and you have some great posts.
 

ChrisG

New member
Debbie,
Do you have this working on the latest DaDaBIK version 5.1.2 ENTERPRISE?

I am not sure this is what I need either.

I am looking to add code so that when I add a record through the Insert/Create Record Form the Insert/Create Record Form will remain populated with the previously entered data. I can then make minor changes for repetitive entries and continue submitting.

Thanks,
Chris
 
Top