172 lines
3.5 KiB
PHP
172 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use \Carbon\Carbon;
|
|
use App\Helpers\URLHelper;
|
|
use Ffw\SocialMediaManager\SocialMediaManager;
|
|
|
|
class Publication extends ExtendedModel
|
|
{
|
|
protected $stateChanged = false;
|
|
|
|
protected $fillable = [
|
|
'platform',
|
|
'state',
|
|
'date_to_publish',
|
|
'published_at'
|
|
];
|
|
|
|
public function noteForPublication($state)
|
|
{
|
|
switch($state)
|
|
{
|
|
// Der Post soll veröffentlicht werden
|
|
case true:
|
|
// Der Post ist noch nicht veröffentlicht
|
|
if($this->state == '')
|
|
{
|
|
// Für die Veröffentlichung vormerken
|
|
$this->changeState('PENDING');
|
|
}
|
|
break;
|
|
|
|
// Der Post soll nicht veröffentlicht werden
|
|
case false:
|
|
// Der Post kann nur zurückgezogen werden, wenn er noch nicht veröffenttlicht wurde
|
|
if($this->published_at == null && $this->state == 'PENDING')
|
|
{
|
|
$this->changeState('');
|
|
}
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function isPending()
|
|
{
|
|
if($this->state == 'PENDING')
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function isPublished()
|
|
{
|
|
if($this->state != '' && $this->state != 'PENDING')
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function publishIfReady()
|
|
{
|
|
if($this->date_to_publish <= Carbon::now() && $this->published_at == null && $this->state == 'PENDING' && $this->post->published == 1)
|
|
{
|
|
$postURL = URLHelper::create($this->post);
|
|
$result = $this->state;
|
|
$this->published_at = Carbon::now();
|
|
switch($this->platform)
|
|
{
|
|
case 'facebook':
|
|
if(getenv('APP_ENV', 'development') == 'development')
|
|
{
|
|
$result = '123456';
|
|
}
|
|
else
|
|
{
|
|
$result = SocialMediaManager::postLinkOnFacebook($postURL);
|
|
}
|
|
break;
|
|
|
|
case 'twitter':
|
|
if(getenv('APP_ENV', 'development') == 'development')
|
|
{
|
|
$result = '654321';
|
|
}
|
|
else
|
|
{
|
|
$result = SocialMediaManager::postLinkOnTwitter($postURL);
|
|
}
|
|
break;
|
|
|
|
case 'stadtanzeiger':
|
|
$result = 'published';
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
$this->changeState($result);
|
|
}
|
|
}
|
|
|
|
public function setPublishDate(Carbon $date)
|
|
{
|
|
switch($this->platform)
|
|
{
|
|
case 'stadtanzeiger':
|
|
// Bei Stadtanzeigerbeiträgen findet die Veröffentlichung zusätzlich zum vorgegebenen Veröffentlichkeitsdatum
|
|
// immer noch einen Sonntag vorher statt
|
|
$this->date_to_publish = $date->startOfWeek();
|
|
|
|
// Wenn das Datum in der Vergangenheit liegt, wird es auf den nächsten Temrin gelegt, an dem der Stadtanzeiger erscheint.
|
|
if($this->date_to_publish < Carbon::now())
|
|
{
|
|
$this->date_to_publish = Carbon::now()->addWeek()->startOfWeek();
|
|
}
|
|
break;
|
|
|
|
default:
|
|
$this->date_to_publish = $date;
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function post()
|
|
{
|
|
return $this->belongsTo('App\Models\Post');
|
|
}
|
|
|
|
public function getState()
|
|
{
|
|
return $this->state;
|
|
}
|
|
|
|
public function stateHasChanged()
|
|
{
|
|
return $this->stateChanged;
|
|
}
|
|
|
|
private function changeState($newState)
|
|
{
|
|
$this->state = $newState;
|
|
$this->stateChanged = true;
|
|
}
|
|
|
|
/**********/
|
|
/* Scopes */
|
|
/**********/
|
|
public function scopeJustValidPosts($query)
|
|
{
|
|
$query = $query->join('posts as posts999', function($join)
|
|
{
|
|
$join->on('post_id', '=', 'posts999.id');
|
|
$join->where('posts999.deleted_at', '=', null);
|
|
$join->where('posts999.published', '=', '1');
|
|
});
|
|
|
|
return $query;
|
|
}
|
|
}
|