Archived : Techno Babble

This is the archived version of my b2evolution code blog.
If you require any help regarding b2evolution then
visit it's support forums

You can find my current blogs here
Code : {@link : WafflesOn}
Personal : {@link : InnerVisions}

« End of an era?E-commerce plugin? »

Custom Admin Pages

Posted on 13th Jul 2009 in : Techno Babble

Add your tabs today!

Unlike most of our other plugins this one does bugger all by itself, it's only reason for existence is to be used by other plugins and at the moment the only plugin that uses it is one that we're currently developing. So, what does it do? What this plugin does is allow your own plugins to easily add their own custom tabs to the admin area. Now, before any of yah point out that plugins already have the ability to easily add their own tab to the tools tab, I know, but this plugin allows you to add tabs to any of the evo tab pages ;)

How it works

There's a couple of rules that your plugin has to play by to make all this work. The first and most obvious one is that it needs to declare this plugin as a dependency to ensure that it's always installed when you need it. You tell evo about dependencies by adding the following code snippet to your plugin.

PHP ( _your_plugin.plugin.php ) :

/**
     * This plugin requires the AM Admin Pages plugin
     *
     * @return array dependencies
     */
    function GetDependencies()
    {
        return array'requires' => array(
            'plugins' => arrayarray'am_adminpages_plugin') ), // requires at least version 1 of the plugin
            ));
    }

Next you need to create a shiny new method in your plugin called GetAdminTabs() which should return an array of all tabs that you wish to add your tabs to, and the tabs that you wish to add ... it's probably simpler to just show you some code ;)

PHP ( _your_plugin.plugin.php ) :

/**
     * Add our tabs to the admin tabs
     *
     * @param array $params
     *    string 'main_tab' : This is the main b2evo tab that is currently being displayed
     * @return array : our tabs
     */
    function GetAdminTabs($params)
    {
        global $admin_url$blog$current_User$user_ID;
        global $AdminUI;
 
        $admin_tabs false;
 
        switch( $params['main_tab'] )
        {
            case 'dashboard' :
            case 'blog_settings' :
                global $blog;
                $bCache get_Cache'BlogCache' );
                if( $blog && $sBlog $bCache->get_by_ID($blog ) )
                {
                    $shop_ID $sBlog->owner_user_ID;
                }
                break;
 
            case 'users' :
                $shop_ID param'user_ID''integer' );
                break;
        }
 
        if( empty$shop_ID ) )
        {
            $shop_ID 0;
        }
 
        switch( $params['main_tab'] )
        {
/* start snippet */
            case 'blog_settings' :
                if( $this->ShopSettings->get('enabled'$shop_ID ) || $current_User->check_perm('blog_properties''edit'false$blog ) )
                {
                    $admin_tabs array(
                        'blogs' => array(
                            'amsc_settings' => array(
                                'text' => $this->T_('Shop Settings' ),
                                'href' => url_add_param$admin_url'ctrl=coll_settings&blog='.$blog.'&amat_tab=amsc_settings' ),
                            ),
                        ),
                    );
                }
                break;
/* end snippet */
        }
        return $admin_tabs;
    }

The important thing to note in that code is the bit of the href which reads 'amat_tab=amsc_settings', this is the parameter that's used to tell our plugin which one of your custom pages you want to display, and it must have a corresponding function with the following naming convention function AdminTabPayload_your_unique_tab_value(). In the above example the corresponding function would be called AdminTabPayload_amsc_settings(), the following is our code for that function :

PHP ( Our Plugin ) :

/**
     * Display our settings
     *
     * @param array $params
     *    string 'main_tab' : This is the main b2evo tab that is currently being displayed
     */
    function AdminTabPayload_amsc_settings($params)
    {
        global $AdminUI;
 
        switch( $ctrl param('ctrl''string' ) )
        {
            case 'dashboard' :
            case 'coll_settings' :
                global $blog;
                $bCache get_Cache'BlogCache' );
                if( $blog && $sBlog $bCache->get_by_ID($blog ) )
                {
                    $shop_ID $sBlog->owner_user_ID;
                }
                break;
 
            case 'users' :
                $shop_ID param'user_ID''integer' );
                break;
        }
 
        if( empty$shop_ID ) )
        {
            $shop_ID 0;
        }
 
        $edited_settings $this->ShopSettings;
        $edited_settings->ID $shop_ID;
        global $Blog;
        amsc_load_disp'shopsettings' );
    }

The final thing you need to do is to inform evo of your shiny new abilities :

PHP ( your_plugin.plugin.php ) :

/**
   * List of events that we handle
   *
   * @return array : events
   */
    function GetExtraEvents()
    {
        return array(
            'GetAdminTabs' => 'Adds all our required tabs',
            'AdminTabPayload_amsc_settings' => 'Displays our settings',
            'AdminTabPayload_amsc_info' => 'Displays our info',
            'AdminTabPayload_amsc_customers' => 'Displays our customers',
            'AdminTabPayload_amsc_invoices' => 'Displays our invoices',
        );
    }

To make life a tad easier I've also coded a demo plugin that uses this plugin so you can (hopefully) see how it all works.

Anyway, enough blather from me, you can download the plugin here am_adminpages_plugin.zip and the demo plugin from here am_adminpagesdemo_plugin.zip
¥

8 comments

Comment by John { @link http://wow-factor.com } on 13/07/09
Both of the downloads are am_adminpagesdemo_plugin
Comment by ¥åßßå on 13/07/09
t'was a tad late and I was on a deadline for a family meal .... corrected the links ;)

¥
Comment by John { @link http://wow-factor.com } on 13/07/09
Deadlines and food... a pretty good excuse :)
Comment by Lurker on 13/07/09
My Options Info Tab info
Hello world!

This page is being displayed on the global_settings page from the AM AdminPagesDemo plugin :D

Which must mean it works... :)
Comment by ¥åßßå on 14/07/09
Bugger, how the hell did you end up as a moderated comment? :S

¥
Comment by John { @link http://wow-factor.com } on 14/07/09
Oh, sometimes I enjoy being moderated.
BTW, when I preview a comment I get the following...
"Notice: Trying to get property of non-object in /home/yabba/public_html/skins/tacky3/_myComment.form.php on line 82"
Comment by ¥åßßå on 14/07/09
Hopefully that's now fixed ;)

¥
Comment by tilqicom on 03/08/09
cool ;D
Page archived : 11th Nov 2009
 

X