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

173 lines
5.5 KiB
PHP
Raw Normal View History

2019-09-27 06:31:13 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2019-09-27 06:31:13 +02:00
*
* @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)
2019-09-27 06:31:13 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-09-27 06:31:13 +02:00
*/
namespace App\Http\Controllers;
2023-10-26 04:57:44 +02:00
use App\Http\Requests\Activity\DownloadHistoricalEntityRequest;
use App\Http\Requests\Activity\ShowActivityRequest;
2023-07-18 05:15:40 +02:00
use App\Models\Activity;
2023-10-26 04:57:44 +02:00
use App\Transformers\ActivityTransformer;
2023-07-18 05:15:40 +02:00
use App\Utils\HostedPDF\NinjaPdf;
2023-10-26 04:57:44 +02:00
use App\Utils\Ninja;
use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\MakesHash;
2023-07-18 05:15:40 +02:00
use App\Utils\Traits\Pdf\PageNumbering;
2023-10-26 04:57:44 +02:00
use App\Utils\Traits\Pdf\PdfMaker;
use Illuminate\Http\Request;
2021-10-20 05:05:46 +02:00
use Illuminate\Support\Facades\Storage;
2023-10-26 04:57:44 +02:00
use stdClass;
2019-09-27 06:31:13 +02:00
class ActivityController extends BaseController
{
2024-01-14 05:05:00 +01:00
use PdfMaker;
use PageNumbering;
use MakesHash;
2020-08-12 02:01:27 +02:00
2019-09-27 06:31:13 +02:00
protected $entity_type = Activity::class;
protected $entity_transformer = ActivityTransformer::class;
public function __construct()
{
parent::__construct();
}
public function index(Request $request)
2019-09-27 06:31:13 +02:00
{
2023-06-24 15:04:19 +02:00
$default_activities = $request->has('rows') ? $request->input('rows') : 75;
2019-09-27 06:31:13 +02:00
2024-01-31 04:44:16 +01:00
/* @var App\Models\Activity[] $activities */
$activities = Activity::with('user')
->orderBy('created_at', 'DESC')
2023-01-23 01:01:27 +01:00
->company()
->take($default_activities);
2024-01-14 05:05:00 +01:00
2023-06-26 14:50:26 +02:00
if($request->has('reactv2')) {
2023-06-24 15:02:38 +02:00
/** @var \App\Models\User auth()->user() */
$user = auth()->user();
if (!$user->isAdmin()) {
$activities->where('user_id', auth()->user()->id);
}
$system = ctrans('texts.system');
2023-08-03 06:30:14 +02:00
$data = $activities->cursor()->map(function ($activity) {
2024-02-13 05:25:18 +01:00
2024-01-31 04:44:16 +01:00
/** @var \App\Models\Activity $activity */
2023-06-24 15:02:38 +02:00
return $activity->activity_string();
});
return response()->json(['data' => $data->toArray()], 200);
}
2022-05-27 10:25:32 +02:00
2019-09-27 06:31:13 +02:00
return $this->listResponse($activities);
}
2020-08-12 02:01:27 +02:00
2023-07-18 05:15:40 +02:00
public function entityActivity(ShowActivityRequest $request)
{
$default_activities = request()->has('rows') ? request()->input('rows') : 75;
$activities = Activity::with('user')
->orderBy('created_at', 'DESC')
->company()
->where("{$request->entity}_id", $request->entity_id)
->take($default_activities);
/** @var \App\Models\User auth()->user() */
$user = auth()->user();
if (!$user->isAdmin()) {
$activities->where('user_id', auth()->user()->id);
}
$system = ctrans('texts.system');
2023-08-03 06:30:14 +02:00
$data = $activities->cursor()->map(function ($activity) {
2023-07-18 05:15:40 +02:00
2024-01-31 04:44:16 +01:00
/** @var \App\Models\Activity $activity */
2023-07-18 05:15:40 +02:00
return $activity->activity_string();
});
return response()->json(['data' => $data->toArray()], 200);
}
2020-08-12 02:41:26 +02:00
public function downloadHistoricalEntity(DownloadHistoricalEntityRequest $request, Activity $activity)
2020-08-12 02:01:27 +02:00
{
2020-08-12 02:13:34 +02:00
$backup = $activity->backup;
2021-10-20 05:05:46 +02:00
$html_backup = '';
2020-08-12 02:01:27 +02:00
/* Refactor 20-10-2021
2021-10-20 05:05:46 +02:00
*
* We have moved the backups out of the database and into object storage.
* In order to handle edge cases, we still check for the database backup
2021-10-20 05:05:46 +02:00
* in case the file no longer exists
*/
if ($backup && $backup->filename && Storage::disk(config('filesystems.default'))->exists($backup->filename)) { //disk
if (Ninja::isHosted()) {
$html_backup = file_get_contents(Storage::disk(config('filesystems.default'))->url($backup->filename));
} else {
$html_backup = file_get_contents(Storage::disk(config('filesystems.default'))->path($backup->filename));
}
} else { //failed
2024-01-14 05:05:00 +01:00
return response()->json(['message' => ctrans('texts.no_backup_exists'), 'errors' => new stdClass()], 404);
}
2020-08-12 02:13:34 +02:00
2021-05-09 13:30:31 +02:00
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
2024-01-14 05:05:00 +01:00
$pdf = (new Phantom())->convertHtmlToPdf($html_backup);
2022-05-25 14:00:17 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $activity->company);
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
} elseif (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
2021-10-20 05:05:46 +02:00
$pdf = (new NinjaPdf())->build($html_backup);
2022-05-25 14:00:17 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $activity->company);
2022-05-25 14:00:17 +02:00
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
} else {
2021-10-20 05:05:46 +02:00
$pdf = $this->makePdf(null, null, $html_backup);
2022-05-25 14:00:17 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $activity->company);
2022-05-25 14:00:17 +02:00
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
2021-05-04 11:56:15 +02:00
}
2020-08-12 02:01:27 +02:00
$activity->company->setLocale();
2024-01-14 05:05:00 +01:00
if (isset($activity->invoice_id)) {
$filename = $activity->invoice->numberFormatter().'.pdf';
} elseif (isset($activity->quote_id)) {
$filename = $activity->quote->numberFormatter().'.pdf';
} elseif (isset($activity->credit_id)) {
$filename = $activity->credit->numberFormatter().'.pdf';
} else {
$filename = 'backup.pdf';
}
return response()->streamDownload(function () use ($pdf) {
2020-08-12 02:01:27 +02:00
echo $pdf;
}, $filename, ['Content-Type' => 'application/pdf']);
2020-08-12 02:01:27 +02:00
}
}