Example #1
0
 /**
  *  Returns all next related notes (next means bigger id)
  *
  *  @return  App\Note  The next notes
  */
 public function next()
 {
     // Eager load all attached notes with a bigger ID than this
     $nextNotes = Note::whereHas('notes', function ($query) {
         $query->where('note2_id', '=', $this->id);
         $query->where('note1_id', '>', $this->id);
     })->get(['id']);
     return $nextNotes;
 }
Example #2
0
 /**
  *  Shows a tag and displays all associated notes
  *
  *  @param   integer  $id  Tag id
  *
  *  @return  Response
  */
 public function show($id)
 {
     if (!$id || $id <= 0) {
         return redirect(url('/tags/index'))->withErrors(['message' => 'No such tag']);
     }
     $tag = Tag::find($id);
     $notes = Note::whereHas('tags', function ($query) use($id) {
         $query->where('tag_id', '=', $id);
     })->get();
     return view('tags.show', compact('notes', 'tag'));
 }
Example #3
0
 /**
  *  Displays a single note
  *
  *  @param   int  $id  Note id
  *
  *  @return  Response
  */
 public function show($id)
 {
     // eager load Note with its tags
     $note = Note::find($id);
     $note->tags;
     // does the note exist? If not, return error
     if (!$note) {
         return redirect('notes/index')->withErrors(['That note does not exist!']);
     }
     // Get all note and their tags if the tag ID is in our tags-array
     $tags = [];
     foreach ($note->tags as $tag) {
         $tags[] = $tag->id;
     }
     // Get all notes that have at least one of this note's tags
     $relatedNotes = Note::whereHas('tags', function ($query) use($tags) {
         $query->whereIn('tag_id', $tags);
     })->get();
     // Now remove this note from collection (to reduce inception level)
     $relatedNotes = $relatedNotes->keyBy('id');
     $relatedNotes->forget($note->id);
     // Now we have all relatedNotes
     // But they have ALL tags with them.
     // We have to determine the relevancy manually
     $maxCount = 0;
     foreach ($relatedNotes as $n) {
         // count all similar tags and write a count-attribute to the model
         $count = 0;
         foreach ($n->tags as $t) {
             foreach ($note->tags as $t2) {
                 if ($t->id == $t2->id) {
                     $count++;
                     break;
                 }
             }
         }
         $n->count = $count;
         // write current maxCount
         if ($count > $maxCount) {
             $maxCount = $count;
         }
     }
     // Now we need to sort the relatedNotes by relevancy
     //$relatedNotes = array_values(array_sort($relatedNotes, function($value) { return $value->count; }));
     //array_multisort($relatedNotes, "SORT_DESC", );
     $relatedNotes = Collection::make($relatedNotes)->sortBy(function ($value) {
         return $value->count;
     }, SORT_REGULAR, true)->all();
     // Now it can be, that some people are just ignoring the tag functionality
     // which is fine. But we then need another algorithm to search notes:
     // heuristic text analysis!
     if (count($note->tags) == 0 || count($relatedNotes) < 5) {
         $relatedNotes = $this->textAnalysis($note);
         // Now retrieve all related Notes
         $tmp = new Collection();
         $maxCount = 0;
         foreach ($relatedNotes as $id) {
             $n = Note::find($id['id']);
             $n->count = $id['relevancy'];
             $tmp->push($n);
             if ($id['relevancy'] > $maxCount) {
                 $maxCount = $id['relevancy'];
             }
         }
         $relatedNotes = Collection::make($tmp)->sortBy(function ($value) {
             return $value->count;
         }, SORT_REGULAR, true)->all();
         // Remove this note to reduce inception level
         $thisNoteIndex = -1;
         foreach ($relatedNotes as $index => $n) {
             if ($note->id == $n->id) {
                 $thisNoteIndex = $index;
             }
         }
         unset($relatedNotes[$thisNoteIndex]);
         $relatedNotes = array_values($relatedNotes);
     }
     // Now retrieve IDs and title of all linked notes
     $linkedNotes = $note->notes;
     $mainID = $note->id;
     return view('notes.show', compact('note', 'relatedNotes', 'maxCount', 'linkedNotes', 'mainID'));
 }