需要在使用帮助器表单将其插入数据库之前更改字段值

时间:2016-10-12 14:25:55

标签: database tinymce prestashop helper

我用我的数据库创建一个Helper列表,在pu字段“cont”中使用TinyMCE,但是当我将值写入db html元素而不是商店时,我如何用html代码编写我的字段? 如果我使用db类prestashop我可以使用htmlentities函数来存储我的数据,但我不明白如何在我的fields_form数组中使用它

Db::getInstance()->autoExecute(_DB_PREFIX_.'pdf_files', array(
                'content' => pSQL(htmlentities($content))
            ),
                'INSERT');

这段代码就像我想要的那样,在我的代码中,我不知道我在哪里可以使用

htmlentities($content)

这是我的对象模型和我的控制器

<?php
class PdfData extends ObjectModel{
    public $id;
    public $name;
    public $cont;
    public $head_1;
    public $head_2;
    public $head_3;
    public $head_4;
    public $head_5;
    public $inst;
    public $facebook;


    /**
     * @see ObjectModel::$definition
     */
    public static $definition = array(
        'table' => 'pdf_files',
        'primary' => 'id',
        'fields' => array(
            'name' => array(
                'type' => 'varchar',
                'validate' => 'isGenericName',
                'required' => true,
                'class' => 'lg'
            ),
            'head_1' => array(
                'type' => 'varchar',
                'validate' => 'isGenericName',
                'required' => false,
                'class' => 'lg'
            ),
            'head_2' => array(
                'type' => 'varchar',
                'validate' => 'isGenericName',
                'required' => false,
                'class' => 'lg'
            ),
            'head_3' => array(
                'type' => 'varchar',
                'value' => 'lal',
                'validate' => 'isGenericName',
                'required' => false,
                'class' => 'lg'
            ),
            'head_4' => array(
                'type' => 'varchar',
                'validate' => 'isGenericName',
                'required' => false,
                'class' => 'lg'
            ),
            'head_5' => array(
                'type' => 'varchar',
                'validate' => 'isGenericName',
                'required' => false,
                'class' => 'lg'
            ),
            'inst' => array(
                'type' => 'bool',
                //'validate' => 'isGenericName',
                'required' => false,
                'class' => 'lg'
            ),
            'facebook' => array(
                'type' => 'bool',
                //'validate' => 'isGenericName',
                'required' => false,
                'class' => 'lg'
            ),
            'cont' => array(
                'type' => 'text',
                'validate' => 'isString',
                'required' => false,
                'class' => 'lg'
            ),
        ),
    );
}

class PdfListController extends ModuleAdminController{
    const CONTROLLER_NAME = 'PdfList';
    public $displayName = 'PDFer';

    public function __construct(){
        $this->bootstrap = true;
        $this->className = 'PdfData';
        $this->table = 'pdf_files';
            //Tools::getValue('id_product');
        $this->fields_list = array(
            'id' => array(
                'title' => 'ID',
                'width' => 'auto',
                'type' => 'int'
            ),
            'name' => array(
                'title' => $this->l('Name'),
                'width' => 'auto',
                'type' => 'varchar'
            ),
            'cont'=>array(
                'title' => $this->l('content'),
                'width' => 'auto',
                'type' => 'varchar'
            )

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

        );

        parent::__construct();
        $this->callAction();
    }
    private function callAction() {
        $actionName = 'process' . ucfirst(trim($_GET['action']));

        if(method_exists($this, $actionName)) {
            $this->$actionName();
        }
    }

    public function renderList()
    {
        $this->addRowAction('download');
        $this->addRowAction('delete');
        $this->addRowAction('edit');

        return parent::renderList();

    }
    public function processDelete()
    {
        $sql= "DELETE FROM `"._DB_PREFIX_."pdf_files` WHERE `ps_pdf_files`.`id` = ". Tools::getValue('id');
        $return = Db::getInstance()->execute($sql);
        return $return;
    }
    public function processDownload()
    {
        //var_dump(Configuration::get(pdfer::HEAD_3));die;
        require_once _PS_MODULE_DIR_ . 'pdfer/classes/pdf/HTMLTemplatePdf.php';
        $sql = "SELECT * FROM "._DB_PREFIX_."pdf_files WHERE id = " . Tools::getValue('id');
        $pdf = new stdClass();
        $pdfData = Db::getInstance()->getRow($sql);
        $pdf->cont = html_entity_decode($pdfData["cont"]);
        $pdf->name = $pdfData["name"];
        $pdf->head_1 = $pdfData["head_1"];
        $pdf->head_2 = $pdfData["head_2"];
        $pdf->head_3 = $pdfData["head_3"];
        $pdf->head_4 = $pdfData["head_4"];
        $pdf->head_5 = $pdfData["head_5"];
        //$pdf->header = html_entity_decode($pdfData["head"]);
        $pdf->path_logo = _PS_MODULE_DIR_.'pdfer/views/templates/images/header_logo.jpg';
        $pdf->path_footer = _PS_MODULE_DIR_.'pdfer/views/templates/images/footer_signature.jpg';
        $pdf->path_facebook = _PS_MODULE_DIR_.'pdfer/views/templates/images/facebook.jpg';
        $pdf->path_inst = _PS_MODULE_DIR_.'pdfer/views/templates/images/inst.jpg';
        $pdf->include_inst = (string)Configuration::get(pdfer::CHECK_IMAGE_INST);
        $pdf->include_facebook = (string)Configuration::get(pdfer::CHECK_IMAGE_FACEBOOK);
        $pdfGen = new PDF($pdf, 'Pdf', $this->context->smarty);

        $content = $pdfGen->render(false);

        header("ContentType: application/pdf");
        header("Content-disposition: attachment; filename=\"$pdf->name.pdf\"");
        header("Content-Length: " . strlen($content));
        echo $content;
        exit;
    }
    public function displayDownloadLink($token = null, $id, $name = null)
    {
        $tpl = $this->createTemplate('helpers/list/list_action_view.tpl');
        if (!array_key_exists('Download', self::$cache_lang))
            self::$cache_lang['Download'] = $this->l('Download');

        $tpl->assign(array(
            'href' => static::$currentIndex .'&'.$this->identifier.'='.$id.'&action=download&'.$this->table.'&token='.($token != null ? $token : $this->token),
            'action' => self::$cache_lang['Download'],
            'id' => $id
        ));

        return $tpl->fetch();
    }


    /**
     * @return string
     */
    public function renderForm() {
        // Building the Add/Edit form

        $this->fields_value['head_1'] = (string)Configuration::get(pdfer::HEAD_1);
        $this->fields_value['head_2'] = (string)Configuration::get(pdfer::HEAD_2);
        $this->fields_value['head_3'] = (string)Configuration::get(pdfer::HEAD_3);
        $this->fields_value['head_4'] = (string)Configuration::get(pdfer::HEAD_4);
        $this->fields_value['head_5'] = (string)Configuration::get(pdfer::HEAD_5);

        $this->fields_form = array(
            'tinymce' => true,
            'legend' => array(
                'title' => $this->l('New Pdf')
            ),
            'input' => array(
                array(
                    'type' => 'text',
                    'label' => $this->l('Name'),
                    'name' => 'name',
                    'class' => 'lg',
                    'required' => true,
                    'desc' => $this->l('adadada'),
                    ),
                array(
                    'type' => 'textarea',
                    'label' => $this->l('Content'),
                    'name' => 'cont',
                    //'class' => 'lg',
                    'required' => false,
                    'autoload_rte' => true,
                ),
                array(
                    'type' => 'hidden',
                    'label' => $this->l('Content'),
                    'name' => 'head_1',
                    //'class' => 'lg',
                    'required' => false,
                    'autoload_rte' => true,
                ),
                array(
                    'type' => 'hidden',
                    'label' => $this->l('Content'),
                    'name' => 'head_2',
                    //'class' => 'lg',
                    'required' => false,
                    'autoload_rte' => true,
                ),
                array(
                    'type' => 'hidden',
                    'label' => $this->l('Content'),
                    'name' => 'head_3',
                    //'class' => 'lg',
                    'required' => false,
                    'autoload_rte' => true,
                ),
                array(
                    'type' => 'hidden',
                    'label' => $this->l('Content'),
                    'name' => 'head_4',
                    //'class' => 'lg',
                    'required' => false,
                    'autoload_rte' => true,
                ),
                array(
                    'type' => 'hidden',
                    'label' => $this->l('Content'),
                    'name' => 'head_5',
                    //'class' => 'lg',
                    'required' => false,
                    'autoload_rte' => true,
                ),
                array(
                    'type' => 'hidden',
                    'label' => $this->l('Content'),
                    'name' => 'inst',
                    //'class' => 'lg',
                    'required' => false,
                    'autoload_rte' => true,
                ),
                array(
                    'type' => 'hidden',
                    'label' => $this->l('Content'),
                    'name' => 'facebook',
                    //'class' => 'lg',
                    'required' => false,
                    'autoload_rte' => true,
                ),
            ),
                'submit' => array(
                    'title' => $this->l('Save'),
                    //'class' => 'button'
                )
    );
        return parent::renderForm();
    }
}

here's picture, in first i enter some text, 2 i edit, but there is text only

1 个答案:

答案 0 :(得分:0)

您不应该使用pSQL(htmlentities($ content))将内容插入数据库表。

Tye将其替换为Tools :: htmlentitiesUTF8($ content),同时保存HTML内容,并在从数据库中提取数据时使用Tools :: htmlentitiesDecodeUTF8($ content)。