2021-08-27 06:46:27 -04:00
|
|
|
<?php
|
2021-08-29 01:26:29 -04:00
|
|
|
namespace PonePaste\Models;
|
2021-08-27 19:24:48 -04:00
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2021-08-27 06:46:27 -04:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
class Paste extends Model {
|
2021-10-22 21:43:35 -04:00
|
|
|
public const VISIBILITY_PUBLIC = 0;
|
|
|
|
public const VISIBILITY_UNLISTED = 1;
|
|
|
|
public const VISIBILITY_PRIVATE = 2;
|
|
|
|
|
2021-08-27 06:46:27 -04:00
|
|
|
protected $table = 'pastes';
|
|
|
|
|
2021-08-29 01:26:29 -04:00
|
|
|
protected $guarded = [];
|
|
|
|
|
2021-08-27 06:46:27 -04:00
|
|
|
public function user() {
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tags() {
|
|
|
|
return $this->belongsToMany(Tag::class, 'paste_taggings');
|
|
|
|
}
|
2021-08-27 19:24:48 -04:00
|
|
|
|
2021-08-29 01:26:29 -04:00
|
|
|
public function replaceTags(array $tags) {
|
|
|
|
$this->tags()->detach();
|
|
|
|
|
|
|
|
foreach ($tags as $tagName) {
|
|
|
|
$tag = Tag::getOrCreateByName($tagName);
|
|
|
|
$this->tags()->attach($tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: We need to get rid of tagsys.
|
|
|
|
$this->tagsys = implode(',', $tags);
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
2021-08-27 19:24:48 -04:00
|
|
|
public static function getRecent(int $count = 10) : Collection {
|
|
|
|
return Paste::with('user')
|
|
|
|
->orderBy('created_at', 'DESC')
|
|
|
|
->where('visible', 0)
|
|
|
|
->limit($count)->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getRecentlyUpdated(int $count = 10) : Collection {
|
|
|
|
return Paste::with('user')
|
|
|
|
->orderBy('updated_at', 'DESC')
|
|
|
|
->where('visible', 0)
|
|
|
|
->limit($count)->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getMostViewed(int $count = 10) : Collection {
|
|
|
|
return Paste::with('user')
|
|
|
|
->orderBy('views')
|
|
|
|
->where('visible', 0)
|
|
|
|
->limit($count)->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getMonthPopular(int $count = 10) : Collection {
|
|
|
|
return Paste::with('user')
|
|
|
|
->whereRaw('MONTH(created_at) = MONTH(NOW())')
|
|
|
|
->where('visible', 0)
|
|
|
|
->orderBy('views')
|
|
|
|
->limit($count)->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getRandom(int $count = 10) : Collection {
|
|
|
|
return Paste::with('user')
|
|
|
|
->orderByRaw('RAND()')
|
|
|
|
->where('visible', 0)
|
|
|
|
->limit($count)->get();
|
|
|
|
}
|
2021-08-27 06:46:27 -04:00
|
|
|
}
|