2010-10-13 00:45:02 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* eGroupWare
|
|
|
|
*
|
|
|
|
* @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$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* export CSV plugin of calendar
|
|
|
|
*/
|
|
|
|
class calendar_export_csv implements importexport_iface_export_plugin {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exports records as defined in $_definition
|
|
|
|
*
|
|
|
|
* @param egw_record $_definition
|
|
|
|
*/
|
|
|
|
public function export( $_stream, importexport_definition $_definition) {
|
|
|
|
$options = $_definition->plugin_options;
|
|
|
|
$this->bo = new calendar_bo();
|
2011-05-24 21:56:32 +02:00
|
|
|
$config = config::read('phpgwapi');
|
2011-02-23 21:48:57 +01:00
|
|
|
|
2011-08-30 16:49:49 +02:00
|
|
|
$limit_exception = bo_merge::is_export_limit_excepted();
|
2011-08-17 17:38:56 +02:00
|
|
|
|
2011-02-23 21:48:57 +01:00
|
|
|
// Custom fields need to be specifically requested
|
|
|
|
$cfs = array();
|
|
|
|
foreach($options['mapping'] as $key => $label) {
|
|
|
|
if($key[0] == '#') $cfs[] = substr($key,1);
|
|
|
|
}
|
2011-05-24 21:56:32 +02:00
|
|
|
|
2011-05-30 19:23:46 +02:00
|
|
|
if($options['selection']['select'] == 'criteria') {
|
2011-05-24 21:56:32 +02:00
|
|
|
$query = array(
|
|
|
|
'start' => $options['selection']['start'],
|
|
|
|
'end' => $options['selection']['end'],
|
|
|
|
'categories' => $options['categories'] ? $options['categories'] : $options['selection']['categories'],
|
|
|
|
'enum_recuring' => false,
|
|
|
|
'daywise' => false,
|
|
|
|
'users' => $options['selection']['owner'],
|
|
|
|
'cfs' => $cfs // Otherwise we shouldn't get any custom fields
|
|
|
|
);
|
2011-08-30 17:13:58 +02:00
|
|
|
if($config['export_limit'] && !$limit_exception) {
|
2011-05-24 21:56:32 +02:00
|
|
|
$query['offset'] = 0;
|
|
|
|
$query['num_rows'] = (int)$config['export_limit'];
|
|
|
|
}
|
|
|
|
$events =& $this->bo->search($query);
|
2011-05-30 19:23:46 +02:00
|
|
|
} elseif ($options['selection']['select'] = 'search_results') {
|
2011-05-26 18:35:25 +02:00
|
|
|
$states = $GLOBALS['egw']->session->appsession('session_data','calendar');
|
|
|
|
if($states['view'] == 'listview') {
|
|
|
|
$query = $GLOBALS['egw']->session->appsession('calendar_list','calendar');
|
|
|
|
$query['num_rows'] = -1; // all
|
|
|
|
$query['start'] = 0;
|
|
|
|
$query['cfs'] = $cfs;
|
2011-05-24 21:56:32 +02:00
|
|
|
|
2011-08-30 17:13:58 +02:00
|
|
|
if($config['export_limit'] && !$limit_exception) {
|
2011-05-26 18:35:25 +02:00
|
|
|
$query['num_rows'] = (int)$config['export_limit'];
|
|
|
|
}
|
|
|
|
$ui = new calendar_uilist();
|
|
|
|
$ui->get_rows($query, $events);
|
|
|
|
} else {
|
|
|
|
$query = $GLOBALS['egw']->session->appsession('session_data','calendar');
|
|
|
|
$query['users'] = explode(',', $query['owner']);
|
|
|
|
$query['num_rows'] = -1;
|
2011-08-30 17:13:58 +02:00
|
|
|
if($config['export_limit'] && !$limit_exception) {
|
2011-05-26 18:35:25 +02:00
|
|
|
$query['num_rows'] = (int)$config['export_limit'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$events = array();
|
|
|
|
switch($states['view']) {
|
|
|
|
case 'month':
|
|
|
|
$query += $this->get_query_month($states);
|
|
|
|
break;
|
|
|
|
case 'week':
|
|
|
|
$query += $this->get_query_week($states);
|
|
|
|
break;
|
|
|
|
case 'day':
|
|
|
|
$query += $this->get_query_day($states);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$ui = new calendar_uiviews($query);
|
|
|
|
$query += array(
|
|
|
|
'start' => is_array($ui->first) ? $this->bo->date2ts($ui->first) : $ui->first,
|
|
|
|
'end' => is_array($ui->last) ? $this->bo->date2ts($ui->last) : $ui->last
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
$bo = new calendar_boupdate();
|
|
|
|
$events = $bo->search($query + array(
|
|
|
|
'offset' => 0,
|
|
|
|
'order' => 'cal_start',
|
|
|
|
));
|
2011-05-24 21:56:32 +02:00
|
|
|
}
|
2011-02-23 21:48:57 +01:00
|
|
|
}
|
2010-10-13 00:45:02 +02:00
|
|
|
|
|
|
|
$export_object = new importexport_export_csv($_stream, (array)$options);
|
|
|
|
$export_object->set_mapping($options['mapping']);
|
2011-04-07 01:12:50 +02:00
|
|
|
$convert_fields = calendar_egw_record::$types;
|
2010-11-04 18:27:08 +01:00
|
|
|
|
2010-11-09 20:26:25 +01:00
|
|
|
$recurrence = $this->bo->recur_types;
|
2010-10-13 00:45:02 +02:00
|
|
|
|
2011-04-07 01:12:50 +02:00
|
|
|
$lookups = array(
|
|
|
|
'priority' => Array(
|
|
|
|
0 => '',
|
|
|
|
1 => lang('Low'),
|
|
|
|
2 => lang('Normal'),
|
|
|
|
3 => lang('High')
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2010-10-13 00:45:02 +02:00
|
|
|
$record = new calendar_egw_record();
|
|
|
|
foreach ($events as $event) {
|
2011-04-18 21:25:23 +02:00
|
|
|
// Get rid of yearly recurring events that don't belong
|
2011-05-24 21:56:32 +02:00
|
|
|
if($options['selection']['select'] == 'criteria' && ($event['start'] > $query['end'] || $event['end'] < $query['start'])) continue;
|
2011-04-18 21:25:23 +02:00
|
|
|
|
2010-11-04 18:27:08 +01:00
|
|
|
// Add in participants
|
|
|
|
if($options['mapping']['participants']) {
|
|
|
|
$event['participants'] = implode(", ",$this->bo->participants($event,true));
|
|
|
|
}
|
|
|
|
|
2010-10-13 00:45:02 +02:00
|
|
|
$record->set_record($event);
|
2010-11-09 20:26:25 +01:00
|
|
|
if($options['mapping']['recurrence']) {
|
|
|
|
$record->recurrence = $recurrence[$record->recur_type];
|
|
|
|
if($record->recur_type != MCAL_RECUR_NONE) $record->recurrence .= ' / '. $record->recur_interval;
|
|
|
|
}
|
|
|
|
|
2010-11-04 18:27:08 +01:00
|
|
|
// Standard stuff
|
2010-11-23 00:44:31 +01:00
|
|
|
if($options['convert']) {
|
2011-04-07 01:12:50 +02:00
|
|
|
importexport_export_csv::convert($record, $convert_fields, 'calendar', $lookups);
|
2010-12-10 23:54:27 +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-10-13 00:45:02 +02:00
|
|
|
$export_object->export_record($record);
|
|
|
|
}
|
|
|
|
unset($record);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns translated name of plugin
|
|
|
|
*
|
|
|
|
* @return string name
|
|
|
|
*/
|
|
|
|
public static function get_name() {
|
|
|
|
return lang('Calendar CSV export');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns translated (user) description of plugin
|
|
|
|
*
|
|
|
|
* @return string descriprion
|
|
|
|
*/
|
|
|
|
public static function get_description() {
|
|
|
|
return lang("Exports events from your Calendar 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';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return html for options.
|
|
|
|
*
|
|
|
|
*/
|
2011-05-30 19:23:46 +02:00
|
|
|
public function get_options_etpl($definition = null) {
|
2010-10-13 00:45:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns selectors of this plugin
|
|
|
|
*
|
|
|
|
*/
|
2011-05-30 19:23:46 +02:00
|
|
|
public function get_selectors_etpl($definition = null) {
|
2011-03-14 21:39:59 +01:00
|
|
|
$states = $GLOBALS['egw']->session->appsession('session_data','calendar');
|
2011-04-07 00:25:21 +02:00
|
|
|
|
|
|
|
$start= new egw_time($states['date']);
|
|
|
|
if($states['view'] == 'week') {
|
|
|
|
$days = isset($_GET['days']) ? $_GET['days'] : $GLOBALS['egw_info']['user']['preferences']['calendar']['days_in_weekview'];
|
|
|
|
if ($days != 5) $days = 7;
|
|
|
|
$end = "+$days days";
|
|
|
|
} else {
|
|
|
|
$end = '+1 ' . $states['view'];
|
|
|
|
}
|
|
|
|
|
2011-05-30 19:23:46 +02:00
|
|
|
$prefs = unserialize($GLOBALS['egw_info']['user']['preferences']['importexport'][$definition->definition_id]);
|
2010-10-13 00:45:02 +02:00
|
|
|
return array(
|
|
|
|
'name' => 'calendar.export_csv_select',
|
|
|
|
'content' => array(
|
2011-04-07 00:25:21 +02:00
|
|
|
'plugin_override' => true, // Plugin overrides preferences
|
2011-05-30 19:23:46 +02:00
|
|
|
'select' => $prefs['selection']['select'] ? $prefs['selection']['select'] : 'criteria',
|
2011-04-07 00:25:21 +02:00
|
|
|
'start' => $start->format('ts'),
|
|
|
|
'end' => strtotime($end, $start->format('ts'))-1,
|
2011-03-14 21:39:59 +01:00
|
|
|
'owner' => $states['owner']
|
2010-10-13 00:45:02 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2011-05-26 18:35:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get additional query parameters used when in various views
|
|
|
|
* This stuff copied out of calendar_uiviews
|
|
|
|
*/
|
2011-05-30 19:23:46 +02:00
|
|
|
public static function get_query_month($states)
|
2011-05-26 18:35:25 +02:00
|
|
|
{
|
|
|
|
$timespan = array(
|
|
|
|
'start' => mktime(0,0,0,$states['month'],1,$states['year']),
|
|
|
|
'end' => mktime(0,0,0,$states['month']+1,1,$states['year'])-1
|
|
|
|
);
|
|
|
|
return $timespan;
|
|
|
|
}
|
|
|
|
|
2011-05-30 19:23:46 +02:00
|
|
|
public static function get_query_week($states)
|
2011-05-26 18:35:25 +02:00
|
|
|
{
|
|
|
|
$query = array();
|
|
|
|
$days = $states['days'];
|
|
|
|
$ui = new calendar_uiviews($states);
|
|
|
|
if (!$days)
|
|
|
|
{
|
2011-05-30 19:23:46 +02:00
|
|
|
$days = isset($_GET['days']) ? $_GET['days'] : $ui->cal_prefs['days_in_weekview'];
|
2011-05-26 18:35:25 +02:00
|
|
|
if ($days != 5) $days = 7;
|
|
|
|
}
|
|
|
|
if ($days == 4) // next 4 days view
|
|
|
|
{
|
|
|
|
$query['start'] = $this->bo->date2ts($states['date']);
|
|
|
|
$query['end'] = strtotime("+$days days",$query['start']) - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$query['start'] = $ui->datetime->get_weekday_start($states['year'],$states['month'],$states['day']);
|
|
|
|
if ($days == 5) // no weekend-days
|
|
|
|
{
|
|
|
|
switch($ui->cal_prefs['weekdaystarts'])
|
|
|
|
{
|
|
|
|
case 'Saturday':
|
|
|
|
$query['start'] = strtotime("+2 days",$query['start']);
|
|
|
|
break;
|
|
|
|
case 'Sunday':
|
|
|
|
$query['start'] = strtotime("+1 day",$query['start']);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$query['end'] = strtotime("+$days days",$query['start']) - 1;
|
|
|
|
}
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function get_query_day($states)
|
|
|
|
{
|
|
|
|
$query = array();
|
|
|
|
$bo = new calendar_bo();
|
|
|
|
$query['start'] = $bo->date2ts((string)$states['date']);
|
|
|
|
$query['end'] = $query['start']+DAY_s-1;
|
|
|
|
return $query;
|
|
|
|
}
|
2010-10-13 00:45:02 +02:00
|
|
|
}
|