Skip to content

Commit

Permalink
add bouton to edit page on listing (product / category and CMS)
Browse files Browse the repository at this point in the history
  • Loading branch information
PrestaSafe committed Jan 26, 2024
1 parent baf862f commit 7b6a624
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 3 deletions.
12 changes: 11 additions & 1 deletion config/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ admin_prettyblocks:
_controller: 'PrestaSafe\PrettyBlocks\Controller\AdminThemeManagerController::indexAction'
_legacy_controller: AdminThemeManagerController
_legacy_link: AdminThemeManagerController
# route generator (for opening custom page in prettyblocks)

prettyblocks_route_generator:
path: prettyblocks/route_generator/
methods: [GET,POST]
defaults:
_controller: 'PrestaSafe\PrettyBlocks\Controller\AdminThemeManagerController::routeGeneratorAction'
_legacy_controller: AdminThemeManagerControllerRouteGenerator
_legacy_link: AdminThemeManagerControllerRouteGenerator

prettyblocks_block_render:
path: prettyblocks/block/
Expand Down Expand Up @@ -88,4 +97,5 @@ prettyblocks_theme_settings:
defaults:
_controller: 'PrestaSafe\PrettyBlocks\Controller\AdminThemeManagerController::getSettingsAction'
_legacy_controller: AdminThemeManagerController
_legacy_link: AdminThemeManagerController
_legacy_link: AdminThemeManagerController

26 changes: 24 additions & 2 deletions prettyblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
use PrestaSafe\PrettyBlocks\Core\Components\Title;
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

if (!defined('_PS_VERSION_')) {
exit;
Expand Down Expand Up @@ -52,6 +54,7 @@ class PrettyBlocks extends Module implements WidgetInterface
'displayHeader',
'actionDispatcher',
'actionFrontControllerSetVariables',
'displayBackOfficeHeader',
];

public function __construct()
Expand Down Expand Up @@ -149,21 +152,40 @@ private function removeDb()
}

public function getContent()
{
return Tools::redirect($this->getPrettyBlocksUrl());
}

private function getPrettyBlocksUrl()
{
$domain = Tools::getShopDomainSsl(true);
$symfonyUrl = $domain . Link::getUrlSmarty([

return $domain . Link::getUrlSmarty([
'entity' => 'sf',
'route' => 'admin_prettyblocks',

]);


return Tools::redirect($symfonyUrl);
}

private function loadDefault()
{
return Configuration::updateGlobalValue('_PRETTYBLOCKS_TOKEN_', Tools::passwdGen(25));
}

public function hookdisplayBackOfficeHeader($params)
{
$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');
}


public function install()
{
return parent::install()
Expand Down
35 changes: 35 additions & 0 deletions src/Controller/AdminThemeManagerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,21 @@ public function indexAction()
// url to load at startup : provided url or shop home page
$startup_url = \Tools::getValue('startup_url', $shop_url);

if(\Tools::getValue('endpoint')){
switch(\Tools::getValue('endpoint'))
{
case 'product':
$startup_url = $link->getProductLink((int)\Tools::getValue('id'));
break;
case 'category':
$startup_url = $link->getCategoryLink((int)\Tools::getValue('id'));
break;
case 'cms':
$startup_url = $link->getCMSLink((int)\Tools::getValue('id'));
break;
}
}

return $this->render('@Modules/prettyblocks/views/templates/admin/index.html.twig', [
'css_back_custom' => $uri,
'base_url' => $link->getBaseLink(),
Expand Down Expand Up @@ -614,4 +629,24 @@ protected function getLangLink($idLang = null, \Context $context = null, $idShop

return \Language::getIsoById($idLang) . '/';
}


public function routeGeneratorAction(Request $request)
{
$posts = json_decode($request->getContent(), true);
$endpoint = $posts['endpoint'];
$id = (int)$posts['id'];

$router = $this->get('router');
$url = $router->generate('admin_prettyblocks', [
'endpoint' => $endpoint,
'id' => $id,
]);
return (new JsonResponse())->setData([
'url' => $url,
]);


}

}
64 changes: 64 additions & 0 deletions views/js/back.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

const addPrettyButton = (btnGroupClass = '.btn-group-action', endpoint, idEndpointTarget) => {
let btnGroups = document.querySelectorAll(btnGroupClass);
btnGroups.forEach(btnGroup => {
let id_cms = parseInt(btnGroup.closest('tr').querySelector(idEndpointTarget).textContent);

// Début de la sélection
let newElement = document.createElement('a');
newElement.innerHTML = "<img width='30' src='"+prettyblocks_logo+"' alt='edit in prettyblocks'>";
newElement.dataset.endpoint = endpoint;
newElement.dataset.idEndpoint = id_cms;
newElement.dataset.action = "open_in_prettyblocks";
// Fin de la sélection
btnGroup.prepend(newElement);
});
}

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

// CMS LIST Button
if (document.body.classList.contains('admincmscontent')) {
addPrettyButton('.btn-group-action', 'cms', '.column-id_cms');
}

// Product LIST Button
if (document.body.classList.contains('adminproducts')) {
addPrettyButton('.btn-group-action', 'product', '.column-id_product');
}
if (document.body.classList.contains('admincategories')) {
addPrettyButton('.btn-group-action', 'category', '.column-id_category');
}


// 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
});
});


})

0 comments on commit 7b6a624

Please sign in to comment.