<?php
/* 
 * phpMyVisites : website statistics and audience measurements
 * Copyright (C) 2002 - 2006
 * http://www.phpmyvisites.net/ 
 * phpMyVisites is free software (license GNU/GPL)
 * Authors : phpMyVisites team
*/

// $Id: ViewReferers.class.php 29 2006-08-18 07:35:21Z matthieu_ $


require_once INCLUDE_PATH."/core/include/ViewModule.class.php";
require_once INCLUDE_PATH."/libs/Xml/Serializer.php";

class ViewPagesDetailsOne extends ViewModule
{
    var $viewTemplate = "pdo/viewpagesdetailsone.tpl";
    
    /**
     * Constructor
     */
	function ViewPagesDetailsOne()
	{
		parent::ViewModule( "pages");
	}
	
	
	function returnQueryXML ($p_query, $p_query2 = "") {
		$r = query($p_query);
		$allElem = array();
		while($l = mysql_fetch_assoc($r))
		{
			$l['idcategory'] = "c".$l['idcategory'];
			$allElem[] = $l;
		}
		if ($p_query2 != "") {
			$r = query($p_query2);
			while($l = mysql_fetch_assoc($r))
			{
				if (isPrefixTag($l['name'])) {
					$l['idpage'] = "f".$l['idpage'];
				}
				else {
					$l['idpage'] = "p".$l['idpage'];
				}
				$allElem[] = $l;
			}
		}
		return $allElem;
	}
	
	function returnAllElemXML ($allElem) {
		// An array of serializer options
		$serializer_options = array (
			    'addDecl' => TRUE,
			    'encoding' => 'UTF-8',
			    'indent' => '  ',
			    'rootName' => 'ViewPagesElem',
			    'defaultTagName' => 'item',
			    'rootAttributes' => array ( 'version' => '1.0'),
		);
			
		$Serializer = &new XML_Serializer($serializer_options);
		// Serialize the data structure
		$Serializer->setOption("keyAttribute", "rdf:about");
		
		$status = $Serializer->serialize($allElem);
		
		$allData =  $Serializer->getSerializedData();
		
		// Display the XML document
		header('Content-type: text/html');
		print($allData);
		exit;
	}
	
	function loadSubCategory ($p_cat) {
		// Serach category
		$v_cat = substr($p_cat, 1);
		$v_query = "SELECT idcategory, complete_name, name, level, idparent FROM ".T_CATEGORY." WHERE idparent = '$v_cat'";
		$r = query($v_query);
		$allElem = array();
		$trouve = false;
		while($l = mysql_fetch_assoc($r))
		{
			//$trouve = true;
			$l['idcategory'] = "c".$l['idcategory'];
			$allElem[] = $l;
		}
		if (! $trouve) {
			// Search pages
			$r = query("SELECT idpage, idcategory, name  FROM ".T_PAGE." WHERE idcategory = '$v_cat'");
			while($l = mysql_fetch_assoc($r))
			{
				if (isPrefixTag($l['name'])) {
					$l['idpage'] = "f".$l['idpage'];
				}
				else {
					$l['idpage'] = "p".$l['idpage'];
				}
				
				$allElem[] = $l;
			}
		}
		$this->returnAllElemXML($allElem);
	}
	
	function constructData ($tabStatVal, $nbElem, $returnData) {
		$idLast = $tabStatVal[$nbElem-1];
		//print "IdLast : $idLast<br>\n";
		$key = "";
		for ($i = 0; $i < $nbElem-1; $i ++) {
			if ($i != "") {
				$key .= ">";
			}
			$key .= $tabStatVal[$i];
		}
		$tmpData = $this->data->getPagesZoom($key);
		//print "PageZoom : $key<br>\n";

		$returnData ['dates'] = $tmpData['sum'][0];
		$idSearch = $idLast;
		if(($nbElem > 1) && (substr($idSearch, 0, 1) === 'c')) {
			$idSearch = $tabStatVal[$nbElem-2].">".$idSearch;
		}
		//print "IdSearch : $idSearch<br>\n";
		$trouve = false;
		foreach($tmpData['sum'][1] as $info) {
			if ($info['id'] == $idSearch) {
				$trouve = true;
				$returnData [$idLast]['sum'] = $info;
			}
		}
		if (! $trouve) {
			//var_dump($tmpData);
		}
		return $returnData;
	}

	function getName ($key) {
		$ret = "";
		if (substr($key, 0, 1)== "c") {
			$r = query("SELECT complete_name FROM ".T_CATEGORY." WHERE idcategory = '".substr($key, 1)."'");
			if($l = mysql_fetch_assoc($r)) {
				$ret = $l['complete_name'];
			}
		}
		else {
			$r = query("SELECT name FROM ".T_PAGE." WHERE idpage = '".substr($key, 1)."'");
			if($l = mysql_fetch_assoc($r)) {
				$ret = $l['name'];
			}
		}
		return $ret;
	}
	/**
	 * Data processing method
	 * only called if template is not cached
	 */
	function process()
	{
		$selectCat = getRequestVar ('cat', "", 'string');
		$statVal = getRequestVar ('stat', "", 'string');
		$initKey = getRequestVar ('init', "", 'string');
		if ($initKey != "") {
			$tabInitKey = split(",", $initKey);
			$this->tpl->assign("InitKey", $tabInitKey);
			// Search lib for each key
			$tabInitLib = array();
			foreach ($tabInitKey as $key) {
				$tabInitLib[] = $this->getName ($key);
			}
			$this->tpl->assign("InitLib", $tabInitLib);
		}
		elseif ($statVal != "") {
			$methods = array(
			  "zoom" => array('')
			);
			$this->getDataMethod( $methods );
			
			$tabStatVal = split(",", $statVal);
			$nbElem = count($tabStatVal);
			$returnData = array();
			for ($i = 1; $i <= $nbElem; $i++) {
				$returnData = $this->constructData ($tabStatVal, $i, $returnData);
			}
			$this->returnAllElemXML($returnData);
			
/*
			$this->getDataMethod( $methods );
			$tmpData = $this->data->getPagesZoom("");
			var_dump ($tmpData);
			exit;
*/
/*
			$tempData = unserialize(uncompress($this->data->infoSerialized['vis_pag_grp'],1));
			$tabStatVal = split(",", $statVal);
			$returnData = $tempData;
			foreach ($tabStatVal as $key) {
				if (isset ($returnData[$key])) {
					$returnData = $returnData[$key];
				}
				else {
					$returnData = null;
					break;
				}
			}
			if (isset($returnData["p_pmv_sum"])) {
//				$this->returnAllElemXML($returnData["p_pmv_sum"]);
				$this->returnAllElemXML($returnData);
			}
			else {
				$this->returnAllElemXML($returnData);
			}
			//var_dump($tempData);
			//print "fin";
			//exit;
			exit;
*/
		}
		elseif ($selectCat != "") {
			if ($selectCat == "-1") {
				$this->returnAllElemXML(
						$this->returnQueryXML("SELECT idcategory, complete_name, name, level, idparent FROM ".T_CATEGORY." WHERE level=1",
									"SELECT idpage, idcategory, name  FROM ".T_PAGE." WHERE idcategory = 0"));
			}
			else {
				$this->loadSubCategory ($selectCat);				
			}
			
			//$this->returnXML ("SELECT idcategory, complete_name, name, level, idparent FROM ".T_CATEGORY." WHERE level = 3");
			//$this->returnXML ("SELECT idpage, idcategory, name  FROM ".T_PAGE." WHERE idcategory = 0");
			//$this->returnXML ("SELECT idcategory, complete_name, name, level, idparent FROM ".T_CATEGORY." WHERE level = 3");
		}
		else {
		
			// Select first categories
			$allElem = $this->returnQueryXML("SELECT idcategory, complete_name, name, level, idparent FROM ".T_CATEGORY." WHERE level=1",
									"SELECT idpage, idcategory, name  FROM ".T_PAGE." WHERE idcategory = 0");
			$this->tpl->assign("FirstCategoryList", $allElem);
		}		
		
		
/*

		$a_dataToLoad = array(
				
				// tables
				'searchengines' 		=> 'refererssearchengines',
				'searchenginesinterest' => 'refererssearchenginesinterest',
				'keywords'				=> 'refererskeywords',
				'keywordsinterest' 		=> 'refererskeywordsinterest',
				'sites' 				=> 'refererssites',
				'sitesinterest' 		=> 'refererssitesinterest',
				'partners' 				=> 'refererspartners',
				'partnersinterest'		=> 'refererspartnersinterest',
				'newsletters' 			=> 'referersnewsletters',
				'newslettersinterest' 	=> 'referersnewslettersinterest',
				'typeinterest' 			=> 'refererstypeinterest',
				
			);
			
				
		$o_mod = new ViewDataArray( null );
		$o_mod->init($this->request);//, $this->tpl);
		
		foreach($a_dataToLoad as $key => $value)
		{
			//printTime('before'.$key, true);
			$this->tpl->assign( $key, $o_mod->showAll( $value , true, true ));
		}

		$this->request->setModuleName('view_referers');
		
		$methodsToLoad = array(
			"numbers" => array()
		);
		$this->getDataMethod( $methodsToLoad );
*/
	}
}
?>