We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 813 ☆ A M B ☆
    • 326 Posts
    PLEASE excuse the length of this post; I don’t think it’s possible to ask for ideas on this one without explaining the goals and history of my project. smiley

    As some of you may know, I’m working on a fairly complex course catalog system for a university, built entirely on MODx. It’s definitely had its challenges, but the flexibility of MODx has made developing it a breeze (thank goodness for custom snippets and @INHERIT TVs, to name just a few).

    Now...onto one of those challenges! One of the trickiest parts of the project is that we need an entire catalog year’s worth of data to be duplicated for new catalog years as easily as using the "Duplicate Resource" function in MODx, while keeping the previous year as-is to serve as an archive. The duplicating part is easy, but the problem is, because MODx relies heavily on the unique IDs assigned to every resource in the system, I had to write a lot of custom snippets that returned an ID based on your level in the site hierarchy and your page title rather than the ID itself, and then use this generated ID as the start ID in other snippets like Ditto and Wayfinder. Otherwise, you’d have to go through and manually change a bunch of snippet settings, links, etc. every time you added a catalog year, which would be a nightmare.

    That problem is (mostly) solved, but now for my new issue: while some data, such as old catalog years, needs to be static, there is also a need for some of this archived data (such as a phone number or web site link) to be "live" data, so that it’s always current regardless of which catalog year you’re looking at. I have a system set up that does this using an extra table in the DB, a DB editor module and some predefined TVs, but this system can only be displayed in places where I’ve defined it in the template.

    What I want to do now is build more of a free-form live data system that lets my content editors throw in a short line of code to display data from a table in the DB, with as little coding know-how as possible (they will have access to edit the data in this table, so don’t worry about that aspect). I could write a snippet to do this and use snippet parameters to define what fields need to be pulled, like so...

    [!livedata? &tablerow=`23` &field=`url`!]


    ...with "tablerow" being the row in the table they want to use, and "field" being the info they want to pull. This may be the best method and I’m comfortable writing this snippet; however, I’m concerned about the complexity of that syntax. It would be nice if the content editors could write something simpler that avoids using backticks, worrying about caching, etc., like maybe a TV with a clever @SELECT binding, such as this:

    [*livedata_23_url*]


    Even that is a little complicated, but better. Or maybe, the best option is a plugin that watches for a certain syntax, and then makes the call? I worry a bit about the security of this option.

    I guess what I’m looking for are ideas. Has anyone tried anything like this before? Any suggestions or helpful tips? How would you go about it if you were creating this system?

    Thanks for reading my novel!!! wink
      • 4172
      • 5,888 Posts
      you can create a snippet, which generates modx-placeholders for each allowed db-row and field. Place this snippet on top of your template(s).

      <?php
      $fields='id,url,name,place';
      $rs=$modx->db->select($fields,$modx->getFullTablename('mytable'),'allowed=1');
      $rows=$modx->db->makeArray($rs);
      
      $fields=explode(',',$fields);
      
      if (count($rows)>0){
          foreach ($rows as $row){
              foreach ($fields as $field){
                  $modx->setPlaceholder('livedata.'.$row['id'].'.'.$field,$row[$field]);
              }
          }
      }


      And they can easy put something like [+livedata.23.url+] on every place after your snippet-call.
      This is what placeholders are for.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 813 ☆ A M B ☆
        • 326 Posts
        Thank you for the example code -- this is exactly what I’m trying to accomplish, and I think that syntax will be simple enough.

        However, it isn’t working quite right. Two scenarios are happening:

        1. If I call the snippet cached ([[livedata]]) and put the placeholder directly in the template it works fine, but if I call the snippet uncached ([!livedata!]) and put the placeholder directly in the template, it doesn’t show up. This seems backwards to me -- I usually have more issues with cached snippets.
        2. If I put the placeholder into a TV Rich Text textarea (which I need to be able to do), regardless of snippet caching, nothing shows up at all. I tried it in a plain-text TV, and it works, so long as scenario #1 is followed correctly. I’m guessing this has more to do with TinyMCE? Is there a way to use a placeholder in them that I don’t know about? Using the HTML button doesn’t work.

        Any idea what’s going wrong here?
          • 4172
          • 5,888 Posts
          hmm.... I put [!livedata!] on top of my template.

          and for example [+livedata.20.pagetitle+] into the field content with or without tiny.
          works perfectly.

          also when I put it into a richtext-tv and then these richttext-tv als nested placeholder into content with tiny it works for me.
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 813 ☆ A M B ☆
            • 326 Posts
            Hmmm, that’s strange. I will do a little experimenting and see if I can figure out why it doesn’t work for me.

            One other question: the DB editor module I’m using is set up to work with tables that don’t have the same prefix as the MODx system tables, to prevent my editors from messing up the site. How would I rewrite this to work with tables that don’t start with the prefix? It doesn’t seem to work if I just put the actual table name, which is "live_general".
              • 813 ☆ A M B ☆
              • 326 Posts
              Sorry, nevermind. I had some misplaced syntax in my snippet. It’s working with a non-prefixed table now.

              Still having the other issue, but I’ll keep working on it.

              Thanks!
                • 813 ☆ A M B ☆
                • 326 Posts
                After trying many different things, I’m still not able to use a snippet or a placeholder in a Rich Text TV, regardless of whether I paste it in using the wysiwyg or the HTML button. It appears to be caused by TinyMCE converting & into &amp;, because if I make the same call in a different TV that isn’t set as Rich Text, it works fine.

                Ideas? Is it possible to disable the entity encoding in TinyMCE? I’ve been reading other posts about this and it doesn’t appear to be an easy thing to do.
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  No, it’s not an easy thing to do. And the few discussions about it in the Moxiecode forums got a very hostile response from the Moxie developers. Since they think it’s a Good Thing, then it must be good for everybody. tongue
                    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
                    • 813 ☆ A M B ☆
                    • 326 Posts
                    I noticed that! They were definitely a little hostile and unwilling to listen to why their way might not be the best and only way.

                    I guess I’m back at square one. Or am I? I’m wondering why it doesn’t work for me, but Bruno17 was able to make it work. If this isn’t an option, does anyone have any other ideas?
                      • 813 ☆ A M B ☆
                      • 326 Posts
                      And maybe I’m misidentifying the problem? If I use the placeholder method rather than a snippet, it doesn’t work either, and that method doesn’t contain any "&" signs in the TVs. I’m totally confused at this point...