Commit 9e9638ec authored by Jean-Baptiste Oellers's avatar Jean-Baptiste Oellers
Browse files

Ajout autocomplete partie front

parent 5ba188a7
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
@@ -23,6 +23,36 @@ class ConventionModeleInstanceController extends AbstractOscarController impleme
            return ['conventionModeleInstance' => $conventionModeleInstance];
        }

        if ($this->params()->fromPost('action') == "rechercher_organisation") {

            $resultsQuery = $this->getEntityManager()
                ->createQueryBuilder('o')
                ->select('o')
                ->from(\Oscar\Entity\Organization::class, 'o');
            $resultsQuery = $resultsQuery->getQuery();
            $results = $resultsQuery->setFirstResult(1)
                ->setMaxResults(5)
                ->getResult();

            $organisations = [];
            
            foreach ($results as $o) {
                $organisation = [];
                $organisation['nom_court'] = $o->getShortName();
                $organisation['nom_complet'] = $o->getFullName();
                $organisation['siret'] = $o->getSiret() ? $o->getSiret() : '';
                $organisation['adresse_1'] = $o->getStreet1() ? $o->getStreet1() : '';
                $organisation['code_postal'] = $o->getZipCode() ? $o->getZipCode() : '';
                $organisation['ville'] = $o->getCity() ? $o->getCity() : '';
                $organisation['pays'] = $o->getCountry() ? $o->getCountry() : '';
                $organisations[] = $organisation;
            }
            
            $response = new \Laminas\View\Model\JsonModel();
            $response->setVariables(['organisations' => $organisations]);
            return $response;
        }

        if ($this->params()->fromPost('action') == "enregistrer_reponses") {

            $reponses = [];
+131 −4
Original line number Diff line number Diff line
@@ -13,6 +13,20 @@
    background-color: rgb(220, 220, 220);
  }
  
  button {
    background-color: white;
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
    padding-left: 1rem;
    padding-right: 1rem;
    border: 1px solid lightgrey;
    cursor: pointer;
    border-radius: 1rem;
  }
  button:hover {
    background-color: rgb(220, 220, 220);
  }

  #demarrer-chemin-decisionnel {
    display: flex;
    flex-direction: column;
@@ -91,12 +105,125 @@
          <?php elseif ($information->type == \Oscar\Entity\ConventionModeleEtapeInformation::TYPE_RECHERCHE_D_UNE_ORGANISATION): ?>

          <div>
            <label>Rechercher une organisation par SIREN, par nom... :</label> <input type="text" size="40" id="reponse-<?= $information->id ?>-recherche" name="reponse-<?= $information->id ?>-recherche" />
          </div>
            <label>Rechercher une organisation par SIREN, par nom... :</label>
            <input type="text" size="40" id="reponse-<?= $information->id ?>-recherche" name="reponse-<?= $information->id ?>-recherche" />
            <button id="reponse-<?= $information->id ?>-recherche-bouton" type="button">🔍 Rechercher</button>
            <script type="text/javascript">
                window.addEventListener("load", function () {
                    
                    document.getElementById("reponse-<?= $information->id ?>-recherche-bouton").addEventListener("click", function() {
                        event.preventDefault();
                        
                        let rechercher_organisation_results = document.getElementById("rechercher_organisation_results");
                        rechercher_organisation_results.innerHTML = '';

                        let texte_recherche = document.getElementById("reponse-<?= $information->id ?>-recherche").value;
                        if (!texte_recherche) {
                            let newElemList = document.createElement('li');
                            newElemList.innerText = 'Veuillez saisir un texte à rechercher';
                            rechercher_organisation_results.appendChild(newElemList);
                            return;
                        }

                        fetch('', {
                            method: "POST",
                            body: new URLSearchParams({ action: "rechercher_organisation" })
                        })
                            .then((res) => {
                                if (!res.ok) {
                                    throw new Error('Request failed, caused by: ' + res.status + " " + res.statusText);
                                }
                                return res.json();
                            }).then((res) => {
                                const organisations = res.organisations;
                                if (organisations.length === 0) {
                                    let newElemList = document.createElement('li');
                                    newElemList.innerText = 'Aucune organisation trouvée';
                                    rechercher_organisation_results.appendChild(newElemList);
                                    return;
                                }
                                for (const o of organisations) {
                                    let newElemDivTitreNomCourt = document.createElement('div');
                                    newElemDivTitreNomCourt.classList.add("nom_court");
                                    newElemDivTitreNomCourt.innerText = o.nom_court;
                                    
                                    let newElemDivTitreBouton = document.createElement('button');
                                    newElemDivTitreBouton.setAttribute('type', 'button');
                                    newElemDivTitreBouton.innerText = '➕ Sélectionner cette organisation';
                                    
                                    let newElemDivTitre = document.createElement('div');
                                    newElemDivTitre.classList.add("rechercher_organisation_results_titre");
                                    newElemDivTitre.appendChild(newElemDivTitreNomCourt);
                                    newElemDivTitre.appendChild(newElemDivTitreBouton);
                                    newElemDivTitre.addEventListener("click", function() {
                                        event.preventDefault();
                                        var xpath = '//h2[contains(text(),"Identification de l\'utilisateur : nom de l\'organisme")]/following-sibling::div//input';
                                        var matchingElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        if (matchingElement) {
                                            matchingElement.value = o.nom_complet;
                                        }
                                        var xpath = '//h2[contains(text(),"Identification de l\'utilisateur : numéro SIREN")]/following-sibling::div//input';
                                        var matchingElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        if (matchingElement) {
                                            matchingElement.value = o.siret;
                                        }
                                        var xpath = '//h2[contains(text(),"Identification de l\'utilisateur : adresse du siège social")]/following-sibling::div//input';
                                        var matchingElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                                        if (matchingElement) {
                                            matchingElement.value = o.adresse_1 + ' ' + o.code_postal + ' ' + o.ville + ' ' + o.pays;
                                        }
                                        rechercher_organisation_results.innerHTML = '';
                                    });

                                    let newElemDivNomComplet = document.createElement('div');
                                    newElemDivNomComplet.innerText = o.nom_complet;
                                    
                                    let newElemDivSiret = document.createElement('div');
                                    newElemDivSiret.innerHTML = '<span>SIRET : </span>' + o.siret;
                                    
                                    let newElemDivAdresse = document.createElement('div');
                                    newElemDivAdresse.innerHTML = '<span>Adresse : </span>' + o.adresse_1 + ' ' + o.code_postal + ' ' + o.ville + ' ' + o.pays;

                                    let newElemList = document.createElement('li');
                                    newElemList.appendChild(newElemDivTitre);
                                    newElemList.appendChild(newElemDivNomComplet);
                                    newElemList.appendChild(newElemDivSiret);
                                    newElemList.appendChild(newElemDivAdresse);
                                    rechercher_organisation_results.appendChild(newElemList);
                                };
                            }).catch((error) => {
                                let newElemList = document.createElement('li');
                                newElemList.innerText = 'Une erreur est survenue : ' + error;
                                rechercher_organisation_results.appendChild(newElemList);
                                return;
                            }).finally(() => {

                            });
                    });
                });
            </script>
          </div>
          <div>
            <label>SIRET</label>
            <input type="text" size="80" id="reponse-<?= $information->id ?>-siret" name="reponse-<?= $information->id ?>-siret" />
              <style>
                  #rechercher_organisation_results li {
                      border: 0.5rem solid #dadada;
                      margin-top: 1rem;
                      padding: 1rem;
                  }
                  .rechercher_organisation_results_titre {
                      display: flex;
                      justify-content: space-between; 
                      margin-bottom: 0.5rem;
                  }
                  #rechercher_organisation_results .nom_court {
                      font-weight: bold;
                      font-size: larger;
                  }
                  #rechercher_organisation_results span {
                      font-weight: bold;
                  }
              </style>
              <ul id="rechercher_organisation_results"></ul>
          </div>

          <?php endif; ?>