pepebe
4/6/2014 - 2:44 PM

ManagerQuickNav v:0.01i (BETA) ManagerQuickNav adds: - buttons with links to the previous or next resource in the current menuindex - a

ManagerQuickNav v:0.01i (BETA) ManagerQuickNav adds:

  • buttons with links to the previous or next resource in the current menuindex
  • a combobox with links the current parent resource and all siblings.
  • a button to quickly add a new resource with the same parent
<?php
/**
 * ManagerQuickNav v:0.0.1j (BETA)
 * Last edited: 2014-04-09 14:26
 * 
 * AUTHOR: 
 * info@pepebe.de
 *
 * DESCRIPTION:
 * This plugin injects js to add new items to 'modx-action-buttons'
 * (the button group in the top right corner of the update resource window).
 * 
 *   ManagerQuickNav adds:
 *   - buttons with links to the previous or next resource in the current menuindex 
 *   - a combobox with links to the current parent resource and all siblings.
 * 
 * The plugin is especially useful with other plugins that hide child resources like:
 * - articles
 * - collections
 * - grid_class_key
 * 
 * USAGE:
 * 1. Create a new plugin and copy the content of this file into the plugin code textarea.
 * 2. Check the 'OnDocFormRender' system event and make sure the plugin is not disabled.
 * 3. Done.
 * 
 * OPTIONS:.
 * emptyText = (string) Placeholder text for combobox
 * width     = (int) width of comobox
 * newTitle  = (string) Tooltip text for the create new resource button
 * 
 * hide_close = (bolean) Hide obsolete buttons
 *
 * IMPORTANT:
 * This is just a proof of concept. 
 * 
 * 1. It assumes that the menuindex of a group of child resources looks like this: 0,1,2,3,...
 * Im sure this is not always the case. The final version will handle this problem in a better way.
 * If you have a good solution for this problem, send me a mail.
 * 
 * CHANGELOG:
 * v0.0.1a Initial Release
 * v0.0.1b Customizazion options for combobox settings
 * v0.0.1c Trigger MODx.releaseLock(MODx.request.id) before leaving current page.
 * v0.0.1d Added ManagerQuickNav to root Elements
 * v0.0.1e Added option to remove obolsolet buttons and and button to create a new resource with the same parent.
 * v0.0.1f Language settings for en and de
 * v0.0.1g New changelog
 * v0.0.1h Now also (somewhat) working with articles. @treigh: Thanks for your feedback.
 * v0.0.1i Now COMPLETELY working with articles!
 * v0.0.1j Link generations completly rewritten. Note2Self: Action ids are individual for EACH modx installation!
 * 
 */ 
 
    /**/
    
    $showQuickNav = array('22');
 
    /* Optional settings */
    
    $language = 'en';
    
    $lang['de']['emptyText']    = "Weiter mit...";
    $lang['de']['newTitle']     = "Neues Dokument anlegen";
    
    $lang['en']['emptyText']    = "Move to";
    $lang['en']['newTitle']     = "Create new resource";
    
    $emptyText  = $lang[$language]['emptyText'];
    $newTitle   = $lang[$language]['newTitle'];
    
    $width      = 150; /* Might be to small for your resource titles */ 
    $hide_close = 1;
    
    
    /* Don't change anything below this comment if you don't know your code-fu... */
    
    $prevId    = 0;
    $prefTitle = "";
    $nextId    = 0;
    $nextTitle = "";
    
    $parentId           = $resource->get('parent');
    $context_key        = $resource->get('context_key');
    
    $currentId          = $resource->get('id');
    $currentIndex       = $resource->get('menuindex');
    $currentTitle       = $resource->get('pagetitle');
    $currentTemplate    = $resource->get('template');
    
    #if(!in_array($currentTemplate,$showQuickNav)) { return; }
    
    if($currentIndex == 0) { $prev = 0;} 

    if($parentId == 0) {
        /* All root elements... */
        
        $c = $modx->newQuery('modResource');
        $c->sortby('menuindex','ASC');
        $c->where(array(
           'parent' => 0,
           'context_key' => $context_key
        ));
        
        $children = $modx->getCollection('modResource', $c);        
    }
    else {
        $parent             = $resource->getOne('Parent');
        
        $parent_class_key   = $parent->get('class_key');
        if($parent_class_key == 'ArticlesContainer'){
            $settings = $parent->getProperties('articles');
            $articleTemplate = !empty($settings['articleTemplate']) ? $settings['articleTemplate'] : $modx->config[’articles.default_article_template’];
        }
        $parentId           = $parent->get('id');
        $parentTitle        = $parent->get('pagetitle');
        
        $c = $modx->newQuery('modResource');
        $c->sortby('menuindex','ASC');
        
        $children = $parent->getMany('Children',$c);    
    }

    if (!$children ) { return false; }
    
    if($parentId != 0){
        $store[] = "['$parentId','- $parentTitle']";
    }
    
    foreach ($children as $child) {
        $childId    = $child->get('id');
        $childIndex = $child->get('menuindex');
        $childTitle = $child->get('pagetitle');
        
        if(($childIndex +1 ) == $currentIndex){
            $prevId    = $childId; 
            $prevTitle = $childTitle;
        }
        if(($childIndex -1 ) == $currentIndex){
            $nextId    = $childId; 
            $nextTitle = $childTitle;
        }
        if($id != $childId) {
            $childTitle = "-- $childTitle";
        }
        else {
            $childTitle = "-- <b>$childTitle</b>";
        }
        $store[]    = "['$childId','$childTitle']";
    }
    $store = implode(",",$store);

switch($modx->event->name){
case 'OnDocFormRender':
    $html = "
        <script type='text/javascript' src='//code.jquery.com/jquery-1.10.2.min.js'></script>
    
        <script type='text/javascript'>
        Ext.onReady(function () {
            
            console.log('ManagerQuickNav plugin loaded');  
            
            var parent_class_key  = '" . $parent_class_key . "';
            
            if(parent_class_key == 'ArticlesContainer'){
                var articleTemplate = '" . $articleTemplate . "';
            }
            
            var hide_close   = $hide_close;
        
            var context_key  = '$context_key';
            var parentId     = $parentId;
        
            var emptyText    = '$emptyText';
            var width        =  $width;
            
            var newTitle     = '$newTitle'
            
            var prevId       =  $prevId;
            var prevTitle    = '$prevTitle';
            var nextId       =  $nextId;
            var nextTitle    = '$nextTitle';
            
            var currentId    =  $currentId;
            var currentTitle = '$currentTitle';
            var currentTemplate = $currentTemplate;
            
            var store        = [$store];
            
            var modab = Ext.getCmp('modx-action-buttons');

            var modx_abtn_cancel = Ext.getCmp('modx-abtn-cancel');
            var modx_stay_menu   = Ext.getCmp('modx-stay-menu');

            if(hide_close){
                if(modx_abtn_cancel) {
                    //modx_abtn_cancel.hide();
                }
                if(modx_stay_menu){
                    modx_stay_menu.hide();
                }
            }
            else {
                modab.add('-');    
            }
            
            modab.add({
                 xtype: 'button'
                ,disabled: true
                ,tooltip: prevTitle
                ,iconCls: 'x-tbar-page-prev'
                ,id: 'prevResource'
                ,handler: function(){
                    MODx.releaseLock(MODx.request.id);
                    querystring = 'id=' + prevId;
                    MODx.loadPage(MODx.action['resource/update'], querystring);                    
                    
                }
            });            

            if(prevId != 0){
                Ext.getCmp('prevResource').enable();
            }
            else {
                Ext.getCmp('prevResource').disable();
            } 
            
            modab.add({
                 xtype: 'combo'
                ,emptyText: emptyText
                ,width: width
                ,id: 'siblings'
                ,mode: 'local'
                ,displayField: 'pagetitle'
                ,valueField: 'id'
                ,store: new Ext.data.ArrayStore({
                     id: 0
                    ,fields: ['id','pagetitle']
                    ,data: store
                })
                ,listeners:{
                      scope: this
                    ,'select': function(combo,value){
                        resource = combo.getValue();
                        if(resource != currentId){
                            MODx.releaseLock(MODx.request.id);
                            querystring = 'id=' + resource;
                            MODx.loadPage(MODx.action['resource/update'], querystring);                          
                        }
                        else {
                            combo.clearValue();
                            combo.applyEmptyText();
                            combo.getPicker().getSelectionModel().doMultiSelect([], false);
                            //alert(combo.getValue());
                        }
                    }
                }                
                ,forceSelection: true
                ,triggerAction: 'all'
                ,selectOnFocus:true
                ,editable: false
                ,typeAhead: false
            });
            
            modab.add({
                 xtype: 'button'
                ,disabled: true
                ,tooltip: nextTitle                
                ,iconCls: 'x-tbar-page-next'
                ,id: 'nextResource'
                ,handler: function(){
                    MODx.releaseLock(MODx.request.id);
                    querystring = 'id=' + nextId;
                    MODx.loadPage(MODx.action['resource/update'], querystring);                    
                    
                }
            });            
            
            if(nextId != 0){
                Ext.getCmp('nextResource').enable();
            }
            else {
                Ext.getCmp('nextResource').disable();
            }             
            
            modab.add({
                 xtype: 'button'
                ,disabled: false
                ,tooltip: newTitle                
                ,text: '+'
                //,iconCLs: 'x-tool-plus' Can't find a decent icons...
                ,id: 'newResource'
                ,handler: function(){
                    MODx.releaseLock(MODx.request.id);
                    if(parent_class_key == 'ArticlesContainer'){
                        querystring = 'class_key=Article&parent=' + parentId + '&context_key=' + context_key + '&template=' + articleTemplate;
                        MODx.loadPage(MODx.action['resource/create'], querystring);
                    }
                    else {
                        querystring = 'parent=' + parentId + '&context_key=' + context_key;
                        MODx.loadPage(MODx.action['resource/create'], querystring);
                    }
                }
            });            
            
            modab.doLayout();
        });
        </script>
    ";
    
    $modx->regClientStartupHTMLBlock($html);
    break;
default:
    break;
}

/**/