2010-11-08 18:19:22 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* eGroupWare
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package infolog
|
|
|
|
* @subpackage importexport
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
* @copyright Nathan Gray
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* export plugin of infolog
|
|
|
|
*/
|
|
|
|
class infolog_export_csv implements importexport_iface_export_plugin {
|
|
|
|
|
|
|
|
|
2012-12-31 21:38:00 +01:00
|
|
|
public function __construct() {
|
|
|
|
translation::add_app('infolog');
|
|
|
|
$this->bo = new infolog_bo();
|
|
|
|
$this->get_selects();
|
|
|
|
}
|
|
|
|
|
2010-11-08 18:19:22 +01:00
|
|
|
/**
|
|
|
|
* Exports records as defined in $_definition
|
|
|
|
*
|
|
|
|
* @param egw_record $_definition
|
|
|
|
*/
|
|
|
|
public function export( $_stream, importexport_definition $_definition) {
|
|
|
|
$options = $_definition->plugin_options;
|
|
|
|
|
|
|
|
$selection = array();
|
|
|
|
$query = array();
|
2011-11-29 01:21:15 +01:00
|
|
|
$cf_links = array();
|
2010-11-08 18:19:22 +01:00
|
|
|
|
2012-10-16 20:45:13 +02:00
|
|
|
$this->export_object = new importexport_export_csv($_stream, (array)$options);
|
|
|
|
$this->export_object->set_mapping($options['mapping']);
|
2011-03-02 18:14:49 +01:00
|
|
|
|
2010-11-08 18:19:22 +01:00
|
|
|
// do we need to query the cf's
|
2012-12-31 21:38:00 +01:00
|
|
|
foreach($options['mapping'] + (array)$_definition->filter as $field => $map) {
|
2011-11-29 01:21:15 +01:00
|
|
|
if($field[0] == '#') {
|
|
|
|
$query['custom_fields'][] = $field;
|
|
|
|
|
2012-10-10 17:18:43 +02:00
|
|
|
if($GLOBALS['egw_info']['user']['apps'][$this->bo->customfields[substr($field,1)]['type']])
|
2011-11-29 01:21:15 +01:00
|
|
|
{
|
2012-10-10 17:18:43 +02:00
|
|
|
$cf_links[$field] = $this->bo->customfields[substr($field,1)]['type'];
|
2011-11-29 01:21:15 +01:00
|
|
|
}
|
|
|
|
}
|
2010-11-08 18:19:22 +01:00
|
|
|
}
|
|
|
|
|
2011-11-26 00:10:39 +01:00
|
|
|
$ids = array();
|
2011-11-29 01:21:15 +01:00
|
|
|
switch($options['selection'])
|
|
|
|
{
|
|
|
|
case 'search':
|
2012-12-31 21:38:00 +01:00
|
|
|
$query = array_merge((array)$GLOBALS['egw']->session->appsession('session_data','infolog'), $query);
|
2011-11-29 01:21:15 +01:00
|
|
|
// Fall through
|
2012-12-31 21:38:00 +01:00
|
|
|
case 'filter':
|
2011-11-29 01:21:15 +01:00
|
|
|
case 'all':
|
2012-12-31 21:38:00 +01:00
|
|
|
if($options['selection'] == 'filter')
|
|
|
|
{
|
|
|
|
$fields = importexport_helper_functions::get_filter_fields($_definition->application, $this);
|
|
|
|
$query['col_filter'] = $_definition->filter;
|
|
|
|
|
2013-01-02 19:14:05 +01:00
|
|
|
// Backend expects a string
|
|
|
|
if($query['col_filter']['info_responsible'])
|
|
|
|
{
|
|
|
|
$query['col_filter']['info_responsible'] = implode(',',$query['col_filter']['info_responsible']);
|
|
|
|
}
|
|
|
|
|
2012-12-31 21:38:00 +01:00
|
|
|
// Handle ranges
|
|
|
|
foreach($query['col_filter'] as $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]);
|
|
|
|
}
|
|
|
|
}
|
2011-11-29 01:21:15 +01:00
|
|
|
$query['num_rows'] = 500;
|
|
|
|
$query['start'] = 0;
|
|
|
|
do {
|
2012-10-10 17:18:43 +02:00
|
|
|
$selection = $this->bo->search($query);
|
2011-11-29 01:21:15 +01:00
|
|
|
$ids = array_keys($selection);
|
|
|
|
|
|
|
|
// Pre-load any cfs that are links
|
|
|
|
$cf_preload = array();
|
|
|
|
foreach($cf_links as $field => $app) {
|
|
|
|
foreach($selection as &$row) {
|
|
|
|
if($row[$field]) $cf_preload[$app][] = $row[$field];
|
|
|
|
}
|
|
|
|
if($cf_preload[$app]){
|
|
|
|
$selects[$field] = egw_link::titles($app, $cf_preload[$app]);
|
|
|
|
error_log('Preload ' . $field . '['.$app . ']: ' . implode(',',$cf_preload[$app]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-16 20:45:13 +02:00
|
|
|
$this->export_records($this->export_object, $options, $selection, $ids);
|
2011-11-29 01:21:15 +01:00
|
|
|
$query['start'] += $query['num_rows'];
|
|
|
|
} while($query['start'] < $query['total']);
|
|
|
|
|
2012-10-16 20:45:13 +02:00
|
|
|
return $this->export_object;
|
2011-11-29 01:21:15 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$ids = $selection = explode(',',$options['selection']);
|
2012-10-16 20:45:13 +02:00
|
|
|
$this->export_records($this->export_object, $options, $selection, $ids);
|
2011-11-29 01:21:15 +01:00
|
|
|
break;
|
2011-11-26 00:10:39 +01:00
|
|
|
}
|
2012-10-16 20:45:13 +02:00
|
|
|
return $this->export_object;
|
2011-11-29 01:21:15 +01:00
|
|
|
}
|
2011-11-26 00:10:39 +01:00
|
|
|
|
2011-11-29 01:21:15 +01:00
|
|
|
protected function export_records(&$export_object, $options, &$selection, $ids = array())
|
|
|
|
{
|
|
|
|
// Pre-load links all at once
|
|
|
|
if($ids && $options['mapping']['info_link_id'])
|
|
|
|
{
|
2011-11-29 01:42:00 +01:00
|
|
|
$links = egw_link::get_links_multiple('infolog', $ids, true, '!'.egw_link::VFS_APPNAME);
|
2011-11-29 01:21:15 +01:00
|
|
|
foreach($links as $id => $link) {
|
|
|
|
if(!is_array($selection[$id])) break;
|
|
|
|
$selection[$id]['info_link_id'] = $link;
|
|
|
|
if($options['convert']) $selection[$id]['info_link_id'] = egw_link::title($link['app'], $link['id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If exporting PM fields, pre-load them all at once
|
2011-11-26 00:10:39 +01:00
|
|
|
if($ids && ($options['mapping']['pm_id'] || $options['mapping']['project']))
|
|
|
|
{
|
|
|
|
$projects = egw_link::get_links_multiple('infolog', $ids, true, 'projectmanager');
|
|
|
|
foreach($projects as $id => $links)
|
|
|
|
{
|
|
|
|
if(!is_array($selection[$id])) break;
|
|
|
|
$selection[$id]['pm_id'] = current($links);
|
|
|
|
$selection[$id]['project'] = egw_link::title('projectmanager', $selection[$id]['pm_id']);
|
2013-01-02 19:14:05 +01:00
|
|
|
$this->selects['pl_id'] = ExecMethod('projectmanager.projectmanager_pricelist_bo.pricelist',$selection[$id]['pm_id']);
|
2011-11-26 00:10:39 +01:00
|
|
|
}
|
2010-11-08 18:19:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($selection as $_identifier) {
|
|
|
|
if(!is_array($_identifier)) {
|
|
|
|
$record = new infolog_egw_record($_identifier);
|
2011-11-29 01:21:15 +01:00
|
|
|
if($link = $links[$record->info_id]) $record->info_link_id = $options['convert'] ? egw_link::title($link['app'], $link['id']) : $link;
|
2011-11-26 00:10:39 +01:00
|
|
|
if($project = $projects[$record->info_id])
|
|
|
|
{
|
|
|
|
$record->pm_id = current($project);
|
|
|
|
$record->project = egw_link::title('projectmanager', $record->pm_id);
|
|
|
|
}
|
2010-11-08 18:19:22 +01:00
|
|
|
} else {
|
|
|
|
$record = new infolog_egw_record();
|
|
|
|
$record->set_record($_identifier);
|
|
|
|
}
|
|
|
|
// Some conversion
|
2010-11-23 00:45:11 +01:00
|
|
|
if($options['convert']) {
|
2012-10-10 17:18:43 +02:00
|
|
|
$this->selects['info_status'] = $this->bo->get_status($record->info_type);
|
2012-04-17 18:37:38 +02:00
|
|
|
importexport_export_csv::convert($record, infolog_egw_record::$types, 'infolog', $this->selects);
|
2010-11-23 00:45:11 +01:00
|
|
|
$this->convert($record);
|
2012-04-17 18:37:38 +02:00
|
|
|
|
|
|
|
// Force 0 times to ''
|
|
|
|
foreach(array('info_planned_time', 'info_used_time', 'info_replanned_time') as $field)
|
|
|
|
{
|
|
|
|
if($record->$field == 0) $record->$field = '';
|
|
|
|
}
|
2010-12-10 23:48:59 +01:00
|
|
|
} else {
|
|
|
|
// Implode arrays, so they don't say 'Array'
|
|
|
|
foreach($record->get_record_array() as $key => $value) {
|
|
|
|
if(is_array($value)) $record->$key = implode(',', $value);
|
|
|
|
}
|
2010-11-23 00:45:11 +01:00
|
|
|
}
|
2010-11-08 18:19:22 +01:00
|
|
|
$export_object->export_record($record);
|
|
|
|
unset($record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns translated name of plugin
|
|
|
|
*
|
|
|
|
* @return string name
|
|
|
|
*/
|
|
|
|
public static function get_name() {
|
|
|
|
return lang('Infolog CSV export');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns translated (user) description of plugin
|
|
|
|
*
|
|
|
|
* @return string descriprion
|
|
|
|
*/
|
|
|
|
public static function get_description() {
|
|
|
|
return lang("Exports Infolog entries into a CSV File.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* retruns file suffix for exported file
|
|
|
|
*
|
|
|
|
* @return string suffix
|
|
|
|
*/
|
|
|
|
public static function get_filesuffix() {
|
|
|
|
return 'csv';
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function get_mimetype() {
|
|
|
|
return 'text/csv';
|
|
|
|
}
|
|
|
|
|
2012-10-16 20:45:13 +02:00
|
|
|
/**
|
|
|
|
* Suggest a file name for the downloaded file
|
|
|
|
* No suffix
|
|
|
|
*/
|
|
|
|
public function get_filename()
|
|
|
|
{
|
|
|
|
if(is_object($this->export_object) && $this->export_object->get_num_of_records() == 1)
|
|
|
|
{
|
|
|
|
return $this->export_object->record->get_title();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-08 18:19:22 +01:00
|
|
|
/**
|
|
|
|
* return html for options.
|
|
|
|
* this way the plugin has all opertunities for options tab
|
|
|
|
*
|
|
|
|
* @return string html
|
|
|
|
*/
|
|
|
|
public function get_options_etpl() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns slectors of this plugin via xajax
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function get_selectors_etpl() {
|
2011-03-28 23:51:45 +02:00
|
|
|
return array(
|
|
|
|
'name' => 'infolog.export_csv_selectors',
|
|
|
|
'content' => 'search'
|
|
|
|
);
|
2010-11-08 18:19:22 +01:00
|
|
|
}
|
|
|
|
|
2012-12-31 21:38:00 +01:00
|
|
|
protected function get_selects()
|
|
|
|
{
|
|
|
|
$this->selects['info_type'] = $this->bo->enums['type'];
|
|
|
|
$this->selects['info_priority'] = $this->bo->enums['priority'];
|
2013-01-02 19:14:05 +01:00
|
|
|
$this->selects['pl_id'] = ExecMethod('projectmanager.projectmanager_pricelist_bo.pricelist',false);
|
2012-12-31 21:38:00 +01:00
|
|
|
$this->selects['info_status'] = $this->bo->get_status();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_filter_fields(Array &$filters)
|
|
|
|
{
|
|
|
|
foreach($filters as $field_name => &$settings)
|
|
|
|
{
|
|
|
|
if($this->selects[$field_name]) $settings['values'] = $this->selects[$field_name];
|
|
|
|
|
|
|
|
// Infolog can't handle ranges in custom fields due to the way searching is done.
|
|
|
|
if(strpos($field_name, '#') === 0 && strpos($settings['type'],'date') === 0) unset($filters[$field_name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-08 18:19:22 +01:00
|
|
|
/**
|
|
|
|
* Convert some internal data to something with more meaning
|
|
|
|
*
|
|
|
|
* This is for something specific to Infolog, in addition to the normal conversions.
|
|
|
|
*/
|
|
|
|
public static function convert(infolog_egw_record &$record) {
|
|
|
|
// Stub, for now
|
|
|
|
}
|
|
|
|
}
|