Your IP : 216.73.216.201


Current Path : /home/balossw/www/temp/common/includes/
Upload File :
Current File : /home/balossw/www/temp/common/includes/csv.inc.php

<?php
/*---------------------------------------------------------------------------
		File name ............ includes/csv.inc.php
		Author ............... Antoine Lemoine
		Creation date ........ 03.09.2009
		Modification date .... 30.10.2010
---------------------------------------------------------------------------*/


if (!class_exists('CsvFile'))
{
	class CsvFile
	{
		var $first_row_index;
		var $name;
		var $filename;
		var $column_name_array;
		var $separator;
		var $separators_array;
		var $lines;

		function CsvFile($filename, $name=''){
			$this->first_row_index = 1;
			$this->filename = $filename;
			$this->name = $name!='' ? $name : $filename;
			$this->column_name_array = array();
			$this->lines = null;
			$this->separator = null;
			$this->separators_array = array(','=>',', ';'=>';', "\t"=>'[TAB]', ' '=>'[SPACE]');
			
			$this->lines = $this->_getLines();
		}

		function _getLines(){
			$file_stream = fopen($this->filename,'r');

			if ($file_stream){
				$separators_count = array();
				foreach ($this->separators_array as $key => $value){
					$separators_count[$key] = 0;
				}
	
				$lines = array();
				if ($file_stream){
					while (!feof($file_stream)){
						$line = fgets($file_stream, 4096);
						if (trim($line)!=''){
							$lines[] = preg_replace("/[\n\r]*/",'',$line);
							foreach ($separators_count as $separator => $value){
								$separators_count[$separator] += $this->_getSubstringCount($line,$separator);
							}
						}
					}
				}
	
				$max = 0;
				foreach ($separators_count as $separator => $value){
					if ($value>$max){
						$this->separator = $separator;
						$max = $value;
					}
				}
	
				fclose($file_stream);
				return $lines;
			} else {
				return null;
			}
		}

		function _getSubstringCount($string, $substring) { 
			$result = explode($substring,$string); 
			if (!is_array($result)) { return 0; }
			return count($result) - 1;
		}

		function getFilename() {
			return $this->filename;
		}

		function getName() {
			return $this->name;
		}

		function setFirstRowIndex($first_row_index) {
			$this->first_row_index = $first_row_index;
		}

		function getFirstRowIndex() {
			return $this->first_row_index;
		}

		function setSeparator($separator) {
			if ($separator!='') {
				if (in_array($separator,$this->separators_array)) {
					$this->separator = array_search($separator,$this->separators_array);
				} else {
					$this->separator = $separator;
				}
			}
		}

		function getSeparatorString() {
			if (isset($this->separators_array[$this->separator])) return $this->separators_array[$this->separator];
			else return $this->separator;
		}

		function getSeparator() {
			return $this->separator;
		}

		function getRowCount() {
			$count = count($this->lines)-($this->first_row_index-1);
			if ($count<0) $count = 0;
			return $count;
		}
		
		function getColumnCount() {
			foreach ($this->lines as $line) {
				$column_count = $this->_getSubstringCount($line,$this->separator);
			}
			return $column_count;
		}
		
		function getRow($index) {
			return $this->lines[$index+$this->first_row_index-1];
		}

		function getRowData($index) {
			$row = $this->getRow($index);

			$data = explode($this->separator,$row);
			if (!is_array($data)) $data = array($data);
			$result = array();

			return $data;
		}
	}

	class BinaryImportationOption
	{
		var $key;
		var $label;
		var $yes_label;
		var $no_label;
		var $default_value;
		var $value;
		
		function BinaryImportationOption($key, $label, $yes_label, $no_label, $default_value) {
			$this->key = $key;
			$this->label = $label;
			$this->yes_label = $yes_label;
			$this->no_label = $no_label;
			$this->default_value = $default_value;
			$this->value = $default_value;
		}
	}

	class SelectImportationOption
	{
		var $key;
		var $label;
		var $values;
		var $default_value;
		var $value;
		
		function SelectImportationOption($key, $label, $values, $default_value) {
			$this->key = $key;
			$this->label = $label;
			$this->values = $values;
			$this->default_value = $default_value;
			$this->value = $default_value;
		}
	}

	class CsvFileImporter
	{
		var $csv_file;
		var $reference_row_index;
		var $column_index_array;
		var $options;
		var $data_fields_array;
		var $self_link;

		function CsvFileImporter($data_fields_array, $options, $self_link) {
			$this->data_fields_array = $data_fields_array;
			$this->options = $options;
			$this->self_link = $self_link;
		}

		function getOptions($row_data,$index_key) {
			//$options = "<option value=\"-1\">Ne pas importer</option>";
			$options = array('-1'=>'Ne pas importer');
			$i = 0;
			foreach ($row_data as $key => $value) {
				/*$options .= "<option value=\"".$i."\""
					. ($this->column_index_array[$index_key]==$i ? " selected=\"selected\"" : '').">"
					. '[colonne '.($i+1).'] ' . strip_tags($value)."</option>";*/
				$options[$i] = '[colonne '.($i+1).'] ' . strip_tags($value);
				$i++;
			}
			return $options;
		}
	
		function getData($row_index,$key,$type,$replacement='') {
			$row_data = $this->csv_file->getRowData($row_index);
			$value = isset($row_data[$this->column_index_array[$key]]) ? $row_data[$this->column_index_array[$key]] : $replacement;
			if ($type=='number') {
				$value = str_replace(array(',',' '),array('.',' '),$value);
			}
			return $value;
		}
	
		function getRowCount() {
			return isset($this->csv_file) ? $this->csv_file->getRowCount() : 0;
		}
		
		function putOption($key, $option) {
			$this->options[$key] = $option;
		}

		function init() {
			$this->reference_row_index = 0;

			$this->column_index_array = array();
			$i = 0;
			foreach ($this->data_fields_array as $key => $field) {
				$this->column_index_array[$key] = $i;
				$i++;
			}
		}

		function getResultDisplay($result_array) {
			$html_code = "<div class=\"box\">"
				. "<h2>Importer un fichier CSV</h2>"
				. "<table><tr><th colspan=\"2\">R&eacute;sultats de l'importation</th></tr>";
			
			$i = 0;
			foreach ($result_array as $label => $value) {
				$html_code .= "<tr class=\"row".($i%2)."\"><td>".$label."</td><td>".$value."</td></tr>";
				$i++;
			}

			$html_code .= "</table></div><div class=\"button_div\">"
				. "<input name=\"cancel\" type=\"button\" value=\"Retour\" onclick=\"location = '".$this->self_link."&amp;action=import_csv';\" /></div>";
			return $html_code;
		}

		function setConfig($_config) {
			if (isset($_config['reference_row_index'])) {
				$this->reference_row_index = $_config['reference_row_index'];
			}
			if (isset($_config['separator'])) {
				$this->csv_file->setSeparator($_config['separator']);
			}
			if (isset($_config['first_row_index'])) {
				$this->csv_file->setFirstRowIndex($_config['first_row_index']);
			}
			
			foreach ($_config as $key => $value) {
				if (substr($key,0,13)=='column_index_') {
					$real_key = substr($key,13,strlen($key)-13);
					$this->column_index_array[$real_key] = $value;
				} else if (substr($key,0,8)=='options_') {
					$real_key = substr($key,8,strlen($key)-8);
					$this->options[$real_key]->value = $value;
				}
			}
		}

		function getUploadForm() {
			return "
						<h2>Importer un fichier CSV</h2>
						<form action=\"".$this->self_link."&amp;subaction=upload\" method=\"post\" enctype=\"multipart/form-data\">
							<p>S&eacute;lectionner un fichier &nbsp;"
							. createFilefieldInput(array('name'=>'csv_file','accept'=>'*/*'))."
							<p>
								<input name=\"sent\" value=\"T&eacute;l&eacute;charger\" type=\"submit\" class=\"button\" />
							</p>
						</form>
					";
		}

		function import(&$process_authorized, $import_config=null) {
			$process_authorized = false;
	
			$html_code = '';

			if (isset($_GET['subaction'])) {
				switch ($_GET['subaction']) {
					case 'upload' :
						if (isset($_FILES['csv_file']['tmp_name'])) {
							$this->csv_file = new CsvFile($_FILES['csv_file']['tmp_name'],$_FILES['csv_file']['name']);
							$this->init();
						}
						break;
					case 'cancel' :
						$this->csv_file = null;
						break;
				}
			}
			
			if (!isset($this->csv_file)) {
				$html_code .= $this->getUploadForm();
			} else {
				if ($this->csv_file->getRowCount()+$this->csv_file->getFirstRowIndex()>0) {
					if (!isset($import_config) && isset($_POST) && count($_POST)>0) {
						$import_config = $_POST;
					}
					if (isset($import_config)) $this->setConfig($import_config);

					if (isset($import_config['csv_do_import']) && $import_config['csv_do_import']=='1') {
						$process_authorized = true;
					} else {
						//$lines_options = '';
						$lines_options = array();
						for ($i=0; $i<min(10,$this->csv_file->getRowCount()); $i++) {
							/*$lines_options .= "<option value=\"".$i."\""
								. ($this->reference_row_index==$i ? " selected=\"selected\"" : '').">"
								. '[ligne '.($i+1).'] ' . substr(strip_tags($this->csv_file->getRow($i)),0,100)."</option>";*/
							$lines_options[$i] .= '[ligne '.($i+1).'] ' . substr(strip_tags($this->csv_file->getRow($i)),0,100);
						}
			
						$row_data = $this->csv_file->getRowData($this->reference_row_index);
	
						$html_code .= "<div class=\"box\">"
							. "<h2>Importer un fichier CSV</h2>"
							. "<form id=\"csv_data_form\" action=\"".$this->self_link."\" method=\"post\">"
							. "<table>"
							. "<tr><th colspan=\"2\">Informations sur le fichier</th></tr>"
							. "<tr class=\"row0\"><td>Nom du fichier</td><td>".$this->csv_file->getName()."</td></tr>"
							. "<tr class=\"row1\"><td>Nombre de lignes</td><td>".$this->csv_file->getRowCount()."</td></tr>"
							. "<tr><th colspan=\"2\">Param&egrave;tres d'importation</th></tr>"
							. "<tr class=\"row0\"><td>Ligne de r&eacute;f&eacute;rence</td><td>".createTextfieldSelect(array('name'=>'reference_row_index'),$lines_options,$this->reference_row_index)."</td></tr>"
							. "<tr class=\"row1\"><td>S&eacute;parateur</td><td>".createTextfieldInput(array('name'=>'separator','value'=>$this->csv_file->getSeparatorString()))."</td></tr>"
							. "<tr class=\"row0\"><td>Premi&egrave;re ligne</td><td>".createTextfieldInput(array('name'=>'first_row_index','value'=>$this->csv_file->getFirstRowIndex()))."</td></tr>";
						
						$i = 1;
						foreach ($this->options as $key => $option) {
							$html_code .= "<tr class=\"row".($i%2)."\"><td>".$option->label."</td><td>";
							if (isset($option->values)) {
								//$options_code = '';
								$options_code = array();
								foreach ($option->values as $label => $value) {
									/*$options_code .= "<option value=\"".$value."\""
										. ($option->value==$value ? " selected=\"selected\"" : '').">"
										. $label."</option>";*/
									$options_code[$value] = $label;
								}
								$html_code .= createTextfieldSelect(array('name'=>'options_'.$key),$options_code,$option->value);
							} else {
								$html_code .= "<input type=\"radio\" class=\"radio\" name=\"options_".$key."\" value=\"0\""
										. (!$this->options[$key]->value ? " checked=\"checked\"" : "")." /> ".$option->no_label
									. " &nbsp; <input type=\"radio\" class=\"radio\" name=\"options_".$key."\" value=\"1\""
										. ($this->options[$key]->value ? " checked=\"checked\"" : "")." /> ".$option->yes_label;
							}
							$html_code .= "</td></tr>";
							$i++;
						}
						
						$html_code .= "<tr><th colspan=\"2\">Correspondance des donn&eacute;es</th></tr>";
						
						$i = 0;
						foreach ($this->data_fields_array as $key => $field) {
							$label = is_array($field) ? $field['label'] : $field;
							$html_code .= "<tr class=\"row".($i%2)."\"><td>".$label."</td><td>".createTextfieldSelect(array(
									'name' => 'column_index_'.$key,
								),$this->getOptions($row_data,$key),$this->column_index_array[$key])."</td></tr>";
							$i++;
						}
						$html_code .= "</tr></table>"
							. "<p><input id=\"csv_do_import\" name=\"csv_do_import\" type=\"hidden\" value=\"\" />"
							. "<input type=\"submit\" class=\"button\" value=\"Importer\""
							. " onclick=\"document.getElementById('csv_do_import').value = '1';\" /> &nbsp; "
							. "<input value=\"Actualiser l'aper&ccedil;u\" type=\"submit\" class=\"button\" /></p>"
							. "</form>";
		
						$html_code .= "<table><tr>";
						foreach ($this->data_fields_array as $key => $field) {
							$label = is_array($field) ? $field['label'] : $field;
							$html_code .= "<th>".$label."</th>";
						}
		
						for ($i=0; $i<min(10,$this->csv_file->getRowCount()); $i++) {
							$row_data = $this->csv_file->getRowData($i);
							$html_code .=  "<tr class=\"row".($i%2)."\">";
							
							foreach ($this->data_fields_array as $key => $field) {
								$type = is_array($field) ? $field['type'] : 'text';
								$html_code .= "<td>".strip_tags($this->getData($i,$key,$type))."</td>";
							}
	
							$html_code .=  "</tr>";
						}
						$html_code .=  "</table></div><div class=\"button_div\">"
							. "<input name=\"cancel\" type=\"button\" class=\"button\" value=\"Annuler\""
							. " onclick=\"location = '".$this->self_link."&amp;subaction=cancel';\" /></div>";
					}
				} else {
					error('Le fichier ne contient pas de donn&eacute;e');
				}
			}
			
			return $html_code;
		}
	}
}

?>