/
RepeatCustomers.php
131 lines (113 loc) · 2.92 KB
/
RepeatCustomers.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
<?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\RepeatCustomers as RepeatingCustomersStat;
use craft\commerce\web\assets\statwidgets\StatWidgetsAsset;
use craft\helpers\DateTimeHelper;
use craft\helpers\StringHelper;
/**
* Repeat Customers 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 RepeatCustomers extends Widget
{
/**
* @var int|\DateTime|null
*/
public $startDate;
/**
* @var int|\DateTime|null
*/
public $endDate;
/**
* @var string|null
*/
public $dateRange;
/**
* @var null|RepeatingCustomersStat
*/
private $_stat;
/**
* @inheritDoc
*/
public function init()
{
parent::init();
$this->dateRange = !$this->dateRange ? RepeatingCustomersStat::DATE_RANGE_TODAY : $this->dateRange;
$this->_stat = new RepeatingCustomersStat(
$this->dateRange,
DateTimeHelper::toDateTime($this->startDate, true),
DateTimeHelper::toDateTime($this->endDate, true)
);
}
/**
* @inheritdoc
*/
public static function isSelectable(): bool
{
return Craft::$app->getUser()->checkPermission('commerce-manageCustomers');
}
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('commerce', 'Repeat Customers');
}
/**
* @inheritdoc
*/
public static function icon(): string
{
return Craft::getAlias('@craft/commerce/icon-mask.svg');
}
/**
* @inheritdoc
*/
public function getTitle(): string
{
return '';
}
/**
* @inheritdoc
*/
public function getBodyHtml()
{
$numbers = $this->_stat->get();
$timeFrame = $this->_stat->getDateRangeWording();
$view = Craft::$app->getView();
$view->registerAssetBundle(StatWidgetsAsset::class);
return $view->renderTemplate('commerce/_components/widgets/customers/repeat/body', compact('numbers', 'timeFrame'));
}
/**
* @inheritDoc
*/
public static function maxColspan()
{
return 1;
}
/**
* @inheritdoc
*/
public function getSettingsHtml(): string
{
$id = 'repeat' . StringHelper::randomString();
$namespaceId = Craft::$app->getView()->namespaceInputId($id);
return Craft::$app->getView()->renderTemplate('commerce/_components/widgets/customers/repeat/settings', [
'id' => $id,
'namespaceId' => $namespaceId,
'widget' => $this,
]);
}
}