Location: PHPKode > projects > Toutateam Groupware > toutateam-v1.56/outsync/outsync.class.php
<?php

class outsync_crossref extends mosDBTable
{
	var $ID;
	var $type;
	var $data_local_ID;
	var $data_foreign_ID;
	var $user_ID;
	
	var $_debug=false;
	var $_tablename="outsync_cross_ref";
	var $_Types=array("calendar","tasks","notes","contacts");
	var $_TablesFromType=array(	"calendar"=>"termine",
							"notes"=>"notes",
							"tasks"=>"todo",
							"contacts"=>"contacts",
							"cleancalendar"=>"termine",
							"cleannotes"=>"notes",
							"cleantasks"=>"todo",
							"cleancontacts"=>"contacts");
    function outsync_crossref($database,$theDebug=false)
    {
    	$this->_debug=$theDebug;
    	$this->mosDBTable( $this->_tablename, 'ID', $database );
    }
    
    
	function checkRecurAppointmentInterval($myCurRes)
	{
	    if($myCurRes->div2=="recur" && $myCurRes->div1='-1')
		{
			$myAppOptions=unserialize($myCurRes->options1);
			if($myAppOptions===false)
				return false;
			$myAppEnd=-1;
			if(isset($myAppOptions->endday) && isset($myAppOptions->endmonth) && isset($myAppOptions->endyear))
			{
				if($myAppOptions->endday>0 && $myAppOptions->endmonth>0 && $myAppOptions->endyear>0)
				{
		        	$myAppEnd=strtotime($myAppOptions->endyear."-".$myAppOptions->endmonth."-".$myAppOptions->endday);
				}
			}
			if($myAppEnd==false || $myAppEnd==-1)
				return false;
				
	    	$myIntevalBeginTime=strtotime($myCurRes->intervalbegin);
	    	$myIntevalEndTime=strtotime($myCurRes->intervalend);
	    	$myAppBegin=strtotime($myCurRes->datum);
	    	//echo "Comparaison dates d/f / id/if : ".$myAppBegin."/".$myAppEnd." / ".$myIntevalBeginTime."/".$myIntevalEndTime."<br>";
	    	if	( 
	        		!($myAppBegin<=$myIntevalEndTime && $myAppBegin>=$myIntevalBeginTime)
	        		&&
	        		!($myAppEnd<=$myIntevalEndTime && $myAppEnd>=$myIntevalBeginTime)
	    		)
	    		return false;
		}
		return true;
	}

    function cleanOrphans($theUserId=-1,$theType="")
    {
    	$debug=false;
    	if($debug) echo __FUNCTION__."@".__LINE__."=> Début avec ".$theUserId." et ".$theType."<br>";
    	$myParseTypes=$this->_Types;
    	if($theUserId>0)
    		$myQueryUser=" AND ".$this->_tablename.".user_ID=".intval($theUserId)." ";
    	else
    		$myQueryUser="";
    	if($debug) echo __FUNCTION__."@".__LINE__."=> Filtre utilisateur : ".$myQueryUser."<br>";
    	if($theType!="")
    	{
    		if(in_array($theType,$this->_Types))
    			$myParseTypes=array($theType);
    		else
    			$myParseTypes=array();
    	}
    	if($debug) echo __FUNCTION__."@".__LINE__."=> Types nettoyés : ".Tools::Display($myParseTypes)."<br>";
    	if(is_array($myParseTypes) && count($myParseTypes)>0)
    	{
	    	
    		foreach($myParseTypes as $curtype)
    		{
    			$curdatatable=$this->_TablesFromType[$curtype];
    			$myQueryType=" AND ".$this->_tablename.".type='".$curtype."' ";
		    	$myQuery="SELECT ".$this->_tablename.".* FROM ".$this->_tablename." LEFT JOIN ".$curdatatable." ON ".$curdatatable.".ID=".$this->_tablename.".data_local_ID WHERE ".$curdatatable.".ID IS NULL AND (".$this->_tablename.".data_foreign_ID IS NULL OR ".$this->_tablename.".data_foreign_ID='') ".$myQueryUser.$myQueryType;
		    	if($debug) echo __FUNCTION__."@".__LINE__."=> Requête de récupération des enregistrements : ".$myQuery."<br>";
		    	$this->_db->setQuery($myQuery);
		    	$myListTypes=$this->_db->loadObjectList();
		    	$myArrayCleanId="";
		    	if($debug) echo __FUNCTION__."@".__LINE__."=> Nb enregistrements : ".count($myListTypes)."<br>";
		    	if(is_array($myListTypes) && count($myListTypes)>0)
		    		foreach($myListTypes as $curdbtype)
		    		{
				    	//if($debug) echo __FUNCTION__."@".__LINE__."=> Ajout de : ".Tools::Display($curdbtype)."<br>";
		    			$myArrayCleanId.=(($myArrayCleanId!="")?",":"").$curdbtype->ID;
		    		}
		    	if($debug) echo __FUNCTION__."@".__LINE__."=> Nettoyage de : ".$myArrayCleanId."<br>";
	    		if($myArrayCleanId!="")
	    		{
	    			$this->_db->setQuery("DELETE FROM ".$this->_tablename." WHERE ID IN (".$myArrayCleanId.")");
			    	if($debug) echo __FUNCTION__."@".__LINE__."=> Requete Nettoyage : ".$this->_db->getQuery()."<br>";
	    			$this->_db->query();
	    		}
    		}
    	}
    }
    
    function storeRef($theUserId,$theType,$theObjectID,$theOuIdent)
    {
    	$debug=false  && $this->_debug;
    	unset($this->ID);
    	if($debug) echo "Démarrage stockage, recherche : ".$theUserId.",".$theType.",".$theObjectID."  ...<br />\n";
    	$this->searchRef($theUserId, $theType,$theObjectID);
    	if($debug) echo "Ref trouvée : ".$this->ID." ...<br />\n";
    	$this->user_ID=$theUserId;
    	$this->type=$theType;
    	$this->data_local_ID=$theObjectID;
    	$this->data_foreign_ID=$theOuIdent;
    	if($debug) echo "Stockage ...<br />\n";
    	$this->store();
    	if($debug) echo "Ref stockage : ".$this->ID." ...<br />\n";
    }
    
    function searchRef($theUserID, $theType,$theID)
    {
    	$debug=false && $this->_debug;
    	$myQuery="SELECT * FROM ".$this->_tablename." WHERE user_ID=".$theUserID." AND data_local_ID=".$theID." AND `type`='".$theType."';";
		if($debug) echo "searchRef : requête : ".$myQuery."<br />\n";
    	$this->_db->setQuery($myQuery);
    	$myObj=null;
    	if($this->_db->loadObject($myObj))
    	{
			if($debug) Tools::Trace( "searchRef : ID trouvée : ".$this->ID."<br />\n");
    		return $this->load($myObj->ID);
    	}
    	return false;
    }
    
    function getSingleRecords($theUserId,$theType,$theTable)
    {
    	$this->_db->setQuery("SELECT o.data_local_ID as ttid,o.data_foreign_ID as ouid
			FROM outsync_cross_ref as o
			LEFT JOIN ".$theTable." as t
			ON o.data_local_ID=t.ID 
			WHERE o.user_ID =".$theUserId." AND o.type='".$theType."';");
		$myListSingleRecords=$this->_db->loadObjectList();
		if(count($myListSingleRecords)<=0)
			return false;
		return $myListSingleRecords;
    }
    
    function deleteRefForUser($theUserId,$theType,$theLocalId,$theIdType="local",$isForeignKey=false)
    {
    	$myWhereQry="";
    	if($isForeignKey)
    	{
    		$myWhereQry=" AND data_foreign_ID LIKE '' ";
    	}
    	$myKey=($theIdType!="local")?"data_foreign_ID":"data_local_ID";
    	$this->_db->setQuery("DELETE FROM ".$this->_tablename." WHERE user_ID=".$theUserId." AND `type`='".$theType."' AND ".$myKey."='".$theLocalId."' ".$myWhereQry.";");
    	Tools::Trace(__FUNCTION__."@".__LINE__."==> Requête d'effacement : ".$this->_db->getQuery());
    	$myReturn=$this->_db->query();
    	return $myReturn;
    }
}
?>
Return current item: Toutateam Groupware