spc Posted November 3, 2013 Share Posted November 3, 2013 HelloI´m trying to create a FAQ modules where I can add topics about Faq, and when i press on the head link it show the rest of the topics.I´m using PS 1.5.6.0 and it to new for me..I have copy from others modules.I have faq.php, faq.tpl, faqclass.phpthis is the faq.php file, but i need the add new icon, and to show the faq in a list in the BO.And how do i show it on a page on the front page.faq.php <?php if (!defined('_PS_VERSION_')) exit; class Faq extends Module { public function __construct() { $this->name = 'faq'; $this->tab = 'front_office_features'; $this->version = '1.0'; $this->author = ''; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Faqs'); $this->description = $this->l('A faqs module for your homepage.'); $path = dirname(__FILE__); if (strpos(__FILE__, 'Module.php') !== false) $path .= '/../modules/'.$this->name; include_once $path.'/FaqClass.php'; } public function install() { if (!parent::install() || !$this->registerHook('displayHome') || !$this->registerHook('displayHeader')) return false; $res = Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'faq` ( `id_faq` int(10) unsigned NOT NULL auto_increment, `id_shop` int(10) unsigned NOT NULL , `body_home_logo_link` varchar(255) NOT NULL, PRIMARY KEY (`id_faq`)) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'); if ($res) $res &= Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'faq_lang` ( `id_faq` int(10) unsigned NOT NULL, `id_lang` int(10) unsigned NOT NULL, `body_title` varchar(255) NOT NULL, `body_subheading` varchar(255) NOT NULL, `body_paragraph` text NOT NULL, `body_logo_subheading` varchar(255) NOT NULL, PRIMARY KEY (`id_faq`, `id_lang`)) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'); if ($res) foreach (Shop::getShops(false) as $shop) $res &= $this->createExampleFaq($shop['id_shop']); if (!$res) $res &= $this->uninstall(); return $res; } private function createExampleFaq($id_shop) { $faq = new FaqClass(); $faq->id_shop = (int)$id_shop; $faq->body_home_logo_link = '#'; foreach (Language::getLanguages(false) as $lang) { $faq->body_title[$lang['id_lang']] = 'Lorem ipsum dolor sit amet'; $faq->body_subheading[$lang['id_lang']] = 'Excepteur sint occaecat cupidatat non proident'; $faq->body_paragraph[$lang['id_lang']] = '<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>'; $faq->body_logo_subheading[$lang['id_lang']] = 'Lorem ipsum presta shop amet'; } return $faq->add(); } public function uninstall() { $res = Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'faq`'); $res &= Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'faq_lang`'); if (!$res || !parent::uninstall()) return false; return true; } private function initForm() { $languages = Language::getLanguages(false); foreach ($languages as $k => $language) $languages[$k]['is_default'] = (int)($language['id_lang'] == Configuration::get('PS_LANG_DEFAULT')); $helper = new HelperForm(); $helper->module = $this; $helper->name_controller = 'faq'; $helper->identifier = $this->identifier; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->languages = $languages; $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name; $helper->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT'); $helper->allow_employee_form_lang = true; $helper->toolbar_scroll = true; $helper->toolbar_btn = $this->initToolbar(); $helper->title = $this->displayName; $helper->submit_action = 'submitUpdateFaq'; $this->fields_form[0]['form'] = array( 'tinymce' => true, 'legend' => array( 'title' => $this->displayName, 'image' => $this->_path.'logo.gif' ), 'submit' => array( 'name' => 'submitUpdateFaq', 'title' => $this->l('Save '), 'class' => 'button' ), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Main title'), 'name' => 'body_title', 'lang' => true, 'size' => 64, 'hint' => $this->l('Appears along top of your homepage'), ), array( 'type' => 'text', 'label' => $this->l('Subheading'), 'name' => 'body_subheading', 'lang' => true, 'size' => 64, ), array( 'type' => 'textarea', 'label' => $this->l('Introductory text'), 'name' => 'body_paragraph', 'lang' => true, 'autoload_rte' => true, 'hint' => $this->l('For example... explain your mission, highlight a new product, or describe a recent event.'), 'cols' => 60, 'rows' => 30 ), array( 'type' => 'file', 'label' => $this->l('Homepage logo'), 'name' => 'body_homepage_logo', 'display_image' => true ), array( 'type' => 'text', 'label' => $this->l('Homepage logo link'), 'name' => 'body_home_logo_link', 'size' => 33, ), array( 'type' => 'text', 'label' => $this->l('Homepage logo subheading'), 'name' => 'body_logo_subheading', 'lang' => true, 'size' => 33, ), ) ); return $helper; } private function initToolbar() { $this->toolbar_btn['save'] = array( 'href' => '#', 'desc' => $this->l('Save') ); return $this->toolbar_btn; } public function getContent() { $this->_html = ''; $this->postProcess(); $helper = $this->initForm(); $id_shop = (int)$this->context->shop->id; $faq = FaqClass::getByIdShop($id_shop); if (!$faq) //if faq ddo not exist for this shop => create a new example one $this->createExampleFaq($id_shop); foreach ($this->fields_form[0]['form']['input'] as $input) //fill all form fields if ($input['name'] != 'body_homepage_logo') $helper->fields_value[$input['name']] = $faq->{$input['name']}; $helper->fields_value['body_homepage_logo']['image'] = (file_exists(dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg') ? '<img src="'.$this->_path.'homepage_logo_'.(int)$id_shop.'.jpg">' : ''); if ($helper->fields_value['body_homepage_logo']) $helper->fields_value['body_homepage_logo']['size'] = filesize(dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg') / 1000; $this->_html .= $helper->generateForm($this->fields_form); return $this->_html; } public function postProcess() { $errors = ''; $id_shop = (int)$this->context->shop->id; // Delete logo image if (Tools::isSubmit('deleteImage')) { if (!file_exists(dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg')) $errors .= $this->displayError($this->l('This action cannot be made.')); else { unlink(dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg'); Configuration::updateValue('FAQ_IMAGE_DISABLE', 1); $this->_clearCache('faq.tpl'); Tools::redirectAdmin('index.php?tab=AdminModules&configure='.$this->name.'&token='.Tools::getAdminToken('AdminModules'.(int)(Tab::getIdFromClassName('AdminModules')).(int)$this->context->employee->id)); } $this->_html .= $errors; } if (Tools::isSubmit('submitUpdateFaq')) { $id_shop = (int)$this->context->shop->id; $faq = FaqClass::getByIdShop($id_shop); $faq->copyFromPost(); $faq->update(); /* upload the image */ if (isset($_FILES['body_homepage_logo']) && isset($_FILES['body_homepage_logo']['tmp_name']) && !empty($_FILES['body_homepage_logo']['tmp_name'])) { Configuration::set('PS_IMAGE_GENERATION_METHOD', 1); if (file_exists(dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg')) unlink(dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg'); if ($error = ImageManager::validateUpload($_FILES['body_homepage_logo'])) $errors .= $error; elseif (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES['body_homepage_logo']['tmp_name'], $tmpName)) return false; elseif (!ImageManager::resize($tmpName, dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg')) $errors .= $this->displayError($this->l('An error occurred while attempting to upload the image.')); if (isset($tmpName)) unlink($tmpName); } $this->_html .= $errors == '' ? $this->displayConfirmation($this->l('Settings updated successfully.')) : $errors; if (file_exists(dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg')) { list($width, $height, $type, $attr) = getimagesize(dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg'); Configuration::updateValue('FAQ_IMAGE_WIDTH', (int)round($width)); Configuration::updateValue('FAQ_IMAGE_HEIGHT', (int)round($height)); Configuration::updateValue('FAQ_IMAGE_DISABLE', 0); } $this->_clearCache('faq.tpl'); } } public function hookDisplayHome($params) { if (!$this->isCached('faq.tpl', $this->getCacheId())) { $id_shop = (int)$this->context->shop->id; $faq = FaqClass::getByIdShop($id_shop); if (!$faq) return; $faq = new FaqClass((int)$faq->id, $this->context->language->id); if (!$faq) return; $this->smarty->assign(array( 'faq' => $faq, 'default_lang' => (int)$this->context->language->id, 'image_width' => Configuration::get('FAQ_IMAGE_WIDTH'), 'image_height' => Configuration::get('FAQ_IMAGE_HEIGHT'), 'id_lang' => $this->context->language->id, 'homepage_logo' => !Configuration::get('FAQ_IMAGE_DISABLE') && file_exists('modules/faq/homepage_logo_'.(int)$id_shop.'.jpg'), 'image_path' => $this->_path.'homepage_logo_'.(int)$id_shop.'.jpg' )); } return $this->display(__FILE__, 'faq.tpl', $this->getCacheId()); } public function hookDisplayHeader() { $this->context->controller->addCSS(($this->_path).'faq.css', 'all'); } } Link to comment Share on other sites More sharing options...
gonebdg - webindoshop.com Posted November 3, 2013 Share Posted November 3, 2013 Without have to develop a module, you can create FAQ with CMS feature. Back Office > Preferences > CMS Create New CMS Category : FAQ Then you can add New CMS as many as you want inside CMS Category FAQ Or you may add CMS Sub.Categroy FAQ-1, FAQ-2, etc But if you insist to create a module, you may use module controller file to display your FAQ page e.g: /modules/faq/controllers/front/default.php With it's own controller file, your faq module will have it's own page and you can configure the SEO stuff for that page on Back Office > Preferences > SEO & URLs But if you plan to display your faq module with Prestashop hook module, then you may use the necessary front office hook e.g : public function hookLeftColumn() public function hookRightColumn($params) { // Your Code .... return $this->display(__FILE__, 'column.tpl'); } The smarty file can be placed in sub.directory of your module (since P.S v.1.5) e.g : /modules/faq/views/templates/front/column.tpl OR placed inside your module directory, for Prestashop compatibility matter e.g : /modules/faq/column.tpl Please read the documentation : Creating a PrestaShop module Link to comment Share on other sites More sharing options...
spc Posted November 3, 2013 Author Share Posted November 3, 2013 (edited) Thanks I whill read the documentation,I have manage to make the front wiew..So now i can se it in the templates i make... But i think I have mist a lot of stuff, I whill uppload the file soon...I whant to create the admin page like this: Edited November 3, 2013 by spc (see edit history) Link to comment Share on other sites More sharing options...
spc Posted November 4, 2013 Author Share Posted November 4, 2013 HelloI work late in to the night.But i think i get it to work, i´m missing the faqs links under the add form.I have try to get it to work, but NOOOOOOOO... I hope there is some one that can help me.The code is this now: <?php if (!defined('_PS_VERSION_')) exit; class Faq extends Module { public function __construct() { $this->name = 'faq'; $this->tab = 'front_office_features'; $this->version = '1.0'; $this->author = 'xxxx'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Faqs'); $this->description = $this->l('A faqs module for your homepage.'); $path = dirname(__FILE__); if (strpos(__FILE__, 'Module.php') !== false) $path .= '/../modules/'.$this->name; include_once $path.'/FaqClass.php'; } public function install() { if (!parent::install() || !$this->registerHook('displayLeftColumn') || !$this->registerHook('displayHeader')) return false; $res = Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'faq` ( `id_faq` int(10) unsigned NOT NULL auto_increment, `id_shop` int(10) unsigned NOT NULL , PRIMARY KEY (`id_faq`)) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'); if ($res) $res &= Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'faq_lang` ( `id_faq` int(10) unsigned NOT NULL, `id_lang` int(10) unsigned NOT NULL, `body_title` varchar(255) NOT NULL, `body_paragraph` text NOT NULL, PRIMARY KEY (`id_faq`, `id_lang`)) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'); if ($res) foreach (Shop::getShops(false) as $shop) $res &= $this->createExampleFaq($shop['id_shop']); if (!$res) $res &= $this->uninstall(); return $res; } private function createExampleFaq($id_shop) { $faq = new FaqClass(); $faq->id_shop = (int)$id_shop; $faq->body_home_logo_link = ''; foreach (Language::getLanguages(false) as $lang) { $faq->body_title[$lang['id_lang']] = 'Lorem ipsum dolor sit amet'; $faq->body_subheading[$lang['id_lang']] = 'Excepteur sint occaecat cupidatat non proident'; $faq->body_paragraph[$lang['id_lang']] = '<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>'; $faq->body_logo_subheading[$lang['id_lang']] = 'Lorem ipsum presta shop amet'; } return $faq->add(); } public function uninstall() { $res = Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'faq`'); $res &= Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'faq_lang`'); if (!$res || !parent::uninstall()) return false; return true; } private function initForm() { $languages = Language::getLanguages(false); foreach ($languages as $k => $language) $languages[$k]['is_default'] = (int)($language['id_lang'] == Configuration::get('PS_LANG_DEFAULT')); $helper = new HelperForm(); $helper->module = $this; $helper->name_controller = 'faq'; $helper->identifier = $this->identifier; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->languages = $languages; $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name; $helper->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT'); $helper->allow_employee_form_lang = true; $helper->toolbar_scroll = true; $helper->toolbar_btn = $this->initToolbar(); $helper->title = $this->displayName; $helper->submit_action = 'submitUpdateFaq'; $this->fields_form[0]['form'] = array( 'tinymce' => true, 'legend' => array( 'title' => $this->displayName, 'image' => $this->_path.'logo.gif' ), 'submit' => array( 'name' => 'submitUpdateFaq', 'title' => $this->l('Save '), 'class' => 'button' ), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Main title'), 'name' => 'body_title', 'lang' => true, 'size' => 64, 'hint' => $this->l('Appears along top of your homepage'), ), array( 'type' => 'textarea', 'label' => $this->l('Introductory text'), 'name' => 'body_paragraph', 'lang' => true, 'autoload_rte' => true, 'hint' => $this->l('For example... explain your mission, highlight a new product, or describe a recent event.'), 'cols' => 60, 'rows' => 30 ), ) ); return $helper; } private function initToolbar() { $this->toolbar_btn['save'] = array( 'href' => '#', 'desc' => $this->l('Save') ); return $this->toolbar_btn; } public function getContent() { $this->_html = ''; $this->postProcess(); $helper = $this->initForm(); $id_shop = (int)$this->context->shop->id; $faq = FaqClass::getByIdShop($id_shop); if (!$faq) //if faq ddo not exist for this shop => create a new example one $this->createExampleFaq($id_shop); foreach ($this->fields_form[0]['form']['input'] as $input) //fill all form fields if ($input['name'] != 'body_homepage_logo') $helper->fields_value[$input['name']] = $faq->{$input['name']}; $this->_html .= $helper->generateForm($this->fields_form); return $this->_html; } public function getLinks() { $result = array(); // Get id and url $sql = 'SELECT b.`id_faq`, b.`id_lang`, b.`body_title`, b.`body_paragraph` FROM `'._DB_PREFIX_.'faq_lang` b'; $sql .= (int)Configuration::get('PS_FAQ_ORDERWAY') == 1 ? ' ORDER BY `id_faq` DESC' : ''; if (!$faq = Db::getInstance()->executeS($sql)) return false; $i = 0; foreach ($faq as $new) { $result[$i]['id'] = $new['id_faq']; $result[$i]['body_title'] = $new['body_title']; $result[$i]['body_paragraph'] = $new['body_paragraph']; // Get multilingual text if (!$texts = Db::getInstance()->executeS('SELECT `id_lang`, `body_title` , `body_paragraph` FROM '._DB_PREFIX_.'faq_lang WHERE `id_faq`='.(int)$new['id_faq'])) return false; foreach ($texts as $text) $result[$i]['text_'.$text['id_lang']] = $text['body_paragraph']; $i++; } return $result; } public function postProcess() { $errors = ''; $id_shop = (int)$this->context->shop->id; // Delete logo image if (Tools::isSubmit('deleteImage')) { if (!file_exists(dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg')) $errors .= $this->displayError($this->l('This action cannot be made.')); else { unlink(dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg'); Configuration::updateValue('FAQ_IMAGE_DISABLE', 1); $this->_clearCache('faq.tpl'); Tools::redirectAdmin('index.php?tab=AdminModules&configure='.$this->name.'&token='.Tools::getAdminToken('AdminModules'.(int)(Tab::getIdFromClassName('AdminModules')).(int)$this->context->employee->id)); } $this->_html .= $errors; } if (Tools::isSubmit('submitUpdateFaq')) { $id_shop = (int)$this->context->shop->id; $faq = FaqClass::getByIdShop($id_shop); $faq->copyFromPost(); $faq->update(); /* upload the image */ if (isset($_FILES['body_homepage_logo']) && isset($_FILES['body_homepage_logo']['tmp_name']) && !empty($_FILES['body_homepage_logo']['tmp_name'])) { Configuration::set('PS_IMAGE_GENERATION_METHOD', 1); if (file_exists(dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg')) unlink(dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg'); if ($error = ImageManager::validateUpload($_FILES['body_homepage_logo'])) $errors .= $error; elseif (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES['body_homepage_logo']['tmp_name'], $tmpName)) return false; elseif (!ImageManager::resize($tmpName, dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg')) $errors .= $this->displayError($this->l('An error occurred while attempting to upload the image.')); if (isset($tmpName)) unlink($tmpName); } $this->_html .= $errors == '' ? $this->displayConfirmation($this->l('Settings updated successfully.')) : $errors; if (file_exists(dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg')) { list($width, $height, $type, $attr) = getimagesize(dirname(__FILE__).'/homepage_logo_'.(int)$id_shop.'.jpg'); Configuration::updateValue('FAQ_IMAGE_WIDTH', (int)round($width)); Configuration::updateValue('FAQ_IMAGE_HEIGHT', (int)round($height)); Configuration::updateValue('FAQ_IMAGE_DISABLE', 0); } $this->_clearCache('faq.tpl'); } } public function hookdisplayLeftColumn($params) { if (!$this->isCached('faq.tpl', $this->getCacheId())) { $id_shop = (int)$this->context->shop->id; $faq = FaqClass::getByIdShop($id_shop); if (!$faq) return; $faq = new FaqClass((int)$faq->id, $this->context->language->id); if (!$faq) return; $this->smarty->assign(array( 'faq' => $faq, 'default_lang' => (int)$this->context->language->id, 'image_width' => Configuration::get('FAQ_IMAGE_WIDTH'), 'image_height' => Configuration::get('FAQ_IMAGE_HEIGHT'), 'id_lang' => $this->context->language->id, 'homepage_logo' => !Configuration::get('FAQ_IMAGE_DISABLE') && file_exists('modules/faq/homepage_logo_'.(int)$id_shop.'.jpg'), 'image_path' => $this->_path.'homepage_logo_'.(int)$id_shop.'.jpg' )); } return $this->display(__FILE__, 'faq.tpl', $this->getCacheId()); } public function hookDisplayHeader() { $this->context->controller->addCSS(($this->_path).'faq.css', 'all'); } } I add 2 image so you can se what i have done.1 is the BO, and the other is the page. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now