Show pages in menu filtered by users atributes

Permalink
hello,
i have a situation.
i have an atribute for users that can get those values:
10º
11º
12º

i have a button in the menu that i hide and i only want it to show when this atribute is set to 12º.

how can i do that?

 
adajad replied on at Permalink Reply
adajad
To get information from the user object you first need to get the User Info object.

<?php
$u = new User(); //get the current user
$uID = $u->getUserID(); //find the current user id
$ui = UserInfo::getByID($uID); // user info object for the current user
$attrValue = $ui->getUserTemp;  // set $attrValue to the value of the attribute with the handle 'temp'
if ($attrValue == '12º') {
    echo '<button type="button">Click Me!</button>';
}
?>


So in the code above I'm getting the value from the attribute I have given a handle named 'temp'. I check if the value of that attribute is 12º, and if it is I output the html for a button.
ranonymus replied on at Permalink Reply
thanks for your reply, but the button in the autonav as the atribute of exclude from autonav, i want to set that attribute off if the user have the attribute 12º
adajad replied on at Permalink Reply
adajad
I don't get it...

You said you wanted to show a button if the attribute had the value 12, right?

Now you say you want to change another attribute value if the first value is 12.

Please explain what it is you want to achieve, and I might be able to give you better assistance.
ranonymus replied on at Permalink Reply 1 Attachment
so its like this:

as you can see in the attached file under "alunos" i have 4 subcategorys. but in fact i have 5 beacause that one as an attribute set "exclude from nav" wich name is "PAP"

the user logs into my website and in the login page i check if the user attribute wich the name is "Ano" is set to 12º. if so i set the attribute of the "PAP" page off...
adajad replied on at Permalink Reply
adajad
In autonav:

You need to check the active version of the nav item object and get the handle for the page named 'PAP' (should be 'pap'). If the handle is 'pap' then check if the user attribute with handle 'ano' equals 12. In that case show the link, otherwise move on to the next nav item. This assumes your page handle won't change.

You can also add your own page attribute and check if the page has the attribute, and in that case check if the user has the appropriate user attribute set.

You can solve this with either a switch statement or if statements.

Using switch statement you can later, if desired, easily add different nav links for 10 and 11 as well.

Note: doing the above makes the page attribute 'Exclude From Nav' obsolete, so you should remove that page attribute.
adajad replied on at Permalink Best Answer Reply 1 Attachment
adajad
I just reread my response, and it wasn't that clear. Attached you will find a custom template for autonav. It's not pretty and uses if/elseif instead of switch statement, but I guess it works.

Following assumptions are made and the template depends on it:
- You have a page attribute of type checkbox with handle 'pap'
- You have a user attribute of type select with multiple values ('10', '11', '12' aso) having the handle 'ano'.
- You do not use 'Exclude From Nav' page attribute.

For every page you want to hide in the nav from people not having the attribute 'ano' set to '12' just add the page attribute 'pap' and mark the checkbox.

How to use the attached file:
1. save the file to 'webroot/blocks/autonav/templates/[your_template_name]/view.php'. Create folders as necessary and rename view.txt to view.php.
2. Click on your autonav and click 'Custom Template'. Choose [Your Template Name].

This template is completely unstyled, so if you want to style it in any way then just add a view.css with your styles in the same folder.
********************
An even easier way to achieve the same thing is to use group permissions for the page. Create user groups for the users and set view permissions only for Administrators and the group '12 Años'. That way guest users cant reach it and minors can't reach - not even with a direct link, which they can with my template.

Anyways, you now have two solutions to your problem.
ranonymus replied on at Permalink Reply
thank you for your help.
i will try everything and tell you what i got
ranonymus replied on at Permalink Reply
i think u got it wrong, pap is not an atribute its the page name. exclude from nav yes is the attribute.
adajad replied on at Permalink Reply
adajad
The code I supplied is still valid.

Just follow the instructions and you will find it will will work. If you don't want to use 'pap' as attribute handle and/or name, then you can use whatever suits you. Just remember to change in the code as well where the code refers to
$ni->cObj->getAttribute('attribute_handle');


And you need to remove the attribute 'Exclude From Nav', otherwise the page will be hidden for ALL users.

I still think the best option for you is to set up permissions instead.
adajad replied on at Permalink Reply
adajad
How did you do? Have you solved it yet? Do you need any more assistance?
ranonymus replied on at Permalink Reply
yes i solved trought the groups permissions...
now i have another situation...
in the add user on dashboard i want to add 2fields (like the user and email fields) to the form.
those are Nome and Cargo, but i want those to be saved to the db to...where can i edit that?
adajad replied on at Permalink Reply
adajad
Create two user attributes and make them required upon registration.
ranonymus replied on at Permalink Reply
the thing its me that will add the users... questions of security...
adajad replied on at Permalink Reply
adajad
User attributes is still the way to go. I'm on my phone right now in a pub so I have a hard time to give you the correct links and names of pages, but you should be able to create new user attributes from the dashboard. Those attributes will then be accessible from Add User.

If you want to show or use those attributes later (and not just in the user list in the dashboard) you can reach them through the UserInfo object. You can find more info about that in the Developer Index under the Documentation link above.