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; |
9
|
|
|
|
10
|
|
|
use Craft; |
11
|
|
|
use craft\events\RegisterComponentTypesEvent; |
12
|
|
|
use craft\events\RegisterUrlRulesEvent; |
13
|
|
|
use craft\helpers\UrlHelper; |
14
|
|
|
use craft\services\Dashboard; |
15
|
|
|
use craft\services\Fields; |
16
|
|
|
use craft\web\UrlManager; |
17
|
|
|
use craft\web\twig\variables\CraftVariable; |
18
|
|
|
use dukt\analytics\base\PluginTrait; |
19
|
|
|
use dukt\analytics\fields\Report as ReportField; |
20
|
|
|
use dukt\analytics\models\Settings; |
21
|
|
|
use dukt\analytics\web\twig\variables\AnalyticsVariable; |
22
|
|
|
use dukt\analytics\web\assets\analytics\AnalyticsAsset; |
23
|
|
|
use dukt\analytics\widgets\Ecommerce; |
24
|
|
|
use dukt\analytics\widgets\Realtime; |
25
|
|
|
use dukt\analytics\widgets\Report; |
26
|
|
|
use yii\base\Event; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Plugin |
30
|
|
|
* |
31
|
|
|
* @package dukt\analytics |
32
|
|
|
*/ |
33
|
|
|
class Plugin extends \craft\base\Plugin |
34
|
|
|
{ |
35
|
|
|
// Traits |
36
|
|
|
// ========================================================================= |
37
|
|
|
|
38
|
|
|
use PluginTrait; |
39
|
|
|
|
40
|
|
|
// Properties |
41
|
|
|
// ========================================================================= |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
public $hasCpSettings = true; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \dukt\analytics\Plugin The plugin instance. |
50
|
|
|
*/ |
51
|
|
|
public static $plugin; |
52
|
|
|
|
53
|
|
|
// Public Methods |
54
|
|
|
// ========================================================================= |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function init() |
60
|
|
|
{ |
61
|
|
|
parent::init(); |
62
|
|
|
self::$plugin = $this; |
63
|
|
|
|
64
|
|
|
$this->setComponents([ |
65
|
|
|
'analytics' => \dukt\analytics\services\Analytics::class, |
66
|
|
|
'apis' => \dukt\analytics\services\Apis::class, |
67
|
|
|
'cache' => \dukt\analytics\services\Cache::class, |
68
|
|
|
'geo' => \dukt\analytics\services\Geo::class, |
69
|
|
|
'metadata' => \dukt\analytics\services\Metadata::class, |
70
|
|
|
'oauth' => \dukt\analytics\services\Oauth::class, |
71
|
|
|
'reports' => \dukt\analytics\services\Reports::class, |
72
|
|
|
'views' => \dukt\analytics\services\Views::class, |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) { |
76
|
|
|
$rules = [ |
77
|
|
|
'analytics/settings' => 'analytics/settings/index', |
78
|
|
|
'analytics/settings/oauth' => 'analytics/settings/oauth', |
79
|
|
|
'analytics/settings/views' => 'analytics/settings/views', |
80
|
|
|
'analytics/settings/views/new' => 'analytics/settings/edit-view', |
81
|
|
|
'analytics/settings/views/<viewId:\d+>' => 'analytics/settings/edit-view', |
82
|
|
|
'analytics/settings/sites' => 'analytics/settings/sites', |
83
|
|
|
'analytics/settings/sites/<siteId:\d+>' => 'analytics/settings/edit-site', |
84
|
|
|
'analytics/utils' => 'analytics/utils/metadata', |
85
|
|
|
'analytics/utils/metadata' => 'analytics/utils/metadata', |
86
|
|
|
'analytics/tests/data-types' => 'analytics/tests/data-types', |
87
|
|
|
'analytics/tests' => 'analytics/tests/columns', |
88
|
|
|
'analytics/tests/columns' => 'analytics/tests/columns', |
89
|
|
|
'analytics/tests/column-groups' => 'analytics/tests/column-groups', |
90
|
|
|
'analytics/tests/formatting' => 'analytics/tests/formatting', |
91
|
|
|
'analytics/tests/report-widgets' => 'analytics/tests/report-widgets', |
92
|
|
|
'analytics/tests/template-variables' => 'analytics/tests/template-variables', |
93
|
|
|
'analytics/api4' => 'analytics/api4', |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
$event->rules = array_merge($event->rules, $rules); |
97
|
|
|
}); |
98
|
|
|
|
99
|
|
|
Event::on(Dashboard::class, Dashboard::EVENT_REGISTER_WIDGET_TYPES, function(RegisterComponentTypesEvent $event) { |
100
|
|
|
$event->types[] = Ecommerce::class; |
101
|
|
|
$event->types[] = Realtime::class; |
102
|
|
|
$event->types[] = Report::class; |
103
|
|
|
}); |
104
|
|
|
|
105
|
|
|
Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) { |
106
|
|
|
$event->types[] = ReportField::class; |
107
|
|
|
}); |
108
|
|
|
|
109
|
|
|
Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event) { |
110
|
|
|
/** @var CraftVariable $variable */ |
111
|
|
|
$variable = $event->sender; |
112
|
|
|
$variable->set('analytics', AnalyticsVariable::class); |
113
|
|
|
}); |
114
|
|
|
|
115
|
|
|
if ($this->isInstalled && !Craft::$app->getRequest()->getIsConsoleRequest() && Craft::$app->getRequest()->getIsCpRequest()) { |
116
|
|
|
Craft::$app->getView()->registerAssetBundle(AnalyticsAsset::class); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
Craft::setAlias('@analyticsLib', __DIR__ . '/../lib'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritdoc |
124
|
|
|
*/ |
125
|
|
|
public function getSettingsResponse() |
126
|
|
|
{ |
127
|
|
|
$url = UrlHelper::cpUrl('analytics/settings'); |
128
|
|
|
|
129
|
|
|
Craft::$app->controller->redirect($url); |
130
|
|
|
|
131
|
|
|
return ''; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Protected Methods |
135
|
|
|
// ========================================================================= |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritdoc |
139
|
|
|
*/ |
140
|
|
|
protected function createSettingsModel() |
141
|
|
|
{ |
142
|
|
|
return new Settings(); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|