name = 'imaxmultiean'; $this->tab = 'administration'; $this->path = _PS_MODULE_DIR_ . $this->name . '/'; $this->version = '1.7'; $this->author = 'Informax'; $this->need_instance = 0; $this->idManual = ''; $this->forceCheck = 0; $this->sufijo = self::prefijo; $this->prefijo = self::prefijo; parent::__construct(); $nombreModulo = Configuration::getGlobalValue($this->sufijo . 'NOMBRE_MODULO'); $descripcionModulo = Configuration::getGlobalValue($this->sufijo . 'DESCRIPCION_MODULO'); if ($nombreModulo) { $this->displayName = $nombreModulo; $this->description = $descripcionModulo; } else { $this->displayName = $this->l('Gestor de EANs Múltiples'); $this->description = $this->l('Permite añadir y gestionar múltiples códigos EAN para cada producto y combinación.'); } if (version_compare(_PS_VERSION_, '1.7.0.0 ', '>=')) { $this->versionPS = 17; $context = Context::getContext(); $this->idShop = $context->shop->id; $this->idLang = $context->language->id; } elseif (version_compare(_PS_VERSION_, '1.6.0.0 ', '>=')) { $this->versionPS = 16; $context = Context::getContext(); $this->idShop = $context->shop->id; $this->idLang = $context->language->id; } elseif (version_compare(_PS_VERSION_, '1.5.0.0 ', '>=')) { $this->versionPS = 15; $context = Context::getContext(); $this->idShop = $context->shop->id; $this->idLang = $context->language->id; } else { $this->_html .= $this->l("La version minima de funcionamiento para nuestros modulos es la 1.5"); } } public function install() { include(dirname(__FILE__).'/configuration.php'); foreach ($configuracion AS $indice => $valor) { if (!Configuration::updateGlobalValue($indice, $valor)) { return false; } } if (!parent::install()) return false; foreach ($hooks as $hook) { if (!$this->registerHook($hook)) { $this->_errors[] = $this->l('Ha fallado la instalacion del hook:').' '.$hook; return false; } } include(dirname(__FILE__).'/sql-install.php'); foreach ($sql as $s) { if (!Db::getInstance()->execute($s)) { $this->_errors[] = $this->l("Error al ejecutar").$s; return false; } } if (!$this->installTab()) { $this->_errors[] = $this->l('Error al instalar el tab'); return false; } return true; } public function uninstall() { if (!parent::uninstall()) { return false; } include(dirname(__FILE__).'/sql-unninstall.php'); foreach ($sql as $s) { if (!Db::getInstance()->execute($s)) { $this->_errors[] = $this->l("Error al ejecutar").$s; return false; } } if (!$this->uninstallTab()) { $this->_errors[] = $this->l('Error al eliminar el tab'); return false; } include(dirname(__FILE__).'/configuration.php'); foreach ($configuracion AS $indice => $valor) { if (Configuration::getGlobalValue($indice) !== FALSE) { if (!Configuration::deleteByName($indice)) { return false; } } } return true; } public function getContent() { $this->getTxtFiles(); $this->addCSS('css.css'); $this->addJS('SucesionTeclas.js'); $this->addJS('functions.js'); $this->addCSS('publi.css'); $this->_html .= $this->createHelpHeader(); if (!empty($_POST)) { $this->_html .= $this->_postProcess(); } $this->_displayForm(); $this->_html .= $this->getModuleFooter(); return $this->_html; } private function _postProcess() { $accion = Tools::getValue("accion"); $this->idTab = Tools::getValue("idTab"); $html = ""; switch ($accion) { case 'importacion': ini_set('auto_detect_line_endings', 1); $error = false; $file_import = dirname(__FILE__) . '/import/' . $_FILES['file']['name'] . '.' . date('Ymdhis') . '.csv'; if (move_uploaded_file($_FILES['file']['tmp_name'], $file_import)) { require_once dirname(__FILE__).'/clases/accesoDatos/iAccesoDatos.php'; switch ($this->detectarTipoArchivo($file_import)) { case self::ARCHIVO_CSV: require_once dirname(__FILE__).'/clases/accesoDatos/AccesoDatosCSV.php'; $acceso = new clases\accesoDatos\AccesoDatosCSV($file_import, 0, 1, ';'); break; case self::ARCHIVO_EXCEL: require_once dirname(__FILE__).'/clases/accesoDatos/AccesoDatosExcel.php'; require_once dirname(__FILE__).'/clases/accesoDatos/ChunkReadFilter.php'; $acceso = new clases\accesoDatos\AccesoDatosExcel($file_import, 0, 32768, 1); break; default: require_once dirname(__FILE__).'/clases/accesoDatos/AccesoDatosCSV.php'; $acceso = new clases\accesoDatos\AccesoDatosCSV($file_import, 0, 1, ';'); break; } while (($data = $acceso->obtenerFila()) !== FALSE) { $this->getFunciones()->storeEAN($data[0], $data[2], $data[1]); } unlink($file_import); } else { $error = true; } if(!$error) { $html .= $this->displayConfirmation($this->l('EANs cargados correctamente.')); } else { $html .= $this->displayError($this->l('Error al cargar os EANs.')); } break; case 'gestionLicencia': $this->forceCheck = 1; if (Configuration::updateGlobalValue(self::prefijo . 'LICENCIA', trim(Tools::getValue('licencia')))) { $html .= $this->displayConfirmation($this->l('Licencia guardada correctamente.')); } else { $html .= $this->displayError($this->l('Ha ocurrido un error al guardar la licencia.')); } break; case 'gestionPubli': Configuration::updateGlobalValue($this->sufijo . 'TXT_FILE', ''); $urlPubli = trim(Tools::getValue('urlPubli')); $tipoPubli = trim(Tools::getValue('namePubli')); $nombre = trim(Tools::getValue('nameDeveloper')); $urlEmpresa = trim(Tools::getValue('urlEmpresa')); $urlManual = trim(Tools::getValue('urlManual')); $urlSoporte = trim(Tools::getValue('urlSoporte')); $descripcionModulo = trim(Tools::getValue('descripcionModulo')); $nombreModulo = trim(Tools::getValue('nombreModulo')); if (Configuration::updateGlobalValue($this->sufijo . 'URL_TXT', $urlPubli) && Configuration::updateGlobalValue($this->sufijo . 'TIPO', $tipoPubli) && Configuration::updateGlobalValue($this->sufijo . 'URL_DEVELOPER', $urlEmpresa) && Configuration::updateGlobalValue($this->sufijo . 'URL_TICKETS', $urlSoporte) && Configuration::updateGlobalValue($this->sufijo . 'URL_MANUAL', $urlManual) && Configuration::updateGlobalValue($this->sufijo . 'NOMBRE_DEVELOPER', $nombre) && Configuration::updateGlobalValue($this->sufijo . 'DESCRIPCION_MODULO', $descripcionModulo) && Configuration::updateGlobalValue($this->sufijo . 'NOMBRE_MODULO', $nombreModulo)) { Configuration::updateGlobalValue($this->sufijo . 'DESCARGA_ARCHIVO', 100001); $html .= $this->displayConfirmation('Datos guardados correctamente.'); $this->installTabNewData(); } else { $html .= $this->displayError('Ha ocurrido un error al guardar los datos de desarrollador.'); } $this->forceCheck = 1; default: break; } return $html; } public function _displayForm() { return $this->displayFormTrait(array('_configuracion' => $this->l('Configuracion'), '_mostrarLicencia' => $this->l('Licencia')), ''); } private function _mostrarLicencia() { return $this->mostrarLicenciaTrait(2); } private function _configuracion() { include_once(dirname(__FILE__).'/functionsForm.php'); include_once(dirname(__FILE__).'/imaxAcordeon.php'); $cantidad = Configuration::getGlobalValue(self::prefijo.'PROCESAR'); $token = Configuration::getGlobalValue(self::prefijo.'TOKEN'); $shopDomain = Configuration::getGlobalValue('PS_SHOP_DOMAIN'); $modoCalculoCombinacion = Configuration::getGlobalValue(self::prefijo.'PRECIO_COMB') == 1 ? true : false; $elementos_proceso = @unserialize(Configuration::getGlobalValue(self::prefijo.'ELEMENTOS_ORDEN')); $orden_proceso = @unserialize(Configuration::getGlobalValue(self::prefijo.'ORDEN_PROCESO')); $beneficio_minimo = Configuration::getGlobalValue(self::prefijo.'BENEFICIO_MIN'); $acordeon = new imaxAcordeon($this->_path); $form = new imaxForm($this, $this->_path); $form->createHidden("accion", "configuarion"); $form->createHidden("idTab", "1"); $text = ''.$this->l('ATENCION:').' '.$this->l('Sin parámetros para configurar'); $form->createFormInfomationText($text, 'confirm'); $form->createSubmitButton('opcionesConfiguracion', $this->l('Guardar')); $html = $acordeon->renderAcordeon($this->l('Configuracion'), $form->renderForm()); unset($form); $form = new imaxForm($this, $this->_path, Tools::safeOutput($_SERVER['REQUEST_URI']), 'post', 'uploadFile', "multipart/form-data", false, false, $this->path); $form->createHidden("accion", "importacion"); $form->createHidden("idTab", "1"); $form->addToForm('

'.$this->l('Pulsa este enlace para bajar un archivo de ejemplo.').'

'); $form->createFormUploadFile('file', $this->l('Fichero con EANs'), ''); $form->createSubmitButton('opcionesConfiguracion', $this->l('Guardar')); $html .= $acordeon->renderAcordeon($this->l('Importacion'), $form->renderForm()); return $html; } /** * Devuelve las funciones especificas del modulo. * @return FuncionesImaxMultiEAN */ public function getFunciones() { if(!self::$funciones) { self::$funciones = new FuncionesImaxMultiEAN($this); } return self::$funciones; } public function hookDisplayAdminProductsExtra($params) { if (isset($params['id_product'])) { $id_product = $params['id_product']; } else { $id_product = Tools::getValue('id_product'); } $product = new Product($id_product); // Obtener el nombre del producto $product_name = $product->name[$this->context->language->id]; // Si el producto no tiene combinaciones, obtenemos los EANs directamente para el producto. $product_eans = $this->getFunciones()->getEANs($id_product); $combinations = $product->getAttributeCombinations($this->context->language->id); $combinations_details = []; // Agrupar las combinaciones por id_product_attribute $groupedCombinations = []; foreach ($combinations as $combination) { $key = $combination['id_product_attribute']; if (!isset($groupedCombinations[$key])) { $groupedCombinations[$key] = [ 'id_product_attribute' => $key, 'attributes' => [] ]; } $groupedCombinations[$key]['attributes'][$combination['group_name']] = $combination['attribute_name']; } // Procesar las combinaciones agrupadas foreach ($groupedCombinations as $combination) { $attribute_info = implode(' - ', $combination['attributes']); $combinations_details[$combination['id_product_attribute']] = [ 'id' => $combination['id_product_attribute'], 'attribute_name' => $attribute_info, 'eans' => $this->getFunciones()->getEANs($id_product, $combination['id_product_attribute']) ]; } // Pasar la información a la plantilla .tpl para mostrarla $this->context->smarty->assign([ 'product_id' => $id_product, 'product_name' => $product_name, 'product_eans' => $product_eans, 'combinations_details' => $combinations_details, 'urlBase' => $this->getFrontUrl() . '/modules/' . $this->name, ]); return $this->display($this->name, 'templates/hook/admin_products_extra.tpl'); } private function getFrontUrl() { $url = false; $idShopDefault = Configuration::getGlobalValue('PS_SHOP_DEFAULT'); $tienda = new Shop($idShopDefault); if (Validate::isLoadedObject($tienda)) { if (isset($tienda->domain)) { $url = $tienda->domain; } if (!empty($tienda->physical_uri)) { $url .= $tienda->physical_uri; } if (!empty($tienda->virtual_uri)) { $url .= $tienda->virtual_uri; } } return $url; } /** * Devuelve el tipo de archivo. * @param string $rutaArchivo * @return int */ public static function detectarTipoArchivo($rutaArchivo) { $resultado = false; $ext = explode(',', $rutaArchivo); $ext = end($ext); $mime = mime_content_type($rutaArchivo); if ($mime == 'text/plain') { $resultado = self::ARCHIVO_CSV; } elseif ($mime == 'application/vnd.ms-excel' || $mime == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') { $resultado = self::ARCHIVO_EXCEL; } elseif ($ext == 'csv') { $resultado = self::ARCHIVO_CSV; } elseif ($ext == 'xls' || $ext == 'xlsx') { $resultado = self::ARCHIVO_EXCEL; } return $resultado; } }