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 #__judirectory_categories WHERE criteriagroup_id =" . $table->id;
         $db->setQuery($query);
         $assignedCats = $db->loadColumn();
         $unassignedCats = array_diff($assignedCats, $data['assigntocats']);
         if (!empty($unassignedCats)) {
             $query = "UPDATE #__judirectory_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 #__judirectory_criterias_values WHERE rating_id IN" . "\n (SELECT r.id FROM" . "\n #__judirectory_rating AS r" . "\n JOIN #__judirectory_listings AS listing" . "\n ON listing.id = r.listing_id " . "\n JOIN #__judirectory_listings_xref AS listingxref" . "\n ON (" . "\n listing.id = listingxref.listing_id" . "\n AND listingxref.main = 1" . "\n )" . "\n WHERE listingxref.cat_id = " . $unassignedCat . ")";
                 $db->setQuery($query);
                 $db->execute();
                 JUDirectoryHelper::changeInheritedCriteriaGroupId($unassignedCat, 0);
             }
         }
         $catsToAddNewCriteriaGroup = array_diff($data['assigntocats'], $assignedCats);
         if ($catsToAddNewCriteriaGroup) {
             $query = "UPDATE #__judirectory_categories SET selected_criteriagroup = {$table->id}, criteriagroup_id = {$table->id}  WHERE id IN (" . implode(',', $catsToAddNewCriteriaGroup) . ")";
             $db->setQuery($query);
             $db->execute();
             foreach ($catsToAddNewCriteriaGroup as $catToAddNewCriteriaGroup) {
                 JUDirectoryHelper::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;
 }
示例#2
0
 public function saveCategoryChangeCriteriaGroup($tableBeforeSave, $table, $isNew)
 {
     if (!$isNew) {
         if ($tableBeforeSave->criteriagroup_id != $table->criteriagroup_id) {
             JUDirectoryHelper::changeInheritedCriteriaGroupId($table->id, $table->criteriagroup_id);
         }
     }
 }