1 | <?php |
||||
2 | /** |
||||
3 | * @link https://dukt.net/analytics/ |
||||
4 | * @copyright Copyright (c) 2022, Dukt |
||||
5 | * @license https://github.com/dukt/analytics/blob/master/LICENSE.md |
||||
6 | */ |
||||
7 | |||||
8 | namespace dukt\analytics\widgets; |
||||
9 | |||||
10 | use Craft; |
||||
11 | use craft\helpers\Json; |
||||
12 | use dukt\analytics\Plugin as Analytics; |
||||
13 | use dukt\analytics\web\assets\ecommercewidget\EcommerceWidgetAsset; |
||||
14 | use craft\web\View; |
||||
15 | |||||
16 | class Ecommerce extends \craft\base\Widget |
||||
17 | { |
||||
18 | // Properties |
||||
19 | // ========================================================================= |
||||
20 | |||||
21 | /** |
||||
22 | * @var string|null |
||||
23 | */ |
||||
24 | public $viewId; |
||||
25 | |||||
26 | /** |
||||
27 | * @var string|null |
||||
28 | */ |
||||
29 | public $period; |
||||
30 | |||||
31 | // Public Methods |
||||
32 | // ========================================================================= |
||||
33 | |||||
34 | /** |
||||
35 | * @inheritdoc |
||||
36 | */ |
||||
37 | public static function displayName(): string |
||||
38 | { |
||||
39 | return Craft::t('analytics', 'E-commerce'); |
||||
40 | } |
||||
41 | |||||
42 | /** |
||||
43 | * @inheritdoc |
||||
44 | */ |
||||
45 | public static function icon() |
||||
46 | { |
||||
47 | return Craft::getAlias('@dukt/analytics/icons/report.svg'); |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
48 | } |
||||
49 | |||||
50 | /** |
||||
51 | * @inheritDoc IWidget::getBodyHtml() |
||||
52 | * |
||||
53 | * @return string|false |
||||
54 | * @throws \Twig_Error_Loader |
||||
55 | * @throws \yii\base\Exception |
||||
56 | * @throws \yii\base\InvalidConfigException |
||||
57 | */ |
||||
58 | public function getBodyHtml() |
||||
59 | { |
||||
60 | $view = Craft::$app->getView(); |
||||
61 | |||||
62 | if (!Analytics::$plugin->getAnalytics()->checkPluginRequirements()) { |
||||
63 | return $view->renderTemplate('analytics/_special/not-connected'); |
||||
64 | } |
||||
65 | |||||
66 | if (!Analytics::$plugin->getSettings()->enableWidgets) { |
||||
67 | return $view->renderTemplate('analytics/_components/widgets/Ecommerce/disabled'); |
||||
68 | } |
||||
69 | |||||
70 | $reportingViews = Analytics::$plugin->getViews()->getViews(); |
||||
71 | |||||
72 | if (count($reportingViews) === 0) { |
||||
73 | return $view->renderTemplate('analytics/_special/no-views'); |
||||
74 | } |
||||
75 | |||||
76 | $widgetSettings = $this->settings; |
||||
77 | $reportingView = Analytics::$plugin->getViews()->getViewById($widgetSettings['viewId']); |
||||
78 | |||||
79 | if (!$reportingView) { |
||||
0 ignored issues
–
show
|
|||||
80 | return $view->renderTemplate('analytics/_special/view-not-configured'); |
||||
81 | } |
||||
82 | |||||
83 | $widgetId = $this->id; |
||||
84 | $widgetSettings = $this->settings; |
||||
85 | |||||
86 | $widgetOptions = [ |
||||
87 | 'viewId' => $widgetSettings['viewId'], |
||||
88 | 'period' => $widgetSettings['period'] ?? null, |
||||
89 | 'currencyDefinition' => Analytics::$plugin->getAnalytics()->getCurrencyDefinition($reportingView->gaViewCurrency), |
||||
90 | 'chartLanguage' => Analytics::$plugin->getAnalytics()->getChartLanguage(), |
||||
91 | ]; |
||||
92 | |||||
93 | $view->registerAssetBundle(EcommerceWidgetAsset::class); |
||||
94 | $view->registerJs('var AnalyticsChartLanguage = "'.Craft::$app->language.'";', true); |
||||
0 ignored issues
–
show
true of type true is incompatible with the type integer expected by parameter $position of yii\web\View::registerJs() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
95 | $view->registerJs('new Analytics.EcommerceWidget("widget'.$widgetId.'", '.Json::encode($widgetOptions).');'); |
||||
96 | |||||
97 | return $view->renderTemplate('analytics/_components/widgets/Ecommerce/body', [ |
||||
98 | 'widgetSettings' => $widgetSettings |
||||
99 | ]); |
||||
100 | } |
||||
101 | |||||
102 | /** |
||||
103 | * @inheritdoc |
||||
104 | */ |
||||
105 | public static function maxColspan() |
||||
106 | { |
||||
107 | return 1; |
||||
108 | } |
||||
109 | |||||
110 | /** |
||||
111 | * @inheritDoc ISavableComponentType::getSettingsHtml() |
||||
112 | * |
||||
113 | * @return string |
||||
114 | * @throws \Twig_Error_Loader |
||||
115 | * @throws \yii\base\Exception |
||||
116 | */ |
||||
117 | public function getSettingsHtml() |
||||
118 | { |
||||
119 | $settings = $this->getSettings(); |
||||
120 | $reportingViews = Analytics::$plugin->getViews()->getViews(); |
||||
121 | |||||
122 | if (count($reportingViews) > 0) { |
||||
123 | return Craft::$app->getView()->renderTemplate('analytics/_components/widgets/Ecommerce/settings', [ |
||||
124 | 'settings' => $settings, |
||||
125 | 'reportingViews' => $reportingViews, |
||||
126 | ]); |
||||
127 | } |
||||
128 | |||||
129 | return null; |
||||
130 | } |
||||
131 | } |
||||
132 |