如何在1 admincontroller中组合fields_list + .tpl视图?

时间:2017-06-22 08:01:18

标签: php model-view-controller prestashop prestashop-1.6

我无法弄清楚如何在我的自定义管理控制器中显示1个fields_list + .tpl文件的内容。目标是在库存中显示我的产品密钥+下面的一些额外功能(来自tpl文件的内容)。

我可以显示字段列表 OR 来自.tpl文件的消息。但没有合并......我在网上发现了一个教程,这个非常接近,但没有用。

<?php

 require_once(_PS_MODULE_DIR_ . 'avanto_key/classes/AvantoStock.php');

class AdminAvantokeyStockController extends ModuleAdminController
{
    protected $position_identifier = 'id_avanto_keys';

    public function __construct()
    {
        //$this->fields_form = $this->fieldForm();
        $this->bootstrap = true;
        $this->table = 'avanto_keys'; // DB table name where your object data stored
        $this->className = "AvantoStock"; // The class name of my object
        //$this->identifier = 'id_avanto_keys';
        //$this->list_id = 'id_avanto_keys';
        $this->_defaultOrderBy = 'id_avanto_keys';
        //$this->lang = FALSE;

        $this->addRowAction('edit');
        $this->addRowAction('delete');

        $this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'),
            'confirm' => $this->l('Delete selected items?')), );

        //Shop::addTableAssociation($this->table, array('type' => 'shop'));



    parent::__construct();
        $this->_select =  'pl.`name` as product_name, a.`serial_key` as serial_display';
        $this->_join = 'LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = a.`id_product` AND pl.`id_lang` 
        = '.(int)$this->context->language->id.')';
    }

    public function renderView()
    {
        $tpl = $this->context->smarty->createTemplate(
            dirname(__FILE__).
            '/../../views/templates/admin/view.tpl');
        return $tpl->fetch();
    }

    public function renderList()
    {
        $this->toolbar_title = $this->l('Stock Management');
        $this->toolbar_btn['new'] = null;
        $this->fields_list = array(
            'id_avanto_keys' => array(
                'title' => $this->l('ID Key'),
                'width' => 140,
            ),
            'id_product' => array(
                'title' => $this->l('Product ID'),
                'width' => 140,
            ),
            'serial_key' => array(
                'title' => $this->l('Serial Keys'),
                'width' => 140,
            ),
            'product_name' => array(
                'title' => $this->l('Product Name'),
                'width' => 140,
            ),
        );

        return parent::renderList();
    }


    public function init()
    {
        parent::init();
    }

    public function initContent()
    {

        $this->context->smarty->assign(array(
            'form' => $form,
            'base_dir' =>  _PS_MODULE_DIR_,
        ));
        $this->setTemplate('stock.tpl');
        $lists = parent::initContent();


        $this->renderList();
        $lists .= parent::initContent();

        return $lists;
    }



    public function renderForm()
    {
        $this->display = 'edit';
        $this->initToolbar();

        $this->fields_form = array(
            'tinymce' => true,
            'legend' => array(
                'title' => $this->l('Edit product key'),
            ),
            'input' => array(
                array(
                    'type' => 'text',
                    'label' => $this->l('Key ID'),
                    'name' => 'id_product',
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Product ID'),
                    'name' => 'id_avanto_keys',
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Serial Key'),
                    'required' => true,
                    'name' => 'serial_key',
                ),
            ),
            'submit' => array(
                'title' => $this->l('Save'),
                'class' => 'btn btn-default pull-right'
            )
        );

        return parent::renderForm();
    }




}

?>  

上面的代码只显示消息hello world而不是我的产品列表。 任何人都知道如何结合这个?

提前致谢!!!

1 个答案:

答案 0 :(得分:3)

有点困惑,我们必须做一些整洁:):

第一:
列表中的字段最好在__construct中声明,因此:

public function __construct()
{
    $this->module = 'YourModuleName'; // Here you have to put your module name

    $this->bootstrap = true;
    $this->table = 'avanto_keys'; // DB table name where your object data stored
    $this->className = "AvantoStock"; // The class name of my object

    //$this->identifier = 'id_avanto_keys';
    //$this->list_id = 'id_avanto_keys';
    $this->_defaultOrderBy = 'id_avanto_keys';
    //$this->lang = FALSE;
    $this->explicitSelect = true; // This if you do a select manually after

    $this->addRowAction('edit');
    $this->addRowAction('delete');

    $this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'),
        'confirm' => $this->l('Delete selected items?')), );

    //Shop::addTableAssociation($this->table, array('type' => 'shop'));


    $this->_select =  'pl.`name` as product_name, a.`serial_key` as serial_display';
    $this->_join = 'LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = a.`id_product` AND pl.`id_lang` 
    = '.(int)$this->context->language->id.')';

    $this->fields_list = array(
        'id_avanto_keys' => array(
            'title' => $this->l('ID Key'),
            'width' => 140,
        ),
        'id_product' => array(
            'title' => $this->l('Product ID'),
            'width' => 140,
        ),
        'serial_key' => array(
            'title' => $this->l('Serial Keys'),
            'width' => 140,
        ),
        'product_name' => array(
            'title' => $this->l('Product Name'),
            'width' => 140,
        ),
    );

    parent::__construct();
}


renderList方法制作其他内容,让我们将其与您想要显示的内容分开:

public function renderList()
{
    // Here we retrieve the list (without doing any strange thing)
    $list = parent::renderList();

    // Assign some vars to pass to our custom tpl
    $this->context->smarty->assign(
        array( 
            'var1' => "Test",
            'var2' => "Test2"
            )
        );

    // Get the custom tpl rendered
    $content = $this->context->smarty->fetch(_PS_MODULE_DIR_ . "avanto_key/views/templates/admin/avantokeystock/customcontent.tpl");

    // return the list plus your content
    return $list . $content;
}

第三
保留父initContent原样,不要覆盖,因为他制作了很多东西

我想这是一个很好的开始:) 试试这种方式让我知道;)