1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Http/Controllers/DocumentController.php

198 lines
6.1 KiB
PHP
Raw Normal View History

2020-06-23 13:05:41 +02:00
<?php
namespace App\Http\Controllers;
2021-01-27 11:38:28 +01:00
use App\Filters\DocumentFilters;
2020-06-24 14:22:44 +02:00
use App\Http\Requests\Document\DestroyDocumentRequest;
2021-09-26 14:27:18 +02:00
use App\Http\Requests\Document\EditDocumentRequest;
2020-06-24 10:59:56 +02:00
use App\Http\Requests\Document\ShowDocumentRequest;
use App\Http\Requests\Document\StoreDocumentRequest;
use App\Http\Requests\Document\UpdateDocumentRequest;
2022-03-14 23:36:32 +01:00
use App\Jobs\Document\ZipDocuments;
2020-06-24 10:59:56 +02:00
use App\Models\Document;
2020-06-24 12:17:42 +02:00
use App\Repositories\DocumentRepository;
use App\Transformers\DocumentTransformer;
use App\Utils\Traits\MakesHash;
2020-06-23 13:05:41 +02:00
use Illuminate\Http\Request;
2020-10-28 11:10:49 +01:00
use Illuminate\Http\Response;
2020-06-23 13:05:41 +02:00
2020-06-24 10:59:56 +02:00
class DocumentController extends BaseController
2020-06-23 13:05:41 +02:00
{
2020-06-24 12:17:42 +02:00
use MakesHash;
protected $entity_type = Document::class;
protected $entity_transformer = DocumentTransformer::class;
/**
* @var DocumentRepository
*/
protected $document_repo;
public function __construct(DocumentRepository $document_repo)
{
parent::__construct();
2020-08-11 05:21:43 +02:00
$this->middleware('password_protected', ['only' => ['destroy']]);
2020-06-24 12:17:42 +02:00
$this->document_repo = $document_repo;
}
2020-06-23 13:05:41 +02:00
/**
2021-01-27 11:38:28 +01:00
* @OA\Get(
* path="/api/v1/documents",
* operationId="getDocuments",
* tags={"documents"},
* summary="Gets a list of documents",
* description="Lists documents, search and filters allow fine grained lists to be generated.
Query parameters can be added to performed more fine grained filtering of the documents, these are handled by the DocumentsFilters class which defines the methods available",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\Response(
* response=200,
* description="A list of documents",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Document"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
* @param DocumentsFilters $filters
* @return Response|mixed
2020-06-23 13:05:41 +02:00
*/
2021-01-27 11:38:28 +01:00
public function index(DocumentFilters $filters)
2020-06-23 13:05:41 +02:00
{
2021-01-27 11:38:28 +01:00
$documents = Document::filter($filters);
return $this->listResponse($documents);
2020-06-23 13:05:41 +02:00
}
/**
* Show the form for creating a new resource.
*
2020-10-28 11:10:49 +01:00
* @return void
2020-06-23 13:05:41 +02:00
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param StoreDocumentRequest $request
* @return void
2020-06-23 13:05:41 +02:00
*/
2020-06-24 10:59:56 +02:00
public function store(StoreDocumentRequest $request)
2020-06-23 13:05:41 +02:00
{
//
}
/**
* Display the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param ShowDocumentRequest $request
* @param Document $document
* @return Response
2020-06-23 13:05:41 +02:00
*/
2020-06-24 10:59:56 +02:00
public function show(ShowDocumentRequest $request, Document $document)
2020-06-23 13:05:41 +02:00
{
2020-06-24 12:17:42 +02:00
return $this->itemResponse($document);
}
2020-06-24 10:59:56 +02:00
2020-06-24 14:12:43 +02:00
public function download(ShowDocumentRequest $request, Document $document)
2020-06-24 12:17:42 +02:00
{
2021-09-26 14:27:18 +02:00
$headers = [];
if (request()->input('inline') == 'true') {
2021-09-26 14:27:18 +02:00
$headers = array_merge($headers, ['Content-Disposition' => 'inline']);
}
2021-09-26 14:27:18 +02:00
return response()->streamDownload(function () use ($document) {
2020-06-24 12:17:42 +02:00
echo file_get_contents($document->generateUrl());
2021-09-26 14:27:18 +02:00
}, basename($document->generateUrl()), $headers);
2020-06-23 13:05:41 +02:00
}
/**
* Show the form for editing the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param EditDocumentRegquest $request
* @param Document $document
* @return Response
2020-06-23 13:05:41 +02:00
*/
2021-09-26 14:27:18 +02:00
public function edit(EditDocumentRequest $request, Document $document)
2020-06-23 13:05:41 +02:00
{
2020-06-24 14:12:43 +02:00
return $this->itemResponse($document);
2020-06-23 13:05:41 +02:00
}
/**
* Update the specified resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param UpdateDocumentRequest $request
* @param Document $document
* @return Response
2020-06-23 13:05:41 +02:00
*/
2020-06-24 10:59:56 +02:00
public function update(UpdateDocumentRequest $request, Document $document)
2020-06-23 13:05:41 +02:00
{
2020-06-24 14:12:43 +02:00
return $this->itemResponse($document);
2020-06-23 13:05:41 +02:00
}
/**
* Remove the specified resource from storage.
*
2020-10-28 11:10:49 +01:00
* @param DestroyDocumentRequest $request
* @param Document $document
* @return Response
2020-06-23 13:05:41 +02:00
*/
2020-06-24 10:59:56 +02:00
public function destroy(DestroyDocumentRequest $request, Document $document)
2020-06-23 13:05:41 +02:00
{
2020-06-24 12:17:42 +02:00
$this->document_repo->delete($document);
2021-01-24 23:24:13 +01:00
return response()->json(['message'=> ctrans('texts.success')]);
2020-06-23 13:05:41 +02:00
}
public function bulk()
{
2020-06-24 12:17:42 +02:00
$action = request()->input('action');
$ids = request()->input('ids');
$documents = Document::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get();
2022-03-14 23:30:19 +01:00
if (! $documents) {
2021-01-24 23:24:13 +01:00
return response()->json(['message' => ctrans('texts.no_documents_found')]);
2020-06-24 12:17:42 +02:00
}
if ($action == 'download') {
2022-03-14 23:30:19 +01:00
ZipDocuments::dispatch($documents->pluck('id'), auth()->user()->company(), auth()->user());
2022-03-14 23:30:19 +01:00
return response()->json(['message' => ctrans('texts.sent_message')], 200);
}
2020-06-24 12:17:42 +02:00
/*
* Send the other actions to the switch
*/
$documents->each(function ($document, $key) use ($action) {
if (auth()->user()->can('edit', $document)) {
2022-03-17 03:20:58 +01:00
$this->document_repo->{$action}($document);
2020-06-24 12:17:42 +02:00
}
});
/* Need to understand which permission are required for the given bulk action ie. view / edit */
return $this->listResponse(Document::withTrashed()->whereIn('id', $this->transformKeys($ids))->company());
2020-06-23 13:05:41 +02:00
}
}