We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    It would be much better to have a MIGX grid TV on an arbitrary resource. Use a drop-down select of a list of resources (perhaps based on parent, or on template) and select your top resources. Order them by dragging and dropping in the grid. At that point, that resource's TV value will be a JSON string of the resource IDs in the grid's order. You can fetch that resource's TV value and process the JSON string - convert it to an array and generate your list display.

    Actually it's already been done. http://rtfm.modx.com/extras/revo/migx/migx.tutorials/migx.sortable-resourcelist
      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 think this might be close to what you want (in a plugin attached to OnDocFormSave):

      $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 '';
      


      This should renumber the TV values from 1 to n while keeping them in the same order. [ed. note: BobRay last edited this post 11 years, 4 months ago.]
        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: I don't see how this could work. Say i have my top 10 and i want to move the page that is on rank8 to rank5 thus creating a new rank5.
        Then the old rank5 should become rank6, right? On the other hand if i would degrade rank3 to rank5 then the old rank5 should become rank4. I understand the foreach loop in your example to renumber the values for the TV. But i don't see how the getcollection would deal with the sortby when there are 2 rank5's? This is what would happen; the old rank5 and the new rank5 would both be in the collection, and there would be no rank8 (in my first example).

        @Susan: the resources in my top10 coould come from anywhere, not just 1 specific parent which is necesary for the MIGX solution, right?. And also It would mean that the editor can edit a page from the top10 and then has to go to another resource, that only exists for the sake of hosting a MIGX grid, to adjust the ranking for that page. Or am i misunderstanding the MIGX solution?

        RDG
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Select all of that TV's content values, filter out those with a value less than the new value for the current resource, and also filter out the one with the current resource's ID.

          Loop through them.

          For each one, add one to it 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
            • 4172
            • 5,888 Posts
            I'm sure, what you are trying should be possible with MIGX-grids.
            They reorder the selected resources by drag/drop.
            You can get the ordered list of resource-ids by looping the json and sort by that order with
            &sortby=`FIELD(modResource.id,[[getOrderedIds? &tvname=`migxtv` &docid=`XX`]])`


            or you can store the order onDocFormSave to a custom-table and use migxLoopCollection with a join to that table to get the sorted output.
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 4172
              • 5,888 Posts
              with your TV-solution, you would need a loop, which loops over all resources in a group and does something like that:

              https://github.com/Bruno17/MIGX/blob/master/core/components/migx/processors/mgr/default/handlepositionselector.php#L42
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 3749
                • 24,544 Posts
                If you change a rank8 page to rank5 by editing its TV, and there already is a rank5 page, you will have two rank5 pages. How would any code that runs later know which of the two rank5 pages should come first?


                Typically, PHP would put the one with the earlier creation date first (with a lower rank using my code).

                At any rate, my code would do this, which I still think might be what you want (in the example here 8 is changed to 5):
                Initial -- after editing TV -- rank after the code runs
                10               10                   10
                9                9                    9
                8                5                    6
                7                7                    8
                6                6                    7 
                5                5                    5
                4                4                    4
                3                3                    3
                2                2                    2
                1                1                    1 
                
                







                  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
                  @Bruno: i understand MIGX can reorder the rank on drag and drop but i want to be able to set each resource with its own rank TV without having to go to a seperate grid for that. I think its real elegant to have the same input type there is for menu index on each resource. so you can take a resource that has a rank of 8 and just turn the 8 to a 5 and all other resources that have a rank reorder on save.

                  @BobRay
                  Quote from: BobRay at May 29, 2015, 04:49 PM
                  If you change a rank8 page to rank5 by editing its TV, and there already is a rank5 page, you will have two rank5 pages. How would any code that runs later know which of the two rank5 pages should come first?

                  Typically, PHP would put the one with the earlier creation date first (with a lower rank using my code).
                  thats exactly the challenge. in your code above there are 2 ranks5. of course the resource that is being edditted has to change to rank5 the other one has to change to rank6. in your code above you'd have no control over which of the to rank5's would become rank6.

                  collection[0] = {id:0;rank:0}
                  collection[1] = {id:1;rank:1}
                  collection[2] = {id:2;rank:2}
                  collection[3] = {id:3;rank:3}
                  collection[4] = {id:4;rank:4} 
                  collection[5] = {id:5;rank:5}
                  collection[6] = {id:6;rank:6}
                  collection[7] = {id:7;rank:7}
                  collection[8] = {id:8;rank:8}
                  collection[9] = {id:9;rank:9}
                  

                  Now move nr 8 to 5th place
                  collection[0] = {id:0;rank:0}
                  collection[1] = {id:1;rank:1}
                  collection[2] = {id:2;rank:2}
                  collection[3] = {id:3;rank:3}
                  collection[4] = {id:4;rank:4} 
                  collection[8] = {id:8;rank:8}
                  collection[5] = {id:5;rank:5}
                  collection[6] = {id:6;rank:6}
                  collection[7] = {id:7;rank:7}
                  
                  collection[9] = {id:9;rank:9}
                  


                  and reindex
                  collection[0] = {id:0;rank:0}
                  collection[1] = {id:1;rank:1}
                  collection[2] = {id:2;rank:2}
                  collection[3] = {id:3;rank:3}
                  collection[4] = {id:4;rank:4} 
                  collection[5] = {id:8;rank:5}
                  collection[6] = {id:5;rank:6}
                  collection[7] = {id:6;rank:7}
                  collection[8] = {id:7;rank:8}
                  collection[9] = {id:9;rank:9}
                  

                  I just have'nt a clue how to do this with a plugin. Actually my first thought was doing this with an custom TV input type. but that's way over my head.
                    • 3749
                    • 24,544 Posts
                    thats exactly the challenge. in your code above there are 2 ranks5. of course the resource that is being edditted has to change to rank5 the other one has to change to rank6. in your code above you'd have no control over which of the to rank5's would become rank6.


                    My point is that *no* code will have control over that. As I said, the code has no way of knowing which one you want first.

                    That said, it's worth trying my code and setting the changed page to 4.5 or 5.5. That would allow you to say where you want it and my code would renumber them all to consecutive integers.
                      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
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      It's a basic sorting algorithm. Get in the database, get the values for that TV that are less than or equal to the new TV value for the current resource, skipping the current resource, then add one to each of the values you've retrieved. GetIterator would be your best bet, since all you're doing is adding one to each one of them.

                      You don't need to worry about the resources, other than the current one, just the TV's contentvalues.
                        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