Index: trunk/kernel/include/itemdb.php =================================================================== diff -u -N -r8104 -r8397 --- trunk/kernel/include/itemdb.php (.../itemdb.php) (revision 8104) +++ trunk/kernel/include/itemdb.php (.../itemdb.php) (revision 8397) @@ -2,8 +2,8 @@ define('FT_OPTION', 1); // option formatter -class clsItemDB -{ +class clsItemDB +{ var $Formatters = Array(); // by Alex var $m_dirtyFieldsMap = array(); var $Data = array(); @@ -13,46 +13,46 @@ var $id_field; var $NoResourceId; var $debuglevel; - + var $Prefix = ''; var $Special = ''; - + var $SelectSQL = 'SELECT * FROM %s WHERE %s'; - + /** * Application object * * @var kApplication */ var $Application = null; - + /** * Connection to database * * @var kDBConnection */ var $Conn = null; - + function clsItemDB() { if (class_exists('kApplication')) { // just in case when aplication is not found - $this->Application =& kApplication::Instance(); + $this->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; @@ -68,14 +68,14 @@ { case FT_OPTION: $this->Formatters[$field]['options'] = $params; - break; + break; } } - /* + /* function FormatFields() { // format item in list data before printing - + foreach($this->Formatters as $field => $formatter) $this->Data[$field] = $this->FormatField($field); } @@ -90,20 +90,20 @@ { case FT_OPTION: return $fmt['options'][ $this->Data[$field] ]; - break; + 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(); @@ -120,7 +120,7 @@ function IdField() { if(!strlen($this->id_field)) - { + { return $this->tablename."Id"; } else @@ -134,17 +134,17 @@ function SetUniqueId($value) { - $var = $this->IdField(); - + $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)) { @@ -162,28 +162,32 @@ { echo '
'.print_r($this->Data, true).'
'; } - + // ================================================================= function GetFormatted($name) - { + { // get formatted field value return $this->FormatField($name); } - function Get($name) - { + function Get($name, $ifempty=null) + { // get un-formatted field value //if( !isset($this->Data[$name]) ) print_pre( debug_backtrace() ); - return $this->HasField($name) ? $this->Data[$name] : ''; + $ret = $this->HasField($name) ? $this->Data[$name] : ''; + if (isset($ifempty) && $ret == '') { + $ret = $this->Data[$ifempty]; + } + return $ret; } // ================================================================= function HasField($name) { // checks if field exists in item - return isset($this->Data[$name]) ? 1 : 0; + return isset($this->Data[$name]) ? 1 : 0; } - + /** * Set's value(-s) of field(-s) specified. * Modifies HasChanges flag automatically. @@ -207,8 +211,8 @@ $this->_Set($name,$value); } } - - + + /** * Set's value(-s) of field(-s) specified. * Modifies HasChanges flag automatically. @@ -240,9 +244,9 @@ } } else - { - foreach($list as $field) - { + { + foreach($list as $field) + { $this->m_dirtyFieldsMap[$field] = $this->Data[$field]; } } @@ -256,9 +260,9 @@ $this->m_dirtyFieldsMap=array(); } else - { - foreach($list as $value) - { + { + foreach($list as $value) + { $varname = "m_" . $value; unset($this->m_dirtyFieldsMap[$value]); } @@ -271,9 +275,9 @@ } function SetFromArray($data, $dirty = false) - { + { if(is_array($data)) - { + { $this->Data = $data; if($dirty) $this->m_dirtyFieldsMap = $data; } @@ -290,7 +294,7 @@ } function Delete() - { + { global $Errors; if($this->Get($this->IdField())==0) @@ -316,7 +320,7 @@ global $Errors, $objSession; if( !$this->raiseEvent('OnBeforeItemUpdate') ) return false; - + if( count($this->m_dirtyFieldsMap) == 0 ) return true; $this->SetModified($UpdatedBy, $modificationDate); @@ -328,17 +332,17 @@ { if( is_null($value) ) { - $value = 'NULL'; + $value = 'NULL'; } else { - $value = $this->adodbConnection->qstr( isset($GLOBALS['_CopyFromEditTable']) ? $value : stripslashes($value) ); + $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.'
'; @@ -349,28 +353,28 @@ } 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 + // 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 = "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) @@ -413,10 +417,10 @@ } } $sql = sprintf('%s)',$sql); - + return $sql; } - + /** * Set's HasChanges flag based on new field * with $name with value $value. @@ -426,27 +430,27 @@ * @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 != '') { + + 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); + $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) { @@ -474,36 +478,36 @@ } */ $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); - + $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); + $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 { @@ -517,7 +521,7 @@ $Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"",get_class($this),"Increment"); return false; } - + if ($calculate_hot) { $this->Set($field,$new_val); } @@ -527,7 +531,7 @@ } function Decrement($field) - { + { global $Errors; $sql = "Update ".$this->tablename." set $field=$field-1 where ".$this->IdField()."=" .(int)$this->UniqueId(); @@ -541,7 +545,7 @@ } $this->Set($field,$this->Get($field)-1); } - + function GetFieldList($UseLoadedData=FALSE) { if(count($this->Data) && $UseLoadedData==TRUE) @@ -554,38 +558,38 @@ } 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)); + $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) @@ -595,7 +599,7 @@ } $sql = sprintf($this->SelectSQL, $this->tablename, implode(' AND ', $conditions) ); // --------- multiple ids allowed: end -------------------- - if($this->debuglevel) echo "Load SQL: $sql
"; + if($this->debuglevel) echo "Load SQL: $sql
"; $result = $this->adodbConnection->Execute($sql); if ($result === false) { @@ -609,13 +613,13 @@ $this->Clean(); return TRUE; } - + function FieldExists($field) { $res = array_key_exists($field,$this->Data); - return $res; + return $res; } - + function ValueExists($Field,$Value) { $sql = "SELECT $Field FROM ".$this->tablename." WHERE $Field='$Value'"; @@ -639,7 +643,7 @@ else $ret = 0; } - + function FieldMin($Field) { $sql = "SELECT Min($Field) as m FROM ".$this->tablename; @@ -651,19 +655,18 @@ 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 + // 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 @@ -675,24 +678,24 @@ $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;*/ } - + } ?>