|
1
|
|
|
<?php namespace Bedard\Shop\FormWidgets; |
|
2
|
|
|
|
|
3
|
|
|
use Bedard\Shop\Models\DriverConfig as ConfigModel; |
|
4
|
|
|
use Form; |
|
5
|
|
|
use Model; |
|
6
|
|
|
use Backend\Classes\FormWidgetBase; |
|
7
|
|
|
use Bedard\Shop\Classes\DriverManager; |
|
8
|
|
|
use Bedard\Shop\Interfaces\DriverInterface; |
|
9
|
|
|
use October\Rain\Exception\ValidationException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* DriverConfig Form Widget. |
|
13
|
|
|
*/ |
|
14
|
|
|
class DriverConfig extends FormWidgetBase |
|
15
|
|
|
{ |
|
16
|
|
|
use \Bedard\Shop\Traits\LangJsonable; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritdoc} |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $defaultAlias = 'bedard_shop_driver_config'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Bedard\Shop\Classes\DriverManager; |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $manager; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function init() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->manager = DriverManager::instance(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function render() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->prepareVars(); |
|
42
|
|
|
|
|
43
|
|
|
return $this->makePartial('driverconfig'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Prepares the form widget view data. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function prepareVars() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->vars['drivers'] = $this->getShippingDrivers(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function loadAssets() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->addJs('/plugins/bedard/shop/assets/dist/vendor.js'); |
|
60
|
|
|
$this->addJs('/plugins/bedard/shop/assets/dist/driverconfig.js', 'Bedard.Shop'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the form for a driver. |
|
65
|
|
|
* |
|
66
|
|
|
* @param DriverInterface $driver |
|
67
|
|
|
* @return object |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function getDriverForm(DriverInterface $driver) |
|
70
|
|
|
{ |
|
71
|
|
|
$model = new Model; |
|
72
|
|
|
// foreach (array_merge(array_keys($fields), array_keys($tabFields)) as $key) { |
|
|
|
|
|
|
73
|
|
|
// $model->$key = $driver->getConfig($key); |
|
|
|
|
|
|
74
|
|
|
// } |
|
75
|
|
|
|
|
76
|
|
|
$form = $this->makeConfigFromArray($driver->getFormFields()); |
|
77
|
|
|
$form->model = $model; |
|
78
|
|
|
|
|
79
|
|
|
return $form; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getSaveValue($value) |
|
86
|
|
|
{ |
|
87
|
|
|
return $value; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get the driver details. |
|
92
|
|
|
* |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function getShippingDrivers() |
|
96
|
|
|
{ |
|
97
|
|
|
foreach ($this->manager->getShippingDrivers() as $driver) { |
|
98
|
|
|
$drivers[] = [ |
|
|
|
|
|
|
99
|
|
|
'driver' => get_class($driver), |
|
100
|
|
|
'details' => $driver->driverDetails(), |
|
101
|
|
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $drivers; |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Load the driver settings. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function onLoadDriverSettings() |
|
113
|
|
|
{ |
|
114
|
|
|
$driverClass = input('driver'); |
|
115
|
|
|
$driver = new $driverClass; |
|
116
|
|
|
|
|
117
|
|
|
$config = $this->getDriverForm($driver); |
|
118
|
|
|
$form = $this->makeWidget('Backend\Widgets\Form', $config); |
|
119
|
|
|
|
|
120
|
|
|
return $this->makePartial('popup', [ |
|
121
|
|
|
'driver' => $driverClass, |
|
122
|
|
|
'details' => $driver->driverDetails(), |
|
123
|
|
|
'form' => $form->render(), |
|
124
|
|
|
]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Validate the form. |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
* @throws \AjaxException |
|
132
|
|
|
*/ |
|
133
|
|
|
public function onFormSubmitted() |
|
134
|
|
|
{ |
|
135
|
|
|
// grab our relevant input data |
|
136
|
|
|
$data = input(); |
|
137
|
|
|
$driverClass = $data['_driver']; |
|
138
|
|
|
$driver = new $driverClass; |
|
139
|
|
|
|
|
140
|
|
|
// clean up fields not needed for driver data |
|
141
|
|
|
unset($data['_driver']); |
|
142
|
|
|
unset($data['_session_key']); |
|
143
|
|
|
unset($data['_token']); |
|
144
|
|
|
|
|
145
|
|
|
// give the driver a change to validate this form if they want to |
|
146
|
|
|
if (method_exists($driver, 'validate')) $driver->validate($data); |
|
147
|
|
|
|
|
148
|
|
|
// if the driver defined it's own save method, call it |
|
149
|
|
|
if (method_exists($driver, 'save')) $driver->save($data); |
|
150
|
|
|
|
|
151
|
|
|
// otherwise just use the default save |
|
152
|
|
|
else $this->save($driverClass, $data); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Save a driver. |
|
157
|
|
|
* |
|
158
|
|
|
* @param name $driver The class name of the driver. |
|
|
|
|
|
|
159
|
|
|
* @param array $config Driver data to save. |
|
160
|
|
|
* @return void |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function save($driverClass, $config) |
|
163
|
|
|
{ |
|
164
|
|
|
$model = ConfigModel::firstOrNew(['driver' => $driverClass]); |
|
165
|
|
|
$model->config = $config; |
|
166
|
|
|
$model->save(); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.