2006-11-10 16:30:01 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* eGroupWare - importexport
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package importexport
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Cornelius Weiss <nelius@cwtech.de>
|
|
|
|
* @copyright Cornelius Weiss <nelius@cwtech.de>
|
2006-11-10 16:38:31 +01:00
|
|
|
* @version $Id$
|
2006-11-10 16:30:01 +01:00
|
|
|
*/
|
|
|
|
|
2016-05-10 00:49:20 +02:00
|
|
|
use EGroupware\Api;
|
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/** bo to define {im|ex}ports
|
|
|
|
*
|
|
|
|
* @todo make this class an egw_record_pool!
|
|
|
|
*/
|
2010-03-22 16:09:26 +01:00
|
|
|
class importexport_definitions_bo {
|
2006-11-10 16:30:01 +01:00
|
|
|
|
|
|
|
const _appname = 'importexport';
|
2010-03-22 16:09:26 +01:00
|
|
|
const _defintion_table = 'egw_importexport_definitions';
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
2016-05-10 00:49:20 +02:00
|
|
|
* @var so_sql holds Api\Storage\Base
|
2006-11-10 16:30:01 +01:00
|
|
|
*/
|
|
|
|
private $so_sql;
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2007-06-09 17:36:30 +02:00
|
|
|
/**
|
|
|
|
* @var array hold definitions
|
|
|
|
*/
|
2006-11-10 16:30:01 +01:00
|
|
|
private $definitions;
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2011-02-03 17:42:03 +01:00
|
|
|
public function __construct($_query=false, $ignore_acl = false)
|
2006-11-10 16:30:01 +01:00
|
|
|
{
|
2016-05-10 00:49:20 +02:00
|
|
|
$this->so_sql = new Api\Storage\Base(self::_appname, self::_defintion_table );
|
2006-11-10 16:30:01 +01:00
|
|
|
if ($_query) {
|
2011-02-03 17:42:03 +01:00
|
|
|
$definitions = $this->so_sql->search($_query, false);
|
2006-11-10 16:30:01 +01:00
|
|
|
foreach ((array)$definitions as $definition) {
|
2011-02-03 17:42:03 +01:00
|
|
|
if(self::is_permitted($definition) || $ignore_acl) $this->definitions[] = $definition['definition_id'];
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2011-04-19 18:00:43 +02:00
|
|
|
public function get_rows(&$query, &$rows, &$readonlys)
|
|
|
|
{
|
2011-05-18 16:46:57 +02:00
|
|
|
// Filter only definitions user is allowed to use
|
|
|
|
if(!$GLOBALS['egw_info']['user']['apps']['admin']) {
|
|
|
|
$this_membership = $GLOBALS['egw']->accounts->memberships($GLOBALS['egw_info']['user']['account_id'], true);
|
|
|
|
$this_membership[] = $GLOBALS['egw_info']['user']['account_id'];
|
|
|
|
$sql .= ' (';
|
2012-09-05 22:02:03 +02:00
|
|
|
$read = array();
|
2011-05-18 16:46:57 +02:00
|
|
|
foreach($this_membership as $id)
|
|
|
|
{
|
|
|
|
$read[] = 'allowed_users '.
|
|
|
|
$GLOBALS['egw']->db->capabilities['case_insensitive_like'].' '.
|
|
|
|
$GLOBALS['egw']->db->quote('%,'.str_replace('_','\\_',$id) .',%');
|
|
|
|
}
|
|
|
|
$sql .= implode(' OR ', $read);
|
2011-05-19 18:11:26 +02:00
|
|
|
$sql .= ') OR owner = '.$GLOBALS['egw_info']['user']['account_id'];
|
2011-05-18 16:46:57 +02:00
|
|
|
$query['col_filter'][] = $sql;
|
|
|
|
}
|
2019-07-22 23:10:43 +02:00
|
|
|
|
2014-07-15 20:09:20 +02:00
|
|
|
// Handle allowed filter
|
|
|
|
if($query['col_filter']['allowed_users'])
|
|
|
|
{
|
|
|
|
$allowed = array();
|
|
|
|
foreach((array)$query['col_filter']['allowed_users'] as $id)
|
|
|
|
{
|
|
|
|
$allowed[] = 'allowed_users '.
|
|
|
|
$GLOBALS['egw']->db->capabilities['case_insensitive_like'].' '.
|
|
|
|
$GLOBALS['egw']->db->quote('%,'.str_replace('_','\\_',$id) .',%');
|
|
|
|
}
|
|
|
|
if($allowed)
|
|
|
|
{
|
|
|
|
unset($query['col_filter']['allowed_users']);
|
|
|
|
$query['col_filter'][] = '('.implode(' OR ', $allowed) . ')';
|
|
|
|
}
|
|
|
|
}
|
2019-07-22 23:10:43 +02:00
|
|
|
|
2011-05-18 01:27:42 +02:00
|
|
|
$total = $this->so_sql->get_rows($query, $rows, $readonlys);
|
2011-05-18 16:22:31 +02:00
|
|
|
$ro_count = 0;
|
2011-05-18 16:46:57 +02:00
|
|
|
foreach($rows as &$row) {
|
|
|
|
// Strip off leading + trailing ,
|
|
|
|
$row['allowed_users'] = substr($row['allowed_users'],1,-1);
|
|
|
|
|
2012-06-05 23:50:34 +02:00
|
|
|
$readonlys["edit[{$row['definition_id']}]"] = $readonlys["delete[{$row['definition_id']}]"] =
|
2011-05-18 01:27:42 +02:00
|
|
|
($row['owner'] != $GLOBALS['egw_info']['user']['account_id']) &&
|
|
|
|
!$GLOBALS['egw_info']['user']['apps']['admin'];
|
2012-06-05 23:50:34 +02:00
|
|
|
if($readonlys["edit[{$row['definition_id']}]"])
|
2011-10-26 21:54:15 +02:00
|
|
|
{
|
2014-02-27 22:27:14 +01:00
|
|
|
$row['class'] .= 'rowNoEdit rowNoDelete';
|
2011-10-26 21:54:15 +02:00
|
|
|
$ro_count++;
|
|
|
|
}
|
2011-12-01 19:49:42 +01:00
|
|
|
$row['class'] .= ' ' . $row['type'];
|
2011-05-18 01:27:42 +02:00
|
|
|
}
|
2011-05-18 16:22:31 +02:00
|
|
|
$readonlys['delete_selected'] = $ro_count == count($rows);
|
2011-05-18 01:27:42 +02:00
|
|
|
return $total;
|
2011-04-19 18:00:43 +02:00
|
|
|
}
|
|
|
|
|
2007-06-08 00:08:38 +02:00
|
|
|
/**
|
2007-06-09 17:36:30 +02:00
|
|
|
* gets array of definition ids
|
2009-04-28 18:05:04 +02:00
|
|
|
*
|
2007-06-08 00:08:38 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2006-11-10 16:30:01 +01:00
|
|
|
public function get_definitions() {
|
|
|
|
return $this->definitions;
|
|
|
|
}
|
2009-09-09 10:07:32 +02:00
|
|
|
public function read($definition_id) {
|
2011-05-18 16:22:31 +02:00
|
|
|
if(is_numeric($definition_id)) {
|
|
|
|
$this->so_sql->read($definition_id);
|
|
|
|
$definition = new importexport_definition($this->so_sql->data['name']);
|
|
|
|
} else {
|
|
|
|
$definition = new importexport_definition( $definition_id['name'] );
|
|
|
|
}
|
|
|
|
return $definition->get_record_array();
|
2009-09-09 10:07:32 +02:00
|
|
|
}
|
2007-06-08 00:08:38 +02:00
|
|
|
/**
|
|
|
|
* deletes a defintion
|
|
|
|
*
|
|
|
|
* @param array $keys
|
|
|
|
*/
|
|
|
|
public function delete($keys) {
|
2011-05-18 16:22:31 +02:00
|
|
|
foreach ($keys as $index => $key) {
|
|
|
|
// Check for ownership
|
|
|
|
$definition = $this->read($key);
|
|
|
|
if($definition['owner'] && $definition['owner'] == $GLOBALS['egw_info']['user']['account_id'] || $GLOBALS['egw_info']['user']['apps']['admin']) {
|
|
|
|
// clear private cache
|
2021-10-14 16:55:36 +02:00
|
|
|
if(is_array($this->definitions))
|
|
|
|
{
|
|
|
|
unset($this->definitions[array_search($key, $this->definitions)]);
|
|
|
|
}
|
2011-05-18 16:22:31 +02:00
|
|
|
} else {
|
|
|
|
unset($keys[$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(count($keys) > 0) {
|
|
|
|
$this->so_sql->delete(array('definition_id' => $keys));
|
2006-11-16 12:04:09 +01:00
|
|
|
}
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2010-02-26 00:13:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a definition
|
|
|
|
*
|
|
|
|
* @param definition $definition
|
|
|
|
*/
|
|
|
|
public function save(Array $data) {
|
2010-03-22 16:09:26 +01:00
|
|
|
$definition = new importexport_definition();
|
2010-02-26 00:13:18 +01:00
|
|
|
$definition->set_record($data);
|
|
|
|
$definition->save($data['definition_id']);
|
|
|
|
}
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* checkes if user if permitted to access given definition
|
|
|
|
*
|
|
|
|
* @param array $_definition
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
static public function is_permitted($_definition) {
|
2011-01-28 20:44:24 +01:00
|
|
|
$allowed_user = is_array($_definition['allowed_users']) ? $_definition['allowed_users'] : explode(',',$_definition['allowed_users']);
|
|
|
|
$this_user_id = $GLOBALS['egw_info']['user']['account_id'];
|
|
|
|
$this_membership = $GLOBALS['egw']->accounts->memberships($this_user_id, true);
|
|
|
|
$this_membership[] = $this_user_id;
|
2012-06-05 23:50:34 +02:00
|
|
|
$this_membership[] = 'all';
|
2011-01-28 20:44:24 +01:00
|
|
|
$alluser = array_intersect($allowed_user,$this_membership);
|
2011-02-22 18:13:03 +01:00
|
|
|
return ($this_user_id == $_definition['owner'] || count($alluser) > 0);
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* exports definitions
|
|
|
|
*
|
|
|
|
* @param array $keys to export
|
|
|
|
*/
|
|
|
|
public function export($keys)
|
|
|
|
{
|
|
|
|
$export_data = array('metainfo' => array(
|
|
|
|
'type' => 'importexport definitions',
|
2016-05-10 00:49:20 +02:00
|
|
|
'charset' => Api\Translation::charset(),
|
2006-11-10 16:30:01 +01:00
|
|
|
'entries' => count($keys),
|
|
|
|
));
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2007-06-08 00:08:38 +02:00
|
|
|
$export_data['definitions'] = array();
|
|
|
|
foreach ($keys as $definition_id) {
|
2010-03-22 16:09:26 +01:00
|
|
|
$definition = new importexport_definition( $definition_id );
|
2007-06-08 00:08:38 +02:00
|
|
|
$export_data['definitions'][$definition->name] = $definition->get_record_array();
|
2009-04-28 18:05:04 +02:00
|
|
|
$export_data['definitions'][$definition->name]['allowed_users'] =
|
2010-03-29 19:38:54 +02:00
|
|
|
importexport_helper_functions::account_id2name(
|
2009-04-28 18:05:04 +02:00
|
|
|
$export_data['definitions'][$definition->name]['allowed_users']
|
2007-06-09 17:36:30 +02:00
|
|
|
);
|
2011-03-09 16:47:11 +01:00
|
|
|
if($export_date['definitions'][$definition->name]['owner']) {
|
|
|
|
$export_data['definitions'][$definition->name]['owner'] =
|
|
|
|
importexport_helper_functions::account_id2name(
|
|
|
|
$export_data['definitions'][$definition->name]['owner']
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
unset($export_data['definitions'][$definition->name]['owner']);
|
|
|
|
}
|
2007-06-08 00:08:38 +02:00
|
|
|
unset($export_data['definitions'][$definition->name]['definition_id']);
|
2011-03-09 16:47:11 +01:00
|
|
|
unset($export_data['definitions'][$definition->name]['description']);
|
|
|
|
unset($export_data['definitions'][$definition->name]['user_timezone_read']);
|
2011-03-21 18:13:25 +01:00
|
|
|
if(is_array($export_data['definitions'][$definition->name]['plugin_options'])) {
|
|
|
|
unset($export_data['definitions'][$definition->name]['plugin_options']['user_timezone_read']);
|
|
|
|
}
|
2007-06-08 00:08:38 +02:00
|
|
|
unset($definition);
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2009-04-28 18:05:04 +02:00
|
|
|
|
|
|
|
|
2010-03-29 19:38:54 +02:00
|
|
|
$xml = new importexport_arrayxml();
|
2007-06-08 00:08:38 +02:00
|
|
|
return $xml->array2xml($export_data, 'importExportDefinitions');
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2007-06-08 00:08:38 +02:00
|
|
|
/**
|
|
|
|
* imports definitions from file
|
|
|
|
*
|
|
|
|
* @param string $import_file
|
|
|
|
* @throws Exeption
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function import( $_import_file )
|
2006-11-10 16:30:01 +01:00
|
|
|
{
|
2007-06-08 00:08:38 +02:00
|
|
|
if ( !is_file( $_import_file ) ) {
|
2007-06-21 17:09:51 +02:00
|
|
|
throw new Exception("'$_import_file' does not exist or is not readable" );
|
2007-06-08 00:08:38 +02:00
|
|
|
}
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2010-03-31 16:32:41 +02:00
|
|
|
$data = importexport_arrayxml::xml2array( file_get_contents( $_import_file ) );
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2007-06-08 00:08:38 +02:00
|
|
|
$metainfo = $data['importExportDefinitions']['metainfo'];
|
|
|
|
$definitions = $data['importExportDefinitions']['definitions'];
|
|
|
|
unset ( $data );
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2006-11-16 12:04:09 +01:00
|
|
|
// convert charset into internal used charset
|
2016-05-10 00:49:20 +02:00
|
|
|
$definitions = Api\Translation::convert(
|
2007-06-08 00:08:38 +02:00
|
|
|
$definitions,
|
|
|
|
$metainfo['charset'],
|
2016-05-10 00:49:20 +02:00
|
|
|
Api\Translation::charset()
|
2007-06-08 00:08:38 +02:00
|
|
|
);
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2011-03-21 15:12:37 +01:00
|
|
|
// Avoid warning if no definitions found
|
2012-01-20 19:39:50 +01:00
|
|
|
if(!is_array($definitions)) return lang('None found');
|
2011-03-21 15:12:37 +01:00
|
|
|
|
2006-11-16 12:04:09 +01:00
|
|
|
// save definition(s) into internal table
|
2007-06-08 00:08:38 +02:00
|
|
|
foreach ( $definitions as $name => $definition_data )
|
2006-11-10 16:30:01 +01:00
|
|
|
{
|
2007-06-09 17:36:30 +02:00
|
|
|
// convert allowed_user
|
2010-03-22 16:09:26 +01:00
|
|
|
$definition_data['allowed_users'] = importexport_helper_functions::account_name2id( $definition_data['allowed_users'] );
|
2012-06-05 23:50:34 +02:00
|
|
|
if($definition_data['all_users'] && !$definition_data['allowed_users']) $definition_data['allowed_users'] = 'all';
|
|
|
|
|
2010-03-22 16:09:26 +01:00
|
|
|
$definition_data['owner'] = importexport_helper_functions::account_name2id( $definition_data['owner'] );
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2010-03-22 16:09:26 +01:00
|
|
|
$definition = new importexport_definition( $definition_data['name'] );
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2011-02-03 19:09:39 +01:00
|
|
|
// Only update if the imported is newer
|
2023-01-26 10:42:18 +01:00
|
|
|
if(!$definition->modified || strtotime($definition->modified) < strtotime($definition_data['modified']))
|
2011-02-03 19:09:39 +01:00
|
|
|
{
|
|
|
|
$definition_id = $definition->get_identifier() ? $definition->get_identifier() : NULL;
|
|
|
|
|
|
|
|
$definition->set_record( $definition_data );
|
2013-02-20 17:17:03 +01:00
|
|
|
$definition->save( $definition_id );
|
2011-02-03 19:09:39 +01:00
|
|
|
}
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2012-01-20 19:39:50 +01:00
|
|
|
return $definitions;
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2009-04-28 18:05:04 +02:00
|
|
|
|
2011-12-01 19:49:42 +01:00
|
|
|
/**
|
|
|
|
* Create a matching export definition from the given import definition.
|
|
|
|
*
|
|
|
|
* Sets up the field mapping as closely as possible, and sets charset,
|
|
|
|
* header, conversion, etc. using the associated export plugin. Plugin
|
|
|
|
* is determined by replacing 'import' with 'export'.
|
|
|
|
*
|
|
|
|
* It is not possible to handle some plugin options automatically, because they
|
|
|
|
* just don't have equivalents. (eg: What to do with unknown categories)
|
|
|
|
*
|
|
|
|
* @param importexport_definition $definition Import definition
|
|
|
|
*
|
|
|
|
* @return importexport_definition Export definition
|
|
|
|
*/
|
|
|
|
public static function export_from_import(importexport_definition $import)
|
|
|
|
{
|
|
|
|
// Only operates on import definitions
|
2016-05-10 00:49:20 +02:00
|
|
|
if($import->type != 'import') throw new Api\Exception\WrongParameter('Only import definitions');
|
2013-02-06 16:17:33 +01:00
|
|
|
|
2011-12-01 19:49:42 +01:00
|
|
|
// Find export plugin
|
|
|
|
$plugin = str_replace('import', 'export',$import->plugin);
|
|
|
|
$plugin_list = importexport_helper_functions::get_plugins($import->application, 'export');
|
|
|
|
foreach($plugin_list as $appname => $type)
|
|
|
|
{
|
|
|
|
$plugins = $type['export'];
|
|
|
|
foreach($plugins as $name => $label)
|
|
|
|
{
|
|
|
|
if($plugin == $name) break;
|
|
|
|
}
|
|
|
|
if($plugin !== $name) $plugin = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
$export = new importexport_definition();
|
2013-02-06 16:17:33 +01:00
|
|
|
|
2011-12-01 19:49:42 +01:00
|
|
|
// Common settings
|
|
|
|
$export->name = str_replace('import', 'export',$import->name);
|
|
|
|
if($export->name == $import->name) $export->name = $export->name . '-export';
|
|
|
|
$test = new importexport_definition($export->name);
|
|
|
|
if($test->name) $export->name = $export->name .'-'.$GLOBALS['egw_info']['user']['account_lid'];
|
|
|
|
|
|
|
|
$export->application = $import->application;
|
|
|
|
$export->plugin = $plugin;
|
|
|
|
$export->type = 'export';
|
2013-02-06 16:17:33 +01:00
|
|
|
|
2011-12-01 19:49:42 +01:00
|
|
|
// Options
|
|
|
|
$i_options = $import->plugin_options;
|
|
|
|
$e_options = array(
|
|
|
|
'delimiter' => $i_options['fieldsep'],
|
|
|
|
'charset' => $i_options['charset'],
|
|
|
|
'begin_with_fieldnames' => $i_options['num_header_lines'] ? ($i_options['convert'] ? 'label' : 1) : 0,
|
|
|
|
'convert' => $i_options['convert']
|
|
|
|
);
|
|
|
|
|
|
|
|
// Mapping
|
|
|
|
foreach($i_options['field_mapping'] as $col_num => $field)
|
|
|
|
{
|
|
|
|
// Try to use heading from import file, if possible
|
|
|
|
$e_options['mapping'][$field] = $i_options['csv_fields'][$col_num] ? $i_options['csv_fields'][$col_num] : $field;
|
|
|
|
}
|
2011-12-14 17:33:54 +01:00
|
|
|
// Keep field names
|
|
|
|
$e_options['no_header_translation'] = true;
|
|
|
|
|
2011-12-01 19:49:42 +01:00
|
|
|
$export->plugin_options = $e_options;
|
|
|
|
|
|
|
|
// Permissions
|
|
|
|
$export->owner = $import->owner;
|
|
|
|
$export->allowed_users = $import->allowed_users;
|
|
|
|
|
|
|
|
return $export;
|
|
|
|
}
|
2019-07-22 23:10:43 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* changes or deletes entries with a specified owner (for deleteaccount hook)
|
|
|
|
*
|
|
|
|
* @param array $args hook arguments
|
|
|
|
* @param int $args['account_id'] account to delete
|
|
|
|
* @param int $args['new_owner']=0 new owner
|
|
|
|
* @todo test deleting an owner with replace and without
|
|
|
|
*/
|
|
|
|
function change_delete_owner(array $args) // new_owner=0 means delete
|
|
|
|
{
|
|
|
|
if (!(int) $args['new_owner'])
|
|
|
|
{
|
|
|
|
$this->so_sql->delete(array('owner'=>$args['account_id']));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->db->update(
|
|
|
|
self::_defintion_table,
|
|
|
|
array('owner'=>$args['new_owner']),
|
|
|
|
array('owner'=>$args['account_id']),
|
|
|
|
__LINE__,__FILE__,'importexport'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-26 10:42:18 +01:00
|
|
|
}
|