Skip to content

Commit

Permalink
Merge pull request PrestaShop#8378 from Quetzacoalt91/feat-prestatrust
Browse files Browse the repository at this point in the history
Integration of PrestaTrust in module management
  • Loading branch information
toutantic authored Nov 6, 2017
2 parents 98da2d7 + 5395295 commit 637323a
Show file tree
Hide file tree
Showing 33 changed files with 1,537 additions and 333 deletions.
105 changes: 86 additions & 19 deletions admin-dev/themes/default/js/bundle/module/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ var AdminModuleController = function() {
this.addonsLogoutModalBtnSelector = '#page-header-desc-configuration-addons_logout';
this.addonsImportModalBtnSelector = '#page-header-desc-configuration-add_module';
this.dropZoneModalSelector = '#module-modal-import';
this.dropZoneModalFooterSelector = '#module-modal-import .modal-footer';
this.dropZoneImportZoneSelector = '#importDropzone';
this.addonsConnectModalSelector = '#module-modal-addons-connect';
this.addonsLogoutModalSelector = '#module-modal-addons-logout';
Expand All @@ -91,6 +92,7 @@ var AdminModuleController = function() {
this.moduleImportFailureDetailsBtnSelector = '.module-import-failure-details-action';
this.moduleImportSelectFileManualSelector = '.module-import-start-select-manual';
this.moduleImportFailureMsgDetailsSelector = '.module-import-failure-details';
this.moduleImportConfirmSelector = '.module-import-confirm';

/**
* Initialize all listners and bind everything
Expand Down Expand Up @@ -461,6 +463,8 @@ var AdminModuleController = function() {
dropzone.removeAttr('style');
$(self.moduleImportFailureMsgDetailsSelector).hide();
$(self.moduleImportSuccessConfigureBtnSelector).hide();
$(self.dropZoneModalFooterSelector).html('');
$(self.moduleImportConfirmSelector).hide();
});

// Change the way Dropzone.js lib handle file input trigger
Expand Down Expand Up @@ -515,46 +519,109 @@ var AdminModuleController = function() {
dictDefaultMessage: '',
hiddenInputContainer: self.dropZoneImportZoneSelector,
addedfile: function() {
// State that we start module upload
self.isUploadStarted = true;
$(self.moduleImportStartSelector).hide(0);
dropzone.css('border', 'none');
$(self.moduleImportProcessingSelector).fadeIn();
self.animateStartUpload();
},
processing: function () {
// Leave it empty since we don't require anything while processing upload
},
error: function (file, message) {
$(self.moduleImportProcessingSelector).finish().fadeOut(function() {
$(self.moduleImportFailureMsgDetailsSelector).html(message);
$(self.moduleImportFailureSelector).fadeIn();
});
self.displayOnUploadError(message);
},
complete: function (file) {
if (file.status !== 'error') {
var responseObject = jQuery.parseJSON(file.xhr.response);
if (typeof responseObject.is_configurable === 'undefined') responseObject.is_configurable = null;
if (typeof responseObject.module_name === 'undefined') responseObject.module_name = null;

$(self.moduleImportProcessingSelector).finish().fadeOut(function() {
if (responseObject.status === true) {
if (responseObject.is_configurable === true) {
var configureLink = self.baseAdminDir + 'module/manage/action/configure/' + responseObject.module_name + window.location.search;
self.displayOnUploadDone(responseObject);
}
// State that we have finish the process to unlock some actions
self.isUploadStarted = false;
}
};
dropzone.dropzone($.extend(dropzoneOptions));

this.animateStartUpload = function() {
// State that we start module upload
self.isUploadStarted = true;
$(self.moduleImportStartSelector).hide(0);
dropzone.css('border', 'none');
$(self.moduleImportProcessingSelector).fadeIn();
};

this.animateEndUpload = function(callback) {
$(self.moduleImportProcessingSelector).finish().fadeOut(callback);
};

/**
* Method to call for upload modal, when the ajax call went well.
*
* @param object result containing the server response
*/
this.displayOnUploadDone = function(result) {
var self = this;
self.animateEndUpload(function() {
if (result.status === true) {
if (result.is_configurable === true) {
var configureLink = self.baseAdminDir + 'module/manage/action/configure/' + result.module_name + window.location.search;
$(self.moduleImportSuccessConfigureBtnSelector).attr('href', configureLink);
$(self.moduleImportSuccessConfigureBtnSelector).show();
}
$(self.moduleImportSuccessSelector).fadeIn();
} else if (typeof result.confirmation_subject !== 'undefined') {
self.displayPrestaTrustStep(result);
} else {
$(self.moduleImportFailureMsgDetailsSelector).html(responseObject.msg);
$(self.moduleImportFailureMsgDetailsSelector).html(result.msg);
$(self.moduleImportFailureSelector).fadeIn();
}
});
}
// State that we have finish the process to unlock some actions
self.isUploadStarted = false;
}
};
dropzone.dropzone($.extend(dropzoneOptions));

/**
* Method to call for upload modal, when the ajax call went wrong or when the action requested could not
* succeed for some reason.
*
* @param string message explaining the error.
*/
this.displayOnUploadError = function(message) {
self.animateEndUpload(function() {
$(self.moduleImportFailureMsgDetailsSelector).html(message);
$(self.moduleImportFailureSelector).fadeIn();
});
};

/**
* If PrestaTrust needs to be confirmed, we ask for the confirmation modal content and we display it in the
* currently displayed one. We also generate the ajax call to trigger once we confirm we want to install
* the module.
*
* @param Previous server response result
*/
this.displayPrestaTrustStep = function (result) {
var self = this;
var modal = module_card_controller.replacePrestaTrustPlaceholders(result);
var moduleName = result.module.attributes.name;
$(this.moduleImportConfirmSelector).html(modal.find('.modal-body').html()).fadeIn();
$(this.dropZoneModalFooterSelector).html(modal.find('.modal-footer').html()).fadeIn();
$(this.dropZoneModalFooterSelector).find(".pstrust-install").off('click').on('click', function() {

$(self.moduleImportConfirmSelector).hide();
$(self.dropZoneModalFooterSelector).html('');
self.animateStartUpload();

// Install ajax call
$.post(result.module.attributes.urls.install, { 'actionParams[confirmPrestaTrust]': "1"})
.done(function(data) {
self.displayOnUploadDone(data[moduleName]);
})
.fail(function(data) {
self.displayOnUploadError(data[moduleName]);
})
.always(function() {
self.isUploadStarted = false;
});
});
};
};

this.getBulkCheckboxesSelector = function () {
Expand Down
62 changes: 61 additions & 1 deletion admin-dev/themes/default/js/bundle/module/module_card.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,58 @@ var AdminModuleCard = function () {
return false; // do not allow a.href to reload the page. The confirm modal dialog will do it async if needed.
};

/**
* Update the content of a modal asking a confirmation for PrestaTrust and open it
*
* @param {array} result containing module data
* @return {void}
*/
this.confirmPrestaTrust = function confirmPrestaTrust(result) {
var that = this;
var modal = this.replacePrestaTrustPlaceholders(result);
modal.find(".pstrust-install").off('click').on('click', function() {
// Find related form, update it and submit it
var install_button = $(that.moduleActionMenuInstallLinkSelector, '.module-item[data-tech-name="' + result.module.attributes.name + '"]');
var form = install_button.parent("form");
$('<input>').attr({
type: 'hidden',
value: '1',
name: 'actionParams[confirmPrestaTrust]'
}).appendTo(form);
install_button.click();
modal.modal('hide');
});
modal.modal();
};

this.replacePrestaTrustPlaceholders = function replacePrestaTrustPlaceholders(result) {
var modal = $("#modal-prestatrust");
var module = result.module.attributes;
if (result.confirmation_subject !== 'PrestaTrust' || !modal.length) {
return;
}

var alertClass = module.prestatrust.status ? 'success' : 'warning';

if (module.prestatrust.check_list.property) {
modal.find("#pstrust-btn-property-ok").show();
modal.find("#pstrust-btn-property-nok").hide();
} else {
modal.find("#pstrust-btn-property-ok").hide();
modal.find("#pstrust-btn-property-nok").show();
modal.find("#pstrust-buy").attr("href", module.url).toggle(module.url !== null);
}

modal.find("#pstrust-img").attr({src: module.img, alt: module.name});
modal.find("#pstrust-name").text(module.displayName);
modal.find("#pstrust-author").text(module.author);
modal.find("#pstrust-label").attr("class", "text-" + alertClass).text(module.prestatrust.status ? 'OK' : 'KO');
modal.find("#pstrust-message").attr("class", "alert alert-"+alertClass);
modal.find("#pstrust-message > p").text(module.prestatrust.message);

return modal;
}

this.dispatchPreEvent = function (action, element) {
var event = jQuery.Event('module_card_action_event');
$(element).trigger(event, [action]);
Expand All @@ -79,6 +131,9 @@ var AdminModuleCard = function () {
});

$(document).on('click', this.moduleActionMenuInstallLinkSelector, function () {
if ($("#modal-prestatrust").length) {
$("#modal-prestatrust").modal('hide');
}
return _this.dispatchPreEvent('install', this) && _this.confirmAction('install', this) && _this.requestToController('install', $(this));
});
$(document).on('click', this.moduleActionMenuEnableLinkSelector, function () {
Expand Down Expand Up @@ -120,15 +175,17 @@ var AdminModuleCard = function () {
var form = element.closest("form");
var spinnerObj = $("<button class=\"btn-primary-reverse onclick unbind spinner \"></button>");
var url = "//" + window.location.host + form.attr("action");
var actionParams = form.serializeArray();

if (forceDeletion === "true" || forceDeletion === true) {
url +="&deletion=true";
actionParams.push({name: "actionParams[deletion]", value: true});
}

$.ajax({
url: url,
dataType: 'json',
method: 'POST',
data: actionParams,
beforeSend: function () {
jqElementObj.hide();
jqElementObj.after(spinnerObj);
Expand All @@ -139,6 +196,9 @@ var AdminModuleCard = function () {
} else {
var moduleTechName = Object.keys(result)[0];
if (result[moduleTechName].status === false) {
if (typeof result[moduleTechName].confirmation_subject !== 'undefined') {
_this.confirmPrestaTrust(result[moduleTechName]);
}
$.growl.error({message: result[moduleTechName].msg});
} else {
$.growl.notice({message: result[moduleTechName].msg});
Expand Down
9 changes: 9 additions & 0 deletions admin-dev/themes/new-theme/scss/pages/_modules.scss
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,12 @@ form#importDropzone {
.module-import-success-configure {
display: none;
}

/**
* PrestaTrust modal
**/

#pstrust-img {
max-height: 64px;
max-width: 64px;
}
2 changes: 2 additions & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ csa_guzzle:

prestashop:
addons:
prestatrust:
enabled: true
api_client:
ttl: 7200 # 2h
# verify_ssl: ~ # Bundle CA by default, declaring "addons.api_client.verify_ssl" parameter overrides its usage
Loading

0 comments on commit 637323a

Please sign in to comment.