Changeset 485 for trunk/phpbms/include/imports.php
- Timestamp:
- 04/07/09 11:44:18 (3 years ago)
- Files:
-
- 1 modified
-
trunk/phpbms/include/imports.php (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/phpbms/include/imports.php
r433 r485 1 1 <?php 2 2 class phpbmsImport{ 3 3 4 4 var $table; 5 5 var $error = ""; … … 10 10 var $data; 11 11 var $revertID = 0; 12 12 13 13 // Do not manually override 14 14 var $transactionRecords = array(); 15 15 var $tempFileID = 0; 16 16 var $pageType = "main"; 17 17 18 18 function phpbmsImport($table, $importType = "csv"){ 19 19 20 20 $this->table = $table; 21 21 $this->importType = $importType; … … 25 25 break; 26 26 }//end switch 27 27 28 28 $this->table->db->logError = true; 29 29 //So, that, when there is a db error, it will go all the way through and not just stop 30 30 $this->table->db->stopOnError = false; 31 //Won't display db errors, just log them. 31 //Won't display db errors, just log them. 32 32 $this->table->db->showError = false; 33 33 if(isset($_POST["pageType"])) 34 34 $this->pageType = $_POST["pageType"]; 35 35 36 36 if(isset($_POST["tempFileID"])) 37 37 $this->tempFileID = ((int)$_POST["tempFileID"]); 38 38 39 39 }//end method --imports-- 40 40 41 41 function _parseFromData($data){ 42 42 43 43 switch($this->importType){ 44 44 45 45 case "csv": 46 46 47 47 if(is_readable($data)){ 48 48 $contents = $this->_getFile($data); 49 49 50 50 if(is_readable($contents)){ 51 51 $this->docError = "invalid csv document"; 52 52 return false; 53 53 }//end if 54 54 55 55 }//end if 56 56 57 57 $this->parser->parse($data); 58 58 59 59 if(!count($this->parser->titles) || !count($this->parser->data)){ 60 60 $this->docError = "invalid csv document"; 61 return false; 61 return false; 62 62 }//end if 63 63 64 64 return true; 65 65 break; 66 66 67 67 }//end swtich 68 68 69 69 }//end method --_parseFromFile-- 70 71 70 71 72 72 function _getTransactionData(){ 73 73 //needs to be changed for more complicated tables … … 75 75 foreach($this->transactionIDs as $theid) 76 76 $inStatement .= $theid.","; 77 77 78 78 if($inStatement){ 79 79 80 80 $inStatement = substr($inStatement, 0, -1); 81 81 82 82 $querystatement = " 83 83 SELECT … … 88 88 `id` IN (".$inStatement."); 89 89 "; 90 90 91 91 $queryresult = $this->table->db->query($querystatement); 92 92 93 93 while($therecord = $this->table->db->fetchArray($queryresult)) 94 94 $this->transactionRecords[] = $therecord; 95 96 }//end if 97 95 96 }//end if 97 98 98 }//end method --_getTransactionData-- 99 100 99 100 101 101 function _getFile($fileName){ 102 102 if(function_exists('file_get_contents')){ … … 106 106 $file = addslashes(fread(fopen($fileName, 'r'), filesize($fileName))); 107 107 }//end if 108 108 109 109 return $file; 110 110 }//end method --_getFile-- 111 111 112 112 //DO NOT CALL IN TRANSACTION 113 113 function _storeTempCSV($fileName){ 114 114 115 115 $querystatement = " 116 116 INSERT INTO … … 136 136 '".$_SESSION["userinfo"]["id"]."', 137 137 '".$_SESSION["userinfo"]["id"]."' 138 138 139 139 ) 140 140 "; 141 141 142 142 $this->table->db->query($querystatement); 143 143 144 144 $id = $this->table->db->insertId(); 145 145 146 146 if($id) 147 147 $this->tempFileID = ((int) $id); 148 148 else 149 149 $this->error .= '<li> inserting temporary file failure </li>'; 150 150 151 151 }//end method --_storeTempCSV-- 152 153 152 153 154 154 function _getTempCSV($tempFileID){ 155 155 156 156 if($tempFileID){ 157 157 158 158 $querystatement = " 159 159 SELECT … … 164 164 id = ".((int)$tempFileID)." 165 165 "; 166 166 167 167 $queryresult = $this->table->db->query($querystatement); 168 168 169 169 $therecord = $this->table->db->fetchArray($queryresult); 170 170 171 171 return $therecord["file"]; 172 172 173 173 }//end if 174 174 175 175 return false; 176 176 177 177 }//end method --_getTempCSV-- 178 178 179 179 //DO NOT CALL IN TRANSACTION 180 180 function _removeTempCSV($tempFileID = 0){ 181 181 182 182 $querystatement = " 183 183 DELETE FROM … … 192 192 ); 193 193 "; 194 194 195 195 $queryresult = $this->table->db->query($querystatement); 196 196 197 197 $querystatement = " 198 198 ALTER TABLE … … 200 200 AUTO_INCREMENT = ".((int) $tempFileID)."; 201 201 "; 202 202 203 203 $queryresult = $this->table->db->query($querystatement); 204 204 205 205 }//end method --_removeTempCSV-- 206 206 207 207 //DO NOT USE THIS METHOD INSIDE AN OPEN TRANSACTION. 208 208 //IT WILL AUTOMATICALLY COMMIT THE TRANSACTION 209 209 function _revertAutoIncrement($revertID = 0){ 210 210 211 211 //check to see if there is a revert id (i.e. there was a valid insert) 212 212 if($revertID) 213 213 if(is_numeric($revertID)){ 214 214 215 215 $querystatement = " 216 216 ALTER TABLE … … 218 218 AUTO_INCREMENT = ".((int) $revertID)."; 219 219 "; 220 220 221 221 $this->table->db->query($querystatement); 222 222 223 223 }//end if 224 224 225 225 }//end method --_revertAutoIncrement-- 226 227 226 227 228 228 function importRecords($rows, $titles){ 229 229 230 230 switch($this->importType){ 231 232 case "csv": 231 232 case "csv": 233 233 //count total fieldnames (top row of csv document) 234 234 $fieldNum = count($titles); 235 235 236 236 //the file starts at line number 1, but since line 1 is 237 237 //supposed to be the fieldnames in the table(s), the lines 238 238 //being insereted start @ 2. 239 239 $rowNum = 2; 240 240 241 241 //get the data one row at a time 242 242 foreach($rows as $rowData){ 243 244 $theid = 0; 245 243 244 $theid = 0; // set for when verifification does not pass 245 $verify = array(); //set for when number of field rows does not match number of titles 246 246 247 //trim off leading/trailing spaces 247 248 $trimmedRowData = array(); 249 248 250 foreach($rowData as $name => $data) 249 251 $trimmedRowData[$name] = trim($data); 250 252 251 253 //check to see if number of fieldnames is consistent for each row 252 254 $rowFieldNum = count($trimmedRowData); 253 255 254 256 //if valid, insert, if not, log error and don't insert. 255 if($rowFieldNum == $fieldNum) 256 $theid = $this->table->insertRecord($trimmedRowData); 257 else 257 if($rowFieldNum == $fieldNum){ 258 $verify = $this->table->verifyVariables($trimmedRowData); 259 if(!count($verify)) 260 $theid = $this->table->insertRecord($trimmedRowData); 261 }else 258 262 $this->error .= '<li> incorrect amount of fields for line number '.$rowNum.'.</li>'; 259 263 260 264 if($theid){ 261 265 //keep track of the ids in the transaction to be able to select them 262 266 //for preview purposes 263 267 $this->transactionIDs[] = $theid; 264 268 265 269 //get first id to correct auto increment 266 270 if(!$this->revertID) … … 268 272 }else 269 273 $this->error .= '<li> failed insert for line number '.$rowNum.'.</li>'; 270 274 275 foreach($verify as $error) 276 $this->error .= '<li class="subError">'.$error.'</li>'; 277 271 278 $rowNum++; 272 279 273 280 }//end foreach 274 281 break; 275 282 276 283 }//end switch 277 284 278 285 }//end method --importRecords-- 279 280 286 287 281 288 function displayTransaction($recordsArray, $fieldsArray){ 282 289 //needs to be changed for more complicated tables … … 317 324 <?php 318 325 }//end if 319 326 320 327 }//end method --displayTransaction-- 321 322 328 329 323 330 function processImportPage(){ 324 331 325 332 $this->table->getTableInfo(); 326 333 327 334 if(!isset($_POST["command"])){ 328 335 329 336 //happens upon first coming to page 330 337 331 338 //remove any other temporary csv files in the `files` table 332 339 //present from previous imports 333 340 $this->_removeTempCSV(); 334 341 335 342 //check to see if user has the rights to be here. 336 343 //If not, kick him to the no access page. 337 344 if(!hasRights($this->table->importroleid)) 338 345 goURL(APP_PATH."noaccess.php"); 339 346 340 347 }else{ 341 348 //form has been submitted 342 349 switch($_POST["command"]){ 343 350 344 351 //cancel button pressed. 345 352 case "cancel": … … 354 361 }//end if 355 362 break; 356 363 357 364 case "upload": 358 365 359 366 //check for valid file upload 360 367 if(!$_FILES["import"]["error"] && ($_FILES["import"]["size"] > 0)){ 361 368 362 369 //check and parse the file 363 370 if($this->_parseFromData($_FILES["import"]["tmp_name"])){ 364 371 365 372 //start transaction 366 373 $this->table->db->startTransaction(); 367 374 368 375 $this->importRecords($this->parser->data, $this->parser->titles); 369 376 370 377 //get data for preview purposes 371 378 $this->_getTransactionData(); 372 379 //"undo" any inserts 373 380 $this->table->db->rollbackTransaction(); 374 381 375 382 //DO NOT CALL IN TRANSACTION 376 383 //ALTER TABLES AUTO COMMIT AND THE FILE NEEDS TO CARRY … … 378 385 $this->_revertAutoIncrement($this->revertID); 379 386 $this->_storeTempCSV($_FILES["import"]["tmp_name"]); 380 387 381 388 }//end if 382 389 383 390 }else 384 391 $this->docError .= "failed file upload"; 385 392 386 393 //switch page types 387 394 $this->pageType = "confirm"; 388 395 389 396 if(!$this->error && !$this->docError){ 390 397 $therecord["phpbmsStatus"] = "Confirm Import"; … … 394 401 }else 395 402 $therecord["phpbmsStatus"] = "Import Error"; 396 403 397 404 break; 398 405 399 406 case "import": 400 407 401 408 //get the contents of the stored csv document 402 409 $CSVcontents = $this->_getTempCSV($this->tempFileID); 403 410 404 411 //parser uses newline character to be able to parse the last line 405 412 if(substr($CSVcontents,-1,1) != "\n") 406 413 $CSVcontents .= "\n"; 407 408 414 415 409 416 $this->parser->parse($CSVcontents); 410 417 411 418 $this->importRecords($this->parser->data, $this->parser->titles); 412 419 413 420 $this->table->db->commitTransaction(); 414 421 415 422 //DO NOT CALL IN TRANSACTION 416 423 417 424 //get rid of temporary csv document 418 425 $this->_removeTempCSV($this->tempFileID); 419 426 420 427 $therecord["phpbmsStatus"] = "Record(s) Imported"; 421 428 //change page type 422 429 $this->pageType = "main"; 423 430 break; 424 431 425 432 }//end command switch 426 433 427 434 }// end if 428 435 429 436 //display the title 430 437 $therecord["title"] = $this->table->displayname." Import"; 431 438 return $therecord; 432 439 433 440 }//end method --imports-- 434 441 435 442 }//end class --imports-- 436 437 443 444 438 445 //this class is to have different buttons, and no created/modified. 439 446 if(class_exists("phpbmsForm")){ 440 447 class importForm extends phpbmsForm{ 441 448 442 449 function importForm($action = NULL, $method="post", $name="record", $onsubmit="return validateForm(this);", $dontSubmit = true){ 443 450 444 451 parent::phpbmsForm($action,$method,$name,$onsubmit,$dontSubmit); 445 452 446 453 }//end method --importForm-- 447 448 function startForm($pageTitle, $pageType ){449 450 ?><form action="<?php echo str_replace("&","&",$this->action) ?>" method="<?php echo $this->method?>" name="<?php echo $this->name?>" onsubmit="<?php echo $this->onsubmit?>" <?php 454 455 function startForm($pageTitle, $pageType, $numberOfRecords = 0){ 456 457 ?><form action="<?php echo str_replace("&","&",$this->action) ?>" method="<?php echo $this->method?>" name="<?php echo $this->name?>" onsubmit="<?php echo $this->onsubmit?>" <?php 451 458 if(isset($this->enctype)) echo ' enctype="'.$this->enctype.'" '; 452 459 if(isset($this->id)) echo ' id="'.$this->id.'" '; 453 ?>><?php 460 ?>><?php 454 461 if($this->dontSubmit){ 455 462 ?><div id="dontSubmit"><input type="submit" value=" " onclick="return false;" /></div><?php 456 463 } ?> 457 <div id="topButtons"><?php $this->showButtons(1, $pageType ); ?></div>458 <h1 id="h1Title"><span><?php echo $pageTitle ?></span></h1><?php 459 464 <div id="topButtons"><?php $this->showButtons(1, $pageType, $numberOfRecords); ?></div> 465 <h1 id="h1Title"><span><?php echo $pageTitle ?></span></h1><?php 466 460 467 }//end method --startForm-- 461 462 function showButtons($ids = 1, $pageType = "main" ){468 469 function showButtons($ids = 1, $pageType = "main", $numberOfRecords = 0){ 463 470 ?> 464 471 <div class="importCancels"> 465 472 <?php if($pageType == "main"){ ?> 466 <input <?php if($ids==1) {?>accesskey=" i"<?php }?> title="Upload (alt+u)" id="uploadButton<?php echo $ids?>" name="command" type="submit" value="upload" class="Buttons" />473 <input <?php if($ids==1) {?>accesskey="u"<?php }?> title="Upload (alt+u)" id="uploadButton<?php echo $ids?>" name="command" type="submit" value="upload" class="Buttons" /> 467 474 <input id="cancelButton<?php echo $ids?>" name="command" type="submit" value="cancel" class="Buttons" <?php if($ids==1) {?>accesskey="x" <?php }?> title="(access key+x)" /> 468 475 <?php }else{?> 469 <input type="submit" class="Buttons" value="import" name="command" id="import<?php echo $ids?>" title="commit" />476 <input type="submit" class="Buttons" value="import" name="command" id="import<?php echo $ids?>" title="commit" <?php echo ($numberOfRecords? '':'disabled="disabled"') ?>/> 470 477 <input type="submit" class="Buttons" value="cancel" name="command" id="cancelButton<?php echo $ids?>" title="rollback"/> 471 478 <?php }//end if ?> 472 479 </div><?php 473 480 }//end method --showButtons-- 474 481 475 482 }//end class --importForm-- 476 483 }