page.search.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $results[$page->id] = array(
  28. 'type' => 'page',
  29. 'item' => $page,
  30. 'excerpt' => $excerpt
  31. );
  32. }
  33. }
  34. }
  35. }
  36. foreach(WikiCategory::staticQuery('SELECT * FROM {{table}} WHERE label REGEXP ? ',array(implode('|',$rterms)),true) as $category){
  37. $results[$category->id] = array(
  38. 'type' => 'category',
  39. 'item' => $category,
  40. 'excerpt' => 'Catégorie'
  41. );
  42. }
  43. }
  44. ?>
  45. <h3 class="search-title"><i class="fas fa-search"></i> RECHERCHE</h3>
  46. <small class="tags-container <?php if(empty($results)) echo 'light-border-bottom'; ?>">
  47. <?php if(isset($terms[0]) && !empty($terms[0])): ?>
  48. <h6 class="d-inline-block user-select-none">Tags : </h6>
  49. <?php foreach ($terms as $term):
  50. if(empty($term)) continue;
  51. $bgColor = random_hex_pastel_color($term);
  52. $color = get_light($bgColor) >= 0.65 ? '#4c4c4c' : '#fdfdfd';
  53. ?>
  54. <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>
  55. <?php endforeach; ?>
  56. <?php endif; ?>
  57. </small>
  58. <?php if(empty($results) && isset($terms[0]) && empty($terms[0])): ?>
  59. <p class="mt-2"><i class="far fa-sad-tear"></i> Aucun mot-clé renseigné...<p>
  60. <?php elseif(empty($results)): ?>
  61. <p class="mt-2"><i class="far fa-grin-beam-sweat"></i> Oops ! Rien trouvé..<p>
  62. <?php else: ?>
  63. <h3 class="wiki-title mt-0"><?php echo count($results); ?> Résultats</h3>
  64. <ul class="category-recent">
  65. <?php foreach($results as $result):
  66. $item = $result['item'];
  67. if($result['type'] == 'page'): ?>
  68. <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);">
  69. <h5><i class="far fa-sticky-note"></i> <?php echo $item->label; ?></h5>
  70. <small class="wiki-small">
  71. <span class="font-weight-bold"><i class="far fa-bookmark"></i> <?php echo $item->join('category')->label; ?></span>
  72. <span> - Edité <?php echo $item->created(); ?> par <i class="far fa-meh-blank"></i> <?php echo $item->author(); ?></span>
  73. </small>
  74. <br>
  75. <small>...<?php echo $result['excerpt']; ?></small>
  76. </li>
  77. <?php else: ?>
  78. <li data-category="<?php echo $item->slug; ?>" onclick="wiki_category_open($(this).attr('data-category'),$(this).attr('data-page'),event);">
  79. <h5><i class="far fa-bookmark"></i> <?php echo $item->label; ?></h5>
  80. <small class="wiki-small">
  81. <span class="font-weight-bold"><i class="far fa-bookmark"></i> <?php echo $item->label; ?></span>
  82. <span> - Edité <?php echo $item->created(); ?> par <i class="far fa-meh-blank"></i> <?php echo $item->author(); ?></span>
  83. </small>
  84. <br>
  85. </li>
  86. <?php endif; ?>
  87. <?php endforeach; ?>
  88. </ul>
  89. <?php endif; ?>