Passed
Push — master ( 758032...380927 )
by Benjamin
28:34 queued 21:24
created

Ecommerce::getBodyHtml()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 26
nc 5
nop 0
dl 0
loc 43
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link      https://dukt.net/craft/analytics/
4
 * @copyright Copyright (c) 2018, Dukt
5
 * @license   https://dukt.net/craft/analytics/docs/license
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
15
class Ecommerce extends \craft\base\Widget
16
{
17
    // Properties
18
    // =========================================================================
19
20
    /**
21
     * @var string|null
22
     */
23
    public $viewId;
24
25
    /**
26
     * @var string|null
27
     */
28
    public $period;
29
30
    // Public Methods
31
    // =========================================================================
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public static function displayName(): string
37
    {
38
        return Craft::t('analytics', 'E-commerce');
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public static function iconPath()
45
    {
46
        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::iconPath() of null|string.
Loading history...
47
    }
48
49
    /**
50
     * @inheritDoc IWidget::getBodyHtml()
51
     *
52
     * @return string|false
53
     * @throws \Twig_Error_Loader
54
     * @throws \yii\base\Exception
55
     * @throws \yii\base\InvalidConfigException
56
     */
57
    public function getBodyHtml()
58
    {
59
        $view = Craft::$app->getView();
60
61
        if (!Analytics::$plugin->getAnalytics()->checkPluginRequirements()) {
62
            return $view->renderTemplate('analytics/_special/not-connected');
63
        }
64
65
        if (!Analytics::$plugin->getSettings()->enableWidgets) {
66
            return $view->renderTemplate('analytics/_components/widgets/Ecommerce/disabled');
67
        }
68
69
        $reportingViews = Analytics::$plugin->getViews()->getViews();
70
71
        if (count($reportingViews) === 0) {
72
            return $view->renderTemplate('analytics/_special/no-views');
73
        }
74
75
        $widgetSettings = $this->settings;
76
        $reportingView = Analytics::$plugin->getViews()->getViewById($widgetSettings['viewId']);
77
78
        if (!$reportingView) {
79
            return $view->renderTemplate('analytics/_special/view-not-configured');
80
        }
81
82
        $widgetId = $this->id;
83
        $widgetSettings = $this->settings;
84
        $localeDefinition = Analytics::$plugin->getAnalytics()->getD3LocaleDefinition(['currency' => $reportingView->gaViewCurrency]);
85
86
        $widgetOptions = [
87
            'viewId' => $widgetSettings['viewId'],
88
            'period' => $widgetSettings['period'] ?? null,
89
            'localeDefinition' => $localeDefinition,
90
            'chartLanguage' => Analytics::$plugin->getAnalytics()->getChartLanguage(),
91
        ];
92
93
        $view->registerJsFile('//www.gstatic.com/charts/loader.js');
94
        $view->registerAssetBundle(EcommerceWidgetAsset::class);
95
        $view->registerJs('var AnalyticsChartLanguage = "'.Craft::$app->language.'";', true);
96
        $view->registerJs('new Analytics.EcommerceWidget("widget'.$widgetId.'", '.Json::encode($widgetOptions).');');
97
98
        return $view->renderTemplate('analytics/_components/widgets/Ecommerce/body', [
99
            'widgetSettings' => $widgetSettings
100
        ]);
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106
    public static function maxColspan()
107
    {
108
        return 1;
109
    }
110
111
    /**
112
     * @inheritDoc ISavableComponentType::getSettingsHtml()
113
     *
114
     * @return string
115
     * @throws \Twig_Error_Loader
116
     * @throws \yii\base\Exception
117
     */
118
    public function getSettingsHtml()
119
    {
120
        $settings = $this->getSettings();
121
        $reportingViews = Analytics::$plugin->getViews()->getViews();
122
123
        if (count($reportingViews) > 0) {
124
            return Craft::$app->getView()->renderTemplate('analytics/_components/widgets/Ecommerce/settings', [
125
                'settings' => $settings,
126
                'reportingViews' => $reportingViews,
127
            ]);
128
        }
129
130
        return null;
131
    }
132
}
133