$context['current_action'] alternative in loadUserSettings() in Load.php?

Started by Neol, March 06, 2007, 01:11:19 PM

Previous topic - Next topic

Neol

Here's the code at the end of the function loadUserSettings() in Load.php:


  else
    $user_info['query_see_board'] = '(FIND_IN_SET(' . implode(', b.memberGroups) OR FIND_IN_SET(', $user_info['groups']) . ', b.memberGroups))';



After that, I need to add some code like this:


  if ($context['current_action'] === $myaction)
    $user_info['query_see_board'] .= ' AND (some_sql_here)';


I think I cannot use $context in this function (correct me if I'm wrong). So, is there an alternative?

Please help, I'm making a mod package that I guess will be quite popular, and I'm stuck at the code above. Pretty much all the rest of my mod is already written.




For reference, my mod is a conversion of Shared Forum Mod by Goosemoose from Joomla+SMF to pure SMF.

The original code uses $_REQUEST['action'] instead of $context['current_action']. I can't use that, because the action parameter value has to be retained even when the user navigates away from the page with ?action= in the URL (hence $context['current_action']).

The list of specific values that my code looks for in $context['current_action'] is defined in the $modSettings['mymod...'] array.

Rudolf

Quote from: Zeri YT! on March 06, 2007, 01:11:19 PM
The original code uses $_REQUEST['action'] instead of $context['current_action']. I can't use that, because the action parameter value has to be retained even when the user navigates away from the page with ?action= in the URL (hence $context['current_action']).

If the user navigates away the $context['current_action'] will change too. Using it is like using $_REQUEST['action']. The $context variable is mostly used for the templates, you fill it with stuff that later you will use in the templates. So theme writers won't have to mess with the system variables.
If you need to retain states even between different page loads you 'll have to use session variables.
I will update all my mods in the next few weeks. Thanks for your patience.

SVG-Collapse (you need an SVG compliant browser)

Neol

Quote from: Rudolf on March 06, 2007, 03:54:17 PM
If you need to retain states even between different page loads you 'll have to use session variables.

And how do I do that?

I gave it some further thought. It seems to me that I have to declare a global variable in index.php, because the variable has to get the value of $_REQUEST['action'], like $context['current_action'] does. But its value also has to be persistent until the next index.php?action= URL, and I need to use it inside the function loadUserSettings() in Load.php; I can't do these with $context['current_action'].

What do you think? Do I have to declare a new global variable in index.php and use that variable in Load.php (and also BoardIndex.php - because I need to add the same code there)?

Rudolf

I'm still having problems understanding what you want.
If I understand good you want to know what was the previous action?
Example:
User clicks on the 'Profile' Link => the action is profile
Then the user clicks on "My Messages" => the action is pm. Here you want to know that the user 'came' from the Profile page?

However, your examples are nothing like this.

It seems to me that you just want to know what the current action is. For that just use the $_REQUEST['action'] variable. That's a global variable and it's available everywhere, and it is the current action.
I will update all my mods in the next few weeks. Thanks for your patience.

SVG-Collapse (you need an SVG compliant browser)

Neol

Let me make myself clearer:

Read this topic: http://www.simplemachines.org/community/index.php?topic=155827.0

Quote from: akabugeyes on March 06, 2007, 03:29:26 PM
$context['current_action'] is only set when there is an action set in the url. Such as index.php?action=profile. As soon as there is no longer an action in the url, it is no longer set.

My mod needs to know what the last action was, whether it's specified in the current URL or not. So I'm thinking of creating a new global variable that will initially be empty; every time an action is specified in the URL, the variable value will be set to the action URL parameter value. This variable will hold the action value even when there is no action specified in the URL - which is why I can't use $context['current_action'].

Do you know where should I declare such a variable, so that it will be accessible throughout SMF? Maybe $modSettings['mymod_current_action']?

Also, where is $context['current_action'] set? I need to set my variable at the same time too.

karlbenson

depends where you need it and whether you have access to it.  For example the place you need to use the variable may be before the modSettings are set up.

Rudolf

QuoteAfter that, when the user navigates away to a non-action page, like index.php or index.php?board=9.0, is 'profile' still in $context['current_action'], at least until the user visits another action page?
The $contex['current_action'] is empty since there's no action.
To have whatever was before, you need to use a SESSION variable - if it's enough to have it until the session ends, meaning that you don't care about what the value was when a person leaves the forum, turns off the computer and return two hours later. IF you only need to know the value for every browsing session then that's the way.

Where the $context['current_action'] is defined(Load.php - loadTheme() function) add after:

$_SESSION['last_action'] = !empty($context['current_action']) ? $context['current_action'] : isset($_SESSION['last_action']) ? $_SESSION['last_action'] : '';

After that you can use $_SESSION['last_action'] all around the script, and it will have the last action set (retained from previous loads if no new action is set).

What does this code? If the $context['current_action'] is not empty it will pass the value of the $_SESSION['last_action']. If it is empty it will check if you have already a 'last_action' in your SESSION from a previous page load. If it finds one it will keep the value, else it will initiate it as empty string.
This way you can use it anywhere without worrying about undefined index warnings and such.
I think this is what you're after, hope it helps.
I will update all my mods in the next few weeks. Thanks for your patience.

SVG-Collapse (you need an SVG compliant browser)

Neol

Thanks again for replying, Rudolf.

Quote from: Rudolf on March 07, 2007, 04:04:21 PM
The $contex['current_action'] is empty since there's no action.
To have whatever was before, you need to use a SESSION variable - if it's enough to have it until the session ends, meaning that you don't care about what the value was when a person leaves the forum, turns off the computer and return two hours later. IF you only need to know the value for every browsing session then that's the way.

That should be fine. Thanks for the suggestion. :) But I'm curious: if I want the value to be preserved even between sessions, do I have to set $user_settings['last_action'] (and add a table column in the database) for that? This might be better, considering the following:


Quote from: Rudolf on March 07, 2007, 04:04:21 PM
Where the $context['current_action'] is defined(Load.php - loadTheme() function) add after:

$_SESSION['last_action'] = !empty($context['current_action']) ? $context['current_action'] : isset($_SESSION['last_action']) ? $_SESSION['last_action'] : '';

After that you can use $_SESSION['last_action'] all around the script, and it will have the last action set (retained from previous loads if no new action is set).

What does this code? If the $context['current_action'] is not empty it will pass the value of the $_SESSION['last_action']. If it is empty it will check if you have already a 'last_action' in your SESSION from a previous page load. If it finds one it will keep the value, else it will initiate it as empty string.
This way you can use it anywhere without worrying about undefined index warnings and such.
I think this is what you're after, hope it helps.


I think that the code you posted should work. There's only one possible issue: The variable is used in my mod's code in loadUserSettings(), where the $context array is not even defined -- it is defined in loadBoard(). And loadTheme() is called after loadUserSettings() in index.php:


  // Load the user's cookie (or set as guest) and load their settings.
  loadUserSettings();

  // Load the current board's information.
  loadBoard();

  // Load the current theme.  (note that ?theme=1 will also work, may be used for guest theming.)
  loadTheme();


Should it be OK to use your code in loadUserSettings()? That's where I need to add my code. Please see the first post.

Neol

The suggestion to use a session variable worked fine for me. Thanks Rudolf!

Advertisement: