| 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\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
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
|
|||||
| 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->registerAssetBundle(RealtimeReportWidgetAsset::class); |
||||
| 104 | $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
Loading history...
|
|||||
| 105 | $view->registerJs('new Analytics.Realtime("widget'.$widgetId.'", '.Json::encode($widgetOptions).');'); |
||||
| 106 | |||||
| 107 | return $view->renderTemplate('analytics/_components/widgets/Realtime/body', [ |
||||
| 108 | 'reportingView' => $reportingView |
||||
| 109 | ]); |
||||
| 110 | } |
||||
| 111 | |||||
| 112 | return $view->renderTemplate('analytics/_components/widgets/Realtime/disabled'); |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | return $view->renderTemplate('analytics/_special/view-not-configured'); |
||||
| 116 | } |
||||
| 117 | |||||
| 118 | return $view->renderTemplate('analytics/_special/no-views'); |
||||
| 119 | } |
||||
| 120 | |||||
| 121 | return $view->renderTemplate('analytics/_components/widgets/Realtime/disabled'); |
||||
| 122 | } |
||||
| 123 | |||||
| 124 | return $view->renderTemplate('analytics/_special/not-connected'); |
||||
| 125 | } |
||||
| 126 | |||||
| 127 | /** |
||||
| 128 | * @inheritdoc |
||||
| 129 | */ |
||||
| 130 | public static function maxColspan() |
||||
| 131 | { |
||||
| 132 | return 1; |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | /** |
||||
| 136 | * @inheritDoc ISavableComponentType::getSettingsHtml() |
||||
| 137 | * |
||||
| 138 | * @return string |
||||
| 139 | * @throws \Twig_Error_Loader |
||||
| 140 | * @throws \yii\base\Exception |
||||
| 141 | */ |
||||
| 142 | public function getSettingsHtml() |
||||
| 143 | { |
||||
| 144 | $settings = $this->getSettings(); |
||||
| 145 | $reportingViews = Analytics::$plugin->getViews()->getViews(); |
||||
| 146 | |||||
| 147 | if (count($reportingViews) > 0) { |
||||
| 148 | return Craft::$app->getView()->renderTemplate('analytics/_components/widgets/Realtime/settings', [ |
||||
| 149 | 'settings' => $settings, |
||||
| 150 | 'reportingViews' => $reportingViews, |
||||
| 151 | ]); |
||||
| 152 | } |
||||
| 153 | |||||
| 154 | return null; |
||||
| 155 | } |
||||
| 156 | } |
||||
| 157 |