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
|
|
|
|
*/
|
2018-11-29 18:32:42 +01:00
|
|
|
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
|
|
|
|
*/
|
2018-11-29 18:32:42 +01:00
|
|
|
public function export( $_stream, importexport_definition $_definition)
|
|
|
|
{
|
2010-11-15 21:14:50 +01:00
|
|
|
$options = $_definition->plugin_options;
|
|
|
|
|
|
|
|
$selection = array();
|
2018-11-29 18:32:42 +01:00
|
|
|
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']);
|
2012-03-29 14:06:10 +02:00
|
|
|
$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
|
|
|
}
|
2018-11-29 18:32:42 +01:00
|
|
|
elseif ( $options['selection'] == 'all' || $options['selection'] == 'filter')
|
|
|
|
{
|
2010-11-15 21:14:50 +01:00
|
|
|
$query = array(
|
|
|
|
'num_rows' => -1,
|
2012-12-18 17:35:24 +01:00
|
|
|
'filter2' => -3, // Accessories & resources
|
2012-03-29 14:06:10 +02:00
|
|
|
'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
|
2018-11-29 18:32:42 +01:00
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
2018-11-29 18:32:42 +01:00
|
|
|
|
2013-01-22 23:44:15 +01:00
|
|
|
$this->bo->get_rows($query,$selection,$readonlys);
|
2018-11-29 18:32:42 +01:00
|
|
|
}
|
|
|
|
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;
|
2018-11-29 18:32:42 +01:00
|
|
|
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
|
|
|
|
2018-11-29 18:32:42 +01:00
|
|
|
foreach ($selection as $record)
|
|
|
|
{
|
2010-11-15 21:14:50 +01:00
|
|
|
if(!is_array($record) || !$record['res_id']) continue;
|
|
|
|
|
2018-11-29 18:32:42 +01:00
|
|
|
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);
|
2018-11-29 18:32:42 +01:00
|
|
|
if($options['convert'])
|
|
|
|
{
|
2013-01-22 23:44:15 +01:00
|
|
|
importexport_export_csv::convert($resource, resources_egw_record::$types, 'resources', $this->selects);
|
2018-11-29 18:32:42 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-10 23:56:02 +01:00
|
|
|
// Implode arrays, so they don't say 'Array'
|
2018-11-29 18:32:42 +01:00
|
|
|
foreach($resource->get_record_array() as $key => $value)
|
|
|
|
{
|
2010-12-10 23:56:02 +01:00
|
|
|
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
|
|
|
|
*/
|
2018-11-29 18:32:42 +01:00
|
|
|
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
|
|
|
|
*/
|
2018-11-29 18:32:42 +01:00
|
|
|
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
|
|
|
|
*/
|
2018-11-29 18:32:42 +01:00
|
|
|
public static function get_filesuffix()
|
|
|
|
{
|
2010-11-15 21:14:50 +01:00
|
|
|
return 'csv';
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:32:42 +01:00
|
|
|
public static function get_mimetype()
|
|
|
|
{
|
2010-11-15 21:14:50 +01:00
|
|
|
return 'text/csv';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-29 18:32:42 +01:00
|
|
|
* Return array of settings for export dialog
|
|
|
|
*
|
|
|
|
* @param $definition Specific definition
|
2010-11-15 21:14:50 +01:00
|
|
|
*
|
2018-11-29 18:32:42 +01:00
|
|
|
* @return array (
|
|
|
|
* name => string,
|
|
|
|
* content => array,
|
|
|
|
* sel_options => array,
|
|
|
|
* readonlys => array,
|
|
|
|
* preserv => array,
|
|
|
|
* )
|
2010-11-15 21:14:50 +01:00
|
|
|
*/
|
2018-11-29 18:32:42 +01:00
|
|
|
public function get_options_etpl(importexport_definition &$definition = NULL)
|
|
|
|
{
|
|
|
|
return false;
|
2010-11-15 21:14:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns selectors information
|
|
|
|
*
|
|
|
|
*/
|
2018-11-29 18:32:42 +01:00
|
|
|
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()
|
2018-11-29 18:32:42 +01:00
|
|
|
{
|
2013-01-22 23:44:15 +01:00
|
|
|
$this->selects = array();
|
2018-11-29 18:32:42 +01:00
|
|
|
}
|
2013-01-22 23:44:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Customize automatically generated filter fields
|
|
|
|
*/
|
2016-05-04 21:34:30 +02:00
|
|
|
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
|
|
|
|
);
|
2016-05-04 21:34:30 +02:00
|
|
|
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
|
|
|
}
|