<?php

/**************************************
** MClassa za XML - MYSQL pretvorbo	 **
** Author: Dave @ zabec.net	       **
** Date: 2007 						       **
**************************************/

 /* Class za export XMLja iz mysql baze */
/****************************************/
class exportXML
{
	var $mysql;
	function exportXML($mysql)
	{
		$this->mysql = $mysql;
		$this->tabela = "tabela";
		$this->vrstica = "vrstica";
		$this->append = array();
	}
	function createXML($sql)
	{
		$doc = new DOMDocument('1.0', 'utf-8');
		$doc->formatOutput = true;

		//generiramo root node
		$root = $doc->createElement($this->tabela);
		$root = $doc->appendChild($root);

		//generiramo polja iz databaze
		$result = $this->mysql->query($sql);
		while($r = $result->fetchTextOnly())
		{
			$vrstica = $doc->createElement($this->vrstica);
			foreach ($r as $key => $value)
			{
				$polje = $doc->createElement($key);
				$polje = $vrstica->appendChild($polje);
				//dodamo dodaten tekst če je nastavljen
				if($this->append[$key]) {
					$append = $this->append[$key][0];
				}
				else {
					$append = "";
				}
				$vsebina_polja = $doc->createTextNode($append.$value);
				$vsebina_polja = $polje->appendChild($vsebina_polja);
			}
			$vrstica = $root->appendChild($vrstica);
		}
		$this->dom = $doc;
	}

	function setTableName($tabela)
	{
		$this->tabela = $tabela;
	}
	function setVrstica($vrstica)
	{
		$this->vrstica = $vrstica;
	}
	//funkcija doda tekst k določeni spremenljivki
	function appendText($polje, $text)
	{
		$this->append[$polje] = array($text);
	}

	function display()
	{
		return $this->dom->saveXML();
	}

	function save($file)
	{
		$this->dom->save($file);
	}
}

/* Class za import XMLja v bazo */
/********************************/
class importXML
{
	var $mysql;
	function importXML($mysql)
	{
		$this->mysql = $mysql;
	}

	function import($file, $tabela)
	{
		$dom = new DomDocument;
		$dom->load($file);
		$this->dom = $dom;

		$this->tabela = $tabela;


		$dom = $this->dom;
		$nodeList = $dom->getElementsByTagName("vrstica");
		$this->nodeList = $nodeList;
		if ($this->preveriCeObstajaTabela() AND $this->preveriPravilnostTabele())
		{
			$this->vstaviXMLtoMYSQL();
		}
		else
		{
			die("Tabela z imenom $this->tabela ne obstaja ali nima pravilnih polj!");
		}
	}


	function izprazni($tabela)
	{
		$this->mysql->query("TRUNCATE TABLE $tabela");
	}

	private function vstaviXMLtoMYSQL()
	{
		$nodeList = $this->nodeList;

		$size = $nodeList->length;
		for ($x = 0; $x<$size; $x++)
		{
			$node = $nodeList->item($x)->childNodes;
			$nodesize = $node->length;
			$this->vrsticaSQL = "";
			for ($i = 0; $i<$nodesize; $i++)
			{
				if ($node->item($i)->nodeType == 1)
				{
					$this->vrsticaSQL .= "'".$node->item($i)->nodeValue."', ";
				}
			}
			$this->vrsticaSQL = substr($this->vrsticaSQL, 0, -2);

			$sql = "INSERT INTO ".$this->tabela." (".$this->stolpciSQL.") VALUES (".$this->vrsticaSQL.")";
			$this->mysql->query($sql);
		}
	}

	private function getStolpci()
	{
		$nodeList = $this->nodeList;
		$firstnode = $nodeList->item(0)->childNodes;
		$size = $firstnode->length;
		$this->stolpciSQL = "";
		for ($i = 0; $i<$size; $i++)
		{
			if ($firstnode->item($i)->nodeType == 1)
			{
				$stolpci[] = $firstnode->item($i)->nodeName;
				$this->stolpciSQL .= $firstnode->item($i)->nodeName.", ";
			}
		}
		$this->stolpci = $stolpci;
		$this->stolpciSQL = substr($this->stolpciSQL, 0, -2);
	}

	private function preveriPravilnostTabele()
	{
		$this->getStolpci();
		$result = $this->mysql->query("SHOW columns FROM $this->tabela");
		while($r = $result->fetch())
		{
			if (!in_array($r[0],$this->stolpci))
			{
				$error = 1;
			}
		}
		if ($error == 1)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	private function preveriCeObstajaTabela()
	{
		$result = $this->mysql->query("SHOW TABLES");

		while($r = $result->fetch())
		{
			$baza[] = $r[0];
		}
		if (in_array($this->tabela,$baza))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

class exportXMLtoArray {

	function ReturnData($vsebina){
	}
}


/*****************************************/
//Primer uporabe IMPORTA iz XMLja v Mysql//
/*****************************************/
//$export = new exportXML($mysql);

/* Nastavimo importXML class (zahteva mysql povezavo iz lib_mysql.php classa */
//$import = new importXML($mysql);

/* Import xml datoteke v tabelo (ta mora že obstajati) */
//$import->import("ime_polja.xml", "ime_tabele");

/* Ukaz za izpraznjenje tabele (opcijsko)*/
//$import->izprazni("test");

/***************************************/
//Primer uporabe EXPORTA iz Mysql v XML//
/***************************************/
/* Nastavimo exportXML class (zahteva mysql povezavo iz lib_mysql.php classa */
//$export = new exportXML($db);

/* Nastavimo ime tabele v xmlju (opcijsko, default je tabela)*/
//$export->setTableName("goodies");
//$export->setVrstica("goodie");
//$export->appendText("picture", "/images/projekti/slike/nas_flash/");
//$export->appendText("url", $settings['site_url']."slo/projekti/karneki/");

/* Export iz tabele v XML (preko imena polj v SQL stavku določimo katere podatke želimo) */
//$export->createXML("SELECT slika_flash AS picture, naslov AS title, naslovka_kratekopis AS description, naslov_link AS url FROM modul_projekti AS p LEFT JOIN modul_projekti_slo AS d ON p.id = d.item_id WHERE na_naslovko = '1'");

/* Izpišemo XML vsebino v brskalnik (opcijsko)*/
//echo $export->display();

/* Shranimo XML vsebino v datoteko (opcijsko)*/
//$export->save("pekarija.xml");

?>