email to user

kraadde 20 Nov, 2024

I need to add the usergroup of the user that is logged in the confirmation email, like " You are logged in as xxx user"

Any advise how to do that?

Max_admin 20 Nov, 2024

you will need a Read Data action to get you the group name from the groups table based on the user's usergroup, you can get the user's groups with {user:groups}, but this will return an array

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
kraadde 20 Nov, 2024

thanks for your answer, but as far as I see, ther is no data read action in CF5.

I tried the following Custom Code in the setup section:

<?php

$user = JFactory::getUser();

if ($user->id) {

$mainGroupId = $user->get('gid');

$mainGroup = $user->get('groups');

$Usergr = $mainGroup[$mainGroupId];}

?>

and added the following line in the Email action, email template:

....

<tr><td>Usergroup: {Usergr}</td></tr>

....

What did I wrong?

Regards

kraadde

kraadde 20 Nov, 2024
Answer

now I have found the solution:

correct code to retrieve the usergroup:

<?php
// Load the Joomla user object
$user = JFactory::getUser();

// Check if the user is logged in
if (!$user->guest) {
    // Get the user group IDs
    $userGroups = $user->groups;

    // Load the Joomla database
    $db = JFactory::getDbo();

    // Query to get the group names
    $query = $db->getQuery(true)
        ->select('title')
        ->from('#__usergroups')
        ->where('id IN (' . implode(',', array_map('intval', $userGroups)) . ')');
    $db->setQuery($query);
    $groupNames = $db->loadColumn();

    // Join group names into a string
    $groupNamesString = implode(', ', $groupNames);

    // Save the group names into a form variable
    $form->data['user_groups'] = $groupNamesString;
} else {
    $form->data['user_groups'] = 'Guest';
}
?>

and following to insert in the email template::

Your user group(s): {user_groups}

Hope this will be helpful to others.

Regards

kraadde

Max_admin 20 Nov, 2024

you are using v5 ? I thought you are using v8

Thank you for posting the solution

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
You need to login to be able to post a reply.