Filter images

This commit is contained in:
Benjamin Beganović 2021-11-09 17:30:17 +01:00
parent c46ce9bc81
commit 9b383b525b
4 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,30 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Helpers\Document;
trait WithTypeHelpers
{
/**
* Returns boolean based on checks for image.
*
* @return bool
*/
public function isImage(): bool
{
if (in_array($this->type, ['png', 'svg', 'jpeg', 'jpg', 'tiff', 'gif'])) {
return true;
}
return false;
}
}

View File

@ -11,6 +11,7 @@
namespace App\Models;
use App\Helpers\Document\WithTypeHelpers;
use App\Models\Filterable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
@ -19,6 +20,7 @@ class Document extends BaseModel
{
use SoftDeletes;
use Filterable;
use WithTypeHelpers;
const DOCUMENT_PREVIEW_SIZE = 300; // pixels

View File

@ -756,6 +756,10 @@ html {
$container->setAttribute('style', 'display:grid; grid-auto-flow: row; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(2, 1fr);');
foreach ($this->entity->documents as $document) {
if (!$document->isImage()) {
continue;
}
$image = $dom->createElement('img');
$image->setAttribute('src', $document->generateUrl());

View File

@ -0,0 +1,45 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace Tests\Unit;
use App\Models\Account;
use App\Models\Company;
use App\Models\Document;
use Tests\TestCase;
class WithTypeHelpersTest extends TestCase
{
public function testIsImageHelper(): void
{
$account = Account::factory()->create();
$company = Company::factory()->create([
'account_id' => $account->id,
]);
/** @var Document */
$document = Document::factory()->create([
'company_id' => $company->id,
'type' => 'jpeg',
]);
$this->assertTrue($document->isImage());
/** @var Document */
$document = Document::factory()->create([
'company_id' => $company->id,
]);
$this->assertFalse($document->isImage());
}
}