We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38290
    • 712 Posts
    I'm working on scripting some repetitive tasks and trying to figure out how to duplicate an existing context using xPDO. Also trying to do things like create a new User Group and add a User to it.

    This question has been answered by Bruno17. See the first response.

      jpdevries
    • discuss.answer
      • 4172
      • 5,888 Posts
      can't you just run the context/duplicate - processor?

      for creating a new usergroup and adding a user to it does this work?

      $group = $modx->newObject('modUserGroup');
      $group->set('name','groupname');
      //$group->set('parent','');
      //$group->set('rank','');
      //$group->set('description','');
      
      if ($group->save()){
          if ($user = $modx->getObject('modUser',$userID)){
              $user->joinGroup($group->get('id'),$roleID);
          }
      }
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 38290
        • 712 Posts
        Quote from: Bruno17 at Jun 23, 2013, 11:58 PM
        can't you just run the context/duplicate - processor?

        for creating a new usergroup and adding a user to it does this work?

        $group = $modx->newObject('modUserGroup');
        $group->set('name','groupname');
        //$group->set('parent','');
        //$group->set('rank','');
        //$group->set('description','');
        
        if ($group->save()){
            if ($user = $modx->getObject('modUser',$userID)){
                $user->joinGroup($group->get('id'),$roleID);
            }
        }

        Thanks! Giving this a try. Might little alter to try and do
        $modx->newObject('modUser');


        Also looking into using and abusing runProcessor
        http://rtfm.modx.com/display/revolution20/Using+runProcessor
          jpdevries
          • 38290
          • 712 Posts
          So far this dies on when run as a bash script. I'm not sure if HTTP_MODAUTH needs to be passed into runProcessor but I tried using $modx->user->getUserToken('mgr') and it didn't make a difference.
          $response = $modx->runProcessor('context/duplicate',array(
             'key' => 'astro',
             'newkey' => 'magic'
          ));
          


          (ERROR) Could not load class: modError from moderror.
            jpdevries
            • 38290
            • 712 Posts
            The create chunk example at the runProcessor RTFM page gets the same error, so at least that tells me it must be something environmental.

            <?php
            
            $tstart = microtime(true);
            set_time_limit(0);
            
            require 'ABSOLUTE_PATH_TO_MODX/config.core.php';
            require MODX_CORE_PATH . 'model/modx/modx.class.php';
            
            $modx = modX::getInstance();
            
            $modx->initialize('mgr');
            
            $modx->setLogLevel(modX::LOG_LEVEL_INFO);
            $modx->setLogTarget('ECHO');
            
            $response = $modx->runProcessor('element/chunk/create',array(
               'name' => 'NewChunk',
               'description' => 'A test Chunk made with runProcessor.',
               'snippet' => '<h3>Chunkify!</h3>',
            ));
            if ($response->isError()) {
                echo $response->getMessage();
            }
            $chunkArray = $response->getObject();
            echo 'The chunk "'.$chunkArray['name'].' was created with ID '.$chunkArray['id'];


            [2013-06-23 22:37:16] (ERROR) Could not load class: modError from moderror.
            [2013-06-23 22:37:16] (ERROR) Problem getting service error, instance of class modError, from path 
            
            Fatal error: Call to a member function hasError() on a non-object in /home/heart/core/model/modx/modprocessor.class.php on line 119
              jpdevries
              • 38290
              • 712 Posts
              This is working great for creating a new User Group and adding a user to it! Just need to figure out how to add the created User Group to a given Context (context access).

              <?php
              
              $tstart = microtime(true);
              set_time_limit(0);
              
              require '/home/heart/public_html/config.core.php';
              require MODX_CORE_PATH . 'model/modx/modx.class.php';
              
              $modx = modX::getInstance();
              
              $modx->initialize('mgr');
              
              $modx->setLogLevel(modX::LOG_LEVEL_INFO);
              $modx->setLogTarget('ECHO');
              
              $groupName = $argv[1];
              $userName = $argv[2];
              $userEmail = $argv[3];
              $password = $argv[4];
              
              $group = $modx->newObject('modUserGroup');
              $group->set('name',$groupName);
              //$group->set('parent','');
              //$group->set('rank','');
              //$group->set('description','');
               
              if ($group->save()){
              	$user = $modx->newObject('modUser', array('username'=>$userName, 'password'=>$password));
              	 
              	$profile  = $modx->newObject('modUserProfile',               
              	array(
              	//'internalKey' => $id,
              	//'address' => '12345',
              	//'zip' => '91355',
              	//'city' => 'Valencia',
              	//'state' => 'CA',
              	//'country' => 'USA'
              	'fullname' => $groupName,
              	'email' => $userEmail
              	));
              	 
              	$user->addOne($profile);
              	
              	if ($user->save()) {
              		$user->joinGroup($group->get('id'),3);
              	} else {
              		$modx->log(modX::LOG_LEVEL_INFO, "oops -- problem with saving user");
              	}
              	
              } else {
              	$modx->log(modX::LOG_LEVEL_INFO, "oops problem creating user group");
              }


              Usage:
              createusergroup.php MyGroup johndoe [email protected] johnspassword
                jpdevries
                • 38290
                • 712 Posts
                Reason I got the error message is because the error service needs to be actviated like this:
                $modx->getService('error','error.modError', '', '');


                So finally, the below code is working perfectly
                <?php
                
                $tstart = microtime(true);
                set_time_limit(0);
                
                require '/home/heart/public_html/config.core.php';
                require MODX_CORE_PATH . 'model/modx/modx.class.php';
                
                $modx = modX::getInstance();
                
                $modx->initialize('mgr');
                $modx->getService('error','error.modError', '', '');
                
                $modx->setLogLevel(modX::LOG_LEVEL_INFO);
                $modx->setLogTarget('ECHO');
                
                $key = $argv[1];
                $newkey = $argv[2];
                
                $response = $modx->runProcessor('context/duplicate',array(
                   'action' => 'duplicate',
                   'key' => $key,
                   'newkey' => $newkey,
                ));
                if ($response->isError()) {
                    echo $response->getMessage();
                }
                $chunkArray = $response->getObject();
                $modx->log(modX::LOG_LEVEL_ERROR, "The context $key was duplicated as $newkey");
                [ed. note: dinocorn last edited this post 13 years, 3 months ago.]
                  jpdevries