Welcome to the MetaMod and Chameleon Support Forums.

Before posting, please check out the FAQs.

helpme

 

Need extra help with your Joomla site? Consider paid Joomla support by the developer of Chameleon and MetaMod.

 

MetaTemplate/metamod/session

MetaTemplate/metamod/session

Hi again smile

I am using metatemplate on a site that lets registered users create their own minipage on my site. This page is generated by a component called GmapFP and all data is store in a table called __gmapfp. One of the parameters mini page creators can decide on is what template to use. The URL to their minipage looks something like this: 'www.example.com/com_gmapfp/minipage1 

When a guest visits one of these minipages, it gives the feel of going to different website. As long as the guest does not enter the frontpage or one of the other minipages, the template is retained. So most functionality of the main site is being used.

Now, here´s the problem. The minipage creator is also allowed to choose which modules he would like to display. Different minipage creators would decide to include different modules.

The metamod problems I´ve encountered.
Firstly, different templates use different positions, so simply including a module will not do much (if I´m not missing something here).
Secondly using the standard metamod approch "if (
$option == 'com_gmapfp'and $view ==...)return XXX", including the modules the user would like, would only be in effect on the minipage (not the entire site as I would like).

This is what I have tried:
Using Metatemplate, I figure out if the user is on a minipage and if he/she is, a query is made to the gmapfp table, using the $id" variable to retrieve the right record. The record contains data on the template and modules to be used.

In Metatemplate:

$dd = JRequest::getVar('id');
$de = strrchr($dd, 58);          //the id says "#:the-alias"
$df = substr($de,1);              //so I have to extract the part after ":"
$query="SELECT*FROM #__gmapfp WHERE alias = '$df' ";
$result = mysql_query($query);
$temp = mysql_result($result, $i, 'template');
$​mod1 = mysql_result($result, $i, 'mod1');
$​mod2 = mysql_result($result, $i, 'mod2');


$session = JFactory::getSession();   // Setting the retrieved variables to the session
$session->set('firstmod', $mod1);     // Hopefully will be able the fetch them with

$session->set('secondmod', $mod2);  // metamod
$session->set('template', $temp);



if ($option == 'com_gmapfp'
and $view == 'gmapfp'
return $temp;





In metamod


$session = JFactory::getSession(); //gets the session variable
$first = $session->get('firstmod'); // and outputs the string : {module #number }
echo "{module ",$first,"}";   //this is a tag used by "modules anywhere to include a module

//I use this in order to bypass the position problem

-> Using this approch displays the correct modules on the minipage, but not on any other page..

I know next to nothing about the JSession, so that is probably where I´m going wrong. And I´m pretty new to metatemplate..

I´m using MAMP. I don´t know if that makes any difference.
I am truely lost on this one, any ideas?

Sorry about the loooong post:)

--Love your software

apollo123
Beginner Modder
ranks
useravatar
Offline
17 Posts
Administrator has disabled public posting

Re: MetaTemplate/metamod/session

Wow, what an amazing setup!

I'm sure that you can get this all working with MetaMod and MetaTemplate - the devil is just in the details, as usual.

Let's look at the issues in turn.

1 - the initial database query etc.

Have you done any debugging to test if you are getting the right data out of the database?

As long as there is a "notices" area set up in your template, then you can use the following code to output information from MetaTemplate into that area:

JError::raiseNotice( 'ERR', "message here" );

e.g.

JError::raiseNotice( 'ERR', "temp: $temp, mod1: $mod1, mod2: $mod2" );

I'd suggest that you try that at the end of the MetaTemplate rule, just to check that you really are getting the right information out of it.

2 - always sanitise anything you put into an SQL query!!!

instead of this:

$df = substr($de,1);              //so I have to extract the part after ":"
$query="SELECT*FROM #__gmapfp WHERE alias = '$df' ";

do this:

$df = substr($de,1);
$df = $db->Quote( $db->getEscaped( $df ) ); // escapes it and quotes it
$query="SELECT * FROM #__gmapfp WHERE alias = $df ";

3 - Sessions

The right way to use the sessions is to provide an additional namespace (a sort of identifier) so that your variables don't clobber other people's variables of the same name.

e.g.
$session =& JFactory::getSession();
$namespace = "gmapfp";
$session->set('firstmod', $mod1, $namespace );
$session->set('secondmod', $mod2, $namespace );
$session->set('template', $temp, $namespace );

Then to retrieve them later in MetaMod:

$session =& JFactory::getSession();
$namespace = "gmapfp";
$firstmod = $session->get('firstmod', null, $namespace); // key, default val, namespace
$secondmod = $session->get('secondmod', null, $namespace); // key, default val, namespace
$template = $session->get('template', null, $namespace); // key, default val, namespace


4 - Handling different module positions on different templates

This is something I have considered putting into MetaTemplate - the ability to map different module positions to different names. But until I add that, this is something that can be achieved with MetaMod Pro, with a little magic.

In MetaMod Pro, you get the ability to alter the module position that a module will appear in. You are not actually restricted to placing modules inside the MetaMod.

To do this, you use the mechanism described in Module Param Control.

So as well as determining which modules will be present, you can change the module position that they will appear in, depending on what template has been selected.

In this case I am going to retrieve the name of the template not from the session, but from the system, as it's more definitive.

$mainframe =& JFactory::getApplication();
$template =& $mainframe->getTemplate();
// now find out which modules I have to display
$namespace = "gmapfp";
$session =& JFactory::getSession();
$firstmod = $session->get('firstmod', null, $namespace); // key, default val, namespace
$secondmod = $session->get('secondmod', null, $namespace); // key, default val, namespace
// now alter the module positions for these modules based on the current template
switch( $template ) {
  case 'template1':
    $pos = 'top1'; break;
  case 'template2':
    $pos = 'advert2'; break;
  case 'template3':
    $pos = 'right-top'; break;
}
$changes->mod( $firstmod )->position( $pos );
$changes->mod( $secondmod )->position( $pos );
// publish them
return "$firstmod, $secondmod";

Alternative to MetaMod Pro

You could probably just put a MetaMod into each of the module positions in each of the templates that you want to put the modules into (e.g. "top1", "advert2" and "right-top" from the example above). In each of the positions, only include the target modules if you are on the correct template. That would work almost as well, except that you just have more modules splatted around and it's harder to manage.


Hope that gets you started, at least!

Cheers,
Stephen

[edited to fix the syntax of the getSession command]

Edited By: metamodguy
17-Jan-11 20:25:55

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3313 Posts
User info in posts
Administrator has disabled public posting

Re: MetaTemplate/metamod/session

Hi again:)
Thank you so much for your insight and quick reply, I definitely learned a few new things:) Unfortunately, the problem does not seem to be solved..

I made the changes you suggested, but the result remained the same.
I echo the session variables in metamod (using the advanced debug mode).(I
As long as I  am visiting a minipage, the variables are echoed, but on no other pages.. So it seems the query is working fine (I also confirmed this using the JError::raiseNotice( 'ERR', "message here" ); you suggested), and the session method works. However the results are not saved. Or, the session seems to expire as soon as I visit another page.

My impression was that the data in the session would be available as long as the user remained on the site. -Though, I may very well be mistaken.

Another way to go would perhaps be to try setcookie(). I experimented a bit with this method as well and got some very strange results.(once again, have in mind my limited experience). I set the cookie using metatemplate.

First I tried just setting a name "john smith" as the value. like so: $expire=time()+60*60*24*30;
setcookie("user", "john smith", $expire);
And echo $_COOKIE["user"]; in metamod;

The cookie it seems is set too late for metamod to pick it up on the first visit, but on every page after that, the name is echoed.
Now, if I substitute the name with the query result, $mod1, the value is displayed on the second page that is being visited, but then mysteriously disappears. Viewing the cookies that are set in safari, it is now gone..

I am sure it is possible to solve my problem somehow, but I am really not having any luck here I´m afraid.

apollo123
Beginner Modder
ranks
useravatar
Offline
17 Posts
Administrator has disabled public posting

Re: MetaTemplate/metamod/session

Hmmm, I see an error that I made above.

Instead of this:

$session = JFactory::getSession();

use this:

$session =& JFactory::getSession();

That goes when you are setting the sessions in MetaTemplate, and retrieving them in MetaMod.

See if that helps...

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3313 Posts
User info in posts
Administrator has disabled public posting

Re: MetaTemplate/metamod/session

No, I´m afraid not..the result remains the same.

apollo123
Beginner Modder
ranks
useravatar
Offline
17 Posts
Administrator has disabled public posting

Re: MetaTemplate/metamod/session

Hmmm, that's pretty strange.

Can you check all your cookie setup in your browser? If you can't even set/accept cookies then there's little that MetaMod can do to remember values in sessions, since the session key is held in a cookie.

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3313 Posts
User info in posts
Administrator has disabled public posting

Re: MetaTemplate/metamod/session

I have set the accept all cookies in Safari and I have also tried using Firefox, the result is the same.
I don´t think MAMP may be the culprit here, but just incase, I will try to upload the site to a host and see if that changes anything. I will report my findings here:)

BTW: Thank you for your patience, chances are I am doing something wrong, so I really appreciate that you are  trying to help.

apollo123
Beginner Modder
ranks
useravatar
Offline
17 Posts
Administrator has disabled public posting

Re: MetaTemplate/metamod/session

Good news->I got it working:)smile
Bad news->Once again my limited programming skills were to blame...

Here´s the working metatemplate rule:
if ($option == 'com_gmapfp'
and $view == 'gmapfp'
and $id != ''){ //#####################I OMITTED THE BRACKET#########
$dd = JRequest::getVar('id');
$de = strrchr($dd, 58);
$df = substr($de,1);
$df = $db->Quote( $db->getEscaped( $df ) ); // escapes it and quotes
$query="SELECT * FROM sgj_gmapfp WHERE alias = $df ";
$result = mysql_query($query);
$mod1 = mysql_result($result, $i, 'mod1');
$mod2 = mysql_result($result, $i, 'mod2');
$temp = mysql_result($result, $i, 'temp');
$session =& JFactory::getSession();
$namespace = "gmapfp";
$session->set('firstmod', $mod1, $namespace );
$session->set('secondmod', "$mod2" , $namespace );
$session->set('template', $temp, $namespace );
JError::raiseNotice( 'ERR', "temp: $temp, mod1: $mod1, mod2: $mod2, id: $dd, Hallllllllead");
return true;}// ############ FORGOT THIS BRACKET AS WELL ###########

This rule sets sessionVariable A which is then picked up by the next rule, to keep the template throughout the session.

Picking up and setting the variables in metamod now works like a charm:)

However, one problem remains;
I don´t want the PHP rule to return true, but rather the template value I have retrieved.

-Doing this: return $temp;, returns the following error;
Fatal error: Call to undefined method JText::strintf() in /home/xsprtest/public_html/components/com_metatemplate/helper.php on line 768.

Or, simply hardcoding a template like this-> return "beez";, has the same result.

Might this be a feature reserved for the Pro version? -Is the syntax wrong, or is this a bug?

Either way, please let me know.

BTW: Metatemplate/Metamod is suuuuupercoool:) It is truely amazing what can be achieved using this software.

apollo123
Beginner Modder
ranks
useravatar
Offline
17 Posts
Administrator has disabled public posting

Re: MetaTemplate/metamod/session

Oh, what a bummer sad  Looks like there's a simple typo in MetaTemplate that I didn't pick up during testing the last version.

I'm away for the weekend so can't fix this immediately - but you can probably fix it yourself...

edit the file /home/xsprtest/public_html/components/com_metatemplate/helper.php on line 768, and replace the word "strintf" with "sprintf".

Hopefully I didn't leave any other bugs in there...

Sorry!
Stephen

ps. well done on getting the rest of it working - it's a very cool application for MetaMod and MetaTemplate!

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3313 Posts
User info in posts
Administrator has disabled public posting

Re: MetaTemplate/metamod/session

Now it´s working flawlessly...coool:)
Hehe, for someone as typo-prone as me, it´s good to see that others also make the occasional mishap. 

As always, thank you for being so helpful:)

Hope you enjoy your weekend.

apollo123
Beginner Modder
ranks
useravatar
Offline
17 Posts
Administrator has disabled public posting

Board Info

Board Stats:
 
Total Topics:
1667
Total Polls:
6
Total Posts:
5910
Total Posts Today:
1
User Info:
 
Total Users:
10144
Newest User:
leonard5412396
Members Online:
2
Guests Online:
224


Forum Legend:

 Topic
 New
 Locked
 Sticky
 Active
 New/Active
 New/Locked
 New Sticky
 Locked/Active
 Active/Sticky
 Sticky/Locked
 Sticky/Active/Locked