2016-09-02 1 views
0

Ich habe nach Dokumentation zu diesem Problem gesucht. Aber fand keinen Wunsch.Prestashop 1.6: Erstellen und Senden von benutzerdefinierten Formular mit Admin-Controller

Ich weiß, wie man Admin-Vorlage mit Admin-Controller laden. ich ein Formular mit

<form action="{$link->getModuleLink('pushnotification', 'AdminPushNotification', [], true)|escape:'html'}" method="post"> 

Here "pushnotification" geschaffen haben, ist mein Modulnamen und "AdminPushNotification" name mein Admin-Controller ist.

wenn ich einreichen getroffen es http://example.com/en/module/pushnotification/AdminPushNotification URL geht, die somit keine gültige URL ist 404 Seite

bekommen Aber ich möchte das Formular abzuschicken und in der gleichen Seite zu bleiben.

Ich weiß nicht, wie Sie die Einreichung von Formularen in Admin Controller einreichen und bearbeiten.

Vielen Dank im Voraus

Struktur Meine Moduldatei: pushnotification Controller Admin AdminPushnotification.php Ansichten Vorlagen Admin pushnotificationform.tpl

Code My Admin Controller:

<?php 
 

 
class AdminPushNotificationController extends ModuleAdminControllerCore 
 
{ 
 
    public function __construct() { 
 
     $this->bootstrap = true; 
 
     parent::__construct(); 
 
    } 
 

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

 

 
     $smarty = $this->context->smarty; 
 
     $this->context->smarty->assign(array(
 
      'msg' => "", 
 
      'title' => "" 
 
     )); 
 
     $content = $smarty->fetch(_PS_MODULE_DIR_ . 'pushnotification/views/templates/admin/pushnotificationform.tpl'); 
 
     $this->context->smarty->assign(array(
 
      'content' => $this->content . $content 
 
     )); 
 
    } 
 

 
    public function postProcess() 
 
    { 
 
     if (Tools::isSubmit('sendTokenMessage')) 
 
     { 
 
      $title = Tools::getValue('title'); 
 
      $msg = Tools::getValue('message'); 
 

 
      $content = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'pushnotification/views/templates/admin/pushnotificationform.tpl'); 
 
      $this->context->smarty->assign(array(
 
       'content' => $this->content . $content, 
 
       'msg' => $msg, 
 
       'title' => $title 
 
      )); 
 
     } 
 
    } 
 

 
    public function sendMessage($title,$msg) 
 
    { 
 

 
     $sql = " Select Token From Token"; 
 

 
     $result = DB::getInstance()->execute($sql); 
 

 
     foreach($result as $row) { 
 
      $message = array("title" => $title, "text"=> $msg); 
 
      $message_status = send_notification($row["token"], $message); 
 
     } 
 
     return $message_status; 
 
    } 
 
}

und meine pushnotificationform.tpl Code:

<div> 
 
    {if $msg!=""} 
 
     <p>{$msg}</p> 
 
    {/if} 
 
</div> 
 
<form action="{$link->getAdminLink('pushnotification', 'AdminPushNotification', [], true)|escape:'html'}" method="post"> 
 
    <div style="padding-bottom:10px;" class="form-group"> 
 
     <label for="" class="control-label col-lg-6">Title</label> 
 
     <div class="col-lg-6"> 
 
      <input class="form-control input-lg" type="text" name="title" /> 
 
     </div> 
 
    </div> 
 
    <div class="form-group"> 
 
     <label for="" class="control-label col-lg-6">Message</label> 
 
     <div class="col-lg-6"> 
 
      <textarea name="message" class="form-control" style="min-width: 100%"></textarea> 
 
     </div> 
 
    </div> 
 
    <input type="submit" name="sendTokenMessage" value="send" class="btn btn-default" /> 
 
</form>

Antwort

0

$link->getModuleLink() einen Link zu einem Controllermodul Front abruft.

Sie müssen $link->getAdminLink('AdminPushNotification') verwenden, um eine Verknüpfung zu Ihrem Administrator-Controller zu erstellen.

Edit:

Sie sind die $link->getAdminLink() Methode falsch aufrufen. Alles was Sie brauchen in Ihrem Formular zu tun ist:

<form action="{$link->getAdminLink('AdminPushNotification')|escape:'htmlall':'utf-8'}" method="post"> 

getAdminLink() Methode von getModuleLink() unterscheidet, da es nur einen String-Parameter akzeptiert, die den Namen des Admin-Modul-Controller ist AdminPushNotification.

Sie können die Klasse classes/Link.php für verschiedene Methoden zum Erstellen von Links und deren Parameter überprüfen.

Edit2:

Sie zuweisen Inhalt zweimal - in postProcess() und initContent() die Fehler wirft.

Verwenden Sie postProcess(), um etwas Hintergrundarbeit mit Eingaben dh zu tun. Ihre send_message($title, $msg) Methode oder Validierung.

Verwenden initContent() Smarty Variablen/Vorlagen usw. setzen

So würde ich diese beiden Methoden wie diese Refactoring:

protected $msg = ""; 
protected $title = ""; 

public function postProcess() 
{ 
    if (Tools::isSubmit('sendTokenMessage')) 
    { 
     // store inputs to object properties, 
     // here they can be validated and reused in initContent() 
     $this->title = Tools::getValue('title'); 
     $this->msg = Tools::getValue('message'); 

     // perhaps do some inputs validation here 

     $this->send_message($this->title, $this->msg); 
    } 
} 

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

    $smarty = $this->context->smarty; 
    // msg and title variables must be assigned before fetching template 
    // otherwise they are not recognized in template 
    $smarty->assign(array(
     'msg' => $this->msg, 
     'title' => $this->title 
    )); 
    $content = $smarty->fetch(_PS_MODULE_DIR_ . 'pushnotification/views/templates/admin/pushnotificationform.tpl'); 
    $smarty->assign(array(
     'content' => $this->content . $content 
    )); 
} 

Natürlich auch gegen XSS zu entkommen $msg und $title in Vorlagen erinnern zu schützen und auch Strings bereinigen, um SQL-Injektionen zu verhindern.

+0

Ich habe zu $ ​​link-> getAdminLink geändert. Jetzt 404 Seite bekommen. Kannst du mir etwas über die Handhabung des Formulars im Admin-Controller geben? –

+0

öffentliche Funktion postProcess() { if (Extras :: isSubmit ('sendTokenMessage')) { $ title = Extras :: getValue ('title'); $ msg = Extras :: getValue ('Nachricht'); $ content = $ this-> context-> smarty-> holen (_PS_MODULE_DIR_. 'Pushnotification/views/templates/admin/pushnotificationform.tpl'); $ this-> context-> smarty-> zuweisen (array ( 'content' => $ this-> content. $ Content, 'msg' => $ msg, 'title' => $ title )) ; } } –

+0

Leider glaube ich nicht, es gibt etwas anderes als [dies] (http://blog.belvg.com/how-to-use-the-class-admincontroller-in-prestashop.html) aber wenn Sie können Ihren gesamten Controller-Code zeigen, ich könnte Ihnen weiterhelfen. – TheDrot

Verwandte Themen