egroupware/resources/inc/class.resources_export_csv.inc.php

241 lines
5.5 KiB
PHP
Raw Normal View History

2010-11-15 21:14:50 +01:00
<?php
/**
* eGroupWare
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package resources
* @subpackage importexport
* @link http://www.egroupware.org
* @author Nathan Gray
* @copyright Nathan Gray
* @version $Id$
*/
2016-05-05 22:22:04 +02:00
use EGroupware\Api;
use EGroupware\Api\Acl;
2010-11-15 21:14:50 +01:00
/**
* export resources to CSV
*/
class resources_export_csv implements importexport_iface_export_plugin
{
2010-11-15 21:14:50 +01:00
2013-01-22 23:44:15 +01:00
public function __construct()
{
$this->bo = new resources_bo();
$this->get_selects();
}
2010-11-15 21:14:50 +01:00
/**
* Exports records as defined in $_definition
*
* @param egw_record $_definition
*/
public function export( $_stream, importexport_definition $_definition)
{
2010-11-15 21:14:50 +01:00
$options = $_definition->plugin_options;
$selection = array();
if ($options['selection'] == 'search')
{
2010-11-15 21:14:50 +01:00
// ui selection with checkbox 'selected'
2016-05-05 22:22:04 +02:00
$query = Api\Cache::getSession('resources', 'get_rows');
2010-11-15 21:14:50 +01:00
$query['num_rows'] = -1; // all
unset($query['store_state']);
$query['csv_export'] = true; // so get_rows method _can_ produce different content or not store state in the session
2013-01-22 23:44:15 +01:00
$this->bo->get_rows($query,$selection,$readonlys);
2010-11-15 21:14:50 +01:00
}
elseif ( $options['selection'] == 'all' || $options['selection'] == 'filter')
{
2010-11-15 21:14:50 +01:00
$query = array(
'num_rows' => -1,
'filter2' => -3, // Accessories & resources
'csv_export' => true, // so get_rows method _can_ produce different content or not store state in the session
2010-11-15 21:14:50 +01:00
); // all
2013-01-22 23:44:15 +01:00
if($options['selection'] == 'filter')
{
$filter = $_definition->filter;
// Handle ranges
foreach($filter as $field => $value)
{
if($field == 'cat_id')
{
$query['filter'] = $value;
continue;
}
if($field == 'filter2')
{
$query['filter2'] = $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]);
}
}
2013-01-22 23:44:15 +01:00
$this->bo->get_rows($query,$selection,$readonlys);
}
else
{
2010-11-15 21:14:50 +01:00
$selection = explode(',',$options['selection']);
}
$export_object = new importexport_export_csv($_stream, (array)$options);
$export_object->set_mapping($options['mapping']);
2010-11-23 19:27:14 +01:00
// Check if we need to load the custom fields
$need_custom = false;
foreach(Api\Storage\Customfields::get('resources') as $field => $settings)
{
if($options['mapping']['#'.$field])
{
2010-11-23 19:27:14 +01:00
$need_custom = true;
break;
}
}
2010-11-15 21:14:50 +01:00
foreach ($selection as $record)
{
2010-11-15 21:14:50 +01:00
if(!is_array($record) || !$record['res_id']) continue;
if($need_custom)
{
2013-01-22 23:44:15 +01:00
$record = $this->bo->read($record['res_id']);
2010-11-23 19:27:14 +01:00
}
2010-11-15 21:14:50 +01:00
$resource = new resources_egw_record();
$resource->set_record($record);
2011-04-06 01:36:40 +02:00
$resource->long_description = strip_tags($resource->long_description);
if($options['convert'])
{
2013-01-22 23:44:15 +01:00
importexport_export_csv::convert($resource, resources_egw_record::$types, 'resources', $this->selects);
}
else
{
// Implode arrays, so they don't say 'Array'
foreach($resource->get_record_array() as $key => $value)
{
if(is_array($value)) $resource->$key = implode(',', $value);
}
}
2010-11-15 21:14:50 +01:00
$export_object->export_record($resource);
unset($resource);
}
2013-01-22 23:44:15 +01:00
return $export_object;
2010-11-15 21:14:50 +01:00
}
/**
* returns translated name of plugin
*
* @return string name
*/
public static function get_name()
{
2010-11-15 21:14:50 +01:00
return lang('Resources CSV export');
}
/**
* returns translated (user) description of plugin
*
* @return string descriprion
*/
public static function get_description()
{
2010-11-15 21:14:50 +01:00
return lang("Exports a list of resources to a CSV File.");
}
/**
* retruns file suffix for exported file
*
* @return string suffix
*/
public static function get_filesuffix()
{
2010-11-15 21:14:50 +01:00
return 'csv';
}
public static function get_mimetype()
{
2010-11-15 21:14:50 +01:00
return 'text/csv';
}
/**
* Return array of settings for export dialog
*
* @param $definition Specific definition
2010-11-15 21:14:50 +01:00
*
* @return array (
* name => string,
* content => array,
* sel_options => array,
* readonlys => array,
* preserv => array,
* )
2010-11-15 21:14:50 +01:00
*/
public function get_options_etpl(importexport_definition &$definition = NULL)
{
return false;
2010-11-15 21:14:50 +01:00
}
/**
* returns selectors information
*
*/
public function get_selectors_etpl()
{
2010-11-15 21:14:50 +01:00
return array(
2013-01-22 23:44:15 +01:00
'name' => 'importexport.export_csv_selectors',
2010-11-15 21:14:50 +01:00
);
}
2013-01-22 23:44:15 +01:00
/**
* Get selectbox values
*/
protected function get_selects()
{
2013-01-22 23:44:15 +01:00
$this->selects = array();
}
2013-01-22 23:44:15 +01:00
/**
* Customize automatically generated filter fields
*/
public function get_filter_fields(Array &$filters)
{
2013-01-22 23:44:15 +01:00
// In resources, not all categories are used
$filters['cat_id']['type'] = 'select';
$filters['cat_id']['name'] = 'filter';
2016-05-05 22:22:04 +02:00
$filters['cat_id']['values']= (array)$this->bo->acl->get_cats(Acl::READ);
2013-01-22 23:44:15 +01:00
// Add in resources / accessories
$filters['filter2'] = array(
'name' => 'filter2',
'label' => 'Filter',
'type' => 'select',
'rows' => 5,
'values' => resources_bo::$filter_options
);
foreach($filters as $field_name => &$settings)
{
if($this->selects[$field_name]) $settings['values'] = $this->selects[$field_name];
}
}
/**
* Get the class name for the egw_record to use while exporting
*
* @return string;
*/
public static function get_egw_record_class()
{
return 'resources_egw_record';
}
2010-11-15 21:14:50 +01:00
}