2017 © Pedro Peláez
 

library cog-mothership-reports

Mothership cogule for reports

image

mothership-ec/cog-mothership-reports

Mothership cogule for reports

  • Tuesday, November 17, 2015
  • by thomasjthomasj
  • Repository
  • 4 Watchers
  • 1 Stars
  • 2,396 Installations
  • PHP
  • 8 Dependents
  • 1 Suggesters
  • 2 Forks
  • 3 Open issues
  • 12 Versions
  • 0 % Grown

The README.md

Mothership Reports

The Message\Mothership\Report cogule provides a framework for providing reports in a new tab in the control panel., (*1)

Creating a new report

Registering reports

Register your reports in the Service container of the module it's in. This is an example of code in MS-User:, (*2)

public function registerReports($services)
{
    $services['user.user_summary'] = $services->factory(function($c) {
        return new User\Report\UserSummary(
            $c['db.query.builder.factory'],
            $c['routing.generator']
        );
    });

    $services['user.reports'] = function($c) {
        $reports = new ReportCollection;
        $reports
            ->add($c['user.user_summary'])
        ;
            return $reports;
    };
}

Creating reports

Create a folder Reports within Src if there isn't already one. All reports for this module will be saved in here., (*3)

Initial set up

All reports must extend AbstractReport and will use:, (*4)

  • Message\Cog\DB\QueryBuilderInterface;
  • Message\Cog\DB\QueryBuilderFactory;
  • Message\Cog\Routing\UrlGenerator;
Constructor

Set the: - name: Used as an identifier and for naming the download file. - displayName: Used on the front-end. - description: Used on the front-end. - reportGroup: Used to group similar reports on the dashboard., (*5)

Set all the filters and charts you want to use in the report., (*6)

Set any default filters you want for the report when it's first viewed., (*7)

This report displays TableChart, and uses filters DateRange and Choices. It sets the form default StartDate for 1 month ago., (*8)

        parent::__construct($builderFactory, $routingGenerator);
        $this->_setName('payments_refunds');
        $this->_setDisplayName('Payments & Refunds');
        $this->_setReportGroup('Transactions');
        $this->_setDescription('
            This report displays all payments & refunds.
            By default it includes all data from the last month (by completed date).
        ');
        $this->_charts[]   = new TableChart;
        $this->_filters->add(new DateRange);
        $startDate = new \DateTime;
        $this->getFilters()
            ->get('date_range')
            ->setStartDate($startDate->setTimestamp(strtotime(date('Y-m-d H:i')." -1 month")));
        // Params for Choices filter: unique filter name, label, choices, multi-choice
        $this->_filters->add(new Choices(
            "type",
            "Type",
            [
                'payment' => 'Payment',
                'refund' => 'Refund',
            ],
            false
        ));
    }
getCharts()

All reports need this function:, (*9)

public function getCharts()
{
    $data = $this->_dataTransform($this->_getQuery()->run(), "json");
    $columns = $this->_parseColumns($this->getColumns());
        foreach ($this->_charts as $chart) {
        $chart->setColumns($columns);
        $chart->setData($data);
    }
    return $this->_charts;
}
getColumns()

Set all the columns needed into an array with the key as the name and value as type of data that's expected. This type is used for Google Charts., (*10)

public function getColumns()
{
    return [
        'Date'         => 'string',
        'Created by'   => 'string',
        'Currency'     => 'string',
        'Method'       => 'string',
        'Amount'       => 'number',
        'Type'         => 'string',
        'Order/Return' => 'string',
    ];
}
_getQuery()

Using QueryBuilder create your report query. A simple example is the User Summary report:, (*11)

protected function _getQuery()
{
    $queryBuilder = $this->_builderFactory->getQueryBuilder();
        $queryBuilder
        ->select('user.user_id AS "ID"')
        ->select('created_at AS "Created"')
        ->select('CONCAT(surname,", ",forename) AS "User"')
        ->select('email AS "Email"')
        ->from('user')
        ->orderBy('surname')
    ;

    return $queryBuilder->getQuery();
}

_dataTransform()

This takes the data from the query and converts it into either a string in JSON format for use in Google Charts, or a simple array for the CSV download., (*12)

For more complicated data you might need to pass in some optional properties. See: https://developers.google.com/chart/interactive/docs/reference#cell_object, (*13)

For example, to order dates in their numerical representation rather than alphabetically you will need to send the timestamp as a value but the string as formatted value., (*14)

[
    'v' => $row->Created,
    'f' => date('Y-m-d H:i', $row->Created)
],

Another example is for currency values:, (*15)

[
    'v' => (float) $row->Gross,
    'f' => (string) number_format($row->Gross,2,'.',',')
],

For links, if you send just the html link as the value it will order the values using the full html rather than the display text. This is fine if the text is the same values used to create the URL, but in most cases the ID isn't what will be displayed. In this case, the user's name is sent as the value and the html (using the user-id) is sent as the formatted value., (*16)

[
    'v' => utf8_encode($row->User),
    'f' => (string) '
        <a href ="'.$this->generateUrl('ms.cp.user.admin.detail.edit',
        ['userID' => $row->ID]).'">'
        .ucwords(utf8_encode($row->User)).'</a>'
]

Any strings which may contain special characters need encoded to UTF-8, as User does in the above example., (*17)

The full code used in the User Summary report:, (*18)

    protected function _dataTransform($data, $output = null)
    {
        $result = [];

        if ($output === "json") {

            foreach ($data as $row) {

                $result[] = [
                    $row->User ? [
                        'v' => utf8_encode($row->User),
                        'f' => (string) '<a href ="'.$this->generateUrl('ms.cp.user.admin.detail.edit', ['userID' => $row->ID]).'">'.ucwords(utf8_encode($row->User)).'</a>'
                    ] : $row->User,
                    $row->Email,
                    [
                        'v' => $row->Created,
                        'f' => date('Y-m-d H:i', $row->Created)
                    ],
                ];

            }
            return json_encode($result);

        } else {

            foreach ($data as $row) {
                $result[] = [
                    utf8_encode($row->User),
                    $row->Email,
                    date('Y-m-d H:i', $row->Created),
                ];
            }
            return $result;

        }
    }

Filters

DateRange

This has two datetime form fields to select a range of data between the two., (*19)

Date

This is a singular date form field to select data from one specific date., (*20)

Choices

The Choices form field can be customised depending on what data you are getting in the report., (*21)

When adding a choice form you must add the following parameters:, (*22)

  • A unique filter name, it cannot be the same as any other report
  • A label for displaying on the form
  • The choices
  • Whether the field is multiple-choice
$this->_filters->add(new Choices(
    "type",
    "Sale Type",
        [
            'Order' => 'Order',
            'Return' => 'Return',
            'Exchange' => 'Exchange',
            'shipping' => 'Shipping',
        ],
    true
));

Charts

Table

TableChart is currently the only chart available. https://developers.google.com/chart/interactive/docs/gallery/table, (*23)

The Versions

17/11 2015

dev-feature/ux/admin-rebuild

dev-feature/ux/admin-rebuild http://mothership.ec

Mothership cogule for reports

  Sources   Download

proprietary

The Requires

 

by The Mothership Development Team

data information stats reports reporting mothership

07/10 2015

dev-brand-reports

dev-brand-reports http://mothership.ec

Mothership cogule for reports

  Sources   Download

proprietary

The Requires

 

by The Mothership Development Team

data information stats reports reporting mothership

07/10 2015

dev-develop

dev-develop http://mothership.ec

Mothership cogule for reports

  Sources   Download

proprietary

The Requires

 

by The Mothership Development Team

data information stats reports reporting mothership

07/10 2015

dev-master

9999999-dev http://mothership.ec

Mothership cogule for reports

  Sources   Download

proprietary

The Requires

 

by The Mothership Development Team

data information stats reports reporting mothership

07/10 2015

2.2.0

2.2.0.0 http://mothership.ec

Mothership cogule for reports

  Sources   Download

proprietary

The Requires

 

by The Mothership Development Team

data information stats reports reporting mothership

28/09 2015

2.1.0

2.1.0.0 http://mothership.ec

Mothership cogule for reports

  Sources   Download

proprietary

The Requires

 

by The Mothership Development Team

data information stats reports reporting mothership

24/09 2015

dev-feature/choich-filter-updates

dev-feature/choich-filter-updates http://mothership.ec

Mothership cogule for reports

  Sources   Download

proprietary

The Requires

 

by The Mothership Development Team

data information stats reports reporting mothership

24/02 2015

dev-feature/choice-choices

dev-feature/choice-choices http://mothership.ec

Mothership cogule for reports

  Sources   Download

proprietary

The Requires

 

by The Mothership Development Team

data information stats reports reporting mothership

24/02 2015

2.0.0

2.0.0.0 http://mothership.ec

Mothership cogule for reports

  Sources   Download

proprietary

The Requires

 

by The Mothership Development Team

data information stats reports reporting mothership

08/01 2015

1.0.2

1.0.2.0 http://message.co.uk

Mothership cogule for reports

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.8
  • message/cog-mothership-cp ~1.8 | ~2.0

 

by The Message Development Team

data information stats reports reporting mothership

23/12 2014

1.0.1

1.0.1.0 http://message.co.uk

Mothership cogule for reports

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.8
  • message/cog-mothership-cp ~1.8

 

by The Message Development Team

data information stats reports reporting mothership

23/12 2014

1.0.0

1.0.0.0 http://message.co.uk

Mothership cogule for reports

  Sources   Download

proprietary

The Requires

  • php >=5.4.0
  • message/cog ~3.8
  • message/cog-mothership-cp ~1.8

 

by The Message Development Team

data information stats reports reporting mothership