292 lines
11 KiB
PHP
292 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* Copyright (C) 2020 Futurenext srl
|
|
*
|
|
* This file is part of Zakeke.
|
|
*
|
|
* Zakeke Interactive Product Designer can not be copied and/or distributed
|
|
* without the express permission of Futurenext srl
|
|
*
|
|
* @author Futurenext srl <help@zakeke.com>
|
|
* @copyright 2019 Futurenext srl
|
|
* @license https://www.zakeke.com/privacy/#general_conditions
|
|
*/
|
|
|
|
class ZakekeAddModuleFrontController extends ModuleFrontController
|
|
{
|
|
/** @var Zakeke */
|
|
public $module;
|
|
|
|
/** @var int */
|
|
protected $id_product;
|
|
|
|
/** @var int */
|
|
protected $id_product_attribute;
|
|
|
|
/** @var int */
|
|
protected $id_address_delivery;
|
|
|
|
/** @var string */
|
|
protected $zakekeDesignId;
|
|
|
|
/** @var int */
|
|
protected $qty;
|
|
|
|
/** @var int */
|
|
protected $totalQty;
|
|
|
|
/** @var int */
|
|
protected $id_customization;
|
|
|
|
public function init()
|
|
{
|
|
parent::init();
|
|
|
|
// Get page main parameters
|
|
$this->id_product = (int)Tools::getValue('id_product', null);
|
|
$this->id_product_attribute = (int)Tools::getValue('id_product_attribute', Tools::getValue('ipa'));
|
|
$this->id_address_delivery = (int)Tools::getValue('id_address_delivery');
|
|
|
|
$this->zakekeDesignId = (string)Tools::getValue('zakeke_design');
|
|
|
|
$this->qty = max(abs(Tools::getValue('qty', 1)), 1);
|
|
$this->totalQty = max(abs(Tools::getValue('total_qty', $this->qty)), $this->qty);
|
|
}
|
|
|
|
/**
|
|
* This process add a customization related to Zakeke.
|
|
*/
|
|
protected function processCustomizationData()
|
|
{
|
|
if (_PS_VERSION_ < 1.7) {
|
|
$groups = array();
|
|
|
|
foreach (Tools::getAllValues() as $key => $value) {
|
|
if (Tools::substr($key, 0, 6) == 'group_') {
|
|
$groups[Tools::substr($key, 6)] = $value;
|
|
}
|
|
}
|
|
|
|
if (!empty($groups)) {
|
|
$this->id_product_attribute = (int)ZakekeCompatibility::getIdProductAttributeByIdAttributes(
|
|
$this->id_product,
|
|
$groups,
|
|
true
|
|
);
|
|
}
|
|
} else {
|
|
if (Tools::getIsset('group')) {
|
|
$this->id_product_attribute = (int)Product::getIdProductAttributesByIdAttributes(
|
|
$this->id_product,
|
|
Tools::getValue('group')
|
|
);
|
|
}
|
|
}
|
|
|
|
if (!$this->id_product) {
|
|
$this->errors[] = $this->module->l('Product not found');
|
|
|
|
return;
|
|
}
|
|
|
|
if (!$this->zakekeDesignId) {
|
|
$this->errors[] = $this->module->l('zakeke-design must be not empty.');
|
|
|
|
return;
|
|
}
|
|
|
|
$product = new Product($this->id_product, true, $this->context->language->id);
|
|
if (!$product->id || !$product->active || !$product->checkAccess($this->context->cart->id_customer)) {
|
|
$this->errors[] = $this->module->l('This product is no longer available.');
|
|
|
|
return;
|
|
}
|
|
|
|
// Add cart if no cart found
|
|
if (!$this->context->cart->id) {
|
|
if (Context::getContext()->cookie->id_guest) {
|
|
$guest = new Guest(Context::getContext()->cookie->id_guest);
|
|
$this->context->cart->mobile_theme = $guest->mobile_theme;
|
|
}
|
|
$this->context->cart->add();
|
|
if ($this->context->cart->id) {
|
|
$this->context->cookie->id_cart = (int)$this->context->cart->id;
|
|
$this->context->cookie->write();
|
|
}
|
|
}
|
|
|
|
// Set cache of feature customization to true
|
|
Configuration::updateGlobalValue('PS_CUSTOMIZATION_FEATURE_ACTIVE', '1');
|
|
|
|
try {
|
|
$cartInfo = $this->module->getZakekeApi()->cartInfo($this->zakekeDesignId, $this->totalQty);
|
|
$preview = $cartInfo->previews[0]->url;
|
|
$pricing = $cartInfo->pricing;
|
|
|
|
$zakeke_item_id = ZakekeItem::zakekeItemId($this->zakekeDesignId);
|
|
if ($zakeke_item_id) {
|
|
$zakeke_item = new ZakekeItem($zakeke_item_id);
|
|
$zakeke_item->preview = $preview;
|
|
$zakeke_item->pricing_json = json_encode($pricing);
|
|
$zakeke_item->save();
|
|
} else {
|
|
$zakeke_item = new ZakekeItem();
|
|
$zakeke_item->id_item = $this->zakekeDesignId;
|
|
$zakeke_item->preview = $preview;
|
|
$zakeke_item->pricing_json = json_encode($pricing);
|
|
$zakeke_item->add();
|
|
}
|
|
|
|
$zakekePrice = ZakekePrice::calculatePrice(
|
|
$product,
|
|
$pricing,
|
|
$this->totalQty,
|
|
$this->id_product_attribute
|
|
);
|
|
|
|
$id_customization_field = null;
|
|
|
|
$customization_fields = $product->getCustomizationFields(
|
|
(int)$this->context->language->id,
|
|
(int)$this->context->shop->id
|
|
);
|
|
if (is_array($customization_fields)) {
|
|
if (count($customization_fields) === 1
|
|
&& $customization_fields[0]['type'] == Product::CUSTOMIZE_TEXTFIELD) {
|
|
$id_customization_field = $customization_fields[0]['id_customization_field'];
|
|
} else {
|
|
foreach ($customization_fields as $customization_field) {
|
|
if (($customization_field['name'] === 'Customization')
|
|
&& $customization_field['type'] == Product::CUSTOMIZE_TEXTFIELD) {
|
|
$id_customization_field = $customization_field['id_customization_field'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($id_customization_field == null) {
|
|
if (_PS_VERSION_ < 1.7) {
|
|
$customizedFieldQuery = 'INSERT INTO `' . _DB_PREFIX_ . 'customization_field`
|
|
(`id_product`, `type`, `required`)
|
|
VALUES (' . (int)$this->id_product . ', ' . Product::CUSTOMIZE_TEXTFIELD . ', 0)';
|
|
} else {
|
|
$customizedFieldQuery = 'INSERT INTO `' . _DB_PREFIX_ . 'customization_field`
|
|
(`id_product`, `type`, `required`, `is_module`)
|
|
VALUES (' . (int)$this->id_product . ', ' . Product::CUSTOMIZE_TEXTFIELD . ', 0, 1)';
|
|
}
|
|
if (!Db::getInstance()->execute($customizedFieldQuery)) {
|
|
$this->errors[] = $this->module->l('Failed to add the customization data to the database');
|
|
|
|
return;
|
|
}
|
|
|
|
$id_customization_field = Db::getInstance()->Insert_ID();
|
|
|
|
$customizedFieldQueryLangQuery = 'INSERT INTO `' . _DB_PREFIX_ . 'customization_field_lang`
|
|
(`id_customization_field`, `id_lang`, `id_shop`, `name`)
|
|
VALUES (' . (int)$id_customization_field . ',
|
|
' . (int)$this->context->language->id . ', ' . (int)$this->context->shop->id . ',
|
|
\'Customization\')';
|
|
if (!Db::getInstance()->execute($customizedFieldQueryLangQuery)) {
|
|
$this->errors[] = $this->module->l('Failed to set the customization label to the database');
|
|
|
|
return;
|
|
}
|
|
|
|
$shopList = Shop::getContextListShopID();
|
|
foreach (Language::getLanguages() as $language) {
|
|
foreach ($shopList as $id_shop) {
|
|
if ($language['id_lang'] == (int)$this->context->language->id
|
|
&& $id_shop == (int)$this->context->shop->id) {
|
|
continue;
|
|
}
|
|
|
|
$customizedFieldQueryLangQuery = 'INSERT INTO `' . _DB_PREFIX_ . 'customization_field_lang`
|
|
(`id_customization_field`, `id_lang`, `id_shop`, `name`)
|
|
VALUES (' . (int)$id_customization_field . ',
|
|
' . (int)$language['id_lang'] . ', ' . (int)$id_shop . ', \'Customization\')';
|
|
Db::getInstance()->execute($customizedFieldQueryLangQuery);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!$product->customizable) {
|
|
Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'product` p
|
|
SET p.`customizable` = 1
|
|
WHERE p.`id_product` = ' . (int)$product->id);
|
|
|
|
Db::getInstance()->execute('UPDATE `' . _DB_PREFIX_ . 'product_shop` p
|
|
SET p.`customizable` = 1
|
|
WHERE p.`id_product` = ' . (int)$product->id);
|
|
}
|
|
|
|
$customizationQuery = 'INSERT INTO `' . _DB_PREFIX_ . 'customization`
|
|
(`id_cart`, `id_product`, `id_product_attribute`, `quantity`, `in_cart`)
|
|
VALUES (' . (int)$this->context->cart->id . ', ' . (int)$this->id_product . ',
|
|
' . (int)$this->id_product_attribute . ', 0, 0)';
|
|
if (!Db::getInstance()->execute($customizationQuery)) {
|
|
$this->errors[] = $this->module->l('Failed to add the customization to the database');
|
|
|
|
return;
|
|
}
|
|
|
|
$this->id_customization = Db::getInstance()->Insert_ID();
|
|
|
|
if (_PS_VERSION_ < 1.7) {
|
|
$customizedDataQuery = 'INSERT INTO `' . _DB_PREFIX_ . 'customized_data`
|
|
(`id_customization`, `type`, `index`, `value`)
|
|
VALUES (' . (int)$this->id_customization . ',
|
|
' . Product::CUSTOMIZE_TEXTFIELD . ', ' . (int)$id_customization_field . ',
|
|
\'' . pSQL($this->zakekeDesignId) . '\')';
|
|
} else {
|
|
$jsonValue = ZakekeItem::customizationValueFromItemId($this->zakekeDesignId, $preview);
|
|
|
|
$customizedDataQuery = 'INSERT INTO `' . _DB_PREFIX_ . 'customized_data`
|
|
(`id_customization`, `type`, `index`, `value`, `id_module`, `price`)
|
|
VALUES (' . (int)$this->id_customization . ',
|
|
' . Product::CUSTOMIZE_TEXTFIELD . ', ' . (int)$id_customization_field . ',
|
|
\'' . pSQL($jsonValue) . '\', ' . (int)$this->module->id . ', ' . (float)$zakekePrice . ')';
|
|
}
|
|
if (!Db::getInstance()->execute($customizedDataQuery)) {
|
|
$this->errors[] = $this->module->l('Failed to add the Zakeke customization to the database');
|
|
|
|
return;
|
|
}
|
|
} catch (Exception $e) {
|
|
$this->errors[] = $this->module->l($e);
|
|
}
|
|
}
|
|
|
|
public function postProcess()
|
|
{
|
|
if (!$this->isTokenValid()) {
|
|
Tools::redirect('index.php');
|
|
} elseif (!$this->errors) {
|
|
$this->processCustomizationData();
|
|
}
|
|
}
|
|
|
|
public function display()
|
|
{
|
|
$this->displayAjax();
|
|
}
|
|
|
|
public function displayAjax()
|
|
{
|
|
ob_end_clean();
|
|
header('Content-Type: application/json');
|
|
if (!$this->errors) {
|
|
$this->ajaxDie(json_encode(array(
|
|
'success' => true,
|
|
'ipa' => $this->id_product_attribute,
|
|
'id_customization' => $this->id_customization
|
|
)));
|
|
} else {
|
|
$this->ajaxDie(json_encode(array(
|
|
'hasError' => true,
|
|
'errors' => $this->errors
|
|
)));
|
|
}
|
|
}
|
|
}
|