helper function to construct union-query-columns for calendar_integration

This commit is contained in:
Ralf Becker 2010-04-23 06:52:48 +00:00
parent ee18b0acac
commit 74ccf7501d

View File

@ -701,6 +701,81 @@ class calendar_so
return self::$integration_data;
}
/**
* Return union cols constructed from application cols and required cols
*
* Every col not supplied in $app_cols get returned as NULL.
*
* @param array $app_cols required name => own name pairs
* @param string|array $required array or comma separated column names or table.*
* @param string $required_app='calendar'
* @return string cols for union query to match ones supplied in $required
*/
public static function union_cols(array $app_cols,$required,$required_app='calendar')
{
// remove evtl. used DISTINCT, we currently dont need it
if (($distinct = substr($required,0,9) == 'DISTINCT '))
{
$required = substr($required,9);
}
$return_cols = array();
foreach(is_array($required) ? $required : explode(',',$required) as $cols)
{
if (substr($cols,-2) == '.*')
{
$cols = self::get_columns($required_app,substr($cols,0,-2));
}
elseif (strpos($cols,' AS ') !== false)
{
list(,$cols) = explode(' AS ',$cols);
}
foreach((array)$cols as $col)
{
if (substr($col,0,7) == 'egw_cal') // remove table name
{
$col = preg_replace('/^egw_cal[a-z_]*\.','',$col);
}
if (isset($app_cols[$col]))
{
$return_cols[] = $app_cols[$col];
}
else
{
$return_cols[] = 'NULL';
}
}
}
return implode(',',$return_cols);
}
/**
* Get columns of given table, taking into account historically different column order of egw_cal table
*
* @param string $app
* @param string $table
* @return array of column names
*/
static private function get_columns($app,$table)
{
if ($table != 'egw_cal')
{
$table_def = $GLOBALS['egw']->db->get_table_definitions($app,$table);
$cols = array_keys($table_def['fd']);
}
else
{
// special handling for egw_cal, as old databases have a different column order!!!
$cols =& egw_cache::getSession(__CLASS__,$table);
if (is_null($cols))
{
$meta = $GLOBALS['egw']->db->metadata($table,true);
$cols = array_keys($meta['meta']);
}
}
return $cols;
}
/**
* Checks for conflicts
*/