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