private function ModuleHookExec($moduleName, $hook_name) { $output = ''; $moduleInstance = Module::getInstanceByName($moduleName); if (Validate::isLoadedObject($moduleInstance) && $moduleInstance->id) { $altern = 0; $id_hook = Hook::getIdByName($hook_name); $retro_hook_name = Hook::getRetroHookName($hook_name); $disable_non_native_modules = (bool) Configuration::get('PS_DISABLE_NON_NATIVE_MODULE'); if ($disable_non_native_modules && Hook::$native_module && count(Hook::$native_module) && !in_array($moduleInstance->name, self::$native_module)) { return ''; } //check disable module $device = (int) $this->context->getDevice(); if (Db::getInstance()->getValue(' SELECT COUNT(`id_module`) FROM ' . _DB_PREFIX_ . 'module_shop WHERE enable_device & ' . (int) $device . ' AND id_module=' . (int) $moduleInstance->id . Shop::addSqlRestriction()) == 0) { return ''; } // Check permissions $exceptions = $moduleInstance->getExceptions($id_hook); $controller = Dispatcher::getInstance()->getController(); $controller_obj = Context::getContext()->controller; //check if current controller is a module controller if (isset($controller_obj->module) && Validate::isLoadedObject($controller_obj->module)) { $controller = 'module-' . $controller_obj->module->name . '-' . $controller; } if (in_array($controller, $exceptions)) { return ''; } //retro compat of controller names $matching_name = array('authentication' => 'auth', 'productscomparison' => 'compare'); if (isset($matching_name[$controller]) && in_array($matching_name[$controller], $exceptions)) { return ''; } if (Validate::isLoadedObject($this->context->employee) && !$moduleInstance->getPermission('view', $this->context->employee)) { return ''; } if (!isset($hook_args['cookie']) or !$hook_args['cookie']) { $hook_args['cookie'] = $this->context->cookie; } if (!isset($hook_args['cart']) or !$hook_args['cart']) { $hook_args['cart'] = $this->context->cart; } $hook_callable = is_callable(array($moduleInstance, 'hook' . $hook_name)); $hook_retro_callable = is_callable(array($moduleInstance, 'hook' . $retro_hook_name)); if (($hook_callable || $hook_retro_callable) && Module::preCall($moduleInstance->name)) { $hook_args['altern'] = ++$altern; // Call hook method if ($hook_callable) { $display = $moduleInstance->{'hook' . $hook_name}($hook_args); } elseif ($hook_retro_callable) { $display = $moduleInstance->{'hook' . $retro_hook_name}($hook_args); } $output .= $display; } } return $output; }
/** * Check if the module is transplantable on the hook in parameter * @param string $hook_name * @return bool if module can be transplanted on hook */ public function isHookableOn($hook_name) { $retro_hook_name = Hook::getRetroHookName($hook_name); return is_callable(array($this, 'hook' . ucfirst($hook_name))) || is_callable(array($this, 'hook' . ucfirst($retro_hook_name))); }
public static function registerDesignerHook($hook_name) { // Check hook name validation and if module is installed if (!Validate::isHookName($hook_name)) { throw new PrestaShopException('Invalid hook name'); } // Retrocompatibility $hook_name_bak = $hook_name; if ($alias = Hook::getRetroHookName($hook_name)) { $hook_name = $alias; } // Get hook id $id_hook = Hook::getIdByName($hook_name); $live_edit = Hook::getLiveEditById((int) Hook::getIdByName($hook_name_bak)); // If hook does not exist, we create it if (!$id_hook) { $new_hook = new Hook(); $new_hook->name = pSQL($hook_name); $new_hook->title = pSQL($hook_name); $new_hook->live_edit = pSQL($live_edit); $new_hook->add(); $id_hook = $new_hook->id; if (!$id_hook) { return false; } } return $id_hook; }
public function getModuleAssign($module_name = '', $hook_name = '') { $module = Module::getInstanceByName($module_name); if (_PS_VERSION_ <= "1.5") { $id_hook = Hook::get($hook_name); } else { $id_hook = Hook::getIdByName($hook_name); } if (Validate::isLoadedObject($module) && $module->id) { if (!isset($hookArgs['cookie']) or !$hookArgs['cookie']) { $hookArgs['cookie'] = $this->context->cookie; } if (!isset($hookArgs['cart']) or !$hookArgs['cart']) { $hookArgs['cart'] = $this->context->cart; } if (_PS_VERSION_ < "1.5") { //return self::lofHookExec( $hook_name, array(), $module->id, $array ); $hook_name = strtolower($hook_name); if (!Validate::isHookName($hook_name)) { die(Tools::displayError()); } $altern = 0; if (is_callable(array($module, 'hook' . $hook_name))) { $hookArgs['altern'] = ++$altern; $output = call_user_func(array($module, 'hook' . $hook_name), $hookArgs); } return $output; } else { $hook_name = substr($hook_name, 7, strlen($hook_name)); if (!Validate::isHookName($hook_name)) { die(Tools::displayError()); } $retro_hook_name = Hook::getRetroHookName($hook_name); $hook_callable = is_callable(array($module, 'hook' . $hook_name)); $hook_retro_callable = is_callable(array($module, 'hook' . $retro_hook_name)); $output = ''; if (($hook_callable || $hook_retro_callable) && Module::preCall($module->name)) { if ($hook_callable) { $output = $module->{'hook' . $hook_name}($hookArgs); } else { if ($hook_retro_callable) { $output = $module->{'hook' . $retro_hook_name}($hookArgs); } } } return $output; } } return ''; }
/** * Process posting data */ public function postProcess() { if (Tools::getValue('action') && Tools::getValue('action') == 'modulehook') { $id = (int) Tools::getValue('id'); $tmp_instance = Module::getInstanceById($id); $hooks = array(); foreach ($this->hookspos as $hk) { $retro_hook_name = Hook::getRetroHookName($hk); $hook_callable = is_callable(array($tmp_instance, 'hook' . $hk)); $hook_retro_callable = is_callable(array($tmp_instance, 'hook' . $retro_hook_name)); if ($hook_retro_callable || $hook_callable) { $hooks[] = $hk; // break; } } $hooks = implode("|", $hooks); $sql = 'SELECT * FROM `' . _DB_PREFIX_ . 'leohook` WHERE id_module=' . $id . ' AND theme="' . $this->theme_name . '" AND id_shop=' . (int) $this->context->shop->id; if ($row = Db::getInstance()->getRow($sql)) { die('{"hasError" : false, "hook" : "' . $row['name_hook'] . '","hooks":"' . $hooks . '"}'); } else { die('{"hasError" : true, "errors" : "Can not update module position","hooks":"' . $hooks . '"}'); } } if (Tools::getValue('action') && Tools::getValue('action') == 'overridehook') { $id_module = (int) Tools::getValue('hdidmodule'); $name_hook = Tools::getValue('name_hook'); if (is_numeric($name_hook)) { $sql = 'DELETE FROM`' . _DB_PREFIX_ . 'leohook` WHERE id_module=' . $id_module . ' AND theme="' . $this->theme_name . '" AND id_shop=' . (int) $this->context->shop->id; Db::getInstance(_PS_USE_SQL_SLAVE_)->execute($sql); die('{"hasError" : false, "errors" : done!delete module position"}'); } elseif ($name_hook) { $desHook = Tools::getValue("deshook"); $desHookId = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue("SELECT id_hook FROM `" . _DB_PREFIX_ . "hook` WHERE name='" . $desHook . "'"); $sql = 'SELECT * FROM `' . _DB_PREFIX_ . 'leohook` WHERE id_hook= ' . $desHookId . ' AND id_module=' . $id_module . ' AND theme="' . $this->theme_name . '" AND id_shop=' . (int) $this->context->shop->id; if ($row = Db::getInstance()->getRow($sql)) { $sql = ' UPDATE `' . _DB_PREFIX_ . 'leohook` SET `id_hook`=' . $desHookId . ', name_hook="' . $name_hook . '" WHERE id_module=' . $id_module . ' AND theme="' . $this->theme_name . '" AND id_shop=' . (int) $this->context->shop->id; Db::getInstance(_PS_USE_SQL_SLAVE_)->execute($sql); } else { $sql = ' INSERT INTO `' . _DB_PREFIX_ . 'leohook` (id_hook, id_module,id_shop,theme, name_hook) VALUES(' . $desHookId . ',' . $id_module . ',' . (int) $this->context->shop->id . ',"' . $this->theme_name . '","' . $name_hook . '") '; Db::getInstance(_PS_USE_SQL_SLAVE_)->execute($sql); } die('{"hasError" : false, "errors" : done!update module position"}'); } die('{"hasError" : true, "errors" : "Can not update module position"}'); } if (Tools::getValue('action') && Tools::getValue('action') == 'savepos') { $positions = Tools::getValue('position'); $way = (int) Tools::getValue('way'); $unhook = Tools::getValue('unhook'); $id_shop = Context::getContext()->shop->id; if (is_array($unhook)) { foreach ($unhook as $id_module => $hookId) { $module = Module::getInstanceById($id_module); if (Validate::isLoadedObject($module)) { !$module->unregisterHook((int) $hookId, array($id_shop)); } } } if (is_array($positions) && !empty($positions)) { foreach ($positions as $pos) { $tmp = explode("|", $pos); if (count($tmp) == 2 && $tmp[0] && $tmp[1]) { $position = $tmp[0]; $hookId = Hook::getIdByName($position); $oldhooks = explode(",", Tools::getValue($position)); $ids = explode(",", $tmp[1]); if ($hookId && count($oldhooks)) { foreach ($ids as $index => $id_module) { $module = Module::getInstanceById($id_module); if (Validate::isLoadedObject($module) && isset($oldhooks[$index]) && is_numeric($oldhooks[$index]) && $oldhooks[$index] != $hookId) { Db::getInstance(_PS_USE_SQL_SLAVE_)->execute(' UPDATE `' . _DB_PREFIX_ . 'hook_module` SET id_hook=' . $hookId . ' WHERE id_module=' . $id_module . ' AND id_hook=' . (int) $oldhooks[$index] . ' AND id_shop=' . (int) $id_shop); // echo '<pre>'.print_r( $idshooks ,1); echo $oldhooks[$index] . '<br>'; echo '<br>update:' . $id_module . "-"; } elseif (Validate::isLoadedObject($module) && (!isset($oldhooks[$index]) || !(int) $oldhooks[$index])) { $this->registerHook($id_module, $hookId, array($id_shop)); echo 'new:' . $id_module; } $module->updatePosition($hookId, $way, $index + 1); } } // echo '<pre>'.print_r( $hookId, 1 ); die; } } } // echo '<pre>'.print_r( $position, 1 ); die; die("done done"); } }
public static function renderModuleByHookV15($hook_name, $hookArgs = array(), $id_module = NULL, $array = array()) { global $cart, $cookie; if (!$hook_name || !$id_module) { return; } if (!empty($id_module) and !Validate::isUnsignedId($id_module) or !Validate::isHookName($hook_name)) { die(Tools::displayError()); } if (!isset($hookArgs['cookie']) or !$hookArgs['cookie']) { $hookArgs['cookie'] = $cookie; } if (!isset($hookArgs['cart']) or !$hookArgs['cart']) { $hookArgs['cart'] = $cart; } if ($id_module and $id_module != $array['id_module']) { return; } if (!($moduleInstance = Module::getInstanceByName($array['module']))) { return; } $retro_hook_name = Hook::getRetroHookName($hook_name); $hook_callable = is_callable(array($moduleInstance, 'hook' . $hook_name)); $hook_retro_callable = is_callable(array($moduleInstance, 'hook' . $retro_hook_name)); $output = ''; if (($hook_callable || $hook_retro_callable) && Module::preCall($moduleInstance->name)) { if ($hook_callable) { $output = $moduleInstance->{'hook' . $hook_name}($hookArgs); } else { if ($hook_retro_callable) { $output = $moduleInstance->{'hook' . $retro_hook_name}($hookArgs); } } } return $output; }
/** * Execute modules for specified hook * * @param string $hook_name Hook Name * @param array $hook_args Parameters for the functions * @param int $id_module Execute hook for this module only * @return string modules output */ public static function exec($hook_name, $hook_args = array(), $id_module = null) { // Check arguments validity if ($id_module && !is_numeric($id_module) || !Validate::isHookName($hook_name)) { throw new PrestaShopException('Invalid id_module or hook_name'); } // If no modules associated to hook_name or recompatible hook name, we stop the function if (!($module_list = Hook::getHookModuleExecList($hook_name))) { return ''; } // Check if hook exists if (!($id_hook = Hook::getIdByName($hook_name))) { return false; } // Store list of executed hooks on this page Hook::$executed_hooks[$id_hook] = $hook_name; $live_edit = false; $context = Context::getContext(); if (!isset($hook_args['cookie']) || !$hook_args['cookie']) { $hook_args['cookie'] = $context->cookie; } if (!isset($hook_args['cart']) || !$hook_args['cart']) { $hook_args['cart'] = $context->cart; } $retro_hook_name = Hook::getRetroHookName($hook_name); // Look on modules list $altern = 0; $output = ''; foreach ($module_list as $array) { // Check errors if ($id_module && $id_module != $array['id_module']) { continue; } if (!($moduleInstance = Module::getInstanceByName($array['module']))) { continue; } // Check permissions $exceptions = $moduleInstance->getExceptions($array['id_hook']); if (in_array(Dispatcher::getInstance()->getController(), $exceptions)) { continue; } if (Validate::isLoadedObject($context->employee) && !$moduleInstance->getPermission('view', $context->employee)) { continue; } // Check which / if method is callable $hook_callable = is_callable(array($moduleInstance, 'hook' . $hook_name)); $hook_retro_callable = is_callable(array($moduleInstance, 'hook' . $retro_hook_name)); if (($hook_callable || $hook_retro_callable) && Module::preCall($moduleInstance->name)) { $hook_args['altern'] = ++$altern; // Call hook method if ($hook_callable) { $display = $moduleInstance->{'hook' . $hook_name}($hook_args); } else { if ($hook_retro_callable) { $display = $moduleInstance->{'hook' . $retro_hook_name}($hook_args); } } // Live edit if ($array['live_edit'] && Tools::isSubmit('live_edit') && Tools::getValue('ad') && Tools::getValue('liveToken') == Tools::getAdminToken('AdminModulesPositions' . (int) Tab::getIdFromClassName('AdminModulesPositions') . (int) Tools::getValue('id_employee'))) { $live_edit = true; $output .= self::wrapLiveEdit($display, $moduleInstance, $array['id_hook']); } else { $output .= $display; } } } // Return html string return ($live_edit ? '<script type="text/javascript">hooks_list.push(\'' . $hook_name . '\'); </script> <div id="' . $hook_name . '" class="dndHook" style="min-height:50px">' : '') . $output . ($live_edit ? '</div>' : ''); }
/** * Execute modules for specified hook * * @param string $hook_name Hook Name * @param array $hook_args Parameters for the functions * @param int $id_module Execute hook for this module only * @param bool $array_return If specified, module output will be set by name in an array * @param bool $check_exceptions Check permission exceptions * @param bool $use_push Force change to be refreshed on Dashboard widgets * @param int $id_shop If specified, hook will be execute the shop with this ID * * @throws PrestaShopException * * @return string/array modules output */ public static function exec($hook_name, $hook_args = array(), $id_module = null, $array_return = false, $check_exceptions = true, $use_push = false, $id_shop = null) { if (defined('PS_INSTALLATION_IN_PROGRESS')) { return; } static $disable_non_native_modules = null; if ($disable_non_native_modules === null) { $disable_non_native_modules = (bool) Configuration::get('PS_DISABLE_NON_NATIVE_MODULE'); } // Check arguments validity if ($id_module && !is_numeric($id_module) || !Validate::isHookName($hook_name)) { throw new PrestaShopException('Invalid id_module or hook_name'); } // If no modules associated to hook_name or recompatible hook name, we stop the function if (!($module_list = Hook::getHookModuleExecList($hook_name))) { return ''; } // Check if hook exists if (!($id_hook = Hook::getIdByName($hook_name))) { return false; } if (array_key_exists($hook_name, self::$deprecated_hooks)) { $deprecVersion = isset(self::$deprecated_hooks[$hook_name]['from']) ? self::$deprecated_hooks[$hook_name]['from'] : _PS_VERSION_; Tools::displayAsDeprecated('The hook ' . $hook_name . ' is deprecated in PrestaShop v.' . $deprecVersion); } // Store list of executed hooks on this page Hook::$executed_hooks[$id_hook] = $hook_name; $context = Context::getContext(); if (!isset($hook_args['cookie']) || !$hook_args['cookie']) { $hook_args['cookie'] = $context->cookie; } if (!isset($hook_args['cart']) || !$hook_args['cart']) { $hook_args['cart'] = $context->cart; } $retro_hook_name = Hook::getRetroHookName($hook_name); // Look on modules list $altern = 0; if ($array_return) { $output = array(); } else { $output = ''; } if ($disable_non_native_modules && !isset(Hook::$native_module)) { Hook::$native_module = Module::getNativeModuleList(); } $different_shop = false; if ($id_shop !== null && Validate::isUnsignedId($id_shop) && $id_shop != $context->shop->getContextShopID()) { $old_context = $context->shop->getContext(); $old_shop = clone $context->shop; $shop = new Shop((int) $id_shop); if (Validate::isLoadedObject($shop)) { $context->shop = $shop; $context->shop->setContext(Shop::CONTEXT_SHOP, $shop->id); $different_shop = true; } } foreach ($module_list as $array) { // Check errors if ($id_module && $id_module != $array['id_module']) { continue; } if ((bool) $disable_non_native_modules && Hook::$native_module && count(Hook::$native_module) && !in_array($array['module'], Hook::$native_module)) { continue; } // Check permissions if ($check_exceptions) { $exceptions = Module::getExceptionsStatic($array['id_module'], $array['id_hook']); $controller = Dispatcher::getInstance()->getController(); $controller_obj = Context::getContext()->controller; //check if current controller is a module controller if (isset($controller_obj->module) && Validate::isLoadedObject($controller_obj->module)) { $controller = 'module-' . $controller_obj->module->name . '-' . $controller; } if (in_array($controller, $exceptions)) { continue; } //Backward compatibility of controller names $matching_name = array('authentication' => 'auth'); if (isset($matching_name[$controller]) && in_array($matching_name[$controller], $exceptions)) { continue; } if (Validate::isLoadedObject($context->employee) && !Module::getPermissionStatic($array['id_module'], 'view', $context->employee)) { continue; } } if (!($moduleInstance = Module::getInstanceByName($array['module']))) { continue; } if ($use_push && !$moduleInstance->allow_push) { continue; } // Check which / if method is callable $hook_callable = is_callable(array($moduleInstance, 'hook' . $hook_name)); $hook_retro_callable = is_callable(array($moduleInstance, 'hook' . $retro_hook_name)); if ($hook_callable || $hook_retro_callable) { $hook_args['altern'] = ++$altern; if ($use_push && isset($moduleInstance->push_filename) && file_exists($moduleInstance->push_filename)) { Tools::waitUntilFileIsModified($moduleInstance->push_filename, $moduleInstance->push_time_limit); } // Call hook method if ($hook_callable) { $display = Hook::coreCallHook($moduleInstance, 'hook' . $hook_name, $hook_args); } elseif ($hook_retro_callable) { $display = Hook::coreCallHook($moduleInstance, 'hook' . $retro_hook_name, $hook_args); } if ($array_return) { $output[$moduleInstance->name] = $display; } else { $output .= $display; } } elseif (Hook::isDisplayHookName($hook_name)) { if ($moduleInstance instanceof WidgetInterface) { $display = Hook::coreRenderWidget($moduleInstance, $hook_name, $hook_args); if ($array_return) { $output[$moduleInstance->name] = $display; } else { $output .= $display; } } } } if ($different_shop) { $context->shop = $old_shop; $context->shop->setContext($old_context, $shop->id); } return $output; }
public function getHooksByModuleId($id_module) { $module = self::getModulById($id_module); $moduleInstance = Module::getInstanceByName($module['name']); $hooks = array(); if ($this->hookAssign) { foreach ($this->hookAssign as $hook) { if (_PS_VERSION_ < "1.5") { if (is_callable(array($moduleInstance, 'hook' . $hook))) { $hooks[] = $hook; } } else { $retro_hook_name = Hook::getRetroHookName($hook); if (is_callable(array($moduleInstance, 'hook' . $hook)) || is_callable(array($moduleInstance, 'hook' . $retro_hook_name))) { $hooks[] = $retro_hook_name; } } } } $results = self::getHookByArrName($hooks); return $results; }
public function getHooksByModuleId($id_module, $id_shop) { $module = self::getModulById($id_module, $id_shop); $moduleInstance = Module::getInstanceByName($module['name']); //echo "<pre>";print_r($moduleInstance);die; $hooks = array(); if ($this->_hookAssign) { foreach ($this->_hookAssign as $hook) { $retro_hook_name = Hook::getRetroHookName($hook); if ($hook == 'topcolumn') { $retro_hook_name = 'displayTopColumn'; } if ($hook == 'nav') { $retro_hook_name = 'displayNav'; } if (is_callable(array($moduleInstance, 'hook' . $hook)) || is_callable(array($moduleInstance, 'hook' . $retro_hook_name))) { $hooks[] = $retro_hook_name; } } } $results = self::getHookByArrName($hooks); return $results; }
/** * Execute modules for specified hook * * @param string $hook_name Hook Name * @param array $hook_args Parameters for the functions * @param int $id_module Execute hook for this module only * @return string modules output */ public static function moduleExec($hook_name, $modulename, $hook_args = array(), $id_module = null, $array_return = false, $use_push = false, $id_shop = null) { static $disable_non_native_modules = null; $retro_hook_name = Hook::getRetroHookName($hook_name); // Look on modules list $altern = 0; $output = ''; if ($disable_non_native_modules && !isset(Hook::$native_module)) { Hook::$native_module = Module::getNativeModuleList(); } $different_shop = false; $context = Context::getContext(); if ($id_shop !== null && Validate::isUnsignedId($id_shop) && $id_shop != $context->shop->getContextShopID()) { $old_context = $context->shop->getContext(); $old_shop = clone $context->shop; $shop = new Shop((int) $id_shop); if (Validate::isLoadedObject($shop)) { $context->shop = $shop; $context->shop->setContext(Shop::CONTEXT_SHOP, $shop->id); $different_shop = true; } } $module_list = array(); $module_list[] = array('module' => $modulename, 'id_module' => 0); $live_edit = false; foreach ($module_list as $array) { $array['live_edit'] = false; // Check errors if ($id_module && $id_module != $array['id_module']) { continue; } if ((bool) $disable_non_native_modules && Hook::$native_module && count(Hook::$native_module) && !in_array($array['module'], self::$native_module)) { continue; } if (!($module_instance = Module::getInstanceByName($array['module']))) { continue; } if ($use_push && !$module_instance->allow_push) { continue; } $hook_callable = is_callable(array($module_instance, 'hook' . $hook_name)); $hook_retro_callable = is_callable(array($module_instance, 'hook' . $retro_hook_name)); if (($hook_callable || $hook_retro_callable) && Module::preCall($module_instance->name)) { $hook_args['altern'] = ++$altern; if ($use_push && isset($module_instance->push_filename) && file_exists($module_instance->push_filename)) { Tools::waitUntilFileIsModified($module_instance->push_filename, $module_instance->push_time_limit); } // Call hook method if ($hook_callable) { $display = $module_instance->{'hook' . $hook_name}($hook_args); } elseif ($hook_retro_callable) { $display = $module_instance->{'hook' . $retro_hook_name}($hook_args); } if (!$array_return && $array['live_edit'] && Tools::isSubmit('live_edit') && Tools::getValue('ad') && Tools::getValue('liveToken') == Tools::getAdminToken('AdminModulesPositions' . (int) Tab::getIdFromClassName('AdminModulesPositions') . (int) Tools::getValue('id_employee'))) { $live_edit = true; $output .= Hook::wrapLiveEdit($display, $module_instance, $array['id_hook']); } else { if ($array_return) { $output[$module_instance->name] = $display; } else { $output .= $display; } } } } if ($different_shop) { $context->shop = $old_shop; $context->shop->setContext($old_context, $shop->id); } if ($array_return) { return $output; } else { return ($live_edit ? '<script type="text/javascript">hooks_list.push(\'' . $hook_name . '\');</script> <div id="' . $hook_name . '" class="dndHook" style="min-height:50px">' : '') . $output . ($live_edit ? '</div>' : ''); } }
/** * execute module, return string html * */ private function getModuleAssign($moduleObject, $hook_name) { if (Validate::isLoadedObject($moduleObject) && $moduleObject->id) { if (!isset($hookArgs['cookie']) or !$hookArgs['cookie']) { $hookArgs['cookie'] = $this->context->cookie; } if (!isset($hookArgs['cart']) or !$hookArgs['cart']) { $hookArgs['cart'] = $this->context->cart; } if (_PS_VERSION_ < "1.5") { $hook_name = strtolower($hook_name); if (!Validate::isHookName($hook_name)) { die(Tools::displayError()); } $altern = 0; if (is_callable(array($moduleObject, 'hook' . $hook_name))) { $hookArgs['altern'] = ++$altern; $output = call_user_func(array($moduleObject, 'hook' . $hook_name), $hookArgs); } return $output; } else { //$hook_name = substr($hook_name, 7, strlen($hook_name)); if (!Validate::isHookName($hook_name)) { die(Tools::displayError()); } $retro_hook_name = Hook::getRetroHookName($hook_name); $hook_callable = is_callable(array($moduleObject, 'hook' . $hook_name)); $hook_retro_callable = is_callable(array($moduleObject, 'hook' . $retro_hook_name)); $output = ''; if (($hook_callable || $hook_retro_callable) && Module::preCall($moduleObject->name)) { if ($hook_callable) { $output = $moduleObject->{'hook' . $hook_name}($hookArgs); } elseif ($hook_retro_callable) { $output = $moduleObject->{'hook' . $retro_hook_name}($hookArgs); } } return $output; } } return ''; }
/** * Get list of modules we can execute per hook * * @param string $hook_name Get list of modules for this hook if given * @return array */ public static function getHookModuleExecList($hook_name = null) { $context = Context::getContext(); $cache_id = 'ovic_hook_module_exec_list_' . (isset($context->shop->id) ? '_' . $context->shop->id : '') . (isset($context->customer) ? '_' . $context->customer->id : ''); if (!Cache::isStored($cache_id) || $hook_name == 'displayPayment' || $hook_name == 'displayBackOfficeHeader') { $frontend = true; $groups = array(); $use_groups = Group::isFeatureActive(); if (isset($context->employee)) { $frontend = false; } else { // Get groups list if ($use_groups) { if (isset($context->customer) && $context->customer->isLogged()) { $groups = $context->customer->getGroups(); } elseif (isset($context->customer) && $context->customer->isLogged(true)) { $groups = array((int) Configuration::get('PS_GUEST_GROUP')); } else { $groups = array((int) Configuration::get('PS_UNIDENTIFIED_GROUP')); } } } // SQL Request $sql = new DbQuery(); $sql->select('h.`name` as hook, m.`id_module`, h.`id_hook`, m.`name` as module, h.`live_edit`'); $sql->from('module', 'm'); if ($hook_name != 'displayBackOfficeHeader') { $sql->join(Shop::addSqlAssociation('module', 'm', true, 'module_shop.enable_device & ' . (int) Context::getContext()->getDevice())); $sql->innerJoin('module_shop', 'ms', 'ms.`id_module` = m.`id_module`'); } $sql->innerJoin('ovic_backup_hook_module', 'hm', 'hm.`id_module` = m.`id_module`'); $sql->innerJoin('hook', 'h', 'hm.`id_hook` = h.`id_hook`'); if ($hook_name != 'displayPayment') { $sql->where('h.name != "displayPayment"'); } elseif ($frontend) { if (Validate::isLoadedObject($context->country)) { $sql->where('(h.name = "displayPayment" AND (SELECT id_country FROM ' . _DB_PREFIX_ . 'module_country mc WHERE mc.id_module = m.id_module AND id_country = ' . (int) $context->country->id . ' AND id_shop = ' . (int) $context->shop->id . ' LIMIT 1) = ' . (int) $context->country->id . ')'); } if (Validate::isLoadedObject($context->currency)) { $sql->where('(h.name = "displayPayment" AND (SELECT id_currency FROM ' . _DB_PREFIX_ . 'module_currency mcr WHERE mcr.id_module = m.id_module AND id_currency IN (' . (int) $context->currency->id . ', -1, -2) LIMIT 1) IN (' . (int) $context->currency->id . ', -1, -2))'); } } if (Validate::isLoadedObject($context->shop)) { $sql->where('hm.id_shop = ' . (int) $context->shop->id); } if ($frontend) { if ($use_groups) { $sql->leftJoin('module_group', 'mg', 'mg.`id_module` = m.`id_module`'); if (Validate::isLoadedObject($context->shop)) { $sql->where('mg.id_shop = ' . (int) $context->shop->id . ' AND mg.`id_group` IN (' . implode(', ', $groups) . ')'); } else { $sql->where('mg.`id_group` IN (' . implode(', ', $groups) . ')'); } } } $sql->groupBy('hm.id_hook, hm.id_module'); $sql->orderBy('hm.`position`'); $list = array(); if ($result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql)) { foreach ($result as $row) { $row['hook'] = strtolower($row['hook']); if (!isset($list[$row['hook']])) { $list[$row['hook']] = array(); } $list[$row['hook']][] = array('id_hook' => $row['id_hook'], 'module' => $row['module'], 'id_module' => $row['id_module'], 'live_edit' => $row['live_edit']); } } if ($hook_name != 'displayPayment' && $hook_name != 'displayBackOfficeHeader') { Cache::store($cache_id, $list); // @todo remove this in 1.6, we keep it in 1.5 for retrocompatibility self::$_hook_modules_cache_exec = $list; } } else { $list = Cache::retrieve($cache_id); } // If hook_name is given, just get list of modules for this hook if ($hook_name) { $retro_hook_name = strtolower(Hook::getRetroHookName($hook_name)); $hook_name = strtolower($hook_name); $return = array(); $inserted_modules = array(); if (isset($list[$hook_name])) { $return = $list[$hook_name]; } foreach ($return as $module) { $inserted_modules[] = $module['id_module']; } if (isset($list[$retro_hook_name])) { foreach ($list[$retro_hook_name] as $retro_module_call) { if (!in_array($retro_module_call['id_module'], $inserted_modules)) { $return[] = $retro_module_call; } } } return count($return) > 0 ? $return : false; } else { return $list; } }
public static function hookExec($hook_name, $hook_args = array(), $id_module = null, $array = array()) { if (!empty($id_module) && !Validate::isUnsignedId($id_module) || !Validate::isHookName($hook_name)) { die(Tools::displayError()); } $context = Context::getContext(); if (!isset($hook_args['cookie']) || !$hook_args['cookie']) { $hook_args['cookie'] = $context->cookie; } if (!isset($hook_args['cart']) || !$hook_args['cart']) { $hook_args['cart'] = $context->cart; } if ($id_module && $id_module != $array['id_module']) { return; } if (!($module_instance = Module::getInstanceByName($array['module'])) || !$module_instance->active) { return; } $retro_hook_name = Hook::getRetroHookName($hook_name); $hook_callable = is_callable(array($module_instance, 'hook' . $hook_name)); $hook_retro_callable = is_callable(array($module_instance, 'hook' . $retro_hook_name)); $output = ''; if (($hook_callable || $hook_retro_callable) && Module::preCall($module_instance->name)) { if ($hook_callable) { $output = $module_instance->{'hook' . $hook_name}($hook_args); } else { if ($hook_retro_callable) { $output = $module_instance->{'hook' . $retro_hook_name}($hook_args); } } } return $output; }
public function execModuleHook($hook_name, $hook_args = array(), $id_module = null, $id_shop = null) { // Check arguments validity if ($id_module && !is_numeric($id_module) || !Validate::isHookName($hook_name)) { throw new PrestaShopException('Invalid id_module or hook_name'); } // Check if hook exists if (!($id_hook = Hook::getIdByName($hook_name))) { return false; } // Store list of executed hooks on this page Hook::$executed_hooks[$id_hook] = $hook_name; $live_edit = false; $context = Context::getContext(); if (!isset($hook_args['cookie']) || !$hook_args['cookie']) { $hook_args['cookie'] = $context->cookie; } if (!isset($hook_args['cart']) || !$hook_args['cart']) { $hook_args['cart'] = $context->cart; } $retro_hook_name = Hook::getRetroHookName($hook_name); // Look on modules list $altern = 0; $output = ''; $different_shop = false; if ($id_shop !== null && Validate::isUnsignedId($id_shop) && $id_shop != $context->shop->getContextShopID()) { $old_context_shop_id = $context->shop->getContextShopID(); $old_context = $context->shop->getContext(); $old_shop = clone $context->shop; $shop = new Shop((int) $id_shop); if (Validate::isLoadedObject($shop)) { $context->shop = $shop; $context->shop->setContext(Shop::CONTEXT_SHOP, $shop->id); $different_shop = true; } } if (!($moduleInstance = Module::getInstanceById($id_module))) { continue; } // Check which / if method is callable $hook_callable = is_callable(array($moduleInstance, 'hook' . $hook_name)); $hook_retro_callable = is_callable(array($moduleInstance, 'hook' . $retro_hook_name)); if (($hook_callable || $hook_retro_callable) && Module::preCall($moduleInstance->name)) { $hook_args['altern'] = ++$altern; // Call hook method if ($hook_callable) { $display = $moduleInstance->{'hook' . $hook_name}($hook_args); } elseif ($hook_retro_callable) { $display = $moduleInstance->{'hook' . $retro_hook_name}($hook_args); } $output .= $display; } if ($different_shop) { $context->shop = $old_shop; $context->shop->setContext($old_context, $shop->id); } return $output; }
/** * Execute modules for specified hook * * @param string $hook_name Hook Name * @param array $hook_args Parameters for the functions * @param int $id_module Execute hook for this module only * @param bool $array_return If specified, module output will be set by name in an array * @param bool $check_exceptions Check permission exceptions * @param bool $use_push Force change to be refreshed on Dashboard widgets * @param int $id_shop If specified, hook will be execute the shop with this ID * * @throws PrestaShopException * * @return string/array modules output */ public static function exec($hook_name, $hook_args = array(), $id_module = null, $array_return = false, $check_exceptions = true, $use_push = false, $id_shop = null) { if (defined('PS_INSTALLATION_IN_PROGRESS')) { return; } static $disable_non_native_modules = null; if ($disable_non_native_modules === null) { $disable_non_native_modules = (bool) Configuration::get('PS_DISABLE_NON_NATIVE_MODULE'); } // Check arguments validity if ($id_module && !is_numeric($id_module) || !Validate::isHookName($hook_name)) { throw new PrestaShopException('Invalid id_module or hook_name'); } // If no modules associated to hook_name or recompatible hook name, we stop the function if (!($module_list = Hook::getHookModuleExecList($hook_name))) { return ''; } // Check if hook exists if (!($id_hook = Hook::getIdByName($hook_name))) { return false; } // Store list of executed hooks on this page Hook::$executed_hooks[$id_hook] = $hook_name; $live_edit = false; $context = Context::getContext(); if (!isset($hook_args['cookie']) || !$hook_args['cookie']) { $hook_args['cookie'] = $context->cookie; } if (!isset($hook_args['cart']) || !$hook_args['cart']) { $hook_args['cart'] = $context->cart; } $retro_hook_name = Hook::getRetroHookName($hook_name); // Look on modules list $altern = 0; $output = ''; if ($disable_non_native_modules && !isset(Hook::$native_module)) { Hook::$native_module = Module::getNativeModuleList(); } $different_shop = false; if ($id_shop !== null && Validate::isUnsignedId($id_shop) && $id_shop != $context->shop->getContextShopID()) { $old_context = $context->shop->getContext(); $old_shop = clone $context->shop; $shop = new Shop((int) $id_shop); if (Validate::isLoadedObject($shop)) { $context->shop = $shop; $context->shop->setContext(Shop::CONTEXT_SHOP, $shop->id); $different_shop = true; } } foreach ($module_list as $array) { // Check errors if ($id_module && $id_module != $array['id_module']) { continue; } if ((bool) $disable_non_native_modules && Hook::$native_module && count(Hook::$native_module) && !in_array($array['module'], Hook::$native_module)) { continue; } // Check permissions if ($check_exceptions) { $exceptions = Module::getExceptionsStatic($array['id_module'], $array['id_hook']); $controller = Dispatcher::getInstance()->getController(); $controller_obj = Context::getContext()->controller; //check if current controller is a module controller if (isset($controller_obj->module) && Validate::isLoadedObject($controller_obj->module)) { $controller = 'module-' . $controller_obj->module->name . '-' . $controller; } if (in_array($controller, $exceptions)) { continue; } //Backward compatibility of controller names $matching_name = array('authentication' => 'auth', 'productscomparison' => 'compare'); if (isset($matching_name[$controller]) && in_array($matching_name[$controller], $exceptions)) { continue; } if (Validate::isLoadedObject($context->employee) && !Module::getPermissionStatic($array['id_module'], 'view', $context->employee)) { continue; } } if (!($moduleInstance = Module::getInstanceByName($array['module']))) { continue; } if ($use_push && !$moduleInstance->allow_push) { continue; } // Check which / if method is callable $hook_callable = is_callable(array($moduleInstance, 'hook' . $hook_name)); $hook_retro_callable = is_callable(array($moduleInstance, 'hook' . $retro_hook_name)); if (($hook_callable || $hook_retro_callable) && Module::preCall($moduleInstance->name)) { $hook_args['altern'] = ++$altern; if ($use_push && isset($moduleInstance->push_filename) && file_exists($moduleInstance->push_filename)) { Tools::waitUntilFileIsModified($moduleInstance->push_filename, $moduleInstance->push_time_limit); } // Call hook method if ($hook_callable) { $display = Hook::coreCallHook($moduleInstance, 'hook' . $hook_name, $hook_args); } elseif ($hook_retro_callable) { $display = Hook::coreCallHook($moduleInstance, 'hook' . $retro_hook_name, $hook_args); } // Live edit if (!$array_return && $array['live_edit'] && Tools::isSubmit('live_edit') && Tools::getValue('ad') && Tools::getValue('liveToken') == Tools::getAdminToken('AdminModulesPositions' . (int) Tab::getIdFromClassName('AdminModulesPositions') . (int) Tools::getValue('id_employee'))) { $live_edit = true; $output .= self::wrapLiveEdit($display, $moduleInstance, $array['id_hook']); } elseif ($array_return) { $output[$moduleInstance->name] = $display; } else { $output .= $display; } } } if ($different_shop) { $context->shop = $old_shop; $context->shop->setContext($old_context, $shop->id); } if ($array_return) { return $output; } else { return ($live_edit ? '<script type="text/javascript">hooks_list.push(\'' . $hook_name . '\');</script> <div id="' . $hook_name . '" class="dndHook" style="min-height:50px">' : '') . $output . ($live_edit ? '</div>' : ''); } // Return html string }
public function autoregisterhook($hook_name = 'moduleRoutes', $module_name = 'smartblog', $shop_list = null) { if (Module::isEnabled($module_name) == 1 && Module::isInstalled($module_name) == 1) { $return = true; $id_sql = 'SELECT `id_module` FROM `' . _DB_PREFIX_ . 'module` WHERE `name` = "' . $module_name . '"'; $id_module = Db::getInstance()->getValue($id_sql); if (is_array($hook_name)) { $hook_names = $hook_name; } else { $hook_names = array($hook_name); } foreach ($hook_names as $hook_name) { if (!Validate::isHookName($hook_name)) { throw new PrestaShopException('Invalid hook name'); } if (!isset($id_module) || !is_numeric($id_module)) { return false; } //$hook_name_bak = $hook_name; if ($alias = Hook::getRetroHookName($hook_name)) { $hook_name = $alias; } $id_hook = Hook::getIdByName($hook_name); //$live_edit = Hook::getLiveEditById((int) Hook::getIdByName($hook_name_bak)); if (!$id_hook) { $new_hook = new Hook(); $new_hook->name = pSQL($hook_name); $new_hook->title = pSQL($hook_name); $new_hook->live_edit = (bool) preg_match('/^display/i', $new_hook->name); $new_hook->position = (bool) $new_hook->live_edit; $new_hook->add(); $id_hook = $new_hook->id; if (!$id_hook) { return false; } } if (is_null($shop_list)) { $shop_list = Shop::getShops(true, null, true); } foreach ($shop_list as $shop_id) { $sql = 'SELECT hm.`id_module` FROM `' . _DB_PREFIX_ . 'hook_module` hm, `' . _DB_PREFIX_ . 'hook` h WHERE hm.`id_module` = ' . (int) $id_module . ' AND h.`id_hook` = ' . $id_hook . ' AND h.`id_hook` = hm.`id_hook` AND `id_shop` = ' . (int) $shop_id; if (Db::getInstance()->getRow($sql)) { continue; } $sql = 'SELECT MAX(`position`) AS position FROM `' . _DB_PREFIX_ . 'hook_module` WHERE `id_hook` = ' . (int) $id_hook . ' AND `id_shop` = ' . (int) $shop_id; if (!($position = Db::getInstance()->getValue($sql))) { $position = 0; } $return &= Db::getInstance()->insert('hook_module', array('id_module' => (int) $id_module, 'id_hook' => (int) $id_hook, 'id_shop' => (int) $shop_id, 'position' => (int) ($position + 1))); } } return $return; } else { return false; } }
/** * Return the hooks list where this module can be hooked. * * @return array Hooks list. */ public function getPossibleHooksList() { $hooks_list = Hook::getHooks(); $possible_hooks_list = array(); foreach ($hooks_list as &$current_hook) { $hook_name = $current_hook['name']; $retro_hook_name = Hook::getRetroHookName($hook_name); if (is_callable(array($this, 'hook' . ucfirst($hook_name))) || is_callable(array($this, 'hook' . ucfirst($retro_hook_name)))) { $possible_hooks_list[] = array('id_hook' => $current_hook['id_hook'], 'name' => $hook_name, 'title' => $current_hook['title']); } } return $possible_hooks_list; }