簡體   English   中英

第一個 Prestashop 1.7 模塊中的安全令牌

[英]SECURITY TOKEN in first Prestashop 1.7 module

我已經嘗試了幾天來創建一個模塊。 這將是一個簡單的過程,將一些變量添加到不同的表中,並在您從下拉列表中選擇 select 時將它們顯示在列表中。 我只是還在管理員中,我稍后再創建前面,

我遵循易於遵循的教程。 首先將 sql 復制到數據庫。

CREATE TABLE pasta (
  `id` INT NOT NULL AUTO_INCREMENT,
  `sku` VARCHAR(255) NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `description` TEXT,
  `id_pasta_category` INT NOT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE = InnoDB;

然后我復制 ObjectModel class /override/classes/fc_pasta/Pasta.php

<?php
class Pasta extends ObjectModel {
  public $id; // fields are mandatory for create/update
  public $sku;
  public $name;
  public $created;
  public $category;
  public $id_pasta_category;
  public static $definition = [
    'table' => 'pasta',
    'primary' => 'id',
    'fields' => [
      'sku' =>  ['type' => self::TYPE_STRING, 'validate' => 'isAnything', 'required'=>true],
      'name' =>  ['type' => self::TYPE_STRING, 'validate' => 'isAnything', 'required'=>true],
      'description' =>  ['type' => self::TYPE_HTML, 'validate' => 'isAnything',],
      'created' =>  ['type' => self::TYPE_DATE, 'validate' => 'isDateFormat'],
      'id_pasta_category' => ['type'=>self::TYPE_INT, 'validate'=>'isUnsignedInt','required'=>true,],
    ],
  ];
}

然后我復制模塊/modules/fc_pasta/fc_pasta.php

<?php
if (!defined('_PS_VERSION_')) {exit;}
class Fc_Pasta extends Module {
  public function __construct() {
      $this->name = 'fc_pasta'; // must match folder & file name
      $this->tab = 'administration';
      $this->version = '1.0.0';
      $this->author = 'Florian Courgey';
      $this->bootstrap = true; // use Bootstrap CSS
      parent::__construct();
      $this->displayName = $this->l('PrestaShop Module by FC');
      $this->description = $this->l('Improve your store by [...]');
      $this->ps_versions_compliancy = ['min' => '1.7', 'max' => _PS_VERSION_];
      // install Tab to register AdminController in the database
      $tab = new Tab();
      $tab->class_name = 'AdminPasta';
      $tab->module = $this->name;
      $tab->id_parent = (int)Tab::getIdFromClassName('DEFAULT');
      $tab->icon = 'settings_applications';
      $languages = Language::getLanguages();
      foreach ($languages as $lang) {
          $tab->name[$lang['id_lang']] = $this->l('FC Pasta Admin controller');
      }
      $tab->save();
  }
}

之后我創建了 AdminPastaController

<?php
require_once _PS_ROOT_DIR_.'/override/classes/fc_pasta/Pasta.php';

class AdminPastaController extends ModuleAdminController {
  public function __construct(){
      parent::__construct();
        
        // Base
        $this->bootstrap = true; // use Bootstrap CSS
        $this->table = 'pasta'; // SQL table name, will be prefixed with _DB_PREFIX_
        $this->identifier = 'id'; // SQL column to be used as primary key
        $this->className = 'Pasta'; // PHP class name
        $this->allow_export = true; // allow export in CSV, XLS..

        // List records
        $this->_defaultOrderBy = 'a.sku'; // the table alias is always `a`
        $this->_defaultOrderWay = 'ASC';
        $this->_select = 'a.name as `pastaName`, cl.name as `categoryName`';
        $this->_join = '
            LEFT JOIN `'._DB_PREFIX_.'category` cat ON (cat.id_category=a.id_pasta_category)
            LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cat.id_category=cl.id_category and cat.id_shop_default=cl.id_shop)';
        $this->fields_list = [
            'id' => ['title' => 'ID','class' => 'fixed-width-xs'],
            'sku' => ['title' => 'SKU'],
            'pastaName' => ['title' => 'Name', 'filter_key'=>'a!name'], // filter_key mandatory because "name" is ambiguous for SQL
            'categoryName' => ['title' => 'Category', 'filter_key'=>'cl!name'], // filter_key mandatory because JOIN
            'created' => ['title' => 'Created','type'=>'datetime'],
        ];

        // Read & update record
        $this->addRowAction('details');
        $this->addRowAction('edit');
        $categories = Category::getCategories($this->context->language->id, $active=true, $order=false); // [0=>[id_category=>X,name=>Y]..]
        $categories = [['id'=>1, 'display'=> 'abc'], ['id'=>2, 'display'=>'def']];
        $this->fields_form = [
            'legend' => [
            'title' => 'Pasta',
            'icon' => 'icon-list-ul'
        ],
      'input' => [
        ['type'=>'html','html_content'=>'<div class="alert alert-info">Put here any info content</div>'],
    ['name'=>'id_xxx','label'=>'XXX','type'=>'select',
      'options'=>[ 'query'=>$categories,
        'id'=>'id', // use the key id as the <option> value
        'name'=> 'display', // use the key display as the <option> title
      ]
    ],
        ['name'=>'name','type'=>'text','label'=>'Name','required'=>true],
        ['name'=>'description','type'=>'textarea','label'=>'Description',],
        ['name'=>'created','type'=>'datetime','label'=>'Created',],
        ['name'=>'id_pasta_category','label'=>'Category','type'=>'select','required'=>true,'class'=>'select2',
          'options'=>[ 'query'=>$categories,
            'id'=>'id_category', // use the key "id_category" as the <option> value
            'name'=> 'name', // use the key "name" as the <option> title
        ]],
      ],
      'submit' => [
        'title' => $this->trans('Save', [], 'Admin.Actions'),
      ]
    ];
  }
   protected function getFromClause() {
     return str_replace(_DB_PREFIX_, '', parent::getFromClause());
 }
}

一切都差不多,但每次我更新頁面時,它都會創建一個新菜單:FC Pasta Admin controller,而不是 1,我現在有 25 個。

我找不到原因。

還有一件事,我在模塊中所做的一切我都得到了無效的安全令牌

我真的很想為 PS 1.7 創建模塊,但我真的很想嘗試。

您正在模塊構造函數中創建並保存一個新的后台選項卡,因此每次實例化模塊 object 時都會創建一個新選項卡。

您需要在模塊的 install() 方法中移動選項卡創建登錄名,以便在第一個模塊安裝期間僅創建一次選項卡。

暫無
暫無

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

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