File size: 19,656 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343

/**
 * Executes the first step in the update cycle
 *
 * @param container
 * @param step
 * @param state
 */
Mautic.processUpdate = function (container, step, state) {

    var baseUrl = mauticBasePath + '/';

    switch (step) {
        // Set the update page layout
        case 1:
            mQuery.ajax({
                showLoadingBar: true,
                url: mauticAjaxUrl + '?action=core:updateSetUpdateLayout',
                dataType: 'json',
                success: function (response) {
                    if (response.success) {
                        mQuery('div[id=' + container + ']').html(response.content);
                        Mautic.processUpdate(container, step + 1, state);
                    } else if (response.redirect) {
                        window.location = response.redirect;
                    }
                },
                error: function (request, textStatus, errorThrown) {
                    Mautic.processAjaxError(request, textStatus, errorThrown);
                }
            });
            break;

        // Run pre-update checks
        case 2:
            mQuery.ajax({
                showLoadingBar: true,
                url: mauticAjaxUrl + '?action=core:updateRunChecks',
                dataType: 'json',
                success: function (response) {
                    if (response.redirect) {
                        window.location = response.redirect;
                    } else {
                        mQuery('td[id=update-step-running-checks-status]').html('<span class="hidden-xs">' + response.stepStatus + '</span>');

                        if (response.success) {
                            mQuery('td[id=update-step-running-checks-status]').append(mQuery('<i></i>').addClass('pull-right ri-check-line text-success'));
                            mQuery('#updateTable tbody').append('<tr><td>' + response.nextStep + '</td><td id="update-step-downloading-status"><span class="hidden-xs">' + response.nextStepStatus + '</span><i class="pull-right fa fa-spinner fa-spin"></i></td></tr>');
                            Mautic.processUpdate(container, step + 1, state);
                        } else {
                            console.log(response.errors);
                            mQuery('td[id=update-step-running-checks-status]').append(mQuery('<i></i>').addClass('pull-right ri-alert-line text-danger'));
                            mQuery('div[id=main-update-panel]').removeClass('panel-default').addClass('panel-danger');
                            mQuery('div#main-update-panel div.panel-body').prepend(`<div class="alert alert-danger">
                                <p>${response.message}</p>
                                <ul>
                                    ${response.errors.map(error => `<li>${error}</li>`).join("")}
                                </ul>
                                </div>`);
                        }
                    }
                },
                error: function (request, textStatus, errorThrown) {
                    Mautic.processAjaxError(request, textStatus, errorThrown);
                }
            });
            break;

        // Download the update package
        case 3:
            mQuery.ajax({
                showLoadingBar: true,
                url: mauticAjaxUrl + '?action=core:updateDownloadPackage',
                dataType: 'json',
                success: function (response) {
                    if (response.redirect) {
                        window.location = response.redirect;
                    } else {
                        mQuery('td[id=update-step-downloading-status]').html('<span class="hidden-xs">' + response.stepStatus + '</span>');

                        if (response.success) {
                            mQuery('td[id=update-step-downloading-status]').append(mQuery('<i></i>').addClass('pull-right ri-check-line text-success'));
                            mQuery('#updateTable tbody').append('<tr><td>' + response.nextStep + '</td><td id="update-step-extracting-status"><span class="hidden-xs">' + response.nextStepStatus + '</span><i class="pull-right fa fa-spinner fa-spin"></i></td></tr>');
                            Mautic.processUpdate(container, step + 1, state);
                        } else {
                            mQuery('td[id=update-step-downloading-status]').append(mQuery('<i></i>').addClass('pull-right ri-alert-line text-danger'));
                            mQuery('div[id=main-update-panel]').removeClass('panel-default').addClass('panel-danger');
                            mQuery('div#main-update-panel div.panel-body').prepend('<div class="alert alert-danger">' + response.message + '</div>');
                        }
                    }
                },
                error: function (request, textStatus, errorThrown) {
                    Mautic.processAjaxError(request, textStatus, errorThrown);
                }
            });
            break;

        // Extract the update package
        case 4:
            mQuery.ajax({
                showLoadingBar: true,
                url: mauticAjaxUrl + '?action=core:updateExtractPackage',
                dataType: 'json',
                success: function (response) {
                    if (response.redirect) {
                        window.location = response.redirect;
                    } else {
                        mQuery('td[id=update-step-extracting-status]').html('<span class="hidden-xs">' + response.stepStatus + '</span>');

                        if (response.success) {
                            mQuery('td[id=update-step-extracting-status]').append(mQuery('<i></i>').addClass('pull-right ri-check-line text-success'));
                            mQuery('#updateTable tbody').append('<tr><td>' + response.nextStep + '</td><td id="update-step-moving-status"><span class="hidden-xs">' + response.nextStepStatus + '</span><i class="pull-right fa fa-spinner fa-spin"></i></td></tr>');
                            Mautic.processUpdate(container, step + 1, state);
                        } else {
                            mQuery('td[id=update-step-extracting-status]').append(mQuery('<i></i>').addClass('pull-right ri-alert-line text-danger'));
                            mQuery('div[id=main-update-panel]').removeClass('panel-default').addClass('panel-danger');
                            mQuery('div#main-update-panel div.panel-body').prepend('<div class="alert alert-danger">' + response.message + '</div>');
                        }
                    }
                },
                error: function (request, textStatus, errorThrown) {
                    Mautic.processAjaxError(request, textStatus, errorThrown);
                }
            });
            break;

        // Move the updated bundles into production
        case 5:
            mQuery.ajax({
                showLoadingBar: true,
                url: baseUrl + 'upgrade/upgrade.php?task=moveBundles&updateState=' + state,
                dataType: 'json',
                success: function (response) {
                    if (response.redirect) {
                        window.location = response.redirect;
                    } else {
                        mQuery('td[id=update-step-moving-status]').html('<span class="hidden-xs">' + response.stepStatus + '</span>');

                        if (response.error) {
                            mQuery('td[id=update-step-moving-status]').append(mQuery('<i></i>').addClass('pull-right ri-alert-line text-danger'));
                            // If an error state, we cannot move on
                            mQuery('div[id=main-update-panel]').removeClass('panel-default').addClass('panel-danger');
                            mQuery('div#main-update-panel div.panel-body').prepend('<div class="alert alert-danger">' + response.message + '</div>');
                        } else if (response.complete) {
                            mQuery('td[id=update-step-moving-status]').append(mQuery('<i></i>').addClass('pull-right fa fa-spinner fa-spin'));

                            // If complete then we go into the next step
                            Mautic.processUpdate(container, step + 1, response.updateState);
                        } else {
                            mQuery('td[id=update-step-moving-status]').append(mQuery('<i></i>').addClass('pull-right fa fa-spinner fa-spin'));

                            // In this section, the step hasn't completed yet so we repeat it
                            Mautic.processUpdate(container, step, response.updateState);
                        }
                    }
                },
                error: function (request, textStatus, errorThrown) {
                    Mautic.processAjaxError(request, textStatus, errorThrown);
                }
            });
            break;

        // Move the rest of core into production
        case 6:
            mQuery.ajax({
                showLoadingBar: true,
                url: baseUrl + 'upgrade/upgrade.php?task=moveCore&updateState=' + state,
                dataType: 'json',
                success: function (response) {
                    if (response.redirect) {
                        window.location = response.redirect;
                    } else {
                        mQuery('td[id=update-step-moving-status]').html('<span class="hidden-xs">' + response.stepStatus + '</span>');

                        if (response.error) {
                            // If an error state, we cannot move on
                            mQuery('td[id=update-step-moving-status]').append(mQuery('<i></i>').addClass('pull-right ri-alert-line text-danger'));
                            mQuery('div[id=main-update-panel]').removeClass('panel-default').addClass('panel-danger');
                            mQuery('div#main-update-panel div.panel-body').prepend('<div class="alert alert-danger">' + response.message + '</div>');
                        } else if (response.complete) {
                            mQuery('td[id=update-step-moving-status]').append(mQuery('<i></i>').addClass('pull-right fa fa-spinner fa-spin'));

                            // If complete then we go into the next step
                            Mautic.processUpdate(container, step + 1, response.updateState);
                        } else {
                            mQuery('td[id=update-step-moving-status]').append(mQuery('<i></i>').addClass('pull-right fa fa-spinner fa-spin'));

                            // In this section, the step hasn't completed yet so we repeat it
                            Mautic.processUpdate(container, step, response.updateState);
                        }
                    }
                },
                error: function (request, textStatus, errorThrown) {
                    Mautic.processAjaxError(request, textStatus, errorThrown);
                }
            });
            break;

        // Move the vendors into production
        case 7:
            mQuery.ajax({
                showLoadingBar: true,
                url: baseUrl + 'upgrade/upgrade.php?task=moveVendors&updateState=' + state,
                dataType: 'json',
                success: function (response) {
                    if (response.redirect) {
                        window.location = response.redirect;
                    } else {
                        mQuery('td[id=update-step-moving-status]').html('<span class="hidden-xs">' + response.stepStatus + '</span>');

                        if (response.error) {
                            // If an error state, we cannot move on
                            mQuery('td[id=update-step-moving-status]').append(mQuery('<i></i>').addClass('pull-right ri-alert-line text-danger'));
                            mQuery('div[id=main-update-panel]').removeClass('panel-default').addClass('panel-danger');
                            mQuery('div#main-update-panel div.panel-body').prepend('<div class="alert alert-danger">' + response.message + '</div>');
                        } else if (response.complete) {
                            mQuery('td[id=update-step-moving-status]').append(mQuery('<i></i>').addClass('pull-right ri-check-line text-success'));

                            // If complete then we go into the next step
                            mQuery('#updateTable tbody').append('<tr><td>' + response.nextStep + '</td><td id="update-step-cache-status"><span class="hidden-xs">' + response.nextStepStatus + '</span><i class="pull-right fa fa-spinner fa-spin"></i></td></tr>');
                            Mautic.processUpdate(container, step + 1, response.updateState);
                        } else {
                            // In this section, the step hasn't completed yet so we repeat it
                            mQuery('td[id=update-step-moving-status]').append(mQuery('<i></i>').addClass('pull-right fa fa-spinner fa-spin'));
                            Mautic.processUpdate(container, step, response.updateState);
                        }
                    }
                },
                error: function (request, textStatus, errorThrown) {
                    Mautic.processAjaxError(request, textStatus, errorThrown);
                }
            });
            break;

        // Clear the application cache
        case 8:
            mQuery.ajax({
                showLoadingBar: true,
                url: baseUrl + 'upgrade/upgrade.php?task=clearCache&updateState=' + state,
                dataType: 'json',
                success: function (response) {
                    if (response.redirect) {
                        window.location = response.redirect;
                    } else {
                        mQuery('td[id=update-step-cache-status]').html('<span class="hidden-xs">' + response.stepStatus + '</span>');

                        if (response.error) {
                            mQuery('td[id=update-step-cache-status]').append(mQuery('<i></i>').addClass('pull-right ri-alert-line text-danger'));

                            // If an error state, we cannot move on
                            mQuery('div[id=main-update-panel]').removeClass('panel-default').addClass('panel-danger');
                            mQuery('div#main-update-panel div.panel-body').prepend('<div class="alert alert-danger">' + response.message + '</div>');
                        } else if (response.complete) {
                            mQuery('td[id=update-step-cache-status]').append(mQuery('<i></i>').addClass('pull-right ri-check-line text-success'));

                            // If complete then we go into the next step
                            mQuery('#updateTable tbody').append('<tr><td>' + response.nextStep + '</td><td id="update-step-database-status"><span class="hidden-xs">' + response.nextStepStatus + '</span><i class="pull-right fa fa-spinner fa-spin"></i></td></tr>');
                            Mautic.processUpdate(container, step + 1, response.updateState);
                        } else {
                            mQuery('td[id=update-step-cache-status]').append(mQuery('<i></i>').addClass('pull-right fa fa-spinner fa-spin'));

                            // In this section, the step hasn't completed yet so we repeat it
                            Mautic.processUpdate(container, step, response.updateState);
                        }
                    }
                },
                error: function (request, textStatus, errorThrown) {
                    Mautic.processAjaxError(request, textStatus, errorThrown);

                    // Try again, as exceptions can occur due to excessive caching.
                    Mautic.processUpdate(container, step, response.updateState);
                }
            });
            break;

        // Migrate the database
        case 9:
            mQuery.ajax({
                showLoadingBar: true,
                url: mauticAjaxUrl + '?action=core:updateDatabaseMigration&finalize=1',
                dataType: 'json',
                success: function (response) {
                    if (response.redirect) {
                        window.location = response.redirect;
                    } else {
                        mQuery('td[id=update-step-database-status]').html('<span class="hidden-xs">' + response.stepStatus + '</span>');

                        if (response.success) {
                            mQuery('td[id=update-step-database-status]').append(mQuery('<i></i>').addClass('pull-right ri-check-line text-success'));

                            // If complete then we go into the next step
                            mQuery('#updateTable tbody').append('<tr><td>' + response.nextStep + '</td><td id="update-step-finalization-status"><span class="hidden-xs">' + response.nextStepStatus + '</span><i class="pull-right fa fa-spinner fa-spin"></i></td></tr>');
                            Mautic.processUpdate(container, step + 1, state);
                        } else {
                            mQuery('td[id=update-step-database-status]').append(mQuery('<i></i>').addClass('pull-right ri-alert-line text-danger'));

                            // If an error state, we cannot move on
                            mQuery('div[id=main-update-panel]').removeClass('panel-default').addClass('panel-danger');
                            mQuery('div#main-update-panel div.panel-body').prepend('<div class="alert alert-danger">' + response.message + '</div>');
                        }
                    }
                },
                error: function (request, textStatus, errorThrown) {
                    // Redirect to the update/schema page in a last ditch attempt instead of just failing
                    window.location = mauticBaseUrl + 's/update/schema?update=1';
                }
            });
            break;

        // Finalize update
        case 10:
            mQuery.ajax({
                showLoadingBar: true,
                url: mauticAjaxUrl + '?action=core:updateFinalization',
                dataType: 'json',
                success: function (response) {
                    if (response.redirect) {
                        window.location = response.redirect;
                    } else {
                        if (response.success) {
                            mQuery('div[id=' + container + ']').html('<div class="alert alert-mautic">' + response.message + '</div>');

                            if (response.postmessage) {
                                mQuery('<div>'+response.postmessage+'</div>').insertAfter('div[id=' + container + '] .alert');
                            }
                        } else {
                            mQuery('td[id=update-step-finalization-status]').html('<span class="hidden-xs">' + response.stepStatus + '</span>');
                            mQuery('td[id=update-step-finalization-status]').append(mQuery('<i></i>').addClass('pull-right ri-alert-line text-danger'));
                            mQuery('div[id=main-update-panel]').removeClass('panel-default').addClass('panel-danger');
                            mQuery('div#main-update-panel div.panel-body').prepend('<div class="alert alert-danger">' + response.message + '</div>');
                        }
                    }
                },
                error: function (request, textStatus, errorThrown) {
                    Mautic.processAjaxError(request, textStatus, errorThrown);
                }
            });
            break;
    }

    Mautic.stopPageLoadingBar();
};