Mikeylicious

Occasional ramblings of a web developer dude.

Quickie: Creating an AdminCP Dashboard Widget in IPS4

To create an AdminCP Dashboard Widget in IPS4 proved exceedingly simple. It takes two files, the php file for the widget and a template file to control what's being displayed.

The php file is contained in your applications /extensions/ directory. More specifically, /applications/YOURAPP/extensions/core/Dashboard/Overview.php

The code for that file should look something like this:

<?php
/**
 * @brief        Dashboard extension: Overview
 * @author        <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
 * @copyright    (c) 2001 - SVN_YYYY Invision Power Services, Inc.
 * @license        http://www.invisionpower.com/legal/standards/
 * @package        IPS Social Suite
 * @subpackage    
 * @since        20 Mar 2014
 * @version        SVN_VERSION_NUMBER
 */

namespace IPS\yourapp\extensions\core\Dashboard;

/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
    header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
    exit;
}

/**
 * @brief    Dashboard extension: Overview
 */
class _Overview
{
    /** 
     * Return the block HTML show on the dashboard
     *
     * @return    string
     */
    public function getBlock()
    {
        /* Basic stats */
        $data = array(
            'total_teams'        => (int) \IPS\Db::i()->select( 'COUNT(*)', 'YOURAPP_YOURAPP' )->first(),
        );
        
        /* Display */
        return \IPS\Theme::i()->getTemplate( 'dashboard', 'YOURAPP' )->overview( $data );
    }

    /** 
     * Return the block information
     *
     * @return    array    array( 'name' => 'Block title', 'key' => 'unique_key', 'size' => [1,2,3], 'by' => 'Author name' )
     */
    public function getInfo()
    {
        return array();
    }
}

The template file is contained within your /applications/YOURAPP/dev/html/admin/dashboard/overview.phtml - of course this naming correlates with the template call in the above code as well as the naming of the files as well. The code for that looks similar for most "Overview" blocks in the AdminCP:

<ips:template parameters="$data" />
<ul class="ipsDataList">
    {{foreach $data as $k => $v}}
        <li class="ipsDataItem">
            <span class="ipsDataItem_generic ipsDataItem_size6">
                <strong>{lang="$k"}</strong>
            </span>
            <span class="ipsDataItem_generic">
                {$v}
            </span>
        </li>
    {{endforeach}}
</ul>

And the finished product:

Add a Comment →