Skip to content

Commit

Permalink
Merge pull request #247 from PrestaSafe/fix-btn-product-1.7
Browse files Browse the repository at this point in the history
Fix btn product 1.7 + fix upgrade 3.1.0
  • Loading branch information
PrestaSafe authored Aug 30, 2024
2 parents 1b5af0f + 80601cf commit 3938e3a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 109 deletions.
21 changes: 12 additions & 9 deletions prettyblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,19 +268,22 @@ private function loadDefault()
return Configuration::updateGlobalValue('_PRETTYBLOCKS_TOKEN_', Tools::passwdGen(25));
}

/**
* hook display BackOfficeHeader
*/
public function hookdisplayBackOfficeHeader($params)
{
$route = (new Link())->getAdminLink('AdminThemeManagerControllerRouteGenerator');
Media::addJsDef([
'prettyblocks_route_generator' => $route,
'prettyblocks_logo' => HelperBuilder::pathFormattedToUrl('$/modules/prettyblocks/logo.png'),
'ps_version' => _PS_VERSION_,
'ps17' => version_compare(_PS_VERSION_, '8.0.0', '<='),
'ps8' => version_compare(_PS_VERSION_, '8.0.0', '>='),
]);
if (version_compare(_PS_VERSION_, '8.0.0', '<')) {
$route = (new Link())->getAdminLink('AdminThemeManagerControllerRouteGenerator');
Media::addJsDef([
'prettyblocks_route_generator' => $route,
'prettyblocks_logo' => HelperBuilder::pathFormattedToUrl('$/modules/prettyblocks/logo.png'),
]);

$this->context->controller->addJS($this->_path . 'views/js/back.js');
}
$this->context->controller->addCSS($this->_path . 'views/css/back.css');

// $this->context->controller->addJS($this->_path . 'views/js/back.js');
}

public function install()
Expand Down
2 changes: 1 addition & 1 deletion upgrade/upgrade-3.1.0.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
function upgrade_module_3_1_0($module)
{
$module->registerHook('ActionRegisterBlock');
$module->registerHook($module->hooks);

return true;
}
159 changes: 60 additions & 99 deletions views/js/back.js
Original file line number Diff line number Diff line change
@@ -1,99 +1,60 @@
// const addPrettyButton = (btnGroupClass = '.btn-group-action', endpoint, idEndpointTarget) => {
// let btnGroups = document.querySelectorAll(btnGroupClass);
// btnGroups.forEach(btnGroup => {
// let idTarget = 0;

// // Si idEndpointTarget est une fonction, on l'exécute pour obtenir l'idTarget
// if (typeof idEndpointTarget === 'function') {
// idTarget = idEndpointTarget(btnGroup);
// } else {
// // Sinon, on garde la logique existante
// if (ps8) {
// idTarget = parseInt(btnGroup.closest('tr').querySelector(idEndpointTarget).textContent);
// }
// if (ps17) {
// idTarget = parseInt(btnGroup.closest('tr').getAttribute(idEndpointTarget));
// }
// }

// let newElement = document.createElement('a');
// newElement.innerHTML = "<img style='float: right; padding-top: 6px;' width='30' src='"+prettyblocks_logo+"' alt='edit in prettyblocks'>";
// newElement.dataset.endpoint = endpoint;
// newElement.dataset.idEndpoint = idTarget;
// newElement.dataset.action = "open_in_prettyblocks";

// btnGroup.prepend(newElement);
// });
// }



// document.addEventListener('DOMContentLoaded', function() {
// // Exemples d'utilisation

// // CMS LIST Button
// if (document.body.classList.contains('admincmscontent')) {
// if(ps8){
// addPrettyButton('.btn-group-action', 'cms', '.column-id_cms');
// }
// if(ps17){
// addPrettyButton('.btn-group-action', 'cms', (btnGroup) => {
// if (btnGroup.closest('tr').querySelector('.column-id_cms') !== null) {
// let id_cms = btnGroup.closest('tr').querySelector('.column-id_cms').textContent;
// return parseInt(id_cms);
// }

// });

// }
// }

// // Product LIST Button
// if (document.body.classList.contains('adminproducts')) {

// if(ps8){
// addPrettyButton('.btn-group-action', 'product', '.column-id_product');
// }
// if(ps17){
// addPrettyButton('.btn-group-action', 'product', '[data-product-id]');
// }
// }
// // categories
// if (document.body.classList.contains('admincategories')) {
// addPrettyButton('.btn-group-action', 'category', (btnGroup) => {
// return parseInt(btnGroup.closest('tr').querySelector('.column-id_category').textContent);
// });
// }


// // Début de la sélection
// let prettyBlocksButtons = document.querySelectorAll('[data-action="open_in_prettyblocks"]');
// // Fin de la sélection
// prettyBlocksButtons.forEach(button => {
// button.addEventListener('click', function(event) {
// event.preventDefault();
// // Ajoutez ici le code à exécuter lors du clic sur le bouton
// let endpoint = button.dataset.endpoint;
// let idEndpoint = button.dataset.idEndpoint;
// let action = button.dataset.action;
// let url = prettyblocks_route_generator;

// // Début de la sélection
// fetch(url, {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify({
// endpoint: endpoint,
// id: idEndpoint,
// }),
// })
// .then(response => response.json())
// .then(data => window.open(data.url, '_self'));
// // Fin de la sélection
// });
// });


// })
const addPrettyButton = (btnGroupClass = '.btn-group-action', endpoint, idEndpointTarget) => {
let btnGroups = document.querySelectorAll(btnGroupClass);
btnGroups.forEach(btnGroup => {
let idTarget = 0;
idTarget = parseInt(btnGroup.closest('tr').dataset.productId);
let newElement = document.createElement('a');
newElement.target = '_blank';
newElement.innerHTML = "<img width='30' src='" + prettyblocks_logo + "' alt='edit in prettyblocks'>";
newElement.dataset.endpoint = endpoint;
newElement.dataset.idEndpoint = idTarget;
newElement.dataset.action = "open_in_prettyblocks";


let btnGroupInner = btnGroup.querySelector('.btn-group');
if (btnGroupInner) {
btnGroupInner.appendChild(newElement);
} else {
btnGroup.appendChild(newElement);
}
});
}



document.addEventListener('DOMContentLoaded', function () {

if (document.body.classList.contains('adminproducts')) {
addPrettyButton('.btn-group-action', 'product', '[data-product-id]');
}


let prettyBlocksButtons = document.querySelectorAll('[data-action="open_in_prettyblocks"]');

prettyBlocksButtons.forEach(button => {
button.addEventListener('click', function (event) {
event.preventDefault();

let endpoint = button.dataset.endpoint;
let idEndpoint = button.dataset.idEndpoint;
let action = button.dataset.action;
let url = prettyblocks_route_generator;


fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
endpoint: endpoint,
id: idEndpoint,
}),
})
.then(response => response.json())
.then(data => window.open(data.url, '_self'));
});
});


})

0 comments on commit 3938e3a

Please sign in to comment.