1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/DocumentController.php

202 lines
6.2 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-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",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2021-01-27 11:38:28 +01:00
* @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"),
* ),
* )
2023-08-04 09:12:21 +02:00
* @param DocumentFilters $filters
2021-01-27 11:38:28 +01:00
* @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.
*
2023-08-06 09:03:12 +02:00
* @param EditDocumentRequest $request
2020-10-28 11:10:49 +01:00
* @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)
{
$document->fill($request->all());
$document->save();
return $this->itemResponse($document->fresh());
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()
{
2023-08-04 09:12:21 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
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') {
2023-08-04 09:12:21 +02:00
ZipDocuments::dispatch($documents->pluck('id'), $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
*/
2023-08-04 09:12:21 +02:00
$documents->each(function ($document, $key) use ($action, $user) {
if ($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
}
}