Feuerwehr-eppingen/app/Models/Post.php

201 lines
3.9 KiB
PHP
Executable File

<?php
namespace App\Models;
use App\Models\Traits\FullTextSearch;
use App\Models\Traits\Documents;
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable;
use Illuminate\Support\Arr;
use Cviebrock\EloquentSluggable\Sluggable;
use App\Helpers\TagHelper;
use Carbon\Carbon;
use App\Helpers\PostHelper;
use App\Http\Controllers\ServiceController;
class Post extends ExtendedModel
{
use Sluggable;
use FullTextSearch;
use Documents;
protected $fillable = [
'type',
'title',
'content',
'datetime',
'user_id',
'published',
];
protected $auditInclude = [
'title',
'content',
'datetime',
'user_id'
];
protected $auditExclude = [
'type',
'published',
];
protected $searchable = [
'posts.type',
'title',
'content'
];
function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->modelType = "POST";
}
public function getContentAttribute($value)
{
$json = json_decode($value, true);
if($json)
{
$object = new \stdClass();
foreach($json as $key => $value)
{
$object->$key = $value;
}
return $object;
}
return $value;
}
public function tags()
{
return $this->belongsToMany('App\Models\Tag', 'post_tag');
}
public function tag($type)
{
return $this->belongsToMany('App\Models\Tag', 'post_tag')
->where('type', $type)->get();
}
public function posts()
{
return $this->belongsToMany('App\Models\Post', 'post_post', 'parent_id', 'post_id');
}
public function posts2()
{
return $this->belongsToMany('App\Models\Post', 'post_post', 'post_id', 'parent_id');
}
public function user()
{
return $this->hasOne('App\Models\User', 'id', 'user_id');
}
public function transformAudit(array $data): array
{
if(Arr::has($data, 'new_values.content') && Arr::has($data, 'old_values.content'))
{
$json_new = json_decode($data['new_values']['content'], true);
$json_old = json_decode($data['old_values']['content'], true);
if($json_new && $json_old)
{
foreach($json_new as $key => $new_value)
{
$old_value = $json_old[$key];
if($new_value != $old_value)
{
$data['new_values'][$key] = $new_value;
$data['old_values'][$key] = $old_value;
}
}
unset($data['new_values']['content']);
unset($data['old_values']['content']);
}
}
return $data;
}
public function save(array $options = array())
{
if(key_exists('timestamps', $options))
{
$this->timestamps = $options['timestamps'];
}
if(key_exists('slug_update', $options))
{
if($options['slug_update'] == true)
{
$this->slug = '';
}
}
if($this->getAttribute('type') == 'dienst')
{
$this->title = PostHelper::getServiceString($this);
}
parent::save($options);
}
public function sluggable()
{
return [
'slug' => [
'source' => 'title',
'includeTrashed' => true,
]
];
}
/**********************/
/* Veröffentlichungen */
/**********************/
public function publication($platform)
{
$publications = $this->hasMany('App\Models\Publication', 'post_id')
->where('platform', '=', $platform);
if($publications->exists())
{
$publication = $publications->get();
return $publication[0];
}
else
{
$publication = new Publication();
$publication->platform = $platform;
$publication->state = '';
$publication->save();
$publications->save($publication);
return $publication;
}
}
/**********/
/* Scopes */
/**********/
public function scopeIsPublished($query, $isPublished = true)
{
return $query->where('published', $isPublished);
}
public function scopeIsType($query, $type)
{
return $query->where('type', $type);
}
public function scopeWithTag($query, $type, $name)
{
$query = $query->whereHas('tags', function($q) use($type, $name){
$q->where('type', $type)
->where('name', $name);
});
return $query;
}
}