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

    I am creating a form where a user can input data. After sending the form, a document will be created based on the users input.

    So far, the creation of the document works fine.

    My problem is, that the document needs to be saved in a container based on a selection that the user makes in the form.

    My manager tree has the following structure (Show is the main container):

    Shows (ID 1)
    -> Show1 (ID 2)
    -> ->2012 (ID 3)
    -> -> -> January (ID 4)
    -> -> -> February (ID 5)
    and so on

    In the form, I have a date field. The user chooses a date (eg. 26.02.2012). Now the document should be created in Shows -> Shows1 -> 2012 -> February

    I have no clue, how I can get the ID of the right container based on the users input in the date field.

    Any help is appreciated.
      Modx 2.1.3, PHP 5.2
      • 27117
      • 28 Posts
      Maybe I have to add some additional infos:

      The form is made with formit and I use the following snippet as a posthook in the form call:
      $doc = $modx->newObject('modResource');
      $doc->set('class_key','modResource');
      $doc->set('context_key','web');
      $doc->set('type','document');
      $doc->set('contentType','text/html');
      $doc->set('pagetitle',$pagetitle);
      $doc->set('longtitle',$longtitle);
      $doc->set('introtext',$introtext);
      $doc->set('alias',$alias);
      $doc->set('published','1');
      $doc->set('parent','34');
      $doc->set('isfolder','0');
      $doc->setContent($content);
      $doc->set('richtext','1');
      //$doc->set('menuindex','99');
      $doc->set('searchable','1');
      $doc->set('cacheable','1');
      //$doc->set('menutitle','FAQ');
      $doc->set('donthit','0');
      $doc->set('hidemenu','0');
      $doc->set('publishedon',$publishedon);
      $doc->set('template',$template);
       
      $doc->save();
      
      // Save TVs for NEWS
      $tv = $modx->getObject('modTemplateVar', array ('name'=>'mod')); 
      $tv->setValue($doc->get('id'), $mod);
      $tv->save();
      
      $tv = $modx->getObject('modTemplateVar', array ('name'=>'tb_send_date')); 
      $tv->setValue($doc->get('id'), $send_date);
      $tv->save();


      For now, the parent part is hardcoded with id 34 but this should change when I solved my problem.

      It would be nice, if someone could help me with a tip or a hint into the right direction.
        Modx 2.1.3, PHP 5.2
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        You'll have to have some PHP code to parse the date and determine the parent accordingly. A PHP switch would probably be the way to go; you can even have it create the parent resource if it doesn't already exist.
          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
          • 3749
          • 24,544 Posts
          First, have you tried NewsPublisher? It will do most of that for you automatically and will let you set the parent in the snippet tag. It will also provide a datepicker for the date TV.

          Second, if you are going to roll your own, it will be easier and more reliable to use the recource/create processor to create the resource for you. You can look at the NewsPublisher class file for an example.


          ---------------------------------------------------------------------------------------------------------------
          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
            • 27117
            • 28 Posts
            Thanks for your replies.

            @sottwell: parsing the date is not really the problem. But I can't figure out, how to find the appropriate parent folder. Because there are several shows and therefore several containers with pagetitle "2012", "January" or February, this makes everything a little bit more complicated (at least for me as a non-regular coder smiley

            @BobRay: I heard about NewsPublisher but I thought, it was not yet ready for Revolution but as I just saw, it is smiley I will give it a try but because I have already written a lot of code for my own solution and I really like to dive deeper into Modx and learn as much as I can, I think I will go with my own snippet/form.

            Maybe you can explain why it is better to use resource/create processor instead of
            $doc = $modx->newObject('modResource');


            The above code is mostly copied from your post in this thread:
            https://forums.modx.com/thread/?thread=35982#dis-post-202277
            so I thought it will be ok.
              Modx 2.1.3, PHP 5.2
              • 3749
              • 24,544 Posts
              I think that post was written before the resource/create processor was enabled.

              The resource/create processor will fill in the createdon date and has a bunch of built-in sanity checks that can protect you from some errors. It will also do an automatic alias for you if the system setting for that is set. The processor will also clear the cache for you, so it the new page is published, it will show up in the menus right away. It will also give you an appropriate error message to show if the process failed for any reason (e.g. a duplicate alias or no alias).

              I'm not sure, but I think using the processor may also be safer if you're using user input in the front end for the resource fields.


              ---------------------------------------------------------------------------------------------------------------
              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
                • 27117
                • 28 Posts
                Thank you for the explanation Bob.
                If I will do it with the resource/create processor, how should I modify the above script? I couldn't find a lot of infos about how to use the processor.

                Thanks to your article on your website
                http://bobsguides.com/revolution-objects.html
                I am now pretty familiar with the ModX Objects but I have no clue how to modify the above script to use the resource/create processor.
                  Modx 2.1.3, PHP 5.2
                  • 3749
                  • 24,544 Posts
                  Take a look at the saveResource() function in this file: https://raw.github.com/BobRay/newspublisher/master/core/components/newspublisher/classes/newspublisher.class.php


                  Here's the key part:

                  <?php
                          $response = $this->modx->runProcessor('resource/create', $fields);
                  
                          /* @var $response modProcessorResponse */
                          if ($response->isError()) {
                              if ($response->hasFieldErrors()) {
                                  $fieldErrors = $response->getAllErrors();
                                  $errorMessage = implode("\n", $fieldErrors);
                              } else {
                                  $errorMessage = 'An error occurred: ' . $response->getMessage();
                              }
                              $this->setError($errorMessage);
                              return '';
                  
                          }





                  ---------------------------------------------------------------------------------------------------------------
                  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
                    • 27117
                    • 28 Posts
                    Thank you Bob, I'll look at this.
                      Modx 2.1.3, PHP 5.2