We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40045
    • 534 Posts
    I'm not sure if I didn't use the right search terms, but I couldn't find anything about this kind of task.

    A little background information first:

    I plan to create a cutom(izable) voting component for an actual project. The existing Polls extra is very limited and thus not working for me in this case. I need to be able to add Images, Descriptons, Audiofiles, Video and maybe even more (should also be extendable for future feature requests) for every Answer (on an optional basis...). Also the case can occur, that users first have to input Names/Email before being able to submit their vote (to make the voting as "safe" [ie. not influenceable by users] without blocking whole companies [with the same IP] from voting) .

    Just startet thinking about it but basically I'm planning to handle the whole thing via FormIt and custom hooks for voting and getImageList to output the different voting options. Every voting would be a separate resource in a container, let's be creative, called "Votings". These resources would have a MIGX grid for the options. Like this I think I could be very flexible in creating different kinds of (extensible) votings and ease the managing for the editors (because they don't have to learn a custom cmp, and creating cmps is currently not really in my skillset bc of lacking extjs knowledge).

    Question
    I don't see any problems in creating the votings itself, as MIGX is very flexible in input/output by nature but I actually cannot think of a way to save the actual votes for the options from frontend when the voting is active (publish/unpublish through native document variables)...maybe I'm just not thinking in the right direction but how can I manipulate the JSON of a MIGX grid in an elegant way through a snippet (custom hook)?

    I don't think it's possible to just set the TV value (hidden TV for MIGX grid) via
    $votingresource = $modx->getObject('modResource', 123);
    $newnumberofvotes = $votingresource->getTVValue('migx.voting.votes') + 1;
    $votingresource->setTVValue('migx.voting.votes', $newnumberofvotes);
    


    because that probably would not be reflected in the MIGX TV JSON itself and it needs to be individual for every option (because no saving through manager would occur, so how should the JSON be updated and from where should the grid "know" for which option a vote was given...).

    This would actually be the easier part for the "simple" votings with no names/mailaddress/ip/activationmail etc.

    The harder part are these safe votings where I would need to save every single vote as a custom row (probably another MIGX grid, just for the votes). So to say create complete MIGX grid rows from the frontend via snippet...

    The only possible way I can think of doing this (=manipulating the content of a MIGX TV) is to parse the MIGX JSON in PHP via something like:
    $votingresource = $modx->getObject('modResource', 123);
    $migxjson = $modx->fromJSON($votingresource->getTVValue('migx.grid.voting.votes')); // get migx json from votes grid and parse it to array
    
    // get data from form and create new MIGX grid row by pushing the new vote to the end of the above parsed JSON string and converting it back to JSON
    $fields = array('date' => '1359744625', 'names' => 'Firstname Lastname', 'optionid' => 2, 'optiontitle' => 'Option X', 'IP' => '192.168.100.30', 'mailhash' => 'YW5pdGFoQGJsdWV3aW4uY2g=', 'active' => 0);
    $newmigxjson = $modx->toJSON(array_push($migxjson, $fields));
    
    // save it back to the db
    $votingresource->setTVValue('migx.grid.voting.votes', $newmigxjson);
    // does this actually save the value or just set it and it needs to be saved separately?
    


    but I'm definitely not sure if this is the way to go or if there is a much better approach for doing this? Any suggestions?

    Thanks in advance! [ed. note: exside last edited this post 11 years, 2 months ago.]
      • 4172
      • 5,888 Posts
      why not storing the votes into a custom-table connnected by resource-id to the vote-resource and use something like formit2db to collect the votes into the db-records.
      I would create an additional table for all the votes. This way you can store also all user-informations of each vote into the db.

      the user can manage the votings by a MIGXdb-TV the same way as with a normal MIGX-TV (without drag/drop - ordering)
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 40045
        • 534 Posts
        Hi Bruno, thanks for the answer!

        1. I did not really digg into migxdb yet (need to do that soon anyways)

        and

        2. The drag and drop feature is crucial to reorder the answers =/

        so I guess this is equal to a "no, there is no easy way to interact with a MIGX grid from the frontend"?
          • 4172
          • 5,888 Posts
          well, reordering is possible, just by adding a position field to the custom-table.
          Also manipulating this pos-fields in all records by drag/drop or by selecting a new position by listbox or selecting from grid would be possible in theorie.

          'no easy way' is relativ. Of course its possible to manipulate the MIGX-json with something like formit2db before storing it back to the TV-Value.
          I would say, not more than one or two hours of work.

          But I would suggest to store the records into a custom-table. (you can reuse most of your MIGX-configuration)
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!