It works great, but how do you write data to the frontend? Thank you
Edit History
On 9/2/2019 at 9:45 AM, direwald said:Hello, continuation from my previous topic, I'm now stuck on using the Symphony framework to get my custom file uploaded on the server. Using a module, I'm using the formBuilder to create the file input on category page. I'm not quite sure how to proceed from here though. I tried to follow this (https://symfony.com/doc/3.4/controller/upload_file.html) Symfony guide, but couldn't quite apply it to the Prestashop system.
I think I should be using the UploadedFile component in my module to handle this, but I've no experience with Symfony and I'm not sure how to properly implement it into my module.
Below is my module file, any help would be greatly appreciated
<?php if (!defined('_PS_VERSION_')) { exit; } use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\Extension\Core\Type\NumberType; use Symfony\Component\Validator\Constraints\File; class CustomCategoryFields extends Module { public function __construct() { $this->name = 'customcategoryfields'; $this->tab = 'administration'; $this->version = '1.0.0'; $this->author = 'RV'; $this->need_instance = 0; $this->ps_versions_compliancy = [ 'min' => '1.7', 'max' => _PS_VERSION_ ]; $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('Custom Admin Category Fields'); $this->description = $this->l('Adds custom fields to admin category page'); $this->confirmUninstall = $this->l('Are you sure you want to uninstall?'); if (!Configuration::get('customcategoryfields')) { $this->warning = $this->l('No name provided'); } } public function install() { return parent::install() && $this->_installSql() && $this->registerHook('actionCategoryFormBuilderModifier') && $this->registerHook('actionAfterCreateCategoryFormHandler') && $this->registerHook('actionAfterUpdateCategoryFormHandler'); } public function uninstall() { if (!parent::uninstall() || !$this->_unInstallSql() || !Configuration::deleteByName('customcategoryfields') ) { return false; } return true; } protected function _installSql() { $sqlInstallLang = 'CREATE TABLE '._DB_PREFIX_.'category_custom_fields ( id_category int(10) NULL, image varchar(255) NULL, img_placement int(10) NULL, img_name varchar(32) NULL, price_impact decimal(20,6) NULL)'; $returnSqlLang = Db::getInstance()->execute($sqlInstallLang); return $returnSqlLang; } protected function _unInstallSql() { $sqlInstallLang = 'DROP TABLE '._DB_PREFIX_.'category_custom_fields'; $returnSqlLang = Db::getInstance()->execute($sqlInstallLang); return $returnSqlLang; } public function hookActionCategoryFormBuilderModifier($params) { $formBuilder = $params['form_builder']; $formBuilder->add('image', FileType::class, [ 'label' => $this->getTranslator()->trans('Image', [], 'Modules.customcategoryfields'), 'required' => false, 'mapped' => false, 'constraints' => [ new File([ 'mimeTypes' => [ 'image/png', ], 'mimeTypesMessage' => 'Obrázek musí být ve formátu PNG', ]) ] ]); $formBuilder->add('img_name', TextType::class, [ 'label' => $this->getTranslator()->trans('Image name', [], 'Modules.customcategoryfields'), 'required' => false, 'data' => $this->getImgName($params['id']) ]); $formBuilder->add('img_placement', NumberType::class, [ 'label' => $this->getTranslator()->trans('Image placement', [], 'Modules.customcategoryfields'), 'required' => false, 'data' => $this->getImgPlacement($params['id']) ]); $formBuilder->add('price_impact', TextType::class, [ 'label' => $this->getTranslator()->trans('Price impact', [], 'Modules.customcategoryfields'), 'required' => false, 'data' => $this->getPriceImpact($params['id']) ]); $formBuilder->setData($params['data']); } public function hookActionAfterCreateCategoryFormHandler($params) { $this->updateCategoryFields($params); } public function hookActionAfterUpdateCategoryFormHandler($params) { $this->updateCategoryFields($params); } public function updateCategoryFields($params) { file_put_contents('../modules/customcategoryfields/test_log.txt', var_dump($_FILES)); $idGet = 'SELECT id_category FROM '._DB_PREFIX_.'category_custom_fields WHERE id_category = '.$params['id']; if (Db::getInstance()->getValue($idGet)) { $sql = 'UPDATE '._DB_PREFIX_.'category_custom_fields SET img_placement = '.$params['form_data']['img_placement'].', img_name = "'.$params['form_data']['img_name'].'", price_impact = '.$params['form_data']['price_impact'].' WHERE id_category = '.$params['id']; } else { $sql = 'INSERT INTO '._DB_PREFIX_.'category_custom_fields (id_category,img_placement,img_name,price_impact) VALUES ('.$params['id'].', '.$params['form_data']['img_placement'].', "'.$params['form_data']['img_name'].'", '.$params['form_data']['price_impact'].')'; } Db::getInstance()->execute($sql); } public function getImgName($id_category) { $sqlName = 'SELECT img_name FROM '._DB_PREFIX_.'category_custom_fields WHERE id_category = '.$id_category; return Db::getInstance()->getValue($sqlName) ? Db::getInstance()->getValue($sqlName) : ''; } public function getImgPlacement($id_category) { $sqlPlace = 'SELECT img_placement FROM '._DB_PREFIX_.'category_custom_fields WHERE id_category = '.$id_category; return Db::getInstance()->getValue($sqlPlace) ? (int)Db::getInstance()->getValue($sqlPlace) : 1; } public function getPriceImpact($id_category) { $sqlPrice = 'SELECT price_impact FROM '._DB_PREFIX_.'category_custom_fields WHERE id_category = '.$id_category; return Db::getInstance()->getValue($sqlPrice) ? (float)Db::getInstance()->getValue($sqlPrice) : 0; } }
It works great, but how do you write data to the frontend? Thank you