Passed
Push — develop ( d38550...af508d )
by Benjamin
09:14
created

Ecommerce::maxColspan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * @link      https://dukt.net/analytics/
4
 * @copyright Copyright (c) 2020, 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
The expression return Craft::getAlias('...tics/icons/report.svg') also could return the type boolean which is incompatible with the return type mandated by craft\base\WidgetInterface::icon() of null|string.
Loading history...
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
introduced by
$reportingView is of type dukt\analytics\models\View, thus it always evaluated to true.
Loading history...
80
            return $view->renderTemplate('analytics/_special/view-not-configured');
81
        }
82
83
        $widgetId = $this->id;
84
        $widgetSettings = $this->settings;
85
        $localeDefinition = Analytics::$plugin->getAnalytics()->getD3LocaleDefinition(['currency' => $reportingView->gaViewCurrency]);
86
87
        $widgetOptions = [
88
            'viewId' => $widgetSettings['viewId'],
89
            'period' => $widgetSettings['period'] ?? null,
90
            'localeDefinition' => $localeDefinition,
91
            'chartLanguage' => Analytics::$plugin->getAnalytics()->getChartLanguage(),
92
        ];
93
94
        $view->registerJsFile('//www.gstatic.com/charts/loader.js', [
95
            'position' => View::POS_HEAD,
96
        ]);
97
        $view->registerAssetBundle(EcommerceWidgetAsset::class);
98
        $view->registerJs('var AnalyticsChartLanguage = "'.Craft::$app->language.'";', true);
99
        $view->registerJs('new Analytics.EcommerceWidget("widget'.$widgetId.'", '.Json::encode($widgetOptions).');');
100
101
        return $view->renderTemplate('analytics/_components/widgets/Ecommerce/body', [
102
            'widgetSettings' => $widgetSettings
103
        ]);
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109
    public static function maxColspan()
110
    {
111
        return 1;
112
    }
113
114
    /**
115
     * @inheritDoc ISavableComponentType::getSettingsHtml()
116
     *
117
     * @return string
118
     * @throws \Twig_Error_Loader
119
     * @throws \yii\base\Exception
120
     */
121
    public function getSettingsHtml()
122
    {
123
        $settings = $this->getSettings();
124
        $reportingViews = Analytics::$plugin->getViews()->getViews();
125
126
        if (count($reportingViews) > 0) {
127
            return Craft::$app->getView()->renderTemplate('analytics/_components/widgets/Ecommerce/settings', [
128
                'settings' => $settings,
129
                'reportingViews' => $reportingViews,
130
            ]);
131
        }
132
133
        return null;
134
    }
135
}
136