public function storeValue($value, $type = 'default', $inputData = null) { $db = JFactory::getDbo(); $mainCatId = $value['main']; $secondaryCatIds = explode(",", $value['secondary']); $secondaryCatIds = array_unique($secondaryCatIds); if (is_array($secondaryCatIds) && count($secondaryCatIds) > 0) { foreach ($secondaryCatIds AS $i => $secondaryCatId) { if (!is_numeric($secondaryCatId) || $secondaryCatId <= 0 || $secondaryCatId == $mainCatId) { unset($secondaryCatIds[$i]); } } } if (!isset($this->doc->cat_id) || !$this->doc->cat_id) { $saveMainCat = JUDownloadHelper::addCategory($this->doc_id, $mainCatId, 1); if (!$saveMainCat) { return false; } } else { if ($this->doc->cat_id != $mainCatId) { $query = "UPDATE #__judownload_documents_xref SET cat_id = " . $mainCatId . " WHERE doc_id= " . $this->doc_id . " AND main = 1"; $db->setQuery($query); $db->execute(); } $secondaryCatIdsDB = $this->getSecondaryCategoryIds($this->doc_id); $removeSecondaryCatIds = array_diff($secondaryCatIdsDB, $secondaryCatIds); if ($removeSecondaryCatIds) { $query = "DELETE FROM #__judownload_documents_xref WHERE (doc_id= " . $this->doc_id . " AND main=0 AND cat_id IN (" . implode(",", $removeSecondaryCatIds) . "))"; $db->setQuery($query); $db->execute(); } } foreach ($secondaryCatIds AS $key => $catId) { $query = "SELECT id FROM #__judownload_documents_xref WHERE doc_id = " . $this->doc_id . " AND cat_id = $catId AND main = 0"; $db->setQuery($query); $itemId = $db->loadResult(); if ($itemId) { $query = "UPDATE #__judownload_documents_xref SET ordering = " . ($key + 1) . " WHERE id = " . $itemId; $db->setQuery($query); $db->execute(); } else { JUDownloadHelper::addCategory($this->doc_id, $catId, 0, $key + 1); } } return true; }