Index: branches/5.2.x/core/kernel/db/dblist.php =================================================================== diff -u -N -r13840 -r14092 --- branches/5.2.x/core/kernel/db/dblist.php (.../dblist.php) (revision 13840) +++ branches/5.2.x/core/kernel/db/dblist.php (.../dblist.php) (revision 14092) @@ -1,6 +1,6 @@ PerPage = -1; } + function setGridName($grid_name) + { + $this->gridName = $grid_name; + } + /** * Returns information about all possible filter types * @@ -410,19 +422,32 @@ function CalculateTotals() { + $fields = Array(); $this->Totals = Array(); - $fields = Array(); - foreach($this->Fields as $field_name => $field_options) - { - $totals = getArrayValue($field_options, 'totals'); - if(!$totals) continue; + if ($this->gridName) { + $grids = $this->Application->getUnitOption($this->Prefix, 'Grids'); + $grid_fields = $grids[$this->gridName]['Fields']; + } + else { + $grid_fields = $this->Fields; + } - $calculated_field = isset($this->CalculatedFields[$field_name]) && isset($this->VirtualFields[$field_name]); - $db_field = !isset($this->VirtualFields[$field_name]); + foreach ($grid_fields as $field_name => $field_options) { + if ($this->gridName && array_key_exists('totals', $field_options) && $field_options['totals']) { + $totals = $field_options['totals']; + } + elseif (array_key_exists('totals', $this->Fields[$field_name]) && $this->Fields[$field_name]['totals']) { + $totals = $this->Fields[$field_name]['totals']; + } + else { + continue; + } - if($calculated_field || $db_field) - { + $calculated_field = array_key_exists($field_name, $this->CalculatedFields) && array_key_exists($field_name, $this->VirtualFields); + $db_field = !array_key_exists($field_name, $this->VirtualFields); + + 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; } @@ -457,10 +482,38 @@ function getTotal($field, $total_function) { - if (!$this->TotalsCalculated) $this->CalculateTotals(); - return $this->Totals[$field.'_'.$total_function]; + if (!$this->TotalsCalculated) { + $this->CalculateTotals(); + } + + return $this->Totals[$field . '_' . $total_function]; } + function setTotal($field, $total_function, $value) + { + $this->Totals[$field . '_' . $total_function] = $value; + } + + function getTotalFunction($field) + { + if ($this->gridName) { + $grids = $this->Application->getUnitOption($this->Prefix, 'Grids'); + $field_options = $grids[$this->gridName]['Fields'][$field]; + } + else { + $field_options = $this->Fields[$field]; + } + + if ($this->gridName && array_key_exists('totals', $field_options) && $field_options['totals']) { + return $field_options['totals']; + } + elseif (array_key_exists('totals', $this->Fields[$field]) && $this->Fields[$field]['totals']) { + return $this->Fields[$field]['totals']; + } + + return false; + } + function GetFormattedTotal($field, $total_function) { $val = $this->getTotal($field, $total_function); @@ -649,9 +702,11 @@ { // original multilanguage field - convert to current lang field $formatter = isset($this->Fields[$field]['formatter']) ? $this->Fields[$field]['formatter'] : false; + if ($formatter == 'kMultiLanguage' && !isset($this->Fields[$field]['master_field'])) { - $lang = $this->Application->GetVar('m_lang'); - $field = 'l'.$lang.'_'.$field; + // for now kMultiLanguage formatter is only supported for real (non-virtual) fields + $is_expression = true; + $field = $this->getMLSortField($field); } if (!isset($this->Fields[$field]) && $field != 'RAND()' && !$is_expression) { @@ -662,6 +717,23 @@ } /** + * Returns expression, used to sort given multilingual field + * + * @param string $field + * @return string + */ + function getMLSortField($field) + { + $table_name = '`' . $this->TableName . '`'; + $lang = $this->Application->GetVar('m_lang'); + $primary_lang = $this->Application->GetDefaultLanguageId(); + + $ret = 'IF(COALESCE(%1$s.l' . $lang . '_' . $field . ', ""), %1$s.l' . $lang . '_' . $field . ', %1$s.l' . $primary_lang . '_' . $field . ')'; + + return sprintf($ret, $table_name); + } + + /** * Removes all order fields * * @access public @@ -705,7 +777,19 @@ $pos = 0; } - return isset($this->OrderFields[$pos][0]) ? $this->OrderFields[$pos][0] : ''; + if ( isset($this->OrderFields[$pos][0]) ) { + $field = $this->OrderFields[$pos][0]; + $lang = $this->Application->GetVar('m_lang'); + + if ( preg_match('/^IF\(COALESCE\(.*?\.(l' . $lang . '_.*?), ""\),/', $field, $regs) ) { + // undo result of kDBList::getMLSortField method + return $regs[1]; + } + + return $field; + } + + return ''; } function GetOrderDirection($pos = NULL, $no_default = false) @@ -933,14 +1017,22 @@ $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 ; + if (!$parent_table_key || !$foreign_key_field) { + return ; + } $parent_object =& $this->Application->recallObject($parent_prefix.'.'.$special); - $parent_id = $parent_object->GetDBField($parent_table_key); + /* @var $parent_object kDBItem */ - if (!$parent_id) return ; + if (!$parent_object->isLoaded()) { + $this->addFilter('parent_filter', 'FALSE'); + trigger_error('Parent ID not found (prefix: "' . rtrim($parent_prefix.'.'.$special, '.') . '"; sub-prefix: "' . $this->getPrefixSpecial() . '")', E_USER_NOTICE); + return ; + } - $this->addFilter('parent_filter', '`'.$this->TableName.'`.`'.$foreign_key_field.'` = '.$parent_id); // only for list in this case + // only for list in this case + $parent_id = $parent_object->GetDBField($parent_table_key); + $this->addFilter('parent_filter', '`' . $this->TableName . '`.`' . $foreign_key_field . '` = ' . $this->Conn->qstr($parent_id)); } } @@ -951,7 +1043,7 @@ */ function isLoaded() { - return $this->Queried; + return $this->Queried && !$this->EOL(); } /**