<?php
namespace CorpusParole\Http\Controllers\Api;
use Illuminate\Http\Request;
//use Illuminate\Http\Response;
use CorpusParole\Http\Requests;
use CorpusParole\Http\Controllers\Controller;
use CorpusParole\Repositories\DocumentRepository;
use CorpusParole\Services\TranscriptManager;
class DocumentController extends Controller
{
/**
* Create a new controller instance.
*/
public function __construct(DocumentRepository $documentRepo, TranscriptManager $transcriptManager) {
$this->documentRepository = $documentRepo;
$this->transcriptManager = $transcriptManager;
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$paginator = $this->documentRepository->paginateAll();
$res = $paginator->toArray();
if(array_key_exists('data', $res)) {
$documents = $res['data'];
unset($res['data']);
$res['documents'] = $documents;
//$res['documents'] = $this->documentRepository->resolveLexvo($documents);
}
return response()->json($res);
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param string $id
* @return Response
*/
public function show($id)
{
$doc = $this->documentRepository->get($id);
if(is_null($doc)) {
abort(404);
}
//$this->documentRepository->resolveLexvo([$doc,]);
return response()->json(["document" => $doc]);
}
/**
* Display the resource transcript
*
* @param string $id
* @return Response
*/
public function transcript($id) {
$doc = $this->documentRepository->get($id);
if(is_null($doc) || is_null($doc->getTranscript()) ) {
abort(404);
}
$transcriptDef = $doc->getTranscript();
$transcriptUrl = $transcriptDef['url'];
if(empty($transcriptUrl) || empty($transcriptDef['conforms-to'])) {
abort(404);
}
$converter = $this->transcriptManager->getConverterUrl($transcriptDef['conforms-to'], $doc, $transcriptUrl);
return response()->json($converter->convertToJson());
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified document in storage.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
$data = $request->json();
$document = $data->get('document');
$doc = $this->documentRepository->get($id);
if(is_null($doc)) {
abort(404);
}
//for now, update contributors and subjects only
$doc->setContributors($document['contributors']);
$doc->setSubjects($document['subjects']);
$doc->setModified();
$this->documentRepository->save($doc);
return response('', 204);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}