We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21560
    • 145 Posts
    This is a ’quick-and-dirty’ snippet that is NOT meant for normal site-usage.

    Rather, the goal of this snippet is the menu-index sorting of children. Say, for example, like me you’ve created 73 documents in a parent and didn’t set the menu-index. And now you realize you do want to set it. But you don’t want to go through each and every document by itself. Or try and do the sorting through the (otherwise useful) menu sorting tool.

    It just wasn’t meant for more then 30 documents. And even that is pushing it. So I give you: MenuSort

    You can sort it using the same values you would do as Ditto. (sortBy and sortDir)

    Save as snippet: SortMenu
    <?php
    /* SortMenu micro snippet by Nimja.com
     * Usage   : [!SortMenu? &parent=`0` !]
     * Options : Sort field and sort direction.
     * Example : [!SortMenu? &parent=`0` &sortBY=`longtitle` &sortDir=`DESC` !]
     */
    
    //Parent HAS to be given, we can't do anything without a parent.
    if (isset($parent)) {
    	
    	// &sortBY, content-field to sort by, defaults to pub_date.
    	$sortBy = (!empty($sortBy) ) ? $sortBy : 'pub_date';
    	
    	// &sortHow, sort direction, defaults to ASC(ending).
    	//	Options are: ASC, DESC
    	$sortDir = (!empty($sortHow) ) ? $sortHow : 'ASC';
    	
    	//Fields we use (just ID the sorting comes automatically thanks to the Array).
    	$fields = 'id';
    	
    	//Use nice MODX function to get children. Inclused UNPUBLISHED children.
    	$pages = $modx->getAllChildren($parent, $sortBy, $sortHow, $fields);
    
    	//get full Table name to edit.
    	$table = $this->getFullTableName('site_content');
    	
    	//Go over each page.
    	foreach ($pages as $menu => $page ) {
    		//Get ID and make SQL.
    		$id = $page['id'];
    		$sql = 'update '.$table.' set menuindex='.$menu.' where id='.$id.';';
    
    		//echo $sql.'<br />';
    		$modx->db->query($sql);
    
    	}
    	echo 'Updating done.';
    } else {
    	//Return error message.
    	echo 'No parent given.';
    }
    ?>


    I hope it makes people happy.

    ps. This is MEANT to be used only to load once or so manually on an admin page. To make things a lot easier and you don’t have to mess with the database yourself (which I could have done, but this was more fun). Basically this is a show-and-tell of how to do little annoying things like this without a lot of manual action...

    Wait, that came out wrong.
      [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
      • 7455
      • 2,204 Posts
      the module that is shipped with modx called docmanager does that
        follow me on twitter: @dimmy01
        • 21560
        • 145 Posts
        No, it does not. Which is exactly why I wrote this. If you read my post fully, even though I was a bit vague, I mention DocManager’s possibility to sort menu items by hand.

        It doesn’t allow you to sort it by a content field (as far as I can tell, and I’ve looked again just now)

        The only options are "Save" "Sort Another" "Go Back" and "Close Doc Manager" Neither of which do what my snippet do.
          [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
          • 7455
          • 2,204 Posts
          Ok I understand now
          its sorted by any field you like. and automatic so no drag and drop.
            follow me on twitter: @dimmy01
            • 21560
            • 145 Posts
            Yep, that’s the important thing. It’s mainly a micro snippet for convenience then anything else. And it might be useful for people to learn from?
              [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
              • 10449
              • 956 Posts
              Nice! That might come in handy one day... thanks for sharing.
                • 21560
                • 145 Posts
                Hence the "micro snippet".

                It’s just ’one of those things wink
                  [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com