/**
  * 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);
 }
Ejemplo n.º 2
0
 public function delete()
 {
     $client = static::getEsClient();
     $esParams = array('index' => static::getEsIndex(), 'type' => static::INDEX_TYPE, 'id' => $this->search_id);
     try {
         $client->delete($esParams);
     } catch (Exception $e) {
         $message = json_decode($e->getMessage());
         if ($message->ok) {
             Log::warning("The annotation with id: " . $this->search_id . " was not found in ElasticSearch.  Deleting annotation from the DB...");
         } else {
             throw new Exception("Unable to delete annotation from ElasticSearch: " . $e->getMessage());
         }
     }
     DB::transaction(function () {
         $deletedMetas = NoteMeta::where('annotation_id', '=', $this->id)->delete();
         $deletedComments = AnnotationComment::where('annotation_id', '=', $this->id)->delete();
         $deletedPermissions = AnnotationPermission::where('annotation_id', '=', $this->id)->delete();
         $deletedRanges = AnnotationRange::where('annotation_id', '=', $this->id)->delete();
         $deletedTags = AnnotationTag::where('annotation_id', '=', $this->id)->delete();
         return parent::delete();
     });
 }
Ejemplo n.º 3
0
 protected static function saveAnnotationModel(array $input)
 {
     $retval = DBAnnotation::firstOrNew(array('id' => $input['id']));
     if (isset($input['user'])) {
         $retval->user_id = (int) $input['user']['id'];
     }
     $retval->doc = (int) $input['doc'];
     $retval->id = $input['id'];
     if (isset($input['quote'])) {
         $retval->quote = $input['quote'];
     }
     if (isset($input['text'])) {
         $retval->text = $input['text'];
     }
     if (isset($input['uri'])) {
         $retval->uri = $input['uri'];
     }
     $retval->likes = isset($input['likes']) ? (int) $input['likes'] : 0;
     $retval->dislikes = isset($input['dislikes']) ? (int) $input['dislikes'] : 0;
     $retval->flags = isset($input['flags']) ? (int) $input['flags'] : 0;
     DB::transaction(function () use($retval, $input) {
         $retval->save();
         if (isset($input['ranges'])) {
             foreach ($input['ranges'] as $range) {
                 $rangeObj = AnnotationRange::firstByRangeOrNew(array('annotation_id' => $retval->id, 'start_offset' => $range['startOffset'], 'end_offset' => $range['endOffset']));
                 $rangeObj->start = $range['start'];
                 $rangeObj->end = $range['end'];
                 $rangeObj->save();
             }
         }
         if (isset($input['comments']) && is_array($input['comments'])) {
             foreach ($input['comments'] as $comment) {
                 $commentObj = AnnotationComment::firstOrNew(array('id' => (int) $comment['id'], 'annotation_id' => $retval->id, 'user_id' => (int) $comment['user']['id']));
                 $commentObj->text = $comment['text'];
                 $commentObj->save();
             }
         }
         $permissions = array();
         if (isset($input['permissions']) && is_array($input['permissions'])) {
             foreach ($input['permissions']['read'] as $userId) {
                 $userId = (int) $userId;
                 if (!isset($permissions[$userId])) {
                     $permissions[$userId] = array('read' => false, 'update' => false, 'delete' => false, 'admin' => false);
                 }
                 $permissions[$userId]['read'] = true;
             }
             foreach ($input['permissions']['update'] as $userId) {
                 $userId = (int) $userId;
                 if (!isset($permissions[$userId])) {
                     $permissions[$userId] = array('read' => false, 'update' => false, 'delete' => false, 'admin' => false);
                 }
                 $permissions[$userId]['update'] = true;
             }
             foreach ($input['permissions']['delete'] as $userId) {
                 $userId = (int) $userId;
                 if (!isset($permissions[$userId])) {
                     $permissions[$userId] = array('read' => false, 'update' => false, 'delete' => false, 'admin' => false);
                 }
                 $permissions[$userId]['delete'] = true;
             }
             foreach ($input['permissions']['admin'] as $userId) {
                 $userId = (int) $userId;
                 if (!isset($permissions[$userId])) {
                     $permissions[$userId] = array('read' => false, 'update' => false, 'delete' => false, 'admin' => false);
                 }
                 $permissions[$userId]['admin'] = true;
             }
         }
         foreach ($permissions as $userId => $perms) {
             $userId = (int) $userId;
             $permissionsObj = AnnotationPermission::firstOrNew(array('annotation_id' => $input['id'], 'user_id' => $userId));
             $permissionsObj->read = (int) $perms['read'];
             $permissionsObj->update = (int) $perms['update'];
             $permissionsObj->delete = (int) $perms['delete'];
             $permissionsObj->admin = (int) $perms['admin'];
             $permissionsObj->save();
         }
         if (isset($input['tags']) && is_array($input['tags'])) {
             foreach ($input['tags'] as $tag) {
                 $tag = AnnotationTag::firstOrNew(array('annotation_id' => $input['id'], 'tag' => strtolower($tag)));
                 $tag->save();
             }
         }
     });
     return $retval;
 }