<?php


class CRM_LISTA_ZASOBOW_ACL {
	
	//@2014-02-05
	//funkcja do optymalizacji struktury wzajemnej powiazan zasobow budujaca dwukierunkowo
	//CRM_LISTA_ZASOBOW_ACL <> CRM_LISTA_ZASOBOW
	// CRM_LISTA_ZASOBOW as t1 -> CRM_LISTA_ZASOBOW_ACL.RELATION=PARENT_ID_ACCESS/PARENT_ID_MAP
	// CRM_LISTA_ZASOBOW_ACL.RELATION=PARENT_ID_ACCESS/PARENT_ID_MAP -> CRM_LISTA_ZASOBOW
	// by triggers
	function INIT_TABLE() {		
		$sql="
CREATE TABLE IF NOT EXISTS `CRM_LISTA_ZASOBOW_ACL` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `SRC_ID` int(11) NOT NULL,
  `DST_ID` int(11) NOT NULL,
  `RELATION` enum('PARENT_ID_ACCESS','PARENT_ID_MAP') NOT NULL,
  `A_STATUS` enum('NORMAL','WAITING','OFF_HARD','DELETED') NOT NULL DEFAULT 'NORMAL',
  `A_RECORD_UPDATE_DATE` varchar(40) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `SRC_ID` (`SRC_ID`,`DST_ID`,`RELATION`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin2 COMMENT='powiazania wzajemne zasobow ze soba optymalizujace relacje' AUTO_INCREMENT=1 ;
		";
		DB::query($sql);
		
		
		
		$sql="CREATE TRIGGER `CRM_LISTA_ZASOBOW_ACL_INSERT` AFTER INSERT ON `CRM_LISTA_ZASOBOW_ACL`
		FOR EACH ROW update CRM_LISTA_ZASOBOW set PARENT_ID_MAP=
		(select group_concat(cast(DST_ID as CHAR)) from CRM_LISTA_ZASOBOW_ACL
		where SRC_ID=NEW.SRC_ID and A_STATUS='NORMAL' and RELATION='PARENT_ID_MAP'
		)
		, PARENT_ID_ACCESS=(select group_concat(cast(DST_ID as CHAR)) from CRM_LISTA_ZASOBOW_ACL
		where SRC_ID=NEW.SRC_ID and A_STATUS='NORMAL' and RELATION='PARENT_ID_ACCESS'
		)
		where ID=NEW.SRC_ID
		";
		DB::query($sql);
		$sql="CREATE TRIGGER `CRM_LISTA_ZASOBOW_ACL_UPDATE` AFTER UPDATE ON `CRM_LISTA_ZASOBOW_ACL`
		FOR EACH ROW update CRM_LISTA_ZASOBOW set PARENT_ID_MAP=
		(select group_concat(cast(DST_ID as CHAR)) from CRM_LISTA_ZASOBOW_ACL
		where SRC_ID=NEW.SRC_ID and A_STATUS='NORMAL' and RELATION='PARENT_ID_MAP'
		)
		, PARENT_ID_ACCESS=(select group_concat(cast(DST_ID as CHAR)) from CRM_LISTA_ZASOBOW_ACL
		where SRC_ID=NEW.SRC_ID and A_STATUS='NORMAL' and RELATION='PARENT_ID_ACCESS'
		)
		where ID=NEW.SRC_ID
		";
		DB::query($sql);

		

		
		
		
	}
	
	
	function REFRESH_TABLE() {
	//firstly insert from CRM_LISTA_ZASOBOW to CRM_LISTA_ZASOBOW_ACL
	$sql="select ID,PARENT_ID_ACCESS,PARENT_ID_MAP,A_RECORD_UPDATE_DATE from CRM_LISTA_ZASOBOW where PARENT_ID_ACCESS>0 or PARENT_ID_MAP>0  ";
	$res=DB::query($sql);
			while($h=DB::fetch($res)) {
				echo "<hr>\n";
				$PARENT_ID_ACCESS=array(); $PARENT_ID_MAP=array();
				if(!empty($h->PARENT_ID_ACCESS)) $PARENT_ID_ACCESS=explode(',', $h->PARENT_ID_ACCESS);
				if(!empty($h->PARENT_ID_MAP)) $PARENT_ID_MAP=explode(',', $h->PARENT_ID_MAP);
				$sql="select max(A_RECORD_UPDATE_DATE) as A_RECORD_UPDATE_DATE from CRM_LISTA_ZASOBOW_ACL where SRC_ID='{$h->ID}'";
				 $res_date=DB::query($sql);
				 if(DB::num_rows($res_date)==0 || $h->A_RECORD_UPDATE_DATE>DB::fetch($res_date)->A_RECORD_UPDATE_DATE) {
					  echo "Numrows:".DB::num_rows($res_date);
					 print_r($PARENT_ID_MAP);
					 	foreach($PARENT_ID_MAP as $DST_ID) {
						 	$sql="insert into CRM_LISTA_ZASOBOW_ACL (SRC_ID,DST_ID,RELATION,A_STATUS,A_RECORD_UPDATE_DATE) values ({$h->ID} , {$DST_ID},'PARENT_ID_MAP','NORMAL','{$h->A_RECORD_UPDATE_DATE}' )";
						 	echo "<br> ".$sql;
						 	DB::query($sql);
						}
						foreach($PARENT_ID_ACCESS as $DST_ID) {
						 	$sql="insert into CRM_LISTA_ZASOBOW_ACL (SRC_ID,DST_ID,RELATION,A_STATUS,A_RECORD_UPDATE_DATE) values ({$h->ID} , {$DST_ID},'PARENT_ID_ACCESS','NORMAL','{$h->A_RECORD_UPDATE_DATE}' )";
						 	echo "<br> ".$sql;
						 	DB::query($sql);
						}
					// die();
				 }
				
			}
		
	}
	
	
	function INTRODUCE_SELF() {
		
		//Tutaj powinna sie ona zarejestrowac jako pLUGIN
		$sql="insert into _PLUGINS_OPTIONS_MANAGER.... some infos about options and myself";
	}
	
	function INSTALL() {
		
		self::INIT_TABLE();
		self::REFRESH_TABLE()
		self::INTRODUCE_SELF();
	}
	
}






function CRM_LISTA_ZASOBOW_ACL() {
	
	
	CRM_LISTA_ZASOBOW_ACL::INIT_TABLE();
//	CRM_LISTA_ZASOBOW_ACL::REFRESH_TABLE();

		
	
}

?>