定制 mothership-ec/cog-mothership-reports 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mothership-ec/cog-mothership-reports

最新稳定版本:2.2.0

Composer 安装命令:

composer require mothership-ec/cog-mothership-reports

包简介

Mothership cogule for reports

README 文档

README

The Message\Mothership\Report cogule provides a framework for providing reports in a new tab in the control panel.

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:

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.

Initial set up

All reports must extend AbstractReport and will use:

  • 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.

Set all the filters and charts you want to use in the report.

Set any default filters you want for the report when it's first viewed.

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

		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:

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.

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:

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.

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

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.

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

Another example is for currency values:

[
	'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.

[
	'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.

The full code used in the User Summary report:

	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.

Date

This is a singular date form field to select data from one specific date.

Choices

The Choices form field can be customised depending on what data you are getting in the report.

When adding a choice form you must add the following parameters:

  • 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

mothership-ec/cog-mothership-reports 适用场景与选型建议

mothership-ec/cog-mothership-reports 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.51k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「data」 「stats」 「reporting」 「reports」 「information」 「mothership」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mothership-ec/cog-mothership-reports 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mothership-ec/cog-mothership-reports 我们能提供哪些服务?
定制开发 / 二次开发

基于 mothership-ec/cog-mothership-reports 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 18
  • 依赖项目数: 8
  • 推荐数: 1

GitHub 信息

  • Stars: 1
  • Watchers: 4
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2015-02-24