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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
<?php
// src/entities/Client.php
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityManager; // pour ClientManager?
#[ORM\Entity]
#[ORM\Table(name: 'clients')]
class Client
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue]
private int|null $id = null;
#[ORM\Column]
private string $prenom_nom;
#[ORM\Column]
private string $code_client;
#[ORM\Column]
private string $adresse;
#[ORM\Column]
private string $code_postal;
#[ORM\Column]
private string $ville;
#[ORM\Column]
private string $telephone;
#[ORM\Column]
private string $courriel;
#[ORM\Column]
private string $apropos;
#[ORM\Column(options: ['default' => 'prospect'])]
private string $type = 'prospect'; // valeur par défaut utilisée à l'instanciation, pas par doctrine
public static EntityManager $entityManager;
/*public function __construct()
{
global $entityManager;
self::$entityManager = $entityManager;
}*/
// getters
public function getId(): int
{
return $this->id;
}
public function getPrenomNom(): string
{
return $this->prenom_nom;
}
public function getCodeClient(): string
{
return $this->code_client;
}
public function getAll(): array
{
// n'utiliser get_object_vars() qu'avec une entité parce qu'on maîtrise le nombre de propriétés
return get_object_vars($this);
}
// pour le GUI
public function getAllWithWindowFields(): array // différent de getAll()
{
return [
"Prénom Nom:" => $this->prenom_nom,
"Code client (J.C.Dusse):" => $this->code_client,
"Adresse:" => $this->adresse,
"Code postal:" => $this->code_postal,
"Ville:" => $this->ville,
"Telephone:" => $this->telephone,
"Courriel:" => $this->courriel,
"À propos:" => $this->apropos,
"Client ou Prospect?" => $this->type];
}
// setters
public function set(string $entry, string $input)
{
$input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaine vide
switch($entry)
{
case "Prénom Nom:":
$this->setPrenomNom($input);
break;
case "Code client (J.C.Dusse):":
$this->setCodeClient($input);
break;
case "Adresse:":
$this->setAdresse($input);
break;
case "Code postal:":
$this->setCodePostal($input);
break;
case "Ville:":
$this->setVille($input);
break;
case "Telephone:":
$this->setTelephone($input);
break;
case "Courriel:":
$this->setCourriel($input);
break;
case "À propos:":
$this->setApropos($input);
break;
case "Client ou Prospect?":
$this->setType($input);
break;
}
//self::$entityManager->flush();
}
public function setPrenomNom($value)
{
$this->prenom_nom = (string) $value;
return $this;
}
public function setCodeClient($value)
{
$this->code_client = (string) $value;
return $this;
}
public function setAdresse($value)
{
$this->adresse = (string) $value;
return $this;
}
public function setCodePostal($value)
{
$this->code_postal = (string) $value;
return $this;
}
public function setVille($value)
{
$this->ville = (string) $value;
return $this;
}
public function setTelephone($value)
{
// type string parce que:
// - zenity renvoie une chaine
// - permet de garder le 0 au début et d'inscrire plusieurs numéros
$this->telephone = (string) $value;
return $this;
}
public function setCourriel($value)
{
$this->courriel = (string) $value;
return $this;
}
public function setApropos($value)
{
$this->apropos = (string) $value;
return $this;
}
public function setType($value)
{
$this->type = (string) $value;
return $this;
}
public function typeToClient()
{
if($this->type != 'client')
{
$this->type = 'client';
}
}
private function setAll(array $input)
{
$this->prenom_nom = $input[0];
$this->code_client = $input[1];
$this->adresse = $input[2];
$this->code_postal = $input[3];
$this->ville = $input[4];
$this->telephone = $input[5];
$this->courriel = $input[6];
$this->apropos = $input[7];
}
// à mettre dans une classe mère Model?
protected function cleanSpecialChars(string $data): string
{
$search = ['"'];
return str_replace($search, '', $data);
}
// une partie du code vient de Model::hydrateFromForm()
// à mettre dans une classe ClientManager?
public function enterCustomer(ZenityForms $Form): bool
{
$input = exec($Form->get());
if($input == '')
{
echo "debug: annulation lors de l'enregistrement d'un nouveau client\n";
return false;
}
$data_array = explode('|', $input); // array
if($data_array[0] == '')
{
echo "debug: données insuffisantes, le nom du client doit au minimum être renseigné\n";
return false;
}
else
{
$this->setAll($data_array);
}
self::$entityManager->persist($this);
self::$entityManager->flush();
//$this->id = $this->db->lastInsertId(); // synchro automatique avec doctrine
return true;
}
public static function getObject(ZenityEntry $SearchCustomerForm, ZenityList $SearchCustomerResults) // renvoie un Client ou 0
{
// fenêtres
/*$SearchCustomerForm = new ZenityEntry(ZenitySetup::$recherche_client_text);
$SearchCustomerResults = new ZenityList(ZenitySetup::$resultats_recherche_client_text, []);*/
$Customer = new self; // se serait bien d'hériter d'EntityManager?
$input = exec($SearchCustomerForm->get());
if($input == '') // commenter ce if pour qu'une saisie vide permette d'obtenir tous les clients
{
echo "debug: recherche annulée ou saisie vide\n";
return 0;
}
echo "debug: recherche effectuée\n";
$MatchedObjects = self::searchCustomer($input, $Customer);
if(count($MatchedObjects) === 0) // si aucun client ne correspond, les prendre tous!
{
$repository = self::$entityManager->getRepository('Client');
$MatchedObjects = $repository->findAll();
if(count($MatchedObjects) === 0) // toujours rien
{
echo "debug: aucun client dans la base de données\n";
return 0;
}
}
$clients_array = [];
foreach($MatchedObjects as $object) // d'objets en tableaux
{
$clients_array[] = get_object_vars($object);
}
$SearchCustomerResults->setListRows(
$clients_array,
//count($clients_array[$id]));
count($clients_array[0]));
// sélection parmi les résultats
$input = exec($SearchCustomerResults->get()); // renvoie l'id de la table 'clients'
if($input == '')
{
echo "debug: client pas trouvé ou pas sélectionné\n";
return 0;
}
echo "debug: client sélectionné\n";
//$Customer->setId($input);
//$Customer->hydrate($Customer->findById());
$Customer = $MatchedObjects[$input];
//var_dump($Customer);
return $Customer;
}
private static function searchCustomer(string $input, Client $Customer): array
{
$input_array = explode(' ', $input); // si plusieurs mot, on les recherche tous l'un après l'autre
return $Customer->findByKeywords($input_array, 'prenom_nom'); // on obtient un tableau à deux dimensions avec les entrées trouvées
}
private function findByKeywords(array $keywords, string $field): array // renvoie un tableau d'objets
{
$results = [];
for($i = 0; $i < count($keywords); $i++)
{
// tableau à deux dimensions obtenu pour un mot clé
//$query_result = $this->execQuery('SELECT * FROM ' . $this->table . ' WHERE ' . $field . ' LIKE "%' . $keywords[$i] . '%"')->fetchAll();
$query = self::$entityManager->createQuery('SELECT u FROM Client u WHERE u.' . $field . ' LIKE :expression');
$query->setParameter('expression', '%' . $keywords[$i] . '%');
$query_result = $query->getResult();
foreach($query_result as $object)
{
$id = $object->getId();
$already_exist = false;
//for($j = 0; $j < count($results); $j++)
foreach($results as $one_result) // pour chaque tableau déjà enregistré dans le tableau $results
{
if($one_result->getId() === $id)
{
$already_exist = true;
}
}
if(!$already_exist)
{
$results[$id] = $object;
}
}
}
//var_dump($results);
return $results;
}
}
|