public static function storeCategories($storeArray, $productId, $categoryOrdering = array())
{
if ((int) $productId > 0) {
$db = Factory::getDBO();
/*$query = ' DELETE '
.' FROM #__phocacart_product_categories'
. ' WHERE product_id = '. (int)$productId;
$db->setQuery($query);
$db->execute();*/
// Select stored categories for this ID
$query = 'SELECT a.category_id' . ' FROM #__phocacart_product_categories AS a' . ' WHERE a.product_id = ' . (int) $productId . ' ORDER BY a.product_id';
$db->setQuery($query);
$storedArray = $db->loadColumn();
// ----------------------------------------------------------
// Check if some category, or parent category is unpublished
$storeString = implode(',', array_unique(array_map('intval', $storeArray)));
$q = ' WITH RECURSIVE cte (id, title, parent_id, published) AS (' . ' SELECT id, title, parent_id, published FROM #__phocacart_categories' . ' WHERE id IN (' . $storeString . ')' . ' UNION ALL SELECT p.id, p.title, p.parent_id, p.published' . ' FROM #__phocacart_categories AS p' . ' INNER JOIN cte ON p.id = cte.parent_id' . ' )' . ' SELECT * FROM cte WHERE published = 0;';
try {
$db->setQuery($q);
$unpublishedCats = $db->loadAssocList('id');
// with assoc list the categories will be unique
if (!empty($unpublishedCats)) {
$count = count($unpublishedCats);
if ($count > 1) {
$msg = Text::_('COM_PHOCACART_BE_AWARE_FOLLOWING_SELECTED_CATEGORIES_OR_THEIR_PARENT_CATEGORIES_ARE_NOT_PUBLISHED') . ': ';
} else {
$msg = Text::_('COM_PHOCACART_BE_AWARE_FOLLOWING_SELECTED_CATEGORY_OR_ITS_PARENT_CATEGORY_IS_NOT_PUBLISHED') . ': ';
}
$msg .= '<ul>';
foreach ($unpublishedCats as $k => $v) {
$msg .= '<li><b>' . $v['title'] . '</b></li>';
}
$msg .= '</ul>';
Factory::getApplication()->enqueueMessage($msg, 'warning');
}
} catch (RuntimeException $e) {
// No error, because this is just additional info
//JFactory::getApplication()->enqueueMessage('PROBABLY WITH RECURSIVE IS NOT SUPPORTED', 'warning');
}
// ----------------------------------------------------------
$store = array_diff($storeArray, $storedArray);
// we only store categories which are not stored yet by this product id
$delete = array_diff($storedArray, $storeArray);
// category is stored in db but we removed it in administration so it is
// not more selected for this product and we need to remove it
if (!empty($delete)) {
foreach ($delete as $k => $v) {
$query = ' DELETE ' . ' FROM #__phocacart_product_categories' . ' WHERE product_id = ' . (int) $productId . ' AND category_id = ' . (int) $v;
$db->setQuery($query);
$db->execute();
}
}
if (!empty($store)) {
$values = array();
$valuesString = '';
$store = array_unique($store);
foreach ($store as $k => $v) {
$v = (int) $v;
if (isset($categoryOrdering[$v]) && $categoryOrdering[$v] > 0) {
// Import/Export function - we store the ordering
// if for example all product items are exported to empty database
// stay with stored ordering
$o = $categoryOrdering[$v];
} else {
// New row added
$o = self::getNextOrder((int) $productId, $v);
}
$values[] = ' (' . (int) $productId . ', ' . $v . ', ' . (int) $o . ')';
}
if (!empty($values)) {
$valuesString = implode(',', $values);
$query = ' INSERT INTO #__phocacart_product_categories (product_id, category_id, ordering)' . ' VALUES ' . (string) $valuesString;
$db->setQuery($query);
$db->execute();
}
}
}
}