Index: trunk/kernel/include/custommetadata.php =================================================================== diff -u -r3872 -r4077 --- trunk/kernel/include/custommetadata.php (.../custommetadata.php) (revision 3872) +++ trunk/kernel/include/custommetadata.php (.../custommetadata.php) (revision 4077) @@ -12,100 +12,48 @@ function clsCustomMetaData($CustomDataId=-1,$table="CustomMetaData") { - $this->clsItem(); + parent::clsItem(); $this->tablename=GetTablePrefix()."CustomMetaData"; $this->type=12; $this->BasePermission=""; $this->id_field = "CustomDataId"; $this->NoResourceId=1; //set this to avoid using a resource ID - - if(isset($CustomDataId)) - $this->LoadFromDatabase($CustomDataId); } +} - function Validate() - { - global $Errors; - - $dataValid = true; - if(!strlen($this->Get("ResourceId"))) - { - $Errors->AddError("error.fieldIsRequired",'ResourceId',"","",get_class($this),"Validate"); - $dataValid = false; - } - return $dataValid; - } - - function SetFieldName($name) - { - $this->FieldName = $name; - } - - function GetFieldName() - { - return $this->FieldName; - } - - function Save() - { - if(is_numeric($this->Get("CustomDataId"))) - { - $this->Update(); - } - else - $this->Create(); - } - - /* - function LoadFromDatabase($Id) - { - global $objSession, $Errors; - - if(!isset($Id)) - { - $Errors->AddError("error.AppError",NULL,'Internal error: LoadFromDatabase id',"",get_class($this),"LoadFromDatabase"); - return false; - } - $sql = sprintf("SELECT * FROM ".$this->SourceTable." WHERE CustomDataId = '%s'",$Id); - $result = $this->adodbConnection->Execute($sql); - if ($result === false) - { - $Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"",get_class($this),"LoadFromDatabase"); - return false; - } - $data = $result->fields; - $this->SetFromArray($data); - $this->Clean(); - return true; - } - */ -} /*clsCustomMetaData*/ - class clsCustomDataList extends clsItemCollection { - - function clsCustomDataList($table="") - { - $this->clsItemCollection(); - $this->classname = "clsCustomMetaData"; - if(!strlen($table)) - $table = GetTablePrefix()."CustomMetaData"; - $this->SetTable('live', $table); - } - - function LoadResource($ResourceId) - { - $sql = "SELECT * FROM ".$this->SourceTable." WHERE ResourceId=".$ResourceId; - $this->Query_Item($sql); - return $this->Items; - } + function clsCustomDataList() + { + parent::clsItemCollection(); + $this->classname = 'clsCustomMetaData'; + } + + function LoadResource($ResourceId) + { + // TO REMOVE + } - function DeleteResource($ResourceId) - { - if(!is_numeric($ResourceId))return; - $sql = "DELETE FROM ".$this->SourceTable." WHERE ResourceID=".$ResourceId; - $this->adodbConnection->Execute($sql); - } + function DeleteResource($ResourceId, $main_prefix) + { + if (!$ResourceId) return false; + + $custom_table = $this->Application->getUnitOption($main_prefix.'-cdata', 'TableName'); + $sql = 'DELETE FROM '.$custom_table.' + WHERE ResourceId = '.$ResourceId; + $this->adodbConnection->Execute($sql); + } + + function CopyResource($OldId,$NewId, $main_prefix) + { + $custom_data =& $this->Application->recallObject($main_prefix.'-cdata.-item', null, Array('skip_autoload' => true)); + $custom_data->Load($OldId, 'ResourceId'); + + if ($custom_data->isLoaded()) { + $custom_data->SetDBField('ResourceId', $NewId); + $custom_data->Create(); + } + } function &SetFieldValue($FieldId,$ResourceId,$Value) { @@ -171,108 +119,57 @@ return $found ? ($return_index ? $i : $d) : false; } - function SaveData() + function SaveData($main_prefix, $resource_id) { - foreach($this->Items as $f) { - $FieldId = $f->Get('CustomFieldId'); - $value = $f->Get('Value'); - $ResId = $f->Get('ResourceId'); - $DataId = $f->Get('CustomDataId'); - - if ($FieldId > 0) { - $db_value = $this->adodbConnection->qstr( isset($GLOBALS['_CopyFromEditTable']) ? $value : stripslashes($value) ); - - if (is_numeric($DataId)) { - $sql = 'UPDATE '.$this->SourceTable.' SET Value = '.$db_value.' WHERE CustomFieldId = '.$FieldId.' AND ResourceId = '.$ResId; - $this->adodbConnection->Execute($sql); - } - else { - $sql = 'INSERT INTO '.$this->SourceTable.' (ResourceId,CustomFieldId,Value) VALUES ('.$ResId.','.$FieldId.','.$db_value.')'; - $this->adodbConnection->Execute($sql); - } - } - else - { - $sql = 'DELETE FROM '.$this->SourceTable.' WHERE CustomDataId = '.$DataId; - $this->adodbConnection->Execute($sql); - } + $ml_formatter =& $this->Application->recallObject('kMultiLanguage'); + $custom_data =& $this->Application->recallObject($main_prefix.'-cdata', null, Array('skip_autoload' => true)); + $custom_data->Load($resource_id, 'ResourceId'); + + foreach($this->Items as $f) { + $custom_id = $f->Get('CustomFieldId'); + $value = isset($GLOBALS['_CopyFromEditTable']) ? $f->Get('Value') : stripslashes($f->Get('Value')); + + $custom_name = $ml_formatter->LangFieldName('cust_'.$custom_id); + $custom_data->SetDBField($custom_name, $value); } - - $rs = $this->adodbConnection->Execute("SELECT * FROM ".$this->SourceTable." WHERE CustomDataId=0 "); - while($rs && !$rs->EOF) - { - $m = $this->adodbConnection->Execute("SELECT MIN(CustomDataId) as MinValue FROM ".$this->SourceTable); - $NewId = $m->fields["MinValue"]-1; - - $sql = "UPDATE ".$this->SourceTable." SET CustomDataId=$NewId WHERE ResourceId=".$rs->fields["ResourceId"]." AND "; - $sql .= "CustomFieldId=".$rs->fields["CustomFieldId"]; - $this->adodbConnection->Execute($sql); - $rs->MoveNext(); - } + + $custom_data->SetDBField('ResourceId', $resource_id); + return $custom_data->isLoaded() ? $custom_data->Update() : $custom_data->Create(); } - function CopyToEditTable($idfield, $idlist) - { - global $objSession; - - $edit_table = $objSession->GetEditTable($this->SourceTable); - @$this->adodbConnection->Execute("DROP TABLE IF EXISTS $edit_table"); - if(is_array($idlist)) - { - $list = implode(",",$idlist); - } - else - $list = $idlist; - $query = "SELECT * FROM ".$this->SourceTable." WHERE $idfield IN ($list)"; - $insert = "CREATE TABLE ".$edit_table." ".$query; - if((int)$GLOBALS["debuglevel"]) - echo $insert; - $this->adodbConnection->Execute($insert); - $this->SourceTable = $edit_table; - $this->SaveData(); - - } - - function CopyFromEditTable($idfield, $ResourceIds) - { - global $objSession; - $GLOBALS['_CopyFromEditTable']=1; - - $edit_table = $objSession->GetEditTable($this->SourceTable); - $idlist = array(); - - //$this->adodbConnection->Execute($sql); - - $sql = "SELECT * FROM $edit_table"; - $this->Clear(); - $rs = $this->adodbConnection->Execute($sql); - while($rs && !$rs->EOF) - { - $data = $rs->fields; - $c = $this->AddItemFromArray($data); - - $c->Dirty(); - if($data[$idfield]>0) - { - - $c->Update(); - } - else - { - $c->UnsetIdField(); - $c->Create(); - } - $idlist[] = $c->Get($idfield); - - $rs->MoveNext(); - } - - $sql = "DELETE FROM ".$this->SourceTable." WHERE ResourceId IN (".implode(',', $ResourceIds).') '.(count($idlist) > 0 ? "AND $idfield NOT IN (".implode(",",$idlist).")" : ""); - $this->adodbConnection->Execute($sql); + function &getTempHandler($prefix) + { + if (strlen($prefix) > 2 || strlen($prefix) == 0) { + // not e.g. bb, c, u, but CustomFieldId :) or empty at all + $this->Application->reportError(get_class($this), 'CopyToEditTable'); + } - @$this->adodbConnection->Execute("DROP TABLE IF EXISTS $edit_table"); - unset($GLOBALS['_CopyFromEditTable']); - } + $temp_handler =& $this->Application->recallObject($prefix.'-cdata_TempHandler', 'kTempTablesHandler'); + return $temp_handler; + } + + function CopyToEditTable($main_prefix, $idlist) + { + $temp_handler =& $this->getTempHandler($main_prefix); + $temp_handler->DoCopyLiveToTemp($temp_handler->Tables, $idlist); + } -} /* clsCustomDataList */ + function CopyFromEditTable($main_prefix) + { + $temp_handler =& $this->getTempHandler($main_prefix); + $temp_handler->DoCopyTempToOriginal($temp_handler->Tables); + } + + function PurgeEditTable($main_prefix) + { + $temp_handler =& $this->getTempHandler($main_prefix); + $temp_handler->CancelEdit(); + } + + function CreateEmptyEditTable($main_prefix) + { + $temp_handler =& $this->getTempHandler($main_prefix); + $temp_handler->DoCopyLiveToTemp($temp_handler->Tables, Array(0)); + } +} ?>