ZwiiCMS/module/search/search.php

226 lines
7.5 KiB
PHP
Raw Normal View History

2020-08-14 15:24:14 +02:00
<?php
/**
* This file is part of Zwii.
*
* For full copyright and license information, please see the LICENSE
* file that was distributed with this source code.
*
* @author Rémi Jean <remi.jean@outlook.com>
* @copyright Copyright (C) 2008-2018, Rémi Jean
* @license GNU General Public License, version 3
* @link http://zwiicms.com/
*
* Module rechercher
* Produit par la communauté à partir d'un développement de Sylvain Lelièvre
*/
// Module de recherche d'un mot ou d'une phrase clef
2020-08-15 09:35:43 +02:00
class search extends common {
2020-08-14 15:24:14 +02:00
public static $actions = [
'index' => self::GROUP_VISITOR
];
2020-08-14 16:00:42 +02:00
public static $resultList = '';
public static $nbResults = 0;
public static $resultTitle = '';
2020-08-14 15:24:14 +02:00
2020-08-15 09:35:43 +02:00
public static $motclef = '';
public static $motentier = '';
const SEARCH_VERSION = '1.1';
2020-08-14 16:09:46 +02:00
2020-08-14 15:24:14 +02:00
public function index() {
if($this->isPost()) {
//Initialisations variables
$success = true;
$result = '';
$notification = '';
$total='';
2020-08-14 16:00:42 +02:00
self::$nbResults = 0;
2020-08-14 15:24:14 +02:00
// Récupération du mot clef passé par le formulaire de ...view/index.php, avec caractères accentués
2020-08-15 09:35:43 +02:00
self::$motclef=$this->getInput('searchMotphraseclef');
2020-08-14 15:24:14 +02:00
// Récupération de l'état de l'option mot entier passé par le même formulaire
2020-08-15 09:35:43 +02:00
self::$motentier=$this->getInput('searchMotentier', helper::FILTER_BOOLEAN);
2020-08-14 15:24:14 +02:00
//Pour affichage de l'entête du résultat
2020-08-14 16:00:42 +02:00
self::$resultTitle = 'Aucun résultat';
$result = '';
2020-08-15 09:35:43 +02:00
if (self::$motclef !== "" && strlen(self::$motclef) > 2) {
2020-08-14 15:24:14 +02:00
foreach($this->getHierarchy(null,false,null) as $parentId => $childIds) {
if ($this->getData(['page', $parentId, 'disable']) === false &&
$this->getUser('group') >= $this->getData(['page', $parentId, 'group']) &&
$this->getData(['page', $parentId, 'block']) !== 'bar') {
$url = $parentId;
$titre = $this->getData(['page', $parentId, 'title']);
$contenu = $this->getData(['page', $parentId, 'content']);
// Pages sauf pages filles et articles de blog
2020-08-15 09:35:43 +02:00
$result .= $this->occurrence($url, $titre, $contenu, self::$motclef, self::$motentier);
2020-08-14 15:24:14 +02:00
}
foreach($childIds as $childId) {
// Sous page
if ($this->getData(['page', $childId, 'disable']) === false &&
$this->getUser('group') >= $this->getData(['page', $parentId, 'group']) &&
$this->getData(['page', $parentId, 'block']) !== 'bar') {
$url = $childId;
$titre = $this->getData(['page', $childId, 'title']);
$contenu = $this->getData(['page', $childId, 'content']);
//Pages filles
2020-08-15 09:35:43 +02:00
$result .= $this->occurrence($url, $titre, $contenu, self::$motclef, self::$motentier);
2020-08-14 15:24:14 +02:00
}
// Articles d'une sous-page blog
if ($this->getData(['page', $childId, 'moduleId']) === 'blog')
{
foreach($this->getData(['module',$childId]) as $articleId => $article) {
if($this->getData(['module',$childId,$articleId,'state']) === true) {
$url = $childId . '/' . $articleId;
$titre = $article['title'];
$contenu = $article['content'];
// Articles de sous-page de type blog
2020-08-15 09:35:43 +02:00
$result .= $this->occurrence($url, $titre, $contenu, self::$motclef, self::$motentier);
2020-08-14 15:24:14 +02:00
}
}
}
}
// Articles d'un blog
if ($this->getData(['page', $parentId, 'moduleId']) === 'blog' ) {
foreach($this->getData(['module',$parentId]) as $articleId => $article) {
if($this->getData(['module',$parentId,$articleId,'state']) === true)
{
$url = $parentId. '/' . $articleId;
$titre = $article['title'];
$contenu = $article['content'];
// Articles de Blog
2020-08-15 09:35:43 +02:00
$result .= $this->occurrence($url, $titre, $contenu, self::$motclef, self::$motentier);
2020-08-14 15:24:14 +02:00
}
}
}
}
// Message de synthèse de la recherche
2020-08-14 16:00:42 +02:00
if (self::$nbResults === 0) {
2020-08-14 15:24:14 +02:00
$notification = 'Mot clef non trouv&eacute;. Avez-vous pens&eacute; aux accents ?';
$result .='Mot clef non trouv&eacute;. Avez-vous pens&eacute; aux accents ?';
$success = false;
} else {
2020-08-14 16:00:42 +02:00
$result .= 'Nombre d\'occurrences : '.self::$nbResults;
$notification = 'Nombre d\'occurrences : '.self::$nbResults;
2020-08-15 09:35:43 +02:00
self::$resultTitle = 'Résultat(s) : "' . self::$motclef . '" a été trouvé '. self::$nbResults . ' fois';
2020-08-14 15:24:14 +02:00
$success = true;
}
} else {
$notification = 'Trop court ! Minimum 3 caract&egrave;res';
$result = 'Trop court ! Minimum 3 caract&egrave;res';
$success = false;
}
2020-08-14 16:00:42 +02:00
self::$resultList = $result;
2020-08-14 15:24:14 +02:00
// Valeurs en sortie, affichage du résultat
$this->addOutput([
'title' => '',
'view' => 'result',
'notification' => $notification,
'state' => $success
]);
} else {
// Valeurs en sortie, affichage du formulaire
$this->addOutput([
'title' => '',
'view' => 'index',
'showBarEditButton' => true,
'showPageContent' => true
]);
}
}
// Fonction de recherche des occurrences dans $contenu
// Renvoie le résultat sous forme de chaîne
private function occurrence($url, $titre, $contenu, $motclef, $motentier)
{
// Nettoyage de $contenu : on enlève tout ce qui est inclus entre < et >
$contenu = $this->nettoyer_html($contenu);
// Accentuation
$contenu = html_entity_decode($contenu);
// Initialisations
$nboccu = 0;
$dejavu = '';
$total = '';
$resultat= '';
// Recherche des occurrences
do {
$occu = stristr($contenu,$motclef);
if ($occu !== false) {
if ($motentier === true) {
$controle_entier=$this->test_motentier($contenu,$motclef);
} else {
$controle_entier=true;
}
if ($controle_entier) {
if ($titre !== $dejavu) {
2020-08-14 16:00:42 +02:00
$resultat = '<p><a href="./?'.$url.'" target="_blank" rel="noopener">'.$titre.'</a></p>';
2020-08-14 15:24:14 +02:00
}
$dejavu = $titre;
$nboccu++;
$resultat .= '<p>'.$nboccu.' - "...<em>'.substr($occu,0,200).'</em>..."<br/></p>';
}
// Pour recherche d'une autre occurrence dans le même contenu
$contenu = substr($occu,10);
}
}
while($occu != '');
2020-08-14 16:00:42 +02:00
self::$nbResults = self::$nbResults + $nboccu;
2020-08-14 15:24:14 +02:00
return $resultat;
}
// Déclaration de la fonction nettoyer(string $contenu) : string
// Supprime de $contenu les caractères placés entre < et >, donc les balises html comme <p> <br/> etc...
// Retourne $contenu nettoyée, le résultat est sensiblement différent de celui obtenu avec la fonction strip_tags()
private function nettoyer_html($contenu)
{
do {
$pos1=strpos($contenu,chr(60));
if($pos1!==false) {
$pos2=strpos($contenu,chr(62));
if($pos2!==false) $contenu=substr_replace($contenu," ",$pos1,($pos2 - $pos1 + 1));
}
}
while($pos1!==false);
return $contenu;
}
// Déclaration de la fonction test_motentier(string $chaine, string $clef) : bool
// Vérifie si dans la string $chaine, $clef est un mot entier
// $clef ne doit pas être précédé ni suivi d'une lettre maj ou min
private function test_motentier($chaine, $clef)
{
$resultat=true;
$pos1=stripos($chaine,$clef);
$avant=ord(substr($chaine,$pos1-1, 1));
$apres=ord(substr($chaine,$pos1+strlen($clef),1));
// Traitement pour le caractère qui précède et celui qui suit
if (($avant>=65 && $avant<=90) ||
($avant>=97 && $avant<=122) ||
($apres>=65 && $apres<=90) ||
($apres>=97 && $apres<=122) ) {
$resultat=false;
}
return $resultat;
}
}