2010-10-13 00:45:02 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2016-05-01 19:47:59 +02:00
|
|
|
* EGroupware calendar export
|
2010-10-13 00:45:02 +02:00
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package calendar
|
|
|
|
* @subpackage importexport
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
* @copyright Nathan Gray
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
2016-05-01 19:47:59 +02:00
|
|
|
use EGroupware\Api;
|
|
|
|
|
2010-10-13 00:45:02 +02:00
|
|
|
/**
|
|
|
|
* export CSV plugin of calendar
|
|
|
|
*/
|
|
|
|
class calendar_export_csv implements importexport_iface_export_plugin {
|
|
|
|
|
2013-01-21 19:36:48 +01:00
|
|
|
public function __construct() {
|
2016-05-01 19:47:59 +02:00
|
|
|
Api\Translation::add_app('calendar');
|
2013-01-21 19:36:48 +01:00
|
|
|
$this->bo = new calendar_bo();
|
|
|
|
$this->get_selects();
|
|
|
|
}
|
|
|
|
|
2010-10-13 00:45:02 +02:00
|
|
|
/**
|
|
|
|
* Exports records as defined in $_definition
|
|
|
|
*
|
|
|
|
* @param egw_record $_definition
|
|
|
|
*/
|
|
|
|
public function export( $_stream, importexport_definition $_definition) {
|
|
|
|
$options = $_definition->plugin_options;
|
2011-02-23 21:48:57 +01:00
|
|
|
|
2018-12-06 19:37:51 +01:00
|
|
|
$events = $this->get_events($_definition, $options);
|
|
|
|
|
|
|
|
$export_object = new importexport_export_csv($_stream, (array)$options);
|
|
|
|
if (!$limit_exception)
|
|
|
|
{
|
|
|
|
$export_object->export_limit = $export_limit;
|
|
|
|
}
|
|
|
|
$export_object->set_mapping($options['mapping']);
|
|
|
|
$convert_fields = calendar_egw_record::$types;
|
|
|
|
|
|
|
|
$record = new calendar_egw_record();
|
|
|
|
foreach ($events as $event)
|
|
|
|
{
|
|
|
|
// the condition below (2 lines) may only work on enum_recuring=false and using the iterator to test an recurring event on the given timerange
|
|
|
|
// Get rid of yearly recurring events that don't belong
|
|
|
|
//if($options['selection']['select'] == 'criteria' && ($event['start'] > $query['end'] || $event['end'] < $query['start'])) continue;
|
|
|
|
// Add in participants
|
|
|
|
if($options['mapping']['participants'])
|
|
|
|
{
|
|
|
|
if(is_array($event['participants']))
|
|
|
|
{
|
|
|
|
$event['participants'] = implode(", ",$this->bo->participants($event,true));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Getting results from list already has participants formatted
|
|
|
|
$event['participants'] = str_replace("\n", ' ', $event['participants']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_array($event))
|
|
|
|
{
|
|
|
|
$record->set_record($event);
|
|
|
|
if($options['mapping']['recurrence'])
|
|
|
|
{
|
|
|
|
$rrule = calendar_rrule::event2rrule($event);
|
|
|
|
$record->recurrence = $rrule->__toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Standard stuff
|
|
|
|
if($options['convert'])
|
|
|
|
{
|
|
|
|
importexport_export_csv::convert($record, $convert_fields, 'calendar', $this->selects);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$export_object->export_record($record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($record);
|
|
|
|
return $export_object;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function get_events(importexport_definition $_definition, $options = array())
|
|
|
|
{
|
2016-05-01 19:47:59 +02:00
|
|
|
$limit_exception = Api\Storage\Merge::is_export_limit_excepted();
|
|
|
|
if (!$limit_exception) $export_limit = Api\Storage\Merge::getExportLimit('calendar');
|
2018-12-06 19:37:51 +01:00
|
|
|
|
2011-02-23 21:48:57 +01:00
|
|
|
// Custom fields need to be specifically requested
|
|
|
|
$cfs = array();
|
2018-12-06 19:37:51 +01:00
|
|
|
foreach((array)$options['mapping'] + (array)$_definition->filter as $key => $label) {
|
2011-02-23 21:48:57 +01:00
|
|
|
if($key[0] == '#') $cfs[] = substr($key,1);
|
|
|
|
}
|
2011-05-24 21:56:32 +02:00
|
|
|
|
2013-01-24 00:22:37 +01:00
|
|
|
$query = array(
|
|
|
|
'cfs' => $cfs, // Otherwise we shouldn't get any custom fields
|
|
|
|
'num_rows' => -1,
|
|
|
|
'csv_export' => true
|
|
|
|
);
|
2013-01-21 19:36:48 +01:00
|
|
|
switch($options['selection'])
|
|
|
|
{
|
|
|
|
case 'criteria':
|
|
|
|
$query = array(
|
|
|
|
'categories' => $options['categories'] ? $options['categories'] : $options['criteria']['categories'],
|
|
|
|
//'enum_recuring' => false, // we want the recurring events enumerated for csv export
|
|
|
|
'daywise' => false,
|
|
|
|
'users' => $options['criteria']['owner'],
|
|
|
|
'cfs' => $cfs // Otherwise we shouldn't get any custom fields
|
|
|
|
);
|
2016-09-14 17:44:48 +02:00
|
|
|
if($options['criteria']['start'])
|
|
|
|
{
|
|
|
|
$query['start'] = $options['criteria']['start'];
|
|
|
|
}
|
|
|
|
if($options['criteria']['end'])
|
|
|
|
{
|
|
|
|
$query['end'] = strtotime('+1 day',$options['criteria']['end'])-1;
|
|
|
|
}
|
2016-05-01 19:47:59 +02:00
|
|
|
if(Api\Storage\Merge::hasExportLimit($export_limit) && !$limit_exception) {
|
2013-01-21 19:36:48 +01:00
|
|
|
$query['offset'] = 0;
|
2011-09-13 11:05:33 +02:00
|
|
|
$query['num_rows'] = (int)$export_limit; // ! int of 'no' is 0
|
2011-05-26 18:35:25 +02:00
|
|
|
}
|
2013-01-21 19:36:48 +01:00
|
|
|
$events =& $this->bo->search($query);
|
|
|
|
break;
|
|
|
|
case 'search_results':
|
2016-09-21 16:45:08 +02:00
|
|
|
$states = $this->bo->cal_prefs['saved_states'];
|
|
|
|
$query = Api\Cache::getSession('calendar', 'calendar_list');
|
|
|
|
$query['num_rows'] = -1; // all
|
|
|
|
$query['csv_export'] = true; // so get_rows method _can_ produce different content or not store state in the session
|
|
|
|
$query['start'] = 0;
|
|
|
|
$query['cfs'] = $cfs;
|
|
|
|
if(Api\Storage\Merge::hasExportLimit($export_limit) && !$limit_exception)
|
|
|
|
{
|
|
|
|
$query['num_rows'] = (int)$export_limit; // ! int of 'no' is 0
|
|
|
|
}
|
|
|
|
$ui = new calendar_uilist();
|
|
|
|
if($states['view'] == 'listview')
|
|
|
|
{
|
2013-01-21 19:36:48 +01:00
|
|
|
$ui->get_rows($query, $events, $unused);
|
2018-03-02 19:13:18 +01:00
|
|
|
}
|
2016-09-21 16:45:08 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$query['filter'] = 'custom';
|
2013-01-21 19:36:48 +01:00
|
|
|
$events = array();
|
|
|
|
|
2016-09-21 16:45:08 +02:00
|
|
|
$ui->get_rows($query, $events, $unused);
|
2011-05-26 18:35:25 +02:00
|
|
|
}
|
2018-08-14 18:04:16 +02:00
|
|
|
// Filter out extra things like sel_options
|
|
|
|
unset($events['sel_options']);
|
2013-01-21 19:36:48 +01:00
|
|
|
break;
|
|
|
|
case 'filter':
|
|
|
|
$fields = importexport_helper_functions::get_filter_fields($_definition->application, $this);
|
|
|
|
$filter = $_definition->filter;
|
2011-05-26 18:35:25 +02:00
|
|
|
|
2013-01-21 19:36:48 +01:00
|
|
|
// Handle ranges
|
|
|
|
foreach($filter as $field => $value)
|
|
|
|
{
|
2018-12-06 19:37:51 +01:00
|
|
|
if($field == 'status_filter' && $value)
|
2013-01-21 19:36:48 +01:00
|
|
|
{
|
|
|
|
$query['filter'] = $value;
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-06 19:37:51 +01:00
|
|
|
else if($field == 'users' && $value)
|
|
|
|
{
|
|
|
|
// No cal_ prefix here
|
|
|
|
$query['users'] = $value;
|
|
|
|
}
|
|
|
|
else if(!is_array($value) || (!$value['from'] && !$value['to']))
|
2013-01-21 19:36:48 +01:00
|
|
|
{
|
|
|
|
$query['query']["cal_$field"] = $value;
|
|
|
|
continue;
|
|
|
|
}
|
2011-05-26 18:35:25 +02:00
|
|
|
|
2013-01-21 19:36:48 +01:00
|
|
|
// Ranges are inclusive, so should be provided that way (from 2 to 10 includes 2 and 10)
|
|
|
|
if($value['from']) $query['sql_filter'][] = "cal_$field >= " . (int)$value['from'];
|
|
|
|
if($value['to']) $query['sql_filter'][] = "cal_$field <= " . (int)$value['to'];
|
|
|
|
|
|
|
|
}
|
|
|
|
if($query['sql_filter'] && is_array($query['sql_filter']))
|
|
|
|
{
|
|
|
|
// Set as an extra parameter
|
|
|
|
$sql_filter = implode(' AND ',$query['sql_filter']);
|
2011-05-26 18:35:25 +02:00
|
|
|
}
|
2013-01-21 19:36:48 +01:00
|
|
|
|
|
|
|
case 'all':
|
|
|
|
$events = $this->bo->search($query + array(
|
2011-05-26 18:35:25 +02:00
|
|
|
'offset' => 0,
|
|
|
|
'order' => 'cal_start',
|
2013-01-21 19:36:48 +01:00
|
|
|
),$sql_filter);
|
|
|
|
break;
|
2011-02-23 21:48:57 +01:00
|
|
|
}
|
2018-12-06 19:37:51 +01:00
|
|
|
return $events;
|
2010-10-13 00:45:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns translated name of plugin
|
|
|
|
*
|
|
|
|
* @return string name
|
|
|
|
*/
|
2016-09-21 16:45:08 +02:00
|
|
|
public static function get_name()
|
|
|
|
{
|
2010-10-13 00:45:02 +02:00
|
|
|
return lang('Calendar CSV export');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns translated (user) description of plugin
|
|
|
|
*
|
|
|
|
* @return string descriprion
|
|
|
|
*/
|
2016-09-21 16:45:08 +02:00
|
|
|
public static function get_description()
|
|
|
|
{
|
2010-10-13 00:45:02 +02:00
|
|
|
return lang("Exports events from your Calendar into a CSV File.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* retruns file suffix for exported file
|
|
|
|
*
|
|
|
|
* @return string suffix
|
|
|
|
*/
|
2016-09-21 16:45:08 +02:00
|
|
|
public static function get_filesuffix()
|
|
|
|
{
|
2010-10-13 00:45:02 +02:00
|
|
|
return 'csv';
|
|
|
|
}
|
|
|
|
|
2016-09-21 16:45:08 +02:00
|
|
|
public static function get_mimetype()
|
|
|
|
{
|
2010-10-13 00:45:02 +02:00
|
|
|
return 'text/csv';
|
|
|
|
}
|
2018-12-06 19:37:51 +01:00
|
|
|
|
2010-10-13 00:45:02 +02:00
|
|
|
/**
|
2018-11-29 18:32:42 +01:00
|
|
|
* Return array of settings for export dialog
|
|
|
|
*
|
|
|
|
* @param $definition Specific definition
|
2010-10-13 00:45:02 +02:00
|
|
|
*
|
2018-11-29 18:32:42 +01:00
|
|
|
* @return array (
|
|
|
|
* name => string,
|
|
|
|
* content => array,
|
|
|
|
* sel_options => array,
|
|
|
|
* readonlys => array,
|
|
|
|
* preserv => array,
|
|
|
|
* )
|
2010-10-13 00:45:02 +02:00
|
|
|
*/
|
2018-11-29 18:32:42 +01:00
|
|
|
public function get_options_etpl(importexport_definition &$definition = NULL)
|
2016-09-21 16:45:08 +02:00
|
|
|
{
|
2018-11-29 18:32:42 +01:00
|
|
|
return false;
|
2010-10-13 00:45:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns selectors of this plugin
|
|
|
|
*
|
|
|
|
*/
|
2016-09-21 16:45:08 +02:00
|
|
|
public function get_selectors_etpl($definition = null)
|
|
|
|
{
|
2016-02-18 20:45:37 +01:00
|
|
|
$states = $this->bo->cal_prefs['saved_states'];
|
2016-05-01 19:47:59 +02:00
|
|
|
$list = Api\Cache::getSession('calendar', 'calendar_list');
|
2018-03-02 19:13:18 +01:00
|
|
|
|
2016-09-21 16:45:08 +02:00
|
|
|
$start= new Api\DateTime($list['startdate']);
|
|
|
|
$end = new Api\DateTime($list['enddate']);
|
2018-03-02 19:13:18 +01:00
|
|
|
|
2013-01-22 18:27:07 +01:00
|
|
|
if ($states['view'] == 'listview')
|
2011-11-08 23:01:41 +01:00
|
|
|
{
|
2016-05-01 19:47:59 +02:00
|
|
|
$list = Api\Cache::getSession('calendar', 'calendar_list');
|
2011-11-08 23:01:41 +01:00
|
|
|
|
|
|
|
// Use UI to get dates
|
|
|
|
$ui = new calendar_uilist();
|
2012-03-29 14:06:10 +02:00
|
|
|
$list['csv_export'] = true; // so get_rows method _can_ produce different content or not store state in the session
|
2013-01-21 19:36:48 +01:00
|
|
|
$ui->get_rows($list,$rows,$readonlys);
|
2016-05-01 19:47:59 +02:00
|
|
|
$start = $ui->first ? $ui->first : new Api\DateTime($ui->date);
|
2013-01-22 18:27:07 +01:00
|
|
|
$end = $ui->last;
|
2011-11-08 23:01:41 +01:00
|
|
|
|
|
|
|
// Special handling
|
|
|
|
if($list['filter'] == 'all') $start = $end = null;
|
|
|
|
if($list['filter'] == 'before')
|
|
|
|
{
|
|
|
|
$end = $start;
|
|
|
|
$start = null;
|
|
|
|
}
|
|
|
|
$ui = null;
|
|
|
|
}
|
2016-09-21 16:45:08 +02:00
|
|
|
else if(!$end)
|
2011-11-08 23:01:41 +01:00
|
|
|
{
|
2011-04-07 00:25:21 +02:00
|
|
|
$end = '+1 ' . $states['view'];
|
2011-11-08 23:01:41 +01:00
|
|
|
$end = strtotime($end, $start->format('ts'))-1;
|
2011-04-07 00:25:21 +02:00
|
|
|
}
|
2011-05-30 19:23:46 +02:00
|
|
|
$prefs = unserialize($GLOBALS['egw_info']['user']['preferences']['importexport'][$definition->definition_id]);
|
2011-11-08 23:01:41 +01:00
|
|
|
$data = array(
|
2010-10-13 00:45:02 +02:00
|
|
|
'name' => 'calendar.export_csv_select',
|
|
|
|
'content' => array(
|
2011-04-07 00:25:21 +02:00
|
|
|
'plugin_override' => true, // Plugin overrides preferences
|
2013-01-21 19:36:48 +01:00
|
|
|
'selection' => $prefs['selection'] ? $prefs['selection'] : 'criteria',
|
2013-01-21 19:59:33 +01:00
|
|
|
'criteria' => array(
|
|
|
|
'start' => is_object($start) ? $start->format('ts') : $start,
|
2014-04-30 18:54:47 +02:00
|
|
|
'end' => is_object($end) ? $end->format('ts') : $end,
|
2013-01-21 19:59:33 +01:00
|
|
|
'owner' => $states['owner']
|
|
|
|
)
|
2010-10-13 00:45:02 +02:00
|
|
|
)
|
|
|
|
);
|
2011-11-08 23:01:41 +01:00
|
|
|
return $data;
|
2010-10-13 00:45:02 +02:00
|
|
|
}
|
2011-05-26 18:35:25 +02:00
|
|
|
|
2013-01-21 19:36:48 +01:00
|
|
|
/**
|
|
|
|
* Get select options for use in filter
|
|
|
|
*/
|
|
|
|
protected function get_selects()
|
|
|
|
{
|
|
|
|
$this->selects['priority'] = Array(
|
2013-01-31 15:13:45 +01:00
|
|
|
0 => lang('None'),
|
2013-01-21 19:36:48 +01:00
|
|
|
1 => lang('Low'),
|
|
|
|
2 => lang('Normal'),
|
|
|
|
3 => lang('High')
|
|
|
|
);
|
2018-12-06 19:37:51 +01:00
|
|
|
$this->selects['status_filter'] = array(
|
2013-01-21 19:36:48 +01:00
|
|
|
'default' => lang('Not rejected'),
|
|
|
|
'accepted' => lang('Accepted'),
|
|
|
|
'unknown' => lang('Invitations'),
|
|
|
|
'tentative' => lang('Tentative'),
|
|
|
|
'delegated' => lang('Delegated'),
|
|
|
|
'rejected' => lang('Rejected'),
|
|
|
|
'owner' => lang('Owner too'),
|
|
|
|
'all' => lang('All incl. rejected'),
|
|
|
|
'hideprivate' => lang('Hide private infos'),
|
|
|
|
'showonlypublic' => lang('Hide private events'),
|
|
|
|
'no-enum-groups' => lang('only group-events'),
|
|
|
|
'not-unknown' => lang('No meeting requests'),
|
|
|
|
);
|
2016-02-18 20:45:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
$states = $this->bo->cal_prefs['saved_states'];
|
2013-01-21 19:36:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adjust automatically generated field filters
|
|
|
|
*/
|
|
|
|
public function get_filter_fields(Array &$filters)
|
|
|
|
{
|
2013-01-31 15:13:45 +01:00
|
|
|
|
2013-01-21 19:36:48 +01:00
|
|
|
// Calendar SO doesn't support filtering by column, so we have to remove pretty much everything
|
|
|
|
unset($filters['recur_date']);
|
|
|
|
|
2018-12-06 19:37:51 +01:00
|
|
|
// Add in the participant & status filters at the beginning
|
2013-01-21 19:36:48 +01:00
|
|
|
$filters = array_reverse($filters, true);
|
2018-12-06 19:37:51 +01:00
|
|
|
$filters['status_filter'] = array(
|
2013-01-21 19:36:48 +01:00
|
|
|
'type' => 'select',
|
2018-12-06 19:37:51 +01:00
|
|
|
'name' => 'status_filter',
|
2013-01-21 19:36:48 +01:00
|
|
|
'label' => lang('Filter'),
|
2018-12-06 19:37:51 +01:00
|
|
|
'multiple' => false
|
2013-01-21 19:36:48 +01:00
|
|
|
);
|
2018-12-06 19:37:51 +01:00
|
|
|
$filters['users'] = array(
|
|
|
|
'name' => 'users',
|
|
|
|
'label' => lang('Participant'),
|
|
|
|
'type' => 'calendar-owner',
|
|
|
|
) + $filters['owner'];
|
2013-01-21 19:36:48 +01:00
|
|
|
$filters = array_reverse($filters, true);
|
|
|
|
|
|
|
|
foreach($filters as $field_name => &$settings)
|
|
|
|
{
|
|
|
|
// Can't filter on a custom field
|
|
|
|
if(strpos($field_name, '#') === 0)
|
|
|
|
{
|
|
|
|
unset($filters[$field_name]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass on select options
|
|
|
|
if($this->selects[$field_name]) $settings['values'] = $this->selects[$field_name];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-05-04 21:34:30 +02:00
|
|
|
/**
|
|
|
|
* Get the class name for the egw_record to use while exporting
|
|
|
|
*
|
|
|
|
* @return string;
|
|
|
|
*/
|
|
|
|
public static function get_egw_record_class()
|
|
|
|
{
|
|
|
|
return 'calendar_egw_record';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|