• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Assigning users to Joomla user groups

janes

New Member
Hi,
I've found a few forum threads that sort of relate to my question, but I can't find anything that tells me exactly how to do what I want.

I'm setting up a classical music resource website, and registered users will be in one of three categories (music groups, venues and soloists). I have three Joomla user groups, so that I can showing different menu options to each group.

Is it possible to set things so that a new user signs up, they select one of the options via a radio button or dropdown, and then when the user record is created it is put into the correct Joomla user group? I've played around with options on the JUser plugin and the user groups plug in but nothing works.

If you have a solution that involves adding code, please treat me like an idiot, because I am. (Like a computer, I can follow instructions, as long as they're clear!).

Joomla 2.5.1 and Fabrik 3.0.7

Many thanks
Jane
 
It can be done with code in Fabrik. It will require that you understand the jos_usergroup_map table and how to update it using PHP. You can use MyPhpAdmin to develop the MySQL query for this update then slide it into a Fabrik PHP plug-in.
 
Thanks - any chance of a bit more detail about how to do this? I don't know how to do this from scratch, but if anyone can give me a model of the code, and some more instructions about where to put it, and what else I need to do, I can give it a try.
Jane
 
I've got this far, using the sample code at http://fabrikar.com/wiki/index.php/Common_Joomla_PHP_tasks

Code:
$db =&JFactory::getDBO();
$db->setQuery("INSERT INTO 'jcs_user_usergroup_map' ('user_id', 'group_id') VALUES ('fab_users___userid', 'fab_users___org_type')");
$db->query();

Where org_type is a number that corresponds to one of my custom Joomla User groups. (It should be a radio button, but at the moment I've just set it as a text field, while I get the code right).
It's in the form PHP box, and set to process onAfterProcess (although I've tried all the other options that might relate to the end of the form too).

JUser runs ok, and puts the user into the registered users group, but I can't get it to add the other user group.

Thanks in advance for any help.
Jane
 
Looks good. Beware that if your app would allow the user to add this record multiple times, you'll want to make sure that you don't have multiple records in jos/jcs_user_usergroup_map. In my code, I do a delete before I do an insert because I was not sure if the jos_user_usergroups table had unique constraints. Try submitting your form several times with the same user and input data and then check jos_user_usergroup_map to see if you have duplicate records in the table. If you do, you should remove the dups then add code in your plug-in to either check the table to see if there is already a record in the table or just delete any record matching that group id for that user before inserting it. This is just to keep jos_users_usergroup clean. Not sure what the implications would be for having redundant records in that table.
 
It may look good, but it doesn't work.

Have I got the script processing at the right time?

Am I right in thinking that I only need to paste that script into the PHP box on the form?

I'll try and see if I can get it to output any errors - will be about 24 hours before I'll have a chance to do that though.

Thanks again; it's good to know that I'm roughly on the right tracks!

Jane
 
Ah, well I did notice that you are using single quote marks in your query rather than the normal backticks (below the tilde) as is normal but I thought that was perhaps something from your editor see below

$db =&JFactory::getDBO(); $db->setQuery("INSERT INTO `jcs_user_usergroup_map` (`user_id`, `group_id`) VALUES ('{fab_users___userid}', '{fab_users___org_type}')"); $db->query();
Also, I think you may need to surround your fabrik placeholders with opening and closing curly braces {} (can't remember but I think so)

You'll normally want to use PhpMyAdmin to test your query with hard-coded values. Then, once that is working, past the query into your script and replace the hard-coded values with placeholders from Fabrik. This way, you'll have the query tested so that you'll know it works and that the problem involves resolution of placeholders and not something else. Also, use Fabik Debug. It will show you the queries being issued with Fabrik and often (especially when developing filters) this will allow you to copy queries from the debug window into PhpMyAdmin for testing and that can help find the issue.
 
Well, that query isn't quite right, couple of problems.

Firstly, you are trying to insert the static strings 'fab_users___userid', and 'fab_users___org_type' as data. But I presume you actually need to insert your form data, which you need to get either as Fabrik placeholders, or via our $formModel->getElementData() function.

Also, you are using the wrong quotes for your field names. If you quote table or field names in MySQL, you have to use `backquotes`. You only put 'single' or "double" quotes round data.

Also, rather than hard coding your db prefix, it's generally better just to use #__, which the J! API will automagically replace with the prefix. Helps future proof your code.

Also as pastvne pointed out, you need to make sure you aren't inserting duplicates. Easiest way in this case to do that is to use REPLACE INTO rather than INSERT INTO:

http://dev.mysql.com/doc/refman/5.0/en/replace.html

And finally, whenever you use data from a form in a MySQL query, you MUST run it through $db->Quote(), to sanitize it, and prevent SQL injection attacks (where someone can insert actual SQL commands like "drop table foo" into your form data).

So try something like:

PHP:
$db =&JFactory::getDBO();
$userid = $formModel->getElementData('fab_users___userid', true);
$gid = $formModel->getElementData('fab_users___org_type', true);
if (!empty($userid) && !empty($gid)) {
   $db->setQuery("REPLACE INTO `#__user_usergroup_map` (`user_id`, `group_id`) VALUES (" . $db->Quote($userid) . "," . $db->Quote($gid) . ")");
   $db->query();
}

Oh, also note the sanity check to make sure both userid and gid have a value.

And you'd probably want to run the script onAfterProcess, to make sure all the validation happened, and Fabrik wrote your main rows and ran your juser plugin.

-- hugh
 
Although thinking about it, it'd be a lot easier to use the J! user helper function addUserToGroup() ...

PHP:
$userid = $formModel->getElementData('fab_users___userid', true);
$gid = $formModel->getElementData('fab_users___org_type', true);
if (!empty($userid) && !empty($gid)) {
   JUserHelper::addUserToGroup($userid, $gid);
}

BTW, the true as the second arg to getElementData() tells us to grab the raw value, which is what you want in this usage.

-- hugh
 
Thanks so much for your code help Hugh - but it's still not working. Now I just get a blank screen, with /1 appended to the original form URL and no debug info in the Joomla debug console. I wondered if it was due to the code having single quotes instead of backticks (I'm now confused about which it should be - but I tried both and no change).
This was after updating Fabrik this morning from github in case there were any bugs.

Where do I go now?
Jane
 
UPDATE: the blank screen was being caused by Juser - I had to reinstall it from the downloads page after the github update. I've noticed this happening before.
So, now I'm back to the original problem - the new registered user is being added correctly but not being added to the custom user group. It's as if the PHP code isn't even running.
 
Prove whether the code is being fired by adding

die('im in my script');

to the script then firing it. It should die with a white page containing that message.

Also, you can sprinkle echo statements in your code at various points to see if it is going sideways at a particular point in the script.
-BD
 
I added that at the end, now I get:
"array_keys() expects parameter 1 to be array, integer given in plugins/fabrik_element/databasejoin/databasejoin.php on line 2758
im in my script"

Is that progress?
 
Using standard code bisection debugging techniques. Sprinkle echoes into your code at important points. Put an echo at the top of your script like:

echo 'I'm in my script now<br>';
...
echo 'Point 1<br>';
...
...
echo 'Point 2 <br>';


Then run the script and see how many 'Point' messages appear and what the error is. When you get it down to the offending instruction, then start to var_dump(var); the variables to see what's going wrong.

-BD
 
Need to see if this is an issue unrelated to your script (which is possible). Save your script off into a text file somewhere and remove it from the Fabrik project then re-test. See if the error continues. If so, it might be an issue with the latest git and unrelated to your script. Need to do this to prove.
 
Yes, when I take the script away, everything works fine. The form creates a registered Joomla user, with no errors.

Really hope this can be fixed!

Jane
 
Presumably you mean 'everything works fine except it doesn't do the things in the script'. If that is correct, then put a script in place that contains only a comment. If that causes the error, they I think you need to report it to Fabrik because it may be an issue with the way that Fabrik is firing the plug-in rather than the code itself. My creating a 'do nothing' script, you can tell Fabrik dev that you just created a blank script and it caused the error. That way, they can know it should not be anything you did in the script to cause the error.

If the 'do nothing' script causes no error, then you need to look more closely at the full-code script and start commenting out lines one at a time until the error disappears then you can direct us to that specific instruction that is causing the error to see if further refinement is required.
 
Yes, when I remove the script, the form and the JUser plug in submit without an error.
When I add a text script, it outputs the text.

When I added Hugh's custom script to add a new user to a user group, the form and JUser plugin still run, and create a new user, but the new user is not added to a custom group.

When I added the die line to the code, I got the error message above, but the user was still created and added to the registered user group.

It therefore looks as if the problem is in my plugin PHP, or in the configuration of the element that contains the group id. I can't see any part of the script that I could comment out without it affecting the rest of the script - it looks to me as if removing any bit of it would stop the whole thing from working.

I'm sorry if I'm not explaining things properly. I've been trying to figure this out for 3 days and I don't know any code.
Jane
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top