egroupware/importexport/inc/class.importexport_definitions_bo.inc.php

239 lines
7.3 KiB
PHP
Raw Normal View History

<?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$
*/
/** bo to define {im|ex}ports
*
* @todo make this class an egw_record_pool!
*/
class importexport_definitions_bo {
const _appname = 'importexport';
const _defintion_table = 'egw_importexport_definitions';
2009-04-28 18:05:04 +02:00
/**
* @var so_sql holds so_sql
*/
private $so_sql;
2009-04-28 18:05:04 +02:00
/**
* @var array hold definitions
*/
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)
{
$this->so_sql = new so_sql(self::_appname, self::_defintion_table );
if ($_query) {
2011-02-03 17:42:03 +01:00
$definitions = $this->so_sql->search($_query, false);
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'];
}
}
}
2009-04-28 18:05:04 +02:00
2011-04-19 18:00:43 +02:00
public function get_rows(&$query, &$rows, &$readonlys)
{
// 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 .= ' (';
$read = array();
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);
$sql .= ') OR owner = '.$GLOBALS['egw_info']['user']['account_id'];
$query['col_filter'][] = $sql;
}
$total = $this->so_sql->get_rows($query, $rows, $readonlys);
2011-05-18 16:22:31 +02:00
$ro_count = 0;
foreach($rows as &$row) {
// Strip off leading + trailing ,
$row['allowed_users'] = substr($row['allowed_users'],1,-1);
$readonlys["edit[{$row['definition_id']}]"] = $readonlys["delete[{$row['definition_id']}]"] =
($row['owner'] != $GLOBALS['egw_info']['user']['account_id']) &&
!$GLOBALS['egw_info']['user']['apps']['admin'];
if($readonlys["edit[{$row['definition_id']}]"])
{
$row['class'] .= 'rowNoEdit';
$ro_count++;
}
}
2011-05-18 16:22:31 +02:00
$readonlys['delete_selected'] = $ro_count == count($rows);
return $total;
2011-04-19 18:00:43 +02:00
}
2007-06-08 00:08:38 +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
*/
public function get_definitions() {
return $this->definitions;
}
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();
}
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
unset($this->definitions[array_search($key,$this->definitions)]);
} else {
unset($keys[$index]);
}
}
if(count($keys) > 0) {
$this->so_sql->delete(array('definition_id' => $keys));
}
}
/**
* Save a definition
*
* @param definition $definition
*/
public function save(Array $data) {
$definition = new importexport_definition();
$definition->set_record($data);
$definition->save($data['definition_id']);
}
2009-04-28 18:05:04 +02:00
/**
* checkes if user if permitted to access given definition
*
* @param array $_definition
* @return bool
*/
static public function is_permitted($_definition) {
$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;
$alluser = array_intersect($allowed_user,$this_membership);
return ($this_user_id == $_definition['owner'] || count($alluser) > 0);
}
2009-04-28 18:05:04 +02:00
/**
* exports definitions
*
* @param array $keys to export
*/
public function export($keys)
{
$export_data = array('metainfo' => array(
'type' => 'importexport definitions',
2009-04-28 18:05:04 +02:00
'charset' => translation::charset(),
'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) {
$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'] =
importexport_helper_functions::account_id2name(
2009-04-28 18:05:04 +02:00
$export_data['definitions'][$definition->name]['allowed_users']
);
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']);
unset($export_data['definitions'][$definition->name]['description']);
unset($export_data['definitions'][$definition->name]['user_timezone_read']);
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);
}
2009-04-28 18:05:04 +02:00
$xml = new importexport_arrayxml();
2007-06-08 00:08:38 +02:00
return $xml->array2xml($export_data, 'importExportDefinitions');
}
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 )
{
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
// convert charset into internal used charset
2009-04-28 18:05:04 +02:00
$definitions = translation::convert(
2007-06-08 00:08:38 +02:00
$definitions,
$metainfo['charset'],
2009-04-28 18:05:04 +02:00
translation::charset()
2007-06-08 00:08:38 +02:00
);
2009-04-28 18:05:04 +02:00
// Avoid warning if no definitions found
if(!is_array($definitions)) return;
// save definition(s) into internal table
2007-06-08 00:08:38 +02:00
foreach ( $definitions as $name => $definition_data )
{
// convert allowed_user
$definition_data['allowed_users'] = importexport_helper_functions::account_name2id( $definition_data['allowed_users'] );
$definition_data['owner'] = importexport_helper_functions::account_name2id( $definition_data['owner'] );
2009-04-28 18:05:04 +02:00
$definition = new importexport_definition( $definition_data['name'] );
2009-04-28 18:05:04 +02:00
// Only update if the imported is newer
if(strtotime($definition->modified) < strtotime($definition_data['modified']) || $definition->modified == 0)
{
$definition_id = $definition->get_identifier() ? $definition->get_identifier() : NULL;
$definition->set_record( $definition_data );
$definition->save( $definition_id );
}
}
}
2009-04-28 18:05:04 +02:00
}