Index: trunk/core/kernel/db/dblist.php
===================================================================
diff -u -r3559 -r3703
--- trunk/core/kernel/db/dblist.php (.../dblist.php) (revision 3559)
+++ trunk/core/kernel/db/dblist.php (.../dblist.php) (revision 3703)
@@ -32,14 +32,14 @@
*/
class kDBList extends kDBBase {
-
+
/**
* Holds totals for fields specified in config
*
* @var Array
*/
var $Totals = Array();
-
+
/**
* Description
*
@@ -277,7 +277,7 @@
$this->NoFilterCount = $this->RecordsCount;
return;
}
-
+
$sql = $this->getCountSQL($system_sql);
if( $this->GetGroupClause() )
{
@@ -287,7 +287,7 @@
{
$this->NoFilterCount = (int)$this->Conn->GetOne($sql);
}
-
+
$this->Counted = true;
}
@@ -316,7 +316,7 @@
{
if (!$force && $this->Queried) return true;
$q = $this->GetSelectSQL();
-
+
//$rs = $this->Conn->SelectLimit($q, $this->PerPage, $this->Offset);
//in case we have not counted records try to select one more item to find out if we have something more than perpage
@@ -340,28 +340,28 @@
function CalculateTotals()
{
$this->Totals = Array();
-
+
$fields = Array();
foreach($this->Fields as $field_name => $field_options)
{
$totals = getArrayValue($field_options, 'totals');
if(!$totals) continue;
-
+
$calculated_field = isset($this->CalculatedFields[$field_name]) && isset($this->VirtualFields[$field_name]);
$db_field = !isset($this->VirtualFields[$field_name]);
-
+
if($calculated_field || $db_field)
{
$field_expression = $calculated_field ? $this->CalculatedFields[$field_name] : '`'.$this->TableName.'`.`'.$field_name.'`';
$fields[$field_name] = $totals.'('.$field_expression.') AS '.$field_name.'_'.$totals;
}
}
-
+
if(!$fields) return false;
-
+
$sql = $this->GetSelectSQL(true, false);
$fields = implode(', ', $fields);
-
+
if ( preg_match("/DISTINCT(.*?)FROM(?!_)/is",$sql,$regs ) )
{
$sql = preg_replace("/^.*SELECT DISTINCT(.*?)FROM(?!_)/is", 'SELECT '.$fields.' FROM', $sql);
@@ -370,9 +370,9 @@
{
$sql = preg_replace("/^.*SELECT(.*?)FROM(?!_)/is", 'SELECT '.$fields.' FROM ', $sql);
}
-
+
$totals = $this->Conn->Query($sql);
-
+
foreach($totals as $totals_row)
{
foreach($totals_row as $total_field => $field_value)
@@ -382,12 +382,12 @@
}
}
}
-
+
function getTotal($field, $total_function)
{
return $this->Totals[$field.'_'.$total_function];
}
-
+
/**
* Builds full select query except for LIMIT clause
*
@@ -533,12 +533,12 @@
$lang = $this->Application->GetVar('m_lang');
$field = 'l'.$lang.'_'.$field;
}
-
+
if( !isset($this->Fields[$field]) )
{
trigger_error('Incorrect sorting defined (field = '.$field.'; direction = '.$direction.') in config for prefix '.$this->Prefix.'', E_USER_WARNING);
}
-
+
$this->OrderFields[] = Array($field, $direction);
}
@@ -752,8 +752,12 @@
if($parent_prefix)
{
$parent_table_key = $this->Application->getUnitOption($this->Prefix, 'ParentTableKey');
+ if (is_array($parent_table_key)) $parent_table_key = getArrayValue($parent_table_key, $parent_prefix);
$foreign_key_field = $this->Application->getUnitOption($this->Prefix, 'ForeignKey');
+ if (is_array($foreign_key_field)) $foreign_key_field = getArrayValue($foreign_key_field, $parent_prefix);
+ if (!$parent_table_key || !$foreign_key_field) return ;
+
$parent_object =& $this->Application->recallObject($parent_prefix.'.'.$special);
$parent_id = $parent_object->GetDBField($parent_table_key);
Index: trunk/core/kernel/utility/temp_handler.php
===================================================================
diff -u -r3654 -r3703
--- trunk/core/kernel/utility/temp_handler.php (.../temp_handler.php) (revision 3654)
+++ trunk/core/kernel/utility/temp_handler.php (.../temp_handler.php) (revision 3703)
@@ -277,9 +277,11 @@
if (!getArrayValue($sub_table, 'AutoDelete')) continue;
$sub_TableName = ($object->mode == 't') ? $this->GetTempName($sub_table['TableName']) : $sub_table['TableName'];
- $foreign_key_field = is_array($sub_table['ForeignKey']) ? $sub_table['ForeignKey'][$master['Prefix']] : $sub_table['ForeignKey'];
- $parent_key_field = is_array($sub_table['ParentTableKey']) ? $sub_table['ParentTableKey'][$master['Prefix']] : $sub_table['ParentTableKey'];
+ $foreign_key_field = is_array($sub_table['ForeignKey']) ? getArrayValue($sub_table, 'ForeignKey', $master['Prefix']) : $sub_table['ForeignKey'];
+ $parent_key_field = is_array($sub_table['ParentTableKey']) ? getArrayValue($sub_table, 'ParentTableKey', $master['Prefix']) : $sub_table['ParentTableKey'];
+ if (!$foreign_key_field || !$parent_key_field) continue;
+
$query = 'SELECT '.$sub_table['IdField'].' FROM '.$sub_TableName.'
WHERE '.$foreign_key_field.' = '.$original_values[$parent_key_field];
@@ -343,6 +345,7 @@
foreach ($master['SubTables'] as $sub_table) {
$parent_key = is_array($sub_table['ParentTableKey']) ? $sub_table['ParentTableKey'][$master['Prefix']] : $sub_table['ParentTableKey'];
+ if (!$parent_key) continue;
if ( $ids != '' && $parent_key != $key_field ) {
$query = 'SELECT '.$parent_key.' FROM '.$master['TableName'].'
@@ -527,10 +530,11 @@
function UpdateForeignKeys($master, $live_id, $temp_id) {
foreach ($master['SubTables'] as $sub_table) {
+ $foreign_key_field = is_array($sub_table['ForeignKey']) ? getArrayValue($sub_table, 'ForeignKey', $master['Prefix']) : $sub_table['ForeignKey'];
+ if (!$foreign_key_field) return;
+
list ($live_foreign_key, $temp_foreign_key) = $this->GetForeignKeys($master, $sub_table, $live_id, $temp_id);
- $foreign_key_field = is_array($sub_table['ForeignKey']) ? $sub_table['ForeignKey'][$master['Prefix']] : $sub_table['ForeignKey'];
-
//Update ForeignKey in sub TEMP table
if ($live_foreign_key != $temp_foreign_key) {
$query = 'UPDATE '.$this->GetTempName($sub_table['TableName']).'
@@ -550,8 +554,9 @@
// delete records from live table by foreign key, so that records deleted from temp table
// get deleted from live
if (count($current_ids) > 0 && !in_array($table_sig, $this->CopiedTables) ) {
+ $foreign_key_field = is_array($sub_table['ForeignKey']) ? getArrayValue($sub_table, 'ForeignKey', $master['Prefix']) : $sub_table['ForeignKey'];
+ if (!$foreign_key_field) continue;
$foreign_keys = $this->GetForeignKeys($master, $sub_table, $current_ids);
- $foreign_key_field = is_array($sub_table['ForeignKey']) ? $sub_table['ForeignKey'][$master['Prefix']] : $sub_table['ForeignKey'];
if (count($foreign_keys) > 0) {
$query = 'SELECT '.$sub_table['IdField'].' FROM '.$sub_table['TableName'].'
WHERE '.$foreign_key_field.' IN ('.join(',', $foreign_keys).')';
Index: trunk/core/kernel/kbase.php
===================================================================
diff -u -r3307 -r3703
--- trunk/core/kernel/kbase.php (.../kbase.php) (revision 3307)
+++ trunk/core/kernel/kbase.php (.../kbase.php) (revision 3703)
@@ -21,7 +21,7 @@
* @access public
*/
var $Prefix='';
-
+
/**
* Special, that was used
* to create an object
@@ -30,9 +30,9 @@
* @access public
*/
var $Special='';
-
+
var $OriginalParams;
-
+
/**
* Set's application
*
@@ -43,7 +43,7 @@
{
$this->Application =& kApplication::Instance();
}
-
+
/**
* Create new instance of object
*
@@ -53,7 +53,7 @@
{
return new kBase();
}
-
+
/**
* Set's prefix and special
*
@@ -68,7 +68,7 @@
$this->Special=$special;
$this->OriginalParams = $event_params;
}
-
+
/**
* Returns joined prefix
* and special if any
@@ -83,39 +83,39 @@
$ret = $this->Prefix.$separator.$this->Special;
return rtrim($ret, $separator);
}
-
+
function &getProperty($property_name)
{
return $this->$property_name;
}
-
+
function setProperty($property_name, &$property_value)
{
$this->$property_name =& $property_value;
}
}
class kHelper extends kBase {
-
+
/**
* Connection to database
*
* @var kDBConnection
* @access public
*/
var $Conn;
-
+
function kHelper()
{
parent::kBase();
$this->Conn =& $this->Application->GetADODBConnection();
}
-
+
function InitHelper()
{
-
+
}
-
+
}
class kDBBase extends kBase {
@@ -126,69 +126,69 @@
* @access public
*/
var $Conn;
-
+
/**
* Description
*
* @var string Name of primary key field for the item
* @access public
*/
var $IDField;
-
+
/**
* Holds SELECT, FROM, JOIN parts of SELECT query
*
* @var string
* @access public
*/
var $SelectClause;
-
+
/**
* Fields allowed to be set (from table + virtual)
*
* @var Array
* @access private
*/
var $Fields=Array();
-
+
/**
* All virtual field names
*
* @var Array
* @access private
*/
var $VirtualFields=Array();
-
+
/**
* Fields that need to be queried using custom expression, e.g. IF(...) AS value
*
* @var Array
* @access private
*/
var $CalculatedFields = Array();
-
+
/**
* Description
*
* @var string Item' database table name, without prefix
* @access public
*/
var $TableName;
-
+
/**
* Allows to determine object's table status ('temp' - temp table, '' - live table)
*
* @var string
* @access public
*/
var $mode='';
-
+
function kDBBase()
{
parent::kBase();
$this->Conn =& $this->Application->GetADODBConnection();
}
-
+
/**
* Set current item' database table name
*
@@ -200,7 +200,7 @@
{
$this->TableName = $table_name;
}
-
+
/**
* Set object' TableName to Live table from config
*
@@ -210,7 +210,7 @@
{
$this->TableName = $this->Application->getUnitOption($this->Prefix, 'TableName');
}
-
+
/**
* Set object' TableName to Temp table from config
*
@@ -222,7 +222,7 @@
$this->SetTableName( kTempTablesHandler::GetTempName($this->TableName) );
$this->mode = 't';
}
-
+
/**
* Checks if object uses temp table
*
@@ -232,7 +232,7 @@
{
return kTempTablesHandler::IsTempTable($this->TableName);
}
-
+
/**
* Sets SELECT part of list' query
*
@@ -244,13 +244,13 @@
{
$this->SelectClause = $sql;
}
-
+
function GetSelectSQL($base_query=null)
{
if( !isset($base_query) ) $base_query = $this->SelectClause;
return $q = str_replace( Array('%1$s','%s'), $this->TableName, $base_query);
}
-
+
/**
* Insert calculated fields sql into query in place of %2$s,
* return processed query.
@@ -275,7 +275,7 @@
return str_replace('%2$s', '', $query);
}
}
-
+
/**
* Adds calculated field declaration to object.
*
@@ -286,7 +286,7 @@
{
$this->CalculatedFields[$name] = $sql_clause;
}
-
+
/**
* Sets ID Field name used as primary key for loading items
*
@@ -299,19 +299,19 @@
{
$this->IDField = $field_name;
}
-
+
function Configure()
{
$this->setTableName( $this->Application->getUnitOption($this->Prefix, 'TableName') );
$this->setIDField( $this->Application->getUnitOption($this->Prefix, 'IDField') );
-
+
$this->defineFields();
-
+
$this->ApplyFieldModifiers(); // should be called only after all fields definitions been set
$this->prepareConfigOptions(); // this should go last, but before setDefaultValues, order is significant!
$this->SetDefaultValues();
}
-
+
/**
* Add field definitions from all possible sources (DB Fields, Virtual Fields, Calcualted Fields, e.t.c.)
*
@@ -322,12 +322,12 @@
$this->setVirtualFields( $this->Application->getUnitOption($this->Prefix, 'VirtualFields') );
$this->setCalculatedFields( $this->Application->getUnitOption($this->Prefix, 'CalculatedFields') );
}
-
+
function setCalculatedFields($fields)
{
$this->CalculatedFields = isset($fields[$this->Special]) ? $fields[$this->Special] : (isset($fields['']) ? $fields[''] : false);
}
-
+
/**
* Set's field names from table
* from config
@@ -339,7 +339,7 @@
{
$this->Fields = $fields;
}
-
+
/**
* Override field options with ones defined in submit via "field_modfiers" array (common for all prefixes)
*
@@ -351,13 +351,13 @@
// $this->Application->APCalled[] = $this->getPrefixSpecial();
$allowed_modifiers = Array('required');
-
+
$field_modifiers = $this->Application->GetVar('field_modifiers');
if(!$field_modifiers) return false;
-
+
$field_modifiers = getArrayValue($field_modifiers, $this->getPrefixSpecial());
if(!$field_modifiers) return false;
-
+
foreach ($field_modifiers as $field => $field_options)
{
foreach ($field_options as $option_name => $option_value)
@@ -367,9 +367,9 @@
}
}
}
-
+
/**
- * Set fields (+options) for fields that physically doesn't exist in database
+ * Set fields (+options) for fields that physically doesn't exist in database
*
* @param Array $fields
* @access public
@@ -382,7 +382,7 @@
$this->Fields = array_merge_recursive2($this->VirtualFields, $this->Fields);
}
}
-
+
function SetDefaultValues()
{
foreach($this->Fields as $field => $options)
@@ -393,17 +393,17 @@
}
}
}
-
+
function SetFieldOptions($field, $options)
{
$this->Fields[$field] = $options;
}
-
+
function GetFieldOptions($field)
{
return isset($this->Fields[$field]) ? $this->Fields[$field] : Array();
}
-
+
/**
* Returns formatted field value
*
@@ -422,17 +422,17 @@
}
return $res;
}
-
+
function HasField($name)
{
-
+
}
-
+
function GetFieldValues()
{
-
+
}
-
+
function UpdateFormattersSubFields()
{
foreach ($this->Fields as $field => $options) {
@@ -442,7 +442,7 @@
}
}
}
-
+
function prepareConfigOptions()
{
foreach (array_keys($this->Fields) as $field_name)
@@ -453,19 +453,19 @@
// replace with query result
$select_clause = $field_options['option_title_field'].','.$field_options['option_key_field'];
$sql = sprintf($field_options['options_sql'], $select_clause);
-
+
$options_hash = getArrayValue($field_options,'options');
if($options_hash === false) $options_hash = Array();
-
+
$dynamic_options = $this->Conn->GetCol($sql, $field_options['option_key_field']);
$field_options['options'] = array_merge_recursive2($options_hash, $dynamic_options);
-
+
unset($field_options['options_sql']);
}
$this->PrepareOptions($field_name);
}
}
-
+
function PrepareOptions($field_name)
{
if( $formatter_class = getArrayValue($this->Fields[$field_name], 'formatter') )
@@ -474,7 +474,7 @@
$formatter->PrepareOptions($field_name, $this->Fields[$field_name], $this);
}
}
-
+
/**
* Returns unformatted field value
*
@@ -484,9 +484,9 @@
*/
function GetDBField($field)
{
-
+
}
-
+
/**
* Returns ID of currently processed record
*
@@ -497,7 +497,7 @@
{
return $this->GetDBField($this->IDField);
}
-
+
/**
* Returns parent table information
*
@@ -518,13 +518,19 @@
'ParentTableKey' => $this->Application->getUnitOption($this->Prefix,'ParentTableKey'),
'ParentPrefix' => $parent_prefix
);
-
+ if (is_array($table_info['ForeignKey'])) {
+ $table_info['ForeignKey'] = getArrayValue($table_info, 'ForeignKey', $parent_prefix);
+ }
+ if (is_array($table_info['ParentTableKey'])) {
+ $table_info['ParentTableKey'] = getArrayValue($table_info, 'ParentTableKey', $parent_prefix);
+ }
+
$main_object =& $this->Application->recallObject($parent_prefix.'.'.$special);
return array_merge($table_info, Array('ParentId'=> $main_object->GetDBField( $table_info['ParentTableKey'] ) ) );
}
return false;
}
-
+
}
?>
\ No newline at end of file