Location: PHPKode > projects > Php simple acces data mysql > phpsimpledb_v2/includes/framework/objects/objects.php
<?php
	class DBobjects{
		var $_sPath_projects = '';
		var $_oProjet = NULL;
		
		function DBobjects( $oProjet ){
			$this->_sPath_projects = PATH_BASE.'projects'.DIRECTORY_SEPARATOR;
			$this->_oProjet = $oProjet;
		}
		
		//sauvegarde les objets d'un projet
		function setObjects( $sName, $oObjects ){
			//generation du fichier
			$sData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<datas>\n";
			if( is_array( $oObjects ) && count( $oObjects ) > 0 ){
				$sData .= "\t<objects>\n";
				foreach( $oObjects as $oObject ){
					$sData .= "\t\t<object type=\"".$oObject->sType."\">\n".
						"\t\t\t<table><![CDATA[".$oObject->sTable."]]></table>\n".
						"\t\t\t<name><![CDATA[".$oObject->sName."]]></name>\n".
						"\t\t\t<tagname><![CDATA[".$oObject->sTagname."]]></tagname>\n".
						"\t\t\t<desc><![CDATA[".$oObject->sDesc."]]></desc>\n".
						"\t\t\t<fields>\n";
					if( is_array( $oObject->oFields ) && count( $oObject->oFields ) > 0 )
						foreach( $oObject->oFields as $oField )
							$sData .= "\t\t\t\t<field type=\"".$oField->sTypeField.'"'.( $oField->sTypeField == 'object' ? ' object="'.$oField->sLinkObject.'"' : '' ).">\n".
								"\t\t\t\t\t<name><![CDATA[".$oField->sName."]]></name>\n".
								"\t\t\t\t\t<type><![CDATA[".$oField->sType."]]></type>\n".
								"\t\t\t\t\t<key><![CDATA[".$oField->sKey."]]></key>\n".
								"\t\t\t\t\t<interclassement><![CDATA[".$oField->sInterclassement."]]></interclassement>\n".
								"\t\t\t\t\t<attributs><![CDATA[".$oField->sAttributs."]]></attributs>\n".
								"\t\t\t\t\t<null><![CDATA[".$oField->sNull."]]></null>\n".
								"\t\t\t\t\t<defaut><![CDATA[".$oField->sDefaut."]]></defaut>\n".
								"\t\t\t\t\t<extra><![CDATA[".$oField->sExtra."]]></extra>\n".
								"\t\t\t\t</field>\n";
					$sData .= "\t\t\t</fields>\n".
						"\t\t</object>\n";
				}
				$sData .= "\t</objects>\n";
			}
			$sData .= "</datas>";
			
			//ecriture du fichier des objets
			file_put_contents( $this->_sPath_projects.$sName.DIRECTORY_SEPARATOR.'objets.xml', $sData );
		}
		
		//recupere les objects d'un projet
		function getObjects( $sName ){
			if( !file_exists( $this->_sPath_projects.$sName.DIRECTORY_SEPARATOR.'objets.xml' ) )
				return array();
			
			$oObjects = array();
			
			//lecture du fichier xml du projet
			$oXml = new SimpleXMLElement( file_get_contents( $this->_sPath_projects.$sName.DIRECTORY_SEPARATOR.'objets.xml' ) );
			if( $oXml ){
				$oResult = $oXml->xpath( '/datas/objects/object' );
				while( list( , $oNode ) = each( $oResult ) ){
	    			$oObject = (object)NULL;
	    			$oObject->sType = (string)$oNode[ 'type' ];
	    			$oObject->sTable = (string)$oNode->table;
	    			$oObject->sName = (string)$oNode->name;
	    			$oObject->sTagname = (string)$oNode->tagname;
	    			$oObject->sDesc = (string)$oNode->desc;
	    			$oObject->oFields = array();
	    			
	    			foreach( $oNode->fields->children() as $oNodeField ){
	    				$oField = (object)NULL;
	    				$oField->sTypeField = (string)$oNodeField[ 'type' ];
	    				$oField->sLinkObject = $oField->sTypeField == 'object' ? (string)$oNodeField[ 'object' ] : '';
	    				$oField->sName = (string)$oNodeField->name;
	    				$oField->sType = (string)$oNodeField->type;
	    				$oField->sKey = (string)$oNodeField->key;
	    				$oField->sInterclassement = (string)$oNodeField->interclassement;
	    				$oField->sAttributs = (string)$oNodeField->attributs;
	    				$oField->sNull = (string)$oNodeField->null;
	    				$oField->sDefaut = (string)$oNodeField->defaut;
	    				$oField->sExtra = (string)$oNodeField->extra;
	    				$oObject->oFields[] = $oField;
	    			}
	    			$oObjects[] = $oObject;
				}
			}
			return $oObjects;
		}
		
		//recupere un object d'un projet
		function getObject( $sNameObject, $sNameProject ){
			$oObjects = $this->getObjects( $sNameProject );
			foreach( $oObjects as $oObject )
				if( $oObject->sName == $sNameObject )
					return $oObject;
			return NULL;
		}
		
		//recupere un champ d'un object d'un projet
		function getFieldByObjectProject( $sProject, $sObject, $sField ){
			$oProjects = $this->getObjects( $sProject );
			foreach( $oProjects as $oProject )
				if( $oProject->sName == $sObject )
					foreach( $oProject->oFields as $oField )
						if( $oField->sName == $sField )
							return $oField;
			return NULL;
		}
		
		//recuperation de l'object d'une table
		function getObjectFromTable( $sName ){
			$oBD = new PDO( $this->_oProjet->sChaineConnexion, $this->_oProjet->sUser, $this->_oProjet->sPass );
			
			$oObject = (object)NULL;
			$oObject->sType = 'table';
			$oObject->sTable = $sName;
			$oObject->sName = $sName;
			$oObject->sTagname = $sName;
			$oObject->sDesc = '';
			$oObject->oFields = array();

			//recuperation des données des tables pour les stocker dans les objets associés
			foreach( $oBD->query( 'SHOW COLUMNS FROM '.$sName ) as $oRow ){
				$oField = (object)NULL;
				$oField->sTypeField = 'bdd';
				$oField->sLinkObject = '';
				$oField->sName = $oRow[ 'Field' ];
				$oField->sType = $oRow[ 'Type' ];
				$oField->sKey = $oRow[ 'Key' ];
				$oField->sInterclassement = '';
				$oField->sAttributs = '';
				$oField->sNull = $oRow[ 'Null' ];
				$oField->sDefaut = $oRow[ 'Default' ];
				$oField->sExtra = $oRow[ 'Extra' ];
				$oObject->oFields[] = $oField;
				
				//Field 	Type 	Null 	Key 	Default 	Extra
			}
				
			return $oObject;
		}
		
		//mise a jour du detail d'un objet
		function updateObjectDetail( $sNameProject, $sNameObject, $oDetail ){
			$oObjects = $this->getObjects( $sNameProject );
			foreach( $oObjects as $oObject )
				if( $oObject->sName == $sNameObject ){
					foreach( $oDetail as $sParam=>$sValue )
						$oObject->$sParam = $sValue;
					break;
				}
			//enregistrement des objets
			$this->setObjects( $sNameProject, $oObjects );
		}
		
		//recupere tous les objets avec des identifiants
		function getObjectsGUID( $sNameProject ){
			$oObjectsGUID = array();
			$oObjects = $this->getObjects( $sNameProject );
			foreach( $oObjects as $oObject )
				foreach( $oObject->oFields as $oField )
				if( $oField->sKey == 'PRI' ){
					$oObjectsGUID[] = $oObject;
					break;	
				}
			return $oObjectsGUID;
		}
		
		//mise a jour d'un champ d'un objet
		function updateFieldObject( $sNameProject, $sNameObject, $sNameField, $oDetail ){
			$oObjects = $this->getObjects( $sNameProject );
			foreach( $oObjects as $oObject )
				if( $oObject->sName == $sNameObject )
					foreach( $oObject->oFields as $oField )
						if( $oField->sName == $sNameField ){
							foreach( $oDetail as $sParam=>$sValue )
								$oField->$sParam = $sValue;
							break;
						}
			//enregistrement des objets
			$this->setObjects( $sNameProject, $oObjects );
		}
		
		//supprime un objet du projet
		function deleteObject( $sNameProject, $sNameObject ){
			$oObjects = $this->getObjects( $sNameProject );
			$oNew_objects = array();
			foreach( $oObjects as $oObject )
				if( $oObject->sName != $sNameObject )
					$oNew_objects[] = $oObject;
			//enregistrement des objets
			$this->setObjects( $sNameProject, $oNew_objects );
		}
		
		//supprime tous les objets du projet
		function deleteAllObjects( $sNameProject ){
			$this->setObjects( $sNameProject, array() );
		}
	}
?>
Return current item: Php simple acces data mysql