Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente | |||
clients:php_soletcivilisation [2011/09/29 09:51] vincent effacée |
— (Version actuelle) | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== Exemple du script PHP du site Sol et Civilisation ====== | ||
- | |||
- | <code php> | ||
- | <?php | ||
- | header("content-type: text/html;charset=UTF-8"); | ||
- | |||
- | /* Initialisation */ | ||
- | $url = "http://sct1.scrutari.net/sct/soletcivilisation/JSON"; | ||
- | |||
- | /* Récupération de la chaine de recherche */ | ||
- | $q = $_REQUEST["q"]; | ||
- | |||
- | /* Définition de langue (dans notre cas uniquement le français */ | ||
- | $lang='fr'; | ||
- | |||
- | /* Récupération des information de localisation dans un tableau $locArray | ||
- | Dans notre cas, il y a un seul fichier */ | ||
- | $locArray = parse_ini_file("siteloc.ini"); | ||
- | |||
- | /* Point important : la construction de la requête vers le serveur Scrutari | ||
- | avec les paramètres des champs*/ | ||
- | $query = "type=q-fiche&fichefields=codecorpus,mtitre_def,msoustitre_def,mcomplements_def,annee,href&motclefields=mlibelles&langui=".$lang."&intitules=complement&q=".$q; | ||
- | |||
- | /* | ||
- | * Récupération de l'objet JSON (c'est un tableau | ||
- | */ | ||
- | $obj = Sct_decodeJsonArray($url, $query); | ||
- | |||
- | /* | ||
- | * $ficheSearchResult est l'élément racine (ici, on ne fait pas de traitement d'erreur | ||
- | */ | ||
- | $ficheSearchResult = $obj['ficheSearchResult']; | ||
- | |||
- | /* | ||
- | * Création d'une instance de la classe Sct_FicheSearchResult qui va conserver les résultats | ||
- | */ | ||
- | $sctFicheSearchResult = new Sct_FicheSearchResult($ficheSearchResult, "Sct_Marque"); | ||
- | |||
- | /* | ||
- | * Html résultant à afficher | ||
- | */ | ||
- | $resultString = ""; | ||
- | |||
- | /* | ||
- | * Nombre de fiches dans le résultat | ||
- | */ | ||
- | $ficheCount = $ficheSearchResult['ficheCount']; | ||
- | |||
- | /* | ||
- | * Construction du résultat | ||
- | */ | ||
- | $resultString = ""; | ||
- | if ($ficheCount == 0) { | ||
- | $resultString .= '<h2>'.$locArray["sct_resultat_zero"].$q.$locArray["sct_finguillemet"]."</h2>"; | ||
- | } else if ($ficheCount == 1) { | ||
- | $resultString .= '<h2>'.$locArray["sct_resultat_un"].$q.$locArray["sct_finguillemet"]."</h2>"; | ||
- | } else { | ||
- | $resultString .= '<h2>'.$ficheCount.' '.$locArray["sct_resultat_plusieurs"].$q.$locArray["sct_finguillemet"]."</h2>"; | ||
- | } | ||
- | if ($ficheCount > 0) { | ||
- | $ficheGroupArray = $ficheSearchResult['ficheGroupArray']; | ||
- | $groupCount = count($ficheGroupArray); | ||
- | $resultString .= '<ul class="ul_ListeLiens">'; | ||
- | for($i = 0; $i < $groupCount; $i++) { | ||
- | $ficheGroup = $ficheGroupArray[$i]; | ||
- | $ficheArray = $ficheGroup['ficheArray']; | ||
- | $count = count($ficheArray); | ||
- | for($j = 0; $j < $count; $j++) { | ||
- | $fiche = $ficheArray[$j]; | ||
- | $codecorpus = $fiche['codecorpus']; | ||
- | $resultString .= '<li><p class="p_TitreLien"><a href="'.$fiche['href'].'">'; | ||
- | $resultString .= $sctFicheSearchResult->concatWithSpan($fiche['mtitre']); | ||
- | $resultString .= '</a></p>'; | ||
- | if (array_key_exists('msoustitre', $fiche)) { | ||
- | $resultString .= '<p class="p_SoustitreLien">'; | ||
- | $resultString .= $sctFicheSearchResult->concatWithSpan($fiche['msoustitre']); | ||
- | $resultString .= '</p>'; | ||
- | } | ||
- | $infoTech = ""; | ||
- | if (array_key_exists('mcomplementArray', $fiche)) { | ||
- | $mcomplementArray = $fiche['mcomplementArray']; | ||
- | $compCount = count($mcomplementArray); | ||
- | for($k = 0; $k < $compCount; $k++) { | ||
- | $mcomplement = $mcomplementArray[$k]; | ||
- | $num = $mcomplement['num']; | ||
- | if ($k > 0) { | ||
- | $infoTech .= ', '; | ||
- | } | ||
- | $infoTech .= $sctFicheSearchResult->concatWithSpan($mcomplement['mcomp']); | ||
- | } | ||
- | } | ||
- | if (array_key_exists('annee', $fiche)) { | ||
- | if (strlen($infoTech) > 0) { | ||
- | $infoTech .= ' | '; | ||
- | } | ||
- | $infoTech .= $fiche['annee']; | ||
- | } | ||
- | if (strlen($infoTech) > 0) { | ||
- | $resultString .= '<p class="p_InfosLien">'; | ||
- | $resultString .= $infoTech; | ||
- | $resultString .= '</p>'; | ||
- | } | ||
- | | ||
- | if (array_key_exists('codemotcleArray', $fiche)) { | ||
- | $motcleCount = count($codemotcleArray); | ||
- | $codemotcleArray = $fiche['codemotcleArray']; | ||
- | $resultString .= '<p class="Sct_Motcle">'; | ||
- | $resultString .= '<span class="Sct_IntituleMotcle">'; | ||
- | if ($motcleCount == 1) { | ||
- | $resultString .= $locArray['sct_motscles_un']; | ||
- | } else { | ||
- | $resultString .= $locArray['sct_motscles_plusieurs']; | ||
- | } | ||
- | $resultString .= " "; | ||
- | $resultString .= '</span> '; | ||
- | for($k = 0; $k < $motcleCount; $k++) { | ||
- | if ($k > 0) { | ||
- | $resultString .= ', '; | ||
- | } | ||
- | $codemotcle = $codemotcleArray[$k]; | ||
- | $resultString .= $sctFicheSearchResult->getMotcleString($codemotcle); | ||
- | } | ||
- | $resultString .= '</p>'; | ||
- | } | ||
- | $resultString .= '</li>'."\n"; | ||
- | } | ||
- | } | ||
- | $resultString .= "</ul>"; | ||
- | } | ||
- | |||
- | ?> | ||
- | |||
- | <!-- Partie HTML proprement dite --> | ||
- | <html> | ||
- | <head> | ||
- | <title>Scrutari</title> | ||
- | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
- | <link href="http://soletcivilisation.exemole.fr/_rsc/css/main.css" rel="stylesheet" type="text/css"> | ||
- | </head> | ||
- | <body> | ||
- | <div id="BLOC_GLOBAL"> | ||
- | <div id="BLOC_CORPS"> | ||
- | <?php echo $resultString; ?> | ||
- | </div> | ||
- | </div> | ||
- | </body> | ||
- | </html> | ||
- | <!-- Find de la partie HTML --> | ||
- | |||
- | <?php | ||
- | |||
- | /****************************************************************** | ||
- | * Clasess et fonctions | ||
- | ******************************************************************/ | ||
- | |||
- | /** | ||
- | * Effectue la requête JSon et décode JSon pour obtenir un tableau | ||
- | */ | ||
- | function Sct_decodeJsonArray($url, $query) { | ||
- | $jsonString = file_get_contents($url.'?'.$query); | ||
- | return json_decode($jsonString, true); | ||
- | } | ||
- | |||
- | |||
- | |||
- | /** | ||
- | * Objet encapsulant le résultat d'une recherche et proposant | ||
- | * des fonctions utilitaires. | ||
- | */ | ||
- | class Sct_FicheSearchResult { | ||
- | |||
- | var $corpusIntituleArray; | ||
- | var $spanClass; //contenu de l'attribut class des balises span | ||
- | var $motcleStringMap; //tableau associatif avec comme clé le code d'un mot-clé et comme valeur le libellé du mot-clé | ||
- | |||
- | function __construct($ficheSearchResult, $spanClass) { | ||
- | $this->corpusIntituleArray = $ficheSearchResult['corpusIntituleArray']; | ||
- | $this->spanClass = $spanClass; | ||
- | $this->motcleStringMap = $this->initMotcleStringMap($ficheSearchResult); | ||
- | } | ||
- | | ||
- | /** | ||
- | * Retourne l'intitulé du complément de numéro $compNum pour | ||
- | * le corpus de code $codecorpus | ||
- | */ | ||
- | function getComplementIntitule($codecorpus, $compNum) { | ||
- | $intituleCount = count($this->corpusIntituleArray); | ||
- | for($i = 0; $i < $intituleCount; $i++) { | ||
- | $corpusIntitule = $this->corpusIntituleArray[$i]; | ||
- | if ($corpusIntitule['codecorpus'] == $codecorpus) { | ||
- | return $corpusIntitule['complement_'.$compNum]; | ||
- | } | ||
- | } | ||
- | return "?"; | ||
- | } | ||
- | | ||
- | /** | ||
- | * $markedStringArray est un tableau qui contient soit des chaines soit des tableaux associatifs | ||
- | * comprenant une clé 's' indiquant les chaines marquées. Ces dernières sont entourées d'une balise | ||
- | * span dont la classe est $spanClass | ||
- | */ | ||
- | function concatWithSpan($markedStringArray) { | ||
- | $result = ""; | ||
- | $count = count($markedStringArray); | ||
- | for($i = 0; $i < $count; $i++) { | ||
- | $obj = $markedStringArray[$i]; | ||
- | if (is_array($obj)) { | ||
- | $result .= '<span class="'.$this->spanClass.'">'; | ||
- | $result .= $obj['s']; | ||
- | $result .= '</span>'; | ||
- | } else { | ||
- | $result .= $obj; | ||
- | } | ||
- | } | ||
- | return $result; | ||
- | } | ||
- | | ||
- | /** | ||
- | * Retourne le libellé du mot-clé de code $codemotcle | ||
- | */ | ||
- | function getMotcleString($codemotcle) { | ||
- | return $this->motcleStringMap[$codemotcle]; | ||
- | } | ||
- | | ||
- | /** | ||
- | * Initialisation du tableau associatif des codes de mots-clés et de lerus libellés | ||
- | */ | ||
- | private function initMotcleStringMap($ficheSearchResult) { | ||
- | $motcleStringMap = array(); | ||
- | if (!array_key_exists('motcleArray', $ficheSearchResult)) { | ||
- | return $motcleStringMap; | ||
- | } | ||
- | $motcleArray = $ficheSearchResult['motcleArray']; | ||
- | $count = count($motcleArray); | ||
- | for($i = 0; $i < $count; $i++) { | ||
- | $motcle = $motcleArray[$i]; | ||
- | $codemotcle = $motcle['codemotcle']; | ||
- | $mlibelleArray = $motcle['mlibelleArray']; | ||
- | $lib = ""; | ||
- | $libCount = count($mlibelleArray); | ||
- | for($j = 0; $j < $libCount; $j++) { | ||
- | if ($j > 0) { | ||
- | $lib .= "/"; | ||
- | } | ||
- | $mlib =$mlibelleArray[$j]; | ||
- | $lib .= $this->concatWithSpan($mlib['mlib']); | ||
- | } | ||
- | $motcleStringMap[$codemotcle] = $lib; | ||
- | } | ||
- | return $motcleStringMap; | ||
- | } | ||
- | |||
- | } | ||
- | |||
- | ?> | ||
- | |||
- | </code> | ||
- | |||
- | |||
- | Et pour information, le fichier ''siteloc.ini'' : | ||
- | |||
- | <code> | ||
- | sct_motscles_un = "Mot-clé :" | ||
- | sct_deuxpoints = " :" | ||
- | sct_resultat_zero = "Aucun résultat pour la recherche « " | ||
- | sct_resultat_plusieurs = "résultats pour la recherche « " | ||
- | sct_resultat_un = "Un résultat pour la recherche « " | ||
- | sct_finguillemet = " »." | ||
- | </code> |