Index: trunk/kernel/units/general/cat_dbitem.php =================================================================== diff -u -r3757 -r3787 --- trunk/kernel/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 3757) +++ trunk/kernel/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 3787) @@ -1,9 +1,9 @@ Application->getUnitOption($this->Prefix, 'ItemType'); $sql = 'SELECT CustomFieldId, FieldName FROM '.TABLE_PREFIX.'CustomField WHERE Type = '.$item_type; $this->CustomFields = $this->Conn->GetCol($sql, 'FieldName'); } - + function Create($force_id=false, $system_create=false) { if (!$this->Validate()) return false; - + $this->SetDBField('ResourceId', $this->Application->NextResourceId()); $this->SetDBField('Modified', adodb_mktime() ); $this->SetDBField('CreatedById', $this->Application->GetVar('u_id')); - + if ($this->useFilenames) { $this->checkFilename(); - $this->generateFilename(); + $this->generateFilename(); } - + $ret = parent::Create(); if($ret) { @@ -56,24 +56,24 @@ } return $ret; } - + function Update($id=null, $system_update=false) { $this->VirtualFields['ResourceId'] = Array(); $this->SetDBField('Modified', adodb_mktime() ); $this->SetDBField('ModifiedById', $this->Application->GetVar('u_id')); - + if ($this->useFilenames) { $this->checkFilename(); - $this->generateFilename(); + $this->generateFilename(); } - + $ret = parent::Update($id, $system_update); - + unset($this->VirtualFields['ResourceId']); return $ret; } - + function checkFilename() { if( !$this->GetDBField('AutomaticFilename') ) @@ -82,19 +82,19 @@ $this->SetDBField('Filename', $this->stripDisallowed($filename) ); } } - + function Copy($cat_id=null) { if (!isset($cat_id)) $cat_id = $this->Application->GetVar('m_cat_id'); $this->NameCopy($cat_id); return $this->Create($cat_id); } - + function NameCopy($master=null, $foreign_key=null) { $title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField'); if (!$title_field) return; - + $new_name = $this->GetDBField($title_field); $cat_id = $this->Application->GetVar('m_cat_id'); $original_checked = false; @@ -106,7 +106,7 @@ $new_name = 'Copy of '.$new_name; } $query = 'SELECT '.$title_field.' FROM '.$this->TableName.' - LEFT JOIN '.TABLE_PREFIX.'CategoryItems ON + LEFT JOIN '.TABLE_PREFIX.'CategoryItems ON ('.TABLE_PREFIX.'CategoryItems.ItemResourceId = '.$this->TableName.'.ResourceId) WHERE ('.TABLE_PREFIX.'CategoryItems.CategoryId = '.$cat_id.') AND '. $title_field.' = '.$this->Conn->qstr($new_name); @@ -115,50 +115,50 @@ } while ($res !== false); $this->SetDBField($title_field, $new_name); } - + function MoveToCat($cat_id=null) { // $this->NameCopy(); $cat_id = $this->Application->GetVar('m_cat_id'); // check if the product already exists in destination cat - $query = 'SELECT PrimaryCat FROM '.TABLE_PREFIX.'CategoryItems + $query = '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 - $is_primary = $this->Conn->GetOne($query); + $is_primary = $this->Conn->GetOne($query); $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, + // if it's not primary - delete it from destination category, // as we will move it from current primary below - $query = 'DELETE FROM '.TABLE_PREFIX.'CategoryItems + $query = 'DELETE FROM '.TABLE_PREFIX.'CategoryItems WHERE ItemResourceId = '.$this->GetDBField('ResourceId').' AND CategoryId = '.$cat_id; $this->Conn->Query($query); - + } $query = 'UPDATE '.TABLE_PREFIX.'CategoryItems SET CategoryId = '.$cat_id. - ' WHERE ItemResourceId = '.$this->GetDBField('ResourceId').' AND PrimaryCat = 1'; + ' WHERE ItemResourceId = '.$this->GetDBField('ResourceId').' AND PrimaryCat = 1'; $this->Conn->Query($query); $this->Update(); } - + // We need to delete CategoryItems record when deleting product function Delete($id=null) { if( isset($id) ) { $this->setID($id); } $this->Load($this->GetID()); - + $ret = parent::Delete(); if ($ret) { $query = 'DELETE FROM '.TABLE_PREFIX.'CategoryItems WHERE ItemResourceId = '.$this->GetDBField('ResourceId'); $this->Conn->Query($query); } - + return $ret; } - + /** * Deletes item from categories * @@ -168,13 +168,13 @@ function DeleteFromCategories($delete_category_ids) { $id_field = $this->Application->getUnitOption($this->Prefix, 'IDField'); // because item was loaded before by ResourceId - + $ci_table = $this->Application->getUnitOption('ci', 'TableName'); $resource_id = $this->GetDBField('ResourceId'); - + $item_cats_sql = 'SELECT CategoryId FROM %s WHERE ItemResourceId = %s'; $delete_category_items_sql = 'DELETE FROM %s WHERE ItemResourceId = %s AND CategoryId IN (%s)'; - + $category_ids = $this->Conn->GetCol( sprintf($item_cats_sql, $ci_table, $resource_id) ); $cats_left = array_diff($category_ids, $delete_category_ids); if(!$cats_left) @@ -188,7 +188,7 @@ else { $this->Conn->Query( sprintf($delete_category_items_sql, $ci_table, $resource_id, implode(',', $delete_category_ids) ) ); - + $sql = 'SELECT CategoryId FROM %s WHERE PrimaryCat = 1 AND ItemResourceId = %s'; $primary_cat_id = $this->Conn->GetCol( sprintf($sql, $ci_table, $resource_id) ); if( count($primary_cat_id) == 0 ) @@ -198,24 +198,24 @@ } } } - + function SetCustomField($field, $value) { $cf_id = getArrayValue($this->CustomFields, $field); if(!$cf_id) return false; - + $data_table = TABLE_PREFIX.'CustomMetaData'; - + $sql = 'SELECT CustomDataId FROM '.$data_table.' WHERE CustomFieldId = %s AND ResourceId = %s'; $data_id = (int)$this->Conn->GetOne( sprintf($sql, $cf_id, $this->GetDBField('ResourceId') ) ); - + $lang_id = $this->Application->GetVar('lang.current_id'); - + $sql = 'REPLACE INTO '.$data_table.'(CustomDataId,ResourceId,CustomFieldId,Value,l'.$lang_id.'_Value) VALUES (%1$s,%2$s,%3$s,%4$s,%4$s)'; $this->Conn->Query( sprintf($sql, $data_id, $this->GetDBField('ResourceId'), $cf_id, $this->Conn->qstr($value) ) ); } - + /** * replace not allowed symbols with "_" chars + remove duplicate "_" chars in result * @@ -227,32 +227,32 @@ $not_allowed = Array( ' ', '\\', '/', ':', '*', '?', '"', '<', '>', '|', '`', '~', '!', '@', '#', '$', '%', '^', '&', '(', ')', '~', '+', '=', '-', '{', '}', ']', '[', "'", ';', '.', ','); - + $string = str_replace($not_allowed, '_', $string); $string = preg_replace('/(_+)/', '_', $string); $string = $this->checkAutoFilename($string); - + return $string; } function checkAutoFilename($filename) { if(!$filename) return $filename; - + $item_id = !$this->GetID() ? 0 : $this->GetID(); - + // check temp table $sql_temp = 'SELECT '.$this->IDField.' FROM '.$this->TableName.' WHERE Filename = '.$this->Conn->qstr($filename); $found_temp_ids = $this->Conn->GetCol($sql_temp); - + // check live table $sql_live = 'SELECT '.$this->IDField.' FROM '.kTempTablesHandler::GetLiveName($this->TableName).' WHERE Filename = '.$this->Conn->qstr($filename); $found_live_ids = $this->Conn->GetCol($sql_live); - + $found_item_ids = array_unique( array_merge($found_temp_ids, $found_live_ids) ); - + $has_page = preg_match('/(.*)_([\d]+)([a-z]*)$/', $filename, $rets); - + $duplicates_found = (count($found_item_ids) > 1) || ($found_item_ids && $found_item_ids[0] != $item_id); if ($duplicates_found || $has_page) // other category has same filename as ours OR we have filename, that ends with _number { @@ -272,13 +272,13 @@ if (substr($append, -1) == 'z') $append .= 'a'; $append = substr($append, 0, strlen($append) - 1) . chr( ord( substr($append, -1) ) + 1 ); } - + return $filename.$append; } return $filename; } - + /** * Generate item's filename based on it's title field value * @@ -287,13 +287,13 @@ function generateFilename() { if ( !$this->GetDBField('AutomaticFilename') && $this->GetDBField('Filename') ) return false; - + $title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField'); $name = $this->stripDisallowed( $this->GetDBField($title_field) ); - + if ( $name != $this->GetDBField('Filename') ) $this->SetDBField('Filename', $name); } - + /** * Check if value is set for required field * @@ -313,7 +313,7 @@ if (!$res) $this->FieldErrors[$error_field]['pseudo'] = 'required'; return $res; } - + /** * Adds item to other category * @@ -324,36 +324,36 @@ { $table = TABLE_PREFIX.'CategoryItems'; $key_clause = '(ItemResourceId = '.$this->GetDBField('ResourceId').')'; - + // get all cateories, where item is in $sql = 'SELECT PrimaryCat, CategoryId FROM '.$table.' WHERE '.$key_clause; $item_categories = $this->Conn->GetCol($sql, 'CategoryId'); if (!$item_categories) { $item_categories = Array(); $primary_found = false; } - - // find primary category + + // find primary category foreach ($item_categories as $item_category_id => $primary_found) { if ($primary_found) { break; } } - + if ($primary_found && ($item_category_id == $category_id) && !$is_primary) { // want to make primary category as non-primary :( return true; } else if (!$primary_found) { $is_primary = true; } - + if ($is_primary && $item_categories) { // reset primary mark from all other categories $sql = 'UPDATE '.$table.' SET PrimaryCat = 0 WHERE '.$key_clause; $this->Conn->Query($sql); } - + // UPDATE & INSERT instead of REPLACE because CategoryItems table has no primary key defined in database if (isset($item_categories[$category_id])) { $sql = 'UPDATE '.$table.' SET PrimaryCat = '.($is_primary ? 1 : 0).' WHERE '.$key_clause.' AND (CategoryId = '.$category_id.')'; @@ -364,7 +364,7 @@ $this->Conn->Query( sprintf($sql, $category_id, $this->GetDBField('ResourceId'), $is_primary ? 1 : 0) ); } } - + /** * Removes item from category specified * @@ -375,7 +375,7 @@ $sql = 'DELETE FROM '.TABLE_PREFIX.'CategoryItems WHERE (CategoryId = %s) AND (ItemResourceId = %s)'; $this->Conn->Query( sprintf($sql, $category_id, $this->GetDBField('ResourceId')) ); } - + /** * Returns list of columns, that could exist in imported file * @@ -389,7 +389,7 @@ } return $columns; } - + /** * Returns item's primary image data *