We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44580
    • 189 Posts
    Thanks again BobRay. You're right, I should be extending ModUserProfile. It's interesting though that most references in RTFM etc refer to extending ModUser.

    I'm not quite getting across what I'm trying to achieve so I thought I would try with a picture. What I would like to end up with is a New User form in the manager that looks like this:



    In other words, the standard user form but with the addition of my two custom fields as highlighted in red. And those fields would be listboxes containing all the coaches and parents respectively. Can this be done and if so how? This is really confusing me (can you tell?)

    Any help would be appreciated.
      • 3749
      • 24,544 Posts
      There's an example in my book showing how to add fields to the user form (and process them) with a plugin, but it adds them to the end of the form. I think your example would take some JavaScript, and TBH, I've had little luck (read "no luck at all") doing this because modExt creates the form on the fly and when your code runs, the form fields don't exist yet. I guess in theory you could add them at the end and use CSS with absolute positioning to put them where you want, but I'm not sure how well that would work in different browsers, much less tablets and phones.

      You might also be able to do it with mods to the Manager's Smarty template files. You'd probably have to create a whole new Manager theme to keep them from being overwritten during upgrades of MODX.

      BTW, I you don't need some of those fields (e.g., Country, Fax, Website), you could co-opt them for your data (being careful of the data types) and avoid extending anything. You can change the lexicon strings for them so the prompts would be correct, though you'd have to use the real original field names in any code.
        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
      • MIGXdb and a CMP to create a set of three custom user management pages would be how I would do it. You would only need to have two custom tables, intersect tables for coach_child and parent_child. For the rest, you would use the users, user_attributes and member_groups tables.

        The member_groups table is an excellent example of an intersect table, all it does is connect members (users) to groups. Your intersect tables don't need as many fields, you only need a parent_id or coach_id and child_id field.

        You'll want to be editing "parent" users and "coach" users as well as "child" users, I would presume? Hence the three CMPs.

        By the way, here's an excellent tutorial on using MIGXdb and a CMP http://www.bdcreative-design.com/blog/creating-a-cmp-in-modx-using-migxdb/
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 44580
          • 189 Posts
          First of all, thanks again Bob and Susan for your suggestions. I've been experimenting with some code (mostly other peoples) to try and display the extra fields in the New User Form with some limited success. To your earlier point Bob, about the difficulties of placing the fields where you want and formatting them - I see what you mean! But for now at least, I am just trying to return a list of coaches based on their membership of the Coaches User Group. I'm not getting very far. Here is my plugin code:

          <?php
          /**
           * Add extra fields to New/ Modify User forms
           */
          switch ($modx->event->name) {
              case 'OnUserFormPrerender':
                  /* if you want to add custom scripts, css, etc, register them here */
                  break;
              case 'OnUserFormRender':
                  $v = '';
                  if (isset($scriptProperties['resource'])) {
                      /* on the update screen, so set the value */
                      $v = $scriptProperties['resource']->get('longtitle');
                  } else {
                      /* on the create screen, so get all the coaches names */
                      $usergroup = 2;  /* This is the Coach_ug */
                      $c = $modx->newQuery('modUser');
                      $c->innerJoin ('modUserProfile','Profile');
                      $c->innerJoin ('modUserGroupMember','UserGroupMembers');
                      $c->innerJoin ('modUserGroup','UserGroup','`UserGroupMembers`.`user_group` = `UserGroup`.`id`');
                      $c->leftJoin ('modUserGroupRole','UserGroupRole','`UserGroupMembers`.`role` = `UserGroupRole`.`id`');
                      $c->where(array(
                      'active' => true,
                      'UserGroupMembers.user_group' => $usergroup,
                      'UserGroupMembers.role' => '1',
                      ));
          
                      $users = $modx->getCollection('modUser',$c);
          
                      foreach($users as $var => $value)
                      {
                      $profile = $modx->user->getOne('Profile');
                      $v = $profile->get('fullname');
                      } 
                 }
                  /* now do the HTML */
                  $fields = '
          <div class="x-form-item x-tab-item">
          <p> '.$v.' </p>
          </div>
          ';
                  $modx->event->output($fields);
          break;
              case 'OnUserFormSave':
                  /* do processing logic here. */
                  $resource =& $scriptProperties['resource'];
                  $resource->set('longtitle',$_POST['home']);
                  $resource->save();
                  break;
          }
          return;


          At the moment I am only working with the code in the else statement of the OnUserFormRender case statement. My problem is that the only row I get returned from my query (well actually the query that I found here: http://www.unchi.co.uk/2010/10/31/get-all-members-of-a-user-group-in-modx-revolution/ - with some minor modifications) is the Admin User who is NOT a member of the Coaches user group. I'm guessing the problem is in the foreach loop but my ignorance is stopping me from figuring out what. Any ideas? [ed. note: gissirob last edited this post 10 years, 6 months ago.]
          • Your display is outside of the loop, so you're never actually displaying the Coach users.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 44580
              • 189 Posts
              Hi Susan, I am getting output in the New User form (that is: <p> '.$v.' </p> is displaying). My problem is it display "Default Admin User" rather than the fullnames of all the coaches. There seems to be a logical disconnect between what the query returns and the foreach loop. I suspect that $v is simply being set to the current user (ie me, as Admin).

              I can only assume that the example code I used (linked above) is either incorrect or incomplete but it is beyond my limited knowledge to figure out how to fix it.

              BTW, I realised that the code I posted was slightly incorrect - it has now been updated with the version that executes as described.
                • 44580
                • 189 Posts
                Ok, I've managed to sort some of this out myself. The following foreach loop:

                foreach($users as $var => $value)
                      {
                      $profile = $modx->getObject('modUserProfile', $var);
                      $v = $profile->get('fullname');
                      } 

                successfully displays the last member of the group. What I need to do is:

                • return an array of all group members.
                • somehow get this array into a select list (preferably one that looks and acts like the Gender select on the new user page).
                I have no clue as to how to do this so help would again be much appreciated.
                  • 4172
                  • 5,888 Posts
                  gissirob,

                  I think the fastest way to get there, is to create one/some new CMPs with help of MIGXdb.
                  And I think you will not need much custom coding.

                  I can show you, how to create the needed xpdo-schema and how to create the CMPs with help of MIGXdb.

                  Before starting to build the CMP(s) you should know:

                  Do you need just one parent for each children, or do you need more than one for each.
                  Can each children have only one coach or can a children have more coaches?

                  For the Frontend - Stuff, all you need is migxLoopCollection.
                  May be for some special stuff bloX, but I think migxLoopCollection should do what we need here.

                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 44580
                    • 189 Posts
                    Hi Bruno - thanks for your suggestion. I thought about MIGX (and may still end up there) but I'm big on not re-inventing the wheel and I figured that all I needed to do was add two extra fields to the existing new user page. What could be hard about that? <irony> I feel that I am getting closer, and will continue to pursue this method for now. As this is all new to me I don't really have a handle on the "right" way and so I really appreciate all the good advice I'm getting - while quickly realising that there seems to be almost infinite ways to achieve an end in Modx (that being both a strength and a weakness).

                    Anyway, to answer your question: A child can have only one parent and only one coach. Not exactly real-world, but it works in the scenario I'm trying to build. In this scenario, all users will be created and maintained by the business owner via the Manager console. However, there will be approx 50 other attributes related to the child (lesson times, milestones etc) and the parents (billing, progress reports etc) that will be maintained by the coaches and parents via the front end. It sounds like this part may be perfect for MIGX.
                      • 4172
                      • 5,888 Posts
                      I think the frontend - stuff can be done then with some migxLoopCollection - snippet-calls and some forms with formit/formit2db

                      If you would like to create some fancy MVC/angular-stuff on the frontend-site, MIGXangular does funny things for me and you can build allready your very own angular-APP around MIGXangular.
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!