Feuerwehr-eppingen/app/Helpers/PublishHelper.php

279 lines
8.8 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: Marco Glietsch
* Date: 10.12.2018
* Time: 11:29
*/
namespace App\Helpers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\DB;
use App\Models\Tag;
use App\Models\Post;
use App\Models\User;
use App\Models\Publication;
use App\Helpers\PostHelper;
use App\Helpers\TagHelper;
use \Carbon\Carbon;
use Mail;
use App\Jobs\MailAdminStatistics;
use App\Jobs\MailDepartmentStatistics;
use App\Jobs\MailStadtanzeiger;
use Illuminate\Support\Facades\Bus;
use Illuminate\Contracts\Bus\Dispatcher;
class PublishHelper
{
use DispatchesJobs;
static function publishTodayPosts($platforms, $testRun = false, $daysInFuture = 0)
{
$isStadtanzeiger = in_array('stadtanzeiger', $platforms);
$title = 'Veröffentlichungen in den sozialen Netzwerken';
$allPosts = array();
$debugText = '';
if($testRun == true)
{
$debugText .= "############\n";
$debugText .= "# TESTLAUF #\n";
$debugText .= "############\n";
}
$departments = Tag::select('*')
->where('type', 'abteilung')
->orderBy('name', 'ASC')
->get();
$totalCount = 0;
$template = 'inc.publish.mailSocialMedia';
$adminTemplate = 'inc.publish.mailAdminSocialMedia';
if($isStadtanzeiger)
{
$template = 'inc.publish.mailStadtanzeiger';
$adminTemplate = 'inc.publish.mailAdminStadtanzeiger';
$title = 'Veröffentlichungen im Stadtanzeiger';
}
foreach($departments as $department)
{
$publications1 = Publication::select('publications.id')
->whereState("PENDING")
->whereIn('platform', $platforms)
->where(DB::raw("DATE(`publications`.`date_to_publish`)"), '=', DB::raw("(DATE(NOW()) + INTERVAL $daysInFuture DAY)"))
->orderBy('platform')
->justValidPosts()
->join('posts as posts', function ($join)
{
$join->on('posts.id', 'publications.post_id');
})
->join('post_tag as post_tag', function ($join)
{
$join->on('post_tag.post_id', 'publications.post_id');
})
->join('tags as tags', function ($join) use ($department)
{
$join->on('tags.id', 'post_tag.tag_id')
->where('tags.type', 'abteilung')
->where('tags.id', $department->id);
})
->with('post')
->orderByRaw("FIELD(`posts`.`type` , 'dienst', 'bericht', 'veranstaltung', 'einsatz') ASC")
->get();
$ids = array();
foreach($publications1 as $p)
{
$ids[] = $p->id;
}
$publications = Publication::orderByRaw('FIELD(`id`, '.implode(',', $ids).')')->find($ids);
$count = count($publications);
$totalCount += $count;
// Veröffentlichen
$publications->each(function($publication) use ($testRun){
if($testRun == false)
{
$publication->publishIfReady();
$publication->save();
}
});
$posts = PublishHelper::preparePublications($publications, $platforms);
$allPosts[$department->name] = $posts;
PublishHelper::publishDebug($posts, $department->name);
$posts2 = null;
if($isStadtanzeiger)
{
$posts2[$department->name] = $posts;
}
else
{
$posts2 = $posts;
}
if(count($posts2) > 0)
{
PublishHelper::mailDepartmentStatistics($template, $posts2, $title, $department->name, $count, true, $debugText, $testRun);
}
if($isStadtanzeiger && $count > 0)
{
if($testRun == false)
{
echo "=> Sende Email an stadtanzeiger@eppingen.de\n";
PublishHelper::mailStadtanzeiger('stadtanzeiger@eppingen.de', $template, $posts2, $title, $department->name, $count, true, $debugText);
}
else
{
$admin = User::select('email')
->where('name', 'Admin')
->get();
PublishHelper::mailStadtanzeiger($admin[0]->email, $template, $posts2, $title, $department->name, $count, true, $debugText);
}
}
}
PublishHelper::mailAdminStatistics($adminTemplate, $allPosts, $title, $totalCount, true, $debugText);
}
static function publishNotification($postId, $platforms, $options = array())
{
$debug = false;
if(key_exists('debug', $options))
{
$debug = $options['debug'];
}
$publications = Publication::select('*')
->where('post_id', $postId)
->whereIn('platform', $platforms)
->whereNotNull('state')
->where('state', '!=', '')
->where('state', '!=', 'PENDING')
->orderBy('platform')
->justValidPosts()
->with('post')
->get();
if(count($publications))
{
$department = TagHelper::getTagNameByType($publications[0]->post, 'abteilung');
$posts = PublishHelper::preparePublications($publications, ['facebook', 'twitter']);
$count = count($posts);
if($count)
{
if($debug)
{
PublishHelper::publishDebug($posts, $department);
}
$allPosts[$department] = $posts;
$title = "Veröffentlichungen in den sozialen Netzwerken";
PublishHelper::mailDepartmentStatistics('inc.publish.mailSocialMedia', $posts, $title, $department, $count);
PublishHelper::mailAdminStatistics('inc.publish.mailAdminSocialMedia', $allPosts, $title, $count);
}
$posts = PublishHelper::preparePublications($publications, ['stadtanzeiger']);
$count = count($posts);
if($count)
{
if($debug)
{
PublishHelper::publishDebug($posts, $department);
}
$allPosts[$department] = $posts;
$title = "Veröffentlichungen im Stadtanzeiger";
PublishHelper::mailDepartmentStatistics('inc.publish.mailStadtanzeiger', $posts, $title, $department, $count);
PublishHelper::mailAdminStatistics('inc.publish.mailAdminStadtanzeiger', $allPosts, $title, $count);
// PublishHelper::mailStadtanzeiger('stadtanzeiger@eppingen.de', $template, $posts2, $title, $department->name, $count, true);
}
}
}
static function publishDebug($publications, $department)
{
$count = count($publications);
echo "Veröffentlichungen für Abteilung $department: $count\n\n";
$i = 1;
foreach($publications as $platform => $posts)
{
foreach($posts as $post)
{
echo "$i/$count\n";
echo "Platform: $post->platform\n";
echo "Abteilung: $post->department\n";
echo "Typ: $post->type\n";
echo "Titel: $post->title\n";
echo "Beschreibung: $post->content\n";
if(isset($post->link))
{
echo "Link: $post->link\n";
}
}
$i++;
}
}
static function preparePublications($publications, $platforms = array())
{
$posts = array();
foreach($publications as $publication)
{
if(in_array($publication->platform, $platforms))
{
$post = new \stdClass();
$post->platform = ucfirst($publication->platform);
$post->department = $publication->post->tag('abteilung')[0]->name;
$post->type = ucfirst($publication->post->type);
$post->title = PostHelper::getSocialMediaTitle($publication->post);
$post->content = PostHelper::getSocialMediaDescription($publication->post);
if(isset($publication->post->previewDocument->filename))
{
$post->image= config('app.url')."/uploads/".$publication->post->type."/".$publication->post->id."/".$publication->post->previewDocument->filename;
}
switch($publication->platform)
{
case "facebook":
$post->link = "https://www.facebook.com/".$publication->state;
break;
case "twitter":
$post->link = "https://twitter.com/FEppingen/status/".$publication->state;
break;
}
$posts[$post->platform][] = $post;
}
}
return $posts;
}
static function mailAdminStatistics($template, $posts, $title, $count, $debug = false, $debugText = '')
{
// $mail = new MailAdminStatistics($template, $posts, $title, $count, $debug, $debugText);
// $mail->handle();
$job = (new MailAdminStatistics($template, $posts, $title, $count, $debug, $debugText))->delay(1);
dispatch($job);
}
static function mailDepartmentStatistics($template, $posts, $title, $department, $count, $debug = false, $debugText = '', $isTestRun = false)
{
// $mail = new MailDepartmentStatistics($template, $posts, $title, $department, $count, $debug, $debugText, $isTestRun);
// $mail->handle();
$job = (new MailDepartmentStatistics($template, $posts, $title, $department, $count, $debug, $debugText, $isTestRun))->delay(1);
dispatch($job);
}
static function mailStadtanzeiger($email, $template, $posts, $title, $department, $count, $debug = false, $debugText = '')
{
$job = (new MailStadtanzeiger($email, $template, $posts, $title, $department, $count, $debug, $debugText))->delay(1);
dispatch($job);
}
}