Esempio n. 1
0
    public static function hookExec($hook_name, $hookArgs = array(), $id_module = NULL)
    {
        global $cookie;
        if (!empty($id_module) and !Validate::isUnsignedId($id_module) or !Validate::isHookName($hook_name)) {
            die(Tools::displayError());
        }
        global $cart, $cookie;
        $live_edit = false;
        if (!isset($hookArgs['cookie']) or !$hookArgs['cookie']) {
            $hookArgs['cookie'] = $cookie;
        }
        if (!isset($hookArgs['cart']) or !$hookArgs['cart']) {
            $hookArgs['cart'] = $cart;
        }
        $hook_name = strtolower($hook_name);
        if (!isset(self::$_hookModulesCache)) {
            $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
            $result = $db->ExecuteS('
			SELECT h.`name` as hook, m.`id_module`, h.`id_hook`, m.`name` as module, h.`live_edit`
			FROM `' . _DB_PREFIX_ . 'module` m
			LEFT JOIN `' . _DB_PREFIX_ . 'hook_module` hm ON hm.`id_module` = m.`id_module`
			LEFT JOIN `' . _DB_PREFIX_ . 'hook` h ON hm.`id_hook` = h.`id_hook`
			AND m.`active` = 1
			ORDER BY hm.`position`', false);
            self::$_hookModulesCache = array();
            if ($result) {
                while ($row = $db->nextRow()) {
                    $row['hook'] = strtolower($row['hook']);
                    if (!isset(self::$_hookModulesCache[$row['hook']])) {
                        self::$_hookModulesCache[$row['hook']] = array();
                    }
                    self::$_hookModulesCache[$row['hook']][] = array('id_hook' => $row['id_hook'], 'module' => $row['module'], 'id_module' => $row['id_module'], 'live_edit' => $row['live_edit']);
                }
            }
        }
        if (!isset(self::$_hookModulesCache[$hook_name])) {
            return;
        }
        $altern = 0;
        $output = '';
        foreach (self::$_hookModulesCache[$hook_name] as $array) {
            if ($id_module and $id_module != $array['id_module']) {
                continue;
            }
            if (!($moduleInstance = Module::getInstanceByName($array['module']))) {
                continue;
            }
            $exceptions = $moduleInstance->getExceptions((int) $array['id_hook'], (int) $array['id_module']);
            foreach ($exceptions as $exception) {
                if (strstr(basename($_SERVER['PHP_SELF']) . '?' . $_SERVER['QUERY_STRING'], $exception['file_name']) && !strstr($_SERVER['QUERY_STRING'], $exception['file_name'])) {
                    continue 2;
                }
            }
            if (is_callable(array($moduleInstance, 'hook' . $hook_name))) {
                $hookArgs['altern'] = ++$altern;
                $display = call_user_func(array($moduleInstance, 'hook' . $hook_name), $hookArgs);
                if ($array['live_edit'] && (Tools::isSubmit('live_edit') and Tools::getValue('ad') and Tools::getValue('liveToken') == sha1(Tools::getValue('ad') . _COOKIE_KEY_))) {
                    $live_edit = true;
                    $output .= '<script type="text/javascript"> modules_list.push(\'' . $moduleInstance->name . '\');</script>
								<div id="hook_' . $array['id_hook'] . '_module_' . $moduleInstance->id . '_moduleName_' . $moduleInstance->name . '" 
								class="dndModule" style="border: 1px dotted red;' . (!strlen($display) ? 'height:50px;' : '') . '">
								<span><img src="' . $moduleInstance->_path . '/logo.gif">' . $moduleInstance->displayName . '<span style="float:right">
							 	<a href="#" id="' . $array['id_hook'] . '_' . $moduleInstance->id . '" class="moveModule">
							 		<img src="' . _PS_ADMIN_IMG_ . 'arrow_out.png"></a>
							 	<a href="#" id="' . $array['id_hook'] . '_' . $moduleInstance->id . '" class="unregisterHook">
							 		<img src="' . _PS_ADMIN_IMG_ . 'delete.gif"></span></a>
							 	</span>' . $display . '</div>';
                } else {
                    $output .= $display;
                }
            }
        }
        return ($live_edit ? '<script type="text/javascript">hooks_list.push(\'' . $hook_name . '\'); </script><!--<div id="add_' . $hook_name . '" class="add_module_live_edit">
				<a class="exclusive" href="#">Add a module</a></div>--><div id="' . $hook_name . '" class="dndHook" style="min-height:50px">' : '') . $output . ($live_edit ? '</div>' : '');
    }