Hi,
Is it possible to make a hidden field in the form. Based on the user group of the logged in user the content must be changed.
For example Usergroup A has access to category 1,2 and 3.
Usergroup B has access to category 3 and so on.
The value of the hidden field should be 1,2 and 3 for usergroup A. The value of the hidden field should be 3 for usergroup 3.
I have no idea how and if this is possible at all. I hope someone can help me.
Is it possible to make a hidden field in the form. Based on the user group of the logged in user the content must be changed.
For example Usergroup A has access to category 1,2 and 3.
Usergroup B has access to category 3 and so on.
The value of the hidden field should be 1,2 and 3 for usergroup A. The value of the hidden field should be 3 for usergroup 3.
I have no idea how and if this is possible at all. I hope someone can help me.
Hi Liggie,
You can do this using Custom Code - the code to get a user's groups (two versions) has been posted in this thread.
Note that in Joomla! 3 a user can belong to several groups so your PHP will need to handle that.
Bob
You can do this using Custom Code - the code to get a user's groups (two versions) has been posted in this thread.
Note that in Joomla! 3 a user can belong to several groups so your PHP will need to handle that.
Bob
I think I managed to load the requiered user information. This is a screencopy of my form: Data Array
But now I'm completely stuck. I would like to use the group_id to determine if a checkbox (or dropdown value) has to be checked or not. So for example checkbox A has to be checked automically if user belongs to group_id 21. Could you please give me a hint how to accomplish this?
Milja
Array
(
[option] => com_chronoforms5
[chronoform] => zoekformuliertest
[Itemid] => 0
[0] => Array
(
[user_id] => 593
[group_id] => 2
)
[1] => Array
(
[user_id] => 593
[group_id] => 16
)
[2] => Array
(
[user_id] => 593
[group_id] => 21
)
)
But now I'm completely stuck. I would like to use the group_id to determine if a checkbox (or dropdown value) has to be checked or not. So for example checkbox A has to be checked automically if user belongs to group_id 21. Could you please give me a hint how to accomplish this?
Milja
I need the values in group_id (multiple values possible). I know that I first have to have it in an array, but what is the next step now. For example how can I show these values in my form or use it in a hidden field?
Hi Liggie,
Hmmmm . . . I've not tried this before but I think it should work. In the Form On Load event after the Custom Code to get the groups you can set appropriate values in the $form->data array and they ChronoForms will set the corresponding selections/checkboxes. I can't work out quite what format you've got the Group data in - the array doesn't seem to have a name :-(
Assuming that you have an array like this instead:
Then set the values like this:
Bob
Hmmmm . . . I've not tried this before but I think it should work. In the Form On Load event after the Custom Code to get the groups you can set appropriate values in the $form->data array and they ChronoForms will set the corresponding selections/checkboxes. I can't work out quite what format you've got the Group data in - the array doesn't seem to have a name :-(
Assuming that you have an array like this instead:
groups => Array (
[0] => Array
(
[user_id] => 593
[group_id] => 2
)
[1] => Array
(
[user_id] => 593
[group_id] => 16
)
[2] => Array
(
[user_id] => 593
[group_id] => 21
)
)
)
The you can convert this to a simpler format:
$group_array = array();
foreach ( $form->data['groups'] as $v ) {
$group_array[] = $v['group_id'];
}
Then set the values like this:
if ( in_array('21', $group_array) ) {
$form->data['select_name'] = 'xxx';
}
?>
Bob
Hi thanks for your reply! Maybe very stupid but how do I give a name to the array? I use dbloader to retrieve the group_id for the logged in user. I see no fields in the dbloader where I can give it a name.
Hi Liggie,
I assume that this is the DB Multi Record Loader action? If so then please try adding a Model ID e.g. groups
Bob
I assume that this is the DB Multi Record Loader action? If so then please try adding a Model ID e.g. groups
Bob
No I use single db record loader with the following condition:
I turned Model Id on, now I get this result, is the correct?
<?php
$user = JFactory::getUser();
return array("user_id" => $user->get("id"));?>
I turned Model Id on, now I get this result, is the correct?
Array
(
[option] => com_chronoforms5
[chronoform] => zoekformuliertest
[Itemid] => 0
[user_id] => 166
[groep] => Array
(
[0] => Array
(
[user_id] => 166
[group_id] => 7
)
[1] => Array
(
[user_id] => 166
[group_id] => 8
)
)
Hi Liggie,
That looks good. Which version of ChronoForms are you using? The forum info under the post says CFv4 but the code looks more like CFv5?
Bob
That looks good. Which version of ChronoForms are you using? The forum info under the post says CFv4 but the code looks more like CFv5?
Bob
I started with v4, but switched to v5. Thanks for helping, I'm going to try the code you suggested and will let you know if it works.
I tried this code, but is does not seem to do anything.
This is the result:
$group_array = array();
foreach ( $form->data['groups'] as $v ) {
$group_array[] = $v['group_id'];
}
This is the result:
Data Array
Array
(
[option] => com_chronoforms5
[chronoform] => zoekformuliertest
[Itemid] => 0
[user_id] => 166
[groups] => Array
(
[0] => Array
(
[user_id] => 166
[group_id] => 7
)
[1] => Array
(
[user_id] => 166
[group_id] => 8
)
)
)
Array
(
)
Errors
Array
(
)
Hi Liggie,
The first half of the code doesn't do anything useful on its own, you need to add the second part as well - but amended to do what you need it to do.
Bob
The first half of the code doesn't do anything useful on its own, you need to add the second part as well - but amended to do what you need it to do.
Bob
It works for one value, but not if I add a second one. Is it possible to select multiple checkboxes based on the values in groups?
<? $group_array = array();
foreach ( $form->data['groups'] as $v ) {
$group_array[] = $v['group_id'];
}
if ( in_array('7', $group_array) ) {
$form->data['Repertorium'] = '0';
}
if ( in_array('8', $group_array) ) {
$form->data['Repertorium'] = '1';
}
?>
HI Liggie,
It works OK - but the PHP you have over-writes the first value with the second one.
Assuming that you have a checkbox group with an array name then you need to add the values to the array:
Bob
It works OK - but the PHP you have over-writes the first value with the second one.
Assuming that you have a checkbox group with an array name then you need to add the values to the array:
<?php $group_array = array();
foreach ( $form->data['groups'] as $v ) {
$group_array[] = $v['group_id'];
}
if ( in_array('7', $group_array) ) {
$form->data['Repertorium'][] = '0';
}
if ( in_array('8', $group_array) ) {
$form->data['Repertorium'][] = '1';
}
?>
Bob
This topic is locked and no more replies can be posted.