Assign record to other user

9 posts by 3 authors in: Forums > CMS Builder
Last Post: June 14, 2022   (RSS)

By pgplast - June 6, 2022

This can be done with a simple plugin.

First you include a user list in your section (e.g. 'new_owner') that pulls the nums and names of all your users from your accounts.

When the person creates a new section record, he selects from this list the person to whom he wishes the ownership re-assigned.

On save of the record, a 'post_save' plugin then captures the record number of the section record and the 'new_owner' value and updates the value of createByUseNum  (only if a value for 'new_owner' has been saved).

By andreasml - June 7, 2022

Hi

Thank you for your reply. 

On save of the record, a 'post_save' plugin then captures the record number of the section record and the 'new_owner' value and updates the value of createByUseNum  (only if a value for 'new_owner' has been saved).

How can I create this? Could you please give an example?

Regards, 

Andreas

By pgplast - June 8, 2022

Hi:

Create a field of list type in your section called (e.g.), ‘new_owner.’

Under ‘list’ type select ‘get options from MYSQL query’ from the list options

Put into the code field

SELECT num,CONCAT_WS(", ",last_name,first_name)
FROM `<?php echo $TABLE_PREFIX ?>accounts`
ORDER BY last_name

——

Create plugin called 'new_record_owner.php,'  a la:

<?php
/*
Plugin Name: Reassign record ownership
Description: Plugin file: new_record_owner.php, table: 'your_section_name'
Version: #####
Requires at least: 3.0
*/

addAction('record_postsave', 'new_record_owner', null, 2);

//
function new_record_owner($tableName,$recordNum) {
global $TABLE_PREFIX,$CURRENT_USER, $SETTINGS;

if($tableName!="new_owner") {
return;
}

if (@$_REQUEST['new_owner']) {

$new_owner_num = $_REQUEST['new_owner'];
$record_num = $_REQUEST['num'];

require_once("lib/viewer_functions.php");

mysqlStrictMode(false);


$query = "UPDATE 'your_section_name' SET

createdByUserNum = '".$new_owner_num."',
updatedByUserNum = '".$new_owner_num."',

WHERE num = '".$record_num."'";
mysqli()->query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysqli()->error) . "\n");


}// end if

}// end function
?>

upload and activate the plugin.

Create a dummy record in your section and select a new owner from the 'new_owner" list field and then check back to see if it now belongs to your newly selected owner.

Note that you have to put your section name in the appropriate areas above.

By andreasml - June 8, 2022

Hi

Many thanks for you kind answer. 

Unfortunately, I did not make it work.

Although, i insert the name of the owner (from the drop-down menu list), after saving the record, the owner and the updater of the record remain unchanged (there are still the original owners). I will have another look when I find some time and let you know for sure. Just a small clarification: if I need to have the same property to more than one sections what should I do? Just add the new section's name or something else? (see the following exam)

....

$query = "UPDATE infected_grafts, aortic_aneurysm_disease, carotid_disease SET

.....

Kind regards, 

Andreas

By pgplast - June 9, 2022

Yes, sorry.

First, add a checkbox field to your section called 'assign_new_owner.'

(Leave the default as 0 (false).)

Before saving a record, check the 'assign_new_owner.' checkbox and select a new owner from the list.

When the record is saved, the ownership will be changed and the 'assign_new_owner.' and 'new_owner' valses will be resret so another save of the record will have no effect.

Use the plugin code below

This works fine on my server.

<?php
/*
Plugin Name: Reassign record ownership
Description: Plugin file: new_record_owner.php, table: 'test_update_owner'
Version: #####
Requires at least: 3.0
*/

addAction('record_postsave', 'new_record_owner', null, 2);

//
function new_record_owner($tableName,$recordNum) {
global $TABLE_PREFIX,$CURRENT_USER, $SETTINGS;

if($tableName!="test_update_owner") {
return;
}

if (@$_REQUEST['assign_new_owner'] && @$_REQUEST['new_owner']) {

$new_owner_num = $_REQUEST['new_owner'];
$record_num = $_REQUEST['num'];

require_once("lib/viewer_functions.php");

mysqlStrictMode(false);

$query = "UPDATE cmsb_test_update_owner SET

assign_new_owner = '0',
new_owner = NULL,
updatedDate = NOW(),
createdByUserNum = '".$new_owner_num."',
updatedByUserNum = '".$new_owner_num."'
WHERE num = '".mysql_escape( $record_num )."'";
mysqli()->query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysqli()->error) . "\n");

}// end if

}// end function
?>

By andreasml - June 9, 2022

Hi 

Thanks again!! It seems to work for me also (with some very mild modifications). I will let you know in more detail as long as I try it a bit more.

Kind regards,

Andreas

By andreasml - June 10, 2022

Hi

It does work!! 

I wonder if there is a way to apply this to more than one sections, or you need to create a different plugin for every different section. 

Again, thanks for your assistance, 

Kind regards, 

Andreas

By daniel - June 14, 2022

Hey Andreas,

Glad you were able to get this sorted out!

In regards to getting it working for different sections, the key is the "$tableName" variable and this portion of the code:

if($tableName!="test_update_owner") {
return;
}

As it stands, this checks the current section and halts if it's not "test_update_owner" - it could be modified to permit multiple sections:

if($tableName!="test_update_owner" && $tableName != "other_permitted_table") {
return;
}

It could also be updated to take different actions depending on the section with some moderate modifications:

if($tableName == "test_update_owner") {

 // run code here

} elseif ($tableName == "other_permitted_table") {

 // run alternate code here

}

Hope that helps!

Daniel
Technical Lead
interactivetools.com