Index: branches/5.3.x/core/kernel/db/dbitem.php =================================================================== diff -u -N -r15677 -r15698 --- branches/5.3.x/core/kernel/db/dbitem.php (.../dbitem.php) (revision 15677) +++ branches/5.3.x/core/kernel/db/dbitem.php (.../dbitem.php) (revision 15698) @@ -1,6 +1,6 @@ validator) ) { - $validator_class = $this->Application->getUnitOption($this->Prefix, 'ValidatorClass', 'kValidator'); + $validator_class = $this->getUnitConfig()->getValidatorClass('kValidator'); $this->validator = $this->Application->makeClass($validator_class); } @@ -397,7 +397,7 @@ if ( isset($id_field_name) ) { // restore original IDField from unit config - $this->IDField = $this->Application->getUnitOption($this->Prefix, 'IDField'); + $this->IDField = $this->getUnitConfig()->getIDField(); } if (($id === false) || !$keys_sql) { @@ -1012,45 +1012,52 @@ */ public function NameCopy($master=null, $foreign_key=null, $title_field=null, $format='Copy %1$s of %2$s') { - if (!isset($title_field)) { - $title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField'); - if (!$title_field || isset($this->CalculatedFields[$title_field]) ) return; + if ( !isset($title_field) ) { + $title_field = $this->getUnitConfig()->getTitleField(); + + if ( !$title_field || isset($this->CalculatedFields[$title_field]) ) { + return; + } } - $new_name = $this->GetDBField($title_field); $original_checked = false; + $new_name = $this->GetDBField($title_field); + do { - if ( preg_match('/'.sprintf($format, '([0-9]*) *', '(.*)').'/', $new_name, $regs) ) { - $new_name = sprintf($format, ($regs[1]+1), $regs[2]); + if ( preg_match('/' . sprintf($format, '([0-9]*) *', '(.*)') . '/', $new_name, $regs) ) { + $new_name = sprintf($format, ($regs[1] + 1), $regs[2]); } - elseif ($original_checked) { + elseif ( $original_checked ) { $new_name = sprintf($format, '', $new_name); } // if we are cloning in temp table this will look for names in temp table, // since object' TableName contains correct TableName (for temp also!) // if we are cloning live - look in live - $query = 'SELECT '.$title_field.' FROM '.$this->TableName.' - WHERE '.$title_field.' = '.$this->Conn->qstr($new_name); + $sql = 'SELECT ' . $title_field . ' + FROM ' . $this->TableName . ' + WHERE ' . $title_field . ' = ' . $this->Conn->qstr($new_name); $foreign_key_field = getArrayValue($master, 'ForeignKey'); - $foreign_key_field = is_array($foreign_key_field) ? $foreign_key_field[ $master['ParentPrefix'] ] : $foreign_key_field; + $foreign_key_field = is_array($foreign_key_field) ? $foreign_key_field[$master['ParentPrefix']] : $foreign_key_field; - if ($foreign_key_field && isset($foreign_key)) { - $query .= ' AND '.$foreign_key_field.' = '.$foreign_key; + if ( $foreign_key_field && isset($foreign_key) ) { + $sql .= ' AND ' . $foreign_key_field . ' = ' . $foreign_key; } - $res = $this->Conn->GetOne($query); + $res = $this->Conn->GetOne($sql); - /*// if not found in live table, check in temp table if applicable - if ($res === false && $object->Special == 'temp') { - $query = 'SELECT '.$name_field.' FROM '.$this->GetTempName($master['TableName']).' - WHERE '.$name_field.' = '.$this->Conn->qstr($new_name); - $res = $this->Conn->GetOne($query); + // if not found in live table, check in temp table if applicable + /*if ( $res === false && $this->Special == 'temp' ) { + $sql = 'SELECT ' . $name_field . ' + FROM ' . $this->Application->GetTempName($master['TableName']) . ' + WHERE ' . $name_field . ' = ' . $this->Conn->qstr($new_name); + $res = $this->Conn->GetOne($sql); }*/ $original_checked = true; - } while ($res !== false); + } while ( $res !== false ); + $this->SetDBField($title_field, $new_name); } @@ -1087,44 +1094,48 @@ */ public function setTempID() { - $new_id = (int)$this->Conn->GetOne('SELECT MIN('.$this->IDField.') FROM '.$this->TableName); - if($new_id > 0) $new_id = 0; + $new_id = (int)$this->Conn->GetOne('SELECT MIN(' . $this->IDField . ') FROM ' . $this->TableName); + + if ( $new_id > 0 ) { + $new_id = 0; + } + --$new_id; - $this->Conn->Query('UPDATE '.$this->TableName.' SET `'.$this->IDField.'` = '.$new_id.' WHERE `'.$this->IDField.'` = '.$this->GetID()); + $this->Conn->Query('UPDATE ' . $this->TableName . ' SET `' . $this->IDField . '` = ' . $new_id . ' WHERE `' . $this->IDField . '` = ' . $this->GetID()); - if ($this->ShouldLogChanges(true)) { + if ( $this->ShouldLogChanges(true) ) { // Updating TempId in ChangesLog, if changes are disabled $ses_var_name = $this->Application->GetTopmostPrefix($this->Prefix) . '_changes_' . $this->Application->GetTopmostWid($this->Prefix); $changes = $this->Application->RecallVar($ses_var_name); $changes = $changes ? unserialize($changes) : Array (); - if ($changes) { + if ( $changes ) { foreach ($changes as $key => $rec) { - if ($rec['Prefix'] == $this->Prefix && $rec['ItemId'] == $this->GetID()) { + if ( $rec['Prefix'] == $this->Prefix && $rec['ItemId'] == $this->GetID() ) { // change log for record, that's ID was just updated -> update in change log record too $changes[$key]['ItemId'] = $new_id; } - if ($rec['MasterPrefix'] == $this->Prefix && $rec['MasterId'] == $this->GetID()) { + if ( $rec['MasterPrefix'] == $this->Prefix && $rec['MasterId'] == $this->GetID() ) { // master item id was changed $changes[$key]['MasterId'] = $new_id; } - if (in_array($this->Prefix, $rec['ParentPrefix']) && $rec['ParentId'][$this->Prefix] == $this->GetID()) { + if ( in_array($this->Prefix, $rec['ParentPrefix']) && $rec['ParentId'][$this->Prefix] == $this->GetID() ) { // change log record of given item's sub item -> update changed id's in dependent fields $changes[$key]['ParentId'][$this->Prefix] = $new_id; - if (array_key_exists('DependentFields', $rec)) { + if ( array_key_exists('DependentFields', $rec) ) { // these are fields from table of $rec['Prefix'] table! - // when one of dependent fields goes into idfield of it's parent item, that was changed - $parent_table_key = $this->Application->getUnitOption($rec['Prefix'], 'ParentTableKey'); - $parent_table_key = is_array($parent_table_key) ? $parent_table_key[$this->Prefix] : $parent_table_key; + // when one of dependent fields goes into id field of it's parent item, that was changed + $config = $this->Application->getUnitConfig($rec['Prefix']); - if ($parent_table_key == $this->IDField) { - $foreign_key = $this->Application->getUnitOption($rec['Prefix'], 'ForeignKey'); - $foreign_key = is_array($foreign_key) ? $foreign_key[$this->Prefix] : $foreign_key; + $parent_table_key = $config->getParentTableKey($this->Prefix); + if ( $parent_table_key == $this->IDField ) { + $foreign_key = $config->getForeignKey($this->Prefix); + $changes[$key]['DependentFields'][$foreign_key] = $new_id; } } @@ -1170,12 +1181,14 @@ */ public function ShouldLogChanges($log_changes = null) { - if (!isset($log_changes)) { + $config = $this->getUnitConfig(); + + if ( !isset($log_changes) ) { // specific logging mode no forced -> use global logging settings - $log_changes = $this->Application->getUnitOption($this->Prefix, 'LogChanges') || $this->Application->ConfigValue('UseChangeLog'); + $log_changes = $config->getLogChanges() || $this->Application->ConfigValue('UseChangeLog'); } - return $log_changes && !$this->Application->getUnitOption($this->Prefix, 'ForceDontLogChanges'); + return $log_changes && !$config->getForceDontLogChanges(); } protected function LogChanges($main_prefix, $mode) @@ -1205,7 +1218,8 @@ else { // sub item // collect foreign key values (for serial reset) - $foreign_keys = $this->Application->getUnitOption($this->Prefix, 'ForeignKey', Array ()); + $config = $this->getUnitConfig(); + $foreign_keys = $config->getForeignKey(null, Array ()); $dependent_fields = $fields_hash['ParentId'] = $fields_hash['ParentPrefix'] = Array (); /* @var $foreign_keys Array */ @@ -1218,7 +1232,7 @@ } else { $dependent_fields[$foreign_keys] = $this->GetDBField($foreign_keys); - $fields_hash['ParentPrefix'] = Array ( $this->Application->getUnitOption($this->Prefix, 'ParentPrefix') ); + $fields_hash['ParentPrefix'] = Array ( $config->getParentPrefix() ); $fields_hash['ParentId'][ $fields_hash['ParentPrefix'][0] ] = $this->getParentId('auto'); } @@ -1272,51 +1286,51 @@ { $current_id = $this->GetID(); $current_prefix = $this->Prefix; + $current_config = $this->Application->getUnitConfig($current_prefix); - if ($parent_prefix == 'auto') { - $parent_prefix = $this->Application->getUnitOption($current_prefix, 'ParentPrefix'); + if ( $parent_prefix == 'auto' ) { + $parent_prefix = $current_config->getParentPrefix(); } - if (!$parent_prefix) { + if ( !$parent_prefix ) { return $current_id; } do { // field in this table - $foreign_key = $this->Application->getUnitOption($current_prefix, 'ForeignKey'); - $foreign_key = is_array($foreign_key) ? $foreign_key[$parent_prefix] : $foreign_key; + $foreign_key = $current_config->getForeignKey($parent_prefix); // get foreign key value for $current_prefix - if ($current_prefix == $this->Prefix) { + if ( $current_prefix == $this->Prefix ) { $foreign_key_value = $this->GetDBField($foreign_key); } else { - $id_field = $this->Application->getUnitOption($current_prefix, 'IDField'); - $table_name = $this->Application->getUnitOption($current_prefix, 'TableName'); + $table_name = $current_config->getTableName(); - if ($this->IsTempTable()) { + if ( $this->IsTempTable() ) { $table_name = $this->Application->GetTempName($table_name, 'prefix:' . $current_prefix); } $sql = 'SELECT ' . $foreign_key . ' FROM ' . $table_name . ' - WHERE ' . $id_field . ' = ' . $current_id; + WHERE ' . $current_config->getIDField() . ' = ' . $current_id; $foreign_key_value = $this->Conn->GetOne($sql); } // field in parent table - $parent_table_key = $this->Application->getUnitOption($current_prefix, 'ParentTableKey'); - $parent_table_key = is_array($parent_table_key) ? $parent_table_key[$parent_prefix] : $parent_table_key; + $parent_table_key = $current_config->getParentTableKey($parent_prefix); - $parent_id_field = $this->Application->getUnitOption($parent_prefix, 'IDField'); - $parent_table_name = $this->Application->getUnitOption($parent_prefix, 'TableName'); + $parent_config = $this->Application->getUnitConfig($parent_prefix); - if ($this->IsTempTable()) { + $parent_id_field = $parent_config->getIDField(); + $parent_table_name = $parent_config->getTableName(); + + if ( $this->IsTempTable() ) { $parent_table_name = $this->Application->GetTempName($parent_table_name, 'prefix:' . $current_prefix); } - if ($parent_id_field == $parent_table_key) { - // sub-item is related by parent item idfield + if ( $parent_id_field == $parent_table_key ) { + // sub-item is related by parent item id field $current_id = $foreign_key_value; } else { @@ -1328,11 +1342,12 @@ } $current_prefix = $parent_prefix; + $current_config = $this->Application->getUnitConfig($current_prefix); - if (!$top_most) { + if ( !$top_most ) { break; } - } while ( $parent_prefix = $this->Application->getUnitOption($current_prefix, 'ParentPrefix') ); + } while ( $parent_prefix = $current_config->getParentPrefix() ); return $current_id; } @@ -1344,10 +1359,11 @@ */ public function GetTitleField() { - $title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField'); + $title_field = $this->getUnitConfig()->getTitleField(); - if ($title_field) { + if ( $title_field ) { $value = $this->GetField($title_field); + return $value ? Array ($title_field => $value) : Array (); } @@ -1544,18 +1560,4 @@ { $this->Loaded = $is_loaded; } - - /** - * Returns item's first status field - * - * @return string - * @access public - */ - public function getStatusField() - { - $status_fields = $this->Application->getUnitOption($this->Prefix, 'StatusField'); - - return array_shift($status_fields); - } - } \ No newline at end of file