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.