Use a client-side setDetachedAttributes() to do needed data changes for dates.

r48920: do not convert empty values, otherwise we will get current date or 1970-01-01 instead
r48938: Fix bad logic breaking historylog
r48960: refactored code to always run Nathans fix (before it was only run for ajax requests, not for initial request)
This commit is contained in:
Ralf Becker 2014-10-09 08:39:24 +00:00
parent 7f619fe32d
commit 28afa0994b
2 changed files with 93 additions and 30 deletions

View File

@ -68,7 +68,41 @@ class etemplate_widget_date extends etemplate_widget_transformer
if($this->type != 'date-duration' && $value) if($this->type != 'date-duration' && $value)
{ {
// string with formatting letters like for php's date() method $value = $this->format_date($value);
}
}
/**
* Perform any needed data manipulation on each row
* before sending it to client.
*
* This is used by etemplate_widget_nextmatch on each row to do any needed
* adjustments. If not needed, don't implement it.
*
* @param type $cname
* @param array $expand
* @param array $data Row data
* @return type
*/
public function set_row_value($cname, Array $expand, Array &$data)
{
if($this->type == 'date-duration') return;
$form_name = self::form_name($cname, $this->id, $expand);
$value =& $this->get_array($data, $form_name, true);
$value = $this->format_date($value);
}
/**
* Put date in the proper format for sending to client
* @param string|int $value
* @param string $format
*/
public function format_date($value)
{
if (!$value) return $value; // otherwise we will get current date or 1970-01-01 instead of an empty value
if ($this->attrs['dataformat'] && !is_numeric($value)) if ($this->attrs['dataformat'] && !is_numeric($value))
{ {
$date = date_create_from_format($this->attrs['dataformat'], $value, egw_time::$user_timezone); $date = date_create_from_format($this->attrs['dataformat'], $value, egw_time::$user_timezone);
@ -86,7 +120,7 @@ class etemplate_widget_date extends etemplate_widget_transformer
// postfix date-string with "Z" so javascript doesn't add/subtract anything // postfix date-string with "Z" so javascript doesn't add/subtract anything
$value = $date->format('Y-m-d\TH:i:s\Z'); $value = $date->format('Y-m-d\TH:i:s\Z');
} }
} return $value;
} }
/** /**

View File

@ -164,7 +164,7 @@ class etemplate_widget_nextmatch extends etemplate_widget
} }
if($value['num_rows'] != 0) if($value['num_rows'] != 0)
{ {
$total = self::call_get_rows($send_value, $send_value['rows'], self::$request->readonlys); $total = self::call_get_rows($send_value, $send_value['rows'], self::$request->readonlys, null, null, $this);
} }
if (true) $value =& self::get_array(self::$request->content, $form_name, true); if (true) $value =& self::get_array(self::$request->content, $form_name, true);
@ -344,8 +344,7 @@ class etemplate_widget_nextmatch extends etemplate_widget
$value['csv_export'] = 'refresh'; $value['csv_export'] = 'refresh';
} }
$rows = $result['data'] = $result['order'] = array(); $rows = $result['data'] = $result['order'] = array();
// we can NOT run run_beforeSendToClient in call_get_rows, as it would modify row_modified timestamp, which is used below $result['total'] = self::call_get_rows($value, $rows, $result['readonlys'], null, null, $template);
$result['total'] = self::call_get_rows($value, $rows, $result['readonlys'], null, null, false);
$result['lastModification'] = egw_time::to('now', 'ts')-1; $result['lastModification'] = egw_time::to('now', 'ts')-1;
if (isset($GLOBALS['egw_info']['flags']['app_header']) && self::$request->app_header != $GLOBALS['egw_info']['flags']['app_header']) if (isset($GLOBALS['egw_info']['flags']['app_header']) && self::$request->app_header != $GLOBALS['egw_info']['flags']['app_header'])
@ -368,13 +367,20 @@ class etemplate_widget_nextmatch extends etemplate_widget
$id = $row_id ? $row[$row_id] : $n; $id = $row_id ? $row[$row_id] : $n;
$result['order'][] = $id; $result['order'][] = $id;
$modified = $row[$row_modified];
if (isset($modified) && !(is_int($modified) || is_string($modified) && is_numeric($modified)))
{
$modified = egw_time::to(str_replace('Z', '', $modified), 'ts');
}
// check if we need to send the data // check if we need to send the data
//error_log("$id Known: " . (array_search($id, $knownUids) !== false ? 'Yes' : 'No') . ' Modified: ' . egw_time::to($row[$row_modified]) . ' > ' . egw_time::to($lastModified).'? ' . ($row[$row_modified] > $lastModified ? 'Yes' : 'No')); //error_log("$id Known: " . (array_search($id, $knownUids) !== false ? 'Yes' : 'No') . ' Modified: ' . egw_time::to($row[$row_modified]) . ' > ' . egw_time::to($lastModified).'? ' . ($row[$row_modified] > $lastModified ? 'Yes' : 'No'));
if (!$row_id || !$knownUids || ($kUkey = array_search($id, $knownUids)) === false || if (!$row_id || !$knownUids || ($kUkey = array_search($id, $knownUids)) === false ||
!$lastModified || !isset($row[$row_modified]) || $row[$row_modified] > $lastModified) !$lastModified || !isset($modified) || $modified > $lastModified)
{ {
$result['data'][$id] = self::run_beforeSendToClient($row); $result['data'][$id] = $row;
} }
if ($kUkey !== false) unset($knownUids[$kUkey]); if ($kUkey !== false) unset($knownUids[$kUkey]);
} }
else // non-row data set by get_rows method else // non-row data set by get_rows method
@ -529,9 +535,10 @@ class etemplate_widget_nextmatch extends etemplate_widget
* @param array &$readonlys =null * @param array &$readonlys =null
* @param object $obj =null (internal) * @param object $obj =null (internal)
* @param string|array $method =null (internal) * @param string|array $method =null (internal)
* @param etemplate_widget $widget =null instanciated nextmatch widget to let it's widgets transform each row
* @return int|boolean total items found of false on error ($value['get_rows'] not callable) * @return int|boolean total items found of false on error ($value['get_rows'] not callable)
*/ */
private static function call_get_rows(array &$value,array &$rows,array &$readonlys=null,$obj=null,$method=null, $run_beforeSendToClient=true) private static function call_get_rows(array &$value,array &$rows,array &$readonlys=null,$obj=null,$method=null, etemplate_widget $widget=null)
{ {
if (is_null($method)) $method = $value['get_rows']; if (is_null($method)) $method = $value['get_rows'];
@ -583,13 +590,23 @@ class etemplate_widget_nextmatch extends etemplate_widget
{ {
$total = false; // method not callable $total = false; // method not callable
} }
/* no automatic fallback to start=0 // if we have a nextmatch widget, find the repeating row
if ($method && $total && $value['start'] >= $total) if ($widget && $widget->attrs['template'])
{ {
$value['start'] = 0; $row_template = $widget->getElementById($widget->attrs['template']);
$total = self::call_get_rows($value,$rows,$readonlys,$obj,$method); if(!$row_template)
{
$row_template = etemplate_widget_template::instance($widget->attrs['template']);
}
// Try to find just the repeating part
$repeating_row = null;
// First child should be a grid, we want last row
foreach($row_template->children[0]->children[1]->children as $child)
{
if($child->type == 'row') $repeating_row = $child;
}
} }
*/
// otherwise we might get stoped by max_excutiontime // otherwise we might get stoped by max_excutiontime
if ($total > 200) @set_time_limit(0); if ($total > 200) @set_time_limit(0);
@ -613,7 +630,19 @@ class etemplate_widget_nextmatch extends etemplate_widget
$row['parent_id'] = $row[$parent_id]; // seems NOT used on client! $row['parent_id'] = $row[$parent_id]; // seems NOT used on client!
} }
// run beforeSendToClient methods of widgets in row on row-data // run beforeSendToClient methods of widgets in row on row-data
$rows[$n-$first+$value['start']] = $run_beforeSendToClient ? self::run_beforeSendToClient($row) : $row; if($repeating_row)
{
// Change anything by widget for each row ($row set to 1)
$_row = array(1 => &$row);
$repeating_row->run('set_row_value', array('',array('row' => 1), &$_row), true);
}
else if (!$widget || get_class($widget) != 'etemplate_widget_historylog')
{
// Fallback based on widget names
error_log(self::$request->template['name'] . ' had to fallback to run_beforeSendToClient() because it could not find the row');
$row = self::run_beforeSendToClient($row);
}
$rows[$n-$first+$value['start']] = $row;
} }
elseif(!is_numeric($n)) // rows with string-keys, after numeric rows elseif(!is_numeric($n)) // rows with string-keys, after numeric rows
{ {