Index: branches/5.2.x/core/units/helpers/csv_helper.php =================================================================== diff -u -N -r14585 -r14870 --- branches/5.2.x/core/units/helpers/csv_helper.php (.../csv_helper.php) (revision 14585) +++ branches/5.2.x/core/units/helpers/csv_helper.php (.../csv_helper.php) (revision 14870) @@ -1,6 +1,6 @@ Application->RecallVar('export_rand'); $get_rand = $this->Application->GetVar('export_rand'); - if($export_data && $export_rand == $get_rand) { + $file_helper =& $this->Application->recallObject('FileHelper'); + /* @var $file_helper FileHelper */ + + if ( $export_data && $export_rand == $get_rand ) { $export_data = unserialize($export_data); $first_step = false; } else { // first step - $export_data = Array(); + $export_data = Array (); $export_data['prefix'] = $this->PrefixSpecial; $export_data['grid'] = $this->grid; - $export_data['file_name'] = EXPORT_PATH.'/'.$this->ValidateFileName(EXPORT_PATH, 'export_'.$export_data['prefix'].'.csv'); + $export_data['file_name'] = EXPORT_PATH . '/' . $file_helper->ensureUniqueFilename(EXPORT_PATH, 'export_' . $export_data['prefix'] . '.csv'); $export_data['step'] = EXPORT_STEP; $export_data['delimiter'] = $this->delimiter_mapping[(int)$this->Application->ConfigValue('CSVExportDelimiter')]; $export_data['enclosure'] = $this->enclosure_mapping[(int)$this->Application->ConfigValue('CSVExportEnclosure')]; @@ -50,74 +53,74 @@ $lang_object =& $this->Application->recallObject('lang.current'); /* @var $lang_object LanguagesItem */ - $export_data['source_encoding'] = strtoupper( $lang_object->GetDBField('Charset') ); + + $export_data['source_encoding'] = strtoupper($lang_object->GetDBField('Charset')); $export_data['encoding'] = $this->Application->ConfigValue('CSVExportEncoding') ? false : 'UTF-16LE'; $this->Application->StoreVar('export_rand', $get_rand); $first_step = true; } - $file_helper =& $this->Application->recallObject('FileHelper'); - /* @var $file_helper FileHelper */ - - $file_helper->CheckFolder( dirname($export_data['file_name']) ); - $file = fopen($export_data['file_name'], $first_step ? 'w' : 'a'); $prefix_elems = preg_split('/\.|_/', $export_data['prefix'], 2); $grids = $this->Application->getUnitOption($prefix_elems[0], 'Grids'); - $grid_config = $grids[ $export_data['grid'] ]['Fields']; + $grid_config = $grids[$export_data['grid']]['Fields']; - $list_params = Array('per_page' => $export_data['step'], 'grid' => $export_data['grid']); - $list =& $this->Application->recallObject(rtrim(implode('.', $prefix_elems), '.'), $prefix_elems[0].'_List', $list_params); + $list_params = Array ('per_page' => $export_data['step'], 'grid' => $export_data['grid']); + $list =& $this->Application->recallObject(rtrim(implode('.', $prefix_elems), '.'), $prefix_elems[0] . '_List', $list_params); /* @var $list kDBList */ - $list->SetPage($export_data['page']); + $list->SetPage($export_data['page']); $list->Query(); $list->GoFirst(); $picker_helper =& $this->Application->RecallObject('ColumnPickerHelper'); /* @var $picker_helper kColumnPickerHelper */ + $picker_helper->ApplyPicker(rtrim(implode('.', $prefix_elems), '.'), $grid_config, $export_data['grid']); - if($first_step) { + if ( $first_step ) { // if UTF-16, write Unicode marker - if($export_data['encoding'] == 'UTF-16LE') { - fwrite($file, chr(0xFF).chr(0xFE)); + if ( $export_data['encoding'] == 'UTF-16LE' ) { + fwrite($file, chr(0xFF) . chr(0xFE)); } // inserting header line - $headers = Array(); - foreach($grid_config as $field_name => $field_data) { - $use_phrases = array_key_exists('use_phrases', $field_data) ? $field_data['use_phrases'] : 1; - $header = $use_phrases ? $this->Application->Phrase( $field_data['title'] ) : $field_data['title']; + $headers = Array (); + foreach ($grid_config as $field_name => $field_data) { + $use_phrases = array_key_exists('use_phrases', $field_data) ? $field_data['use_phrases'] : true; + $field_title = isset($field_data['title']) ? $field_data['title'] : 'column:la_fld_' . $field_name; + $header = $use_phrases ? $this->Application->Phrase($field_title) : $field_title; array_push($headers, $header); } + $csv_line = kUtil::getcsvline($headers, $export_data['delimiter'], $export_data['enclosure'], $export_data['record_separator']); - if($export_data['encoding']) { + if ( $export_data['encoding'] ) { $csv_line = mb_convert_encoding($csv_line, $export_data['encoding'], $export_data['source_encoding']); } fwrite($file, $csv_line); } - while(!$list->EOL()) - { - $data = Array(); - foreach($grid_config as $field_name => $field_data) { - if(isset($field_data['export_field'])) { + while (!$list->EOL()) { + $data = Array (); + foreach ($grid_config as $field_name => $field_data) { + if ( isset($field_data['export_field']) ) { $field_name = $field_data['export_field']; } $value = $list->GetField($field_name, isset($field_data['format']) ? $field_data['format'] : null); $value = str_replace("\r\n", "\n", $value); $value = str_replace("\r", "\n", $value); array_push($data, $value); } - if($export_data['encoding'] == 'UTF-16LE') { - fwrite($file, chr(0xFF).chr(0xFE)); + + if ( $export_data['encoding'] == 'UTF-16LE' ) { + fwrite($file, chr(0xFF) . chr(0xFE)); } + $csv_line = kUtil::getcsvline($data, $export_data['delimiter'], $export_data['enclosure'], $export_data['record_separator']); - if($export_data['encoding']) { + if ( $export_data['encoding'] ) { $csv_line = mb_convert_encoding($csv_line, $export_data['encoding'], $export_data['source_encoding']); } fwrite($file, $csv_line); @@ -130,7 +133,7 @@ fclose($file); - if ($records_processed >= $list->GetRecordsCount()) { + if ( $records_processed >= $list->GetRecordsCount() ) { $this->Application->StoreVar('export_data', serialize($export_data)); $this->Application->Redirect($this->Application->GetVar('finish_template')); } @@ -141,30 +144,29 @@ $this->Application->StoreVar('export_data', serialize($export_data)); } - function ValidateFileName($path, $name) - { - $parts = pathinfo($name); - $ext = '.'.$parts['extension']; - $filename = mb_substr($parts['basename'], 0, -mb_strlen($ext)); - $new_name = $filename.$ext; - while ( file_exists($path.'/'.$new_name) ) - { - if ( preg_match('/('.preg_quote($filename, '/').'_)([0-9]*)('.preg_quote($ext, '/').')/', $new_name, $regs) ) { - $new_name = $regs[1].($regs[2]+1).$regs[3]; - } - else { - $new_name = $filename.'_1'.$ext; - } - } - return $new_name; - } - function ExportData($name) { $export_data = unserialize($this->Application->RecallVar('export_data')); return isset($export_data[$name]) ? $export_data[$name] : false; } + /** + * Returns prefix from request or from stored import/export data + * + * @param bool $is_import + * @return string + */ + public function getPrefix($is_import = false) + { + $prefix = $this->Application->GetVar('PrefixSpecial'); + + if ( !$prefix ) { + return $is_import ? $this->ImportData('prefix') : $this->ExportData('prefix'); + } + + return $prefix; + } + function GetCSV() { kUtil::safeDefine('DBG_SKIP_REPORTING', 1);