osb/app/Models/SiteDetail.php

118 lines
2.5 KiB
PHP
Raw Normal View History

2018-07-31 04:11:00 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
2021-12-17 03:59:55 +00:00
use Illuminate\Support\Arr;
2018-07-31 04:11:00 +00:00
use Leenooks\Traits\CompositeKeys;
class SiteDetail extends Model
2018-07-31 04:11:00 +00:00
{
use CompositeKeys;
2021-12-17 03:59:55 +00:00
protected $casts = [
'social' => 'array',
'top_menu' => 'array',
];
2018-07-31 04:11:00 +00:00
public $incrementing = false;
protected $primaryKey = ['site_id','key'];
2021-12-17 03:59:55 +00:00
/**
* Sample data to use with new sites, if there is no database record
*/
private const sampleData = [
'country_id' => 61,
'currency_id' => 6,
'language_id' => 1,
'site_id' => 0,
'site_name' => 'MY OSB SITE',
'site_description'=>'Example Site',
'site_phone'=>'+0 1 2345 6789',
'site_email' => 'nobody@example.com',
'site_address1' => 'Address line 1',
'site_address2' => 'Address line 2',
'site_city' => 'City',
'site_state' => 'State',
'site_postcode' => '12345',
'social'=>[
['name'=>'facebook','url'=>'http://www.facebook.com'],
['name'=>'linkedin','url'=>'http://www.linkedin.com'],
['name'=>'twitter','url'=>'http://www.twitter.com'],
],
'top_menu'=>[
'Home'=>['name'=>'Home','url'=>'/','children'=>[
['name'=>'Link 1','url'=>'#/l2', 'children'=>[]],
['name'=>'Link 2','url'=>'#/l2', 'children'=>[]],
]],
'Option1'=>['name'=>'Option 1','url'=>'/o1','children'=>[]],
'Option2'=>['name'=>'Option 2','url'=>'/o2','children'=>[
['name'=>'O2 Link 1','url'=>'#/o2l1', 'children'=>[]],
['name'=>'O2 Link 1','url'=>'#/o2l2', 'children'=>[]],
]],
]
];
2018-07-31 04:11:00 +00:00
public $timestamps = FALSE;
/* RELATIONS */
2018-07-31 04:11:00 +00:00
public function site()
{
return $this->belongsTo(Site::class);
}
/* ATTRIBUTES */
2021-12-17 03:59:55 +00:00
/**
* Enable us to cast some values
* @param $value
* @return void
*/
public function getValueAttribute($value)
{
2021-12-17 03:59:55 +00:00
switch(Arr::get($this->casts,$this->key)) {
case 'array':
return unserialize($value);
default:
return $value;
}
}
2021-12-17 03:59:55 +00:00
/**
* Set our value, casting it if required
*
* @param $key
* @param $value
* @return string
* @throws \Exception
*/
public function setValueAttribute($value)
{
// Check that the value can be set
if (! $this->key)
throw new \Exception('Please set key first');
2021-12-17 03:59:55 +00:00
switch(Arr::get($this->casts,$this->key)) {
case 'array':
2021-12-17 03:59:55 +00:00
$value = serialize($value ?: []);
break;
}
2021-12-17 03:59:55 +00:00
$this->attributes['value'] = $value;
}
2021-12-17 03:59:55 +00:00
/* METHODS */
/**
* Get a sample key value
*
* @param $key
* @return mixed
* @throws \Exception
*/
2021-12-17 03:59:55 +00:00
public static function sample($key)
{
2021-12-17 03:59:55 +00:00
return Arr::get(self::sampleData,$key);
}
2018-07-31 04:11:00 +00:00
}