File: /home/imensosw/www/mpl.imenso.co/app/Models/MplGlobalNews.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image as ImageLibrary;
class MplGlobalNews extends Model
{
protected $casts = [
'published_at' => 'datetime',
];
public function type()
{
return $this->belongsTo(\App\Models\NewsPostType::class, 'post_type_id');
}
public function user_types()
{
return $this->belongsToMany(\App\Models\UserType::class, 'global_news_user_types', 'global_news_id', 'user_type_id');
}
public function comments()
{
return $this->morphMany(\App\Models\Comment::class, 'commentable');
}
public function likes()
{
return $this->morphMany(\App\Models\Like::class, 'likeable');
}
public function video()
{
return $this->morphOne(\App\Models\Video::class, 'videoable');
}
public function song()
{
return $this->morphOne(\App\Models\Song::class, 'songable');
}
public function images()
{
return $this->morphMany(\App\Models\Image::class, 'imageable');
}
public function thumbnail()
{
return $this->image(9);
}
public function image($image_type_id = 0)
{
if ($this->images()->where('type_id', $image_type_id)->count() > 0) {
return $this->images()->where('type_id', $image_type_id)->first();
} else {
$image = new Image;
$image->default = true;
$image->src = ImageType::findOrFail($image_type_id)->default_image;
return $image;
}
}
public function isLikedByCurrentUser()
{
if (Auth::check()) {
if ($this->likes()->where('user_id', Auth::user()->id)->count() > 0) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public function getPostDate($format)
{
return $this->published_at->format($format);
}
public function remove()
{
foreach ($this->likes as $like) {
$like->remove();
}
foreach ($this->comments as $comment) {
$comment->remove();
}
foreach ($this->images as $image) {
$image->remove();
}
$this->delete();
}
public static function addNew($data)
{
$post = new self;
$post->post_title = $data->post_title;
$post->post_content = $data->post_content;
$post->post_type_id = 1;
$new_date = str_replace('/', '-', $data->created_at_date).' '.date('H:i:s', strtotime($data->created_at_time));
$new_date = strtotime($new_date);
$new_date = date('Y-m-d H:i:s', $new_date);
$post->published_at = $new_date;
$post->save();
$post->addUserTypes($data->target);
if ($data->hasFile('thumbnail')) {
$post->addThumbnail($data->thumbnail);
}
}
public static function addGlobalNewsToNews($user_type_id, $news)
{
$last6monthDate=date('Y-m-d H:i:s', strtotime("-6 month"));
$currentDate=date('Y-m-d H:i:s');
$global_news = self::whereBetween('published_at', array($last6monthDate,$currentDate))->orderBy("published_at","desc")->get();
foreach ($global_news as $item) {
$item->status_id = 2;
$item->is_global = true;
if ($item->user_types->count() == 0) {
$news->push($item);
} else {
foreach ($item->user_types as $user_type) {
if (is_int($user_type_id)) {
if ($user_type->pivot->user_type_id == $user_type_id) {
$news->push($item);
}
} elseif (get_class($user_type_id) == 'App\User') {
if ($user_type->pivot->user_type_id == $user_type_id->type_id) {
$news->push($item);
}
}
}
}
}
$news = $news->sortByDesc(function ($news) {
return $news->published_at;
});
return $news;
}
public function addUserTypes($target_users)
{
$targets = explode(',', $target_users);
foreach ($targets as $t) {
if ($t != 0) {
\DB::insert('insert into global_news_user_types (global_news_id, user_type_id) values (?, ?)', [$this->id, $t]);
}
}
}
public function edit($data)
{
$this->post_title = $data->post_title;
$this->post_content = $data->post_content;
$this->post_type_id = 1;
$new_date = str_replace('/', '-', $data->created_at_date).' '.date('H:i:s', strtotime($data->created_at_time));
$new_date = strtotime($new_date);
$new_date = date('Y-m-d H:i:s', $new_date);
$this->published_at = $new_date;
if ($data->hasFile('thumbnail')) {
$this->addThumbnail($data->thumbnail);
}
$this->save();
}
public function addThumbnail($image)
{
$filename = $this->id.'-'.time().'-hero.'.$image->getClientOriginalExtension();
$web_location = 'news/'.$filename;
$processed_image = ImageLibrary::make($image->getRealPath());
$save = $processed_image->save();
Storage::disk('s3')->put($web_location, $save->__toString());
$this->deleteImage(9);
$this->setImage(9, $web_location);
$this->save();
}
public function deleteImage($type_id)
{
if ($this->images()->where('type_id', $type_id)->count() > 0) {
$this->images()->where('type_id', $type_id)->first()->remove();
}
}
public function setImage($type_id, $location)
{
$image = new Image;
$image->src = $location;
$image->imageable_id = $this->id;
$image->imageable_type = self::class;
$image->type_id = $type_id;
$image->save();
}
public function getShareUrl($url_encoded = true)
{
return url('/');
}
public function doLike(User $user)
{
if (Like::where('user_id', $user->id)
->where('likeable_id', $this->id)
->where('likeable_type', self::class)
->count() == 0
) {
DB::table('likes')
->insert([
'user_id' => $user->id,
'likeable_id' => $this->id,
'likeable_type' => self::class,
'created_at' => date('Y-m-d H:i:s'),
]
);
$post = $this;
//event(new UserLikesPost($user, $post));
}
}
public function undoLike(User $user)
{
if (Like::where('user_id', $user->id)
->where('likeable_id', $this->id)
->where('likeable_type', self::class)
->count() > 0
) {
$like = Like::where('user_id', $user->id)
->where('likeable_id', $this->id)
->where('likeable_type', self::class)
->first()
->remove();
}
}
}