We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 349
    • 76 Posts
    I want to automatically create a new folder for a user when he is registered via the Login.Register snippet or via MODx Usermanagement.
    I found a snippet here(http://modxcms.com/forums/index.php/topic,30114.0.html) but for Evolution.
    //<?php
    $evt    = &$modx->Event;
    
    switch($evt->name){
    	case 'OnWebSaveUser': 
    		if($evt->params['mode']=='new'){
    			$dir = $modx->config['base_path'].$modx->config['rb_base_dir'].'files/'.$evt->params['username'];
    			$folder_permissions = octdec($modx->config['new_folder_permissions']);
    			if(! @mkdir($dir,$folder_permissions) ){
    				$modx->webAlert("Unable to create user folder!");
    			}			
    		}	
    		break;
    }
    //?>


    How to do the same with the Login.Register Snippet for Revo?

    I think i need to use the Post Hook like this:
    [[!Register?
    &submitVar=`registerbtn`
    &activation=`0`
    &usergroups=`partner`
    &successMsg=`Thanks for registering!`
    postHooks=`generateUserDirectory`
    ]]


    I don´t know all the API functions yet. Still much to learn. Any help appreciated.
    Thank you.
      • 28215
      • 4,149 Posts
      Quote from: aceman3000 at Dec 10, 2010, 02:00 PM

      I want to automatically create a new folder for a user when he is registered via the Login.Register snippet or via MODx Usermanagement.
      I found a snippet here(http://modxcms.com/forums/index.php/topic,30114.0.html) but for Evolution.
      //<?php
      $evt    = &$modx->Event;
      switch($evt->name){
      	case 'OnWebSaveUser': 
      		if($evt->params['mode']=='new'){
      			$dir = $modx->config['base_path'].$modx->config['rb_base_dir'].'files/'.$evt->params['username'];
      			$folder_permissions = octdec($modx->config['new_folder_permissions']);
      			if(! @mkdir($dir,$folder_permissions) ){
      				$modx->webAlert("Unable to create user folder!");
      			}			
      		}	
      		break;
      }
      //?>


      I don´t know all the API functions yet. Still much to learn. Any help appreciated.
      Thank you.

      Well, first, you’re missing a & before postHooks in your snippet call. Secondly, you’re using a few deprecated API things. rb_base_dir is being deprecated for 2.1/2.2, and will be removed. Use filemanager_path instead.

      $user =& $scriptProperties['register.user'];
      $dir = (boolean)$modx->getOption('filemanager_path_relative',null,false) ? $modx->getOption('base_path') : '';
      $dir .= $modx->getOption('filemanager_path',null,'').'files/'.$user->get('username');
      $folder_permissions = octdec($modx->getOption('new_folder_permissions'));
      if (!@mkdir($dir,$folder_permissions)) {
          $modx->log(modX::LOG_LEVEL_ERROR,'Could not create user directory: '.$dir);
      }
      return true;
      

      Note a few things I did here:

      • We’re not using a Plugin, so there’s no need for Events. We’re using a hook, which Register passes the User object in the $scriptProperties array as ’register.user’. We’ll assign a reference to it in $user.
      • If the ’username’ field was named ’username’ in your Register form, you could alternatively get the username via: $scriptProperties[’fields’][’username’]
      • We’re using filemanager_path instead of rb_base_dir
      • Some installations might have an absolute filemanager_path dir, so we need to check to see if filemanager_path is relative or not, via the filemanager_path_relative setting. If it is relative, append the base_path. If not, leave it be.
      • Note we’re using ->getOption instead of ->config. This is proper Revo syntax. The method is $modx->getOption($key,$arrayToSearch,$defaultValue).
      • Note the $modx->log method. There is no webAlert; this log method logs the error to the core/cache/logs/error.log file, so it doesn’t interrupt your process.

      Hope that helps!
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 6038
        • 228 Posts
        I bundled this into a plug-in for Revo, ticking the ’onUserFormSave’ event.
        I’m not sure what the difference between that and ’onUserSave’ is?
        - I also had to create the ’new_folder_permissions’ in the System Settings
        - I had to create the ’files’ folder.
        It works, but if the user changes their username, it creates another folder of course.
        Also, is there a better way to get the username?

        <?php
        if ($modx->event->name == 'OnUserFormSave') {
        
        	$username = $_REQUEST['username'];
        	
        	$dir = (boolean)$modx->getOption('filemanager_path_relative',null,false) ? $modx->getOption('base_path') : '';
        	$dir .= $modx->getOption('filemanager_path',null,'').'files/'.$username;
        	$folder_permissions = octdec($modx->getOption('new_folder_permissions'));
        	if (!@mkdir($dir,$folder_permissions)) {
        	    $modx->log(modX::LOG_LEVEL_ERROR,'Could not create user directory: '.$dir);
        	}
        	return true;
        
        }
        
        return;
        
          • 3749
          • 24,544 Posts
          This would be a better method for getting the username:
          $userName = $user->get('username');  


          This should keep it from running when the user record is updated:
          If ($mode == modSystemEvent::MODE_NEW) 


          This is unnecessary since you’re only linked to one event:
          if ($mod->event->name == 'OnUserFormSave')


          The OnUserSave event will fire whenever the user record is saved, which might happen when the user is saved in the code of a plugin or snippet. OnUserFormSave only fires when the user is saved via the user form in the Manager.
            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
            • 6038
            • 228 Posts
            thanks BobRay - those are very helpful peices of info!

            Quote from: BobRay at Mar 01, 2011, 08:18 AM

            This would be a better method for getting the username:
            $userName = $user->get('username');  


            I’m sorry I didn’t explain clearly that my plugin is only operating from the manager, as user registration is disabled on the front-end of the site.
            And I’m guessing that the [tt]$user->get(’username’)[/tt] is only relevant for a logged-in or new user registration on the front-end.

              • 3749
              • 24,544 Posts
              No, if a new user is created, the new $user object is passed to both events so it would be available in your plugin.
                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
                • 6038
                • 228 Posts
                Thats great to know.
                Though when I wrapped the conditional statement [tt]if ($modx == modSystemEvent::MODE_NEW)[/tt] around the code - it no longer created the folder when adding a new user
                  • 3749
                  • 24,544 Posts
                  Quote from: crunch at Mar 18, 2011, 04:37 AM

                  Thats great to know.
                  Though when I wrapped the conditional statement [tt]if ($modx == modSystemEvent::MODE_NEW)[/tt] around the code - it no longer created the folder when adding a new user

                  Sorry, typo. tongue

                  After all this time with MODx, when I type $mod the x comes next no matter what I’m trying to type. smiley

                  if  ($mode == modSystemEvent::MODE_NEW) {
                  }


                  BTW, in case you ever need it, the alternative is modSystemEvent::MODE_UPD when an existing user if updated.
                    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
                    • 39389
                    • 9 Posts
                    Hmm so whats the end code now ?
                    Ive trouble with this Plugin
                    This one work but with the same bugs with double folders
                    <?php
                    if ($modx->event->name == 'OnUserFormSave') {
                     
                        $username = $_REQUEST['username'];
                         
                        $dir = (boolean)$modx->getOption('filemanager_path_relative',null,false) ? $modx->getOption('base_path') : '';
                        $dir .= $modx->getOption('filemanager_path',null,'').'files/'.$username;
                        $folder_permissions = octdec($modx->getOption('new_folder_permissions'));
                        if (!@mkdir($dir,$folder_permissions)) {
                            $modx->log(modX::LOG_LEVEL_ERROR,'Could not create user directory: '.$dir);
                        }
                        return true;
                     
                    }
                     
                    return;


                    and this one dont create a folder

                    if  ($mode == modSystemEvent::MODE_UDP) {
                     
                        $userName = $user->get('username');
                         
                        $dir = (boolean)$modx->getOption('filemanager_path_relative',null,false) ? $modx->getOption('base_path') : '';
                        $dir .= $modx->getOption('filemanager_path',null,'').'files/'.$username;
                        $folder_permissions = octdec($modx->getOption('new_folder_permissions'));
                        if (!@mkdir($dir,$folder_permissions)) {
                            $modx->log(modX::LOG_LEVEL_ERROR,'Konnte Ordner nicht erstellen: '.$dir);
                        }
                        return true;
                     
                    }
                     
                    return;
                    
                    




                    oh smiley i got it now
                    the error was in $userName and $username sorry laugh


                    if  ($mode == modSystemEvent::MODE_NEW) {
                        return true;
                    }
                    
                    if  ($mode == modSystemEvent::MODE_UPD) {
                    
                    	 	$userName = $user->get('username');
                         
                        $dir = (boolean)$modx->getOption('filemanager_path_relative',null,false) ? $modx->getOption('base_path') : '';
                        $dir .= $modx->getOption('filemanager_path',null,'').'assets/kunden/'.$userName;
                        $folder_permissions = octdec($modx->getOption('new_folder_permissions'));
                        if (!@mkdir($dir,$folder_permissions)) {
                            $modx->log(modX::LOG_LEVEL_ERROR,'Could not create user directory: '.$dir);
                        }
                        return true;
                    }



                    this one works for me to create for Updated user.
                    for new user ill change the code inside the if. [ed. note: creativ3minds last edited this post 11 years, 7 months ago.]
                      • 3749
                      • 24,544 Posts
                      In line 9, I would use if (empty()) rather than the cast, though what you're doing is probably fine.

                      I'm glad you got it sorted. Thanks for reporting back. smiley


                      ------------------------------------------------------------------------------------------
                      PLEASE, PLEASE specify the version of MODX you are using.
                      MODX info for everyone: http://bobsguides.com/modx.html
                        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