• [quickCMP] CMP for creating simple backend-modules per snippets#

  • Bruno17 Reply #1, 1 year, 7 months ago

    Reply
    In Evo it was very easy to create simple modules for backend-stuff.
    Made a CMP for making it easy to create simple CMPs per snippet.
    Perhaps someone finds it usefull.

    1. Create a folder /core/components/quickcmp/
    2. Create a file in this folder 'index.php' with content:

    <?php
    
    /* setup default properties */
    $isLimit = !empty($scriptProperties['limit']);
    $start = $modx->getOption('start',$scriptProperties,0);
    $limit = $modx->getOption('limit',$scriptProperties,20);
    $sort = $modx->getOption('sort',$scriptProperties,'name');
    $dir = $modx->getOption('dir',$scriptProperties,'ASC');
    $assetsUrl = $this->modx->getOption('quickcmp.assets_url',null,$modx->getOption('assets_url').'components/quickcmp/');
    $jsUrl = $assetsUrl.'js/';
    
    $modulcategory=isset($_REQUEST['qcmpcategory'])?$_REQUEST['qcmpcategory']:'quickcmp';
    $qcmpcat = isset($_REQUEST['qcmpcategory'])?'&qcmpcategory='.$_REQUEST['qcmpcategory']:'';
    $action=$_REQUEST['a'];
    
    /* query for snippets in modulcategory */
    $c = $modx->newQuery('modSnippet');
    $c->select('modSnippet.id,modSnippet.name');
    $c->sortby($sort,$dir);
    $c->leftJoin('modCategory','modCategory','`modCategory`.`id` = `modSnippet`.`category`');
    $c->where('`modCategory`.`category`="'.$modulcategory.'"');
    if ($isLimit) $c->limit($limit,$start);
    //$c->prepare();
    //echo $c->toSql();
    $snippets = $modx->getCollection('modSnippet',$c);
    $count = $modx->getCount('modSnippet');
    
    /* iterate through snippets */
    
    $snippetnames = array();
    foreach ($snippets as $snippet) {
        $snippet=$snippet->toArray();
    	$snippetnames[]=$snippet['name'];
    
    }
    if (isset($_REQUEST['runmycmp'])&& in_array($_REQUEST['runmycmp'],$snippetnames)){
    $modulname=$_REQUEST['runmycmp'];
    $moduloutput=$modx->runsnippet($modulname);	
    }
    else{
    $moduloutput='click any of the tabs to run a quickCMP';    
    }
    
    if (count ($snippetnames)>0){
    foreach ($snippetnames as $name) {
    	$class= $name==$modulname ?'x-tab-strip-active':'';
    	$tabs.='
                    <li class="'.$class.'">
                        <a href="?a='.$action.'&runmycmp='.$name.$qcmpcat.'" class="x-tab-right"><em class="x-tab-left"><span class="x-tab-strip-inner"><span class="x-tab-strip-text ">'.$name.'</span></span></em></a>
                    </li>
    	';		
    }	
    }
    
    
    
    
    $output = '
    <div id="quickcmp-panel" class=" x-tab-panel modx-tabs">
        <div class="x-tab-panel-header x-unselectable x-tab-panel-header-plain" id="quickcmp-panel-header" style="-moz-user-select: none;">
            <div class="x-tab-strip-wrap" id="quickcmp-tab-container" style="width: 931px;">
                <ul class="x-tab-strip x-tab-strip-top" id="ext-gen66">
                    '.$tabs.'
                    <li class="x-tab-edge" >
                    </li>
                    <div class="x-clear" >
                    </div>
                </ul>
            </div>
            <div class="x-tab-strip-spacer" >
            </div>
        </div>
        <div class="x-tab-panel-bwrap" >
            <div class="x-tab-panel-body x-tab-panel-body-top"  style="padding: 1em; height: auto;">
                <div  class=" x-panel x-panel-noborder" style="width: 929px;">
                    <div class="x-panel-bwrap" >
                        <div class="x-panel-body x-panel-body-noheader x-panel-body-noborder"  style="height: auto; width: 929px;">
                            <div  class=" x-panel x-panel-noborder">
                                <div class="x-panel-bwrap" >
                                    <div class="x-panel-body x-panel-body-noheader x-panel-body-noborder" >
    ';
    $output.= $moduloutput;
    $output.='
                                        <br/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    ';
    
    
    $modx->regClientStartupScript($jsUrl.'quickcmp.js');
    
    
    
    return $output;
    
    ?>
    
    


    3. Create a folder /assets/components/quickcmp/
    4. Create a file 'quickcmp.js' in this folder with:

    var QuickCMP = function(config) {
        config = config || {};
        QuickCMP.superclass.constructor.call(this,config);
    };
    Ext.extend(QuickCMP,Ext.Component,{
        page:{},window:{},grid:{},tree:{},panel:{},combo:{},config: {}
    });
    Ext.reg('quickcmp',QuickCMP);
    
    var QuickCMP = new QuickCMP();
    
    QuickCMP.page.Object = function(config) {
        config = config || {};
        Ext.applyIf(config,{
    		components: [{
                xtype: 'box'
                ,id: 'quickcmp-box'
            }]
    
        }); 
        QuickCMP.page.Object.superclass.constructor.call(this,config);
    };
    Ext.extend(QuickCMP.page.Object,MODx.Component);
    Ext.reg('quickcmp-page-home',QuickCMP.page.Object);
    
    Ext.onReady(function() {
    
        var qcmp = MODx.load({ 
    		xtype: "quickcmp-page-home"
        });    
    	
        var panel =Ext.getCmp("quickcmp-box");
        var el = Ext.get("quickcmp-panel");
        el.appendTo(panel.el);
        
    });
    


    5. Create a namespace 'quickcmp' which points to the folder /core/components/quickcmp/
    6. Create a action with controller: 'index' and this namespace.
    7. Create a menue under components lexicon-key: quickcmp and this action
    9. create a new Category 'quickcmp'
    3. create snippets under that Category, which you want to have as CMP-moduls

    If you want to run one of your module-snippets per default add in the action mymoduls:
    parameters: &runmycmp=YourSnippetName

    If you want to use more categories and have different cmp-groups create more actions and add under parameters: &qcmpcategory=myowncategory


  • cyclissmo Reply #2, 1 year, 7 months ago

    Reply
    That's a fantastic script. This will certainly save a LOT of development time for deploying simple manager pages. Looking forward to trying it out.

    Thanks for the contribution!

    Mike


  • landonpoburan Reply #3, 1 year, 6 months ago

    Reply
    This doesn't seem to display the data from my snippet in the CMP. It displays the snippet name in the tab, then in my snippet I have a simple:

    <?php
    echo 'Hello world';

    and it does not display. Did I possibly do something wrong? I copied your code exactly and just changed each instance of 'mymodulus' to the name of my package.


  • landonpoburan Reply #4, 1 year, 6 months ago

    Reply
    This is because the $moduloutput is not populated unless $_REQUEST['runmymodul'] has a value. So when you click on the tab, it works, but on default load of the page it doesn't have that value.

    We can do this by adding the Parameter in modMenu when we create the Action. Add
    &runmymodul=YourSnippetNameHere

    By adding this it will load your snippet on initial load. I think you should add this to your instructions.



  • Bruno17 Reply #5, 1 year, 6 months ago

    Reply
    Quote from: landonpoburan at Nov 04, 2010, 12:13 PM
    This is because the $moduloutput is not populated unless $_REQUEST['runmymodul'] has a value. So when you click on the tab, it works, but on default load of the page it doesn't have that value.

    We can do this by adding the Parameter in modMenu when we create the Action. Add
    &runmymodul=YourSnippetNameHere

    By adding this it will load your snippet on initial load. I think you should add this to your instructions.

    done.


  • dragona Reply #6, 1 year, 6 months ago

    Reply
    I just began to use it. Great idea. Thanks for the script.


  • Bruno17 Reply #7, 11 months ago

    Reply
    just updated the code at the first post a bit and added a bit extJs. Now if you have long content in your quickCMP, scrolling does hopefully work correctly.