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
|
2020-09-01 20:48:40 +02:00
|
|
|
* @link http://zwiicms.fr/
|
2020-08-14 15:24:14 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-08-15 09:35:43 +02:00
|
|
|
class search extends common {
|
2020-08-14 15:24:14 +02:00
|
|
|
|
|
|
|
public static $actions = [
|
2020-08-16 10:43:09 +02:00
|
|
|
'index' => self::GROUP_VISITOR,
|
2020-09-28 16:45:22 +02:00
|
|
|
'config' => self::GROUP_EDITOR
|
2020-08-14 15:24:14 +02:00
|
|
|
];
|
|
|
|
|
2020-08-20 16:11:11 +02:00
|
|
|
// Variables pour l'affichage des résultats
|
2020-08-14 16:00:42 +02:00
|
|
|
public static $resultList = '';
|
2020-08-19 15:18:57 +02:00
|
|
|
public static $resultError = '';
|
2020-08-14 16:00:42 +02:00
|
|
|
public static $resultTitle = '';
|
2020-08-14 15:24:14 +02:00
|
|
|
|
2020-08-20 16:11:11 +02:00
|
|
|
// Variables pour le dialogue avec le formulaire
|
2020-08-15 09:35:43 +02:00
|
|
|
public static $motclef = '';
|
2020-08-25 21:01:10 +02:00
|
|
|
public static $motentier = true;
|
2020-08-20 16:11:11 +02:00
|
|
|
public static $previewLength = [
|
|
|
|
100 => '100 caractères',
|
|
|
|
200 => '200 caractères',
|
|
|
|
300 => '300 caractères',
|
|
|
|
400 => '400 caractères',
|
|
|
|
];
|
2020-08-15 09:35:43 +02:00
|
|
|
|
2020-08-26 19:54:17 +02:00
|
|
|
// Message par défaut
|
|
|
|
public static $messagePlaceHolder = 'Un ou plusieurs mots-clés entre des espaces ou des guillemets';
|
|
|
|
public static $messageButtontext = 'Rechercher';
|
|
|
|
|
2020-08-15 09:35:43 +02:00
|
|
|
const SEARCH_VERSION = '1.1';
|
2020-08-14 16:09:46 +02:00
|
|
|
|
2020-08-16 10:43:09 +02:00
|
|
|
// Configuration vide
|
|
|
|
public function config() {
|
2020-08-19 21:08:21 +02:00
|
|
|
// Initialisation des données de thème de la galerie dasn theme.json
|
2020-08-16 15:59:37 +02:00
|
|
|
if($this->isPost()) {
|
|
|
|
// Soumission du formulaire
|
2020-08-19 21:08:21 +02:00
|
|
|
$this->setData(['theme', 'search', [
|
|
|
|
'keywordColor' => $this->getInput('searchKeywordColor')
|
|
|
|
]]);
|
|
|
|
$this->setData(['module', $this->getUrl(0), [
|
2020-08-16 15:59:37 +02:00
|
|
|
'submitText' => $this->getInput('searchSubmitText'),
|
2020-08-16 16:24:09 +02:00
|
|
|
'placeHolder' => $this->getInput('searchPlaceHolder'),
|
2020-08-19 18:46:00 +02:00
|
|
|
'resultHideContent' => $this->getInput('searchResultHideContent',helper::FILTER_BOOLEAN),
|
2020-08-20 16:11:11 +02:00
|
|
|
'previewLength' => $this->getInput('searchPreviewLength',helper::FILTER_INT)
|
2020-08-16 15:59:37 +02:00
|
|
|
]]);
|
2020-08-19 18:46:00 +02:00
|
|
|
// Création des fichiers CSS
|
|
|
|
$content = file_get_contents('module/search/ressource/vartheme.css');
|
|
|
|
$themeCss = file_get_contents('module/search/ressource/theme.css');
|
|
|
|
// Injection des variables
|
|
|
|
$content = str_replace('#keywordColor#',$this->getinput('searchKeywordColor'),$content );
|
|
|
|
$success = file_put_contents('module/search/view/index/index.css',$content . $themeCss);
|
|
|
|
|
2020-08-16 15:59:37 +02:00
|
|
|
// Valeurs en sortie, affichage du formulaire
|
|
|
|
$this->addOutput([
|
|
|
|
'redirect' => helper::baseUrl() . $this->getUrl(),
|
2020-08-19 18:46:00 +02:00
|
|
|
'notification' => $success !== FALSE ? 'Modifications enregistrées' : 'Modifications non enregistées !',
|
|
|
|
'state' => $success !== FALSE
|
2020-08-16 15:59:37 +02:00
|
|
|
]);
|
2020-08-19 18:46:00 +02:00
|
|
|
|
2020-08-16 15:59:37 +02:00
|
|
|
}
|
2020-08-16 10:43:09 +02:00
|
|
|
// Valeurs en sortie, affichage du formulaire
|
|
|
|
$this->addOutput([
|
2020-08-16 15:59:37 +02:00
|
|
|
'title' => 'Configuration du module',
|
2020-08-19 18:46:00 +02:00
|
|
|
'view' => 'config',
|
|
|
|
'vendor' => [
|
|
|
|
'tinycolorpicker'
|
|
|
|
]
|
2020-08-16 10:43:09 +02:00
|
|
|
]);
|
|
|
|
}
|
2020-08-14 16:09:46 +02:00
|
|
|
|
2020-08-14 15:24:14 +02:00
|
|
|
public function index() {
|
2020-08-21 09:45:59 +02:00
|
|
|
// Création des valeurs de thème par défaut
|
|
|
|
if ( $this->getData(['theme', 'search']) === null ) {
|
|
|
|
require_once('module/search/ressource/defaultdata.php');
|
|
|
|
$this->setData(['theme', 'search', theme::$defaultData]);
|
|
|
|
}
|
|
|
|
// Création des valeurs de réglage par défaut
|
|
|
|
if ( $this->getData(['module', 'search']) === null ) {
|
|
|
|
require_once('module/search/ressource/defaultdata.php');
|
|
|
|
$this->setData(['module', $this->getUrl(0), data::$defaultData]);
|
|
|
|
}
|
2020-08-20 19:31:11 +02:00
|
|
|
|
2020-08-21 09:45:59 +02:00
|
|
|
if($this->isPost()) {
|
2020-08-14 15:24:14 +02:00
|
|
|
//Initialisations variables
|
|
|
|
$success = true;
|
2020-08-17 16:51:18 +02:00
|
|
|
$result = [];
|
2020-08-14 15:24:14 +02:00
|
|
|
$notification = '';
|
|
|
|
$total='';
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-08-19 19:02:37 +02:00
|
|
|
if (self::$motclef !== '' ) {
|
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']);
|
2020-09-15 20:35:32 +02:00
|
|
|
$contenu = ' ' . $titre . ' ' . $this->getData(['page', $parentId, 'content']);
|
2020-08-14 15:24:14 +02:00
|
|
|
// Pages sauf pages filles et articles de blog
|
2020-08-25 23:16:47 +02:00
|
|
|
$tempData = $this->occurrence($url, $titre, $contenu, self::$motclef, self::$motentier);
|
2020-08-19 15:18:57 +02:00
|
|
|
if (is_array($tempData) ) {
|
|
|
|
$result [] = $tempData;
|
2020-08-17 16:51:18 +02:00
|
|
|
}
|
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']);
|
2020-09-15 20:35:32 +02:00
|
|
|
$contenu = ' ' . $titre . ' ' . $this->getData(['page', $childId, 'content']);
|
2020-08-14 15:24:14 +02:00
|
|
|
//Pages filles
|
2020-08-25 23:16:47 +02:00
|
|
|
$tempData = $this->occurrence($url, $titre, $contenu, self::$motclef, self::$motentier);
|
2020-08-19 15:18:57 +02:00
|
|
|
if (is_array($tempData) ) {
|
|
|
|
$result [] = $tempData;
|
2020-08-17 16:51:18 +02:00
|
|
|
}
|
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'];
|
2020-09-15 20:35:32 +02:00
|
|
|
$contenu = ' ' . $titre . ' ' . $article['content'];
|
2020-08-14 15:24:14 +02:00
|
|
|
// Articles de sous-page de type blog
|
2020-08-25 23:16:47 +02:00
|
|
|
$tempData = $this->occurrence($url, $titre, $contenu, self::$motclef, self::$motentier);
|
2020-08-19 15:18:57 +02:00
|
|
|
if (is_array($tempData) ) {
|
|
|
|
$result [] = $tempData;
|
2020-08-17 16:51:18 +02:00
|
|
|
}
|
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'];
|
2020-09-15 20:35:32 +02:00
|
|
|
$contenu = ' ' . $titre . ' ' . $article['content'];
|
2020-08-14 15:24:14 +02:00
|
|
|
// Articles de Blog
|
2020-08-25 23:16:47 +02:00
|
|
|
$tempData = $this->occurrence($url, $titre, $contenu, self::$motclef, self::$motentier);
|
2020-08-19 15:18:57 +02:00
|
|
|
if (is_array($tempData) ) {
|
|
|
|
$result [] = $tempData;
|
2020-08-17 16:51:18 +02:00
|
|
|
}
|
2020-08-14 15:24:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-19 15:18:57 +02:00
|
|
|
}
|
2020-08-17 10:39:44 +02:00
|
|
|
|
2020-08-19 15:18:57 +02:00
|
|
|
// Message de synthèse de la recherche
|
|
|
|
if (count($result) === 0) {
|
|
|
|
self::$resultTitle = 'Aucun résultat';
|
|
|
|
self::$resultError = 'Avez-vous pensé aux accents ?';
|
2020-08-14 15:24:14 +02:00
|
|
|
} else {
|
2020-08-19 15:18:57 +02:00
|
|
|
self::$resultError = '';
|
|
|
|
self::$resultTitle = ' Résultat de votre recherche';
|
|
|
|
rsort($result);
|
|
|
|
foreach ($result as $key => $value) {
|
|
|
|
$r [] = $value['preview'];
|
2020-08-17 16:51:18 +02:00
|
|
|
}
|
2020-08-19 15:18:57 +02:00
|
|
|
// Générer une chaine de caractères
|
2020-08-19 16:40:23 +02:00
|
|
|
self::$resultList= implode("", $r);
|
2020-08-17 16:51:18 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-19 15:18:57 +02:00
|
|
|
|
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);
|
2020-08-19 15:18:57 +02:00
|
|
|
|
2020-08-26 19:54:17 +02:00
|
|
|
// Découper le chaîne en tenant compte des quillemets
|
|
|
|
$a = str_getcsv(html_entity_decode($motclef), ' ');
|
|
|
|
|
2020-08-25 23:16:47 +02:00
|
|
|
// Construire la clé de recherche selon options de recherche
|
|
|
|
$keywords = '/(';
|
|
|
|
foreach ($a as $key => $value) {
|
|
|
|
$keywords .= $motentier === true ? $value . '|' : '\b' . $value . '\b|' ;
|
|
|
|
}
|
|
|
|
$keywords = substr($keywords,0,strlen($keywords) - 1);
|
|
|
|
$keywords .= ')/i';
|
|
|
|
$keywords = str_replace ('+', ' ',$keywords);
|
|
|
|
|
|
|
|
// Rechercher
|
|
|
|
$valid = preg_match_all($keywords,$contenu,$matches,PREG_OFFSET_CAPTURE);
|
2020-08-19 15:18:57 +02:00
|
|
|
if ($valid > 0 ) {
|
|
|
|
if (($matches[0][0][1]) > 0) {
|
|
|
|
$resultat = '<h2><a href="./?'.$url.'" target="_blank" rel="noopener">' . $titre . '</a></h2>';
|
2020-08-18 19:07:28 +02:00
|
|
|
// Création de l'aperçu
|
|
|
|
// Eviter de découper avec une valeur négative
|
2020-08-19 15:18:57 +02:00
|
|
|
$d = $matches[0][0][1] - 50 < 0 ? 1 : $matches[0][0][1] - 50;
|
2020-08-18 19:07:28 +02:00
|
|
|
// Rechercher l'espace le plus proche
|
2020-09-15 20:35:32 +02:00
|
|
|
$d = $d >= 1 ? strpos($contenu,' ',$d) : $d;
|
2020-08-18 19:07:28 +02:00
|
|
|
// Découper l'aperçu
|
2020-08-25 23:16:47 +02:00
|
|
|
$t = substr($contenu, $d ,$this->getData(['module',$this->getUrl(0),'previewLength']));
|
2020-08-18 19:07:28 +02:00
|
|
|
// Applique une mise en évidence
|
2020-08-25 23:16:47 +02:00
|
|
|
$t = preg_replace($keywords, '<span class="searchKeyword">\1</span>',$t);
|
2020-08-18 19:07:28 +02:00
|
|
|
// Sauver résultat
|
2020-08-19 15:18:57 +02:00
|
|
|
$resultat .= '<p class="searchResult">'.$t.'...</p>';
|
2020-08-25 23:16:47 +02:00
|
|
|
$resultat .= '<p class="searchTitle">' . count($matches[0]) . (count($matches[0]) === 1 ? ' correspondance<p>' : ' correspondances<p>');
|
2020-08-19 15:18:57 +02:00
|
|
|
//}
|
|
|
|
return ([
|
|
|
|
'matches' => count($matches[0]),
|
|
|
|
'preview' => $resultat
|
|
|
|
]);
|
2020-08-14 15:24:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|