We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    The scenario is a form with a multi-select, let’s say for animals that the user likes. So each user can like 1 or many animals. On the back end we have 3 database tables: 1 for users, 1 for animals, and one joining the two tables. If a new user fills out the form and checks 3 different animals, then one row is created in the users table, and 3 rows are created in the joining table, keyed back to the user_id in the user table.

    Can WebLoginPE handle this? Or is there a way to tie in a custom handler to the existing functionality?

    Thanks!
      • 10408
      • 8 Posts
      I’ve been contracted to add this exact functionality to a user login page also and will be attempting to figure this out today.
      First time using WebLoginPE or ModX so reading docs and checking the code to see if there is API functionality to support it.
      If not... guess we’ll just extend it somehow.

      Galen
        • 10408
        • 8 Posts
        Ok here is my progress so far. Please bare with me as this is my first ever contact with ModX and WebloginPE so i am sure there is some big gaps in my knowledge that need filling.

        1] Adjusted the parameters of the plugin to look like so:

        [!WebLoginPE? &type=`register`  &customFields=`user_categories` &inputHandler=`Categories:userCategories:user_categories:select multiple:Service 1(1),Service 2(2),Service 3(3),Product 1(4)`!]

        Notes: this is basically from the plugin documentation which has an example of how to do a multi-select for favorite colors.

        Question 1: I will need this to be data driven so to confirm i am on the right track... I would add some php scripting of this ’page’ to build up the parameters of the weblogin plugin above from the User_Categories table in the database dynamically. Sounds simple enough, but can someone confirm this is the correct modx way? I.e. is there modx API that should be used?

        2] Adjusted the $wlpeRegisterInstantTpl2 definition in the webloginpe.templates.php file to include
        [+form.user_categories+]


        Question 2 : Now i know this must be the wrong way, and that you should be able to override the default template with something called a MODX chunk? That way the plugin files remain untouched. Could someone please give me some clues on how to go about this? Is it just a simple matter of having a chunk with the same name? Do you need to pass the new chunk name as a parameter of plugin etc?

        3] Question 3: when a user selects these values where does it get stored/saved in terms tables? I will need to add some logic elsewhere on a completely separate page.


        Will post my progress as i go to assist Everett and anyone else who may find this helpful.

        Galen
          • 9207 ☆ A M B ☆
          • 2,475 Posts
          Galen-

          Thanks for the quick response. I’m fighting nasty jet-lag, but I’m going to try to work on this today -- my server is having some other issues that’s making it difficult.

          I can help you out with some of your questions. A Chunk is a MODx term for a bit of reusable static text (usually html) -- often you use them where you might have formerly used server-side-includes, e.g. a Chunk to hold your footer info. A Chunk is the counterpart to a Snippet (which is a bit of PHP code). My mnemonic is the PP in Snippet for P-h-P. I did an introductory video to MODx that may be useful to you: http://www.tipsfor.us/2009/01/22/new-overview-of-modx-modx-series-part-ii/
          The wiki page on the various placeholders is also a handy reference:
          http://wiki.modxcms.com/index.php/Placeholders_used_by_MODx_Pages_and_Templates

          Many MODx Snippets use Chunks as miniature templates to format output... in this case WebLoginPE offers many ways to customize its forms by allowing you to create your own Chunks and then reference them by name in the Snippet call.

          I don’t think the task here will involve the MODx API... I think it will involve the API for WebLoginPE... I’m not sure how to set that up though...

          As for where data is passed, it’s in the $_POST array... you can read values passed from the form, and then grab it to insert it into the multiple normalized database tables... WebLoginPE makes the job easier by helping to automatically generate the forms, validate them, and re-populate them, but given this scenario of storing user data across 3 (or more tables), I think some of the more difficult functionality must be custom coded. Normally, the data WebLoginPE collects is stored in the database tables that are named with "web_user" -- there are 2 primary tables that are used.

          Hope this helps. I’ll post anything I figure out as well.
            • 10408
            • 8 Posts
            The snippet call contains the "list" contents that we want our users to be able to multi-select from. This list is derived from a database table. This string of contents will therefore need to be put together by a bit of PHP code. Unless the WebloginPE snippet itself can handle table/field names being passed to it.

            So the first bit of custom code is there. DB call to get a list of values from the database to pass into the snippet.

            The second bit as you have suggested will then need to be put in to handle the population of the selected items into the normalised tables or the newly created user. Ideally some sort of "event" where some custom code could be placed would be ideal. I saw something about events in your tutorial video but i am not clear if they are applicable to our case here. I dont really want to change the code in the snippet which might restrict the companies ability to upgrade the snippet down the track.

            So the second bit of custom code ideally should be put into an event that the snippet raises. This hopefully would still have the selected form variables accessible.

            Anyone else with more experience then us able to give some direction?
              • 9207 ☆ A M B ☆
              • 2,475 Posts
              The WebLoginPE has an API tab with some of its events listed. I was hoping to tie into those events, but I was unclear on how they should be used. It would be possible to use a MODx plugin here (those are the events mentioned in my video), but I think that’s probably overkill... we don’t need MODx application events, we just need the events for the form’s Snippet.

              I’m going to work on this later today... I’ll post any progress here.
                • 10408
                • 8 Posts
                Hi mate, any progress on this?

                In particular I am interested to see how you populate the plug in parameters with values from the DB, as i’ve read through your wiki documentation on the topic and this is the only real bit thats missing from the implementation.

                Its reasonable to assume in most cases the list multi-select list of options will be data driven and not static so an example of this would be awesome.
                  • 30223
                  • 1,010 Posts
                  WebLoginPE is maintained on google-code: http://code.google.com/p/webloginpe/ If you find anything you think needs improving (and especially if you find a good solution) head over there and add an issue in the issue tracker. I’m sure they’ll appreciate some contributions smiley

                  For the rest I can’t help you I’m afraid. I’ve only looked at WLPE once myself ...
                    • 9207 ☆ A M B ☆
                    • 2,475 Posts
                    Galen-

                    I can update the wiki shortly (the big area that’s still missing is reading data BACK from the normalized db structure... that’s the other plugin I still need to write)... I’ve accomplished the feat of generating the multi-select based on the rows in one of my tables for my OWN form, but I haven’t used this to tie into WebLoginPE’s automatic handling. I’m debating whether it’s easier to go the route you’re looking at (i.e. priming the pump for WebLoginPE’s inputHandler), or if it’s better to write my own form generation code that handles populating values. One deciding factor might be WebLoginPE’s apparent inability to specify default values. Anyhow...

                    I’ve put the following function in a file on my web server, and then I have a Snippet that includes that file and executes the one function (I know... it’s more or less the same thing). But here’s my example (I’m using mysqli for my db connections, so you may need to install the php-mysqli package):

                    function list_animals_as_form_options() {
                    /*
                    INPUT: none
                    OUTPUT: String consisting of form elements for a multiple value drop down form, e.g.
                     <option value ="dogs_cats">Dogs & Cats</option>
                     <option value ="frongs_toads">Frogs & Toads</option
                    */
                    
                        $form_contents = '';
                        $link = connect_db('name_of_my_db_handle');
                    
                        /* check connection */
                        if (mysqli_connect_errno()) {
                                printf("Connect failed: %s\n", mysqli_connect_error());
                                exit();
                        }
                    
                        $sql = "SELECT animal, description FROM animals";
                        if ($result = $link->query($sql)) {
                            while ($row = $result->fetch_object()) {
                                $form_contents .= '<option value ="' . htmlentities($row->genre) . '">'. htmlentities($row->description) .'</option>';
                            }
                        }
                    
                        $link->close();
                        return $form_contents;
                    }


                    Please note that I’m referencing another file that contains my database connection functions. THAT file and the connect_db() function is formatted as I outlined in another article: http://www.fireproofsocks.com/php/preparing-mysql-statements-in-php-5/

                    The function above won’t work exactly for populating the WebLoginPE Snippet call... you’ll have to adjust the output format to match what WebLoginPE expects, i.e. something like:
                    Favorite Colors:userFavoriteColors:favorite_colors:select multiple:Red(ff0000),Orange(ff9900),Green(66ff00),Black(000000)


                    I might change my WebLoginPE Snippet call to something like:
                    [!WebLoginPE? &type=`profile` &profileTpl=`profileTpl` 
                    &customFields=`first_name,last_name,favorite_colors` 
                    &inputHandler=`Favorite Colors:userFavoriteColors:favorite_colors:select multiple:[[Populate_Values]]`!]
                    


                    There can be some issues if you nest Snippets like this. I think I read somewhere that you have to alternate [[cached]] and [!un-cached!]. There are couple pages on the Wiki about this I think:
                    http://wiki.modxcms.com/index.php/Snippet_call_anatomy

                    Another option is PHx... I haven’t used it myself yet, but I think it’s built for precisely this type of thing:
                    http://wiki.modxcms.com/index.php/PHx

                    Hope this helps. This post is waaaay to long for the wiki, I think... sorry for the length. If I had more time, it’d be shorter. wink
                      • 9207 ☆ A M B ☆
                      • 2,475 Posts
                      Snap... the select multiple option kacks if there are ampersands in the values. E.g.
                      &inputHandler=`Film Interests:genresID:genres:select multiple:action(Action & Adventure),animation(Anime & Animation)`


                      Won’t parse... your multi-select will only get the first item, then it dies with the ampersand. There are work-arounds, but I thought I should point this out.