|
1
|
|
|
define([ |
|
2
|
|
|
'jquery', |
|
3
|
|
|
'underscore', |
|
4
|
|
|
'routing', |
|
5
|
|
|
'backbone', |
|
6
|
|
|
'orotranslation/js/translator', |
|
7
|
|
|
'oroui/js/mediator', |
|
8
|
|
|
'oroui/js/messenger' |
|
9
|
|
|
], function($, _, routing, Backbone, __, mediator, messenger) { |
|
10
|
|
|
'use strict'; |
|
11
|
|
|
|
|
12
|
|
|
var MadgentoCheckerView; |
|
13
|
|
|
|
|
14
|
|
|
MadgentoCheckerView = Backbone.View.extend({ |
|
15
|
|
|
events: { |
|
16
|
|
|
click: 'processClick' |
|
17
|
|
|
}, |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Check url |
|
21
|
|
|
* @property string |
|
22
|
|
|
*/ |
|
23
|
|
|
route: 'oro_magento_integration_check', |
|
24
|
|
|
url: null, |
|
25
|
|
|
id: null, |
|
26
|
|
|
form: null, |
|
27
|
|
|
/** |
|
28
|
|
|
* Use in case we edit existed integration |
|
29
|
|
|
* and element type is disabled |
|
30
|
|
|
*/ |
|
31
|
|
|
requiredOptions: [ |
|
32
|
|
|
'websiteSelectEl', |
|
33
|
|
|
'websitesListEl', |
|
34
|
|
|
'isExtensionInstalledEl', |
|
35
|
|
|
'connectorsEl', |
|
36
|
|
|
'extensionVersionEl', |
|
37
|
|
|
'magentoVersionEl', |
|
38
|
|
|
'sharedGuestEmailListEl', |
|
39
|
|
|
'isDisplayOrderNotesEl', |
|
40
|
|
|
'isOrderNoteSupportExtensionVersionEl' |
|
41
|
|
|
], |
|
42
|
|
|
|
|
43
|
|
|
resultTemplate: _.template( |
|
44
|
|
|
'<div class="alert alert-<%= type %> connection-status"><%= message %></div>' |
|
45
|
|
|
), |
|
46
|
|
|
|
|
47
|
|
|
connectorTemplate: _.template( |
|
48
|
|
|
'<div class="oro-clearfix">' + |
|
49
|
|
|
'<input type="checkbox" id="oro_integration_channel_form_connectors_<%= i %>" ' + |
|
50
|
|
|
'name="oro_integration_channel_form[connectors][]" value="<%= name %>" <%= checked %>>' + |
|
51
|
|
|
'<label for="oro_integration_channel_form_connectors_<%= i %>"><%= label %></label>' + |
|
52
|
|
|
'</div>' |
|
53
|
|
|
), |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritDoc |
|
57
|
|
|
*/ |
|
58
|
|
|
constructor: function MadgentoCheckerView() { |
|
59
|
|
|
MadgentoCheckerView.__super__.constructor.apply(this, arguments); |
|
60
|
|
|
}, |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritDoc |
|
64
|
|
|
*/ |
|
65
|
|
|
initialize: function(options) { |
|
66
|
|
|
this.options = _.defaults(options || {}, this.options); |
|
67
|
|
|
this.id = options.transportEntityId || null; |
|
68
|
|
|
this.url = this.getUrl(); |
|
69
|
|
|
|
|
70
|
|
|
var requiredMissed = this.requiredOptions.filter(function(option) { |
|
71
|
|
|
return _.isUndefined(options[option]); |
|
72
|
|
|
}); |
|
73
|
|
|
if (requiredMissed.length) { |
|
74
|
|
|
throw new TypeError('Missing required option(s): ' + requiredMissed.join(',')); |
|
75
|
|
|
} |
|
76
|
|
|
}, |
|
77
|
|
|
|
|
78
|
|
|
getForm: function() { |
|
79
|
|
|
if (this.form !== null && this.form.length) { |
|
80
|
|
|
return this.form; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
this.form = this.$el.parents('form'); |
|
84
|
|
|
|
|
85
|
|
|
if (this.form.length === 0) { |
|
86
|
|
|
throw new Error('Expected form not found !'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return this.form; |
|
90
|
|
|
}, |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param additionalParams {Array} |
|
94
|
|
|
* @returns {*} |
|
95
|
|
|
*/ |
|
96
|
|
|
getUrl: function(additionalParams) { |
|
97
|
|
|
var params = _.extend({ |
|
98
|
|
|
id: this.id |
|
99
|
|
|
}, additionalParams || {}); |
|
100
|
|
|
|
|
101
|
|
|
return routing.generate(this.route, params); |
|
102
|
|
|
}, |
|
103
|
|
|
|
|
104
|
|
|
getIntegrationAndTransportTypeParams: function(fields) { |
|
105
|
|
|
var params = {}; |
|
106
|
|
|
var integrationType = _.first( |
|
107
|
|
|
_.filter(fields, function(field) { |
|
108
|
|
|
return field.name.indexOf('[type]') !== -1; |
|
109
|
|
|
}) |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
if (_.isObject(integrationType)) { |
|
113
|
|
|
params.type = integrationType.value; |
|
114
|
|
|
} else { |
|
115
|
|
|
/** |
|
116
|
|
|
* In case we on edit page and field type is disabled |
|
117
|
|
|
* so we can't get it from element data array |
|
118
|
|
|
*/ |
|
119
|
|
|
var typeEl = this.getForm().find('[name$="[type]"]').first(); |
|
120
|
|
|
if (typeEl.length) { |
|
121
|
|
|
params.type = typeEl.val(); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
var transportType = _.first( |
|
126
|
|
|
_.filter(fields, function(field) { |
|
127
|
|
|
return field.name.indexOf('[transportType]') !== -1; |
|
128
|
|
|
}) |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
|
|
if (_.isObject(transportType)) { |
|
132
|
|
|
params.transport = transportType.value; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return params; |
|
136
|
|
|
}, |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param fields {Array} |
|
140
|
|
|
* @returns {Array} |
|
141
|
|
|
*/ |
|
142
|
|
|
getDataForRequestFromFields: function(fields) { |
|
143
|
|
|
var data = _.filter(fields, function(field) { |
|
144
|
|
|
return field.name.indexOf('[transport]') !== -1; |
|
145
|
|
|
}); |
|
146
|
|
|
|
|
147
|
|
|
return _.map(data, function(field) { |
|
148
|
|
|
field.name = field.name.replace(/.+\[(.+)\]$/, 'check[$1]'); |
|
149
|
|
|
return field; |
|
150
|
|
|
}); |
|
151
|
|
|
}, |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Click handler |
|
155
|
|
|
*/ |
|
156
|
|
|
processClick: function() { |
|
157
|
|
|
var fields = this.getForm().formToArray(); |
|
158
|
|
|
var transportAndIntegrationTypeParams = this.getIntegrationAndTransportTypeParams(fields); |
|
159
|
|
|
var url = this.getUrl(transportAndIntegrationTypeParams); |
|
160
|
|
|
var data = this.getDataForRequestFromFields(fields); |
|
161
|
|
|
|
|
162
|
|
|
mediator.execute('showLoading'); |
|
163
|
|
|
$.post({ |
|
164
|
|
|
url: url, |
|
165
|
|
|
data: data, |
|
166
|
|
|
errorHandlerMessage: __('oro.magento.error') |
|
167
|
|
|
}).done(_.bind(this.responseHandler, this)) |
|
168
|
|
|
.always(function() { |
|
169
|
|
|
mediator.execute('hideLoading'); |
|
170
|
|
|
}); |
|
171
|
|
|
}, |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Handler ajax response |
|
175
|
|
|
* |
|
176
|
|
|
* @param res {object} |
|
177
|
|
|
*/ |
|
178
|
|
|
responseHandler: function(res) { |
|
179
|
|
|
if (res.success || false) { |
|
180
|
|
|
this.handleWebsites(res); |
|
181
|
|
|
this.handleIsExtensionInstalled(res); |
|
182
|
|
|
this.handleAdminUrl(res); |
|
183
|
|
|
this.handleConnectors(res); |
|
184
|
|
|
this.handleExtensionVersion(res); |
|
185
|
|
|
this.handleIsOrderNoteSupportExtensionVersion(res); |
|
186
|
|
|
this.handleMagentoVersion(res); |
|
187
|
|
|
this.handleSharedGuestEmailListEl(res); |
|
188
|
|
|
|
|
189
|
|
|
this.renderSuccessMessage(res); |
|
190
|
|
|
} else { |
|
191
|
|
|
this.renderErrorMessage(res); |
|
192
|
|
|
} |
|
193
|
|
|
}, |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param {Object} res |
|
197
|
|
|
*/ |
|
198
|
|
|
renderSuccessMessage: function(res) { |
|
199
|
|
|
if (res.isExtensionInstalled || false) { |
|
200
|
|
|
if (res.isSupportedVersion || false) { |
|
201
|
|
|
this.renderResult('success', __( |
|
202
|
|
|
'oro.magento.success_bridge', |
|
203
|
|
|
{extension_version: res.extensionVersion} |
|
204
|
|
|
)); |
|
205
|
|
|
} else { |
|
206
|
|
|
this.renderResult( |
|
207
|
|
|
'warning', |
|
208
|
|
|
__( |
|
209
|
|
|
'oro.magento.outdated_warning', |
|
210
|
|
|
{ |
|
211
|
|
|
extension_version: res.extensionVersion, |
|
212
|
|
|
required_version: res.requiredExtensionVersion |
|
213
|
|
|
} |
|
214
|
|
|
) |
|
215
|
|
|
); |
|
216
|
|
|
} |
|
217
|
|
|
} else { |
|
218
|
|
|
this.renderResult('success', __('oro.magento.success')); |
|
219
|
|
|
} |
|
220
|
|
|
}, |
|
221
|
|
|
|
|
222
|
|
|
renderErrorMessage: function(res) { |
|
223
|
|
|
if (res.errorMessage) { |
|
224
|
|
|
this.renderResult('error', res.errorMessage); |
|
225
|
|
|
} else { |
|
226
|
|
|
this.renderResult('error', __('oro.magento.error')); |
|
227
|
|
|
} |
|
228
|
|
|
}, |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* websitesModificationAllowed might be undefined, but it should not be false |
|
232
|
|
|
* false is equal - denied |
|
233
|
|
|
* |
|
234
|
|
|
* @param {Object} res |
|
235
|
|
|
*/ |
|
236
|
|
|
handleWebsites: function(res) { |
|
237
|
|
|
if (this.options.websitesModificationAllowed !== false && res.websites) { |
|
238
|
|
|
var $listEl = $(this.options.websitesListEl); |
|
239
|
|
|
var $websiteSelectEl = $(this.options.websiteSelectEl); |
|
240
|
|
|
|
|
241
|
|
|
$listEl.val(JSON.stringify(res.websites)); |
|
242
|
|
|
$websiteSelectEl.empty(); |
|
243
|
|
|
_.each(res.websites, function(website) { |
|
244
|
|
|
$websiteSelectEl.append($('<option />').val(website.id).text(website.label)); |
|
245
|
|
|
}); |
|
246
|
|
|
$websiteSelectEl.trigger('change'); |
|
247
|
|
|
} |
|
248
|
|
|
}, |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* @param {Object} res |
|
252
|
|
|
*/ |
|
253
|
|
|
handleIsExtensionInstalled: function(res) { |
|
254
|
|
|
$(this.options.isExtensionInstalledEl) |
|
255
|
|
|
.val(res.isExtensionInstalled || false ? 1 : 0); |
|
256
|
|
|
}, |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* @param {Object} res |
|
260
|
|
|
*/ |
|
261
|
|
|
handleIsOrderNoteSupportExtensionVersion: function(res) { |
|
262
|
|
|
var isOrderNoteSupportDisabledAttrValue = (res.isOrderNoteSupportExtensionVersion || false) === false; |
|
263
|
|
|
$(this.options.isDisplayOrderNotesEl).inputWidget('disable', isOrderNoteSupportDisabledAttrValue); |
|
264
|
|
|
|
|
265
|
|
|
$(this.options.isOrderNoteSupportExtensionVersionEl) |
|
266
|
|
|
.val(res.isOrderNoteSupportExtensionVersion || false ? 1 : 0); |
|
267
|
|
|
}, |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @param {Object} res |
|
271
|
|
|
*/ |
|
272
|
|
|
handleAdminUrl: function(res) { |
|
273
|
|
|
if (this.options.adminUrlEl) { |
|
274
|
|
|
$(this.options.adminUrlEl).val(res.adminUrl || ''); |
|
275
|
|
|
} |
|
276
|
|
|
}, |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @param {Object} res |
|
280
|
|
|
*/ |
|
281
|
|
|
handleConnectors: function(res) { |
|
282
|
|
|
if (res.connectors) { |
|
283
|
|
|
var connectors = res.connectors; |
|
284
|
|
|
var $form = this.$el.parents('form'); |
|
285
|
|
|
var $connectorsEl = $form.find(this.options.connectorsEl); |
|
286
|
|
|
var i = 0; |
|
287
|
|
|
var checkedBoxes = $connectorsEl.find(':checked'); |
|
288
|
|
|
var checked = {}; |
|
289
|
|
|
|
|
290
|
|
|
_.each(checkedBoxes, function(el) { |
|
291
|
|
|
checked[$(el).val()] = 'checked'; |
|
292
|
|
|
}); |
|
293
|
|
|
|
|
294
|
|
|
$connectorsEl.empty(); |
|
295
|
|
|
for (var key in connectors) { |
|
296
|
|
|
if (connectors.hasOwnProperty(key)) { |
|
297
|
|
|
$connectorsEl.append( |
|
298
|
|
|
this.connectorTemplate({ |
|
299
|
|
|
name: key, |
|
300
|
|
|
label: connectors[key], |
|
301
|
|
|
checked: checked[key] || '', |
|
302
|
|
|
i: i |
|
303
|
|
|
}) |
|
304
|
|
|
); |
|
305
|
|
|
i++; |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
}, |
|
310
|
|
|
|
|
311
|
|
|
handleExtensionVersion: function(res) { |
|
312
|
|
|
$(this.options.extensionVersionEl).val(res.extensionVersion || ''); |
|
313
|
|
|
}, |
|
314
|
|
|
|
|
315
|
|
|
handleMagentoVersion: function(res) { |
|
316
|
|
|
$(this.options.magentoVersionEl).val(res.magentoVersion || ''); |
|
317
|
|
|
}, |
|
318
|
|
|
|
|
319
|
|
|
handleSharedGuestEmailListEl: function(res) { |
|
320
|
|
|
var disabledAttrValue = (res.isExtensionInstalled || false) === false; |
|
321
|
|
|
$(this.options.sharedGuestEmailListEl).prop( |
|
322
|
|
|
'disabled', |
|
323
|
|
|
disabledAttrValue |
|
324
|
|
|
); |
|
325
|
|
|
}, |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* Render check result message |
|
329
|
|
|
* |
|
330
|
|
|
* @param type string |
|
331
|
|
|
* @param message string |
|
332
|
|
|
*/ |
|
333
|
|
|
renderResult: function(type, message) { |
|
334
|
|
|
var container = this.$el.parent(); |
|
335
|
|
|
container.find('.alert').remove(); |
|
336
|
|
|
messenger.notificationMessage(type, message, {container: container, template: this.resultTemplate}); |
|
337
|
|
|
} |
|
338
|
|
|
}); |
|
339
|
|
|
|
|
340
|
|
|
return MadgentoCheckerView; |
|
341
|
|
|
}); |
|
342
|
|
|
|