diff options
Diffstat (limited to 'src/model/entities/Client.php')
-rw-r--r-- | src/model/entities/Client.php | 314 |
1 files changed, 314 insertions, 0 deletions
diff --git a/src/model/entities/Client.php b/src/model/entities/Client.php new file mode 100644 index 0000000..7dd3be6 --- /dev/null +++ b/src/model/entities/Client.php | |||
@@ -0,0 +1,314 @@ | |||
1 | <?php | ||
2 | // src/entities/Client.php | ||
3 | |||
4 | use Doctrine\ORM\Mapping as ORM; | ||
5 | use Doctrine\ORM\EntityManager; // pour ClientManager? | ||
6 | |||
7 | #[ORM\Entity] | ||
8 | #[ORM\Table(name: 'clients')] | ||
9 | class Client | ||
10 | { | ||
11 | #[ORM\Id] | ||
12 | #[ORM\Column(type: 'integer')] | ||
13 | #[ORM\GeneratedValue] | ||
14 | private int|null $id = null; | ||
15 | #[ORM\Column] | ||
16 | private string $prenom_nom; | ||
17 | #[ORM\Column] | ||
18 | private string $code_client; | ||
19 | #[ORM\Column] | ||
20 | private string $adresse; | ||
21 | #[ORM\Column] | ||
22 | private string $code_postal; | ||
23 | #[ORM\Column] | ||
24 | private string $ville; | ||
25 | #[ORM\Column] | ||
26 | private string $telephone; | ||
27 | #[ORM\Column] | ||
28 | private string $courriel; | ||
29 | #[ORM\Column] | ||
30 | private string $apropos; | ||
31 | #[ORM\Column(options: ['default' => 'prospect'])] | ||
32 | private string $type = 'prospect'; // valeur par défaut utilisée à l'instanciation, pas par doctrine | ||
33 | |||
34 | public static EntityManager $entityManager; | ||
35 | |||
36 | /*public function __construct() | ||
37 | { | ||
38 | global $entityManager; | ||
39 | self::$entityManager = $entityManager; | ||
40 | }*/ | ||
41 | |||
42 | |||
43 | // getters | ||
44 | public function getId(): int | ||
45 | { | ||
46 | return $this->id; | ||
47 | } | ||
48 | public function getPrenomNom(): string | ||
49 | { | ||
50 | return $this->prenom_nom; | ||
51 | } | ||
52 | public function getCodeClient(): string | ||
53 | { | ||
54 | return $this->code_client; | ||
55 | } | ||
56 | public function getAll(): array | ||
57 | { | ||
58 | // n'utiliser get_object_vars() qu'avec une entité parce qu'on maîtrise le nombre de propriétés | ||
59 | return get_object_vars($this); | ||
60 | } | ||
61 | |||
62 | // pour le GUI | ||
63 | public function getAllWithWindowFields(): array // différent de getAll() | ||
64 | { | ||
65 | return [ | ||
66 | "Prénom Nom:" => $this->prenom_nom, | ||
67 | "Code client (J.C.Dusse):" => $this->code_client, | ||
68 | "Adresse:" => $this->adresse, | ||
69 | "Code postal:" => $this->code_postal, | ||
70 | "Ville:" => $this->ville, | ||
71 | "Telephone:" => $this->telephone, | ||
72 | "Courriel:" => $this->courriel, | ||
73 | "À propos:" => $this->apropos, | ||
74 | "Client ou Prospect?" => $this->type]; | ||
75 | } | ||
76 | |||
77 | // setters | ||
78 | public function set(string $entry, string $input) | ||
79 | { | ||
80 | $input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaine vide | ||
81 | switch($entry) | ||
82 | { | ||
83 | case "Prénom Nom:": | ||
84 | $this->setPrenomNom($input); | ||
85 | break; | ||
86 | case "Code client (J.C.Dusse):": | ||
87 | $this->setCodeClient($input); | ||
88 | break; | ||
89 | case "Adresse:": | ||
90 | $this->setAdresse($input); | ||
91 | break; | ||
92 | case "Code postal:": | ||
93 | $this->setCodePostal($input); | ||
94 | break; | ||
95 | case "Ville:": | ||
96 | $this->setVille($input); | ||
97 | break; | ||
98 | case "Telephone:": | ||
99 | $this->setTelephone($input); | ||
100 | break; | ||
101 | case "Courriel:": | ||
102 | $this->setCourriel($input); | ||
103 | break; | ||
104 | case "À propos:": | ||
105 | $this->setApropos($input); | ||
106 | break; | ||
107 | case "Client ou Prospect?": | ||
108 | $this->setType($input); | ||
109 | break; | ||
110 | } | ||
111 | //self::$entityManager->flush(); | ||
112 | } | ||
113 | public function setPrenomNom($value) | ||
114 | { | ||
115 | $this->prenom_nom = (string) $value; | ||
116 | return $this; | ||
117 | } | ||
118 | public function setCodeClient($value) | ||
119 | { | ||
120 | $this->code_client = (string) $value; | ||
121 | return $this; | ||
122 | } | ||
123 | public function setAdresse($value) | ||
124 | { | ||
125 | $this->adresse = (string) $value; | ||
126 | return $this; | ||
127 | } | ||
128 | public function setCodePostal($value) | ||
129 | { | ||
130 | $this->code_postal = (string) $value; | ||
131 | return $this; | ||
132 | } | ||
133 | public function setVille($value) | ||
134 | { | ||
135 | $this->ville = (string) $value; | ||
136 | return $this; | ||
137 | } | ||
138 | public function setTelephone($value) | ||
139 | { | ||
140 | // type string parce que: | ||
141 | // - zenity renvoie une chaine | ||
142 | // - permet de garder le 0 au début et d'inscrire plusieurs numéros | ||
143 | $this->telephone = (string) $value; | ||
144 | return $this; | ||
145 | } | ||
146 | public function setCourriel($value) | ||
147 | { | ||
148 | $this->courriel = (string) $value; | ||
149 | return $this; | ||
150 | } | ||
151 | public function setApropos($value) | ||
152 | { | ||
153 | $this->apropos = (string) $value; | ||
154 | return $this; | ||
155 | } | ||
156 | public function setType($value) | ||
157 | { | ||
158 | $this->type = (string) $value; | ||
159 | return $this; | ||
160 | } | ||
161 | |||
162 | public function typeToClient() | ||
163 | { | ||
164 | if($this->type != 'client') | ||
165 | { | ||
166 | $this->type = 'client'; | ||
167 | } | ||
168 | } | ||
169 | |||
170 | private function setAll(array $input) | ||
171 | { | ||
172 | $this->prenom_nom = $input[0]; | ||
173 | $this->code_client = $input[1]; | ||
174 | $this->adresse = $input[2]; | ||
175 | $this->code_postal = $input[3]; | ||
176 | $this->ville = $input[4]; | ||
177 | $this->telephone = $input[5]; | ||
178 | $this->courriel = $input[6]; | ||
179 | $this->apropos = $input[7]; | ||
180 | } | ||
181 | |||
182 | // à mettre dans une classe mère Model? | ||
183 | protected function cleanSpecialChars(string $data): string | ||
184 | { | ||
185 | $search = ['"']; | ||
186 | return str_replace($search, '', $data); | ||
187 | } | ||
188 | |||
189 | // une partie du code vient de Model::hydrateFromForm() | ||
190 | // à mettre dans une classe ClientManager? | ||
191 | public function enterCustomer(ZenityForms $Form): bool | ||
192 | { | ||
193 | $input = exec($Form->get()); | ||
194 | if($input == '') | ||
195 | { | ||
196 | echo "debug: annulation lors de l'enregistrement d'un nouveau client\n"; | ||
197 | return false; | ||
198 | } | ||
199 | |||
200 | $data_array = explode('|', $input); // array | ||
201 | if($data_array[0] == '') | ||
202 | { | ||
203 | echo "debug: données insuffisantes, le nom du client doit au minimum être renseigné\n"; | ||
204 | return false; | ||
205 | } | ||
206 | else | ||
207 | { | ||
208 | $this->setAll($data_array); | ||
209 | } | ||
210 | |||
211 | self::$entityManager->persist($this); | ||
212 | self::$entityManager->flush(); | ||
213 | |||
214 | //$this->id = $this->db->lastInsertId(); // synchro automatique avec doctrine | ||
215 | |||
216 | return true; | ||
217 | } | ||
218 | |||
219 | public static function getObject(ZenityEntry $SearchCustomerForm, ZenityList $SearchCustomerResults) // renvoie un Client ou 0 | ||
220 | { | ||
221 | // fenêtres | ||
222 | /*$SearchCustomerForm = new ZenityEntry(ZenitySetup::$recherche_client_text); | ||
223 | $SearchCustomerResults = new ZenityList(ZenitySetup::$resultats_recherche_client_text, []);*/ | ||
224 | |||
225 | $Customer = new self; // se serait bien d'hériter d'EntityManager? | ||
226 | |||
227 | $input = exec($SearchCustomerForm->get()); | ||
228 | if($input == '') // commenter ce if pour qu'une saisie vide permette d'obtenir tous les clients | ||
229 | { | ||
230 | echo "debug: recherche annulée ou saisie vide\n"; | ||
231 | return 0; | ||
232 | } | ||
233 | echo "debug: recherche effectuée\n"; | ||
234 | |||
235 | $MatchedObjects = self::searchCustomer($input, $Customer); | ||
236 | if(count($MatchedObjects) === 0) // si aucun client ne correspond, les prendre tous! | ||
237 | { | ||
238 | $repository = self::$entityManager->getRepository('Client'); | ||
239 | $MatchedObjects = $repository->findAll(); | ||
240 | |||
241 | if(count($MatchedObjects) === 0) // toujours rien | ||
242 | { | ||
243 | echo "debug: aucun client dans la base de données\n"; | ||
244 | return 0; | ||
245 | } | ||
246 | } | ||
247 | |||
248 | $clients_array = []; | ||
249 | foreach($MatchedObjects as $object) // d'objets en tableaux | ||
250 | { | ||
251 | $clients_array[] = get_object_vars($object); | ||
252 | } | ||
253 | |||
254 | $SearchCustomerResults->setListRows( | ||
255 | $clients_array, | ||
256 | //count($clients_array[$id])); | ||
257 | count($clients_array[0])); | ||
258 | |||
259 | // sélection parmi les résultats | ||
260 | $input = exec($SearchCustomerResults->get()); // renvoie l'id de la table 'clients' | ||
261 | if($input == '') | ||
262 | { | ||
263 | echo "debug: client pas trouvé ou pas sélectionné\n"; | ||
264 | return 0; | ||
265 | } | ||
266 | |||
267 | echo "debug: client sélectionné\n"; | ||
268 | //$Customer->setId($input); | ||
269 | //$Customer->hydrate($Customer->findById()); | ||
270 | $Customer = $MatchedObjects[$input]; | ||
271 | //var_dump($Customer); | ||
272 | |||
273 | return $Customer; | ||
274 | } | ||
275 | |||
276 | private static function searchCustomer(string $input, Client $Customer): array | ||
277 | { | ||
278 | $input_array = explode(' ', $input); // si plusieurs mot, on les recherche tous l'un après l'autre | ||
279 | return $Customer->findByKeywords($input_array, 'prenom_nom'); // on obtient un tableau à deux dimensions avec les entrées trouvées | ||
280 | } | ||
281 | |||
282 | private function findByKeywords(array $keywords, string $field): array // renvoie un tableau d'objets | ||
283 | { | ||
284 | $results = []; | ||
285 | for($i = 0; $i < count($keywords); $i++) | ||
286 | { | ||
287 | // tableau à deux dimensions obtenu pour un mot clé | ||
288 | //$query_result = $this->execQuery('SELECT * FROM ' . $this->table . ' WHERE ' . $field . ' LIKE "%' . $keywords[$i] . '%"')->fetchAll(); | ||
289 | $query = self::$entityManager->createQuery('SELECT u FROM Client u WHERE u.' . $field . ' LIKE :expression'); | ||
290 | $query->setParameter('expression', '%' . $keywords[$i] . '%'); | ||
291 | $query_result = $query->getResult(); | ||
292 | |||
293 | foreach($query_result as $object) | ||
294 | { | ||
295 | $id = $object->getId(); | ||
296 | $already_exist = false; | ||
297 | //for($j = 0; $j < count($results); $j++) | ||
298 | foreach($results as $one_result) // pour chaque tableau déjà enregistré dans le tableau $results | ||
299 | { | ||
300 | if($one_result->getId() === $id) | ||
301 | { | ||
302 | $already_exist = true; | ||
303 | } | ||
304 | } | ||
305 | if(!$already_exist) | ||
306 | { | ||
307 | $results[$id] = $object; | ||
308 | } | ||
309 | } | ||
310 | } | ||
311 | //var_dump($results); | ||
312 | return $results; | ||
313 | } | ||
314 | } | ||