File: /home/imensosw/www/mpl.imenso.co/app/Repositories/RecommendedArtistsRepository.php
<?php
namespace App\Repositories;
use App\Models\User;
use Illuminate\Support\Collection;
use DB;
class RecommendedArtistsRepository
{
public $user;
public $order_by = 'created_at';
public $order_direction = 'DESC';
public $take = 10;
public $skip = 0;
public $items;
public $search = "";
public function __construct()
{
$this->items = new Collection;
}
public static function forUser(User $user)
{
$repo = new self;
$repo->user = $user;
return $repo;
}
public function orderBy($field = 'published_at', $direction = 'DESC')
{
$this->order_by = $field;
$this->order_direction = $direction;
return $this;
}
public function take($count = 10)
{
$this->take = $count;
return $this;
}
public function skip($count = 0)
{
$this->skip = $count;
return $this;
}
public function iflikesearch($search)
{
$this->search = $search ;
return $this;
}
public function get()
{
if ($this->search != "") {
$search = $this->search;
$recommended_bands = User::
where('type_id', 4)
->where(function ($query) use ($search) {
$query->where('email', 'LIKE', '%' . $search . '%')
->orWhere('first_name', 'LIKE', '%' . $search . '%')
->orWhere('last_name', 'LIKE', '%' . $search . '%');
})
->get();
foreach ($recommended_bands as $k => $band) {
if ($this->user->isConnected($band) || $this->user->hasIgnoredCheckoutArtist($band->id)) {
$recommended_bands->forget($k);
}
}
} elseif ($this->user->isFan()) {
$recommended_bands = $this->user->checkoutBandsRecommends();
} else {
$recommended_bands = User::where('type_id', 2)
->where('artist_subscribed', 1)
->where('id', '!=', $this->user->id)
->get();
foreach ($recommended_bands as $k => $band) {
if ($this->user->isConnected($band) || $this->user->hasIgnoredCheckoutArtist($band->id)) {
$recommended_bands->forget($k);
}
}
}
foreach ($recommended_bands as $band) {
$this->items->push($band);
}
return $this->items;
}
public function prepareForExternalUse()
{
$flattened = new Collection;
foreach ($this->items as $item) {
if ($this->user->isFan() && $this->search == "") {
$data['type'] = $item['type'];
$data['artist_id'] = $item['recommendee']->id;
if ($item['type'] == 'standard') {
$data['profile_image'] = url("https://musicplanetliveuploads.s3.amazonaws.com".$item['recommendee']->profileImageSrc());
$data['message'] = '<p><a href="http://musicplanetlive.com/artist/' .
$item['recommender']->clean_url . '">' .
$item['recommender']->getName() .
'</a> recommended <a href="http://musicplanetlive.com/artist/' .
$item['recommendee']->clean_url . '">' .
$item['recommendee']->getName() . '</a></p>';
$data['date'] = $item['timestamp']->diffForHumans();
} elseif ($item['type'] == 'mpl') {
$data['profile_image'] = url('/images/mpl-logo-black.png');
$data['message'] = '<p>We think you might like
<a href="http://musicplanetlive.com/artist/' .
$item['recommendee']->clean_url . '">' . $item['recommendee']->getName() . '
</a></p>';
$data['date'] = $item['timestamp'];
}
if ($data['profile_image'][0] == '/' && $data['profile_image'][1] == '/') {
$data['profile_image'] = 'http:' . $data['profile_image'];
}
$data['banner'] = url("https://musicplanetliveuploads.s3.amazonaws.com".$item['recommendee']->heroImageSrc());
} else {
$data['type'] = 'mpl';
$data['artist_id'] = $item->id;
$data['profile_image'] = url('/images/mpl-logo-black.png');
$data['message'] = '<p>We think you might like
<a href="http://musicplanetlive.com/artist/' . $item->clean_url . '">' . $item->getName() . '</a></p>';
$data['date'] = '-';
if ($data['profile_image'][0] == '/' && $data['profile_image'][1] == '/') {
$data['profile_image'] = 'http:' . $data['profile_image'];
}
$data['banner'] = url("https://musicplanetliveuploads.s3.amazonaws.com". $item->heroImageSrc());
}
$flattened->push($data);
}
return $flattened;
}
}