/**
  * Create a new annotation.
  *
  * @param document ID $doc
  *
  * @throws Exception
  *
  * @return 303 redirect to annotation link
  */
 public function postIndex($doc)
 {
     $body = Input::all();
     $body['doc_id'] = $doc;
     $is_edit = false;
     //Check for edit tag
     if (in_array('edit', $body['tags'])) {
         $is_edit = true;
         //If no explanation present, throw error
         if (!isset($body['explanation'])) {
             throw new Exception('Explanation required for edits');
         }
     }
     $id = \DB::transaction(function () use($body, $doc, $is_edit) {
         $annotation = new Annotation();
         $annotation->doc_id = $doc;
         $annotation->user_id = Auth::user()->id;
         $annotation->quote = $body['quote'];
         $annotation->text = $body['text'];
         $annotation->uri = $body['uri'];
         $annotation->save();
         foreach ($body['ranges'] as $range) {
             $rangeObj = new AnnotationRange();
             $rangeObj->annotation_id = $annotation->id;
             $rangeObj->start_offset = $range['startOffset'];
             $rangeObj->end_offset = $range['endOffset'];
             $rangeObj->start = $range['start'];
             $rangeObj->end = $range['end'];
             $rangeObj->save();
         }
         $permissions = new AnnotationPermission();
         $permissions->annotation_id = $annotation->id;
         $permissions->user_id = Auth::user()->id;
         $permissions->read = 1;
         $permissions->update = 0;
         $permissions->delete = 0;
         $permissions->admin = 0;
         $permissions->save();
         foreach ($body['tags'] as $tag) {
             $tagObj = new AnnotationTag();
             $tagObj->annotation_id = $annotation->id;
             $tagObj->tag = $tag;
             $tagObj->save();
         }
         if ($is_edit) {
             $comment = new AnnotationComment();
             $comment->text = $body['explanation'];
             $comment->user_id = $annotation->user_id;
             $comment->annotation_id = $annotation->id;
             $comment->save();
         }
         //$annotation->updateSearchIndex();
         return $annotation->id;
     });
     $annotation = Annotation::find($id);
     Event::fire(MadisonEvent::DOC_ANNOTATED, $annotation);
     return Redirect::to('/api/docs/' . $doc . '/annotations/' . $id, 303);
 }