Jump to content

Import new data, change changed data and move deleted data via Script


iNoize
 Share

Recommended Posts

Hello, 

i try to write an Import Script. 

Some basic skills i've learned from different postst. But its not really enough so i try to start a new Topic. 

I have an zip file with xml and images inside. 

The different objects have three different actions   "ADD | CHANGE | DELETE"

so the Idea is at the ADD Action to check is the page ID I use for the ID the Object Number alredy exist. 

If not create page 

if the Change Action than check if ID exist grab this Page and write new values 

if Delete Action then Grab this ID if exist and change Parent. 

IMPORTANT: After all I have to delete the Zip file and all the TEMP Files.  How to handle in the right way temporary fils with processwire ? Maybe i dont need the external extract Script ? 

This is my current CODE: 

	<?php

	$userRolesSelector = 'superuser';
	// bootstrap ProcessWire. Update the path in the include if this script is not in the same dir
	include("./index.php"); 

	if( ! wire('user')->hasRole($userRolesSelector)) {
		header('HTTP/1.1 403 Forbidden');
		exit(1);
	}



	 include "import/op_file.php"; // Different functions to handle the zip file EXTRACT Read etc....

	// here some (pseudo/example)code
	 set_time_limit( intval(60 * 5) );  // give it 10 minutes, you may also increase this
	echo "<pre>";
	readXML();

	exit;

	//********************************************


	/** -----------------------------------------------------------------
	* @desc	Finde XML/ZIP, entpacken und einlesen der daten
	*/

	 function readXML()  {

		$opfile = new OPfile();

		// dbg is a wrapper for "echo",  so you can ignore it via one Flag  in op_tools
		
		echo "DOC ROOT: ".$_SERVER['DOCUMENT_ROOT'];

		// Set your path here ...
		$ropa=dirname($opfile->GetTheRoot())."/";		//opfile
		$ropa=$ropa . "demo/";
		echo "ROOT: ".$ropa;
		$modpa=$ropa;
		$projpa=$ropa;

		$origfolderfrom=$ropa."import/ftpin/";
		$ftptemp=$ropa."import/temp/";
		$folderfrom=$origfolderfrom;

		 // Find the ZIP/XML File in the FTP Folder
		 // $fileinfo will be used by following functions. you can add more information if needed

		 $fileinfo= $opfile ->GetUpload($origfolderfrom );  // return = assoc-array
		 $fileinfo ['temp']=$ftptemp;
		 $fileinfo ['folder']=$origfolderfrom ;

		 // Extract or Copy the Files, so we can prozess the xml info
		 if ($fileinfo['type'] =='zip') {
			 // if it is ZIP , extract it to temp
			$xmlfilename= $opfile ->ExtractZIP($fileinfo);

			echo "<p> XML ist:".$xmlfilename."</p>";
			echo "<p>Kopiere: " .$fileinfo ['folder'].$xmlfilename ." <br> nach: ". $ftptemp.$xmlfilename."</p>";
		 }else{
			// if it is XML  copy it to temp
			$xmlfilename = $fileinfo['file'];
			copy ($fileinfo ['folder'].$xmlfilename   ,   $ftptemp.$xmlfilename);
			echo "<p>Kopiere: " .$fileinfo ['folder'].$xmlfilename ." nach: ". $ftptemp.$xmlfilename."</p>";
		}//if

		// XML File from temp folder

		$stat = $opfile ->LoadXML($ftptemp.$xmlfilename);
		$path = $ftptemp.$xmlfilename;
		if (file_exists($path)) {
	   		$xml = simplexml_load_file($path);

	  	  echo "<p>Erfolgreich geladen <br></p>";
	  	 $nodeList = $xml->xpath('//immobilie');
		
			}



		// you can access the XML Object by:  $opfile ->XMLdata;

		if ($stat == true) {
			// read basic infos form XML File
			$xinfo= $opfile->decodeUTF8($opfile ->XMLinfo());
	      //  print_r($xinfo);
	        echo "Objekte im Import: ".$xinfo['anzimmo']."<br >";
	        echo "Anbieter ID: ".$xinfo['anbieterid']."<br >";

		}else {
			// Error in Processing. Stop the run.
			$xinfo['anzanbieter'] =0 ;
			$xinfo['anzimmo'] =0;
			echo "Fehler In der XML Datei";

			// exit can be put away. The For Loop does not run with azi=0
			exit;

		}//if

	 



		$azi=intval($xinfo['anzimmo']); // get the number of Objects in archive 
	   


	// save our start time, so we can find which pages should be removed
	$started = time();

	// keep track of how many changes we've made so we can report at the end
	$numChanged = 0;
	$numAdded = 0;
	$numTrashed = 0;

	// the parent page of our items: /about/what/ is a page from the basic profile
	// update this to be whatever parent you want it to populate...
	$parent = wire('pages')->get('/about/');
	if(!$parent->id) throw new WireException("Parent page does not exist"); 

	for ($ni=1; $ni<=$azi;) {  //$xinfo['anzimmo']

	  echo "<h2>=======Object=".$ni."======</h2>";
	  $idata = $opfile->GetOneImmo( $ni, $xinfo);  // $idata stores all the values in the Array for current Object

	  $action=$idata['verwaltung_techn'] ['aktion']['aktionart'];
			if ($action =='') {
				$action="ADD";
			}//if

	  $page = wire('pages')->get($idata['verwaltung_techn']['objektnr_extern']);

	   if($action == 'ADD') {

	   	if(!$page->id) {
	    $page = new Page();
	    $page->parent = $parent;
	    $page->template = 'immo'; // template new pages should use
	    $page->name = $idata['freitexte']['objekttitel'];
	    echo "Adding new page:".$idata['verwaltung_techn']['objektnr_extern']." Titel ".$idata['freitexte']['objekttitel']." <br>";
	    $numAdded++;
	  				}

	//  	print_r($idata);

	    // now populate our page fields from data in the feed
	  $page->of(false);  // ensure output formatting is off
	  $page->title = $idata['freitexte']['objekttitel']; 
	  $page->summary = $idata['freitexte']['dreizeiler'];
	  $body = "<h2>".$idata['freitexte']['dreizeiler']."</h2>";
	  $page->body = $body;
	   			
	   			}

	 if($action == 'CHANGE') {

	 	echo "CHANGE";

	    if(!$page->id) {
	    $page = new Page();
	    $page->parent = $parent;
	    $page->template = 'immo'; // template new pages should use
	    $page->name = $idata['freitexte']['objekttitel'];
	    echo "<h2>Change page data:</h2>".$idata['verwaltung_techn']['objektnr_extern']." Titel ".$idata['freitexte']['objekttitel']." <br><br>";
	     $numChanged++;
	  				}
			}

	    if($action == 'DELETE') {

	 	echo "DELETE";
	     $numTrashed++;
	    foreach($changes as $change) echo "\nUpdated '$change' on page: $page->name";
	   			
	   			}
	 
	   $ni++;

			}//for Object

	echo "\n\n$numAdded page(s) were added";
	echo "\n$numChanged page(s) were changed";
	echo "\n$numTrashed page(s) were trashed\n";

	 }//funktion



	?>
Link to comment
Share on other sites

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...