Location: PHPKode > projects > Reportmaker > reportmaker_1.0/processreport.class.php
<?php
class processreport {

	private $report;
	private $db;
	private $xml_report;
	private $query_parameters;

	function __construct($report, $db,$query_parameters=array()) {

		$this->report = $report;
		$this->db = $db;
		$this->query_parameters=$query_parameters;
		

	}

	function debuginfo($info) {

		if (DEBUG) {
			//	echo "<p>$info</p>";
		}
	}

	function process() {

		$this->debuginfo("process");

		$xml_report = new DOMDocument();
		$xml_report->loadXML($this->report);

		$this->xml_report = & $xml_report;

		$sections = $xml_report->getElementsByTagName('section');

		for ($i = 0; $i < $sections->length; $i++) {
			$this->process_section($sections->item($i));
		}

		return $xml_report;

	}

	function process_section(& $section) {

		$this->debuginfo("process_section");

		$data_xml = $section->getElementsByTagName('data');

		if ($data_xml->length <= 0) {
			throw new Exception("Data section invalida");
		}

		$this->process_section_data($data_xml->item(0));
	}
	
	/**
	 * @param the query that will be updated
	 * @param array of keys
	 * */
	function update_query($query){
		
		$chiavi=array_keys($this->query_parameters);

		foreach($chiavi as $chiave){
			$query=str_replace($chiave, $this->query_parameters[$chiave], $query);
		}
		
		return $query;
	}

	function process_section_data($data) {

		$xml = $data->getElementsByTagName("xml");
		
		if($xml->length==0){
			//creo io l'elemento xml vuoto
			$elemento_xml=$this->xml_report->createElement('xml');
			$data->appendChild($elemento_xml);
			$xml = $data->getElementsByTagName("xml");
		}
		
		$xml=$xml->item(0);

		$records = $xml->getElementsByTagName("record");

		/*
		 * se esiste, eseguo la query
		 * */
		$query_xml = $data->getElementsByTagName("query");
		if ($query_xml->length >0 && strlen($query_xml->item(0)->nodeValue) > 0) {

			/*
			 * elimino eventuali records
			 * */

			while ($records->length > 0) {
				$xml->removeChild($records->item(0));
			}
			
			$str_query=$this->update_query($query_xml->item(0)->nodeValue);

			$res = $this->db->Execute($str_query);

			while ($rs = $this->db->Fetch($res)) {
				//echo "<pre>".print_r($rs,true)."</pre>";

				$labels = array_keys($rs);

				$record = $this->xml_report->createElement('record');

				for ($i = 1; $i < sizeof($labels); $i += 2) {

					$field = $this->xml_report->createElement($labels[$i], $rs[$labels[$i]]);
					$record->appendChild($field);
				}
				$xml->appendChild($record);

			}
		}

		/*
		 * TODO
		 * se esiste, applico XPATH al contenuto del report
		 * */
		$xpath_element = $data->getElementsByTagName("xpath");

		if ($xpath_element->length == 1 && strlen($xpath_element->item(0)->nodeValue)>0) {
			$query = $xpath_element->item(0)->nodeValue;

			$xpath = new DOMXPath($this->xml_report);

			//eseguo la query
			$entries = $xpath->query($query);

			/*
			 * svuoto la nodelist
			 * */
			while ($records->length > 0) {
				$xml->removeChild($records->item(0));
			}

			/*
			 * inserisco gli elementi che soddifano la query
			 * */
			for ($i = 0; $i < $entries->length; $i++) {
				$xml->appendChild($entries->item($i));
			}

		}

	}
	
}
?>
Return current item: Reportmaker