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
|
|
|
/**
|
|
|
|
* class definition
|
2011-06-08 12:59:45 +02:00
|
|
|
*
|
|
|
|
* definitions are ojects with all nessesary information to do
|
2006-11-10 16:30:01 +01:00
|
|
|
* complete import or export. All options needed for an explicit {Im|Ex}port
|
|
|
|
* are in one assiozative array which is complely managed by {Im|Ex}port plugins
|
|
|
|
* @todo testing
|
|
|
|
*/
|
2010-03-22 16:09:26 +01:00
|
|
|
class importexport_definition implements importexport_iface_egw_record {
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
const _appname = 'importexport';
|
|
|
|
const _defintion_talbe = 'egw_importexport_definitions';
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
private $attributes = array(
|
|
|
|
'definition_id' => 'string',
|
|
|
|
'name' => 'string',
|
|
|
|
'application' => 'string',
|
|
|
|
'plugin' => 'string',
|
|
|
|
'type' => 'string',
|
|
|
|
'allowed_users' => 'array',
|
2007-06-08 00:08:38 +02:00
|
|
|
'plugin_options' => 'array',
|
2012-12-31 21:36:28 +01:00
|
|
|
'filter' => 'array',
|
2006-11-10 16:30:01 +01:00
|
|
|
'owner' => 'int',
|
2006-11-16 12:04:09 +01:00
|
|
|
'description' => 'string',
|
2011-02-03 19:09:39 +01:00
|
|
|
'modified' => 'timestamp'
|
2006-11-10 16:30:01 +01:00
|
|
|
);
|
2011-06-08 12:59:45 +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 object
|
2006-11-10 16:30:01 +01:00
|
|
|
*/
|
|
|
|
private $so_sql;
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
2007-06-09 17:36:30 +02:00
|
|
|
* @var array internal representation of definition
|
2006-11-10 16:30:01 +01:00
|
|
|
*/
|
|
|
|
private $definition = array();
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
2007-06-09 17:36:30 +02:00
|
|
|
* @var int holds current user
|
2006-11-10 16:30:01 +01:00
|
|
|
*/
|
|
|
|
private $user;
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
2007-06-09 17:36:30 +02:00
|
|
|
* @var bool is current user an admin?
|
2006-11-10 16:30:01 +01:00
|
|
|
*/
|
|
|
|
private $is_admin;
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
* reads record from backend if identifier is given.
|
|
|
|
*
|
|
|
|
* @param string $_identifier
|
|
|
|
*/
|
|
|
|
public function __construct( $_identifier='' ) {
|
2016-05-10 00:49:20 +02:00
|
|
|
$this->so_sql = new Api\Storage\Base(self::_appname ,self::_defintion_talbe);
|
2006-11-10 16:30:01 +01:00
|
|
|
$this->user = $GLOBALS['egw_info']['user']['user_id'];
|
2011-06-08 12:59:45 +02:00
|
|
|
$this->is_admin = $GLOBALS['egw_info']['user']['apps']['admin'] || $GLOBALS['egw_setup'] ? true : false;
|
2006-11-10 16:30:01 +01:00
|
|
|
// compability to string identifiers
|
2011-12-01 19:49:42 +01:00
|
|
|
if (!is_numeric($_identifier) && strlen($_identifier) > 3) $_identifier = $this->name2identifier($_identifier);
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
if ((int)$_identifier != 0) {
|
2007-06-08 00:08:38 +02:00
|
|
|
$this->definition = $this->so_sql->read(array('definition_id' => $_identifier));
|
|
|
|
if ( empty( $this->definition ) ) {
|
2006-11-10 16:30:01 +01:00
|
|
|
throw new Exception('Error: No such definition with identifier :"'.$_identifier.'"!');
|
|
|
|
}
|
2011-02-11 19:03:44 +01:00
|
|
|
if ( !( importexport_definitions_bo::is_permitted($this->get_record_array()) || $this->is_admin)) {
|
2006-11-10 16:30:01 +01:00
|
|
|
throw new Exception('Error: User "'.$this->user.'" is not permitted to get definition with identifier "'.$_identifier.'"!');
|
|
|
|
}
|
2013-01-03 16:53:31 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$options_data = importexport_arrayxml::xml2array( $this->definition['plugin_options'] );
|
2016-07-21 22:38:31 +02:00
|
|
|
$this->definition['plugin_options'] = (array)$options_data['root'];
|
2013-01-03 16:53:31 +01:00
|
|
|
if($this->definition['filter']) $filter = importexport_arrayxml::xml2array( $this->definition['filter'] );
|
|
|
|
$this->definition['filter'] = $filter['root'];
|
|
|
|
}
|
|
|
|
catch (Exception $e)
|
|
|
|
{
|
|
|
|
error_log($e->getMessage());
|
|
|
|
}
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* compability function for string identifiers e.g. used by cli
|
|
|
|
*
|
|
|
|
* @param string $_name
|
|
|
|
* @return int
|
|
|
|
*/
|
2007-06-08 00:08:38 +02:00
|
|
|
private function name2identifier( $_name ) {
|
2006-11-10 16:30:01 +01:00
|
|
|
$identifiers = $this->so_sql->search(array('name' => $_name),true);
|
|
|
|
if (isset($identifiers[1])) {
|
|
|
|
throw new Exception('Error: Definition: "'.$_name. '" is not unique! Can\'t convert to identifier');
|
|
|
|
}
|
2007-06-21 16:02:03 +02:00
|
|
|
if ( empty( $identifiers[0] ) ) {
|
2007-06-21 17:09:51 +02:00
|
|
|
// not a good idea, till we don't have different exceptions so far
|
|
|
|
// throw new Exception('Error: No such definition :"'.$_name.'"!');
|
|
|
|
$identifiers = array( array( 'definition_id' => 0 ) );
|
2007-06-21 16:02:03 +02:00
|
|
|
}
|
2006-11-10 16:30:01 +01:00
|
|
|
return $identifiers[0]['definition_id'];
|
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
public function __get($_attribute_name) {
|
|
|
|
if (!array_key_exists($_attribute_name,$this->attributes)) {
|
|
|
|
throw new Exception('Error: "'. $_attribute_name. '" is not an attribute defintion');
|
|
|
|
}
|
|
|
|
switch ($_attribute_name) {
|
|
|
|
case 'allowed_users' :
|
|
|
|
return $this->get_allowed_users();
|
2007-06-08 00:08:38 +02:00
|
|
|
case 'plugin_options' :
|
2006-11-10 16:30:01 +01:00
|
|
|
return $this->get_options();
|
2012-12-31 21:36:28 +01:00
|
|
|
case 'filter':
|
|
|
|
return $this->get_filter();
|
2006-11-10 16:30:01 +01:00
|
|
|
default :
|
|
|
|
return $this->definition[$_attribute_name];
|
|
|
|
}
|
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
public function __set($_attribute_name,$_data) {
|
|
|
|
if (!array_key_exists($_attribute_name,$this->attributes)) {
|
|
|
|
throw new Exception('Error: "'. $_attribute_name. '" is not an attribute defintion');
|
|
|
|
}
|
|
|
|
switch ($_attribute_name) {
|
|
|
|
case 'allowed_users' :
|
|
|
|
return $this->set_allowed_users($_data);
|
2007-06-08 00:08:38 +02:00
|
|
|
case 'plugin_options' :
|
2006-11-10 16:30:01 +01:00
|
|
|
return $this->set_options($_data);
|
2012-12-31 21:36:28 +01:00
|
|
|
case 'filter':
|
2013-01-02 23:38:43 +01:00
|
|
|
return $this->set_filter((Array)$_data);
|
2006-11-10 16:30:01 +01:00
|
|
|
default :
|
|
|
|
$this->definition[$_attribute_name] = $_data;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* gets users who are allowd to use this definition
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function get_allowed_users() {
|
2011-05-18 16:46:57 +02:00
|
|
|
return explode(',',substr($this->definition['allowed_users'],1,-1));
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* sets allowed users
|
|
|
|
*
|
|
|
|
* @param array $_allowed_users
|
|
|
|
*/
|
2007-06-08 00:08:38 +02:00
|
|
|
private function set_allowed_users( $_allowed_users ) {
|
2011-05-18 16:46:57 +02:00
|
|
|
$this->definition['allowed_users'] = ','.implode(',',(array)$_allowed_users) .',';
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* gets options
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function get_options() {
|
2007-06-10 00:23:29 +02:00
|
|
|
return $this->definition['plugin_options'];
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* sets options
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
*/
|
2007-06-08 00:08:38 +02:00
|
|
|
private function set_options(array $_plugin_options) {
|
2007-06-10 00:23:29 +02:00
|
|
|
$this->definition['plugin_options'] = $_plugin_options;
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2012-12-31 21:36:28 +01:00
|
|
|
/**
|
|
|
|
* Get stored data filter
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function get_filter() {
|
|
|
|
return $this->definition['filter'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set stored data filter
|
|
|
|
*
|
|
|
|
* @param filter array of field => settings
|
|
|
|
*/
|
|
|
|
private function set_filter(Array $filter) {
|
|
|
|
$this->definition['filter'] = $filter;
|
|
|
|
}
|
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
2011-06-08 12:59:45 +02:00
|
|
|
* converts this object to array.
|
2006-11-10 16:30:01 +01:00
|
|
|
* @abstract We need such a function cause PHP5
|
|
|
|
* dosn't allow objects do define it's own casts :-(
|
|
|
|
* once PHP can deal with object casts we will change to them!
|
|
|
|
*
|
|
|
|
* @return array complete record as associative array
|
|
|
|
*/
|
|
|
|
public function get_record_array() {
|
|
|
|
$definition = $this->definition;
|
|
|
|
$definition['allowed_users'] = $this->get_allowed_users();
|
2007-06-08 00:08:38 +02:00
|
|
|
$definition['plugin_options'] = $this->get_options();
|
2012-12-31 21:36:28 +01:00
|
|
|
$definition['filter'] = $this->get_filter();
|
2006-11-10 16:30:01 +01:00
|
|
|
return $definition;
|
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* gets title of record
|
2011-06-08 12:59:45 +02:00
|
|
|
*
|
2006-11-10 16:30:01 +01:00
|
|
|
*@return string tiltle
|
|
|
|
*/
|
|
|
|
public function get_title() {
|
|
|
|
return $this->definition['name'];
|
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* sets complete record from associative array
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-06-08 00:08:38 +02:00
|
|
|
public function set_record( array $_record ) {
|
|
|
|
$this->definition = array_intersect_key( $_record, $this->attributes );
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2007-06-08 00:08:38 +02:00
|
|
|
// anything which is not an attribute is perhaps a plugin_option.
|
|
|
|
// If not, it gets whiped out on save time.
|
|
|
|
foreach ( $_record as $attribute => $value) {
|
|
|
|
if ( !array_key_exists( $attribute, $this->attributes ) ) {
|
|
|
|
$this->definition['plugin_options'][$attribute] = $value;
|
|
|
|
}
|
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2010-03-22 16:09:26 +01:00
|
|
|
$this->plugin = $_record['plugin'];
|
|
|
|
|
2007-06-08 00:08:38 +02:00
|
|
|
// convert plugin_options into internal representation
|
2007-06-10 00:23:29 +02:00
|
|
|
$this->set_allowed_users( $this->definition['allowed_users'] );
|
2010-03-22 16:09:26 +01:00
|
|
|
$this->set_options( $this->definition['plugin_options'] ? $this->definition['plugin_options'] : array());
|
2012-12-31 21:36:28 +01:00
|
|
|
$this->set_filter( $this->definition['filter'] ? $this->definition['filter'] : array());
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* gets identifier of this record
|
|
|
|
*
|
2007-06-21 20:07:31 +02:00
|
|
|
* @return int identifier of this record
|
2006-11-10 16:30:01 +01:00
|
|
|
*/
|
|
|
|
public function get_identifier() {
|
|
|
|
return $this->definition['definition_id'];
|
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
|
|
|
|
2014-11-19 00:40:49 +01:00
|
|
|
/**
|
|
|
|
* Gets the URL icon representitive of the record
|
|
|
|
* This could be as general as the application icon, or as specific as a contact photo
|
|
|
|
*
|
|
|
|
* @return string Full URL of an icon, or appname/icon_name
|
|
|
|
*/
|
|
|
|
public function get_icon() {
|
|
|
|
return self::_appname . '/navbar';
|
|
|
|
}
|
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* saves record into backend
|
2011-06-08 12:59:45 +02:00
|
|
|
*
|
2006-11-10 16:30:01 +01:00
|
|
|
* @return string identifier
|
|
|
|
*/
|
|
|
|
public function save ( $_dst_identifier ) {
|
2007-06-08 00:08:38 +02:00
|
|
|
if ( strlen($this->definition['name']) < 3 ) {
|
|
|
|
throw new Exception('Error: Can\'t save definition, no valid name given!');
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2007-06-08 00:08:38 +02:00
|
|
|
$this->so_sql->data = $this->definition;
|
2010-03-29 19:38:54 +02:00
|
|
|
$this->so_sql->data['plugin_options'] = importexport_arrayxml::array2xml( $this->definition['plugin_options'] );
|
2012-12-31 21:36:28 +01:00
|
|
|
$this->so_sql->data['filter'] = importexport_arrayxml::array2xml( $this->definition['filter'] );
|
2011-02-03 19:09:39 +01:00
|
|
|
$this->so_sql->data['modified'] = time();
|
2007-06-08 00:08:38 +02:00
|
|
|
if ($this->so_sql->save( array( 'definition_id' => $_dst_identifier ))) {
|
2016-05-10 00:49:20 +02:00
|
|
|
throw new Exception('Error: Api\Storage\Base was not able to save definition: '.$this->get_identifier());
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
return $this->definition['definition_id'];
|
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* copys current record to record identified by $_dst_identifier
|
|
|
|
*
|
|
|
|
* @param string $_dst_identifier
|
|
|
|
* @return string dst_identifier
|
|
|
|
*/
|
|
|
|
public function copy ( $_dst_identifier ) {
|
|
|
|
$dst_object = clone $this;
|
|
|
|
try {
|
|
|
|
$dst_object->set_owner($this->user);
|
|
|
|
$dst_identifier = $dst_object->save($_dst_identifier);
|
|
|
|
}
|
|
|
|
catch(exception $Exception) {
|
|
|
|
unset($dst_object);
|
|
|
|
throw $Exception;
|
|
|
|
}
|
|
|
|
unset ($dst_object);
|
|
|
|
return $dst_identifier;
|
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* moves current record to record identified by $_dst_identifier
|
|
|
|
* $this will become moved record
|
|
|
|
*
|
|
|
|
* @param string $_dst_identifier
|
|
|
|
* @return string dst_identifier
|
|
|
|
*/
|
|
|
|
public function move ( $_dst_identifier ) {
|
|
|
|
if ($this->user != $this->get_owner() && !$this->is_admin) {
|
|
|
|
throw('Error: User '. $this->user. 'does not have permissions to move definition '.$this->get_identifier());
|
|
|
|
}
|
|
|
|
$old_object = clone $this;
|
|
|
|
try {
|
|
|
|
$dst_identifier = $this->save($_dst_identifier);
|
|
|
|
$old_object->delete();
|
|
|
|
}
|
|
|
|
catch(exception $Exception) {
|
|
|
|
unset($old_object);
|
|
|
|
throw $Exception;
|
|
|
|
}
|
|
|
|
unset($old_object);
|
|
|
|
return $dst_identifier;
|
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* delets current record from backend
|
|
|
|
* @return void
|
2011-06-08 12:59:45 +02:00
|
|
|
*
|
2006-11-10 16:30:01 +01:00
|
|
|
*/
|
|
|
|
public function delete () {
|
|
|
|
if($this->user != $this->get_owner() && !$this->is_admin) {
|
|
|
|
throw('Error: User '. $this->user. 'does not have permissions to delete definition '.$this->get_identifier());
|
|
|
|
}
|
|
|
|
if(!$this->so_sql->delete()) {
|
2016-05-10 00:49:20 +02:00
|
|
|
throw('Error: Api\Storage\Base was not able to delete definition: '.$this->get_identifier());
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* destructor
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __destruct() {
|
|
|
|
unset($this->so_sql);
|
|
|
|
}
|
2011-06-08 12:59:45 +02:00
|
|
|
|
2007-08-30 17:46:31 +02:00
|
|
|
}
|