public function save($data) { $dispatcher = JDispatcher::getInstance(); $table = $this->getTable(); $key = $table->getKeyName(); $pk = !empty($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); $isNew = true; JPluginHelper::importPlugin('content'); try { if ($pk > 0) { $table->load($pk); $isNew = false; } if (!$table->bind($data)) { $this->setError($table->getError()); return false; } $this->prepareTable($table); if (!$table->check()) { $this->setError($table->getError()); return false; } $result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, &$table, $isNew)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } if (!$table->store()) { $this->setError($table->getError()); return false; } if (!isset($data['assigntocats'])) { $data['assigntocats'] = array(); } $db = JFactory::getDbo(); $query = "SELECT id FROM #__judownload_categories WHERE criteriagroup_id =" . $table->id; $db->setQuery($query); $assignedCats = $db->loadColumn(); $unassignedCats = array_diff($assignedCats, $data['assigntocats']); if (!empty($unassignedCats)) { $query = "UPDATE #__judownload_categories SET selected_criteriagroup = 0, criteriagroup_id = 0 WHERE id IN (" . implode(',', $unassignedCats) . ")"; $db->setQuery($query); $db->execute(); foreach ($unassignedCats as $unassignedCat) { $query = "DELETE FROM #__judownload_criterias_values WHERE rating_id IN" . "\n (SELECT r.id FROM" . "\n #__judownload_rating AS r" . "\n JOIN #__judownload_documents AS d" . "\n ON d.id = r.doc_id " . "\n JOIN #__judownload_documents_xref AS dxref" . "\n ON (" . "\n d.id = dxref.doc_id" . "\n AND dxref.main = 1" . "\n )" . "\n WHERE dxref.cat_id = " . $unassignedCat . ")"; $db->setQuery($query); $db->execute(); JUDownloadHelper::changeInheritedCriteriaGroupId($unassignedCat, 0); } } $catsToAddNewCriteriaGroup = array_diff($data['assigntocats'], $assignedCats); if ($catsToAddNewCriteriaGroup) { $query = "UPDATE #__judownload_categories SET selected_criteriagroup = {$table->id}, criteriagroup_id = {$table->id} WHERE id IN (" . implode(',', $catsToAddNewCriteriaGroup) . ")"; $db->setQuery($query); $db->execute(); foreach ($catsToAddNewCriteriaGroup as $catToAddNewCriteriaGroup) { JUDownloadHelper::changeInheritedCriteriaGroupId($catToAddNewCriteriaGroup, $table->id); } } $this->cleanCache(); $dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, &$table, $isNew)); } catch (Exception $e) { $this->setError($e->getMessage()); return false; } $pkName = $table->getKeyName(); if (isset($table->{$pkName})) { $this->setState($this->getName() . '.id', $table->{$pkName}); } $this->setState($this->getName() . '.new', $isNew); return true; }
public function saveCategoryChangeCriteriaGroup($tableBeforeSave, $table, $isNew) { if (!$isNew) { if ($tableBeforeSave->criteriagroup_id != $table->criteriagroup_id) { JUDownloadHelper::changeInheritedCriteriaGroupId($table->id, $table->criteriagroup_id); } } }