// fix delete customization
This commit is contained in:
+60
-57
@@ -656,7 +656,7 @@ class CartCore extends ObjectModel
|
|||||||
// Link customization to product combination when it is first added to cart
|
// Link customization to product combination when it is first added to cart
|
||||||
if (empty($id_customization))
|
if (empty($id_customization))
|
||||||
{
|
{
|
||||||
$customization = $this->getProductCustomization($id_product, null, false);
|
$customization = $this->getProductCustomization($id_product, null, true);
|
||||||
foreach ($customization as $field)
|
foreach ($customization as $field)
|
||||||
{
|
{
|
||||||
if ($field['quantity'] == 0)
|
if ($field['quantity'] == 0)
|
||||||
@@ -694,35 +694,48 @@ class CartCore extends ObjectModel
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function _addCustomization($id_product, $id_product_attribute, $index, $type, $field, $quantity, $id_customization = null)
|
/**
|
||||||
|
* Add customization item to database
|
||||||
|
*
|
||||||
|
* @param int $id_product
|
||||||
|
* @param int $id_product_attribute
|
||||||
|
* @param int $index
|
||||||
|
* @param int $type
|
||||||
|
* @param string $field
|
||||||
|
* @param int $quantity
|
||||||
|
* @return boolean success
|
||||||
|
*/
|
||||||
|
public function _addCustomization($id_product, $id_product_attribute, $index, $type, $field, $quantity)
|
||||||
{
|
{
|
||||||
$exising_customization = Db::getInstance()->getRow('
|
$exising_customization = Db::getInstance()->ExecuteS('
|
||||||
SELECT cu.id_customization, cd.index, cd.value, cd.type, cu.in_cart, cu.quantity FROM `'._DB_PREFIX_.'customization` cu
|
SELECT cu.`id_customization`, cd.`index`, cd.`value`, cd.`type` FROM `'._DB_PREFIX_.'customization` cu
|
||||||
LEFT JOIN `'._DB_PREFIX_.'customized_data` cd
|
LEFT JOIN `'._DB_PREFIX_.'customized_data` cd
|
||||||
ON cu.`id_customization` = cd.`id_customization`
|
ON cu.`id_customization` = cd.`id_customization`
|
||||||
WHERE cu.id_cart = '.(int)$this->id.'
|
WHERE cu.id_cart = '.(int)$this->id.'
|
||||||
AND cu.id_product = '.(int)$id_product.'
|
AND cu.id_product = '.(int)$id_product.'
|
||||||
AND type = '.(int)$type.'
|
|
||||||
AND `index` = '.(int)$index.'
|
|
||||||
AND in_cart = 0');
|
AND in_cart = 0');
|
||||||
|
|
||||||
// If the customization field is alreay filled, delete it
|
|
||||||
if ($exising_customization)
|
if ($exising_customization)
|
||||||
{
|
{
|
||||||
if ($exising_customization['type'] == $type && $exising_customization['index'] == $index)
|
// If the customization field is alreay filled, delete it
|
||||||
|
foreach($exising_customization as $customization)
|
||||||
{
|
{
|
||||||
Db::getInstance()->Execute('
|
if ($customization['type'] == $type && $customization['index'] == $index)
|
||||||
DELETE FROM `'._DB_PREFIX_.'customized_data`
|
|
||||||
WHERE id_customization = '.(int)$exising_customization['id_customization'].'
|
|
||||||
AND type = '.(int)$exising_customization['type'].'
|
|
||||||
AND `index` = '.(int)$exising_customization['index']);
|
|
||||||
if ($type == _CUSTOMIZE_FILE_)
|
|
||||||
{
|
{
|
||||||
@unlink(_PS_UPLOAD_DIR_.$exising_customization['value']);
|
Db::getInstance()->Execute('
|
||||||
@unlink(_PS_UPLOAD_DIR_.$exising_customization['value'].'_small');
|
DELETE FROM `'._DB_PREFIX_.'customized_data`
|
||||||
|
WHERE id_customization = '.(int)$customization['id_customization'].'
|
||||||
|
AND type = '.(int)$customization['type'].'
|
||||||
|
AND `index` = '.(int)$customization['index']);
|
||||||
|
if ($type == _CUSTOMIZE_FILE_)
|
||||||
|
{
|
||||||
|
@unlink(_PS_UPLOAD_DIR_.$customization['value']);
|
||||||
|
@unlink(_PS_UPLOAD_DIR_.$customization['value'].'_small');
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$id_customization = $exising_customization['id_customization'];
|
$id_customization = $exising_customization[0]['id_customization'];
|
||||||
}else
|
}else
|
||||||
{
|
{
|
||||||
Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'customization` (`id_cart`, `id_product`, `id_product_attribute`, `quantity`) VALUES ('.(int)($this->id).', '.(int)($id_product).', '.(int)($id_product_attribute).', '.(int)($quantity).')');
|
Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'customization` (`id_cart`, `id_product`, `id_product_attribute`, `quantity`) VALUES ('.(int)($this->id).', '.(int)($id_product).', '.(int)($id_product_attribute).', '.(int)($quantity).')');
|
||||||
@@ -1519,29 +1532,12 @@ class CartCore extends ObjectModel
|
|||||||
*
|
*
|
||||||
* @return bool Always true
|
* @return bool Always true
|
||||||
*/
|
*/
|
||||||
public function addTextFieldToProduct($id_product, $index, $textValue, Context $context = null)
|
public function addTextFieldToProduct($id_product, $index, $type, $textValue)
|
||||||
{
|
{
|
||||||
if (!$context)
|
|
||||||
$context = Context::getContext();
|
|
||||||
$textValue = str_replace(array("\n", "\r"), '', nl2br($textValue));
|
$textValue = str_replace(array("\n", "\r"), '', nl2br($textValue));
|
||||||
$textValue = str_replace('\\', '\\\\', $textValue);
|
$textValue = str_replace('\\', '\\\\', $textValue);
|
||||||
$textValue = str_replace('\'', '\\\'', $textValue);
|
$textValue = str_replace('\'', '\\\'', $textValue);
|
||||||
$context->cookie->{'textFields_'.(int)($id_product).'_'.(int)($index)} = $textValue;
|
return $this->_addCustomization($id_product, 0, $index, $type, $textValue, 0);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Delete customer's text
|
|
||||||
*
|
|
||||||
* @return bool Always true
|
|
||||||
*/
|
|
||||||
public function deleteTextFieldFromProduct($id_product, $index, Context $context = null)
|
|
||||||
{
|
|
||||||
if (!$context)
|
|
||||||
$context = Context::getContext();
|
|
||||||
|
|
||||||
unset($context->cookie->{'textFields_'.(int)($id_product).'_'.(int)($index)});
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -1549,23 +1545,40 @@ class CartCore extends ObjectModel
|
|||||||
*
|
*
|
||||||
* @return bool Always true
|
* @return bool Always true
|
||||||
*/
|
*/
|
||||||
public function addPictureToProduct($id_product, $index, $type, $field)
|
public function addPictureToProduct($id_product, $index, $type, $file)
|
||||||
{
|
{
|
||||||
$product_array = $this->getProducts(false, $id_product);
|
return $this->_addCustomization($id_product, 0, $index, $type, $file, 0);
|
||||||
$id_customization = $this->_addCustomization($id_product, 0, $index, $type, $field, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Remove a customer's picture
|
* Remove a customer's customization
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function deletePictureToProduct($id_product, $index)
|
public function deleteCustomizationToProduct($id_product, $index)
|
||||||
{
|
{
|
||||||
$product_array = $this->getProducts(false, $id_product);
|
$result = true;
|
||||||
|
|
||||||
if (isset($product_array[0]) && $product = $product_array[0])
|
$custData = Db::getInstance()->getRow('
|
||||||
return $this->_deleteCustomization($product['id_customization'], $id_product, $product['id_product_attribute']);
|
SELECT cu.`id_customization`, cd.`index`, cd.`value`, cd.`type` FROM `'._DB_PREFIX_.'customization` cu
|
||||||
|
LEFT JOIN `'._DB_PREFIX_.'customized_data` cd
|
||||||
|
ON cu.`id_customization` = cd.`id_customization`
|
||||||
|
WHERE cu.`id_cart` = '.(int)$this->id.'
|
||||||
|
AND cu.`id_product` = '.(int)$id_product.'
|
||||||
|
AND `index` = '.(int)$index.'
|
||||||
|
AND `in_cart` = 0'
|
||||||
|
);
|
||||||
|
|
||||||
|
// Delete customization picture if necessary
|
||||||
|
if ($custData['type'] == 0)
|
||||||
|
$result &= (@unlink(_PS_UPLOAD_DIR_.$custData['value']) && @unlink(_PS_UPLOAD_DIR_.$custData['value'].'_small'));
|
||||||
|
|
||||||
|
$result &= Db::getInstance()->execute('DELETE
|
||||||
|
FROM `'._DB_PREFIX_.'customized_data`
|
||||||
|
WHERE `id_customization` = '.(int)$custData['id_customization'].'
|
||||||
|
AND `index` = '.(int)$index
|
||||||
|
);
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1574,7 +1587,7 @@ class CartCore extends ObjectModel
|
|||||||
* @param int $id_product
|
* @param int $id_product
|
||||||
* @return array result rows
|
* @return array result rows
|
||||||
*/
|
*/
|
||||||
public function getProductCustomization($id_product, $type = null, $in_cart = false)
|
public function getProductCustomization($id_product, $type = null, $not_in_cart = false)
|
||||||
{
|
{
|
||||||
$result = Db::getInstance()->ExecuteS('
|
$result = Db::getInstance()->ExecuteS('
|
||||||
SELECT cu.id_customization, cd.index, cd.value, cd.type, cu.in_cart, cu.quantity FROM `'._DB_PREFIX_.'customization` cu
|
SELECT cu.id_customization, cd.index, cd.value, cd.type, cu.in_cart, cu.quantity FROM `'._DB_PREFIX_.'customization` cu
|
||||||
@@ -1584,20 +1597,10 @@ class CartCore extends ObjectModel
|
|||||||
AND cu.id_product = '.(int)$id_product.
|
AND cu.id_product = '.(int)$id_product.
|
||||||
($type == 0 ? ' AND type = 0' : '').
|
($type == 0 ? ' AND type = 0' : '').
|
||||||
($type == 1 ? ' AND type = 1' : '').
|
($type == 1 ? ' AND type = 1' : '').
|
||||||
' AND in_cart = '.($in_cart ? '1' : '0')
|
($not_in_cart ? ' AND in_cart = 0' : '')
|
||||||
);
|
);
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static public function deleteCustomizationInformations($id_product, Context $context = null)
|
|
||||||
{
|
|
||||||
if (!$context)
|
|
||||||
$context = Context::getContext();
|
|
||||||
|
|
||||||
$context->cookie->unsetFamily('pictures_'.(int)($id_product).'_');
|
|
||||||
$context->cookie->unsetFamily('textFields_'.(int)($id_product).'_');
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static public function getCustomerCarts($id_customer)
|
static public function getCustomerCarts($id_customer)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -141,14 +141,15 @@ class ProductControllerCore extends FrontController
|
|||||||
elseif (isset($_GET['deletePicture']) AND !$context->cart->deletePictureToProduct($this->product->id, Tools::getValue('deletePicture')))
|
elseif (isset($_GET['deletePicture']) AND !$context->cart->deletePictureToProduct($this->product->id, Tools::getValue('deletePicture')))
|
||||||
$this->errors[] = Tools::displayError('An error occurred while deleting the selected picture');
|
$this->errors[] = Tools::displayError('An error occurred while deleting the selected picture');
|
||||||
|
|
||||||
$files = self::$cart->getProductCustomization($this->product->id, 0, false);
|
$files = self::$cart->getProductCustomization($this->product->id, _CUSTOMIZE_FILE_, true);
|
||||||
$pictures = array();
|
$pictures = array();
|
||||||
foreach($files as $file)
|
foreach($files as $file)
|
||||||
$pictures['pictures_'.$this->product->id.'_'.$file['index']] = $file['value'];
|
$pictures['pictures_'.$this->product->id.'_'.$file['index']] = $file['value'];
|
||||||
|
|
||||||
$textFields = self::$cookie->getFamily('textFields_'.(int)($this->product->id));
|
$texts = self::$cart->getProductCustomization($this->product->id, _CUSTOMIZE_TEXTFIELD_, true);
|
||||||
foreach ($textFields as $key => $textField)
|
$textFields = array();
|
||||||
$textFields[$key] = str_replace('<br />', "\n", $textField);
|
foreach ($texts as $textField)
|
||||||
|
$textFields['textFields_'.$this->product->id.'_'.$textField['index']] = str_replace('<br />', "\n", $textField['value']);
|
||||||
self::$smarty->assign(array(
|
self::$smarty->assign(array(
|
||||||
'pictures' => $pictures,
|
'pictures' => $pictures,
|
||||||
'textFields' => $textFields));
|
'textFields' => $textFields));
|
||||||
@@ -391,7 +392,6 @@ class ProductControllerCore extends FrontController
|
|||||||
if ($fieldId['type'] == _CUSTOMIZE_FILE_)
|
if ($fieldId['type'] == _CUSTOMIZE_FILE_)
|
||||||
$authorizedFileFields[(int)($fieldId['id_customization_field'])] = 'file'.(int)($fieldId['id_customization_field']);
|
$authorizedFileFields[(int)($fieldId['id_customization_field'])] = 'file'.(int)($fieldId['id_customization_field']);
|
||||||
$indexes = array_flip($authorizedFileFields);
|
$indexes = array_flip($authorizedFileFields);
|
||||||
$id_customization = null;
|
|
||||||
foreach ($_FILES AS $fieldName => $file)
|
foreach ($_FILES AS $fieldName => $file)
|
||||||
if (in_array($fieldName, $authorizedFileFields) AND isset($file['tmp_name']) AND !empty($file['tmp_name']))
|
if (in_array($fieldName, $authorizedFileFields) AND isset($file['tmp_name']) AND !empty($file['tmp_name']))
|
||||||
{
|
{
|
||||||
@@ -412,7 +412,7 @@ class ProductControllerCore extends FrontController
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Store customization in database
|
// Store customization in database
|
||||||
$cart->addPictureToProduct($this->product->id, $indexes[$fieldName], 0, $fileName);
|
$cart->addPictureToProduct($this->product->id, $indexes[$fieldName], _CUSTOMIZE_FILE_, $fileName);
|
||||||
}
|
}
|
||||||
unlink($tmpName);
|
unlink($tmpName);
|
||||||
}
|
}
|
||||||
@@ -434,10 +434,10 @@ class ProductControllerCore extends FrontController
|
|||||||
if (!Validate::isMessage($value))
|
if (!Validate::isMessage($value))
|
||||||
$this->errors[] = Tools::displayError('Invalid message');
|
$this->errors[] = Tools::displayError('Invalid message');
|
||||||
else
|
else
|
||||||
$cart->addTextFieldToProduct((int)($this->product->id), $indexes[$fieldName], $value);
|
$cart->addTextFieldToProduct($this->product->id, $indexes[$fieldName], _CUSTOMIZE_TEXTFIELD_, $value);
|
||||||
}
|
}
|
||||||
elseif (in_array($fieldName, $authorizedTextFields) AND empty($value))
|
elseif (in_array($fieldName, $authorizedTextFields) AND empty($value))
|
||||||
$cart->deleteTextFieldFromProduct((int)($this->product->id), $indexes[$fieldName]);
|
$cart->deleteCustomizationToProduct((int)($this->product->id), $indexes[$fieldName]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function formTargetFormat()
|
public function formTargetFormat()
|
||||||
|
|||||||
Reference in New Issue
Block a user