Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (!defined('_PS_VERSION_')) {
- exit;
- }
- class Colan_Product3DUpload extends Module
- {
- public function __construct()
- {
- $this->name = 'colan_product3dupload';
- $this->version = '1.0.0';
- $this->author = 'Colan Srl - Cristian Capannini & Andrea Lamponi';
- $this->tab = 'administration';
- $this->bootstrap = true;
- $this->displayName = $this->l('Product 3D Model Upload');
- $this->description = $this->l('Consente il caricamento e la visualizzazione di immagini 3D per i prodotti.');
- parent::__construct();
- }
- public function install()
- {
- return parent::install() &&
- $this->registerHook('displayAdminProductsMainStepLeftColumnBottom') &&
- $this->registerHook('actionProductSave') && // Cambiato l'hook
- $this->registerHook('displayProductAdditionalInfo') &&
- $this->create3DModelTable(); // Crea la tabella personalizzata
- }
- private function create3DModelTable()
- {
- $sql = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'product_3d_models` (
- `id_model3d` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
- `id_product` INT(10) UNSIGNED NOT NULL,
- `embed_3d_model` TEXT DEFAULT NULL,
- PRIMARY KEY (`id_model3d`),
- INDEX (`id_product`),
- FOREIGN KEY (`id_product`) REFERENCES `' . _DB_PREFIX_ . 'product`(`id_product`) ON DELETE CASCADE
- ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';
- return Db::getInstance()->execute($sql);
- }
- public function uninstall()
- {
- return parent::uninstall() && $this->delete3DModelTable();
- }
- private function delete3DModelTable()
- {
- $sql = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'product_3d_models`';
- return Db::getInstance()->execute($sql);
- }
- public function hookDisplayAdminProductsMainStepLeftColumnBottom($params)
- {
- $productId = (int)$params['id_product'];
- $model3dUrl = '';
- if ($productId) {
- // Recupera l'URL 3DModel dalla tabella personalizzata per il prodotto specifico
- $model3dUrl = Db::getInstance()->getValue('
- SELECT `embed_3d_model`
- FROM `' . _DB_PREFIX_ . 'product_3d_models`
- WHERE `id_product` = ' . (int)$productId
- );
- }
- $this->context->smarty->assign([
- 'embed_3d_model' => $model3dUrl,
- 'product_id' => $productId,
- ]);
- return $this->display(__FILE__, 'views/templates/admin/model_3d_upload.tpl');
- }
- public function hookActionProductSave($params)
- {
- $product = $params['product'];
- $productId = (int)$product->id;
- // Recupera il codice HTML e prepara per il salvataggio
- $model3dHtml = Tools::getValue('embed_3d_model');
- $model3dHtmlEscaped = htmlspecialchars($model3dHtml, ENT_QUOTES, 'UTF-8'); // Mantiene i tag HTML
- if ($productId) {
- if ($model3dHtmlEscaped) {
- $query = '
- INSERT INTO `' . _DB_PREFIX_ . 'product_3d_models` (`id_product`, `embed_3d_model`)
- VALUES (' . (int)$productId . ', "' . pSQL($model3dHtmlEscaped) . '")
- ON DUPLICATE KEY UPDATE `embed_3d_model` = "' . pSQL($model3dHtmlEscaped) . '"
- ';
- // Inserisce o aggiorna l'HTML 3D per il prodotto
- $result = Db::getInstance()->execute($query);
- // Log per il debug
- file_put_contents(dirname(__FILE__)."/logs.txt", "QUERY: ".$query.PHP_EOL.PHP_EOL.PHP_EOL, FILE_APPEND | LOCK_EX);
- if (!$result) {
- throw new PrestaShopException('Errore nel salvataggio del 3DModel: ' . Db::getInstance()->getMsgError());
- }
- } else {
- Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'product_3d_models` WHERE `id_product` = ' . (int)$productId);
- }
- }
- }
- public function hookDisplayProductAdditionalInfo($params)
- {
- $product = $params['product'];
- $productId = (int)$product->id; // Ottieni l'ID prodotto dall'oggettos
- // Sanitize il productId prima di inserirlo nella query
- $query = "SELECT embed_3d_model FROM " . _DB_PREFIX_ . "product_3d_models WHERE id_product = " . (int)$productId;
- // Recupera l'URL del video dalla query
- $model3dUrl = Db::getInstance()->getValue($query);
- if ($model3dUrl) {
- $this->context->smarty->assign('embed_3d_model', $model3dUrl);
- return $this->display(__FILE__, 'views/templates/front/product_3d_model.tpl');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement