428 lines
21 KiB
PHP
428 lines
21 KiB
PHP
<?php
|
|
|
|
if (!defined('_PS_VERSION_')) {
|
|
exit;
|
|
}
|
|
|
|
require_once _PS_MODULE_DIR_ . '/configurator/classes/Config.php';
|
|
require_once _PS_MODULE_DIR_ . '/configurator/classes/ConfigField.php';
|
|
require_once _PS_MODULE_DIR_ . '/configurator/classes/ConfigFieldValue.php';
|
|
require_once _PS_MODULE_DIR_ . '/configurator/classes/ConfigProduct.php';
|
|
require 'vendor/autoload.php';
|
|
use FontLib\Font;
|
|
|
|
class Configurator extends Module
|
|
{
|
|
private $image_extensions = array('png', 'jpg', 'jpeg');
|
|
|
|
public function __construct()
|
|
{
|
|
$this->name = 'configurator';
|
|
$this->version = '1.0.0';
|
|
$this->author = 'Tings';
|
|
$this->need_instance = 0;
|
|
|
|
$this->bootstrap = true;
|
|
parent::__construct();
|
|
|
|
$this->displayName = $this->trans('Configurator', array(), 'Modules.Banner.Admin');
|
|
$this->description = $this->trans('Pozwala dodać do produktu konfigurację projektu.', array(), 'Modules.Configurator.Admin');
|
|
$this->ps_versions_compliancy = array('min' => '1.7.1.0', 'max' => _PS_VERSION_);
|
|
}
|
|
|
|
public function install()
|
|
{
|
|
$q = "CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "config` (
|
|
`id_config` int(11) NOT NULL AUTO_INCREMENT,
|
|
`level` int(11) NULL,
|
|
`width` int(11) NULL,
|
|
`height` int(11) NULL,
|
|
`print_width` int(11) NULL,
|
|
`print_height` int(11) NULL,
|
|
`name` text NULL,
|
|
`configurator_group` text NULL,
|
|
`image` text NULL,
|
|
`id_product` int(11) NULL,
|
|
`active` tinyint(1) NULL,
|
|
`id_page` int(11) NULL,
|
|
PRIMARY KEY (`id_config`)
|
|
) ENGINE=" . _MYSQL_ENGINE_ . "; ";
|
|
|
|
Db::getInstance()->query($q);
|
|
|
|
$q = "CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "config_field` (
|
|
`id_config_field` int(11) NOT NULL AUTO_INCREMENT,
|
|
`id_config` int(11) NOT NULL,
|
|
`x` int(11) NULL,
|
|
`y` int(11) NULL,
|
|
`type` int(11) NULL,
|
|
`width` int(11) NULL,
|
|
`height` int(11) NULL,
|
|
`rotation` int(11) NULL,
|
|
`font` text NULL,
|
|
`fontsize` text NULL,
|
|
`characters_limit` int(11) NULL,
|
|
`color` text NULL,
|
|
`text` text NULL,
|
|
PRIMARY KEY (`id_config_field`)
|
|
) ENGINE=" . _MYSQL_ENGINE_ . "; ";
|
|
|
|
Db::getInstance()->query($q);
|
|
|
|
$q = "CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "config_product` (
|
|
`id_config_product` int(11) NOT NULL AUTO_INCREMENT,
|
|
`id_order` int(11) NULL,
|
|
`id_cart` int(11) NULL,
|
|
`id_order_detail` int(11) NULL,
|
|
`id_cart_product` int(11) NULL,
|
|
`page` int(11) NULL,
|
|
`token` text NULL,
|
|
PRIMARY KEY (`id_config_product`)
|
|
) ENGINE=" . _MYSQL_ENGINE_ . "; ";
|
|
|
|
Db::getInstance()->query($q);
|
|
|
|
$q = "CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "config_field_value` (
|
|
`id_config_field_value` int(11) NOT NULL AUTO_INCREMENT,
|
|
`id_config_field` int(11) NOT NULL,
|
|
`id_config_product` int(11) NOT NULL,
|
|
`value` text NULL,
|
|
`type` int(11) NULL,
|
|
`id_config` int(11) NULL,
|
|
`x` int(11) NULL,
|
|
`y` int(11) NULL,
|
|
`width` int(11) NULL,
|
|
`height` int(11) NULL,
|
|
`rotation` int(11) NULL,
|
|
`font` text NULL,
|
|
`fontsize` text NULL,
|
|
`characters_limit` int(11) NULL,
|
|
`color` text NULL,
|
|
`text` text NULL,
|
|
PRIMARY KEY (`id_config_field_value`)
|
|
) ENGINE=" . _MYSQL_ENGINE_ . "; ";
|
|
|
|
Db::getInstance()->query($q);
|
|
|
|
$q = "CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "configurator_group` (
|
|
`id_configurator_group` int(11) NOT NULL AUTO_INCREMENT,
|
|
`name` text NULL,
|
|
PRIMARY KEY (`id_configurator_group`)
|
|
) ENGINE=" . _MYSQL_ENGINE_ . "; ";
|
|
|
|
Db::getInstance()->query($q);
|
|
|
|
$tab = new Tab();
|
|
$tab->active = 1;
|
|
$tab->class_name = 'AdminConfigurator';
|
|
$tab->name = array();
|
|
foreach (Language::getLanguages(true) as $lang) {
|
|
$tab->name[$lang['id_lang']] = 'Pobieranie konfiguracji produktów';
|
|
}
|
|
$tab->id_parent = 2;
|
|
$tab->module = 'Configurator';
|
|
$tab->add();
|
|
|
|
$tab = new Tab();
|
|
$tab->active = 1;
|
|
$tab->class_name = 'AdminGroupsConfigurator';
|
|
$tab->name = array();
|
|
foreach (Language::getLanguages(true) as $lang) {
|
|
$tab->name[$lang['id_lang']] = 'Konfiguracje - grupy';
|
|
}
|
|
$tab->id_parent = 2;
|
|
$tab->module = 'Configurator';
|
|
$tab->add();
|
|
|
|
return (parent::install() && $this->registerHook('displayConfigurator') && $this->registerHook('actionBeforeCartUpdateQty')
|
|
&& $this->registerHook('displayBackOfficeHeader') && $this->registerHook('actionProductUpdate')
|
|
&& $this->registerHook('displayAdminProductsExtra')&& $this->registerHook('header') && $this->registerHook('moduleRoutes')
|
|
&& $this->registerHook('actionProductUpdateAfter') && $this->registerHook('displayAdminOrderMainBottom'));
|
|
}
|
|
|
|
public function hookModuleRoutes($params)
|
|
{
|
|
return array(
|
|
'module-configurator-pdf' => array(
|
|
'controller' => 'getpdf',
|
|
'rule' => 'configurator',
|
|
'keywords' => array(
|
|
'token' => array('regexp' => '[_a-zA-Z0-9-\pL]*')
|
|
),
|
|
'params' => array(
|
|
'fc' => 'module',
|
|
'module' => 'configurator',
|
|
'controller' => 'getpdf'
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
public function uninstall()
|
|
{
|
|
$q = "DROP TABLE IF EXISTS `" . _DB_PREFIX_ . "config_field`;";
|
|
Db::getInstance()->query($q);
|
|
$q = "DROP TABLE IF EXISTS `" . _DB_PREFIX_ . "config`;";
|
|
Db::getInstance()->query($q);
|
|
|
|
return parent::uninstall();
|
|
}
|
|
|
|
public function hookActionBeforeCartUpdateQty($params) {
|
|
// $id_product = (int)Tools::getValue('id_product');
|
|
// $id_product_attribute = (int)Tools::getValue('id_product_attribute');
|
|
// $qty = (int)Tools::getValue('qty');
|
|
// if($id_product && $id_product > 0) {
|
|
// $q = "SELECT * FROM `" . _DB_PREFIX_ . "config` where `id_product` = ".$id_product;
|
|
// $configsIds = Db::getInstance()->executeS($q);
|
|
// $q = "SELECT * FROM `" . _DB_PREFIX_ . "config_product` where `id_cart` = ".$this->context->cart->id." and `id_cart_product` = ".$id_product;
|
|
// $prevfields = Db::getInstance()->executeS($q);
|
|
// $sql = "DELETE FROM `" . _DB_PREFIX_ . "config_product` where `id_cart` = ".$this->context->cart->id." and `id_cart_product` = ".$id_product;
|
|
// Db::getInstance()->query($sql);
|
|
// $bytes = random_bytes(20);
|
|
// $page = (int)Tools::getValue('configurator_current_page');
|
|
// $sql = "INSERT INTO `" . _DB_PREFIX_ . "config_product` (`id_cart`, `id_cart_product`, `token`, `page`) VALUES ('".$this->context->cart->id."','".$id_product."','".bin2hex($bytes)."', '".$page."')";
|
|
// Db::getInstance()->query($sql);
|
|
// $configProductId = Db::getInstance()->Insert_ID();
|
|
|
|
// // $sql = "INSERT INTO `" . _DB_PREFIX_ . "customization` (`id_product_attribute`, `id_address_delivery`, `id_cart`, `id_product`, `quantity`, `quantity_refunded`, `quantity_returned`, `in_cart`) VALUES
|
|
// // ('".$id_product_attribute."','".$this->context->cart->id_address_delivery."','".$this->context->cart->id."', '".$id_product."', '".$qty."', '0', '0', '1')";
|
|
// // Db::getInstance()->query($sql);
|
|
// // $customizationId = Db::getInstance()->Insert_ID();
|
|
|
|
// if($configsIds) {
|
|
// foreach ($configsIds as $configId) {
|
|
// $q = "SELECT * FROM `" . _DB_PREFIX_ . "config_field` where `id_config` = ".$configId['id_config'];
|
|
// $fields = Db::getInstance()->executeS($q);
|
|
// foreach ($fields as $field) {
|
|
// foreach ($prevfields as $prevfield) {
|
|
// $sql = "DELETE FROM `" . _DB_PREFIX_ . "config_field_value` where `id_config_field` = ".$field['id_config_field']." and `id_config_product` = ".$prevfield['id_config_product'];
|
|
// Db::getInstance()->query($sql);
|
|
// }
|
|
// }
|
|
// foreach ($fields as $field) {
|
|
// if($field['type'] == 1) {
|
|
// if($value = Tools::getValue('configurator_field_input_'.$field['id_config_field'])) {
|
|
// $sql = "INSERT INTO `" . _DB_PREFIX_ . "config_field_value` (`id_config_field`, `id_config_product`, `value`, `id_config`, `x`, `y`, `width`, `height`, `rotation`, `font`, `fontsize`, `color`, `text`, `type`, `textalign`, `active`) VALUES
|
|
// ('".$field['id_config_field']."','".$configProductId."','".pSQL($value)."', '".$field['id_config']."', '".$field['x']."', '".$field['y']."', '".$field['width']."', '".$field['height']."', '".$field['rotation']."', '".$field['font']."', '".$field['fontsize']."', '".$field['color']."', '".pSQL($value)."', '".$field['type']."', '".$field['textalign']."', '".Tools::getIsset('configurator_field_checkbox_'.$field['id_config_field'])."')";
|
|
// Db::getInstance()->query($sql);
|
|
// }
|
|
// }
|
|
// else {
|
|
// if($value = Tools::getValue('configurator_field_img_'.$field['id_config_field'])) {
|
|
// $sql = "INSERT INTO `" . _DB_PREFIX_ . "config_field_value` (`id_config_field`, `id_config_product`, `value`, `id_config`, `x`, `y`, `width`, `height`, `rotation`, `font`, `fontsize`, `color`, `text`, `type`, `textalign`, `active`) VALUES
|
|
// ('".$field['id_config_field']."','".$configProductId."','".pSQL($value)."', '".$field['id_config']."', '".$field['x']."', '".$field['y']."', '".$field['width']."', '".$field['height']."', '".$field['rotation']."', '".$field['font']."', '".$field['fontsize']."', '".$field['color']."', '".pSQL($value)."', '".$field['type']."', '".$field['textalign']."', '".Tools::getIsset('configurator_field_checkbox_'.$field['id_config_field'])."')";
|
|
// Db::getInstance()->query($sql);
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
}
|
|
|
|
public function hookDisplayAdminOrderMainBottom($params) {
|
|
$order = new Order($params['id_order']);
|
|
if($order) {
|
|
$products = $order->getCartProducts();
|
|
$products_links = array();
|
|
$context = Context::getContext();
|
|
foreach($products as $product) {
|
|
$q = "SELECT * FROM `" . _DB_PREFIX_ . "config_product` where `id_cart` = ".$order->getCartIdStatic($params['id_order'])." and `id_cart_product` = ".(int)$product['id_product']." and `id_customization` = ".(int)$product['id_customization'];
|
|
$configproduct = Db::getInstance()->getRow($q);
|
|
if($configproduct) {
|
|
$products_links[] = array(
|
|
'name' => $product['product_name'],
|
|
'url_pdf' => $context->link->getModuleLink('configurator', 'getpdf', array(
|
|
'token' => $configproduct['token']
|
|
)),
|
|
'url_config' => $context->link->getModuleLink('configurator', 'configureproduct', array(
|
|
'token' => $configproduct['token']
|
|
))
|
|
);
|
|
}
|
|
}
|
|
}
|
|
if($products_links) {
|
|
$this->context->smarty->assign('products_links', $products_links);
|
|
return $this->display(__FILE__, 'views/templates/admin/pdf.tpl');
|
|
}
|
|
}
|
|
|
|
public function hookDisplayAdminProductsExtra($params)
|
|
{
|
|
if (isset($params['id_product']) && (int)$params['id_product']) {
|
|
$product = new Product((int)$params['id_product']);
|
|
}
|
|
else {
|
|
if (!Validate::isLoadedObject($product = new Product((int)Tools::getValue('id_product'))))
|
|
return false;
|
|
}
|
|
$q = "SELECT `id_config`, `width`, `height`, `print_width`, `print_height`, `image`, `name`, `id_configurator_group` FROM `" . _DB_PREFIX_ . "config` where `id_product` = ".$product->id;
|
|
$configsIds = Db::getInstance()->executeS($q);
|
|
$configurator_fields = array();
|
|
if($configsIds) {
|
|
$config = new \Config($configsIds[0]['id_config']);
|
|
foreach ($configsIds as $configId) {
|
|
$q = "SELECT * FROM `" . _DB_PREFIX_ . "config_field` where `id_config` = ".$configId['id_config'];
|
|
$fields = Db::getInstance()->executeS($q);
|
|
$configurator_fields[] = array(
|
|
'id_config' => $configId['id_config'],
|
|
'image' => $configId['image'],
|
|
'id_configurator_group' => $configId['id_configurator_group'],
|
|
'name' => $configId['name'],
|
|
'width' => $configId['width'],
|
|
'height' => $configId['height'],
|
|
'print_width' => $configId['print_width'],
|
|
'print_height' => $configId['print_height'],
|
|
'fields' => $fields
|
|
);
|
|
}
|
|
} else {
|
|
$config = new \Config();
|
|
$config->id_product = $product->id;
|
|
$config->id_page = 0;
|
|
$config->level = 1;
|
|
$config->add();
|
|
$configurator_fields[] = array(
|
|
'id_config' => $config->id,
|
|
'name' => $config->name,
|
|
'image' => '',
|
|
'id_configurator_group' => 0,
|
|
'width' => '',
|
|
'height' => '',
|
|
'print_width' => '',
|
|
'print_height' => '',
|
|
'fields' => []
|
|
);
|
|
}
|
|
$q = "SELECT * FROM `" . _DB_PREFIX_ . "configurator_group`";
|
|
$configurator_groups = Db::getInstance()->executeS($q);
|
|
|
|
$this->context->smarty->assign('configurator_groups', $configurator_groups);
|
|
$this->context->smarty->assign('config', $config);
|
|
$this->context->smarty->assign('configurator_fields', json_encode($configurator_fields));
|
|
$this->context->smarty->assign('product_id', $product->id);
|
|
|
|
$scanned_directory = array_diff(scandir(_PS_MODULE_DIR_ . 'configurator/fonts/'), ['..', '.']);
|
|
$fonts = [];
|
|
|
|
foreach ($scanned_directory as $fontName) {
|
|
try {
|
|
$font = Font::load(_PS_MODULE_DIR_ . 'configurator/fonts/' . $fontName);
|
|
if ($font == null || !$font->getFontFullName()) {
|
|
continue;
|
|
}
|
|
$fonts[] = $font->getFontFullName();
|
|
} catch (\Throwable $e) {
|
|
|
|
}
|
|
}
|
|
|
|
$this->context->smarty->assign('fonts', $fonts);
|
|
|
|
return $this->display(__FILE__, 'views/templates/admin/settings.tpl');
|
|
}
|
|
|
|
public function hookDisplayConfigurator($params = array())
|
|
{
|
|
if(isset($params['id_product']) && $params['id_product']) {
|
|
$q = "SELECT * FROM `" . _DB_PREFIX_ . "config` where `id_product` = ".$params['id_product'];
|
|
$configsIds = Db::getInstance()->executeS($q);
|
|
$configurator_fields = array();
|
|
$configurator_fields_ids = array();
|
|
if($configsIds) {
|
|
$config = new \Config($configsIds[0]['id_config']);
|
|
if($config->active) {
|
|
foreach ($configsIds as $configId) {
|
|
$q = "SELECT * FROM `" . _DB_PREFIX_ . "config_field` where `id_config` = ".$configId['id_config'];
|
|
$fields = Db::getInstance()->executeS($q);
|
|
$fields_ids = array();
|
|
foreach ($fields as $field) {
|
|
$fields_ids[] = $field['id_config_field'];
|
|
}
|
|
$configurator_fields_ids[] = $fields_ids;
|
|
$configurator_fields[] = array(
|
|
'id_config' => $configId['id_config'],
|
|
'image' => $configId['image'],
|
|
'width' => $configId['width'],
|
|
'height' => $configId['height'],
|
|
'name' => $configId['name'],
|
|
'fields' => $fields
|
|
);
|
|
}
|
|
|
|
$this->context->smarty->assign('configurator_fields', $configurator_fields);
|
|
$this->context->smarty->assign('configurator_fields_ids', json_encode($configurator_fields_ids));
|
|
|
|
return $this->display(__FILE__, 'views/templates/hook/configurator.tpl');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function hookDisplayHeader($params = array())
|
|
{
|
|
$this->context->controller->addCSS($this->_path.'croppie.css', 'all');
|
|
$this->context->controller->addJS($this->_path.'croppie.js', 'all');
|
|
$this->context->controller->addCSS($this->_path.'fonts.css', 'all');
|
|
}
|
|
|
|
public function hookDisplayBackOfficeHeader($params = array())
|
|
{
|
|
$this->context->controller->addJS($this->_path.'libs/jquery.imgareaselect/scripts/jquery.imgareaselect.pack.js');
|
|
$this->context->controller->addCSS($this->_path.'libs/jquery.imgareaselect/css/imgareaselect-default.css', 'all');
|
|
$this->context->controller->addJS($this->_path.'colorpicker/jquery.minicolors.js');
|
|
$this->context->controller->addCSS($this->_path.'colorpicker/jquery.minicolors.css', 'all');
|
|
$this->context->controller->addCSS($this->_path.'fonts.css', 'all');
|
|
}
|
|
|
|
public function hookActionProductUpdate($params)
|
|
{
|
|
if (isset($params['id_product']) && (int)$params['id_product']) {
|
|
$product = new Product((int)$params['id_product']);
|
|
}
|
|
else {
|
|
if (!Validate::isLoadedObject($product = new Product((int)Tools::getValue('id_product'))))
|
|
return false;
|
|
}
|
|
$configurator_fields = json_decode(Tools::getValue('configurator_fields'), true);
|
|
if($configurator_fields && is_array($configurator_fields)) {
|
|
foreach ($configurator_fields as $id_page => $configurator_field) {
|
|
$config = new \Config($configurator_field['id_config']);
|
|
$config->active = Tools::getValue('configurator_active');
|
|
$config->name = $configurator_field['name'];
|
|
$config->id_configurator_group = $configurator_field['id_configurator_group'];
|
|
$config->width = $configurator_field['width'] > 0 ? $configurator_field['width'] : (int)Tools::getValue('width');
|
|
$config->height = $configurator_field['height'] > 0 ? $configurator_field['height'] : (int)Tools::getValue('height');
|
|
$config->print_width = $configurator_field['print_width'] > 0 ? $configurator_field['print_width'] : (int)Tools::getValue('configurator_print_width');
|
|
$config->print_height = $configurator_field['print_height'] > 0 ? $configurator_field['print_height'] : (int)Tools::getValue('configurator_print_height');
|
|
$config->id_page = $id_page;
|
|
$config->level = 1;
|
|
$config->update();
|
|
$q = "UPDATE `" . _DB_PREFIX_ . "product` SET `isconfigurable` = ".$config->active." where `id_product` = ".$product->id;
|
|
Db::getInstance()->query($q);
|
|
$q = "DELETE FROM `" . _DB_PREFIX_ . "config_field` where `id_config` = ".$configurator_field['id_config'];
|
|
Db::getInstance()->query($q);
|
|
foreach ($configurator_field['fields'] as $field) {
|
|
$configField = new \ConfigField();
|
|
$configField->id_config = $configurator_field['id_config'];
|
|
$configField->color = $field['color'];
|
|
$configField->width = $field['width'];
|
|
$configField->height = $field['height'];
|
|
$configField->rotation = $field['rotation'];
|
|
$configField->x = $field['x'];
|
|
$configField->y = $field['y'];
|
|
$configField->font = $field['font'];
|
|
$configField->fontsize = $field['fontsize'];
|
|
$configField->characters_limit = $field['characters_limit'];
|
|
$configField->text = $field['text'];
|
|
$configField->type = $field['type'];
|
|
$configField->textalign = $field['textalign'];
|
|
$configField->add();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|