From bf6655a534a6775d30cafa67bd801276bda1d98d Mon Sep 17 00:00:00 2001 From: polo Date: Tue, 13 Aug 2024 23:45:21 +0200 Subject: =?UTF-8?q?VERSION=200.2=20doctrine=20ORM=20et=20entit=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/model/entities/Client.php | 314 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 314 insertions(+) create mode 100644 src/model/entities/Client.php (limited to 'src/model/entities/Client.php') 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 @@ + '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; + } +} -- cgit v1.2.3