osb/app/Models/Site.php

117 lines
2.5 KiB
PHP
Raw Normal View History

2017-12-07 23:04:02 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2017-12-07 23:04:02 +00:00
use Illuminate\Database\Eloquent\Model;
2018-07-31 04:11:00 +00:00
use Illuminate\Support\Facades\Log;
2017-12-07 23:04:02 +00:00
2021-12-17 03:59:55 +00:00
/**
* This table is used to uniquely identify the website and database behind a URL.
*/
2017-12-07 23:04:02 +00:00
class Site extends Model
{
use HasFactory;
2018-07-06 06:57:49 +00:00
public $timestamps = FALSE;
2018-07-31 04:11:00 +00:00
2021-12-17 03:59:55 +00:00
public function __get($key)
{
// If we already have a value, return it
if ($x = parent::__get($key))
return $x;
// Get the value from the details table
if (($x=$this->detail_item($key)) !== NULL)
return $x;
// Get a default value for this key
if (($value = SiteDetail::sample($key)) === NULL)
abort(500,sprintf('No default value for [%s]',$key));
// At this point our DB doesnt have this value, we'll log an alert
if ($this->exists)
Log::alert(sprintf('Site [%d] is missing value for key [%s] - providing a default [%s]',$this->site_id,$key,serialize($value)));
return $value;
}
2017-12-07 23:04:02 +00:00
/* RELATIONS */
public function country()
2018-07-31 04:11:00 +00:00
{
return $this->belongsTo(Country::class);
2018-07-31 04:11:00 +00:00
}
public function currency()
2018-08-01 07:09:38 +00:00
{
return $this->belongsTo(Currency::class);
2018-08-01 07:09:38 +00:00
}
public function details()
2018-07-31 04:11:00 +00:00
{
return $this->hasMany(SiteDetail::class,NULL,'site_id');
2018-08-07 04:26:33 +00:00
}
2018-07-31 04:11:00 +00:00
public function language()
2018-08-07 04:26:33 +00:00
{
return $this->belongsTo(Language::class);
}
2018-07-31 04:11:00 +00:00
public function taxes()
{
return $this->hasMany(Tax::class,'country_id','country_id');
}
/* ATTRIBUTES */
2018-08-07 04:26:33 +00:00
2017-12-07 23:04:02 +00:00
/**
2021-12-17 03:59:55 +00:00
* Return the site address as an array
*
2021-12-17 03:59:55 +00:00
* @return array
2017-12-07 23:04:02 +00:00
*/
public function getAddressAttribute(): array
{
return [
$this->site_address1,
$this->site_address2,
sprintf('%s %s %s',$this->site_city.(($this->site_state OR $this->site_postcode) ? ',' : ''),$this->site_state,$this->site_postcode)
];
}
/**
* Add the path to the mail logo, so it can be displayed.
*
* @return string
*/
2021-12-17 03:59:55 +00:00
public function getEmailLogoAttribute(): string
2017-12-07 23:04:02 +00:00
{
return (($x=$this->detail_item('email_logo')) !== NULL) ? '/storage/'.$x : '/image/generic/150/20/fff';
2017-12-07 23:04:02 +00:00
}
/**
* Add the path to the site logo, so it can be displayed.
*
* @param $value
* @return string
*/
2021-12-17 03:59:55 +00:00
public function getSiteLogoAttribute($value): string
2017-12-07 23:04:02 +00:00
{
return (($x=$this->detail_item('site_logo')) !== NULL) ? '/storage/'.$x : '/image/generic/150/20/fff';
2017-12-07 23:04:02 +00:00
}
2021-12-17 03:59:55 +00:00
/* METHODS */
/**
* Get a key from the site_details
*
* @param $key
* @return mixed
*/
private function detail_item($key)
{
return (($x=$this->details->search(function($item) use ($key) { return $item->key === $key; })) !== FALSE)
? $this->details->get($x)->value
: NULL;
}
2018-08-07 04:26:33 +00:00
}