We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37242 ☆ A M B ☆
    • 339 Posts
    Dear MODX people,

    I'd like to setup a form where user can enter new running events. With this thread: https://forums.modx.com/index.php?topic=54204.0 it's quite easy. But how can I accomplish it to make each city a folder? If I didn't have "Paris" yet and a user enters "Paris" as the location, the snippet should create a container inside the "locations" folder.

    You might ask "Why"? It's because I dont wand a locations folder with 800 resources in it. It's bad to manage and to find anything.

    Cheers,

    Guido

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

    • discuss.answer
      • 37242 ☆ A M B ☆
      • 339 Posts
      Ok, I managed to get it as I wanted. I have two different formIt calls now. One to add a city (which will act as a folder and menu item), and another to enter the actual event.

      Now I am trying to fire a tweet everytime someone enters an new event. That works as a plugin set to run on DocPublish. But with the resource created by formIt, this event does not get fired in the system. How yould you do it? As a snippet or a custom hook in formIt?
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        I would think you would use a pre-hook to generate a select drop-down for "location", and have a separate option, triggered by a checkbox, for creating a new location.
          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
          • 37242 ☆ A M B ☆
          • 339 Posts
          That would mean that I'd have to have two seperate inputs for the same value in the same form. The "location" is the parent for the rest of the forms content and can also be a new resource (as a container). I have no idea how to do this.
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            The "new location" checkbox? button? would trigger the display of a new input field "New Location", and the snippet would check for these and if they are set, ignore the "location" select drop-down, and set the parent and template of the new resource to whatever your "location" container resources should have. This means a bit of javascript involved, to show/hide the "new location" input field. So your form would only need those two extra fields, and all the work would be done by the snippet.
              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
              • 37242 ☆ A M B ☆
              • 339 Posts
              I know kind of how it should work, but my JavaScript skills are very limited smiley

              Right now I am focused on the "autotweet"-function. But I'll get back to the form later if I feel lucky smiley
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                JQuery to the rescue! Have the input field set in CSS to display:none; then attach this to your checkbox:

                <script>
                $('#mycheckbox').click(function() {
                  $('#myinput').toggle();
                });
                </script>


                Or to just show the input on checking the checkbox:
                <script>
                $('#mycheckbox').click (function () {
                    var thisCheck = $(this);
                    if (thischeck.is (':checked')) {
                        $('#myinput').show();
                    }
                });
                </script>


                http://api.jquery.com/toggle/
                  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
                  • 37242 ☆ A M B ☆
                  • 339 Posts
                  Oh Susan, you'to too kind smiley I dont know if its too much to add jquery to the site, just to toggle an input field. Its not done with thaht, as I'll have to edit the formit2resources snippet alot.

                  Can I create two different resources within the same snippet? this is what I do at the moment:
                  <?php
                  $doc = $modx->getObject('modResource',array('id'=>$hook->getValue('resource_id')));
                   
                  if (empty($doc)){
                      $doc = $modx->newObject('modResource');
                      $doc->set('createdby', $modx->user->get('id'));
                  }
                  else{
                      $doc->set('editedby', $modx->user->get('id'));
                  }
                   
                  $allFormFields = $hook->getValues();
                  foreach ($allFormFields as $field=>$value)
                  {
                   
                     if ($field !== 'spam' && $field !== 'resource_id'){
                           $doc->set($field, $value);
                      }
                  }
                  
                  /*---- edit ---*/
                  if ($hook->getValue('type')=='lauftreff') {
                  $doc->set('template', '2');
                  $fireTweet = true;
                  }
                  elseif ($hook->getValue('type')=='stadt') {
                  $doc->set('template', '3');
                  }
                  $doc->set('hidemenu', '0'); //hide from menu
                  $doc->set('published', '1'); //publicate at once
                  /*--- edit ---*/
                  
                  $doc->save();
                   
                  foreach ($allFormFields as $field=>$value)
                  {
                      if ($tv = $modx->getObject('modTemplateVar', array ('name'=>$field)))
                      {
                          /* handles checkboxes & multiple selects elements */
                          if (is_array($value)) {
                              $featureInsert = array();
                              while (list($featureValue, $featureItem) = each($value)) {
                                  $featureInsert[count($featureInsert)] = $featureItem;
                              }
                              $value = implode('||',$featureInsert);
                          }  
                          $tv->setValue($doc->get('id'), $value);
                          $tv->save();
                      }
                  }
                  
                  if ($hook->getValue('type')=='lauftreff') {
                  $params['link'] = $modx->makeUrl($doc->get('id'));
                  $modx->runSnippet('tweetOnPublish', $params);
                  }
                  
                  return true;


                  The tweet itself works, but I didn't figure out yet how to pass the new url to it. [ed. note: gallenkamp last edited this post 13 years, 8 months ago.]
                    • 37242 ☆ A M B ☆
                    • 339 Posts
                    Ok, I can tweet the ID of the new document. But this part does not work:
                    $params['link'] = $modx->makeUrl($doc->get('id'));

                    What do I miss?
                      • 42393
                      • 143 Posts
                      I dont know if its too much to add jquery to the site, just to toggle an input field.

                      I'm a complete noob with MODX but I recently worked out the following which sounds similar to this one aspect of what you're doing:

                      - I was looking for pagination of long pages, but I really don't like asking a user to paginate. So I thought it would be cool to put a jQuery accordion on the page and use Div tags to block content into page-like groups.
                      - Point here is that while I do have jQuery on all pages (standard template/chunks), I only need jQueryUI on some of them.
                      -I created a TV which gets applied to specific templates. Then selected resources make use of those templates. The TV is of type Radio to ask Yes/No if I want an accordion on that page.
                      - So when the template loads, a chunk checks the TV for the current resource, and if I need jQueryUI I insert the script tag, otherwise I skip it.

                      It works great,the pages look great. And since I'm loading the same script all the time, the little pain there is for this script is only incurred once and then it's on the users system for next time.

                      It would be more elegant if the resource had a token of some kind that told the parent chunks to load the script into the page header, but I'm not adept enough to do that yet. YMMV

                      HTH
                        Loved ModX when I was using it a few years ago. Shifted to WordPress, sorry. Thanks, all.