/** * 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); }
public function addOrUpdateComment(array $comment) { $obj = new AnnotationComment(); $obj->text = $comment['text']; $obj->user_id = $comment['user']['id']; if (isset($comment['id'])) { $obj->id = $comment['id']; } $obj->annotation_id = $this->id; $obj->save(); $obj->load('user'); return $obj; }
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->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; }