server/src/app/Http/Controllers/Api/DocumentController.php
author ymh <ymh.work@gmail.com>
Fri, 30 Sep 2016 00:43:04 +0200
changeset 307 07b44a378ad8
parent 304 20071981ba2a
child 319 78990a8a069b
permissions -rw-r--r--
Add the datestats api
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
     */
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    public function index()
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    {
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    30
        $paginator = $this->documentRepository->paginateAll();
125
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    31
        $res = $paginator->toArray();
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    32
        if(array_key_exists('data', $res)) {
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    33
            $documents = $res['data'];
e550b10fe3ca add language resolver on api data
ymh <ymh.work@gmail.com>
parents: 28
diff changeset
    34
            unset($res['data']);
130
fac22d8c2df8 add subjects to model + simple display on bo + add command to downloads documents to fixtures for test
ymh <ymh.work@gmail.com>
parents: 128
diff changeset
    35
            $res['documents'] = $documents;
128
bc18286e55b2 remove resolve lexvo on server in the api
ymh <ymh.work@gmail.com>
parents: 125
diff changeset
    36
            //$res['documents'] = $this->documentRepository->resolveLexvo($documents);
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    37
        }
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    38
        return response()->json($res);
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    }
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
     * Show the form for creating a new resource.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
     *
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
     * @return Response
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
     */
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    public function create()
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    {
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
     * Store a newly created resource in storage.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
     *
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
     * @param  Request  $request
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
     * @return Response
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
     */
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
    public function store(Request $request)
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    {
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
     * Display the specified resource.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
     *
163
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    65
     * @param  string  $id
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
     * @return Response
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
     */
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
    68
    public function show(Request $request, $id)
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
    {
304
20071981ba2a add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
    70
        $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
    71
        $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
    72
        $doc = $this->documentRepository->get($id, $short);
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        if(is_null($doc)) {
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
            abort(404);
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        }
128
bc18286e55b2 remove resolve lexvo on server in the api
ymh <ymh.work@gmail.com>
parents: 125
diff changeset
    76
        //$this->documentRepository->resolveLexvo([$doc,]);
20
a9b98b16b053 add contributor list + edition pane
ymh <ymh.work@gmail.com>
parents: 4
diff changeset
    77
        return response()->json(["document" => $doc]);
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    }
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
    /**
163
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    81
     * Display the resource transcript
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    82
     *
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    83
     * @param string $id
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    84
     * @return Response
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    85
     */
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    86
    public function transcript($id) {
304
20071981ba2a add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
    87
        $id= urldecode($id);
20071981ba2a add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
    88
163
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    89
        $doc = $this->documentRepository->get($id);
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    90
        if(is_null($doc) || is_null($doc->getTranscript()) ) {
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    91
            abort(404);
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    92
        }
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
    93
        $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
    94
        $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
    95
        if(empty($transcriptUrl) || empty($transcript->getConformsTo())) {
163
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    96
            abort(404);
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    97
        }
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
    98
        $converter = $this->transcriptManager->getConverterUrl($transcript->getConformsTo(), $doc, $transcriptUrl);
163
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
    99
        return response()->json($converter->convertToJson());
59c68fc4848e Add transcript api endpoint
ymh <ymh.work@gmail.com>
parents: 138
diff changeset
   100
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
    /**
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
     * Show the form for editing the specified resource.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
     *
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
     * @param  int  $id
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
     * @return Response
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
     */
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
    public function edit($id)
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
    {
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
    /**
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   115
     * Update the specified document in storage.
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
     *
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
     * @param  Request  $request
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
     * @param  int  $id
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
     * @return Response
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
     */
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
    public function update(Request $request, $id)
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
    {
304
20071981ba2a add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
   123
        $id= urldecode($id);
20071981ba2a add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents: 277
diff changeset
   124
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   125
        $data = $request->json();
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   126
        $document = $data->get('document');
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   127
        $doc = $this->documentRepository->get($id);
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   128
        if(is_null($doc)) {
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   129
            abort(404);
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   130
        }
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   131
138
3079cbf80006 add subjects save + set modified date when saing document in rest api
ymh <ymh.work@gmail.com>
parents: 130
diff changeset
   132
        //for now, update contributors and subjects only
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   133
        $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
   134
        $doc->setSubjects($document['subjects']);
277
bd4bc1db4f40 add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   135
        $doc->addGeoInfo()->setRefLocs($document['reflocs']);
bd4bc1db4f40 add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents: 168
diff changeset
   136
        $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
   137
3079cbf80006 add subjects save + set modified date when saing document in rest api
ymh <ymh.work@gmail.com>
parents: 130
diff changeset
   138
        $doc->setModified();
28
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   139
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   140
        $this->documentRepository->save($doc);
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   141
b0b56e0f8c7f Add contributor edition
ymh <ymh.work@gmail.com>
parents: 20
diff changeset
   142
        return response('', 204);
4
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
    }
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
     * Remove the specified resource from storage.
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
     *
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
     * @param  int  $id
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
     * @return Response
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
     */
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
    public function destroy($id)
f55970e41793 first skeleton of bo client in ember
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
    {
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
}