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

    I'm using the last version of Modx Revolution and I'm trying to find a way to add more than 1000 users at once. It's a site for my school and some pages may only be seen by our students.

    I'd like to insert them via a csv file but I couldn't find a way to do it.

    Is there a way to do it like this ?

    username;name;password;email;status;group
    4555;Mark Dumont;Adfr8i;[email protected];active;students

    Really hope it's possible,

    Thanks for your help and advise,

    Sub
      • 3749
      • 24,544 Posts
      It would take a custom snippet. It's a little tricky because the username and password are in one table and the rest of the information goes in a separate table, but it's definitely doable.

      Is this line repeated for every user (it would be easier if it wasn't there at all)?

      username;name;password;email;status;group


      Are you sure no user has a semicolon in their password?



        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
        • 36098
        • 46 Posts
        Yes I'm sure there is no semicolon in the passwords. It was just to show how I import it into other programs and scripts.

        The line "username;name;password;email;status;group" is not repeated but each user has its line.

        like : "jhon;John Dow;k7Lj9;[email protected];active;student"

        Is there a tutorial that explains how to import this data and users into modx revolution?

        Thanks for your help,

        Subran
          • 3749
          • 24,544 Posts
          I don't know of a tutorial, because the exact code would depend on the structure of the file, but the basic snippet code would look something like this for your file:
          <?php
          /* open the user file and read the lines in a loop */
          $file = "full/path/to/userfile.txt";
          $output = "";
          $fp = fopen($file, "r");
          if ($fp) {
              /* skip first line */
              $line = fgets($fp, 4096);
          
              while (($line = fgets($fp, 4096)) !== false) {
                  /* skip empty lines */
                  if (empty($line)) {
                      continue;
                  }
                  /* process line */
                  $fields = explode(';', $line);
                  if (count($fields != 6)) {
                      $output .= "" . 'malformed user info: ' . $fields[0] . ' not created';
                      continue;
                  }
                  $userFields = array();
          
                  $userFields['username'] = $fields[0];
                  $userFields['password'] = $fields[2];
                  $userFields['active'] = ($fields[4] == 'active');
          
                  $newUser = $modx->newObject('modUser', $userFields);
                  $newUser->save();
          
                  /* not sure this next line is necessary, but it can't hurt */
                  $newUser = getObject('modUser', array('username' => $userFields[0]));
          
                  $profile = $newUser->getOne('Profile');
                  $profile->set('fullname', $fields[1]);
                  $profile->set('email', $fields[3]);
                  $newUser->joinGroup($fields[5]);
          
                  $saveUser = $newUser->save();
                  $saveProfile = $profile->save();
                  if ($saveUser && $saveProfile) {
                      $output .= "" . 'created user: ' . $fields[0];
                  } else {
                      $output .= "" . 'error creating user: ' . $fields[0];
                  }
          
              }
              fclose($fp);
          } else {
              return 'Failed to open file: ' . $file;
          }
          
          return $output;
          


          I'd try it with a small group of users first (and delete them before each run). The code will run for a while before anything appears on the screen, especially with the full 1000.

          I see the forum has mangled some of the code. There are br tags inside double quotes at the beginning of the messages to put them on a new line and the forum has removed them.
          [ed. note: BobRay last edited this post 12 years, 4 months ago.]
            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
            • 27106
            • 147 Posts
            With the following CSV:
            username;name;password;email;status;group
            test1;Testish Userguy;Adfr8i;[email protected];active;MyGroup
            

            this is reporting malformed user info and not adding users. Using count($fields) in the output shows 6 fields, but the code fails anyway.
              David Walker
              Principal, Shorewalker DMS
              Phone: 03 8899 7790
              Mobile: 0407 133 020
              • 3749
              • 24,544 Posts
              Is "test1" in the malformed user error message?

              Are you sure there's nothing beyond the end of the user list?


              ---------------------------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
              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
                • 33969
                • 60 Posts
                I wish I had seen this post three days ago - it would have saved me some time and the company I wrote the script for some money. ;-)

                I ran into a similar problem with the field count. I was using a csv from Excel. In my case it used a comma separator - the problem was it added an extra comma at the end of each line.

                The other problem I ran into was line termination. My csv file was from an Excel spreadsheet and I was running it an Apache server. I wasn't using 'fgets', so I don't know for sure, but I had to correct for the MS termination.

                There is a note in the PHP manual that indicates this might be a problem.
                  Michael Regan
                  TIMR Web Services
                  Phone: 250.218.5284
                  Eamil: [email protected]
                  Website: timr.ca