CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • CiviCRM to Joomla user synch -- e-mail address changes
Pages: [1]

Author Topic: CiviCRM to Joomla user synch -- e-mail address changes  (Read 2244 times)

dadinfishers

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Joomla 1.7
  • MySQL version: 5.5.17
  • PHP version: 5.3.8
CiviCRM to Joomla user synch -- e-mail address changes
December 12, 2011, 02:08:53 pm
I am testing CiviCRM 4.0 with Joomla 1.7.  So far everything is working well, including the user synch tool.  It appears, however, that CiviCRM synchronization does not equate to actually changing the Joomla user profile or vice-versa?  For example, when I change the e-mail address in the Joomla profile of user Johnny, synch the users in CiviCRM (CiviCRM » Administer CiviCRM- Synchronize Joomla Users) it does not error, but it also does not update the Joomla profile with the new CiviCRM user e-mail.  I've also found the reverse to be true:  Changing the e-mail in the CiviCRM profile of a user does not update the Joomla profile with the new e-mail.  My questions:

Is this the correct behavior?

If that is the correct behavior, how does CiviCRM link Joomla and CiviCRM users if changes are made to either profile (such as username, e-mail address, etc.)?

Does anyone know of a 3rd party extension, CiviCRM plug-in, or PHP script that can add the ability to propagate changes back and forth?

dadinfishers

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Joomla 1.7
  • MySQL version: 5.5.17
  • PHP version: 5.3.8
Re: CiviCRM to Joomla user synch -- e-mail address changes
December 12, 2011, 02:56:15 pm
After more searching in the forums I found the table that was eluding me:  civicrm_uf_match (if anyone reads this and doesn't know...this table stores the link or "join" between Joomla and CiviCRM).

Looking at this table I think the way CiviCRM must work is the initial synchronization is comparing e-mail addresses and creating new profiles where one does not exist, but after the match is made from that point forward it's using the Joomla user ID and CivicCRM user ID to keep the two profiles connected.  If anyone reading this knows how this works please confirm my understanding or correct me.

Thanks!

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: CiviCRM to Joomla user synch -- e-mail address changes
December 12, 2011, 04:22:57 pm

yes that is correct. The user sync process does not process a matched ID

if you can trap via a plugin whenever a user changes their email address on the joomla side (a hook in drupal/civi terminology) then u can make the same change on the civi side (u can check the drupal code for this).

In 4.1, we update the joomla email address if the contacts primary email has changed

lbo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

dadinfishers

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Joomla 1.7
  • MySQL version: 5.5.17
  • PHP version: 5.3.8
Re: CiviCRM to Joomla user synch -- e-mail address changes
December 12, 2011, 10:00:03 pm
Thanks Donald.

I have v4.0 civi installed.  After taking a break I came back and updated one my contact records in civi (changed e-mail to a new e-mail) then checked the joomla.users table.  To my surprise the e-mail was updated.  Earlier my eyes must have played a trick on me, because I did not think civi was updating Joomla.  So this is a good thing.

Now, I want to reverse that update so if I update Joomla e-mail it will propagate that change to civi.  As you suggest I could try to capture the change, but I wonder if it would work to create a trigger on the joomla.users table so that when the e-mail is updated it updates the civi.email table.

Will this approach work or is it not good to update the civi tables directly in that fashion?  Is there a "hook" or function in civi that will allow me to properly update a field like the e-mail for a contact?

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: CiviCRM to Joomla user synch -- e-mail address changes
December 13, 2011, 11:39:45 am

check the code here:

http://svn.civicrm.org/civicrm/branches/v4.1/drupal/civicrm_user.inc

function civicrm_user_update

see the first 10 lines or so of the function.

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Mattlab

  • I’m new here
  • *
  • Posts: 26
  • Karma: 1
  • CiviCRM version: 1
  • CMS version: joomla
  • MySQL version: 4
  • PHP version: 4
Re: CiviCRM to Joomla user synch -- e-mail address changes
December 13, 2011, 09:05:16 pm
So I would want to do the same thing. If my joomla user changes their email I want it changed in their civi entry. How would I go about doing this? Just a hook? What code exactly?

dadinfishers

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Joomla 1.7
  • MySQL version: 5.5.17
  • PHP version: 5.3.8
Re: CiviCRM to Joomla user synch -- e-mail address changes
December 13, 2011, 09:52:45 pm
Matt -- I've been working on this the past couple evenings (I'm a DBA by day...not much of a coder by night!).  In any case, I'm starting to figure it out.  I think if someone can help me with my piece of code below I can probably get the answer for both of us (maybe you can help me).  Thanks to lobo's push I've been studying the Drupal approach.  To answer your question, I think the correct approach is to use this CiviCRM API:

http://civiapi.idealworld.org/api/civicrm/CRM--Core--BAO--UFMatch.php/function/CRM_Core_BAO_UFMatch%3A%3AupdateContactEmail/3.1.5

In short, it's a matter of trapping the current Joomla user ID and NEW e-mail (after update) then calling the CRM_Core_BAO_UFMatch::updateContactEmail($contactId, $emailAddress) API.

There is a second approach...use brute force and update the civi tables directly using a MySQL trigger on jom_users table or something.  This will work, but probably a bad idea.

With that, I've been using the "blank_module" Joomla module (Joomla v1.7) to easily publish the following PHP code.  My goal is simply to get the CRM_Core_BAO_UFMatch::getContactId API to work before I go forcing e-mail updates.  All I want to do is display the CiviCRM user ID based on the Joomla user ID.  Again, I know I can accomplish this same task using SQL and querying tables directly, but I'm trying to learn and understand the Civi API.

If anyone can help me understand why my PHP code below doesn't work I would appreciate it!

This script is embedded in 'blank_module' inside Joomla.  I'm logged into my site so I'm authenticated.  I get no errors, but what happens is I will get the first echo command, but after I make the API call it's as if the PHP code is exited.  No echo occurs.  No even my final echo.

Anyone?

<?php

require_once 'administrator/components/com_civicrm/civicrm.settings.php';
require_once 'administrator/components/com_civicrm/civicrm/CRM/Core/Config.php';
require_once 'administrator/components/com_civicrm/civicrm/CRM/Core/DAO/UFMatch.php';
//require_once 'administrator/components/com_civicrm/civicrm/api/v3/UFMatch.php';

echo 'Logged in as "' . JFactory::getUser()->username . '"';

$ufID = 42;

$ufmatch =& CRM_Core_BAO_UFMatch::getContactId( $userid->uid );

echo $ufmatch;
echo "Done.";

?>

dadinfishers

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Joomla 1.7
  • MySQL version: 5.5.17
  • PHP version: 5.3.8
Re: CiviCRM to Joomla user synch -- e-mail address changes
December 13, 2011, 10:32:47 pm
I think I see one mistake I've made.

This line:

require_once 'administrator/components/com_civicrm/civicrm/CRM/Core/DAO/UFMatch.php';

Should probably be this line:

require_once 'administrator/components/com_civicrm/civicrm/CRM/Core/BAO/UFMatch.php';

Or it might need to be:

require_once 'administrator/components/com_civicrm/civicrm/api/v3/UFMatch.php';

Unfortunately, changing the require line to either one results in the following error:

Logged in as "admin"DB_DataObject Error: No database name / dsn found anywhere

So I'm stumped at this point...

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: CiviCRM to Joomla user synch -- e-mail address changes
December 14, 2011, 09:08:04 am

i would add:

Code: [Select]
require_once 'administrator/components/com_civicrm/civicrm.settings.php';
require_once 'CRM/Core/Config.php';
$config = CRM_Core_Config::singleton( );

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

dadinfishers

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Joomla 1.7
  • MySQL version: 5.5.17
  • PHP version: 5.3.8
Re: CiviCRM to Joomla user synch -- e-mail address changes
December 14, 2011, 02:04:23 pm
Thanks again lobo.  I really appreciate your help.  That push got me a further.  I've just about got everything working for my test script except the very last part...the update itself.  I'm getting the error below when this script fires.

Code: [Select]
//
// This is my test script that will update the CivicCRM contact e-mail when the corresponding
// Joomla contact e-mail is updated.  My thought for this is that it would be implemented as
// a Joomla plug-in that only fires after the e-mail has been updated (not even sure if this
// is possible).  Alternative approach would be to create an 'after update' trigger on the
// jom_users table that calls the PHP script...not nearly as elegant an approach.
//
// But, for now, since I don't know how to write Joomla plugins yet, I'm tesing the idea by
// using the Joomla extension "blank_module" that allows PHP code to be placed inside a Joomla
// module.  This is a clumsy way of doing this, but it's a simple test.  My blank_module
// contains this PHP code.  I've positioned it on the front page of my test website.  Therefore,
// if you are not logged into the site nothing happens, but as soon as you log in this code will
// fire and it will update the CiviCRM e-mail with the Joomla e-mail of the connected user.  At
// least that is how it is supposed to work.
//

require_once 'administrator/components/com_civicrm/civicrm.settings.php';
require_once 'administrator/components/com_civicrm/civicrm/CRM/Core/Config.php';
require_once 'administrator/components/com_civicrm/civicrm/api/v3/UFMatch.php';
require_once 'administrator/components/com_civicrm/civicrm.settings.php';

$config = CRM_Core_Config::singleton( );

// These are the values of the Joomla session.
$user    =& JFactory::getUser();
$ufID    =& $user->id;
$ufUSER  =& $user->username;
$ufNAME  =& $user->name;
$ufEMAIL =& $user->email;
$ufTYPE  =& $user->usertype;
$ufGUEST =& $user->guest;

// Just showing our values so we know this thing is doing something.
echo "$ufID -- $ufUSER -- $ufNAME -- $ufEMAIL --$ufTYPE -- $ufGUEST";

// This if statement is just confirming that we are actually logged in.  If I don't include this
// and if we aren't logged in, then my approach of making this a module on the front page that
// fires every time will blow up and basically make the site unusable.  This $user check allows
// my testing approach to function without breaking my site.
if($user->id)
{
$contactID =& CRM_Core_BAO_UFMatch::getContactId($ufID);

mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("joomla17") or die(mysql_error());

$sql = mysql_query("select email from civicrm_email where contact_id = $contactID") or die(mysql_error());
$row= mysql_fetch_array( $sql );
$contactEMAIL = $row['email'];

// This is the piece that is broken.  It runs, but it fails with DB error -- duplicate record.
// So I wrapped it in an if statement thinking that this will stop it from updating if the
// e-mails match.  Whether same or different it doesn't like the update.
if ( $contactEMAIL <> $ufEMAIL )
{
CRM_Core_BAO_UFMatch::updateContactEmail($contactID,'$ufEMAIL');
}
}
?>

The errors I'm getting:

Quote
43 -- MyName-- MyFullName -- bogusemail@yahoo.com -- -- 0
/var/www/joomla/administrator/components/com_civicrm/civicrm/CRM/Core/Error.php, backtrace, 205 , handle, /var/www/joomla/administrator/components/com_civicrm/civicrm/packages/PEAR.php, call_user_func, 931 /var/www/joomla/administrator/components/com_civicrm/civicrm/packages/DB.php, PEAR_Error, 968 /var/www/joomla/administrator/components/com_civicrm/civicrm/packages/PEAR.php, DB_Error, 564 /var/www/joomla/administrator/components/com_civicrm/civicrm/packages/DB/common.php, raiseError, 1903 /var/www/joomla/administrator/components/com_civicrm/civicrm/packages/DB/mysql.php, raiseError, 898 /var/www/joomla/administrator/components/com_civicrm/civicrm/packages/DB/mysql.php, mysqlRaiseError, 327 /var/www/joomla/administrator/components/com_civicrm/civicrm/packages/DB/common.php, simpleQuery, 1216 /var/www/joomla/administrator/components/com_civicrm/civicrm/packages/DB/DataObject.php, query, 2420 /var/www/joomla/administrator/components/com_civicrm/civicrm/packages/DB/DataObject.php, _query, 1319 /var/www/joomla/administrator/components/com_civicrm/civicrm/CRM/Core/DAO.php, update, 279 /var/www/joomla/administrator/components/com_civicrm/civicrm/CRM/Core/BAO/UFMatch.php, save, 441 /var/www/joomla/modules/mod_blank17v11/tmpl/default.php(273) : eval()'d code, updateContactEmail, 33 /var/www/joomla/modules/mod_blank17v11/tmpl/default.php, eval, 273 /var/www/joomla/modules/mod_blank17v11/mod_blank17v11.php, require, 13 /var/www/joomla/libraries/joomla/application/module/helper.php, require, 169 /var/www/joomla/libraries/joomla/document/html/renderer/module.php, renderModule, 101 /var/www/joomla/libraries/joomla/document/html/renderer/modules.php, render, 37 /var/www/joomla/libraries/joomla/document/html/html.php, render, 339 /var/www/joomla/libraries/joomla/document/html/html.php, getBuffer, 599 /var/www/joomla/libraries/joomla/document/html/html.php, _renderTemplate, 397 /var/www/joomla/includes/application.php, render, 265 /var/www/joomla/index.php, render, 49
Sorry. A non-recoverable error has occurred.
DB Error:  Already exists.

dadinfishers

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.0
  • CMS version: Joomla 1.7
  • MySQL version: 5.5.17
  • PHP version: 5.3.8
Re: CiviCRM to Joomla user synch -- e-mail address changes
December 14, 2011, 05:39:42 pm
Fixed the "DB" problem (typo).

This line:

Code: [Select]
CRM_Core_BAO_UFMatch::updateContactEmail($contactID,'$ufEMAIL');
Should be (no single-quotes):

Code: [Select]
CRM_Core_BAO_UFMatch::updateContactEmail($contactID,$ufEMAIL);
With that fix, my test script is working.  My Joomla e-mail changes now update civi using the civi API's.  If anyone is interested in this my full script is below.

So my initial questions are answered and problems resolved, but this task is still not complete.  The next step is to take this script and "embed" it in Joomla either as a plugin or through a call that is fired by a Joomla event (like e-mail update).  If anyone is interested in helping out with this or has resources for me to review I would appreciate it!  Thanks for your help lobo.

Code: [Select]
<?php

//
// This is my test script that will update the CivicCRM contact e-mail when the corresponding
// Joomla contact e-mail is updated.  My thought for this is that it would be implemented as
// a Joomla plug-in that only fires after the e-mail has been updated (not even sure if this
// is possible).  Alternative approach would be to create an 'after update' trigger on the
// jom_users table that calls the PHP script...not nearly as elegant an approach.
//
// But, for now, since I don't know how to write Joomla plugins yet, I'm tesing the idea by
// using the Joomla extension "blank_module" that allows PHP code to be placed inside a Joomla
// module.  This is a clumsy way of doing this, but it's a simple test.  My blank_module
// contains this PHP code.  I've positioned it on the front page of my test website.  Therefore,
// if you are not logged into the site nothing happens, but as soon as you log in this code will
// fire and it will update the CiviCRM e-mail with the Joomla e-mail of the connected user.  At
// least that is how it is supposed to work.
//

require_once 'administrator/components/com_civicrm/civicrm.settings.php';
require_once 
'administrator/components/com_civicrm/civicrm/CRM/Core/Config.php';
require_once 
'administrator/components/com_civicrm/civicrm/api/v3/UFMatch.php';
require_once 
'administrator/components/com_civicrm/civicrm.settings.php';
require_once(
'CRM/Core/DAO.php');

$config = CRM_Core_Config::singleton( );

// These are the values of the Joomla session.
$user    =& JFactory::getUser();
$ufID    =& $user->id;
$ufUSER  =& $user->username;
$ufNAME  =& $user->name;
$ufEMAIL =& $user->email;
$ufTYPE  =& $user->usertype;
$ufGUEST =& $user->guest;

// Just showing our values so we know this thing is doing something.
// echo "$ufID -- $ufUSER -- $ufNAME -- $ufEMAIL --$ufTYPE -- $ufGUEST";

// This if statement is just confirming that we are actually logged into Joomla.  If I don't include this
// and if we aren't logged in, then my approach of making this a module on the front page that 
// fires every time will blow up and basically make the site unusable.  This $user check allows
// my testing approach to function without breaking my site.
if($user->id)
{
$contactID =& CRM_Core_BAO_UFMatch::getContactId($ufID);
$query  = "select email from civicrm_email where contact_id = $contactID";
$daoResult =& CRM_Core_DAO::executeQuery( $query, $params);
$daoResult->fetch();

if ( $daoResult->email <> $ufEMAIL )
{
CRM_Core_BAO_UFMatch::updateContactEmail($contactID,$ufEMAIL);
echo "<br>";
echo "Updating CiviCRM e-mail with Joomla e-mail.";
}
else
{
echo "<br>";
echo "Joomla and CiviCRM e-mails match.  No updated required.";
}
}
else
{
echo "You are not logged into Joomla.  Doing nothing...";
}
?>

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • CiviCRM to Joomla user synch -- e-mail address changes

This forum was archived on 2017-11-26.