'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; } }