PHP Classes

YII2 PHP Dashboard Panel: Controller and a widget to display dashboard panel

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-07-28 (-3 hours ago) RSS 2.0 feedNot yet rated by the usersTotal: 137 All time: 9,259 This week: 673Up
Version License PHP version Categories
yii2-panel 1.0.1Freeware7PHP 5, Libraries, Content management
Description 

Author

This package provides a controller and widget to use with the YII2 framework to display a dashboard panel.

It provides a base class that can render panel widgets using a configuration file that defines a class that generates the contents of the widget.

The package also provides a base controller class that can define the dashboard panel behaviors configuration.

Innovation Award
PHP Programming Innovation award winner
January 2019
Winner
Dashboards are useful to present in many sites in order to display many types of useful information to the site users in a single place.

This package simplifies the creation of dashboard by providing base components that can be extended to create dashboard panels with less effort to define the information that they will display and how it will be displayed.

Manuel Lemos
Picture of Uldis Nelsons
Name: Uldis Nelsons <contact>
Classes: 19 packages by
Country: Latvia Latvia
Innovation award
Innovation award
Nominee: 12x

Winner: 2x

 

Documentation

yii2 panel controller

Total Downloads

Yii2Panel was designed to make boards that display different panels from different modules/extensions with access rights control.

Meeting better as expected, as the Yii2Panel can be used for displaying panel from any other module/extension with access control.

Another benefit is that the panels to be displayed are assigned to a module configuration that allows different panels to be used in the module for different projects.

For procesing submited data from PanelWidgwet van use PanleLogic

Realisation

Simply dashboard solution. Each dashboard panel define as panel controller action identically as Yii page:

  • panel controller in behaviors can control access - panel display only for users, who has access;
  • panel controller controller action for creating HTML or response;
  • create view folder in same folder, where all module controller views;
  • for displaying add PanelWidget like anchor in view file;
  • in module config for PanelWidget set one or more panels as Yii routes with parameters to panel controller;

Sequence

  • PanelWidget get panel list from module configuration
  • PanelWidget call panel controller action with parameters
  • Panel controller validate access. If no access, return empty string
  • panel controller action create response HTML
  • PanelWidget output response HTML

Installation by composer

{
    "require": {
       "unyii2/yii2-panel": "dev-master"
    }
}

Or

$ composer require unyii2/yii2-panel "dev-master"

Widget


echo \unyii2\yii2panel\PanelWidget::widget([
    'name' => 'exportSettings',
    'params' => [
        'docId' => 777
    ]
]);

Controller action

For processing submited data form panel widget can use PanelLogic

    public function actionCreate()
    {
    
        $model = new RkInvoiceForm();
        if($model->load($request->post()) && $model->save()) {
            $panelLogic = Yii::createObject([
                'class' => PanelLogic::class,
                'name' => 'LietvedibaRkInvoiceSave',
                'params' => [
                    'modelRecordId' => $model->id
                ]
            ]);
            $panelLogic->run();
            return $this->redirect(['view', 'id' => $model->id]);
        }
        return $this->render('create', [
            'model' => $model,
        ]);
       
    }

Module config

To module add parameter 'panels' and in configuration for module add panels routes

        'invoices' => [
            'class' => 'd3modules\d3invoices\Module',
            'panels' => [
                / for widget */
                'exportSettings' => [
                    [
                        'route' => 'd3accexport/invoice-panel/document-settings',
                        'params' => [
                            'docId' => 13 // action parameter value
                         ]
                        'tag' => 'div', // optinal. Add enclosing tag to panel  
                        'options' => ['class' => 'col-sm-8 col-md-6 col-lg-4'] //enclosing tag options
                     ]
                 ],
                 / for panel logic */
                 'LietvedibaRkInvoiceSave' => [
                    [
                        'route' => 'deal/panel/save'
                    ]
                ],
            ],
        ],

To controller add parameter 'panels' and in configuration for module add panels routes

        'invoices' => [
            'class' => 'd3modules\d3invoices\Module',
            'controllerMap' => [
                'settings' => [
                    'class' => 'yii3\persons\controllers\SettingsController',
                    'panels' => [
                        'UserSettingsProvileLeft' =>
                            [
                                [
                                    'route' => 'myauth/panel/show-qrc'
                                 ]
                            ]
                    ]
                ],
            ],

Optionally, if no possible add to module parameter 'panels', panel routes can define in parameters

'params' => [
    'panelWidget' => [
        'dashboard' => [
            'last' =>  [
                [
                    'route' => 'delivery/panel/transit-declarations',
                    /  
                     * parameters for action method:
                     * public function actionMyTransitDeclarations(array $statusIdList): string 
                     */
                    'params' => [
                        'statusIdList' => [5, 20]
                    ]                    
                ]
            ],
        ]
    ],
]

Panel controller with access control and view rendering

Standard view path: d3modules/d3accexport/views/invoice-panel/setting_grid.php

<?php

namespace d3modules\d3accexport\controllers;

use unyii2\yii2panel\Controller;
use yii\filters\AccessControl;

class InvoicePanelController extends Controller
{

    /
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'rules' => [
                    /
                    * standard definition
                    */
                    [
                        'allow' => true,
                        'actions' => [
                            'document-settings',
                        ],
                        'roles' => [
                            'DocSetting',
                        ],
                    ],
                    
                    /
                    * roles define in panel module config. 
                    *   Example of edi module config:
                    *        'edi' => [
                    *            'class' => 'd3yii2\d3edi\Module',
                    *            'accessRulesMessageRoles' => ['Depo3EdiFull']
                    *        ],
                    *   In Module add property:
                    *        class Module extends D3Module
                    *           public $accessRulesMessageRoles;
                    *           ....
                    */
                    [
                        'allow' => true,
                        'actions' => [
                            'message',
                        ],
                        'roles' => $this->module->rulesMessageRoles??['@'],
                    ],                    
                ],
            ],
        ];
    }

    / for widget */
    public function actionDocumentSettings()
    {
        return $this->render('setting_grid',[]);

    }

    / for widget */
    public function actionMessage()
    {
        return $this->render('message',[]);

    }

    / for controller logic */
    public function actionSave(int $modelRecordId): bool
    {
        if (!$model = DealInvoice::findOne(['invoice_id' => $modelRecordId])) {
            $model = new DealInvoice();
            $model->invoice_id = $modelRecordId;
        }

        $request = Yii::$app->request;
        if ($model->load($request->post())
            && $model->deal_id
            && $model->save()
        ) {
            return true;
        }

        if ($model->hasErrors()) {
            throw new \Exception(json_encode($model->getErrors()));
        }
        return true;
    }

}

panel usage as data transfering between modules

to module panels add panel action

$config = [
    'modules' => [
        'invoices' => [
            'class' => 'd3modules\d3invoices\Module',
            'panels' => [
                'invoice-items-woff' => [
                    [
                        'route' => 'd4storei/data-panel/woff',
                    ]
            ]         
        ]
    ]

executing panel

Can use for getting data or executing something in other module


            $panelLogic = new PanelLogic([
                'name' => 'invoice-items-woff',
                'params' => [
                    'invoiceSysModelId' => $sysModelId,
                    'invoiceId' => $model->id,
                    'items' => $items,
                ],
            ]);
            $panelLogicData = $logic->run();


panel controller

use unyii2\yii2panel\Controller;

/
 * @property Module $module
 */
class DataPanelController extends Controller
{

    public function behaviors(): array
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'rules' => [
                    /
                     * standard definition
                     */
                    [
                        'allow' => true,
                        'actions' => [
                            'woff',
                        ],
                        'roles' => [
                            D3ModulesD4StoreiFullUserRole::NAME,
                        ],
                    ],
                ],
            ],
        ];
    }

            /
             * mas?vs piel?gots InvInvoiceItems::load()
             */
            $list[] = [
                'name' => $row->storeProduct->product->name,
                'count' => $row->productQnt,
        }
        return $list;
    }

    /
     *  write off invoice items
     *  Add refs:
     *  - inv_invoice
     *  - inv_invoice_items
     *  - user
     *
     * @param int $invoiceSysModelId
     * @param int $invoiceId
     * @param array<int, array{
     *     itemSysModelId: int,
     *     itemId: int,
     *     count:float,
     *     unitId: int,
     *     storeProductSysModelId: int,
     *     storeProductId: int
     * }> $items
     * @return bool
     * @throws D3ActiveRecordException
     * @throws Exception
     */
    public function actionWoff(
        int $invoiceSysModelId,
        int $invoiceId,
        array $items
    ): bool
    {
        $storeProductSysModelId = SysModelsDictionary::getIdByClassName(D4StoreStoreProduct::class);
        if (!$transaction = Yii::$app->db->beginTransaction()) {
            throw new Exception('Can not initiate transaction');
        }
        try {
            foreach ($items as $item) {
                if ((int)$item['storeProductSysModelId'] !== $storeProductSysModelId) {
                    throw new Exception('Wrong store product model sys id: ' . $storeProductSysModelId);
                }
                $product = D4StoreStoreProduct::findOne($item['storeProductId']);
                $action = new Action($product);
                $action->outSys($item['count'],  $item['itemSysModelId'], $item['itemId']);
                $action->addRef(Yii::$app->user);
                $action->addRefSys($invoiceSysModelId, $invoiceId);
            }
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollBack();
            FlashHelper::addDanger($e->getMessage());
            Yii::error($e->getMessage() . PHP_EOL . $e->getTraceAsString());
            return false;
        }
        return true;
    }
}



  Files folder image Files (5)  
File Role Description
Files folder imagesrc (3 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:137
This week:0
All time:9,259
This week:673Up