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

    I’d like to have the content of a file (comma separated values) displayed as a checkbox list.

    If a value is written into a text field, I’d like to have it appended to the text file.

    I suppose this works by TVs with @FILE (read), but how should I write into it? With @EVAL?

    Cheers
    Frisco
      • 3749
      • 24,544 Posts
      Just put something like this in the file (and nothing else):

      one==1||two==2||three==3

      and set the type of the TV to checkbox.

      The user will see one, two, three, but their selection will actually be resistered as 1, 2, or 3.

      If you don’t need to simplify things for the user, you can just use:

      one||two||three



      This won’t let you write to the file, however, for that you’d need a snippet or plugin. OTOH, you can just use the code above in the body of the snippet, let the user edit it there, (or in a chunk with @CHUNK), and forget about the file.


        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
        • 10487 MODX Staff
        • 1,535 Posts
        Garry Nutting Reply #3, 17 years ago
        I suppose this works by TVs with @FILE (read), but how should I write into it?
        Sounds like for the writing to the file, you’ll need to craft a plugin that runs on the OnDocFormSave event to capture the POST value and write it to the file.
          Garry Nutting
          Senior Developer
          MODX, LLC

          Email: [email protected]
          Twitter: @garryn
          Web: modx.com
          • 10190
          • 187 Posts
          Thanks,

          I like the idea of using a chunk with @CHUNK, would there be any built in means of appending an existing chunk?

          Otherwise I would code some PHP to append to the file.

          Cheers
          Frisco
            • 10190
            • 187 Posts
            ... what about using the database?

            IIRC there’s a binding for the DB. Could I display a checkbox list from a database field and add elements to the database from user input of a textfield (in the manager, shown from a TV type textfield)?

            Cheers
            Frisco
              • 10190
              • 187 Posts
              ... the docs say "@SELECT sql_query" could be use.

              As an sql query can both read and write to the DB, it should be possible to set up two TVs, one for reading from the DB and displaying the results as a list, the other for writing (appending) to the DB.

              I should put the default value to something that if the second TV (textfield) remains empty, nothing will be added - I wouldn’t like empty entries in the list of the first TV.

              What do you think?

              I’ll work that out tomorrow and report here.

              Cheers,
              Frisco
                • 10190
                • 187 Posts
                ... it seems to be possible as I depicted above, there’s only one problem remaining:

                As I wrote I have to insert new values into the DB. I could leave that up to MODx, as an input from a textfield TV is inserted into the DB, but this way I could only assign one new tag with each document, as MODx overwrites the value in the DB if I overwrite the value in the manager (default behaviour).

                For my special case this is not good, as I would like to have the possibility to add more then one new tag with a new article.

                A dirty approach would be to have more then one textfield, but this would always be limited to the number of textfields I provide, and I don’t like flooding my manager with TVs I use only sometimes.

                A good approach would allow me to add a new tag without overwriting the existing ones.

                I thought about putting the textfield input value to the SQL:

                INSERT INTO `modx`.`modx_site_tmplvar_contentvalues` (`id`, `tmplvarid`, `contentid`, `value`) VALUES (NULL, ’14’, ’0’, ’newTag’);

                but this has two problems:

                1. I would have to write @SELECT insert into ..., which is not correct SQL.
                2. I don’t know how to put the input value of the textfield inside the SQL query. Is there a placeholder, so that I could write something like "insert into ... (..., $newTag); ?

                Cheers
                Frisco
                  • 10190
                  • 187 Posts
                  ... alright, I can’t get the good approach working, so sometimes one has to live with the dirty ones ;-)

                  Here’s how I solved it, maybe it’s useful for someone else:

                  1. Built TVs for new tags (I take 5, so I can assign 5 new tags with each new article (additionally to existing ones, take more if you need more):
                  name: newTag_1 (_2, _3 etc.)
                  long name: 1. Input field for new tags
                  Description: put new tags in here and save (don’t overwrite existing ones or you delete them!)
                  type: Text
                  Choose the templates you will use the TV with.

                  2. Save and make a note of the TV’s id’s (in brackets behind the name, for me 14-18 (remember, I took 5 TVs!)

                  3. Built TV for displaying existing tags as a checkbox list:
                  name: tagList
                  long name: List of already existing tags
                  description: choose one or more, use the fields newTag_* if you need more
                  type: CheckBox
                  input option value: @SELECT distinct `value` FROM `modx`.`modx_site_tmplvar_contentvalues` WHERE (`tmplvarid`= 14 or `tmplvarid`= 15 or `tmplvarid`= 16 or `tmplvarid`= 17 or `tmplvarid`= 18)
                  (fill in the id’s here you noted in 2., take as much "or `tmplvarid`= xx" as you need)
                  Object type: Delimited List
                  Delimiter: ,
                  Choose the templates you will use the TV with.

                  4. Use a ditto call like
                  [!Ditto? &parents=`74` &summarize=`5` &total=`20` &commentsChunk=`CommentsBlog` &tpl=`BlogTemplate` &tagData=`tagList` &tagDisplayDelimiter=` ` &tagDelimiter=`,`!]
                  to bind the TV tagList with the delimiter "," to ditto.

                  That’s all.

                  Edit your ditto documents, fill in some tags in newTag_*, save, and activate them in the checkbox list afterwards. From the second document on, you can directly choose from the checkbox list - only when you add new tags you have to save the new tag and edit the document again to check them in the list.

                  Hope that helps.

                  Cheers
                  Frisco