1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Models/Document.php

278 lines
7.5 KiB
PHP
Raw Normal View History

2019-04-28 07:31:32 +02:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
2019-04-28 07:31:32 +02:00
namespace App\Models;
2020-02-27 00:32:44 +01:00
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
2019-04-28 07:31:32 +02:00
2023-03-08 08:33:42 +01:00
/**
* App\Models\Document
*
* @property int $id
* @property int $user_id
* @property int|null $assigned_user_id
* @property int $company_id
* @property int|null $project_id
* @property int|null $vendor_id
* @property string|null $url
* @property string|null $preview
* @property string|null $name
* @property string|null $type
* @property string|null $disk
* @property string|null $hash
* @property int|null $size
* @property int|null $width
* @property int|null $height
* @property int $is_default
* @property string|null $custom_value1
* @property string|null $custom_value2
* @property string|null $custom_value3
* @property string|null $custom_value4
* @property int|null $deleted_at
* @property int $documentable_id
* @property string $documentable_type
* @property int|null $created_at
* @property int|null $updated_at
* @property int $is_public
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $documentable
* @property-read mixed $hashed_id
* @property-read \App\Models\User $user
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel company()
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel exclude($columns)
* @method static \Database\Factories\DocumentFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder|Document filter(\App\Filters\QueryFilters $filters)
* @method static \Illuminate\Database\Eloquent\Builder|Document newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Document newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Document onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|Document query()
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel scope()
* @method static \Illuminate\Database\Eloquent\Builder|Document withTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|Document withoutTrashed()
* @mixin \Eloquent
*/
2019-04-28 07:31:32 +02:00
class Document extends BaseModel
{
2020-02-27 00:32:44 +01:00
use SoftDeletes;
2021-01-27 11:38:28 +01:00
use Filterable;
2024-01-14 05:05:00 +01:00
public const DOCUMENT_PREVIEW_SIZE = 300; // pixels
2019-04-28 12:25:18 +02:00
/**
2023-05-08 14:55:37 +02:00
* @var array<string>
2019-04-28 12:25:18 +02:00
*/
protected $fillable = [
'is_default',
2020-08-19 08:11:57 +02:00
'is_public',
2023-05-08 14:55:37 +02:00
'name',
2019-04-28 12:25:18 +02:00
];
/**
* @var array<string>
*/
protected $casts = [
'is_public' => 'bool',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
/**
2019-04-28 12:25:18 +02:00
* @var array
*/
public static $types = [
'png' => [
'mime' => 'image/png',
],
'ai' => [
'mime' => 'application/postscript',
],
'jpeg' => [
'mime' => 'image/jpeg',
],
2021-01-14 10:25:39 +01:00
'jpg' => [
'mime' => 'image/jpeg',
],
2019-04-28 12:25:18 +02:00
'tiff' => [
'mime' => 'image/tiff',
],
'pdf' => [
'mime' => 'application/pdf',
],
'gif' => [
'mime' => 'image/gif',
],
'psd' => [
'mime' => 'image/vnd.adobe.photoshop',
],
'txt' => [
'mime' => 'text/plain',
],
'doc' => [
'mime' => 'application/msword',
],
'xls' => [
'mime' => 'application/vnd.ms-excel',
],
'ppt' => [
'mime' => 'application/vnd.ms-powerpoint',
],
'xlsx' => [
'mime' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
],
'docx' => [
'mime' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
],
'pptx' => [
'mime' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
],
];
2019-06-03 07:31:20 +02:00
/**
* @var array
*/
public static $extraExtensions = [
'jpg' => 'jpeg',
'tif' => 'tiff',
];
public function getEntityType()
{
return self::class;
}
2019-06-03 07:31:20 +02:00
2019-04-28 07:31:32 +02:00
public function documentable()
{
return $this->morphTo();
}
2023-01-20 00:58:24 +01:00
public function user()
{
return $this->belongsTo(User::class)->withTrashed();
}
public function generateUrl($absolute = false)
{
2020-06-22 07:52:20 +02:00
$url = Storage::disk($this->disk)->url($this->url);
if ($url && $absolute) {
return url($url);
}
if ($url) {
return $url;
}
return null;
}
2020-06-24 10:59:56 +02:00
public function generateRoute($absolute = false)
{
2024-06-14 09:09:44 +02:00
try {
return route('api.documents.show', ['document' => $this->hashed_id]).'/download';
} catch(\Exception $e) {
2024-06-18 05:47:27 +02:00
nlog("Exception:: Document::" . $e->getMessage());
2024-02-18 01:19:59 +01:00
return '';
}
2020-06-24 10:59:56 +02:00
}
2020-06-24 12:17:42 +02:00
public function deleteFile()
{
Storage::disk($this->disk)->delete($this->url);
}
2020-08-20 03:10:04 +02:00
public function filePath()
{
return Storage::disk($this->disk)->url($this->url);
}
2021-02-15 15:52:13 +01:00
public function diskPath(): string
{
return Storage::disk($this->disk)->path($this->url);
}
2022-03-14 23:30:19 +01:00
public function getFile()
{
2022-03-14 23:36:32 +01:00
return Storage::get($this->url);
2022-03-14 23:30:19 +01:00
}
2022-04-06 04:22:13 +02:00
public function translate_entity()
{
return ctrans('texts.document');
}
2024-02-28 11:15:43 +01:00
public function link()
{
$entity_id = $this->encodePrimaryKey($this->documentable_id);
2024-02-29 03:06:11 +01:00
$link = '';
2024-02-28 11:15:43 +01:00
match($this->documentable_type) {
2024-02-29 22:58:56 +01:00
'App\Models\Vendor' => $link = "/vendors/{$entity_id}",
'App\Models\Project' => $link = "/projects/{$entity_id}",
'invoices' => $link = "/invoices/{$entity_id}/edit",
'App\Models\Quote' => $link = "/quotes/{$entity_id}/edit",
'App\Models\Credit' => $link = "/credits/{$entity_id}/edit",
'App\Models\Expense' => $link = "/expenses/{$entity_id}/edit",
'App\Models\Payment' => $link = "/payments/{$entity_id}/edit",
'App\Models\Task' => $link = "/tasks/{$entity_id}/edit",
'App\Models\Client' => $link = "/clients/{$entity_id}",
2024-03-01 00:34:05 +01:00
'App\Models\RecurringExpense' => $link = "/recurring_expenses/{$entity_id}/edit",
'App\Models\RecurringInvoice' => $link = "/recurring_invoices/{$entity_id}/edit",
2024-02-29 03:06:11 +01:00
default => $link = '',
2024-02-28 11:15:43 +01:00
};
return $link;
}
public function compress(): mixed
{
$image = $this->getFile();
$catch_image = $image;
2023-11-26 08:41:42 +01:00
if(!extension_loaded('imagick')) {
return $catch_image;
2023-11-26 08:41:42 +01:00
}
try {
$file = base64_encode($image);
2024-07-15 05:35:21 +02:00
$img = new \Imagick(); //@phpstan-ignore-line
$img->readImageBlob($file);
2024-07-15 05:35:21 +02:00
$img->setImageCompression(true); //@phpstan-ignore-line
2024-02-06 21:42:46 +01:00
$img->setImageCompressionQuality(40);
return $img->getImageBlob();
2024-01-14 05:05:00 +01:00
2023-11-26 08:41:42 +01:00
} catch(\Exception $e) {
2024-06-18 05:47:27 +02:00
nlog("Exception:: Document::" . $e->getMessage());
nlog($e->getMessage());
return $catch_image;
}
}
2023-11-08 09:54:26 +01:00
/**
* Returns boolean based on checks for image.
*
* @return bool
*/
public function isImage(): bool
{
if (in_array($this->type, ['png', 'jpeg', 'jpg', 'tiff', 'gif'])) {
return true;
}
return false;
}
2019-04-28 07:31:32 +02:00
}