1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Models/Traits/HasLogo.php
Leon Aves edd57e1ca5
Import correct Document class into HasLogo trait. (#3711)
If you aren't using the local driver for logo storage, the attempts to use Document::getDirectFileUrl in this file will fail as it tries to autoload from Traits, Document needs to be explicitly imported.
2020-05-19 09:52:28 +10:00

177 lines
3.5 KiB
PHP

<?php
namespace App\Models\Traits;
use Utils;
use Illuminate\Support\Facades\Storage;
use App\Models\Document;
/**
* Class HasLogo.
*/
trait HasLogo
{
/**
* @return bool
*/
public function hasLogo()
{
return ! empty($this->logo);
}
/**
* @return mixed
*/
public function getLogoDisk()
{
return Storage::disk(env('LOGO_FILESYSTEM', 'logos'));
}
protected function calculateLogoDetails()
{
$disk = $this->getLogoDisk();
if ($disk->exists($this->account_key.'.png')) {
$this->logo = $this->account_key.'.png';
} elseif ($disk->exists($this->account_key.'.jpg')) {
$this->logo = $this->account_key.'.jpg';
}
if (! empty($this->logo)) {
$image = imagecreatefromstring($disk->get($this->logo));
$this->logo_width = imagesx($image);
$this->logo_height = imagesy($image);
$this->logo_size = $disk->size($this->logo);
} else {
$this->logo = null;
}
$this->save();
}
/**
* @return null
*/
public function getLogoRaw()
{
if (! $this->hasLogo()) {
return null;
}
$disk = $this->getLogoDisk();
if (! $disk->exists($this->logo)) {
return null;
}
return $disk->get($this->logo);
}
/**
* @param bool $cachebuster
*
* @return null|string
*/
public function getLogoURL($cachebuster = false)
{
if (! $this->hasLogo()) {
return null;
}
$disk = $this->getLogoDisk();
$adapter = $disk->getAdapter();
if ($adapter instanceof \League\Flysystem\Adapter\Local) {
// Stored locally
$logoUrl = url('/logo/' . $this->logo);
if ($cachebuster) {
$logoUrl .= '?no_cache='.time();
}
return $logoUrl;
}
return Document::getDirectFileUrl($this->logo, $this->getLogoDisk());
}
public function getLogoPath()
{
if (! $this->hasLogo()) {
return null;
}
$disk = $this->getLogoDisk();
$adapter = $disk->getAdapter();
if ($adapter instanceof \League\Flysystem\Adapter\Local) {
return $adapter->applyPathPrefix($this->logo);
} else {
return Document::getDirectFileUrl($this->logo, $this->getLogoDisk());
}
}
/**
* @return mixed|null
*/
public function getLogoWidth()
{
if (! $this->hasLogo()) {
return null;
}
return $this->logo_width;
}
/**
* @return mixed|null
*/
public function getLogoHeight()
{
if (! $this->hasLogo()) {
return null;
}
return $this->logo_height;
}
/**
* @return float|null
*/
public function getLogoSize()
{
if (! $this->hasLogo()) {
return null;
}
return round($this->logo_size / 1000);
}
/**
* @return string|null
*/
public function getLogoName()
{
if (! $this->hasLogo()) {
return null;
}
return $this->logo;
}
/**
* @return bool
*/
public function isLogoTooLarge()
{
return $this->getLogoSize() > MAX_LOGO_FILE_SIZE;
}
public function clearLogo()
{
$this->logo = '';
$this->logo_width = 0;
$this->logo_height = 0;
$this->logo_size = 0;
}
}