Jump to content

Get Category variable in module


l2phmode

Recommended Posts

Modification module ph_blog_column_custom, display-specific blog category in certain categories.

The following actions were taken to address this problem.

Add to DB 

ALTER TABLE `ps_category_lang` ADD `news_category_id` text AFTER `description`;

In  controllers/admin/AdminCategoriesController.php

array(
 'type' => 'text',
 'label' => $this->l('News Category ID:'),
 'name' => 'news_category_id',
 'autoload_rte' => true,
 'lang' => true,
 'rows' => 10,
 'cols' => 100,
 'hint' => $this->l('Invalid characters:').' <>;=#{}'
 ),

In classes/Category.php

/** @var string News Category ID */
 public $news_category_id;

And

'news_category_id' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),

In Category.tpl varible display ok.

 

For Display CMS news in Left column add in Tools.php

public static function getCMSTitle($id_cms,$id_lang){
$cms = new CMS($id_cms, $id_lang);
return $cms->meta_title;
}

public static function getCMSDescription($id_cms,$id_lang){
$cms = new CMS($id_cms, $id_lang);
return $cms->content;
}

Output in header.tpl

{if isset ($category->news_category_id)}
<a class="list-group-item" href="{$link->getCMSLink($category->news_category_id, $cmspages.link_rewrite)|escape:'html':'UTF-8'}">
{Tools::getCMSTitle($category->news_category_id,$cookie->id_lang)}</a>
{Tools::getCMSDescription($category->news_category_id,$cookie->id_lang)}
{/if}
This solution works well.
But there is a problem specific module to display news.
 
In module ph_blog_column_custom.php
Part of the code for the withdrawal of a certain module category.
 public function preparePosts($nb = 6, $from = null)
    {
        $featured = false;
        $order_by = 'sbp.date_add';

        if($from == 'featured')
        {
            $from = null;
            $featured = true;
        }
        elseif($from == 'recent')
            $from = null;
        elseif($from == 'viewed')
        {
            $from = null;
            $order_by = 'sbp.views';
        }
        elseif($from == 'liked')
        {
            $from = null;
            $order_by = 'sbp.likes';
        }
        else
            $from = (int)$from;

        if(!Module::isInstalled('ph_simpleblog') || !Module::isEnabled('ph_simpleblog'))
            return false;

        require_once _PS_MODULE_DIR_ . 'ph_simpleblog/models/SimpleBlogPost.php';

        $id_lang = $this->context->language->id;

        $posts = SimpleBlogPost::getPosts($id_lang, (int)$nb, $from, null, true, $order_by, 'DESC', null, (bool)$featured);

        return $posts;
    }

It is necessary to bring in the code variable.

if($from == 'featured')
        {
           // $from = null;- number category
          //  chenge
           $from = $category->news_category_id;
            $featured = true;
        }

But this design refuses to work.

Help solve the problem.

Link to comment
Share on other sites

I guess that must be in php module file to initialize variables and get them out of msql.

I think there must be something like that.

Who will answer both pass a variable from Сategory.php AdminCategoriesController.php in php module file.???

  public function initContent()
    {
        parent::initContent();

        $this->setTemplate(_PS_THEME_DIR_.'category.tpl');

        if (!$this->customer_access) {
            return;
        }

        if (isset($this->context->cookie->id_compare)) {
            $this->context->smarty->assign('compareProducts', CompareProduct::getCompareProducts((int)$this->context->cookie->id_compare));
        }

        // Product sort must be called before assignProductList()
        $this->productSort();

        $this->assignScenes();
        $this->assignSubcategories();
        $this->assignProductList();

        $this->context->smarty->assign(array(
            'category'             => $this->category,
            'description_short'    => Tools::truncateString($this->category->description, 350),
            'products'             => (isset($this->cat_products) && $this->cat_products) ? $this->cat_products : null,
            'id_category'          => (int)$this->category->id,
            'id_category_parent'   => (int)$this->category->id_parent,
            'return_category_name' => Tools::safeOutput($this->category->name),
            'path'                 => Tools::getPath($this->category->id),
            'add_prod_display'     => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
            'categorySize'         => Image::getSize(ImageType::getFormatedName('category')),
            'mediumSize'           => Image::getSize(ImageType::getFormatedName('medium')),
            'thumbSceneSize'       => Image::getSize(ImageType::getFormatedName('m_scene')),
            'homeSize'             => Image::getSize(ImageType::getFormatedName('home')),
            'allow_oosp'           => (int)Configuration::get('PS_ORDER_OUT_OF_STOCK'),
            'comparator_max_item'  => (int)Configuration::get('PS_COMPARATOR_MAX_ITEM'),
            'suppliers'            => Supplier::getSuppliers(),
            'body_classes'         => array($this->php_self.'-'.$this->category->id, $this->php_self.'-'.$this->category->link_rewrite)
        ));
    }
Edited by l2phmode (see edit history)
Link to comment
Share on other sites

 public function preparePosts($nb = 6, $from = null)
    {   
        $featured = false;
        $order_by = 'sbp.date_add';
       
        if($from == 'featured')
        {
            $from = null;
            $featured = true;
        }
        elseif($from == 'recent'){
        $link = mysqli_connect('localhost', 'user', 'qwerty', 'dbname');
        $id = (int)Tools::getValue('id_category');
        $sql =" SELECT news_category_id FROM ps_category_lang WHERE id_category = '$id' and id_shop = '1' and id_lang = '2'";
        $res = mysqli_query($link, $sql);
       mysqli_close($link);
       while($row = mysqli_fetch_array($res))
  
        $from = $row['news_category_id'];
		}
        elseif($from == 'viewed')
        {
            $from = null;
            $order_by = 'sbp.views';
        }
        elseif($from == 'liked')
        {
            $from = null;
            $order_by = 'sbp.likes';
        }
        else
            $from = (int)$from;

        if(!Module::isInstalled('ph_simpleblog') || !Module::isEnabled('ph_simpleblog'))
            return false;

        require_once _PS_MODULE_DIR_ . 'ph_simpleblog/models/SimpleBlogPost.php';

        $id_lang = $this->context->language->id;

        $posts = SimpleBlogPost::getPosts($id_lang, (int)$nb, $from, null, true, $order_by, 'DESC', null, (bool)$featured);

        return $posts;
    }

Thanks

Link to comment
Share on other sites

To be honest... i'm not sure what are you trying to do...?

 

First of all, for sure "news_category_id" shouldn't by TYPE_HTML but TYPE_INT validated within "isUnsignedInt", also, if this id is related to post i don't think that you need to do additional query on preparePosts method, you should modify getPosts method from SimpleBlogPost and add additional param which would be used by you to filter posts by "news_category_id".

 

bwt. please be careful with sharing my module code, do not share too much of it :)

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...