Index: branches/RC/core/units/general/cat_dbitem.php =================================================================== diff -u -N -r11705 -r11724 --- branches/RC/core/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 11705) +++ branches/RC/core/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 11724) @@ -169,46 +169,74 @@ $this->SetDBField('CategoryId', $cat_id); } - function MoveToCat($cat_id=null) + /** + * Changes item primary category to given/current category + * + * @param int $category_id + */ + function MoveToCat($category_id = null) { // $this->NameCopy(); - if (!isset($cat_id)) $cat_id = $this->Application->GetVar('m_cat_id'); - // check if the product already exists in destination cat + if (!isset($category_id)) { + $category_id = $this->Application->GetVar('m_cat_id'); + } + + $table_name = TABLE_PREFIX . 'CategoryItems'; + if ($this->IsTempTable()) { + $table_name = $this->Application->GetTempName($table_name, 'prefix:' . $this->Prefix); + } + + // check if the item already exists in destination category $sql = 'SELECT PrimaryCat - FROM '.TABLE_PREFIX.'CategoryItems - WHERE CategoryId = '.$cat_id.' AND ItemResourceId = '.$this->GetDBField('ResourceId'); - // if it's not found is_primary will be FALSE, if it's found but not primary it will be int 0 + FROM ' . $table_name . ' + WHERE (CategoryId = ' . $category_id . ') AND (ItemResourceId = ' . $this->GetDBField('ResourceId') . ')'; $is_primary = $this->Conn->GetOne($sql); + + // if it's not found is_primary will be FALSE, if it's found but not primary it will be int 0 $exists = $is_primary !== false; - if ($exists) { // if the Product already exists in destination category - if ($is_primary) return; // do nothing when we paste to primary - // if it's not primary - delete it from destination category, - // as we will move it from current primary below - $sql = 'DELETE FROM '.TABLE_PREFIX.'CategoryItems - WHERE ItemResourceId = '.$this->GetDBField('ResourceId').' AND CategoryId = '.$cat_id; - $this->Conn->Query($sql); + if ($exists) { + // if the item already exists in destination category + if ($is_primary) { + // do nothing when we paste to primary + return ; + } + // if it's not primary - delete it from destination category, as we will move it from current primary below + $sql = 'DELETE FROM ' . $table_name . ' + WHERE (CategoryId = ' . $category_id . ') AND (ItemResourceId = ' . $this->GetDBField('ResourceId') . ')'; + $this->Conn->Query($sql); } - $sql = 'UPDATE '.TABLE_PREFIX.'CategoryItems - SET CategoryId = '.$cat_id.' - WHERE ItemResourceId = '.$this->GetDBField('ResourceId').' AND PrimaryCat = 1'; + + // change category id in existing primary category record + $sql = 'UPDATE ' . $table_name . ' + SET CategoryId = ' . $category_id . ' + WHERE (ItemResourceId = ' . $this->GetDBField('ResourceId') . ') AND (PrimaryCat = 1)'; $this->Conn->Query($sql); + $this->Update(); } - // We need to delete CategoryItems record when deleting product + /** + * When item is deleted, then also delete it from all categories + * + * @param int $id + * @return bool + */ function Delete($id = null) { if( isset($id) ) { $this->setID($id); } + $this->Load($this->GetID()); $ret = parent::Delete(); + if ($ret) { - $query = ' DELETE FROM '.$this->CategoryItemsTable().' - WHERE ItemResourceId = '.$this->GetDBField('ResourceId'); + // TODO: move to OnAfterItemDelete method + $query = ' DELETE FROM ' . $this->CategoryItemsTable() . ' + WHERE ItemResourceId = ' . $this->GetDBField('ResourceId'); $this->Conn->Query($query); }