Files
nexusphp/app/Models/Torrent.php
2021-05-15 19:29:44 +08:00

137 lines
3.3 KiB
PHP

<?php
namespace App\Models;
class Torrent extends NexusModel
{
protected $fillable = [
'name', 'filename', 'save_as', 'descr', 'small_descr', 'ori_descr',
'category', 'source', 'medium', 'codec', 'standard', 'processing', 'team', 'audiocodec',
'size', 'added', 'type', 'numfiles', 'owner', 'nfo', 'sp_state', 'promotion_time_type',
'promotion_until', 'anonymous', 'url', 'pos_state', 'cache_stamp', 'picktype', 'picktime',
'last_reseed', 'pt_gen', 'tags', 'technical_info'
];
const VISIBLE_YES = 'yes';
const VISIBLE_NO = 'no';
const BANNED_YES = 'yes';
const BANNED_NO = 'no';
protected $casts = [
'added' => 'datetime'
];
public function checkIsNormal(array $fields = ['visible', 'banned'])
{
if (in_array('visible', $fields) && $this->getAttribute('visible') != self::VISIBLE_YES) {
throw new \InvalidArgumentException(sprintf('Torrent: %s is not visible.', $this->id));
}
if (in_array('banned', $fields) && $this->getAttribute('banned') == self::BANNED_YES) {
throw new \InvalidArgumentException(sprintf('Torrent: %s is banned.', $this->id));
}
return true;
}
public function user()
{
return $this->belongsTo(User::class, 'owner');
}
public function thanks()
{
return $this->hasMany(Thank::class, 'torrentid');
}
public function thank_users()
{
return $this->belongsToMany(User::class, 'thanks', 'torrentid', 'userid');
}
/**
* 同伴
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function peers()
{
return $this->hasMany(Peer::class, 'torrent');
}
/**
* 完成情况
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function snatches()
{
return $this->hasMany(Snatch::class, 'torrentid');
}
public function upload_peers()
{
return $this->peers()->where('seeder', Peer::SEEDER_YES);
}
public function download_peers()
{
return $this->peers()->where('seeder', Peer::SEEDER_NO);
}
public function finish_peers()
{
return $this->peers()->where('finishedat', '>', 0);
}
public function files()
{
return $this->hasMany(File::class, 'torrent');
}
public function basic_category()
{
return $this->belongsTo(Category::class, 'category');
}
public function basic_source()
{
return $this->belongsTo(Source::class, 'source');
}
public function basic_media()
{
return $this->belongsTo(Media::class, 'medium');
}
public function basic_codec()
{
return $this->belongsTo(Codec::class, 'codec');
}
public function basic_standard()
{
return $this->belongsTo(Standard::class, 'standard');
}
public function basic_processing()
{
return $this->belongsTo(Processing::class, 'processing');
}
public function basic_team()
{
return $this->belongsTo(Team::class, 'team');
}
public function basic_audiocodec()
{
return $this->belongsTo(AudioCodec::class, 'audiocodec');
}
public function scopeVisible($query, $visible = self::VISIBLE_YES)
{
$query->where('visible', $visible);
}
}