osb/app/Models/Site.php

142 lines
2.9 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;
use Illuminate\Support\Collection;
2018-07-31 04:11:00 +00:00
use Illuminate\Support\Facades\Log;
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
2017-12-07 23:04:02 +00:00
protected $casts = [
'address'=>'array',
];
/* 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(SiteDetails::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
/* ATTRIBUTES */
2018-08-07 04:26:33 +00:00
public function getAllowedKeysAttribute(): Collection
{
return $this->_sampledata()->keys();
2018-08-07 04:26:33 +00:00
}
/* METHODS */
public function __get($key)
2020-01-21 09:59:10 +00:00
{
static $details = NULL;
2020-01-21 09:59:10 +00:00
if ($x = parent::__get($key))
return $x;
2020-01-21 09:59:10 +00:00
// Get the value from the details table
if (($x=$this->detail_item($key)) !== NULL)
return $x;
2018-08-07 04:26:33 +00:00
if (is_null($details))
$details = new SiteDetails;
2018-08-07 04:26:33 +00:00
// 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)));
2018-08-07 04:26:33 +00:00
return $value;
2018-07-31 04:11:00 +00:00
}
/* GENERAL METHODS */
2017-12-07 23:04:02 +00:00
/**
* Get a key from the site_details
*
* @param $key
* @return mixed
2017-12-07 23:04:02 +00:00
*/
private function detail_item($key)
2017-12-07 23:04:02 +00:00
{
if (($x=$this->details->search(function($item) use ($key) { return $item->key == $key; })) !== FALSE)
return $this->details->get($x)->value;
2018-07-31 04:11:00 +00:00
return NULL;
2017-12-07 23:04:02 +00:00
}
/**
* Add the path to the mail logo, so it can be displayed.
*
* @return string
*/
public function getEmailLogoAttribute()
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
*/
public function getSiteLogoAttribute($value)
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
}
// @todo - To optimize
2017-12-07 23:04:02 +00:00
private function _address()
{
$return = [];
2018-07-31 04:11:00 +00:00
if ($this->site_address1)
array_push($return,$this->site_address1);
if ($this->site_address2)
array_push($return,$this->site_address2);
if ($this->site_city)
array_push($return,sprintf('%s %s %s',$this->site_city.(($this->site_state OR $this->site_postcode) ? ',' : ''),$this->site_state,$this->site_postcode));
2017-12-07 23:04:02 +00:00
if (! $return)
$return = ['No Address'];
return $return;
}
// @todo - To optimize
2017-12-07 23:04:02 +00:00
public function address($type='plain')
{
switch ($type)
{
case 'html' : return join('<br>',$this->_address());
case 'newline': return join("\m",$this->_address());
default:
return join("\n",$this->_address());
}
}
2018-08-07 04:26:33 +00:00
}