簡體   English   中英

Prestashop 1.7在自定義模塊的tpl上提交表單

[英]Prestashop 1.7 Submit a form on custom module's tpl

我創建了自己的名為cus_avatar的模塊,該模塊將用於上傳客戶的頭像。 我已經制作了一個tpl文件“ uploader.tpl”,其中包含我需要提交的表單,並掛在客戶的個人資料頁面上。 如何發布此表格?

這是我的代碼:

根/模塊/ cus_avatar / cus_avatar.php:

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

class Cus_Avatar extends Module
{
    public function __construct()
    {
        // the module's details and construct codes here
    }

    public function install()
    {
        return parent::install()
            && $this->registerHook('header')
            && $this->registerHook('displayEpAvatar')
            && $this->registerHook('displayEpAvatarSidebar')
            && $this->installDb();
    }

    public function uninstall()
    {
        return parent::uninstall() && $this->uninstallDb();
    }

    protected function installDb(){

        $alterDb = "CREATE TABLE mydb."._DB_PREFIX_."avatar (
                        avatar_id INT NOT NULL AUTO_INCREMENT,
                        id_customer INT NULL,
                        avatar_path VARCHAR(255) NULL,
                        PRIMARY KEY (avatar_id)
                    ) ENGINE = MyISAM";

        return Db::getInstance()->execute($alterDb);
    }

    protected function uninstallDb(){
        $revertDb = "DROP TABLE "._DB_PREFIX_."avatar";
        return Db::getInstance()->execute($revertDb);
    }

    public function hookHeader($params)
    {
        $this->context->controller->addCss($this->_path.'assets/css/style.css', 'all');
        $this->context->controller->addJS($this->_path.'assets/js/script.js');
    }

    public function hookDisplayEpAvatar($params)
    {
        if(isset($_POST['submit_avatar']))
        {
            // THIS CODE DOESNT SEEM TO WORK
            var_dump("HELLO WORLD!");
            die();
        }

        return $this->display(__FILE__, 'views/templates/hook/uploader.tpl');
    }

    public function hookDisplayEpAvatarSideBar($params)
    {
    }
}

根/模塊/ cus_avatar /視圖/模板/鈎/ uploader.tpl:

<form name="form_avatar" method="post">
    <div class="row">
        <div class="col-xs-12 plr30">
            <label class="mt20">PROFILE AVATAR</label>
        </div>
        <div class="col-xs-12 col-md-4 col-lg-3 plr30">
            <div class="avatar-container">
                <label class="avatar">
                    <input type="file" accept="image/*">
                </label>
            </div>
            <button type="submit" name="submit-btn">SUBMIT</button>
        </div>
        <div class="col-xs-12 col-md-8 col-lg-9">
            <small class="text-warning">Avatar is updated seperately from the rest of the form.</small>
        </div>
    </div>
</form>

//忽略這些文本,這些只是為了使描述足夠長以便提交。 敏捷的棕色狐狸跳過了河岸附近的那只懶狗。

因此,我找到了一種方法,如果有人能更好地做到這一點,請告訴我。

我已經在root / modules / cus_avatar / controllers / front /中創建了一個名為“ default.php”的控制器。 該控制器將處理后期處理,因此我不得不使用getModuleLink將表單操作鏈接到該控制器。

在我的根目錄/模塊/cus_avatar/views/templates/hook/uploader.tpl中:

//first parameter is the module name, second is the controller name.
<form action="$link->getModuleLink('cus_avatar', 'default')" method="post">

在我的root / modules / cus_avatar / controllers / front / default.php中:

include_once(dirname(__FILE__).'../../../cus_avatar.php');

class cus_avatarDefaultModuleFrontController extends ModuleFrontController
{
    public function __construct()
    {
        parent::__construct();

        $this->context = Context::getContext();
    }

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

        if(isset($_POST['submit-btn']))
        {
//HANDLE THE POST/UPLOAD PROCESS HERE

        }
// after handling the post process imidiately kill the page to reduce further loaading and then redirect the page back to the page where you originally is, in my case it's index.php?controller=identity.
        die(Tools::redirect('index.php?controller=identity'));
    }
}

然后在我的cus_avatar.php上刪除此部分:

    public function hookDisplayEpAvatar($params)
    {
// remove the if condition now since it has no use. 
            if(isset($_POST['submit_avatar']))
            {
                // THIS CODE DOESNT SEEM TO WORK
                var_dump("HELLO WORLD!");
                die();
            }

            return $this->display(__FILE__, 'views/templates/hook/uploader.tpl');
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM