We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20014
    • 200 Posts
    read another post by bob where he shows some code on how to create a document using a snippet and calling it with &postHooks in login.Register

    I have tweaked it a bit, but there is still one nagging question that I can’t seem to fix: setting the access permission of the page created, I have scroogled like heck and found bits and pieces and even a couple posts which mention that it couldn’t be done, but they were without any solutions. for example this one has me scratching my head http://modxcms.com/forums/index.php?topic=52562.0 also, off topic here, while searching and following links to svn docs, I could see the resource page then abruptly it would redirect to a login page...so I wasn’t able to access those...

    essentially I want to define the resource group at the beginning of the snippet, I tried a few ways, here’s another that didn’t work:
    $resourceGroups = $modx->get('myusergroup');

    then set it in the new resouce with this:
    $resource -> set ('resource group', $resourceGroups);


    I did find this and splittingred’s answer seems to hold what I am looking for, but I am not quite sure if it’s exactly what I am looking for http://modxcms.com/forums/index.php?topic=46794.0

    Also, this would also be really helpful info, and I know Im pushing it here, trying to squeeze the secrets out of the big boys...can I use if statements based upon a user’s join date? i.e. show this if new show that if registered before current day?
      • 3749
      • 24,544 Posts
      I’m doing some guessing here, and I don’t have time to do this right. This bit of info from my book is modified from a section about users and user groups, as described in the schema file.

      There is a many-to-many relationship between resources and resource groups. A resource can be in more than one group and a group may have multiple resources. As a result, there is a table for resources and a table for resource groups, but there is also an intersect table (containing modResourceGroupResource objects) that ties resources to groups (and vice versa). The rows in that table each have two important fields: document_group (the ID of the group), and document (the ID of the resource)

      So what you want, I think, is something like this (assumes you already have the $resource object):

      <?php
      $group = $modx->getObject('modResourceGroup', array('name'=>'SomeGroupName'));
      
      // use newObject() instead of getObject in the line above if you need to create the group
      
      $intersect = $modx->newObject('modResourceGroupResource');
      $intersect->addOne($resource);
      $intersect->addOne($group);
      $intersect->save();


      Either of these might also work, but I’m not sure:
      $resource->addMany($group);
      $resource->save();


      $group->addMany($resource);
      $group->save();


      Let me know if they do. smiley

        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
        • 20014
        • 200 Posts
        thanks bob, I will try that out, right now though I seem to be having a problem with the login.confirmregistration snippet itself, so I have to figure that out before going to this.

        I can’t wait to read your book!
          • 20014
          • 200 Posts
          I think I am botching it big time, the snippet as you had it in the other post does exactly as you says it does, it creates a new resource with all the settings you specify. Now since I am having trouble with login.confirmregistration, I will concentrate on the first part of my question.
          Essentailly I am trying to get the resource group under the access tab of the resource to be ticked off and allowed access by default ( the only thing I seem to get ticked off are the good users of these forums laugh)

          here it is, this is the last one I tried:
          <?php
          $userName = $modx->user->get('username');
          $profile = $modx->user->getOne('Profile');
          $fullName = $profile->get('fullname');
          $parentId = '7';
          $content = '<p>Welcome to your personalized home page [[+name]]! </p>';
          $templateId = '4';
          $group = $modx->getObject('modResourceGroup', array('name'=>'client'));
          
          $resource = $modx->newObject('modResource');
          
          $resource -> set('pagetitle',$userName);
          $resource ->set('alias', $userName);
          $resource ->set('longtitle', 'Personal page of ' . $fullName);
          $resource -> set('parent',$parentId);
          $resource -> set('content', $content);
          $resource -> set('template', $templateId);
          $resource -> set('menu title', $userName);
          $resource -> set('published', '1');
          $resource -> set('richtext', '1');  // or 0
          $resource-> set('resource groups', $group);
          $resource -> set ('access', '1');
          
          $resource -> save();
          ?>


          you noticed I tried to set access to 1, I am just guessing here trying to look at the logic of what you and others have posted,
            • 3749
            • 24,544 Posts
            Leave out the last two lines before the save() and replace them with the code in my first example above (using the actual name of the resource group) and it should do just that. You’ll have to re-edit the doc (and possibly clear the site cache) to see the checkmark, though.
              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
              • 20014
              • 200 Posts
              ok got the localhost mail to work, when I am logged in as manager then go to the front end in a tab and sign in as a new member, I am redirected, the user is activated, and the page is created though it’s created in my admin’s name and the resource group isn’t ticked.

              now if I clear cache, flush all sessions, clear my browser’s cache, then go to the sign up page (also after deleting both page created and user activated) I sign up, go to thunderbird I get the email, follow the link to the confirmation page and get an error:
              Fatal error: Call to a member function get() on a non-object in C:\websites\myrevoinstall\core\cache\elements\modsnippet\42.include.cache.php on line 9

              the user isn’t activated and no page is created.

              going to my text editor, line 9 is this one:

              $fullName = $profile->get('fullname');


              any thoughts? I assume since the snippet is along side the [[!ConfirmRegister]] snippet that it is executing while confirm register is working and there isn’t a fullname yet in the db to call on? I suspect this because the placeholder for [[+name]] is empty in the page that is created when I am logged in as admin
                • 3749
                • 24,544 Posts
                I’m not clear on your test process. Are you deleting the user before the user has confirmed registration? That would explain things, since the activation would fail. So would trying to get a user profile before the user has confirmed registration and logged in.

                A current user that’s not logged in has no profile, so the $profile->get() code will fail.

                Also, always test from another browser where you’re not logged into the Manager. Previewing from the Manager can’t be counted on to show you what a real user will see and can introduce lots of quirks.

                Hope this helps.
                  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
                  • 20014
                  • 200 Posts
                  sorry about that,

                  First, I added the code to a snippet and added that to the verify page that the email would link to. then in another tab, without logging out of manager, I tested the register page, it redirected me to another resource which had the simple check your mail message. then I would go to thunderbird get the email sent out by my local site and click the link.

                  a tab would open in FF with the message: your account has been activated! this page contains the verify snippet and the snippet for page creation. the problem here is that the new page would be created in my admin’s name (cookie issue?), not the name of the user who just registered and the resource group box still didn’t have a checkmark showing it was assigned only to this group, but I figured this is because I didn’t re-edit, or flush as you had instructed, I was mainly concerned about the name of the document.

                  so, to test further, I would clear all my caches and flush, delete the user I just registered and the page which was created in the admin’s name. I would then clear ff history and close the browser. I then opened firefox and navigated to my revoinstall like someone surfing the web, went to the registration page, registered, I was then directed to the check email page.

                  then I retrieved the email, click the link and instead of your account has been activated, I get the error referring to the page creation snippet.

                  here is the snippet, I replaced the last two lines with the example from your first code post, I even left in the comment to remind me later.
                  <?php
                  $userName = $modx->user->get('username');
                  $profile = $modx->user->getOne('Profile');
                  $fullName = $profile->get('fullname');
                  $parentId = '7';
                  $content = '<p>Welcome to your personalized home page [[+name]]! </p>';
                  $templateId = '4';
                  
                  
                  $resource = $modx->newObject('modResource');
                  
                  $resource -> set('pagetitle',$userName);
                  $resource ->set('alias', $userName);
                  $resource ->set('longtitle', 'Personal page of ' . $fullName);
                  $resource -> set('parent',$parentId);
                  $resource -> set('content', $content);
                  $resource -> set('template', $templateId);
                  $resource -> set('menu title', $userName);
                  $resource -> set('published', '1');
                  $resource -> set('richtext', '1');  // or 0
                  $group = $modx->getObject('modResourceGroup', array('name'=>'client'));
                  
                  // use newObject() instead of getObject in the line above if you need to create the group
                  
                  $intersect = $modx->newObject('modResourceGroupResource');
                  $intersect->addOne($resource);
                  $intersect->addOne($group);
                  $intersect->save();
                  
                  $resource -> save();
                  ?>




                    • 3749
                    • 24,544 Posts
                    I think the problem is that when the user visits the confirm register page from the link in the email, he or she is not logged in yet. The snippet on the confirm register page activates the existing user, but, *doesn’t log that user in*. So $modx->user is still ’(anonymous)’ at that point and has no profile.

                    I think you’d be better off sending all users to the same page after they log in and putting your snippet there.

                    Here’s another thought: Look at the code of the snippet on the confirm registration page and see how it figures out who the user is. Find the user that way, rather than using $modx->user.

                    This should be all you’d need to get the user. Just make sure your snippet is below the ConfirmRegister snippet tag and that your snippet is uncached: [[!SnippetName]].

                    /* get user from query params */
                    if (empty($_REQUEST['lp']) || empty($_REQUEST['lu'])) $modx->sendErrorPage();
                    $username = base64_decode(urldecode($_REQUEST['lu']));
                    $password = base64_decode(urldecode($_REQUEST['lp']));
                    
                    /* validate we have correct user */
                    $user = $modx->getObject('modUser',array('username' => $username));
                    if ($user == null) { $modx->sendErrorPage(); }
                    
                    //Now your code: $user->get('Profile'), etc.
                    

                    BTW, it’s a lot simpler to just download some other browser (Chrome, Opera, IE, Safari -- doesn’t matter) and never log in to the MODx Manager with it. That way it’s always available for testing front-end stuff without having to constantly delete the browser cache and cookies.
                      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
                      • 20014
                      • 200 Posts
                      modxlearner123 Reply #10, 16 years ago
                      well it’s been awhile, I had to drop out of this and then resurrect due to some work which came my way, but I have been trying to trouble shoot this for quite some time, and without bob’s help there is no way in heck I could’ve ever even come close on my own.

                      just to update this thread a bit, I had created a redirect from the registration to a resource with the message for the user to check their email, then on the page which the email directs to, the confirmation snippet and login form, unfortunately when the user logs in, (I wanted them to go to the login redirect page where I place the above resource creation snippet) but I get a 404....I think automating the task of creating a page per user, having a unique resource group created specifically for each and having their resource only available to them and not all registered users is waaaaaay past my lowly designer abilities, it’s getting to the point where I think I am only confusing myself.

                      but this is something I have mentioned a couple times, there are situations which all of us face from time to time, requests from clients that seem to be universal, the reason why I really got on this theme was because I thought it would be perfect for my own site where I could have a client automatically log into a page which was tailored to them where they could view milestones reached in their projects, have a a place for them to make payments etc etc, and it seems a lot of people have run into this type of request at least once. I had resolved this in evo, by creating a generic page then inserting code on a per client basis and adding the client’s name to an if statement snippet, but that is a lot of work and isn’t exactly an automated process.

                      of course, one could go the permissions route but if you have a client which wants to do the same for their own customers, this would be the perfect solution neither you nor they would have to mess around with roles or users if it could be done automatically from the front end, if anyone ever finds a good tut on this, decides to put a how to in the modx documentation or share from their own code stash which they’ve implemented before, please pm me to tell me about it...

                      thanks a ton bobray, I just think I still have a lot (to put it mildly) to learn about coding.