-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
126 lines (115 loc) · 5.62 KB
/
Plugin.php
File metadata and controls
126 lines (115 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php namespace Winter\DriverMSGraph;
use App;
use Config;
use Event;
use System\Classes\PluginBase;
use System\Models\MailSetting;
use GuzzleHttp\Client;
use InnoGE\LaravelMsGraphMail\LaravelMsGraphMailServiceProvider;
/**
* DriverMSGRAPH Plugin Information File
*/
class Plugin extends PluginBase
{
public $elevated = true;
const MODE_MSGRAPH = 'microsoft-graph';
public function pluginDetails()
{
return [
'name' => 'winter.drivermsgraph::lang.plugin.name',
'description' => 'winter.drivermsgraph::lang.plugin.description',
'homepage' => 'https://github.com/wintercms/wn-drivermsgraph-plugin',
'author' => 'Winter CMS',
'icon' => 'icon-leaf',
];
}
public function register()
{
App::register(LaravelMsGraphMailServiceProvider::class);
Event::listen('mailer.beforeRegister', function ($mailManager) {
$settings = MailSetting::instance();
if ($settings->send_mode === self::MODE_MSGRAPH) {
$config = App::make('config');
$config->set('mail.mailers.microsoft-graph.transport', self::MODE_MSGRAPH);
$config->set('mail.mailers.microsoft-graph.client_id', $settings->msgraph_client_id);
$config->set('mail.mailers.microsoft-graph.client_secret', $settings->msgraph_client_secret);
$config->set('mail.mailers.microsoft-graph.tenant_id', $settings->msgraph_tenant_id);
$config->set('mail.mailers.microsoft-graph.save_to_sent_items', $settings->msgraph_save_to_sent_items);
$config->set('mail.mailers.microsoft-graph.from', Config::get('mail.from'));
}
});
}
public function boot()
{
MailSetting::extend(function ($model) {
$model->bindEvent('model.beforeValidate', function () use ($model) {
$model->rules['msgraph_client_id'] = 'required_if:send_mode,' . self::MODE_MSGRAPH;
$model->rules['msgraph_client_secret'] = 'required_if:send_mode,' . self::MODE_MSGRAPH;
$model->rules['msgraph_tenant_id'] = 'required_if:send_mode,' . self::MODE_MSGRAPH;
$model->rules['msgraph_save_to_sent_items'] = 'required_if:send_mode,' . self::MODE_MSGRAPH;
});
$model->msgraph_client_id = config('mail.mailers.microsoft-graph.client_id', env('MICROSOFT_GRAPH_CLIENT_ID'));
$model->msgraph_client_secret = config('mail.mailers.microsoft-graph.client_secret', env('MICROSOFT_GRAPH_CLIENT_SECRET'));
$model->msgraph_tenant_id = config('mail.mailers.microsoft-graph.tenant_id', env('MICROSOFT_GRAPH_TENANT_ID'));
$model->msgraph_save_to_sent_items = config('mail.mailers.microsoft-graph.save_to_sent_items', env('MAIL_SAVE_TO_SENT_ITEMS', false));
});
Event::listen('backend.form.extendFields', function ($widget) {
if (!$widget->getController() instanceof \System\Controllers\Settings) {
return;
}
if (!$widget->model instanceof MailSetting) {
return;
}
$field = $widget->getField('send_mode');
$field->options(array_merge($field->options(), [self::MODE_MSGRAPH => 'MSGRAPH']));
$widget->addTabFields([
'msgraph_client_id' => [
'tab' => 'system::lang.mail.general',
'label' => 'winter.drivermsgraph::lang.client_id',
'commentAbove' => 'winter.drivermsgraph::lang.client_id_comment',
'trigger' => [
'action' => 'show',
'field' => 'send_mode',
'condition' => 'value[' . self::MODE_MSGRAPH . ']',
],
'span' => 'left',
],
'msgraph_client_secret' => [
'tab' => 'system::lang.mail.general',
'label' => 'winter.drivermsgraph::lang.client_secret',
'type' => 'sensitive',
'commentAbove' => 'winter.drivermsgraph::lang.client_secret_comment',
'trigger' => [
'action' => 'show',
'field' => 'send_mode',
'condition' => 'value[' . self::MODE_MSGRAPH . ']',
],
'span' => 'auto',
],
'msgraph_tenant_id' => [
'tab' => 'system::lang.mail.general',
'label' => 'winter.drivermsgraph::lang.tenant_id',
'commentAbove' => 'winter.drivermsgraph::lang.tenant_id_comment',
'trigger' => [
'action' => 'show',
'field' => 'send_mode',
'condition' => 'value[' . self::MODE_MSGRAPH . ']',
],
'span' => 'left',
],
'msgraph_save_to_sent_items' => [
'tab' => 'system::lang.mail.general',
'label' => 'winter.drivermsgraph::lang.save_to_sent_items',
'type' => 'checkbox',
'commentAbove' => 'winter.drivermsgraph::lang.save_to_sent_items_comment',
'trigger' => [
'action' => 'show',
'field' => 'send_mode',
'condition' => 'value[' . self::MODE_MSGRAPH . ']',
],
'span' => 'auto',
],
]);
});
}
}