We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17016
    • 138 Posts
    Quote from: BobRay at Mar 27, 2011, 06:18 AM

    The user is defined, but you need to get the user profile:
    <?php
    $profile = $user->getOne('Profile'); 
    $profile->get('fax');
    $newFax = '123-231-2211';
    $profile->set('fax', $newFax);
    $profile->save();
    

    Unfortunately this leads to the following error in our snippet:

    Fatal error: Call to a member function getOne() on a non-object in [...] on line 7

    Line 7: $profile = $user->getOne(’Profile’);

    Do you have any idea what could be the reason for this?

    Beside this: How does the snippet know which user-profile should be retrieved? At the moment the snippet (or plugin) runs, the user will not be logged in because this snippet/plugin will run while the registering process and not after login.
      • 3749
      • 24,544 Posts
      Sorry, the code should work in a plugin during the registration process but not in a snippet for the reasons you mention.

      If you want to test it in a snippet, you’d have to use $modx->user instead of $user and make sure a user is logged in first.

      To actually do what you’re describing, a plugin is definitely the way to go.
        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
        • 17016
        • 138 Posts
        Hi BobRay,

        the test-snippet now works very well. Still not working is the plugin. For test-reasons I tried just this simple line:

        <?php
        $user->joinGroup(’Members’);


        The plugin is listen to "OnUserFormSave". But new users are not added to the group "Members" which I defined before. Is there anything I forgot to add?
          • 3749
          • 24,544 Posts
          Sorry, I can’t think of any reason why that wouldn’t work.
            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
            • 4172
            • 5,888 Posts
            Quote from: Letti at Mar 23, 2011, 03:31 PM

            Is it possible to assign a user automatically to a certain usergroup depending for example on his name or a number he types in while the register-process or profile-update? All the names and numbers which should be assigned to "Group A" will be stored by us in a database.

            Is this feasible?

            Letti



            what is the reason, they have to register themselves. If you have their names and emails in a database you can add them at once to the user-table and let modx send a mail with their login-details.
            example-script:
            <?php
            $object = $modx->getObject($classname, $scriptProperties['object_id']);
            $userfields = array(
            'username' => $object->get('name'), 
            'fullname' => $object->get('name'), 
            'email' => $object->get('email'), 
            'groups' => json_encode(array(array('usergroup' => $config['usergroup'],'role' => $config['userrole'])))
            , );
            $userid=$object->get('userid');
            if (empty($userid)) {
                //add user to user-table
                $user = $modx->runProcessor('security/user/create', array_merge($userfields,array(
            	'newpassword' => true, 
            	'passwordnotifymethod' => 'e', 
            	'passwordgenmethod' => 'g', 
            	'class_key' => 'modUser', 
            	)));
                /*  Above creates user np */
            
                if ($user->isError()) {
                    if ($user->hasFieldErrors()) {
                        $fieldErrors = $user->getAllErrors();
                        $errormsg = implode("\n", $fieldErrors);
                    } else {
                        $errormsg = 'An error occurred: ' . $user->getMessage();
                    }
                    $updateerror = true;
                    return;
                }
            
                $userArray = $user->getObject();
                /*This line seems to return an array not an object */
                $userid = $userArray['id'];
                $object->set('userid', $userid);
            
                $object->save();
            }







              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 17016
              • 138 Posts
              Quote from: BobRay at Apr 02, 2011, 02:49 AM

              Sorry, I can’t think of any reason why that wouldn’t work.

              Maybe because the registering user ist not logged in at the moment of the snippet-call resulting in $user = empty?
                • 17016
                • 138 Posts
                We tried it serveral times but the plugin is not activated by the registering-process although we linked the plugin to "OnUserFormSave".

                When linking the plugin to "OnUserActivate" for testing it was successful when the registered user was activating his account by clicking on the activation-link.

                Do you think this is a bug of MODx (that "OnUserFormSave" doesn´t work)? Or do we maybe have to change any settings in MODx to make it work? Maybe we have to change the "priority" of "OnUserFormSave" (at the moment it is set to 0)?

                Letti
                  • 3749
                  • 24,544 Posts
                  Quote from: Letti at Apr 15, 2011, 02:31 PM

                  We tried it sevreral times but the plugin is not activated by the registering-process although we linked the plugin to "OnUserFormSave".

                  When linking the plugin to "OnUserActivate" for testing it was successful when the registered user was activating his account by clicking on the activation-link.

                  Do you think this is a bug of MODx (that "OnUserFormSave" doesn´t work)? Or do we maybe have to change any settings in MODx to make it work? Maybe we have to change the "priority" of "OnUserFormSave" (at the moment it is set to 0)?

                  Letti

                  The Register/ConfirmRegister snippets bypass the Manager form and save the user directly. That’s why it doesn’t work when connected to OnUserFormSave. OnActivateUser is fired by the add-ons, so you could connect to that, but then you won’t get users who are bulk activated, created in the Manager, or created/activated by other add-ons.

                  I would connect it to OnUserBeforeSave, though, which is guaranteed to fire whenever a user object is saved. You can check the $modx variable to see if it’s a new user or an existing user.
                    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
                    • 17016
                    • 138 Posts
                    Hi BobRay,
                    thanks a lot. "OnUserBeforeSave" works very fine. However for some reason it fires twice while the registering process and of course a third time when the user is activating his account. Do you have any idea how to make it firing only once while the initial registering-process? The third firing doesn´t bother us very much...

                    Letti
                      • 3749
                      • 24,544 Posts
                      If you put this at the top of the code, it will only execute for brand new users:

                      if ($mode != modSystemEvent::MODE_NEW ) {
                          return;
                      }
                      


                      As an alternative, you could test with this to see if the user is already a member:

                      $groups = array(
                          'Editors',
                          'Managers',
                      );
                      if (! $user->isMember($groups) ) {
                         /* do your stuff here */
                      }



                      This will tell you if the user is a member of *all* the listed groups:

                      if (! $user->isMember($groups, true) ) {
                         /* do your stuff here */
                      }


                        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