We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27738
    • 2 Posts
    Hi All,

    I’d like some advice and help please.

    I have an existing site - not Modx - that allows users to login to their account and then create what are called social stories that comprises of a series sentences. They can use these stories with children to help them learn social skills. They are able to add edit, delete stories and sentences, upload and use images with them, view them via the website or convert them to pdf to download. I wrote this in PHP and mysql with some AJAX for various tasks. I’m in the process of moving it over to modx, I built my resources, have user management sorted and login working fine and I can interact with the DB to do what I need to do with my story data from my custom tables. I’m WELL IMPRESSED with Modx and how versatile it is.

    I’m stuck trying to tie in modx users with my custom tables that I’ve moved over. I use a userid which relates to users in a custom user table (shl_users) but I want to use modx_users table instead of my own for obvious reasons. I looked at extending mod user - http://rtfm.modx.com/display/revolution20/Extending+modUser - I think I’ve got my schema ok but got lost with step 4 (I’m a simple soul and I’ve got it right round my neck now). I’m begining to think this aproach is overkill for what I want to do so thought about using a plugin that would manage adding and removal of a userid - using onUserSave & onUserRemove - in my own user table - shl_users (only contains userid). I can get user info name, id etc from Modx calls and just use my table as a bridge to tie in my custom tables with my story data.

    Does that make any sense huh


    Am I going about this the wrong way and am I lining myself up for a load of grief later on. Should I go with extending moduser or go with the plugin?

    Thank you very much smiley
      • 3749
      • 24,544 Posts
      Extending modUser is probably the most efficient way to go, but you might consider just adding the extra data in extended fields in the MODX user table, since presumably each of your users is also a MODX 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
        • 28215
        • 4,149 Posts
        Only extend modUser if you’re wanting to modify *behavior* for modUser, such as authentication, permissions, or other logic. Use a related table if you want to just add fields but don’t want to use the extended fields columns.

        Pros to using modUser:
        - Can override get/toArray to "lazily" add fields to the user object
        - Can override the authentication of the user

        Cons:
        - Cannot use with modActiveDirectory, rpxUser, or any other custom extended modUser, since you can only inherit once.

        That make sense?
          shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
          • 27738
          • 2 Posts
          Thanks guys - I’ve not been ignorant I’ve only just picked this up again today smiley

          I’ve built a solution in a plugin:
          if ($modx->event->name == 'OnUserActivate' || $modx->event->name == 'OnUserSave' || $modx->event->name == 'OnUserRemove') 
          { 
            $inmanger = false;
            $DoIT = false;
            $inmanager = $modx->insideManager();
          
            if ($inmanager == true && $modx->event->name == 'OnUserSave' && $mode == modSystemEvent::MODE_NEW) 
            {
               $DoIT = true;
            }
            if ($modx->event->name == 'OnUserActivate' )  
            {
               $DoIT = true;
            }
          
            if($DoIT == true)
            {
              $path = MODX_CORE_PATH . 'components/soshul/';
              $result = $modx->addPackage('soshul',$path . 'model/','shl_');
              $ID = $user->get('id');
              $NewUser = $modx->newObject('Users');
              $NewUser->set('user_id', $ID);
              $NewUser->save();
              $NewDesc = $modx->newObject('UserDescriptions');
              $NewDesc->set('usrdesc_userid', $ID);
              $NewDesc->save();
            }
            if ($modx->event->name == 'OnUserRemove')
            {
              $path = MODX_CORE_PATH . 'components/soshul/';
              $result = $modx->addPackage('soshul',$path . 'model/','shl_');
              $ID = $user->get('id');
              $RemUser = $modx->getObject('Users', $ID);
              $RemUser->remove();
              $RemDesc = $modx->getCollection('UserDescriptions', array('usrdesc_userid' => $ID,));
               foreach ( $RemDesc as $RD) 
                {
                 $RD->remove();
                } 
            }
          }
          



          I’ve got it to do just what I want it to do - migh be a bit lash but it works. rolleyes
          I didn’t want to get into the complexity (dont have the time too either) of changing what modx does well already with users and authentication and I needed more than just a few extra fileds.
          Your expert opinions would be much appreciated grin
          Only thing I need to do now is make sure I can get cascading delete to work when I delete from shl_users - doesn’t want to play ball at the moment - but it will

          Thanks Peeps smiley.
            • 3749
            • 24,544 Posts
            I think this might do the same thing and be a little easier to follow (the first case should "fall through" to the second depending on the conditions). Also, insideManager() is deprecated, so I replaced it :

            <?php
            switch($modx->event->name) {
            
                case 'OnUserSave':
                    if ($modx->context->get('key') != 'mgr' ||  $mode != modSystemEvent::MODE_NEW  ) {
                        break;
                    }
                    /* fall through to next case here */
                case 'OnUserActivate':
                    
                    $path = MODX_CORE_PATH . 'components/soshul/';
                    $result = $modx->addPackage('soshul',$path . 'model/','shl_');
                    $ID = $user->get('id');
                    $NewUser = $modx->newObject('Users');
                    $NewUser->set('user_id', $ID);
                    $NewUser->save();
                    $NewDesc = $modx->newObject('UserDescriptions');
                    $NewDesc->set('usrdesc_userid', $ID);
                    $NewDesc->save();
            
                    break;
            
                case 'OnUserRemove':
                    $path = MODX_CORE_PATH . 'components/soshul/';
                    $result = $modx->addPackage('soshul',$path . 'model/','shl_');
                    $ID = $user->get('id');
                    $RemUser = $modx->getObject('Users', $ID);
                    $RemUser->remove();
                    $RemDesc = $modx->getCollection('UserDescriptions', array('usrdesc_userid' => $ID,));
                     foreach ( $RemDesc as $RD)   {
                         $RD->remove();
                     } 
            
                    break;
            }
            


            I don’t see any obvious reason why the cascading deletes aren’t working. Try testing the return value of loadPackage() (true/false) and getCollection() -- if (empty($RemDesc). You Might also try OnBeforeUserRemove, though it shouldn’t make any difference.
              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
              • 27738
              • 2 Posts
              Thanks Bob,

              I’ve reworked it in light of what you posted - works a treat grin

              As for the cascading delete - I couldn’t possibly admit to having "one" instead of "many" in my shl_users schema - I’m sure the coding goblins have been messing with me again rolleyes

              Thanks a lot Bob - I’m flying now grin