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

    @Susan: how would i get all values from site_tmplvar_contentvalues?
    $tvs = $xpdo->getIterator('modTmplVars', array('tmplvarid' => 8));

    doesn't sem to work

    @BobRay:right now i have this function
    function array_move(&$a, $oldpos, $newpos) {
        if ($oldpos==$newpos) {return;}
        array_splice($a,max($newpos,0),0,array_splice($a,max($oldpos,0),1));
    }


    and it works fine. i can move the elements i the array and the order goes fine. the problem is how to get the values for rank.
    when i try to get the values for rank (the old and the new value for a particular resource) i do this

    switch($eventName) {
        case 'OnBeforeDocFormSave':
           $told = $resource->getTVValue('rank');
            break;
            
        case 'OnDocFormSave':
            $tnew = $resource->getTVValue('rank');
            break;
    }

    onBeforeDocFormSave i can retrieve the 'old' value for rank
    and on onDocFormSave i can retrieve the 'new' value for rank. But i can not retrieve both at the same time. and i need them for my sorting function.
    Is there a way to retrieve the value for the new rank, so the value thats filled in in the form, onBeforeDocFormSave?
    I tried to use $form->getTVValue('rank') bur thats not working

    Tnx
    RDG
      • 4172
      • 5,888 Posts
      you can store into a global array, for example into $_REQUEST or into the MODX-object at OnBeforeDocFormSave and retrieve it later at OnDocFormSave
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 24629
        • 370 Posts
        Hi Bruno,
        Thats a good idea unfortunately this
         $_REQUEST['rankold'] = $resource->getTVValue('rank');

        does not seem to work.
        i stuffed into a plugin and the saving process hangs when i try this.
        But based on your idea i tried this:
         $modx->rankold = $resource->getTVValue('rank');

        And that does work but i'm not sure it's good practice to just attach a new var to $modx????

        RDG
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          it's modTmpleVarContent, or something like that. You only need to deal with that one table, it contains the ID of the relevant resource, as well as the ID of the TV and the value.
            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
            • 4172
            • 5,888 Posts
            modTemplateVarContent


            keep in mind, that you will only get the TV-values of Resources, where the value was allready set and not empty.
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              It's the modTemplateVarResource object, and is stored in the modx_site_tmplvar_contentvalues table.

              So you would use getIterator('modTemplateVarResource' ... ) where the tmplvarid == your TV's ID, and the contentid != the current resource's ID, and the value is less than or equal to the new value you just set for the current resource. Loop through the iterator, add 1 to each value and save 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
                • 24629
                • 370 Posts
                Thanks, i managed to get all the tvs with
                $tvs = $modx->getIterator('modTemplateVarResource', array('tmplvarid' => $tvid));

                But i'm not sure how to work with that. the RTFM states its an 'xPDOIterator' but i can't find a description of that. is it an indexed array? an object? a multidimensional array? print_r doesn't show anything..
                I'm looking for a way to sort this iterator by 'rank'?
                Tnx
                RDG
                  • 3749
                  • 24,544 Posts
                  Or you can use my code from three days ago that does exactly that and lets MySQL do the sorting for you (unless I'm misunderstanding you). wink

                  You can change getCollection to getIterator if you like -- the arguments are the same. Both will return an array of modTemplateVarResource objects.


                  $tvId = ##;  // Change this to the ID of the top10 TV.
                   
                  $c = $modx->newQuery('modTemplateVarResource');
                  $c->sortby('value', 'ASC');
                  $c->where('tmplvarid' = $tvid);
                   
                  $tvrs = $modx->getCollection('modTemplateVarResource', $c);
                   
                  $i = 1;
                  foreach($tvrs as $tvr) {
                     $tvr->set('value', $i);
                     $tvr->save();
                     $i++;
                  }
                  return '';


                  As I said, you may be able to control the rank of changed pages by using a floating point value like 4.5 for the changed TV. The code above should retrieve it between the pages ranked 4 and 5 (though I haven't tested this). The old 4 will remain at 4 the new 4.5 will become 5 and the old 5 will become 6. If you use 5.5 on the changed page instead, the old 4 will remain 4, the old 5 will remain 5 and the new 4.5 will become 6.
                    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
                    • 24629
                    • 370 Posts
                    Hi BobRay,

                    the value field seems to be text formatted so doing the sortby results in a collection:
                    rank = 1
                    rank = 10
                    rank = 12
                    rank = 2
                    rank = 22
                    rank = 3
                    rank = 4
                    so i don't think i can let MySQL do the sorting
                    RDG

                      • 3749
                      • 24,544 Posts
                      I was afraid of that, but thought it was worth a try. There are some workarounds here, but none of them very attractive.

                      I think you could cheat by using a leading zero when you save the single-digit ones, and put an extra character on the end of ones you want to come after a duplicate, but I can't think of a way to get them to come before a duplicate.
                        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