public static function exec($hook_name, $hookArgs = array(), $id_module = null) { $memoryUsage = memory_get_usage(); $t0 = microtime(true); $result = parent::exec($hook_name, $hookArgs, $id_module); self::$hookTime[$hook_name] = microtime(true) - $t0; self::$hookMemoryUsage[$hook_name] = memory_get_usage() - $memoryUsage; return $result; }
/** * 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 function exec($hook_name, $hook_args = array(), $id_module = null, $array_return = false, $check_exceptions = true, $use_push = false, $id_shop = null) { try { return \HookCore::exec($hook_name, $hook_args, $id_module, $array_return, $check_exceptions, $use_push, $id_shop); } catch (\Exception $e) { $logger = \PrestaShop\PrestaShop\Adapter\ServiceLocator::get('logger'); $logger->error(sprintf('Exception on hook %s for module %s. %s', $hook_name, $id_module, $e->getMessage())); } }
public static function exec($hook_name, $hook_args = array(), $id_module = null, $array_return = false, $check_exceptions = true) { $memoryUsage = memory_get_usage(); $t0 = microtime(true); $result = parent::exec($hook_name, $hook_args, $id_module, $array_return, $check_exceptions); self::$hookTime[$hook_name] = microtime(true) - $t0; self::$hookMemoryUsage[$hook_name] = memory_get_usage() - $memoryUsage; return $result; }
public static function exec($hook_name, $hook_args = array(), $id_module = null, $array_return = false, $check_exceptions = true, $use_push = false, $id_shop = null) { $output = parent::exec($hook_name, $hook_args, $id_module, $array_return, $check_exceptions, $use_push, $id_shop); $live_edit = Tools::isSubmit('live_edit') ? true : false; if (!($module_list = Hook::getHookModuleExecList($hook_name))) { return ''; } $ip = Configuration::get('witm_config'); $ip_array = explode(',', $ip); $before_output = ''; $after_output = ''; if (in_array(Tools::getRemoteAddr(), $ip_array) || in_array('*', $ip_array)) { if (Configuration::get('witm_h_m') == 1) { $before_output .= ' <div class="div_infos_hook"> <span class="infos_hook"><span onclick="display_infos_hook(\'' . $hook_name . '\')"><i class="icon-expand-alt icon-large"></i> H-M</span> <span class="hook_module" id="hook_module_' . $hook_name . '"><br/> HOOK: ' . $hook_name . '<div class="see_modules"><span onclick="display_module(\'' . $hook_name . '\')"><i class="icon-expand-alt icon-large"></i> Modules</span><span id="d_m_' . $hook_name . '" class="display_modules">'; } else { $before_output .= ' <div class="div_infos_hook"> <span class="infos_hook">H-M <span class="hook_module"><br/> HOOK: ' . $hook_name . '<div class="see_modules"><span onclick="display_module(\'' . $hook_name . '\')"><i class="icon-expand-alt icon-large"></i> Modules</span><span id="d_m_' . $hook_name . '" class="display_modules">'; } $css_js = (int) Configuration::get('witm_css_js'); $img = '<img src="' . Context::getContext()->shop->physical_uri . '/modules/whatisthismodule/img/open_new_tab.png"/>'; foreach ($module_list as $module) { $css = ''; $js = ''; if ($css_js == 1) { foreach (Context::getContext()->controller->css_files as $key => $value) { if (strstr($key, '/' . $module['module'] . '/')) { $css .= '<br/> <a class="file_css" href="' . $key . '" target="_blank">CSS : ' . $key . ' ' . $img . '</a>'; } } foreach (Context::getContext()->controller->js_files as $key => $value) { if (strstr($value, '/' . $module['module'] . '/')) { $js .= '<br/> <a class="file_js" href="' . $value . '" target="_blank">JS : ' . $value . ' ' . $img . '</a>'; } } } $before_output .= '<br/>- ' . $module['module'] . ($css != '' && $js != '' ? ' : ' : '') . $css . $js; } $before_output .= '</span></div></span></span>'; $after_output .= '</div>'; } 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">' : '') . $before_output . $output . $after_output . ($live_edit ? '</div>' : ''); } }
public static function exec($hook_name, $hook_args = array(), $id_module = null, $array_return = false, $check_exceptions = true, $use_push = false, $id_shop = null) { $module_list = Hook::getHookModuleExecList($hook_name); if (is_array($module_list)) { $module_list_ids = array(); foreach ($module_list as $module) { $module_list_ids[] = $module['id_module']; } self::$executedModules = array_merge(self::$executedModules, $module_list_ids); } $memoryUsage = memory_get_usage(); $t0 = microtime(true); $result = parent::exec($hook_name, $hook_args, $id_module, $array_return, $check_exceptions, $use_push, $id_shop); self::$hookTime[$hook_name] = microtime(true) - $t0; self::$hookMemoryUsage[$hook_name] = memory_get_usage() - $memoryUsage; return $result; }
public static function getHookModuleExecList($hook_name = null) { $list = parent::getHookModuleExecList($hook_name); if ($hook_name != 'displayPayment') { return $list; } if (Module::isInstalled('agilemembership') && Module::isInstalled('agilepaypal') && (int) Configuration::get('AGILE_PAYPAL_AM_INTEGRATED') == 1) { include_once _PS_ROOT_DIR_ . "/modules/agilepaypal/agilepaypal.php"; $ap = new AgilePaypal(); if ($ap->hasNonMembershipProducts()) { return $list; } foreach ($list as $payment) { if ($payment['module'] == 'agilepaypal') { return array($payment); } } return false; } return $list; }
/** * Preload hook modules cache * * @deprecated 1.5.0 use Hook::getHookModuleList() instead * * @return bool preload_needed */ public static function preloadHookModulesCache() { Tools::displayAsDeprecated('Use Hook::getHookModuleList() instead'); if (!is_null(self::$_hook_modules_cache)) { return false; } self::$_hook_modules_cache = Hook::getHookModuleList(); return true; }
/** * {@inheritdoc} */ public function getCatalogProductList($offset, $limit, $orderBy, $sortOrder, $post = array(), $avoidPersistence = false, $formatCldr = true) { $filterParams = $this->combinePersistentCatalogProductFilter(array_merge($post, ['last_offset' => $offset, 'last_limit' => $limit, 'last_orderBy' => $orderBy, 'last_sortOrder' => $sortOrder]), $avoidPersistence); $filterParams = AdminFilter::sanitizeFilterParameters($filterParams); $showPositionColumn = $this->isCategoryFiltered(); if ($orderBy == 'position_ordering' && $showPositionColumn) { foreach ($filterParams as $key => $param) { if (strpos($key, 'filter_column_') === 0) { $filterParams[$key] = ''; } } } if ($orderBy == 'position_ordering') { $orderBy = 'position'; } $idShop = \ContextCore::getContext()->shop->id; $idLang = \ContextCore::getContext()->language->id; $sqlSelect = array('id_product' => array('table' => 'p', 'field' => 'id_product', 'filtering' => ' %s '), 'reference' => array('table' => 'p', 'field' => 'reference', 'filtering' => self::FILTERING_LIKE_BOTH), 'price' => array('table' => 'p', 'field' => 'price', 'filtering' => ' %s '), 'id_shop_default' => array('table' => 'p', 'field' => 'id_shop_default'), 'is_virtual' => array('table' => 'p', 'field' => 'is_virtual'), 'name' => array('table' => 'pl', 'field' => 'name', 'filtering' => self::FILTERING_LIKE_BOTH), 'active' => array('table' => 'sa', 'field' => 'active', 'filtering' => self::FILTERING_EQUAL_NUMERIC), 'shopname' => array('table' => 'shop', 'field' => 'name'), 'id_image' => array('table' => 'image_shop', 'field' => 'id_image'), 'name_category' => array('table' => 'cl', 'field' => 'name', 'filtering' => self::FILTERING_LIKE_BOTH), 'price_final' => '0', 'nb_downloadable' => array('table' => 'pd', 'field' => 'nb_downloadable'), 'sav_quantity' => array('table' => 'sav', 'field' => 'quantity', 'filtering' => ' %s '), 'badge_danger' => array('select' => 'IF(sav.`quantity`<=0, 1, 0)', 'filtering' => 'IF(sav.`quantity`<=0, 1, 0) = %s')); $sqlTable = array('p' => 'product', 'pl' => array('table' => 'product_lang', 'join' => 'LEFT JOIN', 'on' => 'pl.`id_product` = p.`id_product` AND pl.`id_lang` = ' . $idLang . ' AND pl.`id_shop` = ' . $idShop), 'sav' => array('table' => 'stock_available', 'join' => 'LEFT JOIN', 'on' => 'sav.`id_product` = p.`id_product` AND sav.`id_product_attribute` = 0 AND sav.id_shop = ' . $idShop), 'sa' => array('table' => 'product_shop', 'join' => 'JOIN', 'on' => 'p.`id_product` = sa.`id_product` AND sa.id_shop = ' . $idShop), 'cl' => array('table' => 'category_lang', 'join' => 'LEFT JOIN', 'on' => 'sa.`id_category_default` = cl.`id_category` AND cl.`id_lang` = ' . $idLang . ' AND cl.id_shop = ' . $idShop), 'c' => array('table' => 'category', 'join' => 'LEFT JOIN', 'on' => 'c.`id_category` = cl.`id_category`'), 'shop' => array('table' => 'shop', 'join' => 'LEFT JOIN', 'on' => 'shop.id_shop = ' . $idShop), 'image_shop' => array('table' => 'image_shop', 'join' => 'LEFT JOIN', 'on' => 'image_shop.`id_product` = p.`id_product` AND image_shop.`cover` = 1 AND image_shop.id_shop = ' . $idShop), 'i' => array('table' => 'image', 'join' => 'LEFT JOIN', 'on' => 'i.`id_image` = image_shop.`id_image`'), 'pd' => array('table' => 'product_download', 'join' => 'LEFT JOIN', 'on' => 'pd.`id_product` = p.`id_product`')); $sqlWhere = array('AND', 1); foreach ($filterParams as $filterParam => $filterValue) { if (!$filterValue && $filterValue !== '0') { continue; } if (strpos($filterParam, 'filter_column_') === 0) { $filterValue = \Db::getInstance()->escape($filterValue, in_array($filterParam, ['filter_column_id_product', 'filter_column_sav_quantity', 'filter_column_price']), true); $field = substr($filterParam, 14); // 'filter_column_' takes 14 chars if (isset($sqlSelect[$field]['table'])) { $sqlWhere[] = $sqlSelect[$field]['table'] . '.`' . $sqlSelect[$field]['field'] . '` ' . sprintf($sqlSelect[$field]['filtering'], $filterValue); } else { $sqlWhere[] = '(' . sprintf($sqlSelect[$field]['filtering'], $filterValue) . ')'; } } // for 'filter_category', see next if($showPositionColumn) block. } $sqlWhere[] = 'state = ' . \Product::STATE_SAVED; $sqlOrder = array($orderBy . ' ' . $sortOrder); if ($orderBy != 'id_product') { $sqlOrder[] = 'id_product asc'; // secondary order by (useful when ordering by active, quantity, price, etc...) } $sqlLimit = $offset . ', ' . $limit; // Column 'position' added if filtering by category if ($showPositionColumn) { $filteredCategoryId = (int) $filterParams['filter_category']; $sqlSelect['position'] = array('table' => 'cp', 'field' => 'position'); $sqlTable['cp'] = array('table' => 'category_product', 'join' => 'INNER JOIN', 'on' => 'cp.`id_product` = p.`id_product` AND cp.`id_category` = ' . $filteredCategoryId); } elseif ($orderBy == 'position') { // We do not show position column, so we do not join the table, so we do not order by position! $sqlOrder = array('id_product ASC'); } // exec legacy hook but with different parameters (retro-compat < 1.7 is broken here) \HookCore::exec('actionAdminProductsListingFieldsModifier', array('_ps_version' => _PS_VERSION_, 'sql_select' => &$sqlSelect, 'sql_table' => &$sqlTable, 'sql_where' => &$sqlWhere, 'sql_order' => &$sqlOrder, 'sql_limit' => &$sqlLimit)); $sql = $this->compileSqlQuery($sqlSelect, $sqlTable, $sqlWhere, $sqlOrder, $sqlLimit); $products = \Db::getInstance()->executeS($sql, true, false); $total = \Db::getInstance()->executeS('SELECT FOUND_ROWS();', true, false); $total = $total[0]['FOUND_ROWS()']; // post treatment $currency = new \CurrencyCore(\Configuration::get('PS_CURRENCY_DEFAULT')); foreach ($products as &$product) { $product['total'] = $total; // total product count (filtered) $product['price_final'] = \ProductCore::getPriceStatic($product['id_product'], true, null, (int) \Configuration::get('PS_PRICE_DISPLAY_PRECISION'), null, false, true, 1, true, null, null, null, $nothing, true, true); if ($formatCldr) { $product['price'] = \ToolsCore::displayPrice($product['price'], $currency); $product['price_final'] = \ToolsCore::displayPrice($product['price_final'], $currency); } $product['image'] = $this->imageManager->getThumbnailForListing($product['id_image']); } // post treatment by hooks // exec legacy hook but with different parameters (retro-compat < 1.7 is broken here) \HookCore::exec('actionAdminProductsListingResultsModifier', array('_ps_version' => _PS_VERSION_, 'products' => &$products, 'total' => $total)); return $products; }
/** * This will handle magic methods registered as listeners. * * These methods are built with the following syntax: * "call_<hookID>_<moduleID>(HookEvent $event, $hookName)" * * @param string $name The method called * @param array $args The HookEvent, and then the hook name (eventName) * @throws \BadMethodCallException */ public function __call($name, $args) { if (strpos($name, 'call_') !== 0) { throw new \BadMethodCallException('The call to \'' . $name . '\' is not recognized.'); } $ids = explode('_', $name); array_shift($ids); // remove 'call' if (count($ids) != 2) { throw new \BadMethodCallException('The call to \'' . $name . '\' is not recognized.'); } $moduleId = $ids[1]; $hookName = $args[1]; $event = $args[0]; /* @var $event HookEvent */ $content = \HookCore::exec($hookName, $event->getHookParameters(), $moduleId, $event instanceof RenderingHookEvent); if ($event instanceof RenderingHookEvent) { $event->setContent(array_values($content)[0], array_keys($content)[0]); } }
/** * Update a quantity for a product or a combination. * * Does not work in Advanced stock management. * * @param \ProductCore $product * @param integer $quantity * @param integer $forAttributeId */ public function processQuantityUpdate(\ProductCore $product, $quantity, $forAttributeId = 0) { // Hook triggered by legacy code below: actionUpdateQuantity('id_product', 'id_product_attribute', 'quantity') \StockAvailableCore::setQuantity((int) $product->id, $forAttributeId, $quantity); \HookCore::exec('actionProductUpdate', array('id_product' => (int) $product->id, 'product' => $product)); }
/** * Get list of modules we can execute per hook * * @since 1.5.0 * @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 = self::MODULE_LIST_BY_HOOK_KEY . (isset($context->shop->id) ? '_' . $context->shop->id : '') . (isset($context->customer) ? '_' . $context->customer->id : ''); if (!Cache::isStored($cache_id) || $hook_name == 'displayPayment' || $hook_name == 'displayPaymentEU' || $hook_name == 'paymentOptions' || $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'); $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('hook_module', 'hm', 'hm.`id_module` = m.`id_module`'); $sql->innerJoin('hook', 'h', 'hm.`id_hook` = h.`id_hook`'); if ($hook_name != 'paymentOptions') { $sql->where('h.`name` != "paymentOptions"'); } elseif ($frontend) { if (Validate::isLoadedObject($context->country)) { $sql->where('((h.`name` = "displayPayment" OR h.`name` = "displayPaymentEU" OR h.`name` = "paymentOptions")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" OR h.`name` = "displayPaymentEU" OR h.`name` = "paymentOptions") 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->cart)) { $carrier = new Carrier($context->cart->id_carrier); if (Validate::isLoadedObject($carrier)) { $sql->where('((h.`name` = "displayPayment" OR h.`name` = "displayPaymentEU" OR h.`name` = "paymentOptions") AND (SELECT `id_reference` FROM `' . _DB_PREFIX_ . 'module_carrier` mcar WHERE mcar.`id_module` = m.`id_module` AND `id_reference` = ' . (int) $carrier->id_reference . ' AND `id_shop` = ' . (int) $context->shop->id . ' LIMIT 1) = ' . (int) $carrier->id_reference . ')'); } } } 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 . (count($groups) ? ' AND mg.`id_group` IN (' . implode(', ', $groups) . ')' : '')); } elseif (count($groups)) { $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']); } } if ($hook_name != 'displayPayment' && $hook_name != 'displayPaymentEU' && $hook_name != 'paymentOptions' && $hook_name != 'displayBackOfficeHeader') { Cache::store($cache_id, $list); // @todo remove this in 1.6, we keep it in 1.5 for backward compatibility 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; } }