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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| <?php
namespace classes\Ticket_ARM;
use DateTime;
use classes\MYPDO\MYPDO; //to use class MYPDO
include(dirname(__DIR__).DIRECTORY_SEPARATOR."config".DIRECTORY_SEPARATOR."config.php");//definition of the constants
include(DIR_ROOT."classes".DIRECTORY_SEPARATOR."MYPDO.php");//definition of the class for DB
class Ticket_ARM
implements \ArrayAccess,\Countable
{
private $vars = [];
private $Id; //string *
private $Alternate_Id;
private $Platform; //string
private $Platform_Owner; //string
private $Publisher; //string
private $Application_Name; //string *
private $Hfmcode;
private $Currently_Active; //boolean
private $Activated_Date; //boolean *
private $Deactivated_Date; //boolean
private $Groupname; //string
private $Office_Country;//string *
private $Firstname; //string
private $Lastname; //string
private $Company_Name;
private $Job_Code;
private $Reporting_Entity;
private $Business_Unit_Name; // *
private $Location; // *
private $Internet_Address; // *
private $Manager_Firstname;
private $Manager_Lastname;
private $Manager_Mail;
/* Constructor
*/
public function __construct()
{
}
/* Method which read a csv file on the hard disk, and then create lines into tickets SQL table
* @return boolean value=TRUE if success, FALSE if not
*/
public function Csv_ARM_To_Db() {
//to convert the date into SQL format : YYYY-MM-DD HH:MM:SS (SQL format)
//initial format : MMM-DD, YYYY (MMM in letters)
function get_date_sql($date) {
$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);}
var_dump($date1->date);
}
if ($date1!=false) return( $date1->format('Y-m-d H:i:s'));
else return(false);
}
//to suppress the substring "SESA"
function Supp_SESA($val) {
return(substr($val,5));
}
?>
<!-- form to download a csv file from the hard disk -->
<form action="" method="post" enctype="multipart/form-data">
<label for="file2"><b>File CSV</b></label>
<input type="file" name="file2" id="file2"><br/>
<input type="submit" name="submitfile2" value="csv file download"><br/>
</form>
<?php
if (!defined('DIR_ROOT')) define('DIR_ROOT', dirname(__DIR__).DIRECTORY_SEPARATOR);
// one checks the extension, and if not csv, one exits
if (isset($_POST['submitfile2'])) {
echo "filename :".$_FILES['file2']["name"]."<br/>";
$info = pathinfo($_FILES['file2']["name"], PATHINFO_EXTENSION);
if ($info!='csv') {
$msg_error="the file is not a csv";exit($msg_error);
}
//to put the file into an array ($data): each cell of the array contents a line of the csv file
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
$sep = ','; // by default
$separators = [ ',', ';', '|'];
foreach ($separators as $separator) {
if ( strpos($header, $separator)> 0) { //there is a coma which is not a separator
$sep = $separator;
break;
}
}
$pdo=new MYPDO(); // creation of an object for DB (instantiation of MYPDO)
//if table of db not empty, one cleans it
$req="SELECT * from `tickets_ARM`";
$var=$pdo->select($req,[]);
if (!empty($var)) self::Clean_Db();
// 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, $sep) ) {
if ($sep==","){//as the code is correct in the case where the date is in a unique line, one puts in that case
$fields[8]=$fields[8].",".$fields[9];
}
$fields[8]=get_date_sql($fields[8]);
$fields[0]=Supp_SESA($fields[0]); //to suppress the substring "SESA"
$nb_cell_line=sizeof($fields);$val="";
$no_id_to_register=[0,5,8,11,17,18,19];
for ($i=0;$i<$nb_cell_line;$i++) {
//suppress some colowns 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
}
$val = substr(substr($val, 0, -1),0,-1); //one suppresses the last 2 elements of the string
$req='INSERT into `tickets_ARM` VALUES("'.$val.')';//to put the string into the db
if (!empty($val)) $var=$pdo->select($req,[]);
}
fclose($handle);
return(TRUE);
}
}
} //end method Csv_ARM_To_Db()
/*
method Clean_Db : to empty SQL table tickets
* @return boolean value=TRUE if success, FALSE if not
*/
public function Clean_Db() {
$pdo=new MYPDO();
$req= 'delete from `tickets_ARM`' ;
$var=$pdo->select($req,[]);
if (isset($var)) return(TRUE);
else return(FALSE);
} // end of method Clean_Dbb
/**
* Interface ArrayAccess
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->vars);
}
/**
* Interface ArrayAccess
* @param mixed offset
*
* @return mixed|null
*/
public function offsetGet($offset)
{
return $this->vars[$offset] ?? null;
}
/**
* Interface ArrayAccess
* @param mixed $offset
*
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
$this->vars[$offset] = $value;
}
/**
* Interface ArrayAccess
* @param mixed $offset
*/
public function offsetUnset($offset)
{
unset($this->vars[$offset]);
}
/**
* Interface Countable
* @return int
*/
public function count()
{
return count($this->vars);
}
/**
* Cette fonction magique permet de renvoyer une chaîne de caractères quand dans un contexte
* particulier il est obligatoire d'avoir du texte : par exemple au sein d'un bloc HEREDOC,
* au moment où on fait echo, print_r... tous ces contextes nécessitent de manipuler du texte
* Si on utilise notre instance dans ce contexte, grâce à cette fonction magique, le contexte
* recevra du texte
*/
public function __tostring()
{
}
} // end class Ticket |
Partager