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

    I wanted to run this past you folks to see if I'm approaching this the right way.

    So I have a protected file area set up - files are in a directory protected by a .htaccess file - and each of these files (mostly PDFs) in turn has a static resource for it, which can have varying permissions set - registered members, strategic partners, anonymous, etc.

    Each of these static resources I am applying a template to to accomplish different things:

    1 - just download the file
    2 - download and email someone inside our org
    3 - download and log to db.
    4 - download, email someone and log to db.

    I originally accomplished this by inside each of the templates executing a snippet before the content - so for the #4 scenario above, the template looked like this:

    [[logDownloadSnippet]]
    [[emailUserSnippet]]
    [[*content]]

    This setup worked pretty well, but i did find it somewhat unreliable, and it did not work at all in Internet Explorer for some reason. I suspect because even though each of those snippets returned nothing, it was buggering up the PDF formatting enough to confuse IE. So... I went back to the drawing board.

    Which brings me to where I am today. I modified each of my action templates to only include [[*content]] which now works in all browsers.

    Now - to accomplish the tasks the snippets previously performed, I wrote a plugin that evaluates on EVERY page load (via the system event: OnWebPagePrerender) what template is currently driving the current resource. Based on this, the plugin executes the various tasks. Seems to be working perfectly.

    So - what brings me here? A gnawing suspicion that perhaps there is a better way. Am I adding a crazy amount of overhead by executing this template check on EVERY page prerender? Is there a better way to attach a plugin action to a template or resource?

    Basically, this is the abbreviated template check:

    switch ($thisTemplate) {
    case 'Downloader.Emailer.Logger':
    sendTheEmail();
    logTheDownload();
    break;
    case 'Downloader.Emailer':
    sendTheEmail();
    break;
    case 'Downloader.Logger':
    logTheDownload();
    break;
    }

    Thanks in advance for the sanity check guys (and/or gals). Much appreciated

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

    • No, I think that's about as good as you're going to get.

      By the way, you can store your files outside of the web root, and static resources can still access them. No need for the htaccess directives.

      You can create a media source for the directory, and the static resources will see the media source structure. It won't pick up the media source definiton's path, though, so I use a chunk with the same path as set in the media source, for example '../downloads/', and prefix the path that the static resource does pick up with the chunk:
      [[+pathPrefix]]rest/of/path/to/file.pdf
        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
      • discuss.answer
        • 3749
        • 24,544 Posts
        Note that if you're using $modx->resource->get('template'), you'll be getting the template's ID, not its name, but using the ID will be much faster than getting the template object and retrieving its name, which would be the alternative.
          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
          • 40041
          • 36 Posts
          Quote from: sottwell at Jul 03, 2013, 04:18 AM
          No, I think that's about as good as you're going to get.

          By the way, you can store your files outside of the web root, and static resources can still access them. No need for the htaccess directives.

          You can create a media source for the directory, and the static resources will see the media source structure. It won't pick up the media source definiton's path, though, so I use a chunk with the same path as set in the media source, for example '../downloads/', and prefix the path that the static resource does pick up with the chunk:
          [[+pathPrefix]]rest/of/path/to/file.pdf

          Interesting! I tried this in the beginning, but I could not get it to work for some reason. The files never got served up. I'll revisit, as this would definitely be preferred. Thanks!
            • 40041
            • 36 Posts
            Quote from: BobRay at Jul 03, 2013, 07:53 AM
            Note that if you're using $modx->resource->get('template'), you'll be getting the template's ID, not its name, but using the ID will be much faster than getting the template object and retrieving its name, which would be the alternative.

            Awesome Bob - great advice as always. I was retrieving it's name and will adjust to grab id instead. Many thanks!
              • 3749
              • 24,544 Posts
              Quote from: robcarey at Jul 03, 2013, 02:07 PM
              Quote from: BobRay at Jul 03, 2013, 07:53 AM
              Note that if you're using $modx->resource->get('template'), you'll be getting the template's ID, not its name, but using the ID will be much faster than getting the template object and retrieving its name, which would be the alternative.

              Awesome Bob - great advice as always. I was retrieving it's name and will adjust to grab id instead. Many thanks!

              It sounds like you might possibly be misunderstanding me. I'm suggesting not getting the Template object at all, but rather just testing the ID you get from $modx->resource->get('template'):

              (replace the numbers with the actual IDs of the templates)

              switch ($modx->resource->get('template')) {
                  case 3:
                      sendTheEmail();
                      logTheDownload();
                      break;
                  case 5:
                      sendTheEmail();
                      break;
                  case '4':
                      logTheDownload();
                      break;
              }
              
              return '';
              

                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
                • 40041
                • 36 Posts
                Quote from: BobRay at Jul 03, 2013, 09:01 PM
                Quote from: robcarey at Jul 03, 2013, 02:07 PM
                Quote from: BobRay at Jul 03, 2013, 07:53 AM
                Note that if you're using $modx->resource->get('template'), you'll be getting the template's ID, not its name, but using the ID will be much faster than getting the template object and retrieving its name, which would be the alternative.

                Awesome Bob - great advice as always. I was retrieving it's name and will adjust to grab id instead. Many thanks!

                It sounds like you might possibly be misunderstanding me. I'm suggesting not getting the Template object at all, but rather just testing the ID you get from $modx->resource->get('template'):

                (replace the numbers with the actual IDs of the templates)

                switch ($modx->resource->get('template')) {
                    case 3:
                        sendTheEmail();
                        logTheDownload();
                        break;
                    case 5:
                        sendTheEmail();
                        break;
                    case '4':
                        logTheDownload();
                        break;
                }
                
                return '';
                


                Nope - I got your gist Bob and implemented it exactly as above - works perfect! Thanks!