1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Http/Controllers/VendorPortal/DocumentController.php

124 lines
3.8 KiB
PHP
Raw Normal View History

2022-07-27 07:39:43 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2022-07-27 07:39:43 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers\VendorPortal;
use App\Http\Controllers\Controller;
use App\Http\Requests\Document\DownloadMultipleDocumentsRequest;
2023-02-16 02:36:09 +01:00
use App\Http\Requests\VendorPortal\Documents\ShowDocumentRequest;
2022-07-27 07:39:43 +02:00
use App\Libraries\MultiDB;
use App\Models\Document;
use App\Utils\TempFile;
use App\Utils\Traits\MakesHash;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
class DocumentController extends Controller
{
use MakesHash;
2022-07-27 08:09:25 +02:00
public const MODULE_PURCHASE_ORDERS = 16384;
2022-07-27 07:39:43 +02:00
/**
* @return Factory|View
*/
public function index()
{
return render('documents.index');
}
/**
* @param ShowDocumentRequest $request
* @param Document $document
* @return Factory|View
*/
public function show(ShowDocumentRequest $request, Document $document)
{
2022-07-27 08:09:25 +02:00
return render('documents.vendor_show', [
2022-07-27 07:39:43 +02:00
'document' => $document,
2022-07-27 08:09:25 +02:00
'settings' => auth()->guard('vendor')->user()->company->settings,
'sidebar' => $this->sidebarMenu(),
'company' => auth()->guard('vendor')->user()->company,
2022-07-27 07:39:43 +02:00
]);
}
2022-07-27 08:09:25 +02:00
private function sidebarMenu() :array
{
$enabled_modules = auth()->guard('vendor')->user()->company->enabled_modules;
$data = [];
// TODO: Enable dashboard once it's completed.
// $this->settings->enable_client_portal_dashboard
// $data[] = [ 'title' => ctrans('texts.dashboard'), 'url' => 'client.dashboard', 'icon' => 'activity'];
if (self::MODULE_PURCHASE_ORDERS & $enabled_modules) {
$data[] = ['title' => ctrans('texts.purchase_orders'), 'url' => 'vendor.purchase_orders.index', 'icon' => 'file-text'];
}
// $data[] = ['title' => ctrans('texts.documents'), 'url' => 'client.documents.index', 'icon' => 'download'];
return $data;
}
2022-07-27 07:39:43 +02:00
public function download(ShowDocumentRequest $request, Document $document)
{
return Storage::disk($document->disk)->download($document->url, $document->name);
}
public function publicDownload(string $document_hash)
{
MultiDB::documentFindAndSetDb($document_hash);
2023-08-04 10:13:26 +02:00
/** @var \App\Models\Document $document */
2022-07-27 07:39:43 +02:00
$document = Document::where('hash', $document_hash)->firstOrFail();
$headers = [];
if (request()->input('inline') == 'true') {
$headers = array_merge($headers, ['Content-Disposition' => 'inline']);
}
return Storage::disk($document->disk)->download($document->url, $document->name, $headers);
}
public function downloadMultiple(DownloadMultipleDocumentsRequest $request)
{
2023-08-04 10:13:26 +02:00
/** @var \Illuminate\Database\Eloquent\Collection<Document> $documents */
2022-07-27 07:39:43 +02:00
$documents = Document::whereIn('id', $this->transformKeys($request->file_hash))
->where('company_id', auth()->guard('vendor')->user()->company_id)
->get();
$zipFile = new \PhpZip\ZipFile();
try {
foreach ($documents as $document) {
$zipFile->addFile(TempFile::path($document->filePath()), $document->name);
}
$filename = now().'-documents.zip';
$filepath = sys_get_temp_dir().'/'.$filename;
$zipFile->saveAsFile($filepath) // save the archive to a file
->close(); // close archive
2023-02-16 02:36:09 +01:00
return response()->download($filepath, $filename)->deleteFileAfterSend(true);
2022-07-27 07:39:43 +02:00
} catch (\PhpZip\Exception\ZipException $e) {
// handle exception
} finally {
$zipFile->close();
}
}
}