Add accessory_of field to import/export & update import plugin

This commit is contained in:
Nathan Gray 2012-12-17 19:03:10 +00:00
parent 0791bbb9f1
commit 72b30aaf93
6 changed files with 112 additions and 234 deletions

View File

@ -21,6 +21,14 @@ class resources_egw_record implements importexport_iface_egw_record
private $identifier = '';
private $record = array();
static $types = array(
'select-account'=> array('creator', 'modifier'),
'date-time' => array('modified', 'created'),
'select-cat' => array('cat_id'),
'select-bool' => array('bookable'),
// Links is field name => app name
'links' => array('accessory_of' => 'resources')
);
/**
* constructor
* reads record from backend if identifier is given.

View File

@ -55,7 +55,7 @@ class resources_export_csv implements importexport_iface_export_plugin {
break;
}
}
$types = importexport_export_csv::$types;
$types = resources_egw_record::$types;
$types['select-bool'] = array('bookable');
foreach ($selection as $record) {

View File

@ -15,36 +15,8 @@
/**
* class to import resources from CSV
*/
class resources_import_csv implements importexport_iface_import_plugin {
class resources_import_csv extends importexport_basic_import_csv {
private static $plugin_options = array(
'fieldsep', // char
'charset', // string
'contact_owner', // int
'update_cats', // string {override|add} overides record
// with cat(s) from csv OR add the cat from
// csv file to exeisting cat(s) of record
'num_header_lines', // int number of header lines
'field_conversion', // array( $csv_col_num => conversion)
'field_mapping', // array( $csv_col_num => adb_filed)
'conditions', /* => array containing condition arrays:
'type' => exists, // exists
'string' => '#kundennummer',
'true' => array(
'action' => update,
'last' => true,
),
'false' => array(
'action' => insert,
'last' => true,
),*/
);
/**
* actions wich could be done to data entries
*/
protected static $actions = array( 'none', 'update', 'insert', 'delete', );
/**
* conditions for actions
@ -53,66 +25,13 @@ class resources_import_csv implements importexport_iface_import_plugin {
*/
protected static $conditions = array( 'exists' );
/**
* @var definition
*/
private $definition;
/**
* @var bo
*/
private $bo;
/**
* @var bool
*/
private $dry_run = false;
/**
* @var bool is current user admin?
*/
private $is_admin = false;
/**
* @var int
*/
private $user = null;
/**
* List of import warnings
*/
protected $warnings = array();
/**
* List of import errors
*/
protected $errors = array();
/**
* List of actions, and how many times that action was taken
*/
protected $results = array();
/**
* imports entries according to given definition object.
* @param resource $_stream
* @param string $_charset
* @param definition $_definition
*/
public function import( $_stream, importexport_definition $_definition ) {
$import_csv = new importexport_import_csv( $_stream, array(
'fieldsep' => $_definition->plugin_options['fieldsep'],
'charset' => $_definition->plugin_options['charset'],
));
$this->definition = $_definition;
// user, is admin ?
$this->is_admin = isset( $GLOBALS['egw_info']['user']['apps']['admin'] ) && $GLOBALS['egw_info']['user']['apps']['admin'];
$this->user = $GLOBALS['egw_info']['user']['account_id'];
// dry run?
$this->dry_run = isset( $_definition->plugin_options['dry_run'] ) ? $_definition->plugin_options['dry_run'] : false;
public function init(importexport_definition $_definition ) {
// fetch the resource bo
$this->bo = new resources_bo();
@ -120,45 +39,26 @@ class resources_import_csv implements importexport_iface_import_plugin {
// For adding ACLs
$this->acl_bo = CreateObject('resources.bo_acl',True);
// set FieldMapping.
$import_csv->mapping = $_definition->plugin_options['field_mapping'];
// set FieldConversion
$import_csv->conversion = $_definition->plugin_options['field_conversion'];
//check if file has a header lines
if ( isset( $_definition->plugin_options['num_header_lines'] ) && $_definition->plugin_options['num_header_lines'] > 0) {
$import_csv->skip_records($_definition->plugin_options['num_header_lines']);
} elseif(isset($_definition->plugin_options['has_header_line']) && $_definition->plugin_options['has_header_line']) {
// First method is preferred
$import_csv->skip_records(1);
// For checking categories
$this->start_time = time();
}
// Start counting successes
$count = 0;
$this->results = array();
// Failures
$this->errors = array();
$types = importexport_export_csv::$types;
$types['select-bool'] = array('bookable');
$lookups = array();
$start_time = time();
while ( $record = $import_csv->get_record() ) {
$success = false;
// don't import empty records
if( count( array_unique( $record ) ) < 2 ) continue;
// Automatically handle human friendly values
importexport_import_csv::convert($record, $types, 'resources', $lookups,($_definition->plugin_options['convert']?$_definition->plugin_options['convert']:0));
/**
* 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']);
$category = $GLOBALS['egw']->categories->read($record->cat_id);
if($category['last_mod'] >= $start_time) {
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']),
@ -171,25 +71,16 @@ class resources_import_csv implements importexport_iface_import_plugin {
//$GLOBALS['egw']->acl->read_repository();
}
//error_log(__METHOD__.__LINE__.array2string($_definition->plugin_options['conditions']));
$conditionexist=false;
if ( $_definition->plugin_options['conditions'] ) {
foreach ( $_definition->plugin_options['conditions'] as $condition ) {
switch ( $condition['type'] ) {
case 'exists' :
if ((isset($condition['true']['action'])&&!empty($condition['true']['action'])) ||
(isset($condition['false']['action'])&&!empty($condition['false']['action']))) $conditionexist=true;
}
}
}
if ($conditionexist) {
foreach ( $_definition->plugin_options['conditions'] as $condition ) {
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']]) {
if($record->$condition['string']) {
$results = $this->bo->so->search(
array( $condition['string'] => $record[$condition['string']]),
array( $condition['string'] => $record->$condition['string']),
False
);
}
@ -198,17 +89,17 @@ class resources_import_csv implements importexport_iface_import_plugin {
// apply action to all contacts matching this exists condition
$action = $condition['true'];
foreach ( (array)$results as $resource ) {
$record['res_id'] = $resource['res_id'];
$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'] ) ) );
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'] ) ) );
}
$success = $this->action( $action['action'], $record, $import_csv->get_current_position() );
$success = $this->action( $action['action'], $record->get_record_array(), $import_csv->get_current_position() );
}
} else {
$action = $condition['false'];
$success = ($this->action( $action['action'], $record, $import_csv->get_current_position() ));
$success = ($this->action( $action['action'], $record->get_record_array(), $import_csv->get_current_position() ));
}
break;
@ -221,11 +112,9 @@ class resources_import_csv implements importexport_iface_import_plugin {
}
} else {
// unconditional insert
$success = $this->action( 'insert', $record, $import_csv->get_current_position() );
$success = $this->action( 'insert', $record->get_record_array(), $import_csv->get_current_position() );
}
if($success) $count++;
}
return $count;
return $success;
}
/**
@ -235,7 +124,7 @@ class resources_import_csv implements importexport_iface_import_plugin {
* @param array $_data contact data for the action
* @return bool success or not
*/
private function action ( $_action, $_data, $record_num = 0 ) {
protected function action ( $_action, Array $_data, $record_num = 0 ) {
switch ($_action) {
case 'none' :
return true;
@ -258,7 +147,7 @@ class resources_import_csv implements importexport_iface_import_plugin {
return true;
} else {
$result = $this->bo->save( $_data );
if($result) {
if($result && !is_numeric($result)) {
$this->errors[$record_num] = $result;
return false;
} else {
@ -335,29 +224,5 @@ class resources_import_csv implements importexport_iface_import_plugin {
public function get_warnings() {
return $this->warnings;
}
/**
* Returns errors that were encountered during importing
* Maximum of one error message per record, but you can append if you need to
*
* @return Array (
* record_# => error message
* )
*/
public function get_errors() {
return $this->errors;
}
/**
* Returns a list of actions taken, and the number of records for that action.
* Actions are things like 'insert', 'update', 'delete', and may be different for each plugin.
*
* @return Array (
* action => record count
* )
*/
public function get_results() {
return $this->results;
}
} // end of iface_export_plugin
?>

View File

@ -30,6 +30,7 @@ class resources_wizard_export_csv extends importexport_wizard_basic_export_csv
'prize' => lang('Prize'),
'long_description' => lang('Long description'),
'inventory_number' => lang('inventory number'),
'accessory_of' => lang('Accessory of')
);
// Custom fields

View File

@ -40,6 +40,7 @@ class resources_wizard_import_csv extends importexport_wizard_basic_import_csv
'prize' => lang('Prize'),
'long_description' => lang('Long description'),
'inventory_number' => lang('inventory number'),
'accessory_of' => lang('Accessory of')
);
$custom = config::get_customfields('resources', true);

View File

@ -18,9 +18,7 @@
<entry type="array" name="mapping">
<entry type="string" name="res_id">Resource ID</entry>
<entry type="string" name="name">Name</entry>
<entry type="string" name="inventory_number">Inventory number</entry>
<entry type="string" name="short_description">Short description</entry>
<entry type="string" name="long_description">Long description</entry>
<entry type="string" name="cat_id">Category</entry>
<entry type="string" name="quantity">Quantity</entry>
<entry type="string" name="useable">Usable</entry>
@ -29,6 +27,9 @@
<entry type="string" name="bookable">Bookable</entry>
<entry type="string" name="buyable">Buyable</entry>
<entry type="string" name="prize">Prize</entry>
<entry type="string" name="long_description">Long description</entry>
<entry type="string" name="inventory_number">Inventory number</entry>
<entry type="string" name="accessory_of">Accessory of</entry>
<entry type="string" name="all_custom_fields">resources</entry>
</entry>
<entry type="string" name="delimiter">;</entry>
@ -36,7 +37,7 @@
<entry type="string" name="begin_with_fieldnames">label</entry>
<entry type="string" name="convert">1</entry>
</entry>
<entry type="string" name="modified">2012-10-10 12:43:09</entry>
<entry type="string" name="modified">2012-12-17 11:56:59</entry>
</entry>
<entry type="array" name="import-resources">
<entry type="string" name="name">import-resources</entry>
@ -54,32 +55,34 @@
<entry type="array" name="csv_fields">
<entry type="string" name="0">Resource ID</entry>
<entry type="string" name="1">Name</entry>
<entry type="string" name="2">Inventory number</entry>
<entry type="string" name="3">Short description</entry>
<entry type="string" name="4">Long description</entry>
<entry type="string" name="5">Category</entry>
<entry type="string" name="6">Quantity</entry>
<entry type="string" name="7">Usable</entry>
<entry type="string" name="8">Location</entry>
<entry type="string" name="9">Storage</entry>
<entry type="string" name="10">Bookable</entry>
<entry type="string" name="11">Buyable</entry>
<entry type="string" name="12">Prize</entry>
<entry type="string" name="2">Short description</entry>
<entry type="string" name="3">Category</entry>
<entry type="string" name="4">Quantity</entry>
<entry type="string" name="5">Usable</entry>
<entry type="string" name="6">Location</entry>
<entry type="string" name="7">Storage</entry>
<entry type="string" name="8">Bookable</entry>
<entry type="string" name="9">Buyable</entry>
<entry type="string" name="10">Prize</entry>
<entry type="string" name="11">Long description</entry>
<entry type="string" name="12">Inventory number</entry>
<entry type="string" name="13">Accessory of</entry>
</entry>
<entry type="array" name="field_mapping">
<entry type="string" name="0">res_id</entry>
<entry type="string" name="1">name</entry>
<entry type="string" name="2">inventory_number</entry>
<entry type="string" name="3">short_description</entry>
<entry type="string" name="4">long_description</entry>
<entry type="string" name="5">cat_id</entry>
<entry type="string" name="6">quantity</entry>
<entry type="string" name="7">useable</entry>
<entry type="string" name="8">location</entry>
<entry type="string" name="9">storage_info</entry>
<entry type="string" name="10">bookable</entry>
<entry type="string" name="11">buyable</entry>
<entry type="string" name="12">prize</entry>
<entry type="string" name="2">short_description</entry>
<entry type="string" name="3">cat_id</entry>
<entry type="string" name="4">quantity</entry>
<entry type="string" name="5">useable</entry>
<entry type="string" name="6">location</entry>
<entry type="string" name="7">storage_info</entry>
<entry type="string" name="8">bookable</entry>
<entry type="string" name="9">buyable</entry>
<entry type="string" name="10">prize</entry>
<entry type="string" name="11">long_description</entry>
<entry type="string" name="12">inventory_number</entry>
<entry type="string" name="13">accessory_of</entry>
</entry>
<entry type="string" name="field_conversion"/>
<entry type="array" name="conditions">
@ -98,7 +101,7 @@
</entry>
<entry type="string" name="convert">1</entry>
</entry>
<entry type="string" name="modified">2012-10-10 12:55:25</entry>
<entry type="string" name="modified">2012-12-17 11:58:15</entry>
</entry>
</entry>
</entry>