Ejemplo n.º 1
0
 public static function createFromAnnotatorArray(array $input)
 {
     if (isset($input['id'])) {
         $retval = static::firstOrNew(array('id' => $input['id']));
     } else {
         $retval = new static();
     }
     $retval->doc_id = (int) $input['doc_id'];
     if (isset($input['user']) && is_array($input['user'])) {
         $retval->user_id = (int) $input['user']['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'];
     }
     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' => $retval->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) {
                 AnnotationTag::where('annotation_id', '=', $retval->id)->delete();
                 $tag = AnnotationTag::firstOrNew(array('annotation_id' => $retval->id, 'tag' => strtolower($tag)));
                 $tag->save();
             }
         }
     });
     return $retval;
 }
Ejemplo n.º 2
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;
 }