class_mcCsv.inc.php 3.04 KB
<?php
class mcCsv {


	/**
	 *	transform array to csv format and export as file
	 * 
	 * @since 20110920 @author n@mensch.coop
	 * @static
	 * @downgraded for php4
	 * 
	 * @param array $param_array
	 * @param type $param_filename 
	 * @param array $param_search
	 * @param array $param_replace 
	 */
	function exportArrayToCsv(array $param_array, $param_charset='utf-8', $param_filename='download.csv', array $param_search = array('"', ';'), array $param_replace = array("'", " ")) {
		$ausgabe = '';
		if (is_array($param_array) && count($param_array)!=0) {
			foreach ($param_array as $daten) {
				if (is_array($daten) == true) {
					foreach ($daten as $key => $value) {
						$ausgabe .= '"'.str_replace($param_search, $param_replace,	$daten[$key]).'";';
					}
				}
				$ausgabe .= "\n";
			}
		} else {
			$ausgabe .= '"no data;"';
		}		

		if (headers_sent() == false) {
			header("Content-type: text/csv; charset='.$param_charset");
			header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
			header("Content-Disposition: attachment; filename =".$param_filename);
			header('Pragma: no-cache');
		}
		echo $ausgabe;		
		
	}

	
	public static function transformStringToCsv($param_string) {
		if(strpos($param_string, '.')!==false) {
			$param_string = str_replace('.', ',', $param_string);
		}		
		return $param_string;
	}	
	
	/**
	 *
	 * @param array $param_csvConfig -- array(0=>'col1',1=>'col2',2=>'col3')
	 * @param array $param_csvValues -- array(0=>'val1',1=>'val2',2=>'val3')
	 * @return array|boolean 
	 */
	public static function getCsvArrayForWriteDB(array $param_csvConfig, array $param_csvValues) {
		if(is_array($param_csvConfig) && !empty($param_csvConfig)) {
			$values = array();
			foreach($param_csvConfig as $csv_coloum => $tab_cloumn)  {
				$values[$tab_cloumn] = $param_csvValues[$csv_coloum];
			}
			return $values;
		} else {
			return false;
		}
	}
	
	/**
	 *	show how the format for csv
	 * 
	 * @param array $param_csvConfig -- array(0=>'col1',1=>'col2',2=>'col3')
	 */
	public static function showCsvFormatAsHtml(array $param_csvConfig) {
		if(!is_null($param_csvConfig)) {
//			$csv_columns = mcConfig::getConfig('csv', 'product');
			$cvs_import_header = array();
			$csv_header = '';
			for($i=0; $i<count($param_csvConfig); $i++) {
				if($param_csvConfig[$i]) {
					$cvs_import_header[] = mcLanguage::getWord($param_csvConfig[$i]);
					$csv_header .= '"'.mcLanguage::getWord($param_csvConfig[$i]).'", ';
				}
			}
			mcTableHtml::showTable(array($cvs_import_header), mcLanguage::getWord('rowformat').': '.$csv_header);					
		}
	}

	
	
	public static function exportXmlToCsv($param_xml,$param_name, $param_charset='utf-8', $param_filename='download.csv'){

		$csv_value=explode('CUT_VALUE_FOR_NOT_THE_HEADER',$param_xml);

		// Header senden
			header("Content-type: text/csv; charset='.$param_charset");
			header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
			header("Content-Disposition: attachment; filename =".$param_filename);
			header('Pragma: no-cache');

		// 	Datei ausgeben
			echo strip_tags(htmlspecialchars_decode(trim($csv_value[1])));
	}	
}
?>