Widget Development

File/Folder Structure

Location: [backend]/extension/widget/[widget_name_here]

+ controller
+ view
        + theme
                + default
                        + template
                                - [statistics.php]
- init.php

## Suppose we are going to develop a widget name ‘statistics’

+ controller
        - statistics.php

                <?php
                class ControllerWidgetStatisticsStatistics extends BaseWidgetController
                {
                        public function index()
                        {
                                $this->data['total_sale'] = 0;

                                return $this->load->view('index', $this->data);
                        }
                }



+ view
        + theme
                + default
                        + template
                                - index.php

                                        <h2>Total Sale: <?php echo $total_sale;?></h2>

+ init.php

                <?php
                class ControllerWidgetStatisticsInit extends Controller {

                        public function index() {}

                        public function install() {}

                        public function uninstall() {}
                }

Step by Step Installation

Step-01: Initiate widget installation from CLI:

Syntax: php cli app:extension-add backend_widget_[code] WidgetName installFlag requiredFlag

php cli app:extension-add backend_widget_statistics 'Statistics Widget' 0 0
Step-02: Install & Enable
Backend > Extensions > Search Widget > Statistics Widget > Manage > Install > Install Now
Backend > Extensions > Search Widget > Statistics Widget > Manage > Manage > Status = Enabled > Update

Step-03: Add Widget Instance

Backend > Extensions > Widget > Add New Instance or Edit existing one

Usages

<?php
$this->load->model('widget');
$widgets = array(1); // 1 is widget ID
$this->data['widgets'] = array();

foreach ($widgets as $widget_id) {

        if ($widget = $this->model_widget->joinGet($widget_id)) {

                if (has_permission('widget.' . $widget['code'])) {

                        $this->load->exLoader($widget['code'], 'widget');
                        $widget_data = $this->load->controller($widget['code']);
                        $this->load->exUnloader();

                        if ($widget_data) {
                                $this->data['widgets'][] = $widget_data;
                        }
                }
        }
}