pub / rememori

Simple file-based bookmarking and notes application
git clone https://src.jayvii.de/pub/rememori.git
Home | Log | Files | Exports | Refs | README | RSS

search.php (5494B)


      1 <?php
      2 
      3 function search_bar(string $query) {
      4 
      5 ?>
      6 
      7 <form action="/" method="post" class="inline">
      8     <input
      9         id="query"
     10         name="query"
     11         type="text"
     12         placeholder="<?php echo $GLOBALS["i18n_search_placeholder"]; ?>"
     13         value="<?php echo $query; ?>"
     14     >
     15     <input
     16         id="action"
     17         name="action"
     18         type="hidden"
     19         value="search"
     20     >
     21     <input
     22         type="submit"
     23         value="<?php echo $GLOBALS["i18n_search"]; ?>"
     24     >
     25 </form>
     26 
     27 <?php
     28 }
     29 
     30 function search_notes(
     31     string $user,
     32     string $query
     33 ) {
     34 
     35     /* gather all notes of the user */
     36     $filenames = gather_notes($user);
     37 
     38     /* split query into words */
     39     $queries = explode(" ", $query);
     40 
     41     /* loop through each file */
     42     for ($i = 0; $i < count($filenames); $i++) {
     43 
     44         $filename = $filenames[$i];
     45 
     46         /* read file content. also add filename and link */
     47         $content = read_note(
     48             $user,
     49             $filename["category"],
     50             $filename["name"] . ".txt"
     51         ) . " " . $filename["name"]. " " . $filename["link"];
     52 
     53         /* collect number of matches */
     54         $matches = 0;
     55         foreach ($queries as $word) {
     56             $matches = $matches + preg_match('/' . $word . '/i', $content);
     57         }
     58 
     59         /* add number of matches to the filesnames array */
     60         $filenames[$i]["matches"] = $matches;
     61 
     62     }
     63 
     64     /* Sort filenames by number of matches */
     65     usort($filenames, "sort_by_matches");
     66 
     67 ?>
     68 
     69     <!-- Headline -->
     70     <h4><?php echo $GLOBALS["i18n_search"] . ": " . $query; ?></h4>
     71 
     72 <?php
     73 
     74     /* loop through each filename and draw a table row */
     75     foreach ($filenames as $filename) {
     76 
     77         /* if there are no matches, skip it! */
     78         if ($filename["matches"] < 1) {
     79             continue;
     80         }
     81 ?>
     82 
     83     <!-- note list item -->
     84     <div
     85         id="<?php echo $filename["category"] . "_" . $filename["name"]; ?>"
     86         class="notes_list_item"
     87     >
     88 
     89 <?php
     90         /* if note starts with an URL, link it in the title
     91          * otherwise just print the title */
     92         if ($filename["link"] !== false) {
     93 ?>
     94         <a href="<?php echo $filename["link"]; ?>" target="_blank">
     95             <strong><?php echo $filename["name"]; ?></strong>
     96         </a><br>
     97 
     98 <?php } else { ?>
     99         <strong><?php echo $filename["name"]; ?></strong><br>
    100 <?php } ?>
    101 
    102         <!-- matches marker -->
    103         <span class="inline">
    104             <?php echo $filename["matches"] . " " . $GLOBALS["i18n_matches"]; ?>
    105         </span>
    106 
    107         <!-- separator -->
    108         <span class="inline">|</span>
    109         
    110         <!-- date marker -->
    111         <span class="inline">
    112             <?php echo date("Y-m-d H:i:s", $filename["time"]); ?>
    113         </span>
    114 
    115         <!-- separator -->
    116         <span class="inline">|</span>
    117 
    118         <!-- category button -->
    119         <form action="/" target="_self" method="post" class="inline">
    120             <input
    121                 id="category"
    122                 name="category"
    123                 type="hidden"
    124                 value="<?php echo $filename["category"]; ?>"
    125             >
    126             <input
    127                 id="action"
    128                 name="action"
    129                 type="hidden"
    130                 value="list"
    131             >
    132             <input
    133                 type="submit"
    134                 class="likeanchor"
    135                 value="<?php echo $filename["category"]; ?>"
    136             >
    137         </form>
    138 
    139         <!-- separator -->
    140         <span class="inline">|</span>
    141 
    142         <!-- edit button -->
    143         <form action="/" method="post" class="inline">
    144             <input
    145                 id="category"
    146                 name="category"
    147                 type="hidden"
    148                 value="<?php echo $filename["category"]; ?>"
    149             >
    150             <input
    151                 id="filename"
    152                 name="filename"
    153                 type="hidden"
    154                 value="<?php echo $filename["name"] . ".txt"; ?>"
    155             >
    156             <input
    157                 id="action"
    158                 name="action"
    159                 type="hidden"
    160                 value="show"
    161             >
    162             <input
    163                 type="submit"
    164                 class="likeanchor"
    165                 value="<?php echo $GLOBALS["i18n_edit"]; ?>"
    166             >
    167         </form>
    168 
    169         <!-- separator -->
    170         <span class="inline">|</span>
    171 
    172         <!-- delete button -->
    173 <?php
    174         $del_id = hash(
    175             "sha256",
    176             $user . "/" . $filename["category"] . "/" . $filename["name"],
    177             false
    178         );
    179 ?>
    180         <form
    181             action="/"
    182             method="post"
    183             id="<?php echo $del_id; ?>"
    184             class="inline"
    185         >
    186             <input
    187                 id="category"
    188                 name="category"
    189                 type="hidden"
    190                 value="<?php echo $filename["category"]; ?>"
    191             >
    192             <input
    193                 id="filename"
    194                 name="filename"
    195                 type="hidden"
    196                 value="<?php echo $filename["name"] . ".txt"; ?>"
    197             >
    198             <input
    199                 id="action"
    200                 name="action"
    201                 type="hidden"
    202                 value="delete"
    203             >
    204             <input
    205                 class="danger likeanchor"
    206                 type="button"
    207                 value="<?php echo $GLOBALS["i18n_delete"]; ?>"
    208                 onclick="deletion_confirm('<?php echo $del_id; ?>');"
    209             >
    210         </form>
    211 
    212     </div>
    213 
    214 <?php
    215 
    216     } // foreach-loop
    217 
    218 } // function
    219 
    220 ?>
    221