We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 50520
    • 7 Posts
    I asked this question in a different thread, but maybe it's best to open a new thread for the question. Basically;

    I plan to run a very simple MODx site, with 2 or 3 editable areas as a MODx example for potential customers. They will be able to edit and save TV's with a publicly displayed login.

    My question is;

    Can I run a cron job with a script to update the database, once a day to reset the TV's to what they were before? For example, a customer edits a TV with the demo account, then saves, and views the content. Later a cron resets the database table for those TV's back to the default. Is that possible or will it cause issues, or even better is there a built in feature to allow the something similar?

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

      • 3749
      • 24,544 Posts
      Not as you describe it. Once the TV values are saved, the old values are lost.

      You can't save the old values when the Resource is saved because if the resource is saved multiple times, the original values are also lost.

      The only way I can think of to do it is to save the original values somewhere (file, chunk, DB table, etc.). Then your cron job would run a snippet that retrieves the values and sets them. The details would depend on whether the original values are the same for each resource, or different for different resources and how many resources are involved.

      I would probably save them in a file if the original values seldom or never change, and in a chunk if they change often.

      [update] If the default TV values are the same for all resources, it would be easier to make the original value the default value of each TV. Then your cron job snippet could just reset them all to their default values.
        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
      • The easiest way to do that would be to dump the site_tmplvar_contentvalues table, then the cron job would just import the resulting .sql file.

        http://stackoverflow.com/questions/4027832/i-need-to-restore-a-database-mysql-every-30-minutes-using-a-cron-job
          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
          I thought of that, but whenever new resources are added or deleted or the TVs are changed, you'd have to dump the table again or values would be lost.

          If resources are deleted and/or new ones are added and you forget to dump the table, I think the snippet might crash or corrupt the modTemplateVarResource table, unless you're restoring the whole DB, which is kind of overkill for this use case.

          It's a great solution if the site never changes, though.
            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
            • 50520
            • 7 Posts
            Quote from: BobRay at May 29, 2015, 04:57 AM
            I thought of that, but whenever new resources are added or deleted or the TVs are changed, you'd have to dump the table again or values would be lost.

            If resources are deleted and/or new ones are added and you forget to dump the table, I think the snippet might crash or corrupt the modTemplateVarResource table, unless you're restoring the whole DB, which is kind of overkill for this use case.

            It's a great solution if the site never changes, though.

            So based on the ideas you guys came up with;

            What if the only TV's in the site are the ones I plan for the users to edit, and they all have the same value (edit this area). The cron job could run an external script with a query something like...

            UPDATE 'modx_site_tmplvar_contentvalues' SET value='<p>Edit this Area</p>' WHERE value !='<p>Edit this Area</p>'


            SQL is not my strong point, but would that work if all the TV's are the same?
            • discuss.answer
              • 4172
              • 5,888 Posts
              you can create a snippet and put it on a resource's template or in its content with code like that:

              <?php
              
              $key = 'lj45jlkj453222l526h12k1l2';
              if (isset($_REQUEST['key']) && $_REQUEST['key'] == $key) {
                  $default_text = '<p>Edit this Area</p>';
              
                  $c = $modx->newQuery('modTemplateVarResource');
                  $c->where(array('value:!=' => $default_text));
              
                  if ($collection = $modx->getCollection('modTemplateVarResource', $c)) {
                      foreach ($collection as $object) {
                          $object->set('value', $default_text);
                          $object->save();
                      }
                  }
              }
              


              and call the url of that resource together with the key in the url with your cronjob.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 50520
                • 7 Posts
                Quote from: Bruno17 at May 29, 2015, 12:52 PM
                you can create a snippet and put it on a resource's template or in its content with code like that:

                <!--?php
                
                $key = 'lj45jlkj453222l526h12k1l2';
                if (isset($_REQUEST['key']) && $_REQUEST['key'] == $key) {
                    $default_text = '<p>Edit this Area</p>';
                
                    $c = $modx->newQuery('modTemplateVarResource');
                    $c->where(array('value:!=' => $default_text));
                
                    if ($collection = $modx->getCollection('modTemplateVarResource', $c)) {
                        foreach ($collection as $object) {
                            $object->set('value', $default_text);
                            $object->save();
                        }
                    }
                }
                


                and call the url of that resource together with the key in the url with your cronjob.

                Thank you very much for taking the time out to create this example. I will give it a try tonight. [ed. note: Bruno17 last edited this post 8 years, 11 months ago.]
                  • 3749
                  • 24,544 Posts
                  It sounds like you will only have one TV that needs processing and it will have the same value for all resources.

                  In that case, I would just do this in the cron snippet:

                  $tvId = ##;  // set this to the ID of the TV
                  $modx->updateCollection(
                       'modTemplateVarResource',
                           array('value' => '<p>Edit this Area</p>'),
                           array('tmplvarid' => $tvId)
                  );


                  You could add Bruno's key if you don't want it to execute when someone else visits the resource.

                    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