{"id":966,"date":"2015-05-21T11:20:58","date_gmt":"2015-05-21T09:20:58","guid":{"rendered":"http:\/\/www.h-hennes.fr\/blog\/?p=966"},"modified":"2018-11-22T18:18:32","modified_gmt":"2018-11-22T16:18:32","slug":"prestashop-1-5-requete-ajax-dans-la-configuration-dun-module","status":"publish","type":"post","link":"https:\/\/www.h-hennes.fr\/blog\/2015\/05\/21\/prestashop-1-5-requete-ajax-dans-la-configuration-dun-module\/","title":{"rendered":"Prestashop 1.5 : Requ\u00eate ajax dans la configuration d&rsquo;un module"},"content":{"rendered":"<p>J&rsquo;ai perdu derni\u00e8rement pas mal de temps en tentant de mettre en place des appels ajax dans la configuration d&rsquo;un module <strong>Prestashop version 1.5.<\/strong><br \/>\nLes m\u00e9thodologies qui fonctionnent tr\u00e8s bien avec les derni\u00e8res version de prestashop ne fonctionnaient pas du tout.<\/p>\n<p>Apr\u00e8s analyse du code, il s&rsquo;av\u00e8re que la gestion des requ\u00eates ajax pour les modules prestashop telle que nous la connaissons n&rsquo;est pas impl\u00e9ment\u00e9e pour les versions inf\u00e9rieures \u00e0 la version 1.6.0.3<\/p>\n<p>Pour corriger cela, il est n\u00e9cessaire de surcharger la fonction <em>postProcess<\/em> de la classe <em>AdminController<\/em>.<\/p>\n<p>Pour faire cela proprement il faut donc cr\u00e9er un override dans le fichier :<br \/>\n<strong>override\/classes\/controller\/AdminController.php<\/strong><\/p>\n<p>avec le contenu suivant :<br \/>\n<em>( j&rsquo;ai tout simplement repris celui des derni\u00e8res version )<\/em><\/p>\n<pre lang=\"php\" escaped=\"true\">\r\n&lt;?php \r\nclass AdminController extends AdminControllerCore {\r\n\r\n    \/**\r\n     * @todo uses redirectAdmin only if !$this->ajax\r\n     *\/\r\n    public function postProcess() {\r\n        if ($this->ajax) {\r\n\r\n            \/\/ from ajax-tab.php\r\n            $action = Tools::getValue('action');\r\n            \/\/ no need to use displayConf() here\r\n            if (!empty($action) && method_exists($this, 'ajaxProcess' . Tools::toCamelCase($action)))\r\n                return $this->{'ajaxProcess' . Tools::toCamelCase($action)}();\r\n            \r\n            \/\/Rajout du code pour pouvoir g\u00e9rer les actions ajax des modules    \r\n            elseif (!empty($action) && $this->controller_name == 'AdminModules' && Tools::getIsset('configure')) {\r\n                $module_obj = Module::getInstanceByName(Tools::getValue('configure'));\r\n                if (Validate::isLoadedObject($module_obj) && method_exists($module_obj, 'ajaxProcess' . $action))\r\n                    return $module_obj->{'ajaxProcess' . $action}();\r\n            }\r\n            \/\/Fin Rajout du code pour pouvoir g\u00e9rer les actions ajax des modules  \r\n            \r\n            elseif (method_exists($this, 'ajaxProcess')) {\r\n                echo 'Appel de la finction ajax process des modules';\r\n                return $this->ajaxProcess();\r\n            }\r\n        } else {\r\n\r\n            \/\/ Process list filtering\r\n            if ($this->filter)\r\n                $this->processFilter();\r\n\r\n            \/\/ If the method named after the action exists, call \"before\" hooks, then call action method, then call \"after\" hooks\r\n            if (!empty($this->action) && method_exists($this, 'process' . ucfirst(Tools::toCamelCase($this->action)))) {\r\n                \/\/ Hook before action\r\n                Hook::exec('actionAdmin' . ucfirst($this->action) . 'Before', array('controller' => $this));\r\n                Hook::exec('action' . get_class($this) . ucfirst($this->action) . 'Before', array('controller' => $this));\r\n                \/\/ Call process\r\n                $return = $this->{'process' . Tools::toCamelCase($this->action)}();\r\n                \/\/ Hook After Action\r\n                Hook::exec('actionAdmin' . ucfirst($this->action) . 'After', array('controller' => $this, 'return' => $return));\r\n                Hook::exec('action' . get_class($this) . ucfirst($this->action) . 'After', array('controller' => $this, 'return' => $return));\r\n\r\n                return $return;\r\n            }\r\n        }\r\n    }\r\n\r\n}\r\n\r\n<\/pre>\n<p>Une fois l&rsquo;override en place vous pourrez effectuer des requ\u00eates ajax facilement dans l&rsquo;administration de votre module.<\/p>\n<p>Voici un code qui pourra vous servir de canevas :<\/p>\n<pre lang=\"php\" escaped=\"true\">&lt;?php \r\nif (!defined('_PS_VERSION_')) \texit; \r\nclass adminajax extends Module { \t\r\n        public function __construct() \t{ \t\t\r\n               $this-&gt;name = 'adminajax';\r\n\t\t$this-&gt;tab = 'others';\r\n\t\t$this-&gt;author = 'hhennes';\r\n\t\t$this-&gt;version = '0.1.0';\r\n\t\t$this-&gt;need_instance = 0;\r\n\r\n\t\tparent::__construct();\r\n\r\n\t\t$this-&gt;displayName = $this-&gt;l('Admin ajax');\r\n\t\t$this-&gt;description = $this-&gt;l('Sample module for admin ajax');\r\n\t}\r\n\r\n\tpublic function install()\r\n\t{\r\n\t\treturn parent::install();\r\n\t}\r\n\r\n\tpublic function uninstall()\r\n\t{\r\n\t\treturn parent::uninstall();\r\n\t}\r\n\r\n\t\/**\r\n\t * Configuration du module\r\n\t *\/\r\n\tpublic function getContent()\r\n\t{\r\n\r\n\t\t$output = 'Test admin ajax sur les diff\u00e9rentes version de Prestashop';\r\n\r\n\t\t$output .= '<script>\r\n         $(document).ready(function(){\r\n\t $.ajax({\r\n            url : \"'.$this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'\",    \r\n            data : {\r\n                ajax : true,\r\n                action : \"Test\",\r\n            },\r\n            success : function(msg){\r\n                alert(msg);\r\n            },\r\n            error : function(msg){\r\n                console.log(\"erreur\");\r\n                console.log(msg);\r\n            }\r\n         });\r\n        });\r\n        <\/script>';\r\n\t\t\r\n\t\treturn $output;\r\n\t}\r\n \/** \r\n * Actions Ajax du modules \r\n *\/ \r\n public function ajaxProcessTest()\r\n { \r\n echo 'Action ajax effectu\u00e9e proprement :-)'; \r\n } \r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>J&rsquo;ai perdu derni\u00e8rement pas mal de temps en tentant de mettre en place des appels ajax dans la configuration d&rsquo;un module Prestashop version 1.5. Les m\u00e9thodologies qui fonctionnent tr\u00e8s bien avec les derni\u00e8res version de prestashop ne fonctionnaient pas du tout. Apr\u00e8s analyse du code, il s&rsquo;av\u00e8re que la gestion des requ\u00eates ajax pour les [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[245],"tags":[348,347,349,288,104],"class_list":["post-966","post","type-post","status-publish","format-standard","hentry","category-prestashop-2","tag-admin","tag-ajax","tag-back-office","tag-module","tag-prestashop","prestashop-1-5","prestashop-1-7-3","prestashop-1-7-8","prestashop-8-1"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/comments?post=966"}],"version-history":[{"count":5,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/966\/revisions"}],"predecessor-version":[{"id":971,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/966\/revisions\/971"}],"wp:attachment":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/media?parent=966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/categories?post=966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/tags?post=966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}