Jump to content

The table doesn't show in the front page


Recommended Posts

Hello, I did create JS for showing the table with  names of products and their ids on the front page,

this is my module code

<?php
class Newmodule extends Module
{
public function __construct()
{

    $this->name="newmodule";
    $this->author='anton';
    $this->version='1.0.0';
    $this->bootstrap=true;
    parent::__construct();
    $this->displayName=$this->l('newmodule');
    $this->description=$this->l('This is new module');
    $this->ps_versions_compliancy=array('min'=>'1.7.0','max'=>'8.99.99');

}
public function install()
{
    include_once ($this->local_path.'sql/install.php');
    return parent::install() && $this->registerHook('displayHome') && $this->registerHook('actionFrontControllerSetMedia')
        && $this->createTabLink() && $this->registerHook('displayHeader'); // TODO: Change the autogenerated stub
}
public function uninstall()
{
    include_once ($this->local_path.'sql/unistall.php');
    return parent::uninstall(); // TODO: Change the autogenerated stub
}
public function hookDisplayHome()
{
    $this->context->smarty->assign(array(
        'Multipurpose'=>Configuration::get('Multipurpose')
    ));
return $this->display(__FILE__,'views/templates/hook/home.tpl') ;

}
public function hookDisplayHeader()
{
    Media::addJsDef(array('mp_ajax'=>$this->_path.'ajax.php'
    ));
    $this->context->controller->addCSS(array(
        $this->_path.'views/css/newmodule.css'
    ));

    $this->context->controller->addJS(array(
        $this->_path.'views/js/newmodule.js'
    ));

}
public function getContent()
{
    if(Tools::issubmit('savemymodule'))
    {
        $name=Tools::getValue('print');
        Configuration::updateValue('Multipurpose',$name,true);
    }
    $this->generateAdminToken();
    $this->context->smarty->assign(array(
        'token'=>$this->generateAdminToken()
    ));
    return $this->display(__FILE__,'views/templates/admin/configure.tpl') ;

}
public function createTabLink()
{
    $tab= new Tab;
    foreach (Language::getLanguages() as $lang){
        $tab->name[$lang['id_lang']]=$this->l('Origin');
    }
    $tab->class_name='AdminOrigin';
    $tab->module=$this->name;
    $tab->id_parent=0;
    $tab->add();
    return true;
}


    /**
     * @throws PrestaShopDatabaseException
     */
    public function getProductsByCategoryID($id_category): string
{
    $obj_cat= new Category($id_category,$this->context->language->id);
    $products=$obj_cat->getProducts($this->context->language->id,0,1000);
    $html = '<ol>';
    foreach ($products as $pr)
    {
        $html .='<li>'.$pr['name'].'</li>';
    }
    $html .= '<ol>';
    return $html;
}
public function generateAdminToken()
{
$cookie=new Cookie('psAdmin');
$id_employee=$cookie->__get('id_employee');
    $controller='AdminOrders';
$id_class=Tab::getIdFromClassName($controller);

return Tools::getAdminToken($controller.$id_class.$id_employee);
}
public function loadProducts()
{$nd=Db::getInstance()->getValue('SELECT COUNT(*) FROM `'._DB_PREFIX_.'product`');
    $data = Db::getInstance()->executeS('SELECT p.`id_product`,pl.`name` FROM `'._DB_PREFIX_.'product` p
    LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON(p.`id_product` = pl.`id_product`)
    WHERE pl.`id_lang`='.(int)$this->context->language->id);
    return array(
        'recordsTotal'=>$nd,
        'recordsFiltered'=>$nd,
        'data'=>$data
    );
}
    public function hookActionFrontControllerSetMedia($params)
    {
        Media ::addJsDef([
            'newmodule' => $this -> context -> link -> getModuleLink($this -> name, 'task', [], true),
        ]);

        $this -> context -> controller -> registerJavascript('newmodule',
            'modules/' . $this -> name . '/views/js/task.js');
        $this -> context -> controller -> registerJavascript('newmodule',
            'modules/' . $this -> name . '/views/js/dataTables.bootstrap.js');
        $this -> context -> controller -> registerJavascript('newmodule',
            'modules/' . $this -> name . '/views/js/jquery.dataTables.js');
    }
}

this is my javascript

$(document).ready(function(){

    $('#producttable').dataTable({
        'processing':true,
        'serverSide':true,
        'ajax':{
            'url': mp_ajax+ '?action=ptable'
        },
        "columns":[
    {"data" :"id_product"},
    {"data":"name"}
        ]

    });
});

this is my PHP code for loading products

<?php

require_once ('../../config/config.inc.php');
require_once ('../../init.php');

$obj_mp= ModuleCore::getInstanceByName('newmodule');
switch(Tools::getValue('action'))
{
    case 'ptable':
        echo json_encode($obj_mp->loadProducts());
        break;
    default:
        echo $obj_mp->getProductsByCategoryID(Tools::getValue('id_category'));
        break;
}
die;

and this is the front page module

<?php
Class NewmoduletaskModuleFrontController extends ModuleFrontControllerCore
{
    public function __construct()
    {
        parent::__construct();
    }
    public function initContent()
    {
        parent::initContent(); // TODO: Change the autogenerated stub
        $this->context->smarty->assign(array(
            'nb_product'=>Db::getInstance()->getValue('SELECT COUNT(*) FROM `'._DB_PREFIX_.'product`'),
            'categories'=>Db::getInstance()->executeS('SELECT `id_category`,`name` FROM `'._DB_PREFIX_.'category_lang` WHERE `id_lang` = '.(int)$this->context->language->id),
            'shopname'=>Configuration::get('PS_SHOP_NAME'),
            'manufacturer'=>Db::getInstance()->getRow('SELECT * FROM`'._DB_PREFIX_.'manufacturer`')
        ));
        $this->setTemplate('module:newmodule/views/templates/front/task.tpl');
    }
}

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...