backup.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * @deprecated since version 2.2.0
  7. */
  8. /* global AdminBackup, setLocation */
  9. /* eslint-disable strict */
  10. define([
  11. 'jquery',
  12. 'Magento_Ui/js/modal/modal',
  13. 'mage/mage',
  14. 'prototype'
  15. ], function (jQuery) {
  16. window.AdminBackup = new Class.create();
  17. AdminBackup.prototype = {
  18. /**
  19. * Initialize.
  20. */
  21. initialize: function () {
  22. this.reset();
  23. this.rollbackUrl = this.backupUrl = '';
  24. },
  25. /**
  26. * reset.
  27. */
  28. reset: function () {
  29. this.time = 0;
  30. this.type = '';
  31. },
  32. /**
  33. * @param {*} type
  34. * @return {Boolean}
  35. */
  36. backup: function (type) {
  37. this.reset();
  38. this.type = type;
  39. this.requestBackupOptions();
  40. return false;
  41. },
  42. /**
  43. * @param {*} type
  44. * @param {*} time
  45. * @return {Boolean}
  46. */
  47. rollback: function (type, time) {
  48. this.reset();
  49. this.time = time;
  50. this.type = type;
  51. this.showRollbackWarning();
  52. return false;
  53. },
  54. /**
  55. * Show rollback warning.
  56. */
  57. showRollbackWarning: function () {
  58. this.showPopup('rollback-warning');
  59. },
  60. /**
  61. * request backup options.
  62. */
  63. requestBackupOptions: function () {
  64. var action;
  65. this.hidePopups();
  66. action = this.type != 'snapshot' ? 'hide' : 'show'; //eslint-disable-line eqeqeq
  67. this.showPopup('backup-options');
  68. $$('#exclude-media-checkbox-container').invoke(action);
  69. },
  70. /**
  71. * Request password.
  72. */
  73. requestPassword: function () {
  74. this.hidePopups();
  75. this.showPopup('rollback-request-password');
  76. this.type != 'db' ? //eslint-disable-line eqeqeq
  77. $('use-ftp-checkbox-row').show() :
  78. $('use-ftp-checkbox-row').hide();
  79. },
  80. /**
  81. * Toggle Ftp Credentials Form.
  82. */
  83. toggleFtpCredentialsForm: function () {
  84. $('use_ftp').checked ? $('ftp-credentials-container').show()
  85. : $('ftp-credentials-container').hide();
  86. $$('#ftp-credentials-container input').each(function (item) {
  87. if (item.name == 'ftp_path') { //eslint-disable-line eqeqeq
  88. return;
  89. }
  90. $('use_ftp').checked ? item.addClassName('required-entry') : item.removeClassName('required-entry');
  91. });
  92. },
  93. /**
  94. * Submit backup.
  95. */
  96. submitBackup: function () {
  97. var data = {
  98. 'type': this.type,
  99. 'maintenance_mode': $('backup_maintenance_mode').checked ? 1 : 0,
  100. 'backup_name': $('backup_name').value,
  101. 'exclude_media': $('exclude_media').checked ? 1 : 0
  102. };
  103. new Ajax.Request(this.backupUrl, {
  104. onSuccess: function (transport) {
  105. this.processResponse(transport, 'backup-options');
  106. }.bind(this),
  107. method: 'post',
  108. parameters: data
  109. });
  110. this.modal.modal('closeModal');
  111. },
  112. /**
  113. * Submit rollback.
  114. */
  115. submitRollback: function () {
  116. var data = this.getPostData();
  117. new Ajax.Request(this.rollbackUrl, {
  118. onSuccess: function (transport) {
  119. this.processResponse(transport, 'rollback-request-password');
  120. }.bind(this),
  121. method: 'post',
  122. parameters: data
  123. });
  124. this.modal.modal('closeModal');
  125. },
  126. /**
  127. * @param {Object} transport
  128. * @param {*} popupId
  129. */
  130. processResponse: function (transport, popupId) {
  131. var json;
  132. if (!transport.responseText.isJSON()) {
  133. return;
  134. }
  135. json = transport.responseText.evalJSON();
  136. if (json.error) {
  137. this.showPopup(popupId);
  138. this.displayError(popupId, json.error);
  139. return;
  140. }
  141. if (json['redirect_url']) {
  142. setLocation(json['redirect_url']);
  143. }
  144. },
  145. /**
  146. * @param {*} parentContainer
  147. * @param {*} message
  148. */
  149. displayError: function (parentContainer, message) {
  150. var messageHtml = this.getErrorMessageHtml(message);
  151. $$('#' + parentContainer + ' .backup-messages .messages').invoke('update', messageHtml);
  152. $$('#' + parentContainer + ' .backup-messages').invoke('show');
  153. },
  154. /**
  155. * @param {*} message
  156. * @return {String}
  157. */
  158. getErrorMessageHtml: function (message) {
  159. return '<div class="message message-error error"><div>' + message + '</div></div>';
  160. },
  161. /**
  162. * @return {*|jQuery}
  163. */
  164. getPostData: function () {
  165. var data = $('rollback-form').serialize(true);
  166. data.time = this.time;
  167. data.type = this.type;
  168. return data;
  169. },
  170. backupConfig: {
  171. 'backup-options': {
  172. title: jQuery.mage.__('Backup options'),
  173. /**
  174. * @return {String}
  175. */
  176. content: function () {
  177. return document.getElementById('backup-options-template').textContent;
  178. },
  179. /**
  180. * Action Ok.
  181. */
  182. actionOk: function () {
  183. this.modal.find('#backup-form').validation({
  184. submitHandler: jQuery.proxy(this.submitBackup, this)
  185. });
  186. this.modal.find('#backup-form').submit();
  187. }
  188. },
  189. 'rollback-warning': {
  190. title: jQuery.mage.__('Warning'),
  191. /**
  192. * @return {String}
  193. */
  194. content: function () {
  195. return document.getElementById('rollback-warning-template').textContent;
  196. },
  197. /**
  198. * Action Ok.
  199. */
  200. actionOk: function () {
  201. this.modal.modal('closeModal');
  202. this.requestPassword();
  203. }
  204. },
  205. 'rollback-request-password': {
  206. title: jQuery.mage.__('Backup options'),
  207. /**
  208. * @return {String}
  209. */
  210. content: function () {
  211. return document.getElementById('rollback-request-password-template').textContent;
  212. },
  213. /**
  214. * Action Ok.
  215. */
  216. actionOk: function () {
  217. this.modal.find('#rollback-form').validation({
  218. submitHandler: jQuery.proxy(this.submitRollback, this)
  219. });
  220. this.modal.find('#rollback-form').submit();
  221. },
  222. /**
  223. * Opened.
  224. */
  225. opened: function () {
  226. this.toggleFtpCredentialsForm();
  227. }
  228. }
  229. },
  230. /**
  231. * @param {*} divId
  232. */
  233. showPopup: function (divId) {
  234. var self = this;
  235. this.modal = jQuery('<div/>').attr({
  236. id: divId
  237. }).html(this.backupConfig[divId].content()).modal({
  238. modalClass: 'magento',
  239. title: this.backupConfig[divId].title,
  240. type: 'slide',
  241. /**
  242. * @param {juery.Event} e
  243. * @param {Object} modal
  244. */
  245. closed: function (e, modal) {
  246. modal.modal.remove();
  247. },
  248. /**
  249. * Opened.
  250. */
  251. opened: function () {
  252. if (self.backupConfig[divId].opened) {
  253. self.backupConfig[divId].opened.call(self);
  254. }
  255. },
  256. buttons: [{
  257. text: jQuery.mage.__('Cancel'),
  258. 'class': 'action cancel',
  259. /**
  260. * Click action.
  261. */
  262. click: function () {
  263. this.closeModal();
  264. }
  265. }, {
  266. text: jQuery.mage.__('Ok'),
  267. 'class': 'action primary',
  268. /**
  269. * Click action.
  270. */
  271. click: function () {
  272. self.backupConfig[divId].actionOk.call(self);
  273. }
  274. }]
  275. });
  276. this.modal.modal('openModal');
  277. },
  278. /**
  279. * Hide Popups.
  280. */
  281. hidePopups: function () {
  282. var mask;
  283. $$('.backup-dialog').each(Element.hide);
  284. mask = $('popup-window-mask');
  285. if (mask) {
  286. mask.hide();
  287. }
  288. }
  289. };
  290. });