Forums

Value hidden field based on user group

Mwa19 03 Nov, 2014
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.
GreyHead 03 Nov, 2014
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
Mwa19 04 Nov, 2014
I think I managed to load the requiered user information. This is a screencopy of my form: Data Array

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
Mwa19 06 Nov, 2014
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?
GreyHead 06 Nov, 2014
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:
 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
Mwa19 06 Nov, 2014
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.
GreyHead 06 Nov, 2014
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
Mwa19 06 Nov, 2014
No I use single db record loader with the following condition:

<?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
                )

        )
GreyHead 06 Nov, 2014
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
Mwa19 06 Nov, 2014
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.
Mwa19 07 Nov, 2014
I tried this code, but is does not seem to do anything.
$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
(
)
GreyHead 08 Nov, 2014
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
Mwa19 11 Nov, 2014
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';
    }

    ?>
GreyHead 11 Nov, 2014
Answer
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:
<?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
Mwa19 11 Nov, 2014
Hi Bob,

You're the best! It works. Thanks for your support. I could not have figured this out by myself or maybe in a year or so 😉 .
This topic is locked and no more replies can be posted.