Ozone Griffox
CLASSES.PHP
<?php
// Acces à la base de donnée
class DB
{
var $db, $ok;
function DB($dbhost,$dbuser,$dbpass,$dbname)
{
$this->disconnect();
$this->db = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$this->db || !mysql_select_db($dbname, $this->db)) die("database error!\n");
$this->ok=true;
}
function disconnect()
{
if ($this->db) mysql_close($this->db);
$this->ok=false;
}
function query($query)
{
if($this->ok) return mysql_query($query, $this->db);
}
}
// Gestion automatique des formulaires
class FORMS
{
function LISTS($name, $db, $text, $value, $query, $default)
{
$rec=$db->query($query);
$result=mysql_fetch_assoc($rec);
$ret='<select name="'.$name.'">'.LF;
while($result)
{
$ret.='<option';
if ($value) $ret.=' value="'.$result[$value].'"';
if ($result[$text]==$default or $result[$value]==$default) $ret.=' selected';
$ret.='>'.$result[$text].'</option>'.LF;
$result=mysql_fetch_assoc($rec);
}
$ret.='</select>'.LF;
return $ret;
}
function CHECKBOXES($name, $format, $db, $field_code, $text, $checked, $query)
{
$rec=$db->query($query);
$result=mysql_fetch_assoc($rec);
$ret='';
while($result)
{
$ret.='<input name="'.$name.$result[$field_code].'" type="checkbox"'.$checked[$result[$field_code]].'>&'.$result[$text].$format.LF;
$result=mysql_fetch_assoc($rec);
}
return $ret;
}
}
// Vues d'interfaçe à la base de donnée
class VIEWS
{
function ALLERGY()
{
$ret='SELECT * FROM ref_allergy ORDER BY all_lib';
return $ret;
}
function CHECK_CNT_ALLERGY($db, $contact, $field_code)
{
$query='SELECT * FROM cnt_allergy WHERE cal_k_contact='.$contact;
$rec=$db->query($query);
$result=mysql_fetch_assoc($rec);
while($result)
{
$ret[$result[$field_code]]=' checked';
$result=mysql_fetch_assoc($rec);
}
return $ret;
}
function COUNTRY()
{
return 'SELECT * FROM ref_country ORDER BY ctr_lib';
}
function GENDER()
{
return 'SELECT * FROM ref_gender ORDER BY gen_lib';
}
function PAY_TYPE()
{
return 'SELECT * FROM ref_pay_type ORDER BY pty_lib';
}
}
// Gestion du fichier Log
class LOGS
{
var $loghandle, $ok, $ln;
function open()
{
if (!$this->ok) $handle=fopen(LOGNAME, 'a');
if ($handle) {
$this->loghandle=$handle;
$this->ok=true;
$this->ln=true;
}
}
function write($var,$ln)
{
if (!$this->ok) $this->open();
if ($var and $this->ok) {
if (gettype($var)=='array') {
foreach($var as $str) $strlog.=$str.', ';
} else {
$strlog=$var;
}
if ($ln) $strlog.=LF;
fputs($this->loghandle, IIF($this->ln,date('[Y/m/d-h:m] ').$_SERVER['REMOTE_HOST'].' ','').$strlog);
$this->ln=$ln;
}
}
}
// Gestion des données des contacts
class CONTACTS
{
var $db_contact, $db_address, $db_cnt_allergy, $db_email, $db_memos, $db_paid;
function check_form($formpost)
{
if ($formpost['param_last_name']=='') $ERROR['last_name']='Le nom est requis';
if ($formpost['param_first_name']=='') $ERROR['first_name']='Le prénom est requis';
if ($formpost['param_address1']=='' and $formpost['param_address2']=='' and $formpost['param_address3']=='') $ERROR['address']="L'adresse est requise";
if ($formpost['param_zip']=='') $ERROR['zip']='Le code postal est requis';
if ($formpost['param_city']=='') $ERROR['city']='La ville est requise';
if ($formpost['param_country']=='') $ERROR['country']='Le pays est requis';
if ($formpost['param_email']=='' or strpos($formpost['param_email'],'@')==0 or strpos($formpost['param_email'],'.')==0) $ERROR['email']="Un email valide est requise";
if ($formpost['param_gender']=='') $ERROR['gender']='Le sexe est requis (ouch)';
if ($formpost['param_nick']=='') $ERROR['nick']='Le surnom est requis';
if ($formpost['param_password']=='') $ERROR['password']='Le mot de passe est requis';
if ($formpost['param_pay_type']=='') $ERROR['pay_type']='Le type de paiment est requis';
if (!CHECK_FRDATE($formpost['param_birthday']) or $formpost['param_birthday']=='01/01/1900') $ERROR['birthday']='La date de naissance est requise au bon format';
if (!CHECK_FRDATE($formpost['param_arrival'])) $ERROR['arrival']="La date d'arrivée est requis au bon format";
if (!CHECK_FRDATE($formpost['param_departure'])) $ERROR['departure']='La date de départ est requise au bon format';
return $ERROR;
}
function set_k_contact($k_contact)
{
$this->db_contact['K_CONTACT'] =$k_contact;
$this->db_address['ADR_K_CONTACT'] =$k_contact;
$this->db_email['EML_K_CONTACT'] =$k_contact;
$this->db_memos['MEM_K_CONTACT'] =$k_contact;
}
function set_from_form($db, $views, $formpost)
{
$ERROR=$this->check_form($formpost);
if (!$ERROR) {
$this->set_k_contact($formpost['K_CONTACT']);
$this->db_contact['CNT_K_CONVENTION'] =K_CONVENTION;
$this->db_contact['CNT_NICK'] =$formpost['param_nick'];
$this->db_contact['CNT_PASSWORD'] =crypt($formpost['param_password'],CRYPT_KEY);
$this->db_contact['CNT_LASTNAME'] =$formpost['param_last_name'];
$this->db_contact['CNT_FIRSTNAME'] =$formpost['param_first_name'];
$this->db_contact['CNT_BIRTHDAY'] =MYSQL_FRDATE($formpost['param_birthday']);
$this->db_contact['CNT_ARRIVAL'] =MYSQL_FRDATE($formpost['param_arrival']);
$this->db_contact['CNT_DEPARTURE'] =MYSQL_FRDATE($formpost['param_departure']);
$this->db_contact['CNT_K_GENDER'] =$formpost['param_gender'];
$this->db_contact['CNT_TOPAY'] =$formpost['param_topay'];
$this->db_contact['CNT_CREATE_DATE'] =date('Y-m-d');
$this->db_address['K_ADDRESS'] =NULL;
$this->db_address['ADR_LINE1'] =$formpost['param_address1'];
$this->db_address['ADR_LINE2'] =$formpost['param_address2'];
$this->db_address['ADR_LINE3'] =$formpost['param_address3'];
$this->db_address['ADR_ZIP'] =$formpost['param_zip'];
$this->db_address['ADR_CITY'] =$formpost['param_city'];
$this->db_address['ADR_PHONE'] =$formpost['param_phone'];
$this->db_address['ADR_K_COUNTRY'] =$formpost['param_country'];
$this->db_address['ADR_CREATE_DATE'] =date('Y-m-d');
$this->db_address['ADR_PREFERED'] =NULL;
$this->db_email['K_EMAIL'] =NULL;
$this->db_email['EML_EMAIL'] =$formpost['param_email'];
$this->db_email['EML_CREATE_DATE'] =date('Y-m-d');
$this->db_email['EML_PREFERED'] =NULL;
$this->db_memos['K_MEMOS'] =NULL;
$this->db_memos['MEM_CREATE_DATE'] =date('Y-m-d');
$this->db_memos['MEM_NOTES'] =str_replace(CRLF,LF,$formpost['param_user_comments']);
$this->db_memos['MEM_OBJECTS'] =str_replace(CRLF,LF,$formpost['param_user_objects']);
$this->db_memos['MEM_ACTIVITIES'] =str_replace(CRLF,LF,$formpost['param_user_activities']);
$this->$db_paid=NULL;
}
return $ERROR;
}
function insert($db, $table, $db_fields)
{
foreach ($db_fields as $field=>$value)
{
if (substr($field,0,2)!='K_') {
$fields.=$field.',';
$values.="'".$value."',";
}
}
$query='INSERT INTO '.$table.' ('.substr($fields,0,strlen($fields)-1).') VALUES ('.substr($values,0,strlen($values)-1).')';
if(!$db->query($query)) die("Erreur lors de l'insertion de données -> ".$table);
return mysql_insert_id();
}
function update($db, $table, $db_fields)
{
foreach ($db_fields as $field=>$value)
{
if (substr($field,0,2)!='K_') {
$fields.=$field."='".$value."',";
} else {
$where=$field."='".$value."',";
}
}
if ($where) {
$query='UPDATE '.$table.' SET '.substr($fields,0,strlen($fields)-1).' WHERE '.$where;
} else {
die('Erreur lors de la mise à jour, pas de clé -> '.$table);
}
if(!$db->query($query)) die('Erreur lors de la mise à jour de données -> '.$table);
}
function write($db)
{
$db->query('START TRANSACTION');
if (!$this->db_contact['K_CONTACT']) {
$this->set_k_contact( $this->insert($db,'contact',$this->db_contact) );
} else {
$this->update($db,'contact',$this->db_contact);;
}
if (!$this->db_address['K_ADDRESS']) {
$this->db_address['K_ADDRESS']=$this->insert($db,'address',$this->db_address);
} else {
$this->update($db,'address',$this->db_address);;
}
if (!$this->db_email['K_EMAIL']) {
$this->db_email['K_EMAIL']=$this->insert($db,'email',$this->db_email);
} else {
$this->update($db,'email',$this->db_email);;
}
if (!$this->db_memos['K_MEMOS']) {
$this->db_memos['K_MEMOS']=$this->insert($db,'memos',$this->db_memos);
} else {
$this->update($db,'memos',$this->db_memos);;
}
$db->query('COMMIT');
}
}
?>
|