Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Tue, Feb 25, 9:51 AM

in-portal

Index: trunk/kernel/units/general/cat_dbitem.php
===================================================================
--- trunk/kernel/units/general/cat_dbitem.php (revision 2336)
+++ trunk/kernel/units/general/cat_dbitem.php (revision 2337)
@@ -1,153 +1,155 @@
<?php
class kCatDBItem extends kDBItem {
function Create()
{
if (!$this->Validate()) return false;
$this->SetDBField('ResourceId', $this->Application->NextResourceId());
$this->SetDBField('Modified', mktime());
+ $this->SetDBField('CreatedById', $this->Application->GetVar('u_id'));
$ret = parent::Create();
if($ret)
{
if ( kTempTablesHandler::IsTempTable($this->TableName) ) {
$table = kTempTablesHandler::GetTempName(TABLE_PREFIX.'CategoryItems');
}
else {
$table = TABLE_PREFIX.'CategoryItems';
}
$cat_id = $this->Application->GetVar('m_cat_id');
$query = 'INSERT INTO '.$table.' (CategoryId,ItemResourceId,PrimaryCat)
VALUES ('.$cat_id.','.$this->GetField('ResourceId').',1)';
$this->Conn->Query($query);
}
return $ret;
}
function Update($id=null)
{
$this->VirtualFields['ResourceId'] = true;
$this->SetDBField('Modified', mktime());
+ $this->SetDBField('ModifiedById', $this->Application->GetVar('u_id'));
return parent::Update($id);
}
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;
do {
if ( preg_match('/Copy ([0-9]*) *of (.*)/', $new_name, $regs) ) {
$new_name = 'Copy '.( (int)$regs[1] + 1 ).' of '.$regs[2];
}
elseif ($original_checked) {
$new_name = 'Copy of '.$new_name;
}
$query = 'SELECT '.$title_field.' FROM '.$this->TableName.'
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);
$res = $this->Conn->GetOne($query);
$original_checked = true;
} 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
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);
$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
$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';
$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
*
* @param Array $delete_category_ids
* @author Alex
*/
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)
{
$sql = 'SELECT %s FROM %s WHERE ResourceId = %s';
$ids = $this->Conn->GetCol( sprintf($sql, $id_field, $this->TableName, $resource_id) );
$temp =& $this->Application->recallObject($this->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
$temp->DeleteItems($this->Prefix, $this->Special, $ids);
}
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 )
{
$sql = 'UPDATE %s SET PrimaryCat = 1 WHERE (CategoryId = %s) AND (ItemResourceId = %s)';
$this->Conn->Query( sprintf($sql, $ci_table, reset($cats_left), $resource_id ) );
}
}
}
}
?>
\ No newline at end of file
Property changes on: trunk/kernel/units/general/cat_dbitem.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.8
\ No newline at end of property
+1.9
\ No newline at end of property
Index: trunk/core/units/general/cat_dbitem.php
===================================================================
--- trunk/core/units/general/cat_dbitem.php (revision 2336)
+++ trunk/core/units/general/cat_dbitem.php (revision 2337)
@@ -1,153 +1,155 @@
<?php
class kCatDBItem extends kDBItem {
function Create()
{
if (!$this->Validate()) return false;
$this->SetDBField('ResourceId', $this->Application->NextResourceId());
$this->SetDBField('Modified', mktime());
+ $this->SetDBField('CreatedById', $this->Application->GetVar('u_id'));
$ret = parent::Create();
if($ret)
{
if ( kTempTablesHandler::IsTempTable($this->TableName) ) {
$table = kTempTablesHandler::GetTempName(TABLE_PREFIX.'CategoryItems');
}
else {
$table = TABLE_PREFIX.'CategoryItems';
}
$cat_id = $this->Application->GetVar('m_cat_id');
$query = 'INSERT INTO '.$table.' (CategoryId,ItemResourceId,PrimaryCat)
VALUES ('.$cat_id.','.$this->GetField('ResourceId').',1)';
$this->Conn->Query($query);
}
return $ret;
}
function Update($id=null)
{
$this->VirtualFields['ResourceId'] = true;
$this->SetDBField('Modified', mktime());
+ $this->SetDBField('ModifiedById', $this->Application->GetVar('u_id'));
return parent::Update($id);
}
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;
do {
if ( preg_match('/Copy ([0-9]*) *of (.*)/', $new_name, $regs) ) {
$new_name = 'Copy '.( (int)$regs[1] + 1 ).' of '.$regs[2];
}
elseif ($original_checked) {
$new_name = 'Copy of '.$new_name;
}
$query = 'SELECT '.$title_field.' FROM '.$this->TableName.'
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);
$res = $this->Conn->GetOne($query);
$original_checked = true;
} 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
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);
$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
$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';
$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
*
* @param Array $delete_category_ids
* @author Alex
*/
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)
{
$sql = 'SELECT %s FROM %s WHERE ResourceId = %s';
$ids = $this->Conn->GetCol( sprintf($sql, $id_field, $this->TableName, $resource_id) );
$temp =& $this->Application->recallObject($this->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
$temp->DeleteItems($this->Prefix, $this->Special, $ids);
}
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 )
{
$sql = 'UPDATE %s SET PrimaryCat = 1 WHERE (CategoryId = %s) AND (ItemResourceId = %s)';
$this->Conn->Query( sprintf($sql, $ci_table, reset($cats_left), $resource_id ) );
}
}
}
}
?>
\ No newline at end of file
Property changes on: trunk/core/units/general/cat_dbitem.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.8
\ No newline at end of property
+1.9
\ No newline at end of property

Event Timeline