File: /home/imensosw/www/mpl.imenso.co/app/Repositories/NewsRepository.php
<?php
namespace App\Repositories;
use App\Models\MplGlobalNews;
use App\Models\News;
use App\Models\User;
use DB;
use Illuminate\Support\Collection;
class NewsRepository extends Repository
{
public $authors = [];
public $user;
public $status_id = 2;
public $global_news = false;
public $only;
public $include_current_user = 0;
public static function forUser(User $user)
{
$repo = new self;
$repo->user = $user;
$repo->fromUsers($user->getFansFollowedIds());
return $repo;
}
public function find($id)
{
$this->items->push(News::findOrFail($id));
return $this;
}
public function fromUsers($user_ids = [])
{
$this->authors = $user_ids;
return $this;
}
public function withStatus($status_id = '2')
{
$this->status_id = $status_id;
return $this;
}
public function includingGlobalNews()
{
$this->global_news = true;
return $this;
}
public function includingUsersNews()
{
$this->include_current_user = 1;
return $this;
}
public function get()
{
if ($this->include_current_user) {
$this->authors[] = $this->user->id;
}
$news = News::whereIn('user_id', $this->authors)
->where('status_id', $this->status_id)
->orderby($this->order_by, $this->order_direction)
->skip($this->skip)
->take($this->take)
->get();
if ($this->global_news) {
//if ($_SERVER['REMOTE_ADDR'] == '62.254.150.2') {
$news = MplGlobalNews::addGlobalNewsToNews($this->user, $news);
//}
}
foreach ($news as $news_item) {
$this->items->push($news_item);
}
return $this->items;
}
public function prepareForExternalUse()
{
$flattened = new Collection;
foreach ($this->items as $item) {
$data = $item->toArray();
$data['post_date'] = $item->published_at->format('jS F Y');
$data['thumbnail'] = url("https://musicplanetliveuploads.s3.amazonaws.com".$item->thumbnail()->full_url);
if ($data['thumbnail'][0] == '/' && $data['thumbnail'][1] == '/') {
$data['thumbnail'] = 'http:'.$data['thumbnail'];
}
if ($data['post_content'] == null) {
$data['post_content'] = ' ';
}
$data['post_extract'] = strip_tags(substr($data['post_content'], 0, 250));
if (strlen(strip_tags($data['post_content'])) > 250) {
$data['post_extract'] .= '...';
}
$data['like_count'] = number_format_short($item->likes->count());
if (! is_null($item->user) && get_class($item->user) == 'App\User') {
$data['author'] = [
'id' => $item->user->id,
'name' => $item->user->getName(),
'image' => url($item->user->profileImageSrc()),
'link' => $item->user->getProfileLink(),
];
} else {
$data['author'] = [
'id' => 0,
'name' => 'MPL',
'image' => url('images/mpl-logo-black.png'),
'link' => url('/'),
];
}
if ($data['author']['image'][0] == '/' && $data['author']['image'][1] == '/') {
$data['author']['image'] = 'http:'.$data['author']['image'];
}
$data['share_url_encoded'] = $item->getShareUrl();
$data['share_url_raw'] = $item->getShareUrl(false);
//$data['comments'] = $item->comments->toArray();
$data['comment_count'] = number_format_short($item->comments->count());
$data['liked'] = DB::table('likes')
->where('likeable_id', $item->id)
->where('likeable_type', get_class($item))
->where('user_id', $this->user->id)
->count();
$flattened->push($data);
}
return $flattened;
}
}