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

Realtime::icon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\realtimereportwidget\RealtimeReportWidgetAsset;
14
use craft\web\View;
15
16
class Realtime extends \craft\base\Widget
17
{
18
    // Properties
19
    // =========================================================================
20
21
    public $viewId;
22
23
    // Public Methods
24
    // =========================================================================
25
26
    /**
27
     * @inheritdoc
28
     */
29
    public static function isSelectable(): bool
30
    {
31
        $plugin = Craft::$app->getPlugins()->getPlugin('analytics');
32
33
        if (!$plugin) {
34
            return false;
35
        }
36
37
        $settings = $plugin->getSettings();
38
39
        if (empty($settings['enableRealtime'])) {
40
            return false;
41
        }
42
43
        return parent::isSelectable();
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public static function displayName(): string
50
    {
51
        return Craft::t('analytics', 'Active users');
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public static function icon()
58
    {
59
        return Craft::getAlias('@dukt/analytics/icons/realtime-report.svg');
0 ignored issues
show
Bug Best Practice introduced by
The expression return Craft::getAlias('...s/realtime-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...
60
    }
61
62
    /**
63
     * @inheritDoc IWidget::getBodyHtml()
64
     *
65
     * @return string|false
66
     * @throws \Twig_Error_Loader
67
     * @throws \yii\base\Exception
68
     * @throws \yii\base\InvalidConfigException
69
     */
70
    public function getBodyHtml()
71
    {
72
        $view = Craft::$app->getView();
73
74
        if (Analytics::$plugin->getAnalytics()->checkPluginRequirements()) {
75
            if (Analytics::$plugin->getSettings()->enableWidgets) {
76
                $reportingViews = Analytics::$plugin->getViews()->getViews();
77
78
                if (count($reportingViews) > 0) {
79
                    $widgetSettings = $this->settings;
80
81
                    $reportingView = Analytics::$plugin->getViews()->getViewById($widgetSettings['viewId']);
82
83
                    if ($reportingView) {
0 ignored issues
show
introduced by
$reportingView is of type dukt\analytics\models\View, thus it always evaluated to true.
Loading history...
84
                        $plugin = Craft::$app->getPlugins()->getPlugin('analytics');
85
                        $pluginSettings = $plugin->getSettings();
86
87
                        if (!empty($pluginSettings['enableRealtime'])) {
88
                            $realtimeRefreshInterval = Analytics::$plugin->getAnalytics()->getRealtimeRefreshInterval();
89
90
                            $widgetId = $this->id;
91
                            $widgetOptions = [
92
                                'viewId' => $widgetSettings['viewId'],
93
                                'refreshInterval' => $realtimeRefreshInterval,
94
                            ];
95
96
                            $view->registerTranslations('analytics', [
97
                                'Minutes ago',
98
                                'Pageviews',
99
                                '{count} minute ago',
100
                                '{count} minutes ago',
101
                            ]);
102
103
                            $view->registerJsFile('//www.gstatic.com/charts/loader.js', [
104
                                'position' => View::POS_HEAD,
105
                            ]);
106
                            $view->registerAssetBundle(RealtimeReportWidgetAsset::class);
107
                            $view->registerJs('var AnalyticsChartLanguage = "'.Craft::$app->language.'";', true);
108
                            $view->registerJs('new Analytics.Realtime("widget'.$widgetId.'", '.Json::encode($widgetOptions).');');
109
110
                            return $view->renderTemplate('analytics/_components/widgets/Realtime/body', [
111
                                'reportingView' => $reportingView
112
                            ]);
113
                        }
114
115
                        return $view->renderTemplate('analytics/_components/widgets/Realtime/disabled');
116
                    }
117
118
                    return $view->renderTemplate('analytics/_special/view-not-configured');
119
                }
120
121
                return $view->renderTemplate('analytics/_special/no-views');
122
            }
123
124
            return $view->renderTemplate('analytics/_components/widgets/Realtime/disabled');
125
        }
126
127
        return $view->renderTemplate('analytics/_special/not-connected');
128
    }
129
130
    /**
131
     * @inheritdoc
132
     */
133
    public static function maxColspan()
134
    {
135
        return 1;
136
    }
137
138
    /**
139
     * @inheritDoc ISavableComponentType::getSettingsHtml()
140
     *
141
     * @return string
142
     * @throws \Twig_Error_Loader
143
     * @throws \yii\base\Exception
144
     */
145
    public function getSettingsHtml()
146
    {
147
        $settings = $this->getSettings();
148
        $reportingViews = Analytics::$plugin->getViews()->getViews();
149
150
        if (count($reportingViews) > 0) {
151
            return Craft::$app->getView()->renderTemplate('analytics/_components/widgets/Realtime/settings', [
152
                'settings' => $settings,
153
                'reportingViews' => $reportingViews,
154
            ]);
155
        }
156
157
        return null;
158
    }
159
}
160