|
| 1 | +<?php |
| 2 | +if (!defined('_PS_VERSION_')) { |
| 3 | + exit; |
| 4 | +} |
| 5 | + |
| 6 | +class Blockheader extends Module |
| 7 | +{ |
| 8 | + public function __construct() |
| 9 | + { |
| 10 | + $this->name = 'blockheader'; // Corrected name to lowercase 'blockheader' |
| 11 | + $this->tab = 'front_office_features'; |
| 12 | + $this->version = '1.0.0'; |
| 13 | + $this->author = 'Your Name'; // Placeholder, will be filled by user if needed |
| 14 | + $this->need_instance = 0; |
| 15 | + $this->bootstrap = true; |
| 16 | + |
| 17 | + parent::__construct(); |
| 18 | + |
| 19 | + public function getContent() |
| 20 | + { |
| 21 | + Tools::redirectAdmin($this->context->link->getAdminLink('AdminCustomheaderlogoSetting')); |
| 22 | + } |
| 23 | + |
| 24 | + $this->displayName = $this->l('Custom Header Block'); |
| 25 | + $this->description = $this->l('Adds a custom sticky header with desktop and mobile views.'); |
| 26 | + $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_); // QloApps is based on PS 1.6 |
| 27 | + } |
| 28 | + |
| 29 | + public function install() |
| 30 | + { |
| 31 | + if (!parent::install() |
| 32 | + || !$this->registerHooks() |
| 33 | + || !$this->installTab('AdminCustomheaderlogoSetting', 'Header Logo & Branding')) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + // Default configurations |
| 38 | + Configuration::updateValue('BH_BRAND_TEXT1', 'Your Brand'); |
| 39 | + Configuration::updateValue('BH_PHONE_NUMBER', 'Call Us: 123-456-7890'); |
| 40 | + Configuration::updateValue('BH_DESKTOP_LINK1_TEXT', 'About Us'); |
| 41 | + Configuration::updateValue('BH_DESKTOP_LINK1_URL', Context::getContext()->link->getPageLink('cms', true, null, 'id_cms=4')); |
| 42 | + Configuration::updateValue('BH_DESKTOP_CTA_TEXT', 'Contact Us'); |
| 43 | + Configuration::updateValue('BH_DESKTOP_CTA_URL', Context::getContext()->link->getPageLink('contact', true)); |
| 44 | + Configuration::updateValue('BH_MOBILE_LINK1_ICON', 'fa fa-home'); |
| 45 | + Configuration::updateValue('BH_MOBILE_LINK1_TEXT', 'Home'); |
| 46 | + Configuration::updateValue('BH_MOBILE_LINK1_URL', Context::getContext()->link->getPageLink('index', true)); |
| 47 | + Configuration::updateValue('BH_MOBILE_LINK3_ICON', 'fa fa-envelope'); |
| 48 | + Configuration::updateValue('BH_MOBILE_LINK3_TEXT', 'Quote'); |
| 49 | + Configuration::updateValue('BH_MOBILE_LINK3_URL', Context::getContext()->link->getPageLink('contact', true)); |
| 50 | + |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + public function uninstall() |
| 55 | + { |
| 56 | + if (!parent::uninstall() |
| 57 | + // || !Configuration::deleteByName('BH_LOGO') // Decide if configs should be deleted |
| 58 | + || !$this->uninstallTab('AdminCustomheaderlogoSetting')) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + // Placeholder for hookDisplayHeader |
| 65 | + // public function hookDisplayHeader($params) |
| 66 | + // { |
| 67 | + // \$this->context->controller->addCSS(\$this->_path.'views/css/blockheader.css'); |
| 68 | + // \$this->context->controller->addJS(\$this->_path.'views/js/blockheader.js'); |
| 69 | + // } |
| 70 | + |
| 71 | + // Placeholder for hookDisplayTop |
| 72 | + // public function hookDisplayTop(\$params) |
| 73 | + // { |
| 74 | + // \$this->context->smarty->assign(array( |
| 75 | + // 'blockheader_logo' => Configuration::get('BLOCKHEADER_LOGO'), |
| 76 | + // 'blockheader_text1' => Configuration::get('BLOCKHEADER_BRAND_TEXT1'), |
| 77 | + // // ... more variables |
| 78 | + // )); |
| 79 | + // return \$this->display(__FILE__, 'views/templates/hook/headerBlock.tpl'); |
| 80 | + // } |
| 81 | + |
| 82 | + // Helper function to install an Admin Tab |
| 83 | + private function installTab(\$className, \$tabName, \$tabParentName = false) |
| 84 | + { |
| 85 | + \$tab = new Tab(); |
| 86 | + \$tab->active = 1; |
| 87 | + \$tab->class_name = \$className; |
| 88 | + \$tab->name = array(); |
| 89 | + foreach (Language::getLanguages(true) as \$lang) { |
| 90 | + \$tab->name[\$lang['id_lang']] = \$tabName; |
| 91 | + } |
| 92 | + if (\$tabParentName) { |
| 93 | + \$tab->id_parent = (int)Tab::getIdFromClassName(\$tabParentName); |
| 94 | + } else { |
| 95 | + // Attempt to find a generic parent tab like "Modules" or a design/theme related one |
| 96 | + \$parentId = Tab::getIdFromClassName('AdminParentModulesSf'); // Common parent for module configurations |
| 97 | + if (!\$parentId) { |
| 98 | + \$parentId = Tab::getIdFromClassName('AdminParentThemes'); |
| 99 | + } |
| 100 | + if (!\$parentId) { |
| 101 | + \$parentId = Tab::getIdFromClassName('AdminModules'); // Older PrestaShop |
| 102 | + } |
| 103 | + if (!\$parentId) { |
| 104 | + \$parentId = (int)Tab::getIdFromClassName('DEFAULT'); // Fallback |
| 105 | + } |
| 106 | + \$tab->id_parent = \$parentId; |
| 107 | + } |
| 108 | + \$tab->module = \$this->name; |
| 109 | + return \$tab->add(); |
| 110 | + } |
| 111 | + |
| 112 | + // Helper function to uninstall an Admin Tab |
| 113 | + private function uninstallTab(\$className) |
| 114 | + { |
| 115 | + \$id_tab = (int)Tab::getIdFromClassName(\$className); |
| 116 | + if (\$id_tab) { |
| 117 | + \$tab = new Tab(\$id_tab); |
| 118 | + return \$tab->delete(); |
| 119 | + } |
| 120 | + return true; // Return true if tab doesn't exist or already deleted |
| 121 | + } |
| 122 | + |
| 123 | + public function hookDisplayHeader($params) |
| 124 | + { |
| 125 | + $this->context->controller->addCSS($this->_path.'views/css/blockheader.css', 'all'); |
| 126 | + $this->context->controller->addJS($this->_path.'views/js/blockheader.js'); |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + public function hookDisplayTop($params) |
| 131 | + { |
| 132 | + if (!$this->isCached('views/templates/hook/headerBlock.tpl', $this->getCacheId())) { |
| 133 | + $this->context->smarty->assign(array( |
| 134 | + 'BH_LOGO' => Configuration::get('BH_LOGO'), |
| 135 | + 'BH_BRAND_TEXT1' => Configuration::get('BH_BRAND_TEXT1'), |
| 136 | + 'BH_BRAND_TEXT2' => Configuration::get('BH_BRAND_TEXT2'), |
| 137 | + 'BH_PHONE_NUMBER' => Configuration::get('BH_PHONE_NUMBER'), |
| 138 | + 'BH_DESKTOP_LINK1_TEXT' => Configuration::get('BH_DESKTOP_LINK1_TEXT'), |
| 139 | + 'BH_DESKTOP_LINK1_URL' => Configuration::get('BH_DESKTOP_LINK1_URL'), |
| 140 | + 'BH_DESKTOP_LINK2_TEXT' => Configuration::get('BH_DESKTOP_LINK2_TEXT'), |
| 141 | + 'BH_DESKTOP_LINK2_URL' => Configuration::get('BH_DESKTOP_LINK2_URL'), |
| 142 | + 'BH_DESKTOP_CTA_TEXT' => Configuration::get('BH_DESKTOP_CTA_TEXT'), |
| 143 | + 'BH_DESKTOP_CTA_URL' => Configuration::get('BH_DESKTOP_CTA_URL'), |
| 144 | + 'BH_MOBILE_BG_IMAGE' => Configuration::get('BH_MOBILE_BG_IMAGE'), |
| 145 | + 'BH_MOBILE_LINK1_ICON' => Configuration::get('BH_MOBILE_LINK1_ICON'), |
| 146 | + 'BH_MOBILE_LINK1_TEXT' => Configuration::get('BH_MOBILE_LINK1_TEXT'), |
| 147 | + 'BH_MOBILE_LINK1_URL' => Configuration::get('BH_MOBILE_LINK1_URL'), |
| 148 | + 'BH_MOBILE_LINK2_ICON' => Configuration::get('BH_MOBILE_LINK2_ICON'), |
| 149 | + 'BH_MOBILE_LINK2_TEXT' => Configuration::get('BH_MOBILE_LINK2_TEXT'), |
| 150 | + 'BH_MOBILE_LINK2_URL' => Configuration::get('BH_MOBILE_LINK2_URL'), |
| 151 | + 'BH_MOBILE_LINK3_ICON' => Configuration::get('BH_MOBILE_LINK3_ICON'), |
| 152 | + 'BH_MOBILE_LINK3_TEXT' => Configuration::get('BH_MOBILE_LINK3_TEXT'), |
| 153 | + 'BH_MOBILE_LINK3_URL' => Configuration::get('BH_MOBILE_LINK3_URL'), |
| 154 | + 'module_dir' => $this->_path, |
| 155 | + 'shop_name' => Configuration::get('PS_SHOP_NAME') |
| 156 | + )); |
| 157 | + } |
| 158 | + return $this->display(__FILE__, 'views/templates/hook/headerBlock.tpl', $this->getCacheId()); |
| 159 | + } |
| 160 | + |
| 161 | + private function registerHooks() |
| 162 | + { |
| 163 | + return $this->registerHook('displayHeader') && |
| 164 | + $this->registerHook('displayTop') && /* Or displayNavFullWidth depending on theme */ |
| 165 | + $this->registerHook('displayBlockHeaderNavigation') && /* Custom hook for desktop nav items */ |
| 166 | + $this->registerHook('displayBlockHeaderMobileNavigation'); /* Custom hook for mobile nav items */ |
| 167 | + } |
| 168 | + |
| 169 | + private function getBlockHeaderNavigationLinks() |
| 170 | + { |
| 171 | + $links = array(); |
| 172 | + for ($i = 1; $i <= 5; ++$i) { |
| 173 | + if (Configuration::get('BH_NAV_LINK'.$i.'_ACTIVE')) { |
| 174 | + $text = Configuration::get('BH_NAV_LINK'.$i.'_TEXT'); |
| 175 | + $url_conf = Configuration::get('BH_NAV_LINK'.$i.'_URL'); |
| 176 | + $url = ''; |
| 177 | + |
| 178 | + if (!empty($text) && !empty($url_conf)) { |
| 179 | + // Check for PrestaShop specific links like contact, cms:ID, category:ID |
| 180 | + if (strpos($url_conf, 'http://') === 0 || strpos($url_conf, 'https://') === 0) { |
| 181 | + $url = $url_conf; |
| 182 | + } elseif (strpos($url_conf, 'cms:') === 0) { |
| 183 | + $id_cms = (int)str_replace('cms:', '', $url_conf); |
| 184 | + $url = $this->context->link->getCMSLink($id_cms); |
| 185 | + } elseif (strpos($url_conf, 'category:') === 0) { |
| 186 | + $id_category = (int)str_replace('category:', '', $url_conf); |
| 187 | + $url = $this->context->link->getCategoryLink($id_category); |
| 188 | + } else { // Simple page names like 'contact', 'authentication' |
| 189 | + $url = $this->context->link->getPageLink($url_conf, true); |
| 190 | + } |
| 191 | + |
| 192 | + $links[] = array( |
| 193 | + 'text' => $text, |
| 194 | + 'url' => $url, |
| 195 | + 'active' => true, // Already filtered by BH_NAV_LINKX_ACTIVE |
| 196 | + // 'current' => false, // Add logic for current page highlighting if needed |
| 197 | + ); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + return $links; |
| 202 | + } |
| 203 | + |
| 204 | + public function hookDisplayBlockHeaderNavigation($params) |
| 205 | + { |
| 206 | + if (!$this->isCached('views/templates/hook/_partials/navigation_links.tpl', $this->getCacheId('desktop_nav'))) { |
| 207 | + $custom_navigation_links = $this->getBlockHeaderNavigationLinks(); |
| 208 | + $this->context->smarty->assign('custom_navigation_links', $custom_navigation_links); |
| 209 | + } |
| 210 | + return $this->display(__FILE__, 'views/templates/hook/_partials/navigation_links.tpl', $this->getCacheId('desktop_nav')); |
| 211 | + } |
| 212 | + |
| 213 | + public function hookDisplayBlockHeaderMobileNavigation($params) |
| 214 | + { |
| 215 | + // For now, mobile uses the same links and template. Can be customized later. |
| 216 | + if (!$this->isCached('views/templates/hook/_partials/navigation_links.tpl', $this->getCacheId('mobile_nav'))) { |
| 217 | + $custom_navigation_links = $this->getBlockHeaderNavigationLinks(); |
| 218 | + $this->context->smarty->assign(array( |
| 219 | + 'custom_navigation_links' => $custom_navigation_links, |
| 220 | + 'mobile_nav_class' => true, // To potentially style mobile UL differently via CSS |
| 221 | + )); |
| 222 | + } |
| 223 | + return $this->display(__FILE__, 'views/templates/hook/_partials/navigation_links.tpl', $this->getCacheId('mobile_nav')); |
| 224 | + } |
| 225 | +} |
0 commit comments