/
TopProductTypes.php
177 lines (154 loc) · 4.16 KB
/
TopProductTypes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\commerce\widgets;
use Craft;
use craft\base\Widget;
use craft\commerce\stats\TopProductTypes as TopProductTypesStat;
use craft\commerce\web\assets\statwidgets\StatWidgetsAsset;
use craft\helpers\DateTimeHelper;
use craft\helpers\StringHelper;
use craft\web\assets\admintable\AdminTableAsset;
/**
* Top Product Types widget
*
* @property string|false $bodyHtml the widget's body HTML
* @property string $settingsHtml the component’s settings HTML
* @property string $title the widget’s title
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0
*/
class TopProductTypes extends Widget
{
/**
* @var int|\DateTime|null
*/
public $startDate;
/**
* @var int|\DateTime|null
*/
public $endDate;
/**
* @var string|null
*/
public $dateRange;
/**
* @var string Options 'revenue', 'qty'.
*/
public $type;
/**
* @var TopProductTypesStat
*/
private $_stat;
/**
* @var string
*/
private $_title;
/**
* @var array
*/
private $_typeOptions;
/**
* @inheritDoc
*/
public function init()
{
$this->_typeOptions = [
'qty' => Craft::t('commerce', 'Qty'),
'revenue' => Craft::t('commerce', 'Revenue'),
];
switch ($this->type) {
case 'revenue':
{
$this->_title = Craft::t('commerce', 'Top Product Types by Revenue');
break;
}
case 'qty':
{
$this->_title = Craft::t('commerce', 'Top Product Types by Qty Sold');
break;
}
default:
{
$this->_title = Craft::t('commerce', 'Top Product Types');
break;
}
}
$this->dateRange = !$this->dateRange ? TopProductTypesStat::DATE_RANGE_TODAY : $this->dateRange;
$this->_stat = new TopProductTypesStat(
$this->dateRange,
$this->type,
DateTimeHelper::toDateTime($this->startDate, true),
DateTimeHelper::toDateTime($this->endDate, true)
);
parent::init();
}
/**
* @inheritdoc
*/
public static function isSelectable(): bool
{
return Craft::$app->getUser()->checkPermission('commerce-manageOrders') && Craft::$app->getUser()->checkPermission('commerce-manageProducts');
}
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('commerce', 'Top Product Types');
}
/**
* @inheritdoc
*/
public static function icon(): string
{
return Craft::getAlias('@craft/commerce/icon-mask.svg');
}
/**
* @inheritdoc
*/
public function getTitle(): string
{
return $this->_title;
}
/**
* @inheritDoc
*/
public function getSubtitle()
{
return $this->_stat->getDateRangeWording();
}
/**
* @inheritdoc
*/
public function getBodyHtml()
{
$stats = $this->_stat->get();
$view = Craft::$app->getView();
$view->registerAssetBundle(StatWidgetsAsset::class);
$view->registerAssetBundle(AdminTableAsset::class);
return $view->renderTemplate('commerce/_components/widgets/producttypes/top/body', [
'stats' => $stats,
'type' => $this->type,
'typeLabel' => $this->_typeOptions[$this->type] ?? '',
'id' => 'top-products' . StringHelper::randomString(),
]);
}
/**
* @inheritdoc
*/
public function getSettingsHtml(): string
{
$id = 'top-products' . StringHelper::randomString();
$namespaceId = Craft::$app->getView()->namespaceInputId($id);
return Craft::$app->getView()->renderTemplate('commerce/_components/widgets/producttypes/top/settings', [
'id' => $id,
'namespaceId' => $namespaceId,
'widget' => $this,
'typeOptions' => $this->_typeOptions,
]);
}
}