We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36764 ☆ A M B ☆
    • 45 Posts
    Evengard aka Eiventeleiron Reply #1, 15 years, 5 months ago
    So, the question is "how setOption" works?
    Is it saving the data to the database, is it using just for the current session, or whatever?

    What I want to do, is to check if the user is logged in, and if yes, do nothing (use the user’s settings, which are got by getOption and are overriden from system settings and context settings), if no, set an option of a current user (basically a cultureKey) until it logs in or leaves the site - basically until he is on the site. Something like a $_SESSION storage. So, what I need to know is if setOption is just a temporary setting only for the current request, for the whole session or for the whole context/user/system for ever...

    Also, how to do a save of a logged in user’s option into database? A creation of a new setting or an updating of an existing one.

    In final what I want to do, is a dropdown list of languages, from which a user can choose the one he prefer. If a non-logged in user choose one - apply it only during the current session until he logs in or leave the site. If a logged in user choose one - save it as a user setting.
      • 22303 MODX Staff
      • 10,725 Posts
      In this case, I would not use User Settings to override the System/Context Settings the provide a default value. Instead, I would use your own $_SESSION and/or $_COOKIE and/or $_REQUEST variables to override System/Context Settings for this. The problem is User Settings for anonymous users don’t work the same and setOption is only a temporary change for the current request cycle (it does not change any values in database OR session).

      Here is an example of how to override using a SESSION variable and System Setting called ’lang’
      <?php
      $lang = $modx->getOption('lang', $_SESSION, 'en');


      Or similarly, to override by COOKIE, GET or POST values...
      <?php
      $lang = $modx->getOption('lang', $_REQUEST, 'en');


      Then all you need to do is set the appropriate SESSION, COOKIE, GET, or POST variable to override System/Context settings, and provide a default in the absence of the Setting.
        • 36764 ☆ A M B ☆
        • 45 Posts
        Evengard aka Eiventeleiron Reply #3, 15 years, 5 months ago
        Ok, thank you, I got all the info I needed!

        setOption is temporary, just for one request, but across the session it is possible to use $_SESSION, this is what I wanted to know.

        Just a question - what is the best solution to check if a user is logged in? Is it $modx->user->isAuthentificated($modx->context->get("key"))?

        And do you recommend storing the language as a user setting (except for the anonymous one, if yes - how to do it for the current user? Or the method is the same as for editing from withing manager a user setting - so I can get that info from sources?) or as an extended field?
          • 36764 ☆ A M B ☆
          • 45 Posts
          Evengard aka Eiventeleiron Reply #4, 15 years, 5 months ago
          Any advice about where to store the language settings of the user? In settings or as an extendedfield?

          They should be editable through Login.Profile extra
            • 36764 ☆ A M B ☆
            • 45 Posts
            Evengard aka Eiventeleiron Reply #5, 15 years, 5 months ago
            Anyone any advice? (
              • 22303 MODX Staff
              • 10,725 Posts
              Language settings are generally set as User Settings with Context and System defaults providing the default. You can do it anyway you like, but User Settings would automatically be available when the user logs in. I have not used the Login.Profile extra so I cannot speak to it being editable there...
                • 36764 ☆ A M B ☆
                • 45 Posts
                Evengard aka Eiventeleiron Reply #7, 15 years, 5 months ago
                Thank you... Another question - how can I get the current system+context+user setting for another user? Just to know the default one in the system/context scope I wish to get the data for, for example, the anonymous user, which will always follow the default one.
                Or I should request by something like that (stub code):
                $default="";
                $userid=0;
                if(getUserSetting("mySetting", $userid))
                {
                $default=getUserSetting("mySetting", $userid);
                }
                elseif(getContextSetting("mySetting"))
                {
                $default=getContextSetting("mySetting");
                }
                elseif(getSystemSetting("mySetting"))
                {
                $default=getSystemSetting("mySetting");
                }

                ?
                  • 36764 ☆ A M B ☆
                  • 45 Posts
                  Evengard aka Eiventeleiron Reply #8, 15 years, 5 months ago
                  any advice?... cause this way is really ugly, there should be a way to accomplish that easier...
                    • 3749
                    • 24,544 Posts
                    If it’s for the current user, you can use this:

                    $setting = $modx->getOption('mySetting', null, "");


                    If it’s for some other user, I think the code you have is necessary.
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 36764 ☆ A M B ☆
                      • 45 Posts
                      Evengard aka Eiventeleiron Reply #10, 15 years, 5 months ago
                      Ok, thank you alot!