We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17459
    • 268 Posts
    Hello!

    I would like to create plugin: after user save resource, if resource tpl_id == 123 (for example) and it is published , modx generate content and (create or update) static file (or static resource related with static file?) in directory hotels/[[*alias]].json in format:

    {"name": "[[*pagetitle]]", "another_key": "[[*myfirstTV]]", "another_key2": "[[*mysecondTV]]"}

    And as with another pages, there is normal template with html, so this json is separated static (!) file (1 static file per each published resource with tpl_id == 123), I can't just put it inside template because of 1. I need template for normal page 2. file should be static and located on Amazon S3. I'm not going to modify it somehow...

    I do not undestand what means static resource in terms of Modx: can I somehow use it in my task for simplicity reasons?.. For example, I added S3 bucket to ModX, it works well... Can I create static resource in ModX, set location to S3 and via this document create & update file on S3?..

    What api functions can help me?..

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

    • discuss.answer
      • 3749
      • 24,544 Posts
      I'm not sure what you want to do, but it sounds like you want a plugin attached to OnDocFormSave that looks something like this (untested):

      
      $template = $resource->get('template');
      $docId - $resource->get('id');
      
      if ( ($docId != 23)  || ($template != 123)) {
          return '';
      }
      
      $alias = $resource->get('alias');
      /* {"name": "[[*pagetitle]]", "another_key": "[[*myfirstTV]]", "another_key2": "[[*mysecondTV]]"} */
      $data = array(
          'name' => $resource->get('pagetitle'),
          'another_key' => $resource->getTVVAlue('myfirstTV'),
          'another_key2' => $resource->getTVValue('mysecondTV'),
      );
      
      $jsonData = $modx->toJSON($data);
      
      $file = MODX_ASSETS_PATH . 'hotels/' . $alias . '.json';
      
      $fp = fopen($file, 'w');
      if ($fp) {
          fwrite($fp, $jsondata);
          fclose($fp);
      } else {
          $modx->log(modX::LOG_LEVEL_ERROR, 'Could not open File: ' . $file);
      }
      
      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
        • 17459
        • 268 Posts
        BobRay, thank you!! You saved a lot of time for me!..

        > I'm not sure what you want to do

        We created little video widget for toursites about hotels... and I need to generate json data for every hotel and upload it to S3...


        Little question about 18 line - is it possible somehow select S3?.. ( http://rtfm.modx.com/revolution/2.x/administering-your-site/media-sources/media-source-types/media-source-type-s3 , I added this media source, 1 id is for file system, 2 for Amazon S3). I can upload file to S3 or Filesystem via ModX Admin, is it possible via API too?..

        something like $modx->create_or_update_file((int)$media_source, $file, $path_to_save);

        or it's not yet implemented in modx?..
          • 3749
          • 24,544 Posts
          You would have to use the S3 API directly. I've never done it, but look in the core/model/aws/ directory.

          This is the only documentation I could find: http://phpcrossref.com/xref/modx/core/model/aws/modaws.class.php.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
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            I was thinking that you might be able to do something with a single MIGx TV and a media source, but I haven't been able to figure out exactly how to do it.
              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
              • 17459
              • 268 Posts
              Thank you!..