@@ -98,22 +98,22 @@ discard block |
||
| 98 | 98 | * @param array _options options for specific backends |
| 99 | 99 | * @return bool |
| 100 | 100 | */ |
| 101 | - public function __construct( $_stream, array $_options ) { |
|
| 101 | + public function __construct($_stream, array $_options) { |
|
| 102 | 102 | if (!is_object($GLOBALS['egw']->translation)) { |
| 103 | 103 | $GLOBALS['egw']->translation = new Api\Translation(); |
| 104 | 104 | } |
| 105 | 105 | $this->translation = &$GLOBALS['egw']->translation; |
| 106 | 106 | $this->handle = $_stream; |
| 107 | - if($_options['charset'] == 'user') $_options['charset'] = $GLOBALS['egw_info']['user']['preferences']['common']['csv_charset']; |
|
| 107 | + if ($_options['charset'] == 'user') $_options['charset'] = $GLOBALS['egw_info']['user']['preferences']['common']['csv_charset']; |
|
| 108 | 108 | $this->csv_charset = $_options['charset'] ? $_options['charset'] : 'utf-8'; |
| 109 | - if ( !empty( $_options ) ) { |
|
| 110 | - $this->csv_options = array_merge( $this->csv_options, $_options ); |
|
| 109 | + if (!empty($_options)) { |
|
| 110 | + $this->csv_options = array_merge($this->csv_options, $_options); |
|
| 111 | 111 | } |
| 112 | 112 | //error_log(__METHOD__.__LINE__.array2string($_options['appname'])); |
| 113 | - if(!Api\Storage\Merge::is_export_limit_excepted()) { |
|
| 113 | + if (!Api\Storage\Merge::is_export_limit_excepted()) { |
|
| 114 | 114 | $this->export_limit = Api\Storage\Merge::getExportLimit($_options['appname']); |
| 115 | 115 | //error_log(__METHOD__.__LINE__.' app:'.$_options['appname'].' limit:'.$this->export_limit); |
| 116 | - if($this->export_limit == 'no') throw new Api\Exception\NoPermission\Admin('Export disabled'); |
|
| 116 | + if ($this->export_limit == 'no') throw new Api\Exception\NoPermission\Admin('Export disabled'); |
|
| 117 | 117 | } |
| 118 | 118 | } |
| 119 | 119 | |
@@ -122,15 +122,15 @@ discard block |
||
| 122 | 122 | * |
| 123 | 123 | * @param array $_mapping egw_field_name => csv_field_name |
| 124 | 124 | */ |
| 125 | - public function set_mapping( array &$_mapping) { |
|
| 125 | + public function set_mapping(array &$_mapping) { |
|
| 126 | 126 | if ($this->num_of_records > 0) { |
| 127 | 127 | throw new Exception('Error: Field mapping can\'t be set during ongoing export!'); |
| 128 | 128 | } |
| 129 | - if($_mapping['all_custom_fields']) { |
|
| 129 | + if ($_mapping['all_custom_fields']) { |
|
| 130 | 130 | // Field value is the appname, so we can pull the fields |
| 131 | 131 | $custom = Api\Storage\Customfields::get($_mapping['all_custom_fields']); |
| 132 | 132 | unset($_mapping['all_custom_fields']); |
| 133 | - foreach($custom as $field => $info) { |
|
| 133 | + foreach ($custom as $field => $info) { |
|
| 134 | 134 | $_mapping['#'.$field] = $this->csv_options['begin_with_fieldnames'] == 'label' ? $info['label'] : $field; |
| 135 | 135 | } |
| 136 | 136 | } |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | * |
| 144 | 144 | * @param array $_conversion |
| 145 | 145 | */ |
| 146 | - public function set_conversion( array $_conversion) { |
|
| 146 | + public function set_conversion(array $_conversion) { |
|
| 147 | 147 | $this->conversion = $_conversion; |
| 148 | 148 | } |
| 149 | 149 | |
@@ -153,59 +153,59 @@ discard block |
||
| 153 | 153 | * @param importexport_iface_egw_record record |
| 154 | 154 | * @return bool |
| 155 | 155 | */ |
| 156 | - public function export_record( importexport_iface_egw_record $_record ) { |
|
| 156 | + public function export_record(importexport_iface_egw_record $_record) { |
|
| 157 | 157 | $this->record = $_record; |
| 158 | 158 | $this->record_array = $_record->get_record_array(); |
| 159 | 159 | |
| 160 | 160 | // begin with fieldnames ? |
| 161 | - if ($this->num_of_records == 0 && $this->csv_options['begin_with_fieldnames'] ) { |
|
| 162 | - if($this->csv_options['begin_with_fieldnames'] == 'label') { |
|
| 161 | + if ($this->num_of_records == 0 && $this->csv_options['begin_with_fieldnames']) { |
|
| 162 | + if ($this->csv_options['begin_with_fieldnames'] == 'label') { |
|
| 163 | 163 | // Load translations for app |
| 164 | 164 | list($appname, $part2) = explode('_', get_class($_record)); |
| 165 | - if(!$GLOBALS['egw_info']['apps'][$appname]) $appname .= $part2; // Handle apps with _ in the name |
|
| 165 | + if (!$GLOBALS['egw_info']['apps'][$appname]) $appname .= $part2; // Handle apps with _ in the name |
|
| 166 | 166 | |
| 167 | 167 | // Get translations from wizard, if possible |
| 168 | - if(!$this->csv_options['no_header_translation']) |
|
| 168 | + if (!$this->csv_options['no_header_translation']) |
|
| 169 | 169 | { |
| 170 | 170 | $backtrace = debug_backtrace(); |
| 171 | 171 | $plugin = $backtrace[1]['class']; |
| 172 | - $wizard_name = $appname . '_wizard_' . str_replace($appname . '_', '', $plugin); |
|
| 172 | + $wizard_name = $appname.'_wizard_'.str_replace($appname.'_', '', $plugin); |
|
| 173 | 173 | try { |
| 174 | 174 | $wizard = new $wizard_name; |
| 175 | 175 | $fields = $wizard->get_export_fields(); |
| 176 | - foreach($this->mapping as $field => &$label) |
|
| 176 | + foreach ($this->mapping as $field => &$label) |
|
| 177 | 177 | { |
| 178 | - if($fields[$field]) |
|
| 178 | + if ($fields[$field]) |
|
| 179 | 179 | { |
| 180 | 180 | $label = $label != $fields[$field] ? $fields[$field] : lang($label); |
| 181 | 181 | } |
| 182 | 182 | // Make sure no * |
| 183 | - if(substr($label,-1) == '*') $label = substr($label,0,-1); |
|
| 183 | + if (substr($label, -1) == '*') $label = substr($label, 0, -1); |
|
| 184 | 184 | } |
| 185 | 185 | } catch (Exception $e) { |
| 186 | 186 | Api\Translation::add_app($appname); |
| 187 | - foreach($this->mapping as $field => &$label) { |
|
| 187 | + foreach ($this->mapping as $field => &$label) { |
|
| 188 | 188 | $label = lang($label); |
| 189 | 189 | } |
| 190 | 190 | } |
| 191 | 191 | } |
| 192 | 192 | } |
| 193 | - $mapping = ! empty( $this->mapping ) ? $this->mapping : array_keys ( $this->record_array ); |
|
| 194 | - self::fputcsv( $this->handle ,$mapping ,$this->csv_options['delimiter'], $this->csv_options['enclosure'] ); |
|
| 193 | + $mapping = !empty($this->mapping) ? $this->mapping : array_keys($this->record_array); |
|
| 194 | + self::fputcsv($this->handle, $mapping, $this->csv_options['delimiter'], $this->csv_options['enclosure']); |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | // Check for limit |
| 198 | - if($this->export_limit && $this->num_of_records >= $this->export_limit) { |
|
| 198 | + if ($this->export_limit && $this->num_of_records >= $this->export_limit) { |
|
| 199 | 199 | return; |
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | // do conversions |
| 203 | - if ( !empty( $this->conversion )) { |
|
| 204 | - $this->record_array = importexport_helper_functions::conversion( $this->record_array, $this->conversion ); |
|
| 203 | + if (!empty($this->conversion)) { |
|
| 204 | + $this->record_array = importexport_helper_functions::conversion($this->record_array, $this->conversion); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | // do fieldmapping |
| 208 | - if ( !empty( $this->mapping ) ) { |
|
| 208 | + if (!empty($this->mapping)) { |
|
| 209 | 209 | $record_data = $this->record_array; |
| 210 | 210 | $this->record_array = array(); |
| 211 | 211 | foreach ($this->mapping as $egw_field => $csv_field) { |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | } |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | - self::fputcsv( $this->handle, $this->record_array, $this->csv_options['delimiter'], $this->csv_options['enclosure'] ); |
|
| 216 | + self::fputcsv($this->handle, $this->record_array, $this->csv_options['delimiter'], $this->csv_options['enclosure']); |
|
| 217 | 217 | $this->num_of_records++; |
| 218 | 218 | } |
| 219 | 219 | |
@@ -237,13 +237,13 @@ discard block |
||
| 237 | 237 | * @return Array of fields to be added to list of fields needing conversion |
| 238 | 238 | */ |
| 239 | 239 | public static function convert_parse_custom_fields($appname, &$selects = array(), &$links = array(), &$methods = array()) { |
| 240 | - if(!$appname) return; |
|
| 240 | + if (!$appname) return; |
|
| 241 | 241 | |
| 242 | 242 | $fields = array(); |
| 243 | 243 | $custom = Api\Storage\Customfields::get($appname); |
| 244 | - foreach($custom as $name => $c_field) { |
|
| 245 | - $name = '#' . $name; |
|
| 246 | - switch($c_field['type']) { |
|
| 244 | + foreach ($custom as $name => $c_field) { |
|
| 245 | + $name = '#'.$name; |
|
| 246 | + switch ($c_field['type']) { |
|
| 247 | 247 | case 'date': |
| 248 | 248 | $fields['date'][] = $name; |
| 249 | 249 | break; |
@@ -254,7 +254,7 @@ discard block |
||
| 254 | 254 | $fields['select-account'][] = $name; |
| 255 | 255 | break; |
| 256 | 256 | case 'ajax_select': |
| 257 | - if($c_field['values']['get_title']) { |
|
| 257 | + if ($c_field['values']['get_title']) { |
|
| 258 | 258 | $methods[$name] = $c_field['values']['get_title']; |
| 259 | 259 | break; |
| 260 | 260 | } |
@@ -268,8 +268,8 @@ discard block |
||
| 268 | 268 | $selects[$name] = $c_field['values']; |
| 269 | 269 | break; |
| 270 | 270 | default: |
| 271 | - list($type) = explode('-',$c_field['type'],2); |
|
| 272 | - if(in_array($type, array_keys($GLOBALS['egw_info']['apps']))) { |
|
| 271 | + list($type) = explode('-', $c_field['type'], 2); |
|
| 272 | + if (in_array($type, array_keys($GLOBALS['egw_info']['apps']))) { |
|
| 273 | 273 | $fields['links'][] = $name; |
| 274 | 274 | $links[$name] = $c_field['type']; |
| 275 | 275 | } |
@@ -288,18 +288,18 @@ discard block |
||
| 288 | 288 | * @parem fields List of field types => field names to be converted |
| 289 | 289 | * @param appname Current appname if you want to do custom fields too |
| 290 | 290 | */ |
| 291 | - public static function convert(importexport_iface_egw_record &$record, Array $fields = array(), $appname = null, $selects = array()) { |
|
| 292 | - if($appname) { |
|
| 293 | - if(!self::$cf_parse_cache[$appname]) { |
|
| 291 | + public static function convert(importexport_iface_egw_record&$record, Array $fields = array(), $appname = null, $selects = array()) { |
|
| 292 | + if ($appname) { |
|
| 293 | + if (!self::$cf_parse_cache[$appname]) { |
|
| 294 | 294 | $c_fields = self::convert_parse_custom_fields($appname, $selects, $links, $methods); |
| 295 | 295 | self::$cf_parse_cache[$appname] = array($c_fields, $selects, $links, $methods); |
| 296 | 296 | } |
| 297 | 297 | list($c_fields, $c_selects, $links, $methods) = self::$cf_parse_cache[$appname]; |
| 298 | 298 | |
| 299 | 299 | // Add in any fields that are keys to another app |
| 300 | - foreach((array)$fields['links'] as $link_field => $app) |
|
| 300 | + foreach ((array)$fields['links'] as $link_field => $app) |
|
| 301 | 301 | { |
| 302 | - if(is_numeric($link_field)) continue; |
|
| 302 | + if (is_numeric($link_field)) continue; |
|
| 303 | 303 | $links[$link_field] = $app; |
| 304 | 304 | // Set it as a normal link field |
| 305 | 305 | $fields['links'][] = $link_field; |
@@ -307,8 +307,8 @@ discard block |
||
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | // Not quite a recursive merge, since only one level |
| 310 | - foreach($fields as $type => &$list) { |
|
| 311 | - if($c_fields[$type]) { |
|
| 310 | + foreach ($fields as $type => &$list) { |
|
| 311 | + if ($c_fields[$type]) { |
|
| 312 | 312 | $list = array_merge($c_fields[$type], $list); |
| 313 | 313 | unset($c_fields[$type]); |
| 314 | 314 | } |
@@ -316,12 +316,12 @@ discard block |
||
| 316 | 316 | $fields += $c_fields; |
| 317 | 317 | $selects += $c_selects; |
| 318 | 318 | } |
| 319 | - foreach((array)$fields['select'] as $name) { |
|
| 320 | - if($record->$name != null && is_array($selects) && $selects[$name]) { |
|
| 319 | + foreach ((array)$fields['select'] as $name) { |
|
| 320 | + if ($record->$name != null && is_array($selects) && $selects[$name]) { |
|
| 321 | 321 | $record->$name = is_string($record->$name) ? explode(',', $record->$name) : $record->$name; |
| 322 | - if(is_array($record->$name)) { |
|
| 322 | + if (is_array($record->$name)) { |
|
| 323 | 323 | $names = array(); |
| 324 | - foreach($record->$name as $_name) { |
|
| 324 | + foreach ($record->$name as $_name) { |
|
| 325 | 325 | $names[] = lang($selects[$name][$_name]); |
| 326 | 326 | } |
| 327 | 327 | $record->$name = implode(', ', $names); |
@@ -334,18 +334,18 @@ discard block |
||
| 334 | 334 | $record->$name = ''; |
| 335 | 335 | } |
| 336 | 336 | } |
| 337 | - foreach((array)$fields['links'] as $name) { |
|
| 338 | - if($record->$name) { |
|
| 339 | - if(is_numeric($record->$name) && !$links[$name]) { |
|
| 337 | + foreach ((array)$fields['links'] as $name) { |
|
| 338 | + if ($record->$name) { |
|
| 339 | + if (is_numeric($record->$name) && !$links[$name]) { |
|
| 340 | 340 | $link = Link::get_link($record->$name); |
| 341 | 341 | $links[$name] = ($link['link_app1'] == $appname ? $link['link_app2'] : $link['link_app1']); |
| 342 | 342 | $record->$name = ($link['link_app1'] == $appname ? $link['link_id2'] : $link['link_id1']); |
| 343 | 343 | } |
| 344 | - if($links[$name]) |
|
| 344 | + if ($links[$name]) |
|
| 345 | 345 | { |
| 346 | 346 | $record->$name = Link::title($links[$name], $record->$name); |
| 347 | 347 | } |
| 348 | - else if ( is_array($record->$name) && $record->$name['app'] && $record->$name['id']) |
|
| 348 | + else if (is_array($record->$name) && $record->$name['app'] && $record->$name['id']) |
|
| 349 | 349 | { |
| 350 | 350 | $record->$name = Link::title($record->$name['app'], $record->$name['id']); |
| 351 | 351 | } |
@@ -355,12 +355,12 @@ discard block |
||
| 355 | 355 | $record->$name = ''; |
| 356 | 356 | } |
| 357 | 357 | } |
| 358 | - foreach((array)$fields['select-account'] as $name) { |
|
| 358 | + foreach ((array)$fields['select-account'] as $name) { |
|
| 359 | 359 | // Compare against null to deal with empty arrays |
| 360 | 360 | if ($record->$name !== null) { |
| 361 | - if(is_array($record->$name)) { |
|
| 361 | + if (is_array($record->$name)) { |
|
| 362 | 362 | $names = array(); |
| 363 | - foreach($record->$name as $_name) { |
|
| 363 | + foreach ($record->$name as $_name) { |
|
| 364 | 364 | $names[] = Api\Accounts::username($_name); |
| 365 | 365 | } |
| 366 | 366 | $record->$name = implode(', ', $names); |
@@ -373,64 +373,64 @@ discard block |
||
| 373 | 373 | $record->$name = ''; |
| 374 | 374 | } |
| 375 | 375 | } |
| 376 | - foreach((array)$fields['select-bool'] as $name) { |
|
| 377 | - if($record->$name !== null) { |
|
| 376 | + foreach ((array)$fields['select-bool'] as $name) { |
|
| 377 | + if ($record->$name !== null) { |
|
| 378 | 378 | $record->$name = $record->$name ? lang('Yes') : lang('No'); |
| 379 | 379 | } |
| 380 | 380 | } |
| 381 | - foreach((array)$fields['date-time'] as $name) { |
|
| 381 | + foreach ((array)$fields['date-time'] as $name) { |
|
| 382 | 382 | //if ($record->$name) $record->$name = date('Y-m-d H:i:s',$record->$name); // Standard date format |
| 383 | 383 | if ($record->$name && !is_numeric($record->$name)) $record->$name = strtotime($record->$name); // Custom fields stored as string |
| 384 | - if ($record->$name && is_numeric($record->$name)) $record->$name = date($GLOBALS['egw_info']['user']['preferences']['common']['dateformat'] . ' '. |
|
| 385 | - ($GLOBALS['egw_info']['user']['preferences']['common']['timeformat'] == '24' ? 'H:i:s' : 'h:i:s a'),$record->$name); // User date format |
|
| 384 | + if ($record->$name && is_numeric($record->$name)) $record->$name = date($GLOBALS['egw_info']['user']['preferences']['common']['dateformat'].' '. |
|
| 385 | + ($GLOBALS['egw_info']['user']['preferences']['common']['timeformat'] == '24' ? 'H:i:s' : 'h:i:s a'), $record->$name); // User date format |
|
| 386 | 386 | if (!$record->$name) $record->$name = ''; |
| 387 | 387 | } |
| 388 | - foreach((array)$fields['date'] as $name) { |
|
| 388 | + foreach ((array)$fields['date'] as $name) { |
|
| 389 | 389 | //if ($record->$name) $record->$name = date('Y-m-d',$record->$name); // Standard date format |
| 390 | 390 | if ($record->$name && !is_numeric($record->$name)) $record->$name = strtotime($record->$name); // Custom fields stored as string |
| 391 | 391 | if ($record->$name && is_numeric($record->$name)) $record->$name = date($GLOBALS['egw_info']['user']['preferences']['common']['dateformat'], $record->$name); // User date format |
| 392 | 392 | if (!$record->$name) $record->$name = ''; |
| 393 | 393 | } |
| 394 | - foreach((array)$fields['float'] as $name) |
|
| 394 | + foreach ((array)$fields['float'] as $name) |
|
| 395 | 395 | { |
| 396 | - static $dec_separator,$thousands_separator; |
|
| 396 | + static $dec_separator, $thousands_separator; |
|
| 397 | 397 | if (is_null($dec_separator)) |
| 398 | 398 | { |
| 399 | 399 | $dec_separator = $GLOBALS['egw_info']['user']['preferences']['common']['number_format'][0]; |
| 400 | 400 | if (empty($dec_separator)) $dec_separator = '.'; |
| 401 | 401 | $thousands_separator = $GLOBALS['egw_info']['user']['preferences']['common']['number_format'][1]; |
| 402 | 402 | } |
| 403 | - if($record->$name && (string)$record->$name != '') |
|
| 403 | + if ($record->$name && (string)$record->$name != '') |
|
| 404 | 404 | { |
| 405 | - if(!is_numeric($record->$name)) |
|
| 405 | + if (!is_numeric($record->$name)) |
|
| 406 | 406 | { |
| 407 | 407 | $record->$name = floatval(str_replace($dec_separator, '.', preg_replace('/[^\d'.preg_quote($dec_separator).']/', '', $record->$name))); |
| 408 | 408 | } |
| 409 | - $record->$name = number_format(str_replace(' ','',$record->$name), 2, |
|
| 410 | - $dec_separator,$thousands_separator |
|
| 409 | + $record->$name = number_format(str_replace(' ', '', $record->$name), 2, |
|
| 410 | + $dec_separator, $thousands_separator |
|
| 411 | 411 | ); |
| 412 | 412 | } |
| 413 | 413 | } |
| 414 | 414 | |
| 415 | 415 | // Some custom methods for conversion |
| 416 | - foreach((array)$methods as $name => $method) { |
|
| 417 | - if($record->$name) $record->$name = ExecMethod($method, $record->$name); |
|
| 416 | + foreach ((array)$methods as $name => $method) { |
|
| 417 | + if ($record->$name) $record->$name = ExecMethod($method, $record->$name); |
|
| 418 | 418 | } |
| 419 | 419 | |
| 420 | 420 | static $cat_object; |
| 421 | - if(is_null($cat_object)) $cat_object = new Api\Categories(false,$appname); |
|
| 422 | - foreach((array)$fields['select-cat'] as $name) { |
|
| 423 | - if($record->$name) { |
|
| 421 | + if (is_null($cat_object)) $cat_object = new Api\Categories(false, $appname); |
|
| 422 | + foreach ((array)$fields['select-cat'] as $name) { |
|
| 423 | + if ($record->$name) { |
|
| 424 | 424 | $cats = array(); |
| 425 | 425 | $ids = is_array($record->$name) ? $record->$name : explode(',', $record->$name); |
| 426 | - foreach($ids as $n => $cat_id) { |
|
| 426 | + foreach ($ids as $n => $cat_id) { |
|
| 427 | 427 | |
| 428 | - if ($cat_id && $cat_object->check_perms(Acl::READ,$cat_id)) |
|
| 428 | + if ($cat_id && $cat_object->check_perms(Acl::READ, $cat_id)) |
|
| 429 | 429 | { |
| 430 | 430 | $cats[] = $cat_object->id2name($cat_id); |
| 431 | 431 | } |
| 432 | 432 | } |
| 433 | - $record->$name = implode(', ',$cats); |
|
| 433 | + $record->$name = implode(', ', $cats); |
|
| 434 | 434 | } |
| 435 | 435 | else |
| 436 | 436 | { |
@@ -457,18 +457,18 @@ discard block |
||
| 457 | 457 | * @param char $delimiter |
| 458 | 458 | * @param char $enclosure |
| 459 | 459 | */ |
| 460 | - protected function fputcsv($filePointer, Array $dataArray, $delimiter, $enclosure){ |
|
| 460 | + protected function fputcsv($filePointer, Array $dataArray, $delimiter, $enclosure) { |
|
| 461 | 461 | $string = ""; |
| 462 | 462 | $writeDelimiter = false; |
| 463 | - foreach($dataArray as $dataElement) { |
|
| 464 | - if($writeDelimiter) $string .= $delimiter; |
|
| 465 | - $string .= $enclosure . str_replace(array("\r\n", '"'), array("\n",'""'), $dataElement) . $enclosure; |
|
| 463 | + foreach ($dataArray as $dataElement) { |
|
| 464 | + if ($writeDelimiter) $string .= $delimiter; |
|
| 465 | + $string .= $enclosure.str_replace(array("\r\n", '"'), array("\n", '""'), $dataElement).$enclosure; |
|
| 466 | 466 | $writeDelimiter = true; |
| 467 | 467 | } |
| 468 | 468 | $string .= "\n"; |
| 469 | 469 | |
| 470 | 470 | // do charset translation |
| 471 | - $string = $this->translation->convert( $string, $this->translation->charset(), $this->csv_charset ); |
|
| 471 | + $string = $this->translation->convert($string, $this->translation->charset(), $this->csv_charset); |
|
| 472 | 472 | |
| 473 | 473 | fwrite($filePointer, $string); |
| 474 | 474 | |
@@ -98,22 +98,32 @@ discard block |
||
| 98 | 98 | * @param array _options options for specific backends |
| 99 | 99 | * @return bool |
| 100 | 100 | */ |
| 101 | - public function __construct( $_stream, array $_options ) { |
|
| 102 | - if (!is_object($GLOBALS['egw']->translation)) { |
|
| 101 | + public function __construct( $_stream, array $_options ) |
|
| 102 | + { |
|
| 103 | + if (!is_object($GLOBALS['egw']->translation)) |
|
| 104 | + { |
|
| 103 | 105 | $GLOBALS['egw']->translation = new Api\Translation(); |
| 104 | 106 | } |
| 105 | 107 | $this->translation = &$GLOBALS['egw']->translation; |
| 106 | 108 | $this->handle = $_stream; |
| 107 | - if($_options['charset'] == 'user') $_options['charset'] = $GLOBALS['egw_info']['user']['preferences']['common']['csv_charset']; |
|
| 109 | + if($_options['charset'] == 'user') |
|
| 110 | + { |
|
| 111 | + $_options['charset'] = $GLOBALS['egw_info']['user']['preferences']['common']['csv_charset']; |
|
| 112 | + } |
|
| 108 | 113 | $this->csv_charset = $_options['charset'] ? $_options['charset'] : 'utf-8'; |
| 109 | - if ( !empty( $_options ) ) { |
|
| 114 | + if ( !empty( $_options ) ) |
|
| 115 | + { |
|
| 110 | 116 | $this->csv_options = array_merge( $this->csv_options, $_options ); |
| 111 | 117 | } |
| 112 | 118 | //error_log(__METHOD__.__LINE__.array2string($_options['appname'])); |
| 113 | - if(!Api\Storage\Merge::is_export_limit_excepted()) { |
|
| 119 | + if(!Api\Storage\Merge::is_export_limit_excepted()) |
|
| 120 | + { |
|
| 114 | 121 | $this->export_limit = Api\Storage\Merge::getExportLimit($_options['appname']); |
| 115 | 122 | //error_log(__METHOD__.__LINE__.' app:'.$_options['appname'].' limit:'.$this->export_limit); |
| 116 | - if($this->export_limit == 'no') throw new Api\Exception\NoPermission\Admin('Export disabled'); |
|
| 123 | + if($this->export_limit == 'no') |
|
| 124 | + { |
|
| 125 | + throw new Api\Exception\NoPermission\Admin('Export disabled'); |
|
| 126 | + } |
|
| 117 | 127 | } |
| 118 | 128 | } |
| 119 | 129 | |
@@ -122,15 +132,19 @@ discard block |
||
| 122 | 132 | * |
| 123 | 133 | * @param array $_mapping egw_field_name => csv_field_name |
| 124 | 134 | */ |
| 125 | - public function set_mapping( array &$_mapping) { |
|
| 126 | - if ($this->num_of_records > 0) { |
|
| 135 | + public function set_mapping( array &$_mapping) |
|
| 136 | + { |
|
| 137 | + if ($this->num_of_records > 0) |
|
| 138 | + { |
|
| 127 | 139 | throw new Exception('Error: Field mapping can\'t be set during ongoing export!'); |
| 128 | 140 | } |
| 129 | - if($_mapping['all_custom_fields']) { |
|
| 141 | + if($_mapping['all_custom_fields']) |
|
| 142 | + { |
|
| 130 | 143 | // Field value is the appname, so we can pull the fields |
| 131 | 144 | $custom = Api\Storage\Customfields::get($_mapping['all_custom_fields']); |
| 132 | 145 | unset($_mapping['all_custom_fields']); |
| 133 | - foreach($custom as $field => $info) { |
|
| 146 | + foreach($custom as $field => $info) |
|
| 147 | + { |
|
| 134 | 148 | $_mapping['#'.$field] = $this->csv_options['begin_with_fieldnames'] == 'label' ? $info['label'] : $field; |
| 135 | 149 | } |
| 136 | 150 | } |
@@ -143,7 +157,8 @@ discard block |
||
| 143 | 157 | * |
| 144 | 158 | * @param array $_conversion |
| 145 | 159 | */ |
| 146 | - public function set_conversion( array $_conversion) { |
|
| 160 | + public function set_conversion( array $_conversion) |
|
| 161 | + { |
|
| 147 | 162 | $this->conversion = $_conversion; |
| 148 | 163 | } |
| 149 | 164 | |
@@ -153,16 +168,23 @@ discard block |
||
| 153 | 168 | * @param importexport_iface_egw_record record |
| 154 | 169 | * @return bool |
| 155 | 170 | */ |
| 156 | - public function export_record( importexport_iface_egw_record $_record ) { |
|
| 171 | + public function export_record( importexport_iface_egw_record $_record ) |
|
| 172 | + { |
|
| 157 | 173 | $this->record = $_record; |
| 158 | 174 | $this->record_array = $_record->get_record_array(); |
| 159 | 175 | |
| 160 | 176 | // begin with fieldnames ? |
| 161 | - if ($this->num_of_records == 0 && $this->csv_options['begin_with_fieldnames'] ) { |
|
| 162 | - if($this->csv_options['begin_with_fieldnames'] == 'label') { |
|
| 177 | + if ($this->num_of_records == 0 && $this->csv_options['begin_with_fieldnames'] ) |
|
| 178 | + { |
|
| 179 | + if($this->csv_options['begin_with_fieldnames'] == 'label') |
|
| 180 | + { |
|
| 163 | 181 | // Load translations for app |
| 164 | 182 | list($appname, $part2) = explode('_', get_class($_record)); |
| 165 | - if(!$GLOBALS['egw_info']['apps'][$appname]) $appname .= $part2; // Handle apps with _ in the name |
|
| 183 | + if(!$GLOBALS['egw_info']['apps'][$appname]) |
|
| 184 | + { |
|
| 185 | + $appname .= $part2; |
|
| 186 | + } |
|
| 187 | + // Handle apps with _ in the name |
|
| 166 | 188 | |
| 167 | 189 | // Get translations from wizard, if possible |
| 168 | 190 | if(!$this->csv_options['no_header_translation']) |
@@ -180,11 +202,16 @@ discard block |
||
| 180 | 202 | $label = $label != $fields[$field] ? $fields[$field] : lang($label); |
| 181 | 203 | } |
| 182 | 204 | // Make sure no * |
| 183 | - if(substr($label,-1) == '*') $label = substr($label,0,-1); |
|
| 205 | + if(substr($label,-1) == '*') |
|
| 206 | + { |
|
| 207 | + $label = substr($label,0,-1); |
|
| 208 | + } |
|
| 184 | 209 | } |
| 185 | - } catch (Exception $e) { |
|
| 210 | + } |
|
| 211 | + catch (Exception $e) { |
|
| 186 | 212 | Api\Translation::add_app($appname); |
| 187 | - foreach($this->mapping as $field => &$label) { |
|
| 213 | + foreach($this->mapping as $field => &$label) |
|
| 214 | + { |
|
| 188 | 215 | $label = lang($label); |
| 189 | 216 | } |
| 190 | 217 | } |
@@ -195,20 +222,24 @@ discard block |
||
| 195 | 222 | } |
| 196 | 223 | |
| 197 | 224 | // Check for limit |
| 198 | - if($this->export_limit && $this->num_of_records >= $this->export_limit) { |
|
| 225 | + if($this->export_limit && $this->num_of_records >= $this->export_limit) |
|
| 226 | + { |
|
| 199 | 227 | return; |
| 200 | 228 | } |
| 201 | 229 | |
| 202 | 230 | // do conversions |
| 203 | - if ( !empty( $this->conversion )) { |
|
| 231 | + if ( !empty( $this->conversion )) |
|
| 232 | + { |
|
| 204 | 233 | $this->record_array = importexport_helper_functions::conversion( $this->record_array, $this->conversion ); |
| 205 | 234 | } |
| 206 | 235 | |
| 207 | 236 | // do fieldmapping |
| 208 | - if ( !empty( $this->mapping ) ) { |
|
| 237 | + if ( !empty( $this->mapping ) ) |
|
| 238 | + { |
|
| 209 | 239 | $record_data = $this->record_array; |
| 210 | 240 | $this->record_array = array(); |
| 211 | - foreach ($this->mapping as $egw_field => $csv_field) { |
|
| 241 | + foreach ($this->mapping as $egw_field => $csv_field) |
|
| 242 | + { |
|
| 212 | 243 | $this->record_array[$csv_field] = $record_data[$egw_field]; |
| 213 | 244 | } |
| 214 | 245 | } |
@@ -222,7 +253,8 @@ discard block |
||
| 222 | 253 | * |
| 223 | 254 | * @return int |
| 224 | 255 | */ |
| 225 | - public function get_num_of_records() { |
|
| 256 | + public function get_num_of_records() |
|
| 257 | + { |
|
| 226 | 258 | return $this->num_of_records; |
| 227 | 259 | } |
| 228 | 260 | |
@@ -236,14 +268,20 @@ discard block |
||
| 236 | 268 | * |
| 237 | 269 | * @return Array of fields to be added to list of fields needing conversion |
| 238 | 270 | */ |
| 239 | - public static function convert_parse_custom_fields($appname, &$selects = array(), &$links = array(), &$methods = array()) { |
|
| 240 | - if(!$appname) return; |
|
| 271 | + public static function convert_parse_custom_fields($appname, &$selects = array(), &$links = array(), &$methods = array()) |
|
| 272 | + { |
|
| 273 | + if(!$appname) |
|
| 274 | + { |
|
| 275 | + return; |
|
| 276 | + } |
|
| 241 | 277 | |
| 242 | 278 | $fields = array(); |
| 243 | 279 | $custom = Api\Storage\Customfields::get($appname); |
| 244 | - foreach($custom as $name => $c_field) { |
|
| 280 | + foreach($custom as $name => $c_field) |
|
| 281 | + { |
|
| 245 | 282 | $name = '#' . $name; |
| 246 | - switch($c_field['type']) { |
|
| 283 | + switch($c_field['type']) |
|
| 284 | + { |
|
| 247 | 285 | case 'date': |
| 248 | 286 | $fields['date'][] = $name; |
| 249 | 287 | break; |
@@ -254,7 +292,8 @@ discard block |
||
| 254 | 292 | $fields['select-account'][] = $name; |
| 255 | 293 | break; |
| 256 | 294 | case 'ajax_select': |
| 257 | - if($c_field['values']['get_title']) { |
|
| 295 | + if($c_field['values']['get_title']) |
|
| 296 | + { |
|
| 258 | 297 | $methods[$name] = $c_field['values']['get_title']; |
| 259 | 298 | break; |
| 260 | 299 | } |
@@ -269,7 +308,8 @@ discard block |
||
| 269 | 308 | break; |
| 270 | 309 | default: |
| 271 | 310 | list($type) = explode('-',$c_field['type'],2); |
| 272 | - if(in_array($type, array_keys($GLOBALS['egw_info']['apps']))) { |
|
| 311 | + if(in_array($type, array_keys($GLOBALS['egw_info']['apps']))) |
|
| 312 | + { |
|
| 273 | 313 | $fields['links'][] = $name; |
| 274 | 314 | $links[$name] = $c_field['type']; |
| 275 | 315 | } |
@@ -288,9 +328,12 @@ discard block |
||
| 288 | 328 | * @parem fields List of field types => field names to be converted |
| 289 | 329 | * @param appname Current appname if you want to do custom fields too |
| 290 | 330 | */ |
| 291 | - public static function convert(importexport_iface_egw_record &$record, Array $fields = array(), $appname = null, $selects = array()) { |
|
| 292 | - if($appname) { |
|
| 293 | - if(!self::$cf_parse_cache[$appname]) { |
|
| 331 | + public static function convert(importexport_iface_egw_record &$record, Array $fields = array(), $appname = null, $selects = array()) |
|
| 332 | + { |
|
| 333 | + if($appname) |
|
| 334 | + { |
|
| 335 | + if(!self::$cf_parse_cache[$appname]) |
|
| 336 | + { |
|
| 294 | 337 | $c_fields = self::convert_parse_custom_fields($appname, $selects, $links, $methods); |
| 295 | 338 | self::$cf_parse_cache[$appname] = array($c_fields, $selects, $links, $methods); |
| 296 | 339 | } |
@@ -299,7 +342,10 @@ discard block |
||
| 299 | 342 | // Add in any fields that are keys to another app |
| 300 | 343 | foreach((array)$fields['links'] as $link_field => $app) |
| 301 | 344 | { |
| 302 | - if(is_numeric($link_field)) continue; |
|
| 345 | + if(is_numeric($link_field)) |
|
| 346 | + { |
|
| 347 | + continue; |
|
| 348 | + } |
|
| 303 | 349 | $links[$link_field] = $app; |
| 304 | 350 | // Set it as a normal link field |
| 305 | 351 | $fields['links'][] = $link_field; |
@@ -307,8 +353,10 @@ discard block |
||
| 307 | 353 | } |
| 308 | 354 | |
| 309 | 355 | // Not quite a recursive merge, since only one level |
| 310 | - foreach($fields as $type => &$list) { |
|
| 311 | - if($c_fields[$type]) { |
|
| 356 | + foreach($fields as $type => &$list) |
|
| 357 | + { |
|
| 358 | + if($c_fields[$type]) |
|
| 359 | + { |
|
| 312 | 360 | $list = array_merge($c_fields[$type], $list); |
| 313 | 361 | unset($c_fields[$type]); |
| 314 | 362 | } |
@@ -316,16 +364,22 @@ discard block |
||
| 316 | 364 | $fields += $c_fields; |
| 317 | 365 | $selects += $c_selects; |
| 318 | 366 | } |
| 319 | - foreach((array)$fields['select'] as $name) { |
|
| 320 | - if($record->$name != null && is_array($selects) && $selects[$name]) { |
|
| 367 | + foreach((array)$fields['select'] as $name) |
|
| 368 | + { |
|
| 369 | + if($record->$name != null && is_array($selects) && $selects[$name]) |
|
| 370 | + { |
|
| 321 | 371 | $record->$name = is_string($record->$name) ? explode(',', $record->$name) : $record->$name; |
| 322 | - if(is_array($record->$name)) { |
|
| 372 | + if(is_array($record->$name)) |
|
| 373 | + { |
|
| 323 | 374 | $names = array(); |
| 324 | - foreach($record->$name as $_name) { |
|
| 375 | + foreach($record->$name as $_name) |
|
| 376 | + { |
|
| 325 | 377 | $names[] = lang($selects[$name][$_name]); |
| 326 | 378 | } |
| 327 | 379 | $record->$name = implode(', ', $names); |
| 328 | - } else { |
|
| 380 | + } |
|
| 381 | + else |
|
| 382 | + { |
|
| 329 | 383 | $record->$name = lang($selects[$name][$record->$name]); |
| 330 | 384 | } |
| 331 | 385 | } |
@@ -334,9 +388,12 @@ discard block |
||
| 334 | 388 | $record->$name = ''; |
| 335 | 389 | } |
| 336 | 390 | } |
| 337 | - foreach((array)$fields['links'] as $name) { |
|
| 338 | - if($record->$name) { |
|
| 339 | - if(is_numeric($record->$name) && !$links[$name]) { |
|
| 391 | + foreach((array)$fields['links'] as $name) |
|
| 392 | + { |
|
| 393 | + if($record->$name) |
|
| 394 | + { |
|
| 395 | + if(is_numeric($record->$name) && !$links[$name]) |
|
| 396 | + { |
|
| 340 | 397 | $link = Link::get_link($record->$name); |
| 341 | 398 | $links[$name] = ($link['link_app1'] == $appname ? $link['link_app2'] : $link['link_app1']); |
| 342 | 399 | $record->$name = ($link['link_app1'] == $appname ? $link['link_id2'] : $link['link_id1']); |
@@ -355,16 +412,22 @@ discard block |
||
| 355 | 412 | $record->$name = ''; |
| 356 | 413 | } |
| 357 | 414 | } |
| 358 | - foreach((array)$fields['select-account'] as $name) { |
|
| 415 | + foreach((array)$fields['select-account'] as $name) |
|
| 416 | + { |
|
| 359 | 417 | // Compare against null to deal with empty arrays |
| 360 | - if ($record->$name !== null) { |
|
| 361 | - if(is_array($record->$name)) { |
|
| 418 | + if ($record->$name !== null) |
|
| 419 | + { |
|
| 420 | + if(is_array($record->$name)) |
|
| 421 | + { |
|
| 362 | 422 | $names = array(); |
| 363 | - foreach($record->$name as $_name) { |
|
| 423 | + foreach($record->$name as $_name) |
|
| 424 | + { |
|
| 364 | 425 | $names[] = Api\Accounts::username($_name); |
| 365 | 426 | } |
| 366 | 427 | $record->$name = implode(', ', $names); |
| 367 | - } else { |
|
| 428 | + } |
|
| 429 | + else |
|
| 430 | + { |
|
| 368 | 431 | $record->$name = Api\Accounts::username($record->$name); |
| 369 | 432 | } |
| 370 | 433 | } |
@@ -373,23 +436,49 @@ discard block |
||
| 373 | 436 | $record->$name = ''; |
| 374 | 437 | } |
| 375 | 438 | } |
| 376 | - foreach((array)$fields['select-bool'] as $name) { |
|
| 377 | - if($record->$name !== null) { |
|
| 439 | + foreach((array)$fields['select-bool'] as $name) |
|
| 440 | + { |
|
| 441 | + if($record->$name !== null) |
|
| 442 | + { |
|
| 378 | 443 | $record->$name = $record->$name ? lang('Yes') : lang('No'); |
| 379 | 444 | } |
| 380 | 445 | } |
| 381 | - foreach((array)$fields['date-time'] as $name) { |
|
| 446 | + foreach((array)$fields['date-time'] as $name) |
|
| 447 | + { |
|
| 382 | 448 | //if ($record->$name) $record->$name = date('Y-m-d H:i:s',$record->$name); // Standard date format |
| 383 | - if ($record->$name && !is_numeric($record->$name)) $record->$name = strtotime($record->$name); // Custom fields stored as string |
|
| 384 | - if ($record->$name && is_numeric($record->$name)) $record->$name = date($GLOBALS['egw_info']['user']['preferences']['common']['dateformat'] . ' '. |
|
| 385 | - ($GLOBALS['egw_info']['user']['preferences']['common']['timeformat'] == '24' ? 'H:i:s' : 'h:i:s a'),$record->$name); // User date format |
|
| 386 | - if (!$record->$name) $record->$name = ''; |
|
| 449 | + if ($record->$name && !is_numeric($record->$name)) |
|
| 450 | + { |
|
| 451 | + $record->$name = strtotime($record->$name); |
|
| 452 | + } |
|
| 453 | + // Custom fields stored as string |
|
| 454 | + if ($record->$name && is_numeric($record->$name)) |
|
| 455 | + { |
|
| 456 | + $record->$name = date($GLOBALS['egw_info']['user']['preferences']['common']['dateformat'] . ' '. |
|
| 457 | + ($GLOBALS['egw_info']['user']['preferences']['common']['timeformat'] == '24' ? 'H:i:s' : 'h:i:s a'),$record->$name); |
|
| 458 | + } |
|
| 459 | + // User date format |
|
| 460 | + if (!$record->$name) |
|
| 461 | + { |
|
| 462 | + $record->$name = ''; |
|
| 463 | + } |
|
| 387 | 464 | } |
| 388 | - foreach((array)$fields['date'] as $name) { |
|
| 465 | + foreach((array)$fields['date'] as $name) |
|
| 466 | + { |
|
| 389 | 467 | //if ($record->$name) $record->$name = date('Y-m-d',$record->$name); // Standard date format |
| 390 | - if ($record->$name && !is_numeric($record->$name)) $record->$name = strtotime($record->$name); // Custom fields stored as string |
|
| 391 | - if ($record->$name && is_numeric($record->$name)) $record->$name = date($GLOBALS['egw_info']['user']['preferences']['common']['dateformat'], $record->$name); // User date format |
|
| 392 | - if (!$record->$name) $record->$name = ''; |
|
| 468 | + if ($record->$name && !is_numeric($record->$name)) |
|
| 469 | + { |
|
| 470 | + $record->$name = strtotime($record->$name); |
|
| 471 | + } |
|
| 472 | + // Custom fields stored as string |
|
| 473 | + if ($record->$name && is_numeric($record->$name)) |
|
| 474 | + { |
|
| 475 | + $record->$name = date($GLOBALS['egw_info']['user']['preferences']['common']['dateformat'], $record->$name); |
|
| 476 | + } |
|
| 477 | + // User date format |
|
| 478 | + if (!$record->$name) |
|
| 479 | + { |
|
| 480 | + $record->$name = ''; |
|
| 481 | + } |
|
| 393 | 482 | } |
| 394 | 483 | foreach((array)$fields['float'] as $name) |
| 395 | 484 | { |
@@ -397,7 +486,10 @@ discard block |
||
| 397 | 486 | if (is_null($dec_separator)) |
| 398 | 487 | { |
| 399 | 488 | $dec_separator = $GLOBALS['egw_info']['user']['preferences']['common']['number_format'][0]; |
| 400 | - if (empty($dec_separator)) $dec_separator = '.'; |
|
| 489 | + if (empty($dec_separator)) |
|
| 490 | + { |
|
| 491 | + $dec_separator = '.'; |
|
| 492 | + } |
|
| 401 | 493 | $thousands_separator = $GLOBALS['egw_info']['user']['preferences']['common']['number_format'][1]; |
| 402 | 494 | } |
| 403 | 495 | if($record->$name && (string)$record->$name != '') |
@@ -413,17 +505,27 @@ discard block |
||
| 413 | 505 | } |
| 414 | 506 | |
| 415 | 507 | // Some custom methods for conversion |
| 416 | - foreach((array)$methods as $name => $method) { |
|
| 417 | - if($record->$name) $record->$name = ExecMethod($method, $record->$name); |
|
| 508 | + foreach((array)$methods as $name => $method) |
|
| 509 | + { |
|
| 510 | + if($record->$name) |
|
| 511 | + { |
|
| 512 | + $record->$name = ExecMethod($method, $record->$name); |
|
| 513 | + } |
|
| 418 | 514 | } |
| 419 | 515 | |
| 420 | 516 | static $cat_object; |
| 421 | - if(is_null($cat_object)) $cat_object = new Api\Categories(false,$appname); |
|
| 422 | - foreach((array)$fields['select-cat'] as $name) { |
|
| 423 | - if($record->$name) { |
|
| 517 | + if(is_null($cat_object)) |
|
| 518 | + { |
|
| 519 | + $cat_object = new Api\Categories(false,$appname); |
|
| 520 | + } |
|
| 521 | + foreach((array)$fields['select-cat'] as $name) |
|
| 522 | + { |
|
| 523 | + if($record->$name) |
|
| 524 | + { |
|
| 424 | 525 | $cats = array(); |
| 425 | 526 | $ids = is_array($record->$name) ? $record->$name : explode(',', $record->$name); |
| 426 | - foreach($ids as $n => $cat_id) { |
|
| 527 | + foreach($ids as $n => $cat_id) |
|
| 528 | + { |
|
| 427 | 529 | |
| 428 | 530 | if ($cat_id && $cat_object->check_perms(Acl::READ,$cat_id)) |
| 429 | 531 | { |
@@ -444,7 +546,8 @@ discard block |
||
| 444 | 546 | * |
| 445 | 547 | * @return |
| 446 | 548 | */ |
| 447 | - public function __destruct() { |
|
| 549 | + public function __destruct() |
|
| 550 | + { |
|
| 448 | 551 | |
| 449 | 552 | } |
| 450 | 553 | |
@@ -457,11 +560,16 @@ discard block |
||
| 457 | 560 | * @param char $delimiter |
| 458 | 561 | * @param char $enclosure |
| 459 | 562 | */ |
| 460 | - protected function fputcsv($filePointer, Array $dataArray, $delimiter, $enclosure){ |
|
| 563 | + protected function fputcsv($filePointer, Array $dataArray, $delimiter, $enclosure) |
|
| 564 | + { |
|
| 461 | 565 | $string = ""; |
| 462 | 566 | $writeDelimiter = false; |
| 463 | - foreach($dataArray as $dataElement) { |
|
| 464 | - if($writeDelimiter) $string .= $delimiter; |
|
| 567 | + foreach($dataArray as $dataElement) |
|
| 568 | + { |
|
| 569 | + if($writeDelimiter) |
|
| 570 | + { |
|
| 571 | + $string .= $delimiter; |
|
| 572 | + } |
|
| 465 | 573 | $string .= $enclosure . str_replace(array("\r\n", '"'), array("\n",'""'), $dataElement) . $enclosure; |
| 466 | 574 | $writeDelimiter = true; |
| 467 | 575 | } |