Yeth Posted March 28, 2020 Share Posted March 28, 2020 He usado el código de un post del foro (Acá está el post de donde saqué el código) para crear un módulo que agregue un campo tipo archivo para subir una imagen, almacena el nombre del archivo y logra subir la imagen, mi problema es saber cómo hacer para mostrar la miniatura de la imagen en e formulario del admin, llevo todo el día partiéndome la cabeza en ello. ¿Saben cómo hacerlo?, acá está el código del módulo: <?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\Validator\Constraints\File; class Jh_CustomCategoryFields extends Module { public function __construct() { $this->name = 'jh_customcategoryfields'; $this->tab = 'administration'; $this->version = '1.0.0'; $this->author = 'RV'; // Original en: https://www.prestashop.com/forums/topic/1000477-custom-file-upload-on-symfony-admin-pages-176/ $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('jh_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('jh_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_name varchar(32) 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('Banner', [], 'Modules.jh_customcategoryfields'), 'required' => false, 'mapped' => false, 'constraints' => [ new File([ 'mimeTypes' => [ 'image/png', 'image/jpg', 'image/jpeg', ], 'mimeTypesMessage' => 'Formato de imagen permitido: png, jpg, jpeg', ]) ] ]); $formBuilder->add('img_name', TextType::class, [ 'label' => $this->getTranslator()->trans('Image name', [], 'Modules.jh_customcategoryfields'), 'required' => false, 'data' => $this->getImgName($params['id']) ]); $formBuilder->setData($params['data']); } public function hookActionAfterCreateCategoryFormHandler($params) { $this->updateCategoryFields($params); } public function hookActionAfterUpdateCategoryFormHandler($params) { $this->updateCategoryFields($params); } public function updateCategoryFields($params) { $id_category = $params['id']; // var_dump($_FILES['category']); $file_name = ''; if (isset($_FILES['category']['name']['image']) && isset($_FILES['category']['tmp_name']['image']) && !empty($_FILES['category']['tmp_name']['image'])) { $image_file = [ 'name' => $_FILES['category']['name']['image'], 'type' => $_FILES['category']['type']['image'], 'tmp_name' => $_FILES['category']['tmp_name']['image'], 'error' => $_FILES['category']['error']['image'], 'size' => $_FILES['category']['size']['image'] ]; if ($error = ImageManager::validateUpload($image_file, 4000000)) { return $error; } else { $ext = substr($image_file['name'], strrpos($image_file['name'], '.') + 1); $file_name = md5($image_file['name']).'.'.$ext; // echo( dirname(__FILE__).'/../../img'.DIRECTORY_SEPARATOR.$file_name ); die(); if (!move_uploaded_file($image_file['tmp_name'], dirname(__FILE__).'/../../img'.DIRECTORY_SEPARATOR.$file_name)) { return $this->displayError($this->trans('An error occurred while attempting to upload the file.', array(), 'Admin.Notifications.Error')); } } //$update_images_values = true; } // file_put_contents('../modules/jh_customcategoryfields/test_log.txt', print_r($_FILES,true)); $idGet = 'SELECT id_category FROM '._DB_PREFIX_.'category_custom_fields WHERE id_category = '.$id_category; if (Db::getInstance()->getValue($idGet)) { $sql = 'UPDATE '._DB_PREFIX_.'category_custom_fields SET img_name = "'.$params['form_data']['img_name'].'", image = "'.$file_name.'" WHERE id_category = '.$id_category; } else { $sql = 'INSERT INTO '._DB_PREFIX_.'category_custom_fields (id_category,image,img_name) VALUES ('.$id_category.', "'.$file_name.'", "'.$params['form_data']['img_name'].'")'; } 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) : ''; } } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now