1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
<?php
namespace Classes\Olivier;
use DateTime;
use vendor\PhpEcho\PhpEcho;
use vendor\rawsrc\Request;
use Classes\MYPDO; //to use class MYPDO
class CSVParser
{
private $sep;
/* Constructor */
public function __construct()
{
} // end __construct
public function parse($file)
{
// to convert priority to integer
function priorityToInt($priority) {
switch($priority) {
case "Low" : return(0); break;
case "Medium" : return(1); break;
case "High" : return(2); break;
default : return(0); break;
}
}
// to convert status to integer
function statusToInt($status) {
switch($status) {
case "Assigned" : return(0); break;
case "Cancelled" : return(1); break;
case "Closed" : return(2); break;
case "In Progressed" : return(3); break;
case "Pending" : return(4); break;
case "Resolved" : return(5); break;
default : return(0); break;
}
}
//to convert the date into SQL format : YYYY-MM-DD HH:MM:SS
function convertDate($olddate) {echo "csvWrite 58 olddate=".$olddate."<br/>";
list($date,$heure)=explode(' ',$olddate);
list($jour,$mois,$annee)=explode('/',$date);
$newdate=date('Y-m-d',mktime(0,0,0,$mois,$jour,$annee));
$newdate=implode([$newdate,' ',$heure]);
return($newdate);
}
function getDateSql($date) {
// possible formats : M d, Y ou M d Y ou M d ou M-d
$date1 = \DateTime::createFromFormat('M d, Y', $date);
if ($date1==false) {$date1=\DateTime::createFromFormat('M d Y', $date);}
if ($date1==false) {
{$date1=\DateTime::createFromFormat('M d', $date);}
if ($date1==false) {$date1=\DateTime::createFromFormat('M-d', $date);}
}
if ($date1!=false) return( $date1->format('Y-m-d H:i:s'));
else return(false);
}
//to suppress the substring "SESA"
function SuppSESA($val) {
return(substr($val,4)); // to return $val, starting from pos 4 (5th character)
}
// deb of code
if (isset($_FILES['file2']))
if (!$_FILES['file2']["error"]) {
$upload_dir = 'csv';
$name=$_FILES['file2']['name'];
if ( false === $handle = fopen(DIR_ROOT.$upload_dir.DIRECTORY_SEPARATOR.$name, 'r') )
throw new Exception("impossible to open the file '$name'");
// one reads the header
$buffer = 4096;
if ( false === ($header = fgets($handle, $buffer)) )
throw new Exception("The file '$name' is empty");
// one tests different separators
$this->sep = ','; // by default
$separators = [',', ';'];
foreach ($separators as $separator) {
if ( strpos($header, $separator)> 0) {
$this->sep = $separator;
break;
}
} // end foreach
//Remove UTF8 Bom
function removeUtf8Bom($text)
{
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}
$header = removeUtf8Bom($header);
$deb_header=substr($header,0,6);
// returns true if $needle is a substring of $haystack
function contains($needle, $haystack)
{
return strpos($haystack, $needle) !== false;
}
if (!contains("SESA",$deb_header))
{
$body = new PhpEcho('', ['msg_error' => "this file is not a licence"]);
$body->setCode('<p>'.nl2br($body('msg_error')).'<br></p>');
echo new PhpEcho([DIR_ROOT, 'view Layout.php'], ['body' => $body]);
exit;
}
$content_csv=[];
// the function fgetcsv reads one line of the csv file and each value is put in the array $fields, knowing the delimiter $rep
while (false !== ($fields = fgetcsv($handle, $buffer, $this->sep)) ) {
//var_dump($fields);
$fields=str_replace('"','',$fields);
//var_dump($fields);
if ($this->sep == ","){//as the code is correct in the case where the date is in a unique line, one puts in that case
$fields[8] = isset($fields[8]) ? ($fields[8].",".$fields[9]) : '';
}
$fields[8]=getDateSql($fields[8]);
$fields[0]=SuppSESA($fields[0]); //to suppress the substring "SESA"
$nb_cell_line=sizeof($fields);$val="";
// the shift depends on the separator
switch($this->sep)
{
case ';' :
$no_id_to_register=[0,5,8,11,17,18,19]; break;
case ',' :
$no_id_to_register=[0,5,8,12,18,19,20]; break;
default : break;
}
for ($i=0;$i<$nb_cell_line;$i++) {
//suppress some colons from the registration
if (in_array($i,$no_id_to_register)) {$val.=$fields[$i].'","';} //some cells of the array are concatenated to make a string
}
$content_csv[] = substr($val, 0, -2); //one suppresses the last 2 elements of the string
} // end of while
} // end if (!$=FILES['file2']["error"])
return($content_csv);
} // end of parse
} //end class CSVParser |
Partager