Advertisement
cristo1985

colan_product3dupload.php

Nov 20th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. <?php
  2.  
  3. if (!defined('_PS_VERSION_')) {
  4. exit;
  5. }
  6.  
  7. class Colan_Product3DUpload extends Module
  8. {
  9. public function __construct()
  10. {
  11. $this->name = 'colan_product3dupload';
  12. $this->version = '1.0.0';
  13. $this->author = 'Colan Srl - Cristian Capannini & Andrea Lamponi';
  14. $this->tab = 'administration';
  15. $this->bootstrap = true;
  16. $this->displayName = $this->l('Product 3D Model Upload');
  17. $this->description = $this->l('Consente il caricamento e la visualizzazione di immagini 3D per i prodotti.');
  18. parent::__construct();
  19. }
  20.  
  21. public function install()
  22. {
  23. return parent::install() &&
  24. $this->registerHook('displayAdminProductsMainStepLeftColumnBottom') &&
  25. $this->registerHook('actionProductSave') && // Cambiato l'hook
  26. $this->registerHook('displayProductAdditionalInfo') &&
  27. $this->create3DModelTable(); // Crea la tabella personalizzata
  28. }
  29.  
  30. private function create3DModelTable()
  31. {
  32. $sql = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'product_3d_models` (
  33. `id_model3d` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  34. `id_product` INT(10) UNSIGNED NOT NULL,
  35. `embed_3d_model` TEXT DEFAULT NULL,
  36. PRIMARY KEY (`id_model3d`),
  37. INDEX (`id_product`),
  38. FOREIGN KEY (`id_product`) REFERENCES `' . _DB_PREFIX_ . 'product`(`id_product`) ON DELETE CASCADE
  39. ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';
  40. return Db::getInstance()->execute($sql);
  41. }
  42.  
  43. public function uninstall()
  44. {
  45. return parent::uninstall() && $this->delete3DModelTable();
  46. }
  47.  
  48. private function delete3DModelTable()
  49. {
  50. $sql = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'product_3d_models`';
  51. return Db::getInstance()->execute($sql);
  52. }
  53.  
  54. public function hookDisplayAdminProductsMainStepLeftColumnBottom($params)
  55. {
  56. $productId = (int)$params['id_product'];
  57. $model3dUrl = '';
  58.  
  59. if ($productId) {
  60. // Recupera l'URL 3DModel dalla tabella personalizzata per il prodotto specifico
  61. $model3dUrl = Db::getInstance()->getValue('
  62. SELECT `embed_3d_model`
  63. FROM `' . _DB_PREFIX_ . 'product_3d_models`
  64. WHERE `id_product` = ' . (int)$productId
  65. );
  66. }
  67.  
  68. $this->context->smarty->assign([
  69. 'embed_3d_model' => $model3dUrl,
  70. 'product_id' => $productId,
  71. ]);
  72.  
  73. return $this->display(__FILE__, 'views/templates/admin/model_3d_upload.tpl');
  74. }
  75.  
  76. public function hookActionProductSave($params)
  77. {
  78. $product = $params['product'];
  79. $productId = (int)$product->id;
  80.  
  81. // Recupera il codice HTML e prepara per il salvataggio
  82. $model3dHtml = Tools::getValue('embed_3d_model');
  83. $model3dHtmlEscaped = htmlspecialchars($model3dHtml, ENT_QUOTES, 'UTF-8'); // Mantiene i tag HTML
  84.  
  85. if ($productId) {
  86. if ($model3dHtmlEscaped) {
  87. $query = '
  88. INSERT INTO `' . _DB_PREFIX_ . 'product_3d_models` (`id_product`, `embed_3d_model`)
  89. VALUES (' . (int)$productId . ', "' . pSQL($model3dHtmlEscaped) . '")
  90. ON DUPLICATE KEY UPDATE `embed_3d_model` = "' . pSQL($model3dHtmlEscaped) . '"
  91. ';
  92.  
  93. // Inserisce o aggiorna l'HTML 3D per il prodotto
  94. $result = Db::getInstance()->execute($query);
  95.  
  96. // Log per il debug
  97. file_put_contents(dirname(__FILE__)."/logs.txt", "QUERY: ".$query.PHP_EOL.PHP_EOL.PHP_EOL, FILE_APPEND | LOCK_EX);
  98.  
  99. if (!$result) {
  100. throw new PrestaShopException('Errore nel salvataggio del 3DModel: ' . Db::getInstance()->getMsgError());
  101. }
  102. } else {
  103. Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'product_3d_models` WHERE `id_product` = ' . (int)$productId);
  104. }
  105. }
  106. }
  107.  
  108.  
  109. public function hookDisplayProductAdditionalInfo($params)
  110. {
  111. $product = $params['product'];
  112. $productId = (int)$product->id; // Ottieni l'ID prodotto dall'oggettos
  113.  
  114. // Sanitize il productId prima di inserirlo nella query
  115. $query = "SELECT embed_3d_model FROM " . _DB_PREFIX_ . "product_3d_models WHERE id_product = " . (int)$productId;
  116.  
  117. // Recupera l'URL del video dalla query
  118. $model3dUrl = Db::getInstance()->getValue($query);
  119.  
  120. if ($model3dUrl) {
  121. $this->context->smarty->assign('embed_3d_model', $model3dUrl);
  122. return $this->display(__FILE__, 'views/templates/front/product_3d_model.tpl');
  123. }
  124. }
  125.  
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement