id = $id; $this->log["insert_req"] = 0; $this->log["inserted"] = 0; $this->log["update_req"] = 0; $this->log["updated"] = 0; $this->log["skipped"] = 0; $this->log["deleted"] = 0; } function check(){ if(!$this->file) $this->log["errors"][] = "Non č stato settato il file da cui importare"; // if(!$this->insert_query) $this->log["errors"][] = "Non č stato la query per fare l'inserimento"; if($this->find_query && !$this->update_query) $this->log["errors"][] = "E' stata settata la query per verificare l'esistenza di un record ma non quella per aggiornarlo se viene trovato"; return !$this->log["errors"]; } function import($file=null){ if($file) $this->file = $file; if($this->check()){ if($this->truncate_query){ $this->eval_query($this->truncate_query); if(mysql_affected_rows() != -1) $this->log["deleted"] += mysql_affected_rows(); } $counter = 0; $handle = fopen($this->file,"r"); while (($row = fgetcsv($handle, 100000, $this->delimiter)) !== FALSE){ $counter++; $func = $this->row_function_name; if($this->row_function_name && !$func($row)){//funzione custom di controllo e manipolazione $this->log["skipped"]++; // $this->log["skippedId"][] = $row[0]; } else if($this->column_count && $this->column_count != count($row)){ $this->log["errors"][] = "numero di colonne errato nella riga: ".var_export($row, true); } else if(!$this->start_row || $counter >= $this->start_row){ if($this->find_query){ $r = $this->eval_query($this->find_query, $row); $esistenti = mysql_num_rows($r); } else{ $esistenti = 0; } if($esistenti == 1 || ($esistenti > 1 && $this->allow_multiple_rows_update)){//aggiorno $this->log["update_req"]++; $this->eval_query($this->update_query, $row); $modifiche = mysql_affected_rows(); if($this->after_update_func && call_user_func($this->after_update_func, $row)){ if(!$modifiche) $modifiche = 1; } if($modifiche){ $this->log["updated"] += $modifiche?$modifiche:1; $this->log["updatedId"][] = $row[0]; } } else if($esistenti === 0){//inserisco $this->log["insert_req"]++; if($this->insert_query){ $this->eval_query($this->insert_query, $row); if(mysql_affected_rows() != -1){ if($this->after_insert_func) call_user_func($this->after_insert_func, $row); $this->log["inserted"] += mysql_affected_rows(); } } } else{//ci sono pių righe con lo stesso id $this->log["errors"][] = "sono state rilevate pių righe con lo stesso identificativo. riga: ".var_export($row, true); } } } fclose($handle); } return $this->log; } function eval_query($query, &$row = null){//quoto e faccio la sostituzione if($row) foreach($row as $i=>$data){ $query = str_replace('$row['.$i.']', str_replace('"', '\"', $data), $query); } $r = mysql_query($query); if(mysql_error()) $this->log["errors"][] = mysql_error() . "
" . $query; return $r; } function basicUsage($file = null){ $src = $file ? $file : $_FILES['file_source']['tmp_name']; if(!$file) $out = $this->getSimpleForm(); if(($file||$_POST["go_$this->id"]) && $src){ $log = $this->import($src); $out .= "

Log importazione $this->id (cambiamenti effettivi/richieste):

"; $out .= ""; unlink($src); } $this->check(); if($log["errors"]){ $out .= "

Errori importazione $this->id:

"; $out .= ""; } return $out; } function getSimpleForm(){ return '
File "'.ucfirst($this->id).'.csv":
'; } } ?>