.01
.02
.03
.04
.05
.06
.07
.08
.09
.10
.11
.12
.13
.14
.15
.16
.17
.18
.19
.20
.21
.22
.23
.24
.25
.26
.27
.28
.29
.30
.31
.32
.33
.34
.35
.36
.37
.38
.39
.40
.41
.42
.43
.44
.45
.46
.47
.48
.49
.50
.51
.52
.53
.54
.55
.56
.57
.58
.59
.60
.61
.62
.63
.64
.65
.66
.67
.68
.69
.70
.71
.72
.73
.74
.75
.76
.77
.78
.79
.80
.81
.82
.83
.84
.85
.86
.87
.88
.89
.90
.91
.92
.93
.94
.95
.96
.97
.98
.99
.100
.101
.102
.103
.104
.105
.106
.107
.108
.109
.110
.111
.112
.113
.114
.115
.116
.117
.118
.119
.120
.121
.122
.123
.124
.125
.126
.127
.128
.129
.130
.131
.132
.133
.134
.135
.136
.137
.138
.139
.140
.141
.142
.143
.144
.145
.146
.147
.148
.149
.150
.151
.152
.153
.154
.155
.156
.157
.158
.159
.160
.161
.162
.163
.164
.165
.166
.167
.168
.169
.170
.171
.172
.173
.174
.175
.176
.177
.178
.179
.180
.181
.182
.183
.184
.185
.186
.187
.188
.189
.190
.191
|
|
<?php
/******************************************************************************/
/* */
/* __ ____ */
/* ___ / / ___ / __/__ __ _____________ ___ */
/* / _ \/ _ \/ _ \_\ \/ _ \/ // / __/ __/ -_|_-< */
/* / .__/_//_/ .__/___/\___/\_,_/_/ \__/\__/___/ */
/* /_/ /_/ */
/* */
/* */
/******************************************************************************/
/* */
/* Titre : Classe Générique */
/* */
/* URL : http://www.phpsources.org/scripts432-PHP.htm */
/* Auteur : erwan */
/* Date édition : 23 Juil 2008 */
/* Website auteur : http://www.gallenne.fr */
/* */
/******************************************************************************/
abstract class Generique{
private $vars;
private $autoIncrement=false;
protected $idPDO;
//constructeur
public function __construct(){
$this->vars=array();
}
//fonctions basiques
public function __get($key){
return $this->vars[$key];
}
public function __set($key,$value){
return $this->vars[$key]=$value;
}
public function load($array){
$retour=true;
if(is_array($array)){
foreach($array as $key=>$value){
$retour = $retour && $this->vars[$key]=$value;
}
return $retour;
}
}
public function unload($keys=NULL){
if(is_null($keys)){
return $this->vars=array();
}elseif(is_array($keys)){
$retour=true;
foreach($keys as $key){
$retour = $retour && $this->unload($key);
}
}elseif(isset($this->vars[$keys])){
unset($this->vars[$keys]);
}
}
//autoincrement : méthode à appeler si la classe est associée à une table
// dont l'identifiant est défini par autoincrement
public function autoIncrement(){
$this->autoIncrement=true;
}
public function idPDO(&$idPDO){
$this->idPDO=$idPDO;
}
//fonctions de sauvegarde et de restauration pas session
public function setSession($nom){
return $_SESSION[$nom]=$this->vars;
}
public function getSession($nom){
return $this->load($_SESSION[$nom]);
}
//fonctions de sauvegarde et de restauration pas cookies
public function setCookie($nom){
foreach($this->vars as $key=>$value){
setCookie(get_class($this).'_'.$nom.'_'.$key,$value);
}
}
public function getCookie($nom){
foreach($_COOKIE as $key=>$value){
$nom=substr($key,strpos($key,'_',strpos($key,'_')+1)+1);
$this->__set($nom,$value);
}
}
//fonctions de sauvegarde et de restauration par bdd
public function setDB($table,$primaryKey){
$query='select count(*) as nb
from '.$table.'
where '.$primaryKey.'="'.$this->vars[$primaryKey].'"';
foreach($this->idPDO->query($query,PDO::FETCH_ASSOC) as $ligne){
$nb=$ligne['nb'];
}
if($nb>0){
$query='update '.$table.' set ';
foreach($this->vars as $key=>$value){
if($key!=$primaryKey){
$query.=' '.$key.'="'.$value.'", ';
}
}
$query=substr($query,0,-2);
$query.=' where '.$primaryKey.'="'.$this->vars[$primaryKey].'"';
return $this->idPDO->exec($query);
}else{
if($this->autoIncrement){
foreach($this->idPDO->query('SHOW TABLE STATUS
like \''.$table.'\'',PDO::FETCH_ASSOC)
as $ligne){
$this->vars[$primaryKey]=$ligne['Auto_increment'];
}
}
$query='insert into '.$table.'('.implode(' ,',array_keys($this->vars)).')
values ("'.implode('","',array_values($this->vars)).'")';
$resultat=$this->idPDO->exec($query);
return $resultat;
}
}
//affecte les attributs à partir des données en base
public function getDB($table,$primaryKey){
$query='select *
from '.$table.'
where '.$primaryKey.'="'.$this->vars[$primaryKey].'"';
$result=$this->idPDO->query($query,PDO::FETCH_ASSOC);
if(sizeof($result)>0){
foreach($result as $ligne){
$this->load($ligne);
}
return true;
}else{
return false;
}
}
//definit les attributs à partir de la variable POST
//(à utiliser pou récupérer les données d'un formulaire
public function setPOST(){
foreach($_POST as $key=>$value){
if(isset($this->vars[$key])){
$this->vars[$key]=$value;
}
}
}
//fonctions de debug
public function debug(){
echo '<hr /> Class : '.get_class($this).'<br />';
foreach($this->vars as $key=>$value){
echo $key.'=>'.$value.'<br />';
}
}
//convertit la classe en XML
public function toXML(){
$xml='';
$xml.='<'.get_class($this).'>'."\n";
foreach($this->vars as $key=>$value){
$xml.='<'.$key.'>';
if(is_object($value)){
$xml.=$value->toXML();
}elseif(is_array($value)){
$xml.=serialize($value);
}else{
$xml.=$value;
}
$xml.='</'.$key.'>'."\n";
}
$xml.='</'.get_class($this).'>'."\n";
return $xml;
}
//fonction pour afficher l'objet dans un tableau
public function toTable($visible=array(),$pre='',$post=''){
$text='<tr>'.$pre;
$cols=array_keys($visible);
foreach($this->vars as $key=>$value){
if(in_array($key,$cols)){
$text.='<td>';
switch($visible[$key]){
case 'url':
$text.='<a href="'.$value.'" target="_blank">URL</a>';
break;
default:
$text.=htmlentities($value);
break;
}
$text.= '</td>';
}
}
$text.=$post.'</tr>';
return $text;
}
}
?>
|