/**
* @param $productId
* @param $attributesArray
* @param int $new
* @param int $copy used by BATCH and COPY ATTRIBUTES - if copy == 1 then only create new tokens, if copy == 2 then create new tokens and create folder and copy the files from source
* @throws Exception
*/
public static function storeAttributesById($productId, $attributesArray, $new = 0, $copy = 0)
{
if ((int) $productId > 0) {
$db = Factory::getDBO();
$app = Factory::getApplication();
$pathAttributes = PhocacartPath::getPath('attributefile');
// to check if attribute option download file exists
// When you add or update attributes and options, you need to have some info about which attributes and options
// are now active - so all others will be removed
$notDeleteAttribs = array();
// Select all attributes which will be not deleted
// Options are defined in attributes array
$i = 1;
// ADD ATTRIBUTES
if (!empty($attributesArray)) {
foreach ($attributesArray as $k => $v) {
if (empty($v['title'])) {
$v['title'] = Factory::getDate()->format("Y-m-d-H-i-s");
}
if (empty($v['alias'])) {
$v['alias'] = $v['title'];
}
$v['alias'] = PhocacartUtils::getAliasName($v['alias']);
// correct simple xml
if (empty($v['title'])) {
$v['title'] = '';
}
if (empty($v['alias'])) {
$v['alias'] = '';
}
if (empty($v['required'])) {
$v['required'] = '';
}
if (empty($v['type'])) {
$v['type'] = '';
}
$idExists = 0;
if ($new == 0) {
if (isset($v['id']) && $v['id'] > 0) {
// Does the row exist
$query = ' SELECT id ' . ' FROM #__phocacart_attributes' . ' WHERE id = ' . (int) $v['id'] . ' ORDER BY id';
$db->setQuery($query);
$idExists = $db->loadResult();
}
}
if ((int) $idExists > 0) {
$query = 'UPDATE #__phocacart_attributes SET' . ' product_id = ' . (int) $productId . ',' . ' title = ' . $db->quote($v['title']) . ',' . ' alias = ' . $db->quote($v['alias']) . ',' . ' required = ' . (int) $v['required'] . ',' . ' type = ' . (int) $v['type'] . ',' . ' ordering = ' . (int) $i . ' WHERE id = ' . (int) $idExists;
$db->setQuery($query);
$db->execute();
$i++;
$newIdA = $idExists;
} else {
$valuesString = '';
$valuesString = '(' . (int) $productId . ', ' . $db->quote($v['title']) . ', ' . $db->quote($v['alias']) . ', ' . (int) $v['required'] . ', ' . (int) $v['type'] . ', ' . $i . ')';
$query = ' INSERT INTO #__phocacart_attributes (product_id, title, alias, required, type, ordering)' . ' VALUES ' . (string) $valuesString;
$db->setQuery($query);
$db->execute();
// insert is not done together but step by step because of getting last insert id
$i++;
// ADD OPTIONS
$newIdA = $db->insertid();
}
$notDeleteAttribs[] = $newIdA;
$notDeleteOptions = array();
// Select all options which will be not deleted
if (!empty($v['options']) && isset($newIdA) && (int) $newIdA > 0) {
$options = array();
// Get Default Value Type - if the attribute type is single select box or multiple checkbox
// If 1 ... it is multiple, you don't need to check for unique default value
// If 0 ... it is single, you need to check that the attribute has selected only one value
$dTV = self::getTypeArray($v['type'], 1);
$dI = 0;
// defaultValue $i
$dVR = 0;
// defaultValue removed?
$j = 0;
// ordering
foreach ($v['options'] as $k2 => $v2) {
if (empty($v2['alias'])) {
$v2['alias'] = $v2['title'];
}
$v2['alias'] = PhocacartUtils::getAliasName($v2['alias']);
// Transform checkbox to INT (1 or 0)
// And check if there are more default values which is not possible e.g. for select box
$defaultValue = 0;
//PhocacartLog::add(3, $v['title'] . '- '. $v2['title']. $v2['type']. ' - '. $v2['default_value']);
// can be "on" (sent by form) or "0" or "1" sent by database e.g. in batch
if (isset($v2['default_value']) && $v2['default_value'] != '0') {
$defaultValue = 1;
// SELECTBOX OR TEXT
if ($dTV == 0 || $dTV == '') {
$dI++;
}
// Example: we are in loop of options of select box
// User has selected two default values (checked)
// But select box can have only one default value, so we need to skip it and inform user
if ($dTV == 0 && (int) $dI > 1) {
// SELECT - only one default value
$defaultValue = 0;
$dVR = 1;
} else {
if ($dTV === '' && (int) $dI > 0) {
// TEXT - no default value
$defaultValue = 0;
$dVR = 1;
}
}
}
// correct simple xml
if (empty($v2['title'])) {
$v2['title'] = '';
}
if (empty($v2['alias'])) {
$v2['alias'] = '';
}
if (empty($v2['operator'])) {
$v2['operator'] = '';
}
if (empty($v2['amount'])) {
$v2['amount'] = '';
}
if (empty($v2['stock'])) {
$v2['stock'] = '';
}
if (empty($v2['operator_weight'])) {
$v2['operator_weight'] = '';
}
if (empty($v2['weight'])) {
$v2['weight'] = '0.0';
}
if (empty($v2['image'])) {
$v2['image'] = '';
}
if (empty($v2['image_medium'])) {
$v2['image_medium'] = '';
}
if (empty($v2['image_small'])) {
$v2['image_small'] = '';
}
if (empty($v2['download_folder'])) {
$v2['download_folder'] = '';
}
if (empty($v2['download_file'])) {
$v2['download_file'] = '';
}
if (empty($v2['download_token'])) {
$v2['download_token'] = '';
}
if (empty($v2['color'])) {
$v2['color'] = '';
}
if (empty($v2['required'])) {
$v2['required'] = '0';
}
if (empty($v2['type'])) {
$v2['type'] = '0';
}
//if (empty($v2['default_value'])) {$v2['default_value'] = '';}
// COPY OR BATCH functions - we cannot do the same tokens so create new token and token folder and if set copy the files
// EACH ATTRIBUTE OPTION DOWNLOAD FILE MUST HAVE UNIQUE DOWNLOAD TOKEN AND DOWNLOAD FOLDER
if ($copy > 0) {
// First create new token and token folder
$oldDownloadFolder = $v2['download_folder'];
$v2['download_token'] = PhocacartUtils::getToken();
$v2['download_folder'] = PhocacartUtils::getToken('folder');
if ($copy == 2 && $v2['download_file'] != '' && File::exists($pathAttributes['orig_abs_ds'] . $v2['download_file'])) {
$newDownloadFile = str_replace($oldDownloadFolder, $v2['download_folder'], $v2['download_file']);
if (!Folder::create($pathAttributes['orig_abs_ds'] . $v2['download_folder'])) {
// Error message will be set below: COM_PHOCACART_ERROR_DOWNLOAD_FILE_OF_ATTRIBUTE_OPTION_DOES_NOT_EXIST
}
if (!File::copy($pathAttributes['orig_abs_ds'] . $v2['download_file'], $pathAttributes['orig_abs_ds'] . $newDownloadFile)) {
// Error message will be set below: COM_PHOCACART_ERROR_DOWNLOAD_FILE_OF_ATTRIBUTE_OPTION_DOES_NOT_EXIST
}
$v2['download_file'] = $newDownloadFile;
} else {
$v2['download_file'] = '';
}
}
// CHECK DOWNLOAD FILE
if ($v2['download_file'] != '' && $v2['download_folder'] == '') {
$msg = Text::_('COM_PHOCACART_ATTRIBUTE') . ': ' . $v['title'] . "<br />";
$msg .= Text::_('COM_PHOCACART_ERROR_DOWNLOAD_FILE_DOES_NOT_INCLUDE_DOWNLOAD_FOLDER');
$app->enqueueMessage($msg, 'error');
}
// If download_file does not exist on the server, remove it
if ($v2['download_file'] != '' && !File::exists($pathAttributes['orig_abs_ds'] . $v2['download_file'])) {
$v2['download_file'] = '';
$msg = Text::_('COM_PHOCACART_ATTRIBUTE') . ': ' . $v['title'] . "<br />";
$msg .= Text::_('COM_PHOCACART_ERROR_DOWNLOAD_FILE_OF_ATTRIBUTE_OPTION_DOES_NOT_EXIST');
$app->enqueueMessage($msg, 'error');
}
$idExists = 0;
if ($new == 0) {
if (isset($v2['id']) && $v2['id'] > 0) {
// Does the row exist
$query = ' SELECT id ' . ' FROM #__phocacart_attribute_values' . ' WHERE id = ' . (int) $v2['id'] . ' ORDER BY id';
$db->setQuery($query);
$idExists = $db->loadResult();
}
}
if ((int) $idExists > 0) {
$v2['amount'] = PhocacartUtils::replaceCommaWithPoint($v2['amount']);
$v2['weight'] = PhocacartUtils::replaceCommaWithPoint($v2['weight']);
$query = 'UPDATE #__phocacart_attribute_values SET' . ' attribute_id = ' . (int) $newIdA . ',' . ' title = ' . $db->quote($v2['title']) . ',' . ' alias = ' . $db->quote($v2['alias']) . ',' . ' operator = ' . $db->quote($v2['operator']) . ',' . ' amount = ' . $db->quote($v2['amount']) . ',' . ' stock = ' . (int) $v2['stock'] . ',' . ' operator_weight = ' . $db->quote($v2['operator_weight']) . ',' . ' weight = ' . $db->quote($v2['weight']) . ',' . ' image = ' . $db->quote($v2['image']) . ',' . ' image_medium = ' . $db->quote($v2['image_medium']) . ',' . ' image_small = ' . $db->quote($v2['image_small']) . ',' . ' download_folder = ' . $db->quote($v2['download_folder']) . ',' . ' download_file = ' . $db->quote($v2['download_file']) . ',' . ' download_token = ' . $db->quote($v2['download_token']) . ',' . ' color = ' . $db->quote($v2['color']) . ',' . ' default_value = ' . (int) $defaultValue . ',' . ' required = ' . (int) $v2['required'] . ',' . ' type = ' . (int) $v2['type'] . ',' . ' ordering = ' . (int) $j . ' WHERE id = ' . (int) $idExists;
$db->setQuery($query);
$db->execute();
$j++;
$newIdO = $idExists;
} else {
$v2['amount'] = PhocacartUtils::replaceCommaWithPoint($v2['amount']);
$v2['weight'] = PhocacartUtils::replaceCommaWithPoint($v2['weight']);
$options = '(' . (int) $newIdA . ', ' . $db->quote($v2['title']) . ', ' . $db->quote($v2['alias']) . ', ' . $db->quote($v2['operator']) . ', ' . $db->quote($v2['amount']) . ', ' . (int) $v2['stock'] . ', ' . $db->quote($v2['operator_weight']) . ', ' . $db->quote($v2['weight']) . ', ' . $db->quote($v2['image']) . ', ' . $db->quote($v2['image_medium']) . ', ' . $db->quote($v2['image_small']) . ', ' . $db->quote($v2['download_folder']) . ',' . $db->quote($v2['download_file']) . ',' . $db->quote($v2['download_token']) . ',' . $db->quote($v2['color']) . ', ' . (int) $defaultValue . ',' . (int) $v2['required'] . ', ' . (int) $v2['type'] . ', ' . (int) $j . ')';
$query = ' INSERT INTO #__phocacart_attribute_values (attribute_id, title, alias, operator, amount, stock, operator_weight, weight, image, image_medium, image_small, download_folder, download_file, download_token, color, default_value, required, type, ordering)' . ' VALUES ' . $options;
$db->setQuery($query);
$db->execute();
$j++;
$newIdO = $db->insertid();
}
$notDeleteOptions[] = $newIdO;
}
// One or more default values removed
if ($dVR == 1) {
$msg = Text::_('COM_PHOCACART_ATTRIBUTE') . ': ' . $v['title'] . "<br />";
$msg .= Text::_('COM_PHOCACART_THIS_ATTRIBUTE_DOES_NOT_ALLOW_TO_STORE_DEFAULT_VALUES_OR_MULTIPLE_DEFAULT_VALUES');
$app->enqueueMessage($msg, 'error');
}
}
// Remove all options except the active
if (!empty($notDeleteOptions)) {
$notDeleteOptionsString = implode(',', $notDeleteOptions);
// Remove all download files from not active attribute values:
$qS = ' SELECT download_folder, download_file' . ' FROM #__phocacart_attribute_values' . ' WHERE attribute_id = ' . (int) $newIdA . ' AND id NOT IN (' . $notDeleteOptionsString . ')';
$query = ' DELETE ' . ' FROM #__phocacart_attribute_values' . ' WHERE attribute_id = ' . (int) $newIdA . ' AND id NOT IN (' . $notDeleteOptionsString . ')';
} else {
// Remove all download files from not active attribute values:
$qS = ' SELECT download_folder, download_file' . ' FROM #__phocacart_attribute_values' . ' WHERE attribute_id = ' . (int) $newIdA;
$query = ' DELETE ' . ' FROM #__phocacart_attribute_values' . ' WHERE attribute_id = ' . (int) $newIdA;
}
$db->setQuery($qS);
$folderFiles = $db->loadAssocList();
self::removeDownloadFolderAndFiles($folderFiles, $pathAttributes);
$db->setQuery($query);
$db->execute();
}
}
// Remove all attributes except the active
if (!empty($notDeleteAttribs)) {
$notDeleteAttribsString = implode(',', $notDeleteAttribs);
// Remove all download files from not active attributes:
$qS = ' SELECT v.download_folder, v.download_file' . ' FROM #__phocacart_attribute_values AS v' . ' LEFT JOIN #__phocacart_attributes AS a ON a.id = v.attribute_id' . ' WHERE a.product_id = ' . (int) $productId . ' AND a.id NOT IN (' . $notDeleteAttribsString . ')';
$query = ' DELETE ' . ' FROM #__phocacart_attributes' . ' WHERE product_id = ' . (int) $productId . ' AND id NOT IN (' . $notDeleteAttribsString . ')';
} else {
// Remove all download files from not active attributes:
$qS = ' SELECT v.download_folder, v.download_file' . ' FROM #__phocacart_attribute_values AS v' . ' LEFT JOIN #__phocacart_attributes AS a ON a.id = v.attribute_id' . ' WHERE a.product_id = ' . (int) $productId;
$query = ' DELETE ' . ' FROM #__phocacart_attributes' . ' WHERE product_id = ' . (int) $productId;
}
$db->setQuery($qS);
$folderFiles = $db->loadAssocList();
self::removeDownloadFolderAndFiles($folderFiles, $pathAttributes);
$db->setQuery($query);
$db->execute();
}
}