<?php
include ("entContact.php" );
include ("entVille.php" );
class entEntreprise
{
private $TabContacts;
private $Taille;
private $Siret;
private $Nom;
private $Secteur;
private $Tel;
private $Fax;
private $AdrNum;
private $AdrRue;
private $AdrComp;
private $NomVille;
private $CodePostal;
function __construct($Siret, $Nom, $Secteur, $Tel, $Fax, $AdrNum, $AdrRue, $AdrComp, $NomVille, $CodePostal)
{
// initialisation du TabContacts
$this->TabContacts =
array();
$this->Taille = 0;
$this->Siret = $Siret;
$this->Nom = $Nom;
$this->Secteur = $Secteur;
$this->Tel = $Tel;
$this->Fax = $Fax;
$this->AdrNum = $AdrNum;
$this->AdrRue = $AdrRue;
$this->AdrComp = $AdrComp;
$this->NomVille = $NomVille;
$this->CodePostal = $CodePostal;
}
//**************************
// Ascesseurs
//**************************
public function getSiret()
{
return $this->Siret;
}
public function getTaille()
{
//return $this->Taille;
return sizeof($this->TabContacts
);
}
private function existeVille($NomVille, $CodePostal)
// Fonction qui recherche l'existence d'un enregistrement ville ds la BDD
// retourne False si erreur d'execution de requete, -1 si pas de résultat, l'id de l'enregistrement sinon
{
$Req = "SELECT IdVille FROM Ville WHERE NomVille='".$NomVille."' AND CodePostal='".$CodePostal."' ";
if ( $Res=adaptateuMySQL::getInstance()->selectQuery($Req) == FALSE)
{ return FALSE; }
if ( $Res=adaptateuMySQL::getInstance()->selectQuery($Req) == -1)
{ return -1; }
else
{
//-------------------------------------------------
//nouvelle ville saisie existe ds la base de donnée
$Id= $Res[0][0]; // resultat unique ds la1ere case du premier tableau
return $Id;
}
}
function createVille($NomVille, $CodePostal)
// Fonction qui créée une nouvelle ville ds la BDD
// retourne TRUE si la ville estcrée, FALSE sinon
{
$Req=" INSERT INTO Ville (NomVille, CodePostal)
VALUES ('".$this->NomVille."', '".$this->CodePostal."') ";
return (adaptateuMySQL::getInstance()->executeQuery($Req) );
}
function modifEntreprise($Siret, $Nom, $Secteur, $Tel, $Fax, $AdrNum, $AdrRue, $AdrComp, $NomVille, $CodePostal)
// Methode qui permet de modifier les infrmation d'une entreprise: ds l'objet et ds la BDD
// retourne TRUE si modif effectuée, FALSE sinon
{
$modifVille=0;
if ( ( $NomVille != $this->NomVille) || ( $CodePostal != $this->CodePostal) )
{ $modifVille=1; }
//*********************************
// requete de mise à jour de la BDD (en premier car source d'erreur)
//*********************************
//--------------------------------------------------------------------------------------------------------------------------
if ( $modifVille==0 ) // pas de modif ville, requete uniquement sur infos de Entreprise
{
$Req = "UPDATE Entreprise SET
(SIRET = '".$Siret."', Nom = '".$Nom."', Secteur = '".$Secteur."',
Tel = '".$Tel."', Fax = '".$Fax."', AdrNum = '".$AdrNum."',
AdrRue = '".$AdrRue."', AdrComp = '".$AdrComp."')";
if(!adaptateuMySQL::getInstance()->executeQuery($Req))
{ return FALSE;}
}
//--------------------------------------------------------------------------------------------------------------------------
else // modification de la ville:
// * test ville existe (cp+Nom),
{ // * Si existe, modifie juste l'id ds la table entreprise
// * sinon, création d'un new enregistrement Ville, puis recup de l'id de cet enreg
// (auto incremente) , puis update entreprise avec new ID
if ( $Id=existeVille($this->NomVille, $this->CodePostal) == FALSE)
{ return FALSE; }
if ( $Id=existeVille($this->NomVille, $this->CodePostal) != -1)
{
//-------------------------------------------------
//nouvelle ville saisie existe ds la base de donnée
$Req = "UPDATE Entreprise SET
(SIRET = '".$Siret."', Nom = '".Nom."', Secteur = '".$Secteur."',
Tel = '".$Tel."', Fax = '".$Fax."', AdrNum = '".$AdrNum."',
AdrRue = '".$AdrRue."', AdrComp = '".$AdrComp."', IdVille = '".$Id."')";
if(!adaptateuMySQL::getInstance()->executeQuery($Req))
{ return FALSE;}
}
else
{
//-------------------------------------------------------
//nouvelle ville saisie n'existe pas ds la base de donnée
if( createVlle($this->NomVille, $this->CodePostal) ) //création ville
{
if ( $Id=existeVille($this->NomVille, $this->CodePostal) == FALSE)
{ return FALSE; }
if ( $Id=existeVille($this->NomVille, $this->CodePostal) != -1) // recup new ID
{
$Req = "UPDATE Entreprise SET
(SIRET = '".$Siret."', Nom = '".$Nom."', Secteur = '".$Secteur."',
Tel = '".$Tel."', Fax = '".$Fax."', AdrNum = '".$AdrNum."',
AdrRue = '".$AdrRue."', AdrComp = '".$AdrComp."', IdVille = '".$Id."')";
if(!adaptateuMySQL::getInstance()->executeQuery($Req)) // MAJ entreprise avec new id ville
{ return FALSE;}
}
else{ return FALSE;}
}
else{ return FALSE;}
}
}
//****************************************
// mise à jour des attributs de l instance
//****************************************
$this->Siret = $Siret;
$this->Nom = $Nom;
$this->Secteur = $Secteur;
$this->Tel = $Tel;
$this->Fax = $Fax;
$this->AdrNum = $AdrNum;
$this->AdrRue = $AdrRue;
$this->AdrComp = $AdrComp;
$this->NomVille=$NomVille;
$this->CodePostal = $CodePostal;
return TRUE;
}
public function addEntreprise()
// fonction qui ajoute une entreprise ds la base de données
// retourne TRUE si l'ajout est effectué, FALSE sinon
{
if ( $Id=existeVille($this->NomVille, $this->CodePostal) == FALSE)
{ return FALSE; }
if ( $Id=existeVille($this->NomVille, $this->CodePostal) != -1)
{
// ville existe recup ID
$Req =" INSERT INTO Entreprise
VALUES (SIRET = '".$this->Siret."', Nom = '".$this->Nom."', Secteur = '".$this->Secteur."',
Tel = '".$this->Tel."', Fax = '".$this->Fax."', AdrNum = '".$this->AdrNum."',
AdrRue = '".$this->AdrRue."', AdrComp = '".$this->AdrComp."', IdVille = '".$Id."')";
if(!adaptateuMySQL::getInstance()->executeQuery($Req))
{return false;}
}
else
{
//ville a créer+recup Id+ inserer new entreprise
if( createVlle($this->NomVille, $this->CodePostal) ) //création ville
{
if ( $Id=existeVille($this->NomVille, $this->CodePostal) == FALSE)
{ return FALSE; }
if ( $Id=existeVille($this->NomVille, $this->CodePostal) != -1) // recup new ID
{
$Req =" INSERT INTO Entreprise
VALUES (Siret='".$this->Siret."', Nom='".$this->Nom."', Secteur='".$this->Secteur."',
Tel='".$this->Tel."', Fax='".$this->Fax."', AdrNum='".$this->AdrNum."',
AdrRue='".$this->AdrRue."', AdrComp='".$this->AdrComp."', IdVille='".$Id."')";
if(!adaptateuMySQL::getInstance()->executeQuery($Req))
{return false;}
}
else{ return FALSE;}
}
else{ return FALSE;}
}
return TRUE;
}
public function delEntreprise()
// Fonction qui vasupprimer une entreprise ds la base de donnees
// returne TRUE si l'entreprise est supprimée, FALSE sinon
{
$Req = "SELECT COUNT(E.IdVille) FROM Entreprise E, Ville V
WHERE E.IdVille=V.IdVille
AND V.NomVille='".$this->NomVille."'
AND V.CodePostal= '".$this->CodePostal."' ";
if($Res=adaptateuMySQL::getInstance()->selectQuery($Req) ==FALSE)
{ return FALSE; }
if($Res=adaptateuMySQL::getInstance()->selectQuery($Req) != -1)
{
if ( ($Res[0][0]) < 2 ) // ville non referencée ou uniquement par l'entreprise en cours d effacement
{
// efface ville
$Req = "DELETE FROM Ville
WHERE IdVille=( SELECT IdVille FROM Entreprise
WHERE Siret='".$this->Siret."')
";
if(!adaptateuMySQL::getInstance()->executeQuery($Req))
{return FALSE;}
// efface entreprise
$Req = "DELETE FROM Entreprise WHERE SIRET = '".$this->Siret."'";
if(adaptateuMySQL::getInstance()->executeQuery($Req))
{return TRUE; }
else{return FALSE;}
}
else // ville utilisee par d'autre entreprises
{
//efface entreprise
$Req = "DELETE FROM Entreprise WHERE SIRET = '".$this->Siret."'";
if(adaptateuMySQL::getInstance()->executeQuery($Req))
{return TRUE; }
else{return FALSE;}
}
}
else {return FALSE;}
return TRUE;
}
// fonctions pour les contacts
public function createTabContacts()
{
// requete
$Req = "SELECT * FROM Contact WHERE Siret = '".$this->Siret."'";
$Res = adaptateuMySQL::getInstance()->selectQuery($Req);
foreach ($Res as $ligne)
{
$this->TabContacts[$this->Taille] = new entContact($ligne[0], $ligne[1], $ligne[2], $ligne[3], $ligne[4], $ligne[5]);
$this->Taille = $this->Taille + 1;
}
}
public function addContact($id, $n, $pren, $email, $t, $p)
{
$cont = new Contact($id, $n, $pren, $email, $t, $p);
$cont.addContact();
$this->Taille = $this->Taille + 1;
$this->TabContacts[$this->Taille] = $cont;
}
public function chercheContact($id)
{
$cpt = 0;
while (($this->TabContacts[$cpt].getId() != $id) && ($cpt < $this->Taille))
{
$cpt = $cpt + 1;
}
if ($cpt == $this->Taille)
return -1;
else
return $cpt;
}
public function delContact($id)
{
if (($cpt = chercheContact($id)) == -1)
return false;
else
{
$this->TabContacts[$cpt].delContact(); // enlève le contact de la BD
unset($this->TabContacts
[$cpt]);
// détruit l'objet contact
// décalage de la fin du tableau
while ($cpt < ($this->Taille - 1))
{
$this->TabContacts[$cpt] = $this->TabContacts[$cpt + 1];
$cpt = $cpt + 1;
}
$this->Taille = $this->Taille - 1;
return true;
}
}
public function modifContact($id, $n, $pren, $email, $t, $p)
{
if (($cpt = chercheContact($id)) == -1)
return false;
else
{
$this->TabContacts[$cpt].modifContact(); // modifie le contact dans la BD
return true;
}
}
}
?>