|
It is common knowledge that you can access the current user information from within the joomla framework i.e within a component or a module by using the global variable $my But in situations where you need to access the current user object from within a wrapper to dispaly personalized content or perform database transactions for the current user a different approach is needed. You can use a session variable to keep track of the user from within wrapper.php. I have found this method to be unreliable and have opted to use a different approach. The approach involves creating a database table and tracking the PHPSESSID session ID variable against the current user object from within the wrapper class. Read on to see how its done...
First of you will need to access the wrapper.php file located in /components/com_wrapper/wrapper.php Since the wrapper.php file is a part of the framework we can access the user object through global $my We are going to be looking at the function
Now we will use $phpSessionId and $userId with a mysql database table. First Create a table with the following name and fields
Now within the same function showWrap we are going to create a database connection and then we are going to insert a row in the table with the userId and the sessionId every time a user session is created. Since each session is unique, the table will give us the ability to access the current userId depending on the current sessionId.
Now to access this information from our wrapper module we will need the ability to write PHP code directly in the WYSIWYG editor and have the server interpret the code as PHP and not just text. To do this we will need a third party mambot that automatically converts any PHP code to executable PHP code for dynamic functionality. We are going to use a mambot called DirectPHP, which can be downloaded here . Note that this is for Joomla 1.0, you can find a 1.5 version on the author's site . Install the mambot and make sure that is is published. The only nuance is to make sure that the PHP code is formatted as a paragraph, within the WYSIWYG editor of your choice. From within the wrapper, the first line should be a connection to the database and a retrieval of the record based on the session ID, which is constant. This is how your typical wrapper file should begin:
Now you have the userId and you can use it to track the currently logged in user. Note that this example has been tested to work in Joomla 1.0. You can also introduce constraints in your SQL syntax to select a distinct value from the database to speed up execution time. The reason being that every time your user accesses the wrapper, an entry is made in the table. So eventually you will have a table full of duplicate session and userId values. To prevent this from happening you can introduce a constraint in the wrapper.php file that will check the database for the sessionId first. If the session id exists then no new information should be written to the table. Here is what your showWrap() function should look, taking the duplicate PHPSESSID issue into account.
|