/
compte.class.php
172 lines (141 loc) · 6.74 KB
/
compte.class.php
1
2
3
4
5
6
7
8
9
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
<?php
class CT {
// --- Partie privée : les variables
var $champs, $valeurs, $champsValeurs, $idx, $nomTable;
var $ct_id, $ct_no, $ct_nom, $ct_genre, $ct_niveau, $ct_rdr;
var $enregis = array("ct_no", "ct_nom", "ct_genre", "ct_niveau", "ct_rdr");
/* fonctions accesseurs */
function set_ct_no ($ct_no) { $this->ct_no = Clean($ct_no); }
function get_ct_no () { return $this->ct_no; }
function set_ct_nom ($ct_nom) { $this->ct_nom = Clean($ct_nom); }
function get_ct_nom () { return $this->ct_nom; }
function set_ct_genre ($ct_genre) { $this->ct_genre = Clean($ct_genre); }
function get_ct_genre () { return $this->ct_genre; }
function set_ct_niveau ($ct_niveau) { $this->ct_niveau = Clean($ct_niveau); }
function get_ct_niveau () { return $this->ct_niveau; }
function set_ct_rdr ($ct_rdr) { $this->ct_rdr = Clean($ct_rdr); }
function get_ct_rdr () { return $this->ct_rdr; }
//---- partie publique : les méthodes
Function CT ($table) { // constructeur
/*********************************************************
liste des champs de l'enregistrement
le champ « ct_id » n'est pas inclus à cause de la
fonction MAJ qui ne doit pas comprendre la clé primaire
********************************************************/
$this->i = 0;
foreach($this->enregis as $value) {
if ($this->i == 0) {
$this->champs .= $value;
} else {
$this->champs .= ", " . $value ;
}
$this->{$value} = "";
$this->i++;
}
$this->nomTable = $table;
return;
} //---------------------- constructeur pour CT
Function Imprimer_form ($action, $css)
{
$listeRdr[1] = "oui";
$listeRdr[0] = "non";
print('<table border="0" bgcolor="#EBEBEB" align="center"><tr><td>');
print(Imprime_titreListe("Gestion d'un compte", "titre"));
print("<br>");
$f = new Formulaire ("POST", "comptes.php");
$f->debutTable(HORIZONTAL);
$f->champTexte(" no du compte", "ct_no", $this->ct_no, 15, 15);
$f->champTexte(" nom du compte", "ct_nom", $this->ct_nom, 50, 50);
$f->champTexte(" 0 = dépenses<br /> 1 = revenus", "ct_genre", $this->ct_genre, 12, 1);
$f->champTexte(" 1 = niveau titre<br /> 2 = niveau compte", "ct_niveau", $this->ct_niveau, 13, 1);
$f->champListe(" rdr", "ct_rdr", $this->ct_rdr, 2, $listeRdr);
$f->champCache("ct_id", $this->ct_id);
$f->finTable();
$f->debutTable(HORIZONTAL);
$f->champValider ("sauver", "action");
$f->finTable();
$f->fin();
print('</td></tr></table>');
} // fin Imprimer_form -----------------------
Function Sauver ($bd)
{
$requete = "SELECT ct_id FROM $this->nomTable WHERE ct_id = '$this->ct_id' ";
$res = $bd->execRequete($requete);
if (mysql_num_rows($res) > 0 )
$this->Maj($bd);
else
$this->Inserer($bd);
} //---------------------------------------- sauver
Function Inserer ($bd)
{
$this->i = 0;
foreach($this->enregis as $value) {
if ($this->i == 0) {
$this->valeurs = "'" . addslashes($this->{$value}) . "'";
} else {
$this->valeurs .= ", '" . addslashes($this->{$value}) . "'";
}
$this->i++;
}
//----- clé primaire « ct_id » placée au début des champs existants
//----- « 0 » comme valeur correspondante dans les valeurs
$this->champs = "ct_id, " . $this->champs;
$this->valeurs = "0, " . $this->valeurs;
$requete = "INSERT INTO $this->nomTable (" . $this->champs . ") VALUES (" . $this->valeurs . ")";
$res = $bd->execRequete($requete);
$this->ct_id = mysql_insert_id(); //--- pour initialiser « ct_id » dans l'objet après l'insertion dans la BD
$this->operation = "opération INSÉRER ok __ $this->ct_no - $this->ct_nom __ (enr. $this->ct_id)";
return;
} //------------------------------------------------- Inserer
Function Maj ($bd)
{
$this->i = 0;
foreach($this->enregis as $value) {
if ($this->i == 0) {
$this->champsValeurs .= $value . " = '" . addslashes($this->{$value}) . "'";
} else {
$this->champsValeurs .= ", " . $value . " = '" . addslashes($this->{$value}) . "'";
}
$this->i++;
}
$requete = "UPDATE $this->nomTable SET " . $this->champsValeurs . " WHERE ct_id = '$this->ct_id' " ;
$resultat = $bd->execRequete($requete);
$this->operation = "opération MAJ ok __ $this->ct_no - $this->ct_nom __ (enr. $this->ct_id)";
return $resultat;
} //---------------------------------------------------------- Maj
Function Detruire ($bd)
{
$requete = "DELETE FROM $this->nomTable WHERE ct_id = '$this->ct_id' " ;
$resultat = $bd->execRequete($requete);
$this->operation = "opération DÉTRUIRE ok __ $this->ct_no - $this->ct_nom- (enr. $this->ct_id)";
return $resultat;
} //---------------------------------------------------------- Detruire
Function Affectation ($ligne, $ct_id)
{
foreach($ligne as $index=>$value) {
if (in_array($index, $this->enregis)) { // initialiser seulement les éléments
$this->{$index} = stripslashes($value); // de $_POST qui font partie de l'enr.
}
}
$this->ct_id = $ct_id; //****** pour initialiser ct_id dans la classe CT
return;
} //-------------------------------------------------
Function Get_operation ()
{
return $this->operation;
} //-------------------------------------------
Function Get_ct ($bd, $ct_id)
{
$requete = "SELECT * FROM $this->nomTable WHERE ct_id = '$ct_id' ";
$res = $bd->execRequete($requete);
if (mysql_num_rows($res) > 0 ) {
$ligne = $bd->ligneSuivante($res);
$this->Affectation($ligne, $ct_id);
$this->operation = "";
} else {
$this->ct_id = $ct_id;
}
return $res;
} //-----------------------------
} //---fin de la classe ----------------------------
?>