Accessing user profile variables - php

Using the profile module I've created a textfield called profile_real_name which the user fills out when registering. How do I access this variable in the node.tpl.php?
I used the dsm($user) function to output the user variables and it contained everything except the data for the profile_real_name
I also ran dsm($vars) on the phptemplate_preprocess_user_profile and I could see it contained an object called account which did contain the info I needed but this object isn't available in the $user variable.
Many thanks

If you want to access the author's profile information in node.tpl.php, then you want to work with a phptemplate_preprocess_node function rather than the user_profile one. The node preprocess function doesn't have an $account object by default though, so you'll have to load it in:
This goes in the phptemplate_preprocess_node function in your template.php file:
if ($vars['uid']) {
$vars['account'] = user_load(array('uid' => $vars['uid']));
}
Then you would be able to access the author's profile values in your node.tpl.php. The value you asked about specifically would be:
print($account->profile_real_name);
However, it sounds like you might want the node author's name to appear as the profile_real_name value rather than their account name?
If so, a MUCH more efficient way would be to override the theme_username function.
That's not directly what you asked about so I won't go into it here, but this post on the drupal.org forums would be an excellent place to start for Drupal 5 or 6:
http://drupal.org/node/122303#comment-204277

$account is what you usually call a user that isn't the global user to avoid accidently overwriting the global user which would result in the user be get logged in as that user.
I just did a bit of checking and the easiest way to solve your problem is to use $account in the template instead of $user.
Using $user in the template or doing like WmasterJ suggests is faulty. You will post the wrong data. You will post the data of the logged in user not the data of the user who's profile is being watched. This bug will happen when you view all other users' profile than your own.
Preprocess functions is not hard to make, in your template.php file in your theme you just replace phptemplate with your theme's name defined the code. In this case you wont need to alter the preprocess function, since you already have what you need.

If you want to do this within for instance the user-profile.tpl.php all the information you need exists within the $account array.
Otherwise you can access user data by loading a user object based on it's id (of the currently logged in person that is, or if you can query the DB and get uid that way).
First get the uid of the current user:
$uid = $user->uid;
Then load the a user object:
// Create user objets based on uid ()
$user_obj = user_load($user->uid);
Then load that users profile variables:
// Load profile
profile_load_profile($user_obj);
Now the $user_obj variable (which is passed by reference to profile_load_profile) has an object with the the profile information that can be accessed like this:
$user_obj->profile_real_name
Hope it helps!

Related

Read Cookie PHPSESSID in opencart:2.3.0.2

I want to use an opencart database for another website, so users don't need to register their details again.
For example: I sell Clothes (unStitched) on opencart and I also give services for stitching them. Opencart doesn't have a module to do this, so I want to redirect users to another website and there I want to read the user details for further processing.
How can I read PHPSESSID cookie? How does opencart read firstname on edit profile forms?
<?php echo $firstname; ?>
Thanks in advance.
In order to work with the Session data in an Opencart view, you will need to add some code to the controller. First we need to add an item in the $data array that the controller uses to pass variables to the view:
Assuming you want to show this link in a product view, you will need to edit the product controller's index action.
Controller file: public_html\catalog\controller\product\product.php
Find this line:
if ($product_info) {
Add the text inside the brace:
$data['mysessionvariable'] = $this->session;
You can restrict the available data to the sessionID only using the code below:
$data['mysessionvariable'] = $this->session->getId();
Now the relevant PHP Session data is available to the view, you can simply reference the session variable in the view wherever your need to. e.g. echo $mysessionvariable['session_id']; or echo $mysessionvariable;
View file: public_html\catalog\view\theme\default\template\product\product.tpl

Options to secure split admin pages in PHP

For the first time I need an admin panel for my PHP application and I thought I had it figured but now I am not so sure. My initial thought was this.
I would create a function to test whether $username, $password are in the database. If they are then I would set $_SESSION to TRUE. Given the site has multiple access levels it would also need to check the user role.
This is all fine, on arriving at the admin panel you enter your username and password then role, if all is ok you get to the admin panel, if not you are redirected to the login page.
What I am thinking though is that in the interest of reusable elements I would want to adopt the same principles in the admin section as on the main site, that is to have a header.php, footer.php and a content.php that will contain whatever it is the admin has chosen to view or do.
There is my issue. If in the header.php I call the doesUserExist function, even if the user is not logged in they can still access the content.php or footer.php pages.
I must be missing something really simple here, the option of checking if the user exists on all 3 pages appears somewhat bloated.
Am I looking at this the wrong way? Is there a method whereby you can check if the user exists and it they do apply user control to the entire admin directory regardless of what they access?
All the examples I could see did not cover this and that makes me wonder if there is a good reason..
Thanks in advance for any suggestions.
Maybe you do something like this:
In another inlcuded PHP page, or maybe in header.php, run all of your checks, like doesUserExist() and such, and assign variables based on your results. Then use those variable to create the page's content.
$content = "Content for a non-logged in user.";
if (doesUserExist()) {
if (getUserType() == 'admin')
$content = "Conent for Admin user";
else
$content = "Content for non-Admin user";
}
Then, use $content, and any other related variables that you created, to fill in your html:
<h1><?=$content?></h1>
Obviously this code is very simplified and just for illustration. getUserType() is just my arbitrary example of a function to determine the user type, and the code here would certainly require more complexity to get all the data you need to build the appropriate page.

Accessing Model from View

I have a foreach loop which generates links with different IDs. I have an ajax function to process this ID and the username (from session) to add a record to the table. (It's like enrolling for some events).
I want to make an additional statement which will check whether the user has already enrolled for the event with some ID and if so, the link will be deactivated or change color. I've tried creating another variable (which is passed to the view)
$data['myvariable'] = $this->mymodel->myfunction();
This function in model checks all the records from the database where user's username appears and insert to array all event IDs. I've tried adding an additional if statement before links in foreach loop which checked whether the ID from link is in array but there appeared some problems with the controller. (i think that I couldnt assign the array to the variable $data['myvariable'] ).
I know that accessing to model from view is not "proper"... Anyone know how to solve this problem?
Do you have a field in the DB to store whether or not the user has enrolled? Is so, run a select query on that table checking for that value then use a conditional statement to effect the link. Kinda like this
$enrolled = $this->your_model->your_method($param);
if($enrolled){
process links here;
}
I'd say it depends on the MVC your using also. You may have to load the model in the view to use it from within the view, not ideally the right approach as you could load it via your controller and set it as a variable to be passed into the view if the variable to be passed contains multiple outputs such as a query from a DB or something you can pass it to the view as an array object and then in the view do a while, for, foreach, whatever on that array.

Should I set sessions for all my site settings or access them via class variables?

Whats a good way to set this up.
I want to allow users to add themes to their profile and each time they log in for that theme to be applied. (the themes are saved in the database)
I have thought of 2 ways to do this.
1) when they log in for a session to be made holding theme information, like theme name, theme source, etc.
2) when they log in to call in get_theme() function in my __construct() that will query the database for the data and apply it to public variables in my class that i can call from within my application?
This is Barbareek. I possess more than 4 years of experience on Java/J2EE stack of technologies.
If I understood the question correctly, you basically trying to save the user's profile theme information. This question should better be addressed like:
There must be a table in the database representing the persistent entity, in this case the user.
All the themes are defined statically i.e. you provide the user the options for the themes that have already been define in the data store.
The theme attribute must be a part of the user entity, that needs to be set, for an individual user as he/she wishes.
Save this information as you are saving the other details for the member.
Hope this helps.
Second one is not an option at all. You cannot use a PHP class to store anything between calls, as it just will die in less than second like all php scripts do.
So, the only choice is persistent storage, like database or session.
Since you have the theme information already in the database and I suppose you may have designed a table for a theme that have certain attributes, the primary key or the discriminator column being the name of that theme.
As the user performs the activity in the session, he/she can choose among the name of available themes and based on name you just pull that information from back end database when the USER session creation is in process.
thanks...

How can I work out this view?

List -> l_user I have a List node that has a user-reference field (l_user).
Story -> s-user I then have Story nodes that also have a user-reference field (s_user).
There is no direct connection between List and Story.
I want to add a view to List that lists all the stories that reference the same user that list references. Basically something like SELECT stories WHERE story.s_user = this list.l_user and l_user is the user referenced in the list this view is on.
So far the view is being filtered by node:type story but I'm not sure what to use (arguments/filter, etc) to link the story s_user to the list l_user.
Is this doable with Views?
You can do this with a Views argument. What you're trying to do is filter that list by user, and you get the user to filter by from the node you're currently on (or more specifically, the node's user reference field). So what you'll need to do is supply Views with an argument that is equal to the node's user reference field.
To do that, set up your view as normal and as if you were showing every user's node. So you might have a view that's like:
Page 1 (by User 1)
Page 2 (by User 1)
Page 3 (by User 2)
Page 4 (by User 2)
Page 5 (by User 1)
Where the user is a user reference field on each page called Story user reference.
Now, under Arguments add an argument for Content: Story user reference. Now, the view will only show nodes that are posted by the user specified in the argument. The problem is, in a block, there is no argument to specify: you need to provide a default argument.
Still on the argument configuration pane for Content: Story user reference, select Provide default argument under Action to take if argument is not present. You'll get a variety of options, but none of them are what you're looking for: the current node's user reference field.
So you'll need to use the PHP code action and use the following code:
$node = node_load(arg(1));
return $node->field_list_user[0]['uid'];
This loads a node based on the node ID retrieved from the current page's path and returns the node's user reference field (change field_list_user to the name of the user reference field on the list nodes).
So if I'm on node 17 whose user reference field states user 4, the argument that'll be passed to the view is 4. The view will then only show nodes in the view who have user references that are also 4.
Save your view, create a block display, and place it wherever you want. When you visit a node page with a user reference field, the block will populate with the referenced user's nodes.
I'd suggest getting all of the user references from the list node and passing those into a views argument. So your code would look something like this (untested):
$user_ids = array();
foreach ($list_node->l_user as $user_reference) {
$user_ids[] = $user_reference['uid'];
}
$view = views_get_view('list_view');
return $view->preview('block_1', array(implode(',', $user_ids));
That assumes you have a view named 'list_view' with a display named 'block_1' (you can see the machine name when you hover over the display). That display needs to be a node view with a filter of node type 'story' and an argument of content user_l set to take multiple values. There's almost certainly a bug in that code, but I know the general concept works, as I've done it several times before.
To solve this you need to download NodeReferrer. http://drupal.org/project/nodereferrer
It provides a counter part to CCK's node reference field.
Then you can call the field "Node referrer" in your View.

Categories