The Joomla! Forum ™





Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 111 posts ]  Go to page 1, 2, 3, 4  Next
Author Message
PostPosted: Thu Nov 16, 2006 5:54 am 
User avatar
Joomla! Guru
Joomla! Guru

Joined: Thu Feb 09, 2006 1:41 am
Posts: 812
Location: Auckland, NZ
Updated 01. May 2007

This hack has moved from the mosLoadModules function to the &initModules function since that one is being called first by all the other functions. This will allow for column collapsing if the module is hidden and no other modules are published in the position.

Credits go to BradWaite for pointing out to modify &initModules to collapse modules.

You only need this hack in one place, in function: &initModules()!!!!

For a more elaborate hack allowing hiding of modules for certain user types see my post further down: http://forum.joomla.org/index.php/topic ... #msg795120


[This hack is done in J! 1.0.12]

I had a situation where i had a menu that I wanted to replace with the User menu once a user was logged in. In other words I wanted to hide that public menu when a user logged in.

This is such a simple hack that I don't see why this couldn't be part of the core. It involves adding a parameter to the module's xml file (in my case /modules/mod_mainmenu.xml but you can add this parameter to any module you want to hide.

Simply insert this in the position you want in the module.xml file:
Code:
      <param name="show_registered" type="radio" default="1" label="Show Menu When Logged In" description="Show the menu also when a registered user is logged in">
         <option value="0">No</option>
         <option value="1">Yes</option>
      </param>


Then open up: /includes/frontend.php and find the function &initModules() around line 72. In there you should see this code:
Code:
   foreach ($modules as $module) {
      $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
   }

Insert immediately before the line with $GLOBALS[... the following code:
Code:
               // HACK: added code to check if module should be shown when a user is logged in
      $params = new mosParameters( $module->params );
      if (!$params->get('show_registered', 1) && $my->id) {
         // simply continue the loop without outputting this module
         continue;
      }
      // --- end HACK ---

This will work for modules that don't have that parameter defined as it will default to 'display' (or 1) the module when logged in as per normal functionality.

Now you can go into your module configuration and set the radio button to 'No' for the module you want to hide from logged in users.

Enjoy.


Last edited by phlux0r on Mon May 14, 2007 9:35 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Thu Nov 16, 2006 10:16 pm 
I've been banned!

Joined: Sat Sep 03, 2005 3:37 pm
Posts: 143
you sir, deserve a medal ( or a swift kick in the pants for not doing this earlier!) good job!

very elegantly done!


Top
 Profile  
 
PostPosted: Sat Nov 18, 2006 6:11 am 
User avatar
Joomla! Master
Joomla! Master

Joined: Thu Aug 18, 2005 10:41 am
Posts: 15752
Moderator note; moved from FAQ >> 3rd Party, Hacks/Patches

_________________
Regards Robin

http://www.linkedin.com/in/robinmuilwijk - http://twitter.com/i_robin


Top
 Profile  
 
PostPosted: Sat Nov 25, 2006 10:38 am 
Joomla! Intern
Joomla! Intern

Joined: Thu Aug 18, 2005 3:36 am
Posts: 78
Hi phlux0r,

Do you have the same implementation for menu items?

I mean, some menu items must be visible to public but should disappear once logged on...

Thank you!

quickwolf ;D


Top
 Profile  
 
PostPosted: Sun Nov 26, 2006 1:49 am 
User avatar
Joomla! Guru
Joomla! Guru

Joined: Thu Feb 09, 2006 1:41 am
Posts: 812
Location: Auckland, NZ
Hi quickwolf,

sorry, but I didn't need that kind of functionality so I haven't looked at hacking that.


Top
 Profile  
 
PostPosted: Sun Nov 26, 2006 6:08 pm 
I've been banned!

Joined: Sat Sep 03, 2005 3:37 pm
Posts: 143
QuickSync wrote:
Hi phlux0r,

Do you have the same implementation for menu items?

I mean, some menu items must be visible to public but should disappear once logged on...

Thank you!

quickwolf ;D


it would seem to me, that if you place all your menu items that should be visible for PUBLIC but invisible for LOGGED IN, you could (should) put all those menu items together in one menu module, then apply the Logged In/out hack to that module.


Top
 Profile  
 
PostPosted: Wed Jan 24, 2007 12:57 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Dec 07, 2006 6:09 am
Posts: 10
This works perfect!!!!!!!!!  Thanks  ;D


Top
 Profile  
 
PostPosted: Sun Jan 28, 2007 8:37 am 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Mon Nov 06, 2006 10:18 am
Posts: 135
Location: Jerusalem, Israel
can i use this for 1.0.12 ?


Top
 Profile  
 
PostPosted: Sun Jan 28, 2007 8:38 am 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Mon Nov 06, 2006 10:18 am
Posts: 135
Location: Jerusalem, Israel
toddrip wrote:
This works perfect!!!!!!!!!  Thanks  ;D


toddrip,

are you using 1.0.12 ?


Top
 Profile  
 
PostPosted: Sun Jan 28, 2007 11:42 am 
User avatar
Joomla! Guru
Joomla! Guru

Joined: Sun Aug 21, 2005 8:46 am
Posts: 575
Location: Perth, Western Australia
I am using it for 1.0.12 and works fine so far for menus. Have not tried it for other modules

_________________
Joomla! - enjoying every minute of the journey!


Top
 Profile  
 
PostPosted: Sun Jan 28, 2007 12:31 pm 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Mon Nov 06, 2006 10:18 am
Posts: 135
Location: Jerusalem, Israel
thanks for the response.

what i need this function for is i have a "become a member" link, and i didnt want it to show to people logged in... cause thats kinda dumb. AND, i could use the space for something else.
so this is what i did.

Code:
if (!($my->id))
{    echo "<a href='/component/option,com_comprofiler/task,registers/' class='help'>Become a member for FREE! </a>"; }


very simple. i didnt use the hack. this was good enough. and i am more comfortable writing it this way.


Top
 Profile  
 
PostPosted: Sun Jan 28, 2007 9:42 pm 
User avatar
Joomla! Guru
Joomla! Guru

Joined: Thu Feb 09, 2006 1:41 am
Posts: 812
Location: Auckland, NZ
You should use the sefRelToAbs() function for URLs so it will work with any SEF component and SEF off. You code only works when you have native SEF enabled... so instead of:

Code:
if (!($my->id))
{    echo "<a href='/component/option,com_comprofiler/task,registers/' class='help'>Become a member for FREE! </a>"; }


write:

Code:
if (!($my->id))
{    echo "<a href='<?php sefRelToAbs("index.php?option=com_comprofiler&task=registers"); ?>' class='help'>Become a member for FREE! </a>"; }


Top
 Profile  
 
PostPosted: Mon Jan 29, 2007 7:32 am 
User avatar
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Mon Nov 06, 2006 10:18 am
Posts: 135
Location: Jerusalem, Israel
phlux0r wrote:
You should use the sefRelToAbs() function for URLs so it will work with any SEF component and SEF off. You code only works when you have native SEF enabled... so instead of:

Code:
if (!($my->id))
{    echo "<a href='/component/option,com_comprofiler/task,registers/' class='help'>Become a member for FREE! </a>"; }


write:

Code:
if (!($my->id))
{    echo "<a href='<?php sefRelToAbs("index.php?option=com_comprofiler&task=registers"); ?>' class='help'>Become a member for FREE! </a>"; }




thanks for the tip. i put this code in my template. and im not sure why but i cant get your code to work.
when your code is in my template, i get an error.
unexpected "
so i replaced " with '
but now the php doesnt get read. how do i write this for my template?
thanks!
nate


Top
 Profile  
 
PostPosted: Wed Mar 28, 2007 5:15 pm 
Joomla! Intern
Joomla! Intern

Joined: Fri Jan 27, 2006 12:29 pm
Posts: 54
Location: Jakarta
Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in /home/mysite/public_html/smunsa/templates/themeku/index.php on line 11

_________________
Audio Indonesia
Joseph Manurung

Good Leaders are Good Followers


Top
 Profile  
 
PostPosted: Thu Mar 29, 2007 10:47 pm 
User avatar
Joomla! Intern
Joomla! Intern

Joined: Sun Nov 06, 2005 12:57 am
Posts: 90
The module hack is just perfect.  Thanks so much!!  :D

_________________
Learn English Online at Open English World: http://www.OpenEnglishWorld.com
Teach English as a Second Language at ESL Jobs World : http://www.ESLJobsWorld.com


Top
 Profile  
 
PostPosted: Sat Mar 31, 2007 12:11 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Wed Feb 07, 2007 9:16 pm
Posts: 2
i've got another good one, to which I have to give phlux0r the credit  8).

How about a hack for "Hide a module when your site is in SSL or on the secure SSL site for payments systems" - here ya go...


Simply insert this in the position you want in the module.xml file:


Code:
      <param name="hide_for_ssl" type="radio" default="0" label="Hide Menu When SSL is active" description="Hide the menu when the SSL is active">
         <option value="0">No</option>
         <option value="1">Yes</option>
      </param>


Then open up: /includes/frontend.php and go to line 155. You should see this code:
Code:
   foreach ($modules as $module) {
      $params = new mosParameters( $module->params );


Insert immediately after that the following code:
Code:
      // HACK: added code to check if module should be shown when the user is in SSL
      if ($params->get('hide_for_ssl', 0) && substr( $mosConfig_live_site, 0, 8 ) == 'https://' ) {
      // simply continue the loop without outputting this module
      continue;
      }
      // --- end HACK ---



This will work for modules that don't have that parameter defined as it will default to 'display' (or 1) the module when logged in as per normal functionality.

Now you can go into your module configuration and set the radio button to 'No' for the module you want to hide from logged in users.

Enjoy.


Top
 Profile  
 
PostPosted: Sat Mar 31, 2007 8:12 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Wed May 17, 2006 6:44 am
Posts: 6
Hi,

I slightly changed the second piece of code, because josgall's version didn't worked for me:
Code:
// HACK: added code to check if module should be shown when the user is in SSL
if ($params->get('hide_for_ssl', 0) && ($_SERVER['HTTPS']=='on')) {
// simply continue the loop without outputting this module
continue;
}
// end HACK


Thank you josgall and phlux0r  :)

Boaz


Top
 Profile  
 
PostPosted: Sun Apr 08, 2007 9:31 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Sep 05, 2006 8:39 pm
Posts: 8
Excellent hack, Phlux0r!

I've made some modifications to the placement so it works with templates that collapse when a module isn't present.

Replace the following code at line 94 of frontend.php

Code:
                foreach ($modules as $module) {
                        $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
                }      



with this:


Code:
                foreach ($modules as $module) {
                        // HACK: added code to check if module should be shown when a user is logged in
                        $params = new mosParameters( $module->params );
                        if (!$params->get('show_registered', 1) && $my->id) {
                                // simply continue the loop without counting this module
                                continue;
                        }      
                        // --- end HACK --
                        $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
                }      


Combine this hack with the mosviewonly/moshideonly mambots from http://www.troozers.com/ and you can really customize your site for registered users.


Top
 Profile  
 
PostPosted: Sat Apr 21, 2007 6:08 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Jan 09, 2007 7:39 am
Posts: 6
This is so usefull that should be in every module and in the default installation. I expected to found it without hacking anything ... it seems a common need for me.


Top
 Profile  
 
PostPosted: Sat Apr 21, 2007 5:59 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Sep 05, 2006 8:39 pm
Posts: 8
For you unix guys, here's a quick perl script to add this parameter to every module.  Just put this into a file called "addparm" and run "addparm *.xml" from your modules/ dir.

Code:
#!/usr/bin/perl -pi

$addparm = qq|\t\t<param name="@spacer" type="spacer" default="" label="" description="" />\n|;
$addparm .= qq|\t\t<param name="show_registered" type="radio" default="1" label="Show to registered users" description="Show this module when a registered user is logged in">\n|;
$addparm .= qq|\t\t\t<option value="0">No</option>\n|;
$addparm .= qq|\t\t\t<option value="1">Yes</option>\n|;
$addparm .= qq|\t\t</param>\n|;

if( /<\/params>/){
        print $addparm;
}


Top
 Profile  
 
PostPosted: Mon Apr 30, 2007 9:31 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Mar 08, 2007 10:46 am
Posts: 10
Hi !

thanks for the great hack! it was EXACTLY what i was looking for, both the first hack and the last hack before the unix one

could you add one more "feature"? I was just wondering if it was possible to hide a certain module only if a super administrator was logged in, instead of any registered user? Or it could technically be any specific level of a user, and when that user is logged in, that module will dissapear and the spot on the template will collapse?

If you could do it, it would be great, if not, it works great anyways!

So thanks in advance!

Miyuki


Last edited by meekochan on Mon Apr 30, 2007 9:33 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Tue May 01, 2007 1:52 am 
User avatar
Joomla! Guru
Joomla! Guru

Joined: Thu Feb 09, 2006 1:41 am
Posts: 812
Location: Auckland, NZ
a) I've updated my original hack so check it out please to see what has changed.

b) Ok, nice challenge meekochan!

This is for Joomla! 1.0.12

Here we go...

To add the possibility of hiding modules for a certain user group (i.e. Authors, Publishers, Managers etc.) this is what is needed:

1) In your /modules/[whatever_module].xml you need to add the following somewhere within the section:

Code:
      <param name="show_registered" type="radio" default="1" label="Show Menu When Logged In" description="Show the menu also when a registered user is logged in">
         <option value="0">No</option>
         <option value="1">Yes</option>
      </param>
      <param name="hide_for_gid" type="list" default="0" label="Hide Module for this User Type Only" description="If above is set to 'No', you can specify which registered user type you want to hide this module for. Default hides the module for any logged in user.">
         <option value="0">Default</option>
         <option value="18">Registered</option>
         <option value="19">Author</option>
         <option value="20">Editor</option>
         <option value="21">Publisher</option>
         <option value="23">Manager</option>
         <option value="24">Administrator</option>
         <option value="25">Super Administrator</option>
         <option value=">19">Author and above</option>
         <option value=">20">Editor and above</option>
         <option value=">21">Publisher and above</option>
         <option value=">23">Manager and above</option>
         <option value=">24">Administrator and above</option>
         <option value="<19">Author and below</option>
         <option value="<20">Editor and below</option>
         <option value="<21">Publisher and below</option>
         <option value="<23">Manager and below</option>
         <option value="<24">Administrator and below</option>
      </param>


2) Then in /includes/frontend.php in function &initModules() on line 73 change the global line to:

Code:
   global $database, $my, $Itemid, $acl;

(added $acl to the variables)

3) Further down in the same function, replace this code:
Code:
      foreach ($modules as $module) {
         $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }


with this code:
Code:
      // HACK: to hide modules for certain groups of logged in users
      $myRealGid = intval($acl->get_group_id($my->usertype));
      // --- end HACK ---
      foreach ($modules as $module) {
         // HACK: added code to check if module should be shown when a user is logged in and is in a particular group
         $params = new mosParameters( $module->params );
         if (!$params->get('show_registered', 1) && $my->id) {
            // check the gid if param set
            if ($params->get('hide_for_gid', 0) != '0') {
               if (strlen($params->get('hide_for_gid')) > 2) {
                  // if we're looking at ranges, extract the comparator and the gid to compare
                  $comparator = substr($params->get('hide_for_gid'), 0, 1);
                  $hideGid = intval(substr($params->get('hide_for_gid'), 1));

                  if (($comparator == ">" && $myRealGid >= $hideGid) || ($comparator == "<" && $myRealGid <= $hideGid)) {
                     continue;
                  }

               } elseif (intval($params->get('hide_for_gid')) == $myRealGid) {
                  // if the user's gid is equal to the parameter, hide the module
                  continue;
               }
            } else {
               // simply continue the loop without outputting this module
               continue;
            }
         }
         // --- end HACK ---
         $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }


And it's done. You can then set for which kinds of registered users you want the module to be hidden. The parameter list allows you to specify the exact level (i.e. Authors only) or a range like "hide for Editors and above" or "hide for Managers and below" which hides the module for that level user and above/below.

Hope this is useful to some of you, as always, enjoy.

Edit: Just realised that the option: "Registered and below" doesn't make sense (cos it's the same as "Default") so I removed it :).


Last edited by phlux0r on Tue May 01, 2007 9:58 am, edited 1 time in total.

Top
 Profile  
 
PostPosted: Tue May 01, 2007 7:04 am 
User avatar
Joomla! Master
Joomla! Master

Joined: Thu Aug 18, 2005 10:41 am
Posts: 15752
Looks very interesting, I'm sure going to check this out!

_________________
Regards Robin

http://www.linkedin.com/in/robinmuilwijk - http://twitter.com/i_robin


Top
 Profile  
 
PostPosted: Tue May 01, 2007 7:40 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Mar 08, 2007 10:46 am
Posts: 10
Hi! Thanks for going extra and even adding below and above, that was a great idea. I couldn't ask for more!

And I tried it, and it works great. Thank you so much!


Top
 Profile  
 
PostPosted: Tue May 01, 2007 10:05 am 
User avatar
Joomla! Guru
Joomla! Guru

Joined: Thu Feb 09, 2006 1:41 am
Posts: 812
Location: Auckland, NZ
Quote:
Hi! Thanks for going extra and even adding below and above, that was a great idea. I couldn't ask for more!


Not a problem... it was a nice addition to the hack and not really all to difficult.

If one wanted to be really fancy, one could create a new parameter type for the section say type="acl_group_list" and get it populated with the ACL groups from the database. At the same time making the list multi select (dunno if you could even have multiple values for a parameter - may need some thought) so you could pick which groups to hide the module for. That way this would work even with the JACLPlus extension/hack...


Top
 Profile  
 
PostPosted: Wed May 09, 2007 6:17 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Mon Feb 05, 2007 9:52 pm
Posts: 2
This is just what I have been trying to sort out for over 2 weeks now since installing my SSL!

Mucho thanks!


Top
 Profile  
 
PostPosted: Mon May 14, 2007 7:47 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Fri Jul 14, 2006 4:50 pm
Posts: 9
Hey, I am having problems implementing this hack, I can add the radio button to the XML fine but in the frontend.php whenever i change it from
This:
Code:

foreach ($modules as $module) {
$GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }



To this:

Code:

foreach ($modules as $module) {

// HACK: added code to check if module should be shown when a user is logged in
if (!$params->get('show_registered', 1) && $my->id) {
// simply continue the loop without outputting this module
continue;
}
// --- end HACK ---

$GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }



The website just displays a blank white screen.
I'm kind of new to coding so any help is greatly appreciated


Last edited by jce108 on Mon May 14, 2007 7:58 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Mon May 14, 2007 9:38 pm 
User avatar
Joomla! Guru
Joomla! Guru

Joined: Thu Feb 09, 2006 1:41 am
Posts: 812
Location: Auckland, NZ
Yep, there's a mistake in the hack snippet in the irst post (I fixed it now).

You need to add:

Code:
$params = new mosParameters( $module->params );

before the if statement. So your replacement should look like:
Code:
foreach ($modules as $module) {

// HACK: added code to check if module should be shown when a user is logged in
$params = new mosParameters( $module->params );
if (!$params->get('show_registered', 1) && $my->id) {
// simply continue the loop without outputting this module
continue;
}
// --- end HACK ---

$GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }


Top
 Profile  
 
PostPosted: Tue May 15, 2007 3:28 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Sep 01, 2005 7:47 pm
Posts: 19
Location: Lincoln UK
I am developing a site that uses "Tabs & Slides In Content Items Plugin" from Joomlaworks and I would like to be able to hide a module when one of the tabs is opened.

I was just wondering if a variation of this hack would do the trick?


Top
 Profile  
 
PostPosted: Sat Jun 23, 2007 2:29 pm 
Joomla! Guru
Joomla! Guru

Joined: Tue Oct 10, 2006 4:09 pm
Posts: 927
@quizzer
tabs and slides is for a content not for a module  ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 111 posts ]  Go to page 1, 2, 3, 4  Next



Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group