We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21255
    • 215 Posts
    The following plugin...
    ...reorganizes the menuindex after saving so that a document with menuindex just set to [tt]n[/tt] really appears at [tt]n[/tt]th position in menu.
    ...stops auto-incrementing menuindex if all childs have set it to 0 (for alphabetical sortings like [tt]ORDER BY menuindex, pagetitle DESC[/tt]).

    It’s written from scratch, bad code, but hopefully useful for someone wink

    global $content;
    $e = &$modx->Event;
    
    switch($e->name) {
    
      case 'OnDocFormPrerender':
        if(($_REQUEST['pid'] > 0) && ($id == 0)) {
          $query = $modx->db->query("SELECT MAX(menuindex) FROM {$modx->db->config['table_prefix']}site_content WHERE parent = {$_REQUEST['pid']}");
          $menuindex = intval($modx->db->getValue($query));
          if($menuindex>0) {
            $content['menuindex'] = $menuindex++;
          } else {
            $content['menuindex'] = 0;
          }
        }
        break;
    
      case 'OnDocFormSave':
        $query = $modx->db->query("SELECT parent, menuindex FROM {$modx->db->config['table_prefix']}site_content WHERE id = {$id}");
        if($current = $modx->db->getRow($query)) {
          $query = $modx->db->query("SELECT id FROM {$modx->db->config['table_prefix']}site_content WHERE parent = {$current['parent']} AND menuindex > 0 ORDER BY menuindex ASC, editedon DESC");
          $i = 1;
          while($sister = $modx->db->getRow($query)) {
            $update = $modx->db->query("UPDATE {$modx->db->config['table_prefix']}site_content SET menuindex = {$i} WHERE id = {$sister['id']}");
            $i++;
          }
        }
        break;
    
      default:
        return;
        break;
    
    }
    


    To use it, create a new plugin, paste the code and select OnDocFormPrerender and OnDocFormSave checkboxes in System Events Tab.
      • 36805
      • 354 Posts
      Super cool! What a hidden gem this is.

      Your post is very old, i intend to try it with evo 1.0.4.

      From the looks of the code this *should* not be a problem though.

      Thanks a bunch for sharing!
        • 36805
        • 354 Posts
        OK so I did try it and it sort of worked. However I found an off-by-one bug where it would not correctly place an item at the highest menu index.

        It took me a while to figure out how the plugin works. It uses a trick by selecting in current order and secondarily by edit time. This is clever but causes problems in few cases in which the ORDER BY must be reversed. So here’s a fixed version. It did not let me down so far, use at your own risk though.

        global $content;
        $e = &$modx->Event;
        
        switch($e->name) {
        
          case 'OnDocFormPrerender':
            if(($_REQUEST['pid'] > 0) && ($id == 0)) {
              $query = $modx->db->query("SELECT MAX(menuindex) FROM {$modx->db->config['table_prefix']}site_content WHERE parent = {$_REQUEST['pid']}");
              $menuindex = intval($modx->db->getValue($query));
              if($menuindex>0) {
                $content['menuindex'] = $menuindex++;
              } else {
                $content['menuindex'] = 0;
              }
            }
            break;
        
          case 'OnDocFormSave':
            $query_max = $modx->db->query("SELECT MAX(menuindex) FROM {$modx->db->config['table_prefix']}site_content WHERE parent = {$_POST['parent']}");
            $menuindex = intval($modx->db->getValue($query_max));
            
            if ($_POST['menuindex'] < $menuindex)
              $sortDir = 'DESC';
            else
              $sortDir = 'ASC';
          
            $query = $modx->db->query("SELECT parent, menuindex FROM {$modx->db->config['table_prefix']}site_content WHERE id = {$id}");
            if($current = $modx->db->getRow($query)) {
              $query = $modx->db->query("SELECT id FROM {$modx->db->config['table_prefix']}site_content WHERE parent = {$current['parent']} AND menuindex > 0 ORDER BY menuindex ASC, editedon $sortDir");
              $i = 1;
              while($sister = $modx->db->getRow($query)) {
                $update = $modx->db->query("UPDATE {$modx->db->config['table_prefix']}site_content SET menuindex = {$i} WHERE id = {$sister['id']}");
                $i++;
              }
            }
            break;
        
          default:
            return;
            break;
        
        }