Application =& kApplication::Instance(); $this->Conn =& $this->Application->GetADODBConnection(); } $this->adodbConnection = &GetADODBConnection(); $this->tablename=""; $this->NoResourceId=0; $this->debuglevel=0; } // ============================================================================================ function GetFormatter($field) { return $this->HasFormatter($field) ? $this->Formatters[$field] : false; } function isLiveTable() { global $objSession; return !preg_match('/'.GetTablePrefix().'ses_'.$objSession->GetSessionKey().'_edit_(.*)/', $this->tablename); } function SetFormatter($field, $type, $params) { // by Alex // all params after 2nd are formmater type specific $this->Formatters[$field]['type'] = $type; switch($type) { case FT_OPTION: $this->Formatters[$field]['options'] = $params; break; } } /* function FormatFields() { // format item in list data before printing foreach($this->Formatters as $field => $formatter) $this->Data[$field] = $this->FormatField($field); } */ function FormatField($field) { // formats single field if it has formatter if( $this->HasFormatter($field) ) { $fmt = $this->Formatters[$field]; switch($fmt['type']) { case FT_OPTION: return $fmt['options'][ $this->Data[$field] ]; break; } } else return $this->Get($field); } function HasFormatter($field) { // checks if formatter is set for field return isset($this->Formatters[$field]) ? 1 : 0; } // ============================================================================================ function UnsetIdField() { $f = $this->IdField(); unset($this->Data[$f]); unset($this->m_dirtyFieldsMap[$f]); } function UnsetField($field) { unset($this->Data[$field]); unset($this->m_dirtyFieldsMap[$field]); } function IdField() { if(!strlen($this->id_field)) { return $this->tablename."Id"; } else return $this->id_field; } function UniqueId() { return $this->Get($this->IdField()); } function SetUniqueId($value) { $var = $this->IdField(); if( $this->UsingTempTable() ) $value = $this->UniqueId(); $this->Set($var, $value); } function SetModified($UserId=NULL,$modificationDate=null) { global $objSession; $keys = array_keys($this->Data); if(in_array("Modified",$keys)) { $this->Set("Modified", isset($modificationDate) ? $modificationDate : adodb_date("U") ); } if(in_array("ModifiedById",$keys)) { if(!$UserId) $UserId = $objSession->Get("PortalUserId"); $this->Set("ModifiedById",$UserId); } } function PrintVars() { echo '
'.print_r($this->Data, true).'
'; } // ================================================================= function GetFormatted($name) { // get formatted field value return $this->FormatField($name); } function Get($name) { // get un-formatted field value //if( !isset($this->Data[$name]) ) print_pre( debug_backtrace() ); return $this->HasField($name) ? $this->Data[$name] : ''; } // ================================================================= function HasField($name) { // checks if field exists in item return isset($this->Data[$name]) ? 1 : 0; } /** * Set's value(-s) of field(-s) specified. * Modifies HasChanges flag automatically. * * @param string/array $name * @param string/array $value * @access public */ function Set($name, $value) { //echo "Setting Field $name: = [$value]
"; if( is_array($name) ) { for ($i=0; $i < sizeof($name); $i++) { $this->_Set($name[$i],$value[$i]); } } else { $this->_Set($name,$value); } } /** * Set's value(-s) of field(-s) specified. * Modifies HasChanges flag automatically. * * @param string $name * @param string $value * @access private */ function _Set($name,$value) { $var = 'm_'.$name; if( !$this->HasField($name) || $this->Data[$name] != $value ) { if( !(isset($_GET['new']) && $_GET['new']) ) { $this->DetectChanges($name, $value); } $this->Data[$name] = $value; $this->m_dirtyFieldsMap[$name] = $value; } } function Dirty($list=NULL) { if($list==NULL) { foreach($this->Data as $field => $value) { $this->m_dirtyFieldsMap[$field] = $value; } } else { foreach($list as $field) { $this->m_dirtyFieldsMap[$field] = $this->Data[$field]; } } } function Clean($list=NULL) { if($list == NULL) { unset($this->m_dirtyFieldsMap); $this->m_dirtyFieldsMap=array(); } else { foreach($list as $value) { $varname = "m_" . $value; unset($this->m_dirtyFieldsMap[$value]); } } } function SetDebugLevel($value) { $this->debuglevel = $value; } function SetFromArray($data, $dirty = false) { if(is_array($data)) { $this->Data = $data; if($dirty) $this->m_dirtyFieldsMap = $data; } } function GetData() { return $this->Data; } function SetData($data, $dirty = false) { $this->SetFromArray($data, $dirty); } function Delete() { global $Errors; if($this->Get($this->IdField())==0) { $Errors->AddError("error.AppError",NULL,'Internal error: Delete requires set id',"",get_class($this),"Delete"); return false; } $sql = sprintf('DELETE FROM %s WHERE %s = %s', $this->tablename, $this->IdField(), $this->UniqueId()); if($this->debuglevel>0) echo $sql."
"; if ($this->adodbConnection->Execute($sql) === false) { $Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"",get_class($this),"Delete"); return false; } return true; } function Update($UpdatedBy=NULL,$modificationDate = null) { global $Errors, $objSession; if( !$this->raiseEvent('OnBeforeItemUpdate') ) return false; if( count($this->m_dirtyFieldsMap) == 0 ) return true; $this->SetModified($UpdatedBy, $modificationDate); $sql = 'UPDATE '.$this->tablename .' SET '; foreach ($this->m_dirtyFieldsMap as $key => $value) { if(!is_numeric($key) && $key != $this->IdField() && $key!='ResourceId') { if( is_null($value) ) { $value = 'NULL'; } else { $value = $this->adodbConnection->qstr( isset($GLOBALS['_CopyFromEditTable']) ? $value : stripslashes($value) ); } $sql .= '`'.$key.'` = '.$value.', '; } } $sql = preg_replace('/(.*), $/','\\1',$sql); $sql .= ' WHERE '.$this->IdField().' = '.$this->adodbConnection->qstr( $this->UniqueId() ); if( $this->debuglevel > 0 ) echo $sql.'
'; if( $this->adodbConnection->Execute($sql) === false ) { $Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"",get_class($this),"Update"); return false; } if( $objSession->GetVariable('HasChanges') == 2 ) $objSession->SetVariable('HasChanges', 1); $this->raiseEvent('OnAfterItemUpdate'); return true; } function ReplaceID($new_id) { // replace item's id, because Update method // is too dummy to do this autommatically // USED in temporary table editing stuff $db =& $this->adodbConnection; $sql = "UPDATE %1\$s SET `%2\$s` = %3\$s WHERE `%2\$s` = %4\$s"; $sql = sprintf($sql, $this->tablename, $this->IdField(), $new_id, (int)$this->UniqueId() ); if($this->debuglevel > 0) echo $sql.'
'; $db->Execute($sql); } function CreateSQL() { global $Errors; $sql = "INSERT INTO ".$this->tablename." ("; $first = 1; foreach ($this->Data as $key => $value) { if($first) { $sql = sprintf("%s %s",$sql,$key); $first = 0; } else { $sql = sprintf("%s, %s",$sql,$key); } } $sql = sprintf('%s ) VALUES (',$sql); $first = 1; foreach ($this->Data as $key => $value) { if( is_array($value) ) { global $debugger; $debugger->dumpVars($value); $debugger->appendTrace(); trigger_error('Value of array type not allowed in method CreateSQL of clsItemDB class', E_USER_ERROR); } if($first) { if(isset($GLOBALS['_CopyFromEditTable'])) $sql = sprintf("%s %s",$sql,$this->adodbConnection->qstr(($value))); else $sql = sprintf("%s %s",$sql,$this->adodbConnection->qstr(stripslashes($value))); $first = 0; } else { if(isset($GLOBALS['_CopyFromEditTable'])) $sql = sprintf("%s, %s",$sql,$this->adodbConnection->qstr(($value))); else $sql = sprintf("%s, %s",$sql,$this->adodbConnection->qstr(stripslashes($value))); } } $sql = sprintf('%s)',$sql); return $sql; } /** * Set's HasChanges flag based on new field * with $name with value $value. * * @param string $name * @param string $value * @access private */ function DetectChanges($name, $value) { global $objSession; if( !is_object($objSession) ) return false; //echo "class: ".get_class($this)."
"; if (!isset($this->Data[$name]) ) return false; if ( getArrayValue($this->Data, $name) != $value && $value != '') { //echo "$name Modified tt ".$this->Data[$name]." tt $value
"; if ($objSession->GetVariable("HasChanges") != 1) { $objSession->SetVariable("HasChanges", 2); } } } function Create() { global $Errors, $objSession; if( !$this->raiseEvent('OnBeforeItemCreate') ) return false; if($this->debuglevel) echo "Creating Item: ".get_class($this)."
"; if($this->NoResourceId!=1 && (int)$this->Get("ResourceId")==0) { $this->Set("ResourceId", GetNextResourceId()); } $sql = $this->CreateSql(); if($this->debuglevel>0) echo $sql."
\n"; if ($this->adodbConnection->Execute($sql) === false) { $Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"",get_class($this),"Create"); return false; } $this->SetUniqueId($this->adodbConnection->Insert_ID()); if ($objSession->GetVariable("HasChanges") == 2) { $objSession->SetVariable("HasChanges", 1); } /*if ($this->adodbConnection->Affected_Rows() > 0) { $objSession->SetVariable("HasChanges", 1); } */ $this->raiseEvent('OnAfterItemCreate'); return true; } function Increment($field, $calculate_hot = false) { global $Errors; if ($calculate_hot) { $sql = "SELECT $field FROM ".$this->tablename." WHERE ".$this->IdField()." = ".$this->UniqueId(); $rs = $this->adodbConnection->Execute($sql); $sql = "SELECT MAX($field) AS max_value FROM ".$this->tablename." WHERE ROUND($field) = ".round($rs->fields[$field]); $rs = $this->adodbConnection->Execute($sql); //echo "MAX VALUE: ".$rs->fields['max_value']."
"; //echo "MAX SQL: $sql
"; $new_val = $rs->fields['max_value'] + 1; $sql = "SELECT count($field) AS count FROM ".$this->tablename." WHERE $field = $new_val"; $rsc = $this->adodbConnection->Execute($sql); while ($rsc->fields['count'] != 0) { $sql = "SELECT count($field) AS count FROM ".$this->tablename." WHERE $field = $new_val"; $rsc = $this->adodbConnection->Execute($sql); //echo "New Value:$new_val
"; if ($rsc->fields['count'] > 0) { $new_val = $new_val + 0.000001; } } $sql = "Update ".$this->tablename." set $field=$new_val where ".$this->IdField()."=" . $this->UniqueId(); } else { $sql = "Update ".$this->tablename." set $field=$field+1 where ".$this->IdField()."=" . $this->UniqueId(); } if($this->debuglevel>0) echo $sql."
"; $result = $this->adodbConnection->Execute($sql); if ($result === false) { $Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"",get_class($this),"Increment"); return false; } if ($calculate_hot) { $this->Set($field,$new_val); } else { $this->Set($field, $this->Get($field) + 1); } } function Decrement($field) { global $Errors; $sql = "Update ".$this->tablename." set $field=$field-1 where ".$this->IdField()."=" .(int)$this->UniqueId(); if($this->debuglevel>0) echo $sql; $result = $this->adodbConnection->Execute($sql); if ($result === false) { $Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"",get_class($this),"Decrement"); return false; } $this->Set($field,$this->Get($field)-1); } function GetFieldList($UseLoadedData=FALSE) { if(count($this->Data) && $UseLoadedData==TRUE) { $res = array_keys($this->Data); } else { $res = $this->adodbConnection->MetaColumnNames($this->tablename); } return $res; } function UsingTempTable() { global $objSession; $temp = $objSession->GetEditTable($this->tablename); $p = GetTablePrefix()."ses"; $t = substr($temp,0,strlen($p)); $ThisTable = substr($this->tablename,0,strlen($p)); if($t==$ThisTable) { return TRUE; } else return FALSE; } function LoadFromDatabase($Id, $IdField = null) // custom IdField by Alex { global $objSession,$Errors; if(!isset($Id)) { $Errors->AddError("error.AppError",NULL,'Internal error: LoadFromDatabase id',"",get_class($this),"LoadFromDatabase"); return false; } // --------- multiple ids allowed: begin ----------------- $id_field = isset($IdField) ? $IdField : $this->IdField(); if( !is_array($id_field) ) $id_field = Array($id_field); if( !is_array($Id) ) $Id = Array($Id); $i = 0; $id_count = count($id_field); $conditions = Array(); while($i < $id_count) { $conditions[] = "(`".$id_field[$i]."` = '".$Id[$i]."')"; $i++; } $sql = sprintf($this->SelectSQL, $this->tablename, implode(' AND ', $conditions) ); // --------- multiple ids allowed: end -------------------- if($this->debuglevel) echo "Load SQL: $sql
"; $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; if($this->debuglevel) print_pre($data); if(is_array($data)) $this->SetFromArray($data); $this->Clean(); return TRUE; } function FieldExists($field) { $res = array_key_exists($field,$this->Data); return $res; } function ValueExists($Field,$Value) { $sql = "SELECT $Field FROM ".$this->tablename." WHERE $Field='$Value'"; $rs = $this->adodbConnection->Execute($sql); if($rs && !$rs->EOF) { return TRUE; } else return FALSE; } function FieldMax($Field) { $sql = "SELECT Max($Field) as m FROM ".$this->tablename; $rs = $this->adodbConnection->Execute($sql); if($rs && !$rs->EOF) { $ret = $rs->fields["m"]; } else $ret = 0; } function FieldMin($Field) { $sql = "SELECT Min($Field) as m FROM ".$this->tablename; $rs = $this->adodbConnection->Execute($sql); if($rs && !$rs->EOF) { $ret = $rs->fields["m"]; } else $ret = 0; } function TableExists($table = null) { static $tables_found = Array (); if($table == null) $table = $this->tablename; if (!isset($tables_found[$table])) { // checks if table specified in item exists in db $db =& GetADODBConnection(); $sql = "SHOW TABLES LIKE '%s'"; $rs = $db->Execute( sprintf($sql, $table) ); if ($rs->RecordCount() == 1) { // table exists in normal case $tables_found[$table] = 1; } else { // check if table exists in lowercase $rs = $db->Execute( sprintf($sql, strtolower($table) ) ); $tables_found[$table] = $rs->RecordCount() == 1 ? 1 : 0; } } return $tables_found[$table]; } function raiseEvent($name, $id = null) { return true; /*if (!getArrayValue($GLOBALS, '_CopyFromEditTable')) { return true; } if( !isset($id) ) $id = $this->GetID(); $event = new kEvent( Array('name'=>$name,'prefix'=>$this->Prefix,'special'=>$this->Special) ); $event->setEventParam('id', $id); $this->Application->HandleEvent($event); return $event->status == erSUCCESS ? true : false;*/ } } ?>