Implement export filters for timesheet

This commit is contained in:
Nathan Gray 2013-01-22 22:19:46 +00:00
parent 66ef9a3273
commit a5ec4e5b36
2 changed files with 62 additions and 13 deletions

View File

@ -16,6 +16,12 @@
*/ */
class timesheet_export_csv implements importexport_iface_export_plugin { class timesheet_export_csv implements importexport_iface_export_plugin {
public function __construct()
{
$this->ui = new timesheet_ui();
$this->get_selects();
}
/** /**
* Exports records as defined in $_definition * Exports records as defined in $_definition
* *
@ -24,20 +30,48 @@ class timesheet_export_csv implements importexport_iface_export_plugin {
public function export( $_stream, importexport_definition $_definition) { public function export( $_stream, importexport_definition $_definition) {
$options = $_definition->plugin_options; $options = $_definition->plugin_options;
$uitimesheet = new timesheet_ui(); $this->ui = new timesheet_ui();
$selection = array(); $selection = array();
if($options['selection'] == 'selected') { if($options['selection'] == 'selected') {
$query = $GLOBALS['egw']->session->appsession('index',TIMESHEET_APP); $query = $GLOBALS['egw']->session->appsession('index',TIMESHEET_APP);
$query['num_rows'] = -1; // all records $query['num_rows'] = -1; // all records
$query['csv_export'] = true; // so get_rows method _can_ produce different content or not store state in the session $query['csv_export'] = true; // so get_rows method _can_ produce different content or not store state in the session
$uitimesheet->get_rows($query,$selection,$readonlys,true); // true = only return the id's $this->ui->get_rows($query,$selection,$readonlys,true); // true = only return the id's
} elseif($options['selection'] == 'all') { } elseif($options['selection'] == 'all') {
$query = array( $query = array(
'num_rows' => -1, 'num_rows' => -1,
'csv_export' => true, // so get_rows method _can_ produce different content or not store state in the session 'csv_export' => true, // so get_rows method _can_ produce different content or not store state in the session
); );
$uitimesheet->get_rows($query,$selection,$readonlys,true); // true = only return the id's $this->ui->get_rows($query,$selection,$readonlys,true); // true = only return the id's
}
else if($options['selection'] == 'filter')
{
$fields = importexport_helper_functions::get_filter_fields($_definition->application, $this);
$filter = $_definition->filter;
$query = array(
'num_rows' => -1,
'csv_export' => true, // so get_rows method _can_ produce different content or not store state in the session
'col_filter' => array()
);
// Handle ranges
foreach($filter as $field => $value)
{
if($field == 'cat_id')
{
$query['cat_id'] = $value;
continue;
}
$query['col_filter'][$field] = $value;
if(!is_array($value) || (!$value['from'] && !$value['to'])) continue;
// Ranges are inclusive, so should be provided that way (from 2 to 10 includes 2 and 10)
if($value['from']) $query['col_filter'][] = "$field >= " . (int)$value['from'];
if($value['to']) $query['col_filter'][] = "$field <= " . (int)$value['to'];
unset($query['col_filter'][$field]);
}
$this->ui->get_rows($query,$selection,$readonlys,true); // true = only return the id's
} }
@ -45,19 +79,12 @@ class timesheet_export_csv implements importexport_iface_export_plugin {
$export_object = new importexport_export_csv($_stream, (array)$options); $export_object = new importexport_export_csv($_stream, (array)$options);
$export_object->set_mapping($options['mapping']); $export_object->set_mapping($options['mapping']);
$lookups = array(
'ts_status' => $uitimesheet->status_labels+array(lang('No status'))
);
foreach($lookups['ts_status'] as &$status) {
$status = str_replace('&nbsp;','',$status); // Remove &nbsp;
}
// $options['selection'] is array of identifiers as this plugin doesn't // $options['selection'] is array of identifiers as this plugin doesn't
// support other selectors atm. // support other selectors atm.
foreach ($selection as $identifier) { foreach ($selection as $identifier) {
$record = new timesheet_egw_record($identifier); $record = new timesheet_egw_record($identifier);
if($options['convert']) { if($options['convert']) {
importexport_export_csv::convert($record, timesheet_egw_record::$types, 'timesheet', $lookups); importexport_export_csv::convert($record, timesheet_egw_record::$types, 'timesheet', $this->selects);
} else { } else {
// Implode arrays, so they don't say 'Array' // Implode arrays, so they don't say 'Array'
foreach($record->get_record_array() as $key => $value) { foreach($record->get_record_array() as $key => $value) {
@ -117,8 +144,30 @@ class timesheet_export_csv implements importexport_iface_export_plugin {
*/ */
public function get_selectors_etpl() { public function get_selectors_etpl() {
return array( return array(
'name' => 'timesheet.export_csv_selectors', 'name' => 'importexport.export_csv_selectors',
'content' => 'selected' 'content' => 'selected'
); );
} }
public function get_selects()
{
$this->selects = array(
'ts_status' => $this->ui->status_labels+array(lang('No status'))
);
foreach($this->selects['ts_status'] as &$status) {
$status = str_replace('&nbsp;','',$status); // Remove &nbsp;
}
}
/**
* Adjust the automatically generated filter fields
*/
public function get_filter_fields(Array &$filters)
{
foreach($filters as $field_name => &$settings)
{
if($this->selects[$field_name]) $settings['values'] = $this->selects[$field_name];
}
}
} }

View File

@ -17,7 +17,7 @@ class timesheet_wizard_export_csv extends importexport_wizard_basic_export_csv
// Field mapping // Field mapping
$bo = new timesheet_bo(); $bo = new timesheet_bo();
$this->export_fields = array('ts_id' => 'Timesheet ID') + $bo->field2label; $this->export_fields = array('ts_id' => 'Timesheet ID') + $bo->field2label + array('ts_modified'=> 'Modified');
// Custom fields // Custom fields
unset($this->export_fields['customfields']); unset($this->export_fields['customfields']);