2006-11-10 16:30:01 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* eGroupWare
|
|
|
|
*
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-03-29 19:38:54 +02:00
|
|
|
* class importexport_helper_functions (only static methods)
|
|
|
|
* use importexport_helper_functions::method
|
2006-11-10 16:30:01 +01:00
|
|
|
*/
|
2010-03-22 16:09:26 +01:00
|
|
|
class importexport_helper_functions {
|
2010-04-03 12:31:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Plugins are scanned and cached for all instances using this source path for given time (in seconds)
|
|
|
|
*/
|
|
|
|
const CACHE_EXPIRATION = 3600;
|
2010-03-29 19:38:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Files known to cause problems, and will be skipped in a plugin scan
|
|
|
|
* If you put appname => true, the whole app will be skipped.
|
|
|
|
*/
|
|
|
|
protected static $blacklist_files = array(
|
|
|
|
'news_admin' => array(
|
|
|
|
'class.news_admin_import.inc.php',
|
|
|
|
),
|
|
|
|
);
|
2010-10-07 00:33:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class used to provide extra conversion functions
|
|
|
|
*
|
|
|
|
* Passed in as a param to conversion()
|
|
|
|
*/
|
|
|
|
protected static $cclass = null;
|
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* nothing to construct here, only static functions!
|
|
|
|
*/
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2007-06-23 14:07:18 +02:00
|
|
|
/**
|
|
|
|
* Converts a custom time string to to unix timestamp
|
2010-04-03 12:31:57 +02:00
|
|
|
* The format of the time string is given by the argument $_format
|
2007-06-23 14:07:18 +02:00
|
|
|
* which takes the same parameters as the php date() function.
|
|
|
|
*
|
2007-07-06 11:42:23 +02:00
|
|
|
* @abstract supportet formatstrings: d,m,y,Y,H,h,i,s,O,a,A
|
2007-06-23 14:07:18 +02:00
|
|
|
* If timestring is empty, php strtotime is used.
|
|
|
|
* @param string $_string time string to convert
|
|
|
|
* @param string $_format format of time string e.g.: d.m.Y H:i
|
2007-08-24 14:52:50 +02:00
|
|
|
* @param int $_is_dst is day light saving time? 0 = no, 1 = yes, -1 = system default
|
2007-06-23 14:07:18 +02:00
|
|
|
*/
|
2007-08-24 14:52:50 +02:00
|
|
|
public static function custom_strtotime( $_string, $_format='', $_is_dst = -1) {
|
2007-06-23 14:07:18 +02:00
|
|
|
if ( empty( $_format ) ) return strtotime( $_string );
|
|
|
|
$fparams = explode( ',', chunk_split( $_format, 1, ',' ) );
|
|
|
|
$spos = 0;
|
|
|
|
foreach ( $fparams as $fparam ) {
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2007-06-23 14:07:18 +02:00
|
|
|
switch ( $fparam ) {
|
|
|
|
case 'd': (int)$day = substr( $_string, $spos, 2 ); $spos += 2; break;
|
|
|
|
case 'm': (int)$mon = substr( $_string, $spos, 2 ); $spos += 2; break;
|
|
|
|
case 'y': (int)$year = substr( $_string, $spos, 2 ); $spos += 2; break;
|
|
|
|
case 'Y': (int)$year = substr( $_string, $spos, 4 ); $spos += 4; break;
|
|
|
|
case 'H': (int)$hour = substr( $_string, $spos, 2 ); $spos += 2; break;
|
|
|
|
case 'h': (int)$hour = substr( $_string, $spos, 2 ); $spos += 2; break;
|
|
|
|
case 'i': (int)$min = substr( $_string, $spos, 2 ); $spos += 2; break;
|
2007-07-06 11:42:23 +02:00
|
|
|
case 's': (int)$sec = substr( $_string, $spos, 2 ); $spos += 2; break;
|
2007-06-23 14:07:18 +02:00
|
|
|
case 'O': (int)$offset = $year = substr( $_string, $spos, 5 ); $spos += 5; break;
|
|
|
|
case 'a': (int)$hour = $fparam == 'am' ? $hour : $hour + 12; break;
|
|
|
|
case 'A': (int)$hour = $fparam == 'AM' ? $hour : $hour + 12; break;
|
|
|
|
default: $spos++; // seperator
|
|
|
|
}
|
|
|
|
}
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2007-06-23 14:07:18 +02:00
|
|
|
print_debug("hour:$hour; min:$min; sec:$sec; mon:$mon; day:$day; year:$year;\n");
|
2007-08-24 14:52:50 +02:00
|
|
|
$timestamp = mktime($hour, $min, $sec, $mon, $day, $year, $_is_dst);
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2007-06-23 14:07:18 +02:00
|
|
|
// offset given?
|
|
|
|
if ( isset( $offset ) && strlen( $offset == 5 ) ) {
|
|
|
|
$operator = $offset{0};
|
|
|
|
$ohour = 60 * 60 * (int)substr( $offset, 1, 2 );
|
|
|
|
$omin = 60 * (int)substr( $offset, 3, 2 );
|
|
|
|
if ( $operator == '+' ) $timestamp += $ohour + $omin;
|
|
|
|
else $timestamp -= $ohour + $omin;
|
|
|
|
}
|
|
|
|
return $timestamp;
|
|
|
|
}
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* converts accound_lid to account_id
|
|
|
|
*
|
2007-06-09 17:36:30 +02:00
|
|
|
* @param mixed $_account_lid comma seperated list or array with lids
|
|
|
|
* @return mixed comma seperated list or array with ids
|
2006-11-10 16:30:01 +01:00
|
|
|
*/
|
2007-06-09 17:36:30 +02:00
|
|
|
public static function account_name2id( $_account_lids ) {
|
|
|
|
$account_lids = is_array( $_account_lids ) ? $_account_lids : explode( ',', $_account_lids );
|
2006-11-10 16:30:01 +01:00
|
|
|
foreach ( $account_lids as $account_lid ) {
|
|
|
|
if ( $account_id = $GLOBALS['egw']->accounts->name2id( $account_lid )) {
|
|
|
|
$account_ids[] = $account_id;
|
|
|
|
}
|
|
|
|
}
|
2007-06-21 17:09:51 +02:00
|
|
|
return is_array( $_account_lids ) ? $account_ids : implode( ',', (array)$account_ids );
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
} // end of member function account_lid2id
|
|
|
|
|
|
|
|
/**
|
|
|
|
* converts account_ids to account_lids
|
|
|
|
*
|
2007-06-09 17:36:30 +02:00
|
|
|
* @param mixed $_account_ids comma seperated list or array with ids
|
|
|
|
* @return mixed comma seperated list or array with lids
|
2006-11-10 16:30:01 +01:00
|
|
|
*/
|
2007-06-09 17:36:30 +02:00
|
|
|
public static function account_id2name( $_account_id ) {
|
|
|
|
$account_ids = is_array( $_account_id ) ? $_account_id : explode( ',', $_account_id );
|
2006-11-10 16:30:01 +01:00
|
|
|
foreach ( $account_ids as $account_id ) {
|
|
|
|
if ( $account_lid = $GLOBALS['egw']->accounts->id2name( $account_id )) {
|
|
|
|
$account_lids[] = $account_lid;
|
|
|
|
}
|
|
|
|
}
|
2007-06-21 17:09:51 +02:00
|
|
|
return is_array( $_account_id ) ? $account_lids : implode( ',', (array)$account_lids );
|
2006-11-10 16:30:01 +01:00
|
|
|
} // end of member function account_id2lid
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* converts cat_id to a cat_name
|
|
|
|
*
|
2007-06-09 17:36:30 +02:00
|
|
|
* @param mixed _cat_ids comma seperated list or array
|
2010-04-03 12:31:57 +02:00
|
|
|
* @return mixed comma seperated list or array with cat_names
|
2006-11-10 16:30:01 +01:00
|
|
|
*/
|
|
|
|
public static function cat_id2name( $_cat_ids ) {
|
2007-06-09 17:36:30 +02:00
|
|
|
$cat_ids = is_array( $_cat_ids ) ? $_cat_ids : explode( ',', $_cat_ids );
|
2006-11-10 16:30:01 +01:00
|
|
|
foreach ( $cat_ids as $cat_id ) {
|
2010-04-03 12:31:57 +02:00
|
|
|
$cat_names[] = categories::id2name( (int)$cat_id );
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2007-06-21 17:09:51 +02:00
|
|
|
return is_array( $_cat_ids ) ? $cat_names : implode(',',(array)$cat_names);
|
2006-11-10 16:30:01 +01:00
|
|
|
} // end of member function category_id2name
|
|
|
|
|
|
|
|
/**
|
|
|
|
* converts cat_name to a cat_id.
|
|
|
|
* If a cat isn't found, it will be created.
|
|
|
|
*
|
2007-06-09 17:36:30 +02:00
|
|
|
* @param mixed $_cat_names comma seperated list or array.
|
|
|
|
* @return mixed comma seperated list or array with cat_ids
|
2006-11-10 16:30:01 +01:00
|
|
|
*/
|
2007-06-09 17:36:30 +02:00
|
|
|
public static function cat_name2id( $_cat_names ) {
|
2010-04-03 12:31:57 +02:00
|
|
|
$cats = new categories(); // uses current user and app (egw_info[flags][currentapp])
|
|
|
|
|
2007-06-09 17:36:30 +02:00
|
|
|
$cat_names = is_array( $_cat_names ) ? $_cat_names : explode( ',', $_cat_names );
|
2006-11-10 16:30:01 +01:00
|
|
|
foreach ( $cat_names as $cat_name ) {
|
2007-08-30 17:46:31 +02:00
|
|
|
if ( $cat_name == '' ) continue;
|
2007-06-21 16:02:03 +02:00
|
|
|
if ( ( $cat_id = $cats->name2id( $cat_name ) ) == 0 ) {
|
2010-04-03 12:31:57 +02:00
|
|
|
$cat_id = $cats->add( array(
|
|
|
|
'name' => $cat_name,
|
|
|
|
'access' => 'public',
|
2007-06-21 16:02:03 +02:00
|
|
|
'descr' => $cat_name. ' ('. lang('Automatically created by importexport'). ')'
|
|
|
|
));
|
|
|
|
}
|
2006-11-10 16:30:01 +01:00
|
|
|
$cat_ids[] = $cat_id;
|
|
|
|
}
|
2007-06-21 17:09:51 +02:00
|
|
|
return is_array( $_cat_names ) ? $cat_ids : implode( ',', (array)$cat_ids );
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
} // end of member function category_name2id
|
|
|
|
|
|
|
|
/**
|
|
|
|
* conversion
|
2010-04-03 12:31:57 +02:00
|
|
|
*
|
|
|
|
* Conversions enable you to change / adapt the content of each _record field for your needs.
|
2006-11-10 16:30:01 +01:00
|
|
|
* General syntax is: pattern1 |> replacement1 || ... || patternN |> replacementN
|
2010-04-03 12:31:57 +02:00
|
|
|
* If the pattern-part of a pair is ommited it will match everything ('^.*$'), which
|
2006-11-10 16:30:01 +01:00
|
|
|
* is only usefull for the last pair, as they are worked from left to right.
|
|
|
|
* Example: 1|>private||public
|
|
|
|
* This will translate a '1' in the _record field to 'privat' and everything else to 'public'.
|
2010-04-03 12:31:57 +02:00
|
|
|
*
|
|
|
|
* In addintion to the fields assign by the pattern of the reg.exp.
|
2007-06-09 17:36:30 +02:00
|
|
|
* you can use all other _record fields, with the syntax |[FIELDINDEX].
|
2010-04-03 12:31:57 +02:00
|
|
|
* Example:
|
|
|
|
* Your record is:
|
2007-06-09 17:36:30 +02:00
|
|
|
* array( 0 => Company, 1 => NFamily, 2 => NGiven
|
2010-04-03 12:31:57 +02:00
|
|
|
* Your conversion string for field 0 (Company):
|
2007-06-09 17:36:30 +02:00
|
|
|
* .+|>|[0]: |[1], |[2]|||[1], |[2]
|
2010-04-03 12:31:57 +02:00
|
|
|
* This constructs something like
|
2007-06-09 17:36:30 +02:00
|
|
|
* Company: FamilyName, GivenName or FamilyName, GivenName if 'Company' is empty.
|
2010-04-03 12:31:57 +02:00
|
|
|
*
|
2007-06-09 17:36:30 +02:00
|
|
|
* Moreover the two helper function cat() and account() can be used.
|
2010-04-03 12:31:57 +02:00
|
|
|
* cat(Cat1,...,CatN) returns a (','-separated) list with the cat_id's. If a
|
2006-11-10 16:30:01 +01:00
|
|
|
* category isn't found, it will be automaticaly added.
|
|
|
|
*
|
2010-04-03 12:31:57 +02:00
|
|
|
* Patterns as well as the replacement can be regular expressions (the replacement is done
|
2006-11-10 16:30:01 +01:00
|
|
|
* via ereg_replace).
|
2010-04-03 12:31:57 +02:00
|
|
|
*
|
2006-11-10 16:30:01 +01:00
|
|
|
* @param array _record reference with record to do the conversion with
|
|
|
|
* @param array _conversion array with conversion description
|
2010-10-07 00:33:52 +02:00
|
|
|
* @param object &$cclass calling class to process the '@ evals'
|
2006-11-10 16:30:01 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2010-10-07 00:33:52 +02:00
|
|
|
public static function conversion( &$_record, $_conversion, &$_cclass = null ) {
|
2007-04-06 10:03:04 +02:00
|
|
|
if (empty( $_conversion ) ) return $_record;
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2010-10-07 00:33:52 +02:00
|
|
|
self::$cclass =& $_cclass;
|
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
$PSep = '||'; // Pattern-Separator, separats the pattern-replacement-pairs in conversion
|
|
|
|
$ASep = '|>'; // Assignment-Separator, separats pattern and replacesment
|
2007-08-30 17:46:31 +02:00
|
|
|
$CPre = '|['; $CPos = ']'; // |[_record-idx] is expanded to the corespondig value
|
2007-06-25 22:47:38 +02:00
|
|
|
$TPre = '|T{'; $TPos = '}'; // |{_record-idx} is trimmed
|
2007-08-30 17:46:31 +02:00
|
|
|
$CntlPre = '|TC{'; // Filter all cntl-chars \x01-\x1f and trim
|
2007-06-25 22:47:38 +02:00
|
|
|
$CntlnCLPre = '|TCnCL{'; // Like |C{ but allowes CR and LF
|
2007-08-30 17:46:31 +02:00
|
|
|
$INE = '|INE{'; // Only insert if stuff in ^^ is not empty
|
2010-02-26 00:13:18 +01:00
|
|
|
|
2007-06-09 17:36:30 +02:00
|
|
|
foreach ( $_conversion as $idx => $conversion_string ) {
|
|
|
|
if ( empty( $conversion_string ) ) continue;
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2007-06-09 17:36:30 +02:00
|
|
|
// fetch patterns ($rvalues)
|
|
|
|
$pat_reps = explode( $PSep, stripslashes( $conversion_string ) );
|
|
|
|
foreach( $pat_reps as $k => $pat_rep ) {
|
|
|
|
list( $pattern, $replace ) = explode( $ASep, $pat_rep, 2 );
|
|
|
|
if( $replace == '' ) {
|
|
|
|
$replace = $pattern; $pattern = '^.*$';
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2007-06-09 17:36:30 +02:00
|
|
|
$rvalues[$pattern] = $replace; // replace two with only one, added by the form
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2007-06-25 22:47:38 +02:00
|
|
|
// conversion list may be longer than $_record aka (no_csv)
|
2007-06-09 17:36:30 +02:00
|
|
|
$val = array_key_exists( $idx, $_record ) ? $_record[$idx] : '';
|
2006-11-10 16:30:01 +01:00
|
|
|
|
2010-10-07 00:33:52 +02:00
|
|
|
$c_functions = array('cat', 'account', 'strtotime');
|
|
|
|
if($_cclass) {
|
|
|
|
// Add in additional methods
|
|
|
|
$reflection = new ReflectionClass(get_class($_cclass));
|
|
|
|
$methods = $reflection->getMethods(ReflectionMethod::IS_STATIC);
|
|
|
|
foreach($methods as $method) {
|
|
|
|
$c_functions[] = $method->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$c_functions = implode('|', $c_functions);
|
2007-06-09 17:36:30 +02:00
|
|
|
foreach ( $rvalues as $pattern => $replace ) {
|
|
|
|
if( ereg( (string)$pattern, $val) ) {
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2007-06-09 17:36:30 +02:00
|
|
|
$val = ereg_replace( (string)$pattern, $replace, (string)$val );
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2007-06-09 17:36:30 +02:00
|
|
|
$reg = '\|\[([a-zA-Z_0-9]+)\]';
|
|
|
|
while( ereg( $reg, $val, $vars ) ) {
|
|
|
|
// expand all _record fields
|
|
|
|
$val = str_replace(
|
2010-04-03 12:31:57 +02:00
|
|
|
$CPre . $vars[1] . $CPos,
|
|
|
|
$_record[array_search($vars[1], array_keys($_record))],
|
2007-06-09 17:36:30 +02:00
|
|
|
$val
|
|
|
|
);
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2010-10-07 00:33:52 +02:00
|
|
|
$val = preg_replace_callback( "/($c_functions)\(([^)]*)\)/i", array( self, 'c2_dispatcher') , $val );
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
|
|
|
}
|
2007-06-25 22:47:38 +02:00
|
|
|
// clean each field
|
2007-08-30 17:46:31 +02:00
|
|
|
$val = preg_replace_callback("/(\|T\{|\|TC\{|\|TCnCL\{|\|INE\{)(.*)\}/", array( self, 'strclean'), $val );
|
2010-02-26 00:13:18 +01:00
|
|
|
|
2007-06-25 22:47:38 +02:00
|
|
|
$_record[$idx] = $val;
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2007-06-25 22:47:38 +02:00
|
|
|
return $_record;
|
2006-11-10 16:30:01 +01:00
|
|
|
} // end of member function conversion
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2007-06-09 17:36:30 +02:00
|
|
|
/**
|
|
|
|
* callback for preg_replace_callback from self::conversion.
|
2010-04-03 12:31:57 +02:00
|
|
|
* This function gets called when 2nd level conversions are made,
|
2007-06-09 17:36:30 +02:00
|
|
|
* like the cat() and account() statements in the conversions.
|
|
|
|
*
|
|
|
|
* @param array $_matches
|
|
|
|
*/
|
|
|
|
private static function c2_dispatcher( $_matches ) {
|
|
|
|
$action = &$_matches[1]; // cat or account ...
|
|
|
|
$data = &$_matches[2]; // datas for action
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2007-06-23 14:07:18 +02:00
|
|
|
switch ( $action ) {
|
|
|
|
case 'strtotime' :
|
|
|
|
list( $string, $format ) = explode( ',', $data );
|
|
|
|
return self::custom_strtotime( trim( $string ), trim( $format ) );
|
|
|
|
default :
|
2010-10-07 00:33:52 +02:00
|
|
|
if(self::$cclass && method_exists(self::$cclass, $action)) {
|
|
|
|
$class = get_class(self::$cclass);
|
|
|
|
return call_user_func("$class::$action", $data);
|
|
|
|
}
|
2007-06-23 14:07:18 +02:00
|
|
|
$method = (string)$action. ( is_int( $data ) ? '_id2name' : '_name2id' );
|
2010-10-07 00:33:52 +02:00
|
|
|
if(self::$cclass && method_exists(self::$cclass, $method)) {
|
|
|
|
$class = get_class(self::$cclass);
|
|
|
|
return call_user_func("$class::$action", $data);
|
|
|
|
} else {
|
|
|
|
return self::$method( $data );
|
|
|
|
}
|
2007-06-23 14:07:18 +02:00
|
|
|
}
|
2007-06-09 17:36:30 +02:00
|
|
|
}
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2007-06-25 22:47:38 +02:00
|
|
|
private static function strclean( $_matches ) {
|
|
|
|
switch( $_matches[1] ) {
|
|
|
|
case '|T{' : return trim( $_matches[2] );
|
|
|
|
case '|TC{' : return trim( preg_replace( '/[\x01-\x1F]+/', '', $_matches[2] ) );
|
2007-08-30 17:46:31 +02:00
|
|
|
case '|TCnCL{' : return trim( preg_replace( '/[\x01-\x09\x11\x12\x14-\x1F]+/', '', $_matches[2] ) );
|
2010-04-03 12:31:57 +02:00
|
|
|
case '|INE{' : return preg_match( '/\^.+\^/', $_matches[2] ) ? $_matches[2] : '';
|
2007-06-25 22:47:38 +02:00
|
|
|
default:
|
|
|
|
throw new Exception('Error in conversion string! "'. substr( $_matches[1], 0, -1 ). '" is not valid!');
|
|
|
|
}
|
|
|
|
}
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
2010-04-03 12:31:57 +02:00
|
|
|
* returns a list of importexport plugins
|
2006-11-10 16:30:01 +01:00
|
|
|
*
|
|
|
|
* @param string $_tpye {import | export | all}
|
|
|
|
* @param string $_appname {<appname> | all}
|
|
|
|
* @return array(<appname> => array( <type> => array(<plugin> => <title>)))
|
|
|
|
*/
|
2007-06-08 00:08:38 +02:00
|
|
|
public static function get_plugins( $_appname = 'all', $_type = 'all' ) {
|
2010-03-29 19:38:54 +02:00
|
|
|
$plugins = egw_cache::getTree(
|
2010-04-03 12:31:57 +02:00
|
|
|
__CLASS__,
|
|
|
|
'plugins',
|
|
|
|
array('importexport_helper_functions','_get_plugins'),
|
2010-03-29 19:38:54 +02:00
|
|
|
array(array_keys($GLOBALS['egw_info']['apps']), array('import', 'export')),
|
|
|
|
self::CACHE_EXPIRATION
|
|
|
|
);
|
2006-11-10 16:30:01 +01:00
|
|
|
$appnames = $_appname == 'all' ? array_keys($GLOBALS['egw_info']['apps']) : (array)$_appname;
|
2007-06-08 00:08:38 +02:00
|
|
|
$types = $_type == 'all' ? array('import','export') : (array)$_type;
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2011-02-03 19:09:39 +01:00
|
|
|
// Testing: comment out egw_cache call, use this
|
|
|
|
//$plugins = self::_get_plugins($appnames, $types);
|
2011-01-28 20:44:24 +01:00
|
|
|
foreach($plugins as $appname => $_types) {
|
|
|
|
if(!in_array($appname, $appnames)) unset($plugins[$appname]);
|
2010-03-29 19:38:54 +02:00
|
|
|
}
|
|
|
|
foreach($plugins as $appname => $types) {
|
|
|
|
$plugins[$appname] = array_intersect_key($plugins[$appname], $types);
|
|
|
|
}
|
|
|
|
return $plugins;
|
|
|
|
}
|
|
|
|
|
2010-04-03 12:31:57 +02:00
|
|
|
public static function _get_plugins(Array $appnames, Array $types) {
|
2006-11-10 16:30:01 +01:00
|
|
|
$plugins = array();
|
|
|
|
foreach ($appnames as $appname) {
|
2010-03-29 19:38:54 +02:00
|
|
|
if(array_key_exists($appname, self::$blacklist_files) && self::$blacklist_files[$appname] === true) continue;
|
|
|
|
|
2010-03-22 16:09:26 +01:00
|
|
|
$appdir = EGW_INCLUDE_ROOT. "/$appname/inc";
|
2010-03-29 19:38:54 +02:00
|
|
|
if(!is_dir($appdir)) continue;
|
2006-11-10 16:30:01 +01:00
|
|
|
$d = dir($appdir);
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
// step through each file in appdir
|
|
|
|
while (false !== ($entry = $d->read())) {
|
2010-03-29 19:38:54 +02:00
|
|
|
// Blacklisted?
|
|
|
|
if(is_array(self::$blacklist_files[$appname]) && in_array($entry, self::$blacklist_files[$appname])) continue;
|
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
list( ,$classname, ,$extension) = explode('.',$entry);
|
|
|
|
$file = $appdir. '/'. $entry;
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
foreach ($types as $type) {
|
2010-03-22 16:09:26 +01:00
|
|
|
if( !is_file($file) || strpos($entry, $type) === false || $extension != 'php' ) continue;
|
2006-11-10 16:30:01 +01:00
|
|
|
require_once($file);
|
2010-03-22 16:09:26 +01:00
|
|
|
$reflectionClass = new ReflectionClass($classname);
|
2010-04-03 12:31:57 +02:00
|
|
|
if($reflectionClass->IsInstantiable() &&
|
2010-03-22 16:09:26 +01:00
|
|
|
$reflectionClass->implementsInterface('importexport_iface_'.$type.'_plugin')) {
|
|
|
|
try {
|
|
|
|
$plugin_object = new $classname;
|
|
|
|
}
|
|
|
|
catch (Exception $exception) {
|
|
|
|
continue;
|
|
|
|
}
|
2006-11-10 16:30:01 +01:00
|
|
|
$plugins[$appname][$type][$classname] = $plugin_object->get_name();
|
2010-03-22 16:09:26 +01:00
|
|
|
unset ($plugin_object);
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$d->close();
|
2011-02-03 19:09:39 +01:00
|
|
|
|
|
|
|
// Check for new definitions to import from $appname/setup/*.xml
|
|
|
|
$appdir = EGW_INCLUDE_ROOT. "/$appname/setup";
|
|
|
|
if(!is_dir($appdir)) continue;
|
|
|
|
$d = dir($appdir);
|
|
|
|
|
|
|
|
// step through each file in app's setup
|
|
|
|
while (false !== ($entry = $d->read())) {
|
|
|
|
$file = $appdir. '/'. $entry;
|
|
|
|
list( $filename, $extension) = explode('.',$entry);
|
|
|
|
if ( $extension != 'xml' ) continue;
|
|
|
|
try {
|
|
|
|
// import will skip invalid files
|
|
|
|
importexport_definitions_bo::import( $file );
|
|
|
|
} catch (Exception $e) {
|
|
|
|
error_log(__CLASS__.__FUNCTION__. " import $appname definitions: " . $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$d->close();
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2007-06-08 00:08:38 +02:00
|
|
|
//error_log(__CLASS__.__FUNCTION__.print_r($plugins,true));
|
2010-04-03 12:31:57 +02:00
|
|
|
return $plugins;
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2006-11-10 16:30:01 +01:00
|
|
|
/**
|
|
|
|
* returns list of apps which have plugins of given type.
|
|
|
|
*
|
|
|
|
* @param string $_type
|
|
|
|
* @return array $num => $appname
|
|
|
|
*/
|
2011-02-03 17:49:49 +01:00
|
|
|
public static function get_apps($_type, $ignore_acl = false) {
|
|
|
|
$apps = array_keys(self::get_plugins('all',$_type));
|
|
|
|
if($ignore_acl) return $apps;
|
|
|
|
|
|
|
|
foreach($apps as $key => $app) {
|
|
|
|
if(!self::has_definitions($app, $_type)) unset($apps[$key]);
|
|
|
|
}
|
|
|
|
return $apps;
|
2006-11-10 16:30:01 +01:00
|
|
|
}
|
|
|
|
|
2007-06-08 00:08:38 +02:00
|
|
|
public static function guess_filetype( $_file ) {
|
2010-04-03 12:31:57 +02:00
|
|
|
|
2007-06-08 00:08:38 +02:00
|
|
|
}
|
2011-01-28 20:44:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns if the given app has importexport definitions for the current user
|
|
|
|
*
|
|
|
|
* @param string $_appname {<appname> | all}
|
|
|
|
* @param string $_type {import | export | all}
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function has_definitions( $_appname = 'all', $_type = 'all' ) {
|
|
|
|
$definitions = egw_cache::getSession(
|
|
|
|
__CLASS__,
|
|
|
|
'has_definitions',
|
|
|
|
array('importexport_helper_functions','_has_definitions'),
|
|
|
|
array(array_keys($GLOBALS['egw_info']['apps']), array('import', 'export')),
|
|
|
|
self::CACHE_EXPIRATION
|
|
|
|
);
|
|
|
|
$appnames = $_appname == 'all' ? array_keys($GLOBALS['egw_info']['apps']) : (array)$_appname;
|
|
|
|
$types = $_type == 'all' ? array('import','export') : (array)$_type;
|
|
|
|
|
|
|
|
foreach($definitions as $appname => $_types) {
|
|
|
|
if(!in_array($appname, $appnames)) unset($definitions[$appname]);
|
|
|
|
}
|
|
|
|
foreach($definitions as $appname => $_types) {
|
|
|
|
$definitions[$appname] = array_intersect_key($definitions[$appname], array_flip($types));
|
|
|
|
}
|
|
|
|
return count($definitions[$appname]) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// egw_cache needs this public
|
|
|
|
public static function _has_definitions(Array $appnames, Array $types) {
|
|
|
|
$def = new importexport_definitions_bo(array('application'=>$appnames, 'type' => $types));
|
|
|
|
$list = array();
|
2011-02-08 17:01:55 +01:00
|
|
|
foreach((array)$def->get_definitions() as $id) {
|
2011-01-28 20:44:24 +01:00
|
|
|
$definition = new importexport_definition($id);
|
|
|
|
if($def->is_permitted($definition->get_record_array())) {
|
|
|
|
$list[$definition->application][$definition->type] = $id;
|
|
|
|
}
|
|
|
|
$definition = null;
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
2010-03-29 19:38:54 +02:00
|
|
|
} // end of importexport_helper_functions
|