page.search.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. require_once(__DIR__.SLASH.'WikiCategory.class.php');
  3. require_once(__DIR__.SLASH.'WikiPage.class.php');
  4. $results = array();
  5. $terms = explode(' ',trim($_['term']));
  6. $rterms = array();
  7. foreach ($terms as $value) {
  8. if(empty(trim($value)) || strlen($value)<=2) continue;
  9. $rterms[] = str_replace('/','\/',preg_quote($value));
  10. }
  11. if(count($rterms)>0){
  12. foreach(WikiPage::loadAll(array(),array('label'),array(), array('*'), 1) as $page){
  13. $page->content();
  14. $content = $page->label.' - '.$page->content;
  15. if(preg_match_all('/'.implode('|',$rterms).'/iUs',$content, $matches, PREG_OFFSET_CAPTURE)){
  16. foreach ($matches as $match) {
  17. foreach ($match as $word) {
  18. $offset = $word[1];
  19. $word = $word[0];
  20. $length = strlen($page->content);
  21. $start = $offset-50;
  22. $start = $start < 0 ? 0 : $start;
  23. $end = $start+100 > $length -1 ? $length -1: 100;
  24. $excerpt = substr($page->content,$start,$end).'...';
  25. $excerpt = htmlentities($excerpt);
  26. $excerpt = preg_replace('|(.*)('.$word.')(.*)|iUs', '$1<span class="wiki-search-highlight">$2</span>$3', $excerpt);
  27. //Dans quel but ? (ligne dessous)
  28. // if(isset($results[$page->id])) $excerpt = $results[$page->id]['excerpt'].' '.$excerpt;
  29. $results[$page->id] = array(
  30. 'type' => 'page',
  31. 'item' => $page,
  32. 'excerpt' => $excerpt
  33. );
  34. }
  35. }
  36. }
  37. }
  38. foreach(WikiCategory::staticQuery('SELECT * FROM {{table}} WHERE label REGEXP ? ',array(implode('|',$rterms)),true) as $category){
  39. $results[$category->id] = array(
  40. 'type' => 'category',
  41. 'item' => $category,
  42. 'excerpt' => 'Catégorie'
  43. );
  44. }
  45. }
  46. ?>
  47. <h3 class="search-title"><i class="fas fa-search"></i> RECHERCHE</h3>
  48. <small class="tags-container <?php if(empty($results)) echo 'light-border-bottom'; ?>">
  49. <?php if(isset($terms[0]) && !empty($terms[0])): ?>
  50. <h6 class="d-inline-block no-select">Tags : </h6>
  51. <?php foreach ($terms as $term):
  52. if(empty($term)) continue;
  53. $bgColor = random_hex_pastel_color($term);
  54. $color = get_light($bgColor) >= 0.65 ? '#4c4c4c' : '#fdfdfd';
  55. ?>
  56. <span class="ml-1 tag-item" style="<?php echo 'background-color:'.$bgColor.';color:'.$color.';'; ?>"><i class="fas fa-tag"></i> <?php echo htmlentities($term); ?> <i class="fas fa-times delete-tag" onclick="wiki_tag_delete(this);"></i></span>
  57. <?php endforeach; ?>
  58. <?php endif; ?>
  59. </small>
  60. <?php if(empty($results) && isset($terms[0]) && empty($terms[0])): ?>
  61. <p class="mt-2"><i class="far fa-sad-tear"></i> Aucun mot-clé renseigné...<p>
  62. <?php elseif(empty($results)): ?>
  63. <p class="mt-2"><i class="far fa-grin-beam-sweat"></i> Oops ! Rien trouvé..<p>
  64. <?php else: ?>
  65. <h3 class="wiki-title mt-0"><?php echo count($results); ?> Résultats</h3>
  66. <ul class="category-recent">
  67. <?php foreach($results as $result):
  68. $item = $result['item'];
  69. if($result['type'] == 'page'): ?>
  70. <li data-category="<?php echo $item->join('category')->slug; ?>" data-page="<?php echo $item->slug; ?>" onclick="wiki_page_open($(this).attr('data-category'),$(this).attr('data-page'),event);">
  71. <h5><i class="far fa-sticky-note"></i> <?php echo $item->label; ?></h5>
  72. <small class="wiki-small">
  73. <span class="font-weight-bold"><i class="far fa-bookmark"></i> <?php echo $item->join('category')->label; ?></span>
  74. <span> - Edité <?php echo $item->created(); ?> par <i class="far fa-meh-blank"></i> <?php echo $item->author(); ?></span>
  75. </small>
  76. <br>
  77. <small>...<?php echo $result['excerpt']; ?></small>
  78. </li>
  79. <?php else: ?>
  80. <li data-category="<?php echo $item->slug; ?>" onclick="wiki_category_open($(this).attr('data-category'),$(this).attr('data-page'),event);">
  81. <h5><i class="far fa-bookmark"></i> <?php echo $item->label; ?></h5>
  82. <small class="wiki-small">
  83. <span class="font-weight-bold"><i class="far fa-bookmark"></i> <?php echo $item->label; ?></span>
  84. <span> - Edité <?php echo $item->created(); ?> par <i class="far fa-meh-blank"></i> <?php echo $item->author(); ?></span>
  85. </small>
  86. <br>
  87. </li>
  88. <?php endif; ?>
  89. <?php endforeach; ?>
  90. </ul>
  91. <?php endif; ?>