|
1
|
|
|
<?php namespace Bedard\Shop\FormWidgets; |
|
2
|
|
|
|
|
3
|
|
|
use Backend\Classes\FormWidgetBase; |
|
4
|
|
|
use Bedard\Shop\Classes\DriverManager; |
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* DriverConfigs Form Widget. |
|
9
|
|
|
*/ |
|
10
|
|
|
class DriverConfigs extends FormWidgetBase |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* {@inheritdoc} |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $defaultAlias = 'bedard_shop_driver_configs'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Filter out form data from popup input. |
|
19
|
|
|
* |
|
20
|
|
|
* @param array $data |
|
21
|
|
|
* @return array |
|
22
|
|
|
*/ |
|
23
|
|
|
public function getFormData($data) |
|
24
|
|
|
{ |
|
25
|
|
|
$formData = []; |
|
26
|
|
|
|
|
27
|
|
|
foreach ($data as $key => $value) { |
|
28
|
|
|
if ($key[0] !== '_') { |
|
29
|
|
|
$formData[$key] = $value; |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return $formData; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getSaveValue($value) |
|
40
|
|
|
{ |
|
41
|
|
|
return $value; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function loadAssets() |
|
48
|
|
|
{ |
|
49
|
|
|
$this->addJs('/plugins/bedard/shop/assets/dist/vendor.min.js', 'Bedard.Shop'); |
|
50
|
|
|
$this->addJs('/plugins/bedard/shop/assets/dist/driver_configs.min.js', 'Bedard.Shop'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Load a driver's configuration form. |
|
55
|
|
|
*/ |
|
56
|
|
|
public function onDriverClicked() |
|
57
|
|
|
{ |
|
58
|
|
|
$class = input('class'); |
|
59
|
|
|
$driver = new $class; |
|
60
|
|
|
|
|
61
|
|
|
$manager = new DriverManager; |
|
62
|
|
|
$registration = $manager->getRegistration($class); |
|
63
|
|
|
|
|
64
|
|
|
$model = $driver->getConfigModel(); |
|
65
|
|
|
$model->populate(); |
|
66
|
|
|
|
|
67
|
|
|
$form = $this->makeConfigFromArray($driver->getFormFields()); |
|
68
|
|
|
$form->model = $model; |
|
69
|
|
|
|
|
70
|
|
|
try { |
|
71
|
|
|
$form->model->populate(); |
|
72
|
|
|
} catch (Exception $e) { |
|
73
|
|
|
// json decoding crashes if the content was incorrectly saved as [] |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this->makePartial('popup', [ |
|
77
|
|
|
'class' => $class, |
|
78
|
|
|
'title' => $registration['name'], |
|
79
|
|
|
'form' => $this->makeWidget('Backend\Widgets\Form', $form), |
|
80
|
|
|
]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Save a driver's configuration. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function onDriverSaved() |
|
87
|
|
|
{ |
|
88
|
|
|
$data = input(); |
|
89
|
|
|
$formData = $this->getFormData(input()); |
|
90
|
|
|
|
|
91
|
|
|
$driver = new $data['_class']; |
|
92
|
|
|
$driver->saveConfig($formData); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Prepares the form widget view data. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function prepareVars() |
|
99
|
|
|
{ |
|
100
|
|
|
$manager = new DriverManager; |
|
101
|
|
|
$drivers = $this->getConfig('drivers'); |
|
102
|
|
|
|
|
103
|
|
|
$this->vars['drivers'] = $manager->getDriversByType($drivers); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function render() |
|
110
|
|
|
{ |
|
111
|
|
|
$this->prepareVars(); |
|
112
|
|
|
|
|
113
|
|
|
return $this->makePartial('driverconfigs'); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|