orioltestart Posted August 8, 2016 Share Posted August 8, 2016 Hi everyone, How could i show a prompt to the user before the Hook is executed. The idea would be like: I wanna add a new Category which will add some more subcategories because of a module. I want that before you can add that new category, a modal or simply a prompt asks you if you are sure, and aware the user of what it will produce. There is an easy way to do that? I have to make a new Controller or i can do it with context variable? Thanks!! Link to comment Share on other sites More sharing options...
rocky Posted August 9, 2016 Share Posted August 9, 2016 This is difficult to do, since PHP doesn't let you display modal dialogues, only JavaScript does. I suggest that you use your hook to add a JavaScript function and then add a modal dialog when the document is ready. For example: function myFunction() { } $(document).ready(function() { if (confirm('Are you sure?')) myFunction(); }); Link to comment Share on other sites More sharing options...
orioltestart Posted August 9, 2016 Author Share Posted August 9, 2016 Well, that would work i guess. The problem is: For now I got all the algorithms which run after the hook triggers on the .php file on the top of the module being executed on the server, I dont have any front-view to put the JS and actually i don't know how to i add this. Maybe would be better if I make a controller? Prestashop should have some methods to create forms I guess. I saw the method renderForm() on controllers examples and I don't get how to use it or what will produce it. Sorry if it sounds stupid, its my first time developing with Prestashop and some things looks weird to me. Thanks! Link to comment Share on other sites More sharing options...
rocky Posted August 9, 2016 Share Posted August 9, 2016 PrestaShop does let you render forms using it's API, but it doesn't let you make modal dialog boxes. If the code must be done in PHP, you could use an AJAX call to run the controller after the user confirms. Link to comment Share on other sites More sharing options...
orioltestart Posted August 9, 2016 Author Share Posted August 9, 2016 Thanks for your help, actually the objective it's not the modal box, I just want the warning sign to the user. It can be made with modals or with wells, for me it doesn't matter. For example, when you try to delete a category and it appears a form on the top of the page asking you where you wanna put the products that belong on the target category. And if I press cancel the category doesnt get deleted. Something like that. Just trying to realize which is the best way to do that. Link to comment Share on other sites More sharing options...
rocky Posted August 9, 2016 Share Posted August 9, 2016 I see. I'm not exactly sure how to do that, since I've never done it before. I can see that the AdminCategoriesController.php does this with it's delete function. Perhaps you can figure out how it does it and copy it. Or you can try to do it your own way, maybe with a "confirmed" parameter and only execute the code then. For example, in the PHP file: if ((int)Tools::getValue('confirmed') == 1) { // Execute hook code } And in your TPL file: {if isset($smarty.get.confirmed) && $smarty.get.confirmed == 1} Display results of your hook here {else} Display confirmation box here {/if} Link to comment Share on other sites More sharing options...
orioltestart Posted August 10, 2016 Author Share Posted August 10, 2016 (edited) Well, finally I made a Controller with its own page and can handle the error signs with jquery. Now the problem becomes when I try to do an AJAX call. I've been reading for a while about how to do that and I literally copied the same steps and didnt work. I did the following. First, I've created a new file inside mymodule/ajax/ajax.php which just have to send "HELLO". <?php include_once(_PS_ROOT_DIR_.'/config/config.inc.php'); include_once(_PS_ROOT_DIR_.'/init.php'); echo "HELLO"; ?> Then I've created the javascript file which its the following $.ajax({ url: '{$ajax}categorias_ajax.php', type: 'GET', data: 'ajax=true' }) .done(function (data, textStatus, jqXHR) { console.log(data); }) .fail(function (jqXHR, textStatus, errorThrown) { console.log(errorThrown); }); Okay, nevermind, I solved the problem trying to explain it. I mark the topic as SOLVED. Thanks rocky!! Edited August 10, 2016 by orioltestart (see edit history) Link to comment Share on other sites More sharing options...
rocky Posted August 10, 2016 Share Posted August 10, 2016 You're welcome. I'm happy you found a solution. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now