-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
90 lines (78 loc) · 3.15 KB
/
Plugin.php
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
<?php namespace Winter\DriverMandrill;
use App;
use Event;
use System\Classes\PluginBase;
use System\Models\MailSetting;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillTransportFactory;
/**
* DriverMandrill Plugin Information File
*/
class Plugin extends PluginBase
{
const MODE_MANDRILL = 'mandrill';
public function pluginDetails()
{
return [
'name' => 'winter.drivermandrill::lang.plugin.name',
'description' => 'winter.drivermandrill::lang.plugin.description',
'homepage' => 'https://github.com/wintercms/wn-drivermandrill-plugin',
'author' => 'Winter',
'icon' => 'icon-leaf',
];
}
public function register()
{
Event::listen('mailer.beforeRegister', function ($mailManager) {
$mailManager->extend(self::MODE_MANDRILL, function ($config) {
$factory = new MandrillTransportFactory();
if (!isset($config['secret'])) {
$config = $this->app['config']->get('services.mandrill', []);
}
return $factory->create(new Dsn(
'mandrill+'.($config['scheme'] ?? 'api'),
$config['endpoint'] ?? 'default',
$config['secret']
));
});
$settings = MailSetting::instance();
if ($settings->send_mode === self::MODE_MANDRILL) {
$config = App::make('config');
$config->set('mail.mailers.mandrill.transport', self::MODE_MANDRILL);
$config->set('services.mandrill.secret', $settings->mandrill_secret);
}
});
}
public function boot()
{
MailSetting::extend(function ($model) {
$model->bindEvent('model.beforeValidate', function () use ($model) {
$model->rules['mandrill_secret'] = 'required_if:send_mode,' . self::MODE_MANDRILL;
});
$model->mandrill_secret = config('services.mandrill.secret', env('MANDRILL_SECRET'));
});
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_MANDRILL => 'Mandrill']));
$widget->addTabFields([
'mandrill_secret' => [
'tab' => 'system::lang.mail.general',
'label' => 'winter.drivermandrill::lang.mandrill_secret',
'commentAbove' => 'winter.drivermandrill::lang.mandrill_secret_comment',
'type' => 'sensitive',
'trigger' => [
'action' => 'show',
'field' => 'send_mode',
'condition' => 'value[mandrill]',
],
],
]);
});
}
}