-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlugin.php
160 lines (131 loc) · 5.69 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
namespace Vdlp\RedirectConditions;
use Backend\Classes\FormTabs;
use Backend\Widgets\Form;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Facades\Event;
use October\Rain\Foundation\Application;
use System\Classes\PluginBase;
use Vdlp\Redirect\Classes\Contracts\RedirectConditionInterface;
use Vdlp\Redirect\Classes\Contracts\RedirectManagerInterface;
use Vdlp\Redirect\Controllers\Redirects;
use Vdlp\Redirect\Models\Redirect;
use Vdlp\RedirectConditions\Models\ConditionParameter;
final class Plugin extends PluginBase
{
public $require = [
'Vdlp.Redirect',
];
public function pluginDetails(): array
{
return [
'name' => 'Redirect Conditions',
'description' => 'Allows plugin developers to create Redirect Condition extension plugins.',
'author' => 'Van der Let & Partners',
'icon' => 'icon-link',
'homepage' => 'https://octobercms.com/plugin/vdlp-redirect',
];
}
public function register(): void
{
Redirect::extend(static function (Redirect $redirect): void {
$redirect->hasMany['conditionParameters'] = [
0 => ConditionParameter::class,
'table' => 'vdlp_redirectconditions_condition_parameters',
];
$redirect->bindEvent('model.afterSave', static function () use ($redirect): void {
/** @var Dispatcher $dispatcher */
$dispatcher = resolve(Dispatcher::class);
$dispatcher->dispatch('vdlp.redirect.afterRedirectSave', ['redirect' => $redirect]);
});
});
Event::listen('vdlp.redirect.afterRedirectSave', static function (Redirect $redirect): void {
if (!Application::getInstance()->runningInBackend()) {
return;
}
/** @var RedirectManagerInterface $manager */
$manager = resolve(RedirectManagerInterface::class);
$postData = request()->get('Redirect', []);
/** @var string $condition */
foreach ($manager->getConditions() as $condition) {
/** @var RedirectConditionInterface $condition */
$condition = resolve($condition);
$formValues = array_get(
$postData,
sprintf('_VdlpRedirectConditionParameters.%s', $condition->getCode()),
[]
);
$isEnabled = (bool) array_get(
$postData,
sprintf('_VdlpRedirectConditionEnabled.%s', $condition->getCode()),
false
);
ConditionParameter::query()->updateOrCreate(
[
'redirect_id' => $redirect->getKey(),
'condition_code' => $condition->getCode(),
],
[
'is_enabled' => $isEnabled ? date('Y-m-d H:i:s') : null,
'parameters' => $formValues,
]
);
}
});
Event::listen('backend.form.extendFields', static function (Form $form): void {
if (!$form->getController() instanceof Redirects) {
return;
}
if (!($form->model instanceof Redirect)) {
return;
}
/** @var RedirectManagerInterface $manager */
$manager = resolve(RedirectManagerInterface::class);
foreach ($manager->getConditions() as $condition) {
/** @var RedirectConditionInterface $condition */
$condition = resolve($condition);
$formParentFieldKey = sprintf('_VdlpRedirectConditionEnabled[%s]', $condition->getCode());
$form->addFields([
$formParentFieldKey => [
'label' => $condition->getDescription(),
'tab' => RedirectConditionInterface::TAB_NAME,
'type' => 'checkbox',
'comment' => $condition->getExplanation(),
],
], FormTabs::SECTION_PRIMARY);
foreach ($condition->getFormConfig() as $formFieldKey => $formField) {
$formFieldKey = sprintf(
'_VdlpRedirectConditionParameters[%s][%s]',
$condition->getCode(),
$formFieldKey
);
$formField['trigger'] = [
'action' => 'show',
'field' => $formParentFieldKey,
'condition' => 'checked',
];
$formField['cssClass'] = 'field-indent';
$form->addFields([
$formFieldKey => $formField,
], FormTabs::SECTION_PRIMARY);
}
}
if (!$form->model->exists) {
return;
}
/*
* Populate the form with the stored values.
*/
$data = [];
/** @var ConditionParameter $conditionParameter */
foreach ($form->model->getAttribute('conditionParameters') as $conditionParameter) {
$data['_VdlpRedirectConditionParameters'][$conditionParameter->getAttribute('condition_code')]
= $conditionParameter->getAttribute('parameters');
$data['_VdlpRedirectConditionEnabled'][$conditionParameter->getAttribute('condition_code')]
= (bool) $conditionParameter->getAttribute('is_enabled');
}
$form->setFormValues($data);
});
}
}