server/src/app/Http/Controllers/Api/DocumentController.php
author ymh <ymh.work@gmail.com>
Sun, 09 Oct 2016 11:44:18 +0200
changeset 325 31a4987f6017
parent 324 92fc9d077f95
child 326 226d5b17a119
permissions -rw-r--r--
Add fields to document index
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
namespace CorpusParole\Http\Controllers\Api;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
use Illuminate\Http\Request;
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
     6
//use Illuminate\Http\Response;
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
use CorpusParole\Http\Requests;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
use CorpusParole\Http\Controllers\Controller;
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
use CorpusParole\Repositories\DocumentRepository;
163
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    11
use CorpusParole\Services\TranscriptManager;
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
class DocumentController extends Controller
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
{
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    /**
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
     * Create a new controller instance.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
     */
163
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    18
    public function __construct(DocumentRepository $documentRepo, TranscriptManager $transcriptManager) {
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
        $this->documentRepository = $documentRepo;
163
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    20
        $this->transcriptManager = $transcriptManager;
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    /**
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
     * Display a listing of the resource.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
     *
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
     * @return Response
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
     */
324
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    28
    public function index(Request $request)
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    {
324
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    30
        $perPage = intval($request->input('perpage', config('corpusparole.documents_per_page')));
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    31
        $paginator = $this->documentRepository->paginateAll($perPage);
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    32
        $res = $paginator->toArray();
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    33
        if(array_key_exists('data', $res)) {
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    34
            $documents = $res['data'];
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    35
            unset($res['data']);
324
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    36
        } else {
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    37
            $documents = [];
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    38
        }
324
92fc9d077f95 add pagination info for document list, move it to meta sub object
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    39
        return response()->json([ 'documents' => $documents, 'meta' => $res]);
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    /**
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
     * Show the form for creating a new resource.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
     *
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
     * @return Response
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
     */
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    public function create()
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        //
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    /**
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
     * Store a newly created resource in storage.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
     *
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
     * @param  Request  $request
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
     * @return Response
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
     */
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    public function store(Request $request)
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        //
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
    /**
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
     * Display the specified resource.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
     *
163
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    66
     * @param  string  $id
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
     * @return Response
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
     */
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
    69
    public function show(Request $request, $id)
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    {
319
78990a8a069b Work on front and back integration, correct the expected data format
ymh <ymh.work@gmail.com>
parents: 304
diff changeset
    71
        $id = urldecode($id);
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
    72
        $short = filter_var($request->input('short', false), FILTER_VALIDATE_BOOLEAN);
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
    73
        $doc = $this->documentRepository->get($id, $short);
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        if(is_null($doc)) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
            abort(404);
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        }
128
bc18286e55b2 remove resolve lexvo on server in the api
ymh <ymh.work@gmail.com>
parents: 125
diff changeset
    77
        //$this->documentRepository->resolveLexvo([$doc,]);
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    78
        return response()->json(["document" => $doc]);
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
    /**
163
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    82
     * Display the resource transcript
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    83
     *
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    84
     * @param string $id
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    85
     * @return Response
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    86
     */
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    87
    public function transcript($id) {
304
20071981ba2a add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
    88
        $id= urldecode($id);
20071981ba2a add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
    89
163
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    90
        $doc = $this->documentRepository->get($id);
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    91
        if(is_null($doc) || is_null($doc->getTranscript()) ) {
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    92
            abort(404);
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    93
        }
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
    94
        $transcript = $doc->getTranscript();
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
    95
        $transcriptUrl = $transcript->getUrl();
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
    96
        if(empty($transcriptUrl) || empty($transcript->getConformsTo())) {
163
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    97
            abort(404);
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    98
        }
168
17f10b56c079 improve document model and propagate changes. This include the change of document fixtures to better reflect what the api is effectively returning
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
    99
        $converter = $this->transcriptManager->getConverterUrl($transcript->getConformsTo(), $doc, $transcriptUrl);
163
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
   100
        return response()->json($converter->convertToJson());
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
   101
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
   102
    }
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
   103
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
   104
    /**
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
     * Show the form for editing the specified resource.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
     *
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
     * @param  int  $id
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
     * @return Response
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
     */
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
    public function edit($id)
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
    {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
        //
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
    /**
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   116
     * Update the specified document in storage.
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
     *
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
     * @param  Request  $request
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
     * @param  int  $id
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
     * @return Response
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
     */
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
    public function update(Request $request, $id)
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
    {
304
20071981ba2a add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
   124
        $id= urldecode($id);
20071981ba2a add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
   125
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   126
        $data = $request->json();
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   127
        $document = $data->get('document');
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   128
        $doc = $this->documentRepository->get($id);
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   129
        if(is_null($doc)) {
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   130
            abort(404);
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   131
        }
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   132
138
3079cbf80006 add subjects save + set modified date when saing document in rest api
ymh <ymh.work@gmail.com>
parents: 130
diff changeset
   133
        //for now, update contributors and subjects only
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   134
        $doc->setContributors($document['contributors']);
138
3079cbf80006 add subjects save + set modified date when saing document in rest api
ymh <ymh.work@gmail.com>
parents: 130
diff changeset
   135
        $doc->setSubjects($document['subjects']);
277
bd4bc1db4f40 add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   136
        $doc->addGeoInfo()->setRefLocs($document['reflocs']);
bd4bc1db4f40 add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   137
        $doc->getGeoInfo()->commit();
138
3079cbf80006 add subjects save + set modified date when saing document in rest api
ymh <ymh.work@gmail.com>
parents: 130
diff changeset
   138
3079cbf80006 add subjects save + set modified date when saing document in rest api
ymh <ymh.work@gmail.com>
parents: 130
diff changeset
   139
        $doc->setModified();
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   140
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   141
        $this->documentRepository->save($doc);
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   142
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   143
        return response('', 204);
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
    /**
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
     * Remove the specified resource from storage.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
     *
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
     * @param  int  $id
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
     * @return Response
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
     */
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
    public function destroy($id)
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
    {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
        //
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
    }
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
}