ZwiiCMS/module/search/search.php

233 lines
7.7 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
2020-08-16 10:55:52 +02:00
* @author Frédéric Tempez <frederic.tempez@outlook.com>
* @copyright Copyright (C) 2018-2020, Frédéric Tempez
* @copyright Sylvain Lelièvre
2020-08-14 15:24:14 +02:00
* @license GNU General Public License, version 3
* @link http://zwiicms.com/
*
*/
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,
'config' => self::GROUP_MODERATOR
2020-08-14 15:24:14 +02:00
];
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 = '';
2020-08-16 16:24:09 +02:00
public static $defaultButtonText = 'Rechercher';
2020-08-17 10:39:44 +02:00
public static $defaultPlaceHolder = 'Que recherchez-vous ?';
2020-08-16 16:24:09 +02:00
2020-08-15 09:35:43 +02:00
const SEARCH_VERSION = '1.1';
2020-08-14 16:09:46 +02:00
// Configuration vide
public function config() {
2020-08-16 15:59:37 +02:00
if($this->isPost()) {
// Soumission du formulaire
$this->setData(['module', $this->getUrl(0), [
'submitText' => $this->getInput('searchSubmitText'),
2020-08-16 16:24:09 +02:00
'placeHolder' => $this->getInput('searchPlaceHolder'),
'resultHideContent' => $this->getInput('searchResultHideContent',helper::FILTER_BOOLEAN)
2020-08-16 15:59:37 +02:00
]]);
// Valeurs en sortie, affichage du formulaire
$this->addOutput([
'redirect' => helper::baseUrl() . $this->getUrl(),
'notification' => 'Modifications enregistrées',
'state' => true
]);
}
// Valeurs en sortie, affichage du formulaire
$this->addOutput([
2020-08-16 15:59:37 +02:00
'title' => 'Configuration du module',
'view' => 'config'
]);
}
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 = '';
// protection des guillemets
// ^((("){1}([^"])*("){1})([ ]+))+$
//preg_match('(?:^|(?<=\s))"([^"]+)"(?:$|(?=\s))',self::$motclef,$matches);
//print_r($matches);
// Découpage de la chaîne de mots clés
$keywords = '/(';
$a = explode(' ',self::$motclef);
foreach ($a as $key => $value) {
$keywords .= self::$motentier === false ? $value . '|' : '\\b' . $value . '\\b|' ;
}
$keywords = substr($keywords,0,strlen($keywords) - 1);
$keywords .= ')/i';
//echo $keywords;
if ($keywords !== "" && strlen($keywords) > 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
$result .= $this->occurrence($url, $titre, $contenu, $keywords, 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
$result .= $this->occurrence($url, $titre, $contenu, $keywords, 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
$result .= $this->occurrence($url, $titre, $contenu, $keywords, 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
$result .= $this->occurrence($url, $titre, $contenu, $keywords, 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-17 10:39:44 +02:00
2020-08-17 15:34:18 +02:00
$result .='Avez-vous pens&eacute; aux accents ?';
2020-08-14 15:24:14 +02:00
$success = false;
} else {
2020-08-17 11:09:32 +02:00
//$r = self::$nbResults == 1 ? '' : '( ' .self::$nbResults . ' éléments découverts )';
self::$resultTitle = ' Résultat de votre recherche';// ' . $r ;
2020-08-14 15:24:14 +02:00
$success = true;
}
} else {
$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([
2020-08-16 15:59:37 +02:00
'view' => 'index',
'showBarEditButton' => true,
2020-08-16 16:24:09 +02:00
'showPageContent' => !$this->getData(['module', $this->getUrl(0),'resultHideContent'])
2020-08-14 15:24:14 +02:00
]);
} else {
// Valeurs en sortie, affichage du formulaire
$this->addOutput([
'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 >
2020-08-17 10:39:44 +02:00
$contenu = preg_replace ('/<[^>]*>/', ' ', $contenu);
2020-08-14 15:24:14 +02:00
// Accentuation
$contenu = html_entity_decode($contenu);
// Initialisations
$nboccu = 0;
$dejavu = '';
$total = '';
$resultat= '';
// Recherche des occurrences
do {
$occu = preg_match_all($motclef,$contenu,$matches,PREG_OFFSET_CAPTURE);
if ($occu !== false && !empty($matches[0]) ) {
/*echo "<pre>";
print_r($matches);
echo "</pre>";*/
if ($titre !== $dejavu) {
$resultat = '<p><a href="./?'.$url.'" target="_blank" rel="noopener">'.$titre.'</a></p>';
2020-08-14 15:24:14 +02:00
}
$dejavu = $titre;
$nboccu .= count($matches[0]);
foreach ($matches[0] as $key => $value) {
2020-08-17 10:39:44 +02:00
// Mise en évidence
2020-08-17 10:51:45 +02:00
$d = $value[1] - 50 < 0 ? 1 : $value[1] - 50;
$t = substr($contenu,(int) $d ,200);
2020-08-17 10:39:44 +02:00
// Nettoyage caractères invalides
$t = preg_replace('/[^[:alnum:][:space:]]/u', '', $t);
// Applique une mise en évidence
2020-08-17 11:05:44 +02:00
$t = preg_replace($motclef, '<span class="evidence">\1</span>',$t);
2020-08-17 10:39:44 +02:00
// Sauver résultat
2020-08-17 10:53:32 +02:00
$resultat .='...<em>'.$t.'</em> ...</p>';
2020-08-17 10:39:44 +02:00
2020-08-14 15:24:14 +02:00
}
}
// Pour recherche d'une autre occurrence dans le même contenu
$contenu = substr($occu,10);
2020-08-14 15:24:14 +02:00
}
while($occu != '');
2020-08-17 10:39:44 +02:00
self::$nbResults = self::$nbResults + $nboccu; // Nombre total d'occurences
2020-08-14 15:24:14 +02:00
return $resultat;
}
}