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

    Is there a way to have the source code of a certain set of pages outputted by MODx into a field of a database table? I'd prefer this to happen with the least amount of interaction possible.

    I am thinking of a table where MODx's resource ID is the primary key, and where there is a column with the rendered resource's source text.

    How could I do that?
    Thanks!

    This question has been answered by neo2.0. See the first response.

      PHP illiterate MODx enthusiast
    • http://www.howtogeek.com/howto/programming/php-get-the-contents-of-a-web-page-rss-feed-or-xml-file-into-a-string-variable/

      Use the preferred method in a snippet, then save it to the custom database table. You can get the ID from the alias used in the URL.
        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
        • 36565
        • 73 Posts
        Thanks a lot, sottwell!

        file_get_contents() looks good. Problem is, the relevant pages are in a password protected zone, and I'd like to keep it that way. So if I just take the http url as argument to file_get_contents(), I get nothing.

        Is there a way to bypass the web server and grab the content directly from MODx?
          PHP illiterate MODx enthusiast
          • 4172
          • 5,888 Posts
          I think you can use a plugin for OnWebPageComplete, which can do some checks and stores the rendered output, when you visit the page.
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 36565
            • 73 Posts
            Alright, a plugin! Now I just looked into the API the first time - it looks daunting.

            Would you mind a little hand-holding? Just dropping some object names or method names (or similar) that point in the right direction would be appreciated.

            I am going to delve in the documentation, but right now I don't know where to start looking.
              PHP illiterate MODx enthusiast
              • 4172
              • 5,888 Posts
              this code in a plugin stores the rendered page into a chunk if you call the page with
              ?store=1
              in the url and the resourceID is in the storeResources-array:

              <?php
               
              if (isset($_REQUEST['store'])) {
                  $storeResources = array(1, 2, 5);
                  $chunkname = 'debug';
                  if (in_array($modx->resource->get('id'), $storeResources)) {
                      if ($chunk = $modx->getObject('modChunk', array('name' => $chunkname))){
                          
                      }else{
                         $chunk = $modx->newObject('modChunk');
                         $chunk->set('name',$chunkname); 
                      }
                      $chunk->setContent($chunk->getContent() . $modx->resource->_output);
                      $chunk->save();
                  }
              }
              [ed. note: Bruno17 last edited this post 12 years, 4 months ago.]
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 36565
                • 73 Posts
                Hey, thank you, Bruno!

                I have put the code into a new plugin and I can confirm that it works (by putting print("Success!"); into appropriate places.

                Now I'm trying to have a look at the new chunk. I suppose its name is 'debug' by guessing at the array in the getObject method, but how to handle it? No chunk so named shows up in the elements tree...

                I have tried print($chunk); but that outputs nothing.
                  PHP illiterate MODx enthusiast
                  • 4172
                  • 5,888 Posts
                  the plugin didn't create the chunk with name 'debug', you had to create it by yourself.

                  But now it does.
                  You can see the result, when you open the chunk 'debug'.
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 36565
                    • 73 Posts
                    Great, it's working!
                    One thing that puzzles me is that my print("..."); commands don't have any effect anymore, even though with the prior version they did put strings into the source code.

                    Why could that be? I thought the plugin suddenly stopped working at all.

                    Anyway, thanks a lot for your awesome help!

                    Next thing up is a mechanism that selects the right resources, strips the EOLs from the chunk content and writes it into the database table. I'll see how far I will get smiley
                      PHP illiterate MODx enthusiast
                      • 3749
                      • 24,544 Posts
                      Print and echo send their output to the output buffer. Since there really isn't one for a plugin, they go nowhere.

                      You might also want to check out the EmailResource plugin, which could easily be modified to put the result in the DB instead of emailing it. It has the advantage of converting any CSS files to inline CSS (which you might or might not want).
                        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