[SOLVED] CB Profile Update Logger in external CB API Add/Edit script

7 years 2 months ago - 7 years 1 month ago #291633 by sysopcrew
Is there a way to trigger the profile update logger when using an external PHP script to enter/update users? I found some CB API code that lets me $user->store(), but the updates are not logged with this plugin.

This is the general code I found elsewhere in the Joomlapolis code for the CB API, but I don't know how to get the update logger to trigger. Do I create an Auto Action? Manually enter data into the update logger table?
function editUser( $user, $update ) {
	global $_CB_framework, $_PLUGINS, $ueConfig;
	$oldUserComplete		=	new \CB\Database\Table\UserTable();

...snip...

	$_PLUGINS->trigger( 'onBeforeUserUpdate', array( &$user, &$user, &$oldUserComplete, &$oldUserComplete ) );
	if ( ! $user->store() ) {
		return false;
	}
	
	$_PLUGINS->trigger( 'onAfterUserUpdate', array( &$user, &$user, $oldUserComplete ) );
	
	return true;
}

--
David Riviera

Please Log in to join the conversation.

7 years 2 months ago #291663 by krileon
You can't use Joomla or CB API in an external script unless it's pathed through Joomla (e.g. ajax endpoint of a component or directly executing a code action in CB Auto Actions). If you need to modify field values externally I recommend using CB Auto Actions and a Field action then setting Triggers to None. You can then use the URL under the global tab to execute the action directly. You can have it execute the action on a specific user by setting &users=USER_ID_HERE as well.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

7 years 2 months ago #291668 by sysopcrew
We train over 650 volunteers each summer, and instructors need a very quick way to update records. I made a form that only has the fields they need in a very sleek interface.

I also use JFactory::getDbo();

These functions I found on Joomlapolis:
function registerUser( $newuser ) {
	global $_CB_framework, $_PLUGINS, $ueConfig;
	$user	=	new \CB\Database\Table\UserTable();
	foreach($newuser as $key=>$value) {
		$user->set( $key, $value);
	}
	$user->store();
}

function editUser( $user, $update ) {
	global $_CB_framework, $_PLUGINS, $ueConfig;
	$oldUserComplete		=	new \CB\Database\Table\UserTable();
	foreach($update as $key=>$value) {
		$user->set( $key, $value);
	}
	$user->store();
}

I load JFactory:
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', '/home/metro/public_html' );
require_once( JPATH_BASE . DS . 'configuration.php');
global $_CB_framework, $mainframe;
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );

/*****************************************************************
Create the Application */

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
jimport('joomla.plugin.helper');
$user = JFactory::getUser();

/*****************************************************************
Load Community Builder */
include_once( JPATH_BASE . DS . 'administrator/components/com_comprofiler/plugin.foundation.php' );
cbimport( 'cb.database' );
$myId = $_CB_framework->myId();
$user =& CBuser::getUserDataInstance( $myId );

This also lets me use the native Joomla login function for ACL to this utility. It's the $user->store() that I thought would trigger the Update Logger, but now I see how that would not happen—it's the native JFactory user object, not a CBuser.

Does that help explain my situation better?

--
David Riviera

Please Log in to join the conversation.

7 years 2 months ago - 7 years 2 months ago #291673 by krileon
As you already have the CB user object then you'll be able to change its values, store them, and fire the triggers in a single usage using storeDatabaseValues or storeDatabaseValue. Examples as follows.

Multiple Values:
$user->storeDatabaseValues( array( 'FIELD_NAME_1' => 'VALUE', 'FIELD_NAME_2' => 'VALUE' ) );

Single Value

$user->storeDatabaseValue( 'FIELD_NAME', 'VALUE' );


Neither of these will actually load plugins for those plugins to act on the triggers though so you need to run the below before doing this.

global $_PLUGINS;

$_PLUGINS->loadPluginGroup( 'user' );

That will ensure plugins are loaded before the trigger executes allowing plugins to act on it. I've no idea how all this will behave externally like that though so it may not even work.

As for a simplified form you can do that with RSForms or CB Auto Actions entirely without needing so much custom code. CB Auto Actions would need 2 actions. 1 code action for rendering the form and 1 fields action (doesn't fire triggers though) for processing the form.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
The following user(s) said Thank You: sysopcrew

Please Log in to join the conversation.

7 years 2 months ago #291676 by sysopcrew
I realize this is very nontraditional use. It ties in with a much broader system that does not involve Joomla content or functions at all, but that outside system uses Joomla ACL. I am open to even keeping my own update history outside of Update Logger. I'll try $_PLUGINS->loadPluginGroup( 'user' );

I'm already including the global $_PLUGINS, but it wasn't doing loadPluginGroup. If that doesn't work, I'm not going to worry about it.

I've been programming so long (HTML 1.0), I haven't caught up to learning AJAX yet. I really need to; sounds like that would solve a lot of problems.

--
David Riviera

Please Log in to join the conversation.

7 years 1 month ago #291840 by sysopcrew
$_PLUGINS->loadPluginGroup( 'user' );

was all I needed to add. Thanks!

--
David Riviera
The following user(s) said Thank You: krileon

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.184 seconds

Facebook Twitter LinkedIn