Rework Site and top level tables
This commit is contained in:
@@ -4,18 +4,37 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* This table is used to uniquely identify the website and database behind a URL.
|
||||
*/
|
||||
class Site extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public $timestamps = FALSE;
|
||||
|
||||
protected $casts = [
|
||||
'address'=>'array',
|
||||
];
|
||||
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;
|
||||
}
|
||||
|
||||
/* RELATIONS */
|
||||
|
||||
@@ -41,53 +60,11 @@ class Site extends Model
|
||||
|
||||
/* ATTRIBUTES */
|
||||
|
||||
public function getAllowedKeysAttribute(): Collection
|
||||
{
|
||||
return $this->_sampledata()->keys();
|
||||
}
|
||||
|
||||
/* METHODS */
|
||||
|
||||
public function __get($key)
|
||||
{
|
||||
static $details = NULL;
|
||||
|
||||
if ($x = parent::__get($key))
|
||||
return $x;
|
||||
|
||||
// Get the value from the details table
|
||||
if (($x=$this->detail_item($key)) !== NULL)
|
||||
return $x;
|
||||
|
||||
if (is_null($details))
|
||||
$details = new SiteDetail;
|
||||
|
||||
// Get a default value for this key
|
||||
$value = $details->sample($key);
|
||||
|
||||
// At this point our DB doesnt have this value, we'll log an alert
|
||||
if ($this->exists)
|
||||
Log::alert(sprintf('Site is missing value for key [%s] - providing a default [%s]',$key,serialize($value)));
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/* GENERAL METHODS */
|
||||
|
||||
/**
|
||||
* Get a key from the site_details
|
||||
* Return the site address as an array
|
||||
*
|
||||
* @param $key
|
||||
* @return mixed
|
||||
* @return array
|
||||
*/
|
||||
private function detail_item($key)
|
||||
{
|
||||
if (($x=$this->details->search(function($item) use ($key) { return $item->key == $key; })) !== FALSE)
|
||||
return $this->details->get($x)->value;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
public function getAddressAttribute(): array
|
||||
{
|
||||
return [
|
||||
@@ -102,7 +79,7 @@ class Site extends Model
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEmailLogoAttribute()
|
||||
public function getEmailLogoAttribute(): string
|
||||
{
|
||||
return (($x=$this->detail_item('email_logo')) !== NULL) ? '/storage/'.$x : '/image/generic/150/20/fff';
|
||||
}
|
||||
@@ -113,8 +90,23 @@ class Site extends Model
|
||||
* @param $value
|
||||
* @return string
|
||||
*/
|
||||
public function getSiteLogoAttribute($value)
|
||||
public function getSiteLogoAttribute($value): string
|
||||
{
|
||||
return (($x=$this->detail_item('site_logo')) !== NULL) ? '/storage/'.$x : '/image/generic/150/20/fff';
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user