egroupware/resources/inc/class.resources_import_csv.inc.php

229 lines
6.1 KiB
PHP
Raw Normal View History

2010-11-15 21:14:50 +01:00
<?php
/**
* eGroupWare import CSV plugin to import resources
*
* @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$
*/
/**
* class to import resources from CSV
*/
class resources_import_csv extends importexport_basic_import_csv {
2010-11-15 21:14:50 +01:00
/**
* conditions for actions
*
* @var array
*/
protected static $conditions = array( 'exists' );
/**
* imports entries according to given definition object.
* @param resource $_stream
* @param string $_charset
* @param definition $_definition
*/
public function init(importexport_definition $_definition ) {
2010-11-15 21:14:50 +01:00
// fetch the resource bo
$this->bo = new resources_bo();
2010-11-15 21:14:50 +01:00
// For adding ACLs
$this->acl_bo = CreateObject('resources.bo_acl',True);
// For checking categories
$this->start_time = time();
}
2010-11-15 21:14:50 +01:00
/**
* Import a single record
*
* You don't need to worry about mappings or translations, they've been done already.
* You do need to handle the conditions and the actions taken.
*
* Updates the count of actions taken
*
* @return boolean success
*/
protected function import_record(importexport_iface_egw_record &$record, &$import_csv)
{
// Check for a new category, it needs permissions set
$category = $GLOBALS['egw']->categories->read($record->cat_id);
if($category['last_mod'] >= $this->start_time) {
// New category. Give read & write permissions to the current user's default group
$this->acl_bo->set_rights($record['cat_id'],
array($GLOBALS['egw_info']['user']['account_primary_group']),
array($GLOBALS['egw_info']['user']['account_primary_group']),
array(),
array(),
array()
);
// Refresh ACL
//$GLOBALS['egw']->acl->read_repository();
2010-11-15 21:14:50 +01:00
}
//error_log(__METHOD__.__LINE__.array2string($_definition->plugin_options['conditions']));
if ($this->definition->plugin_options['conditions']) {
foreach ( $this->definition->plugin_options['conditions'] as $condition ) {
$results = array();
switch ( $condition['type'] ) {
// exists
case 'exists' :
if($record->$condition['string']) {
$results = $this->bo->so->search(
array( $condition['string'] => $record->$condition['string']),
False
);
}
if ( is_array( $results ) && count( array_keys( $results )) >= 1) {
// apply action to all contacts matching this exists condition
$action = $condition['true'];
foreach ( (array)$results as $resource ) {
$record->res_id = $resource['res_id'];
if ( $_definition->plugin_options['update_cats'] == 'add' ) {
if ( !is_array( $resource['cat_id'] ) ) $resource['cat_id'] = explode( ',', $resource['cat_id'] );
if ( !is_array( $record->cat_id ) ) $record->cat_id = explode( ',', $record->cat_id );
$record->cat_id = implode( ',', array_unique( array_merge( $record->cat_id, $resource['cat_id'] ) ) );
2010-11-15 21:14:50 +01:00
}
$success = $this->action( $action['action'], $record->get_record_array(), $import_csv->get_current_position() );
2010-11-15 21:14:50 +01:00
}
} else {
$action = $condition['false'];
$success = ($this->action( $action['action'], $record->get_record_array(), $import_csv->get_current_position() ));
}
break;
// not supported action
default :
die('condition / action not supported!!!');
break;
2010-11-15 21:14:50 +01:00
}
if ($action['last']) break;
2010-11-15 21:14:50 +01:00
}
} else {
// unconditional insert
$success = $this->action( 'insert', $record->get_record_array(), $import_csv->get_current_position() );
2010-11-15 21:14:50 +01:00
}
return $success;
2010-11-15 21:14:50 +01:00
}
/**
* perform the required action
*
* @param int $_action one of $this->actions
* @param array $_data contact data for the action
* @return bool success or not
*/
protected function action ( $_action, Array $_data, $record_num = 0 ) {
2010-11-15 21:14:50 +01:00
switch ($_action) {
case 'none' :
return true;
case 'update' :
// Only update if there are changes
$old = $this->bo->read($_data['res_id']);
// Merge to deal with fields not in import record
$_data = array_merge($old, $_data);
// Fall through
case 'insert' :
if($_action == 'insert') {
// Backend doesn't like inserting with ID specified, it can overwrite
unset($_data['res_id']);
}
if ( $this->dry_run ) {
//print_r($_data);
$this->results[$_action]++;
return true;
} else {
$result = $this->bo->save( $_data );
if($result && !is_numeric($result)) {
2010-11-15 21:14:50 +01:00
$this->errors[$record_num] = $result;
return false;
} else {
$this->results[$_action]++;
return true;
}
}
default:
throw new egw_exception('Unsupported action');
}
}
/**
* returns translated name of plugin
*
* @return string name
*/
public static function get_name() {
return lang('Resources CSV import');
}
/**
* returns translated (user) description of plugin
*
* @return string descriprion
*/
public static function get_description() {
return lang("Imports a list of resources from a CSV file.");
}
/**
* retruns file suffix(s) plugin can handle (e.g. csv)
*
* @return string suffix (comma seperated)
*/
public static function get_filesuffix() {
return 'csv';
}
/**
* return etemplate components for options.
* @abstract We can't deal with etemplate objects here, as an uietemplate
* objects itself are scipt orientated and not "dialog objects"
*
* @return array (
* name => string,
* content => array,
* sel_options => array,
* preserv => array,
* )
*/
public function get_options_etpl() {
// lets do it!
}
/**
* returns etemplate name for slectors of this plugin
*
* @return string etemplate name
*/
public function get_selectors_etpl() {
// lets do it!
}
2011-12-15 17:24:47 +01:00
/**
* Returns warnings that were encountered during importing
* Maximum of one warning message per record, but you can append if you need to
*
* @return Array (
* record_# => warning message
* )
*/
public function get_warnings() {
return $this->warnings;
}
}
2010-11-15 21:14:50 +01:00
?>