Last active
August 29, 2015 14:16
-
-
Save ryanmr/983f308c911a041d4c19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Episode extends Model { | |
protected $hidden = ['content']; | |
/* Relationships */ | |
public function related() { | |
return $this->belongsToMany('App\Episode', 'episodes_relations', 'episode_id', 'episode_related_id')->withPivot('type'); | |
} | |
public function series() { | |
return $this->belongsTo('App\Series', 'series_id'); | |
} | |
public function media() { | |
return $this->hasMany('App\EpisodeMedia', 'episode_id'); | |
} | |
public function people() { | |
return $this->belongsToMany('App\Person', 'people_episodes', 'episode_id', 'person_id')->withPivot('role');; | |
} | |
/* Scopes */ | |
public function scopeVisible($query) { | |
return $query | |
->where('hidden', '=', false) | |
->where('state', '=', 'published'); | |
} | |
public function scopeAccessible($query) { | |
return $query | |
->where('hidden', '=', false) | |
->where(function($query){ | |
$query | |
->where('state', '=', 'preview') | |
->orWhere('state', '=', 'published'); | |
}); | |
} | |
public function scopeRecent($query) { | |
return $query->orderBy('created_at', 'DESC'); | |
} | |
public function scopeWithSlug($query, $slug) { | |
return $query->whereHas('series', function($q) use ($slug) {$q->where('slug', '=', $slug);}); | |
} | |
public function getFormalName() { | |
$name = $this->name; | |
$number = $this->number; | |
$series = $this->series->name; | |
if ( $this->isFringe() && $this->hasParent() ) { | |
$id = $this->id; | |
$parent = \App\Episode::with('series') | |
->whereHas('related', function($query) use($id) { | |
$query | |
->where('type', '=', 'fringe') | |
->where('episode_related_id', '=', $id); }) | |
->first(); | |
$slug = strtoupper($parent->series->slug); | |
$pnumber = $parent->number; | |
$name = "$slug #$pnumber -- $name"; | |
} | |
$template = "$series #$number: $name"; | |
return $template; | |
} | |
public function isNew() { | |
$created_at = $this->created_at; | |
$against = strtotime("-7 days"); | |
return strtotime($created_at) > $against; | |
} | |
public function isFringe() { | |
$series = $this->series->slug; | |
return $series == 'tf'; | |
} | |
public function isParent() { | |
$id = $this->id; | |
$related = Episode::visible() | |
->whereHas('related', function($q) use ($id) { $q->where('type', '=', 'parent')->where('episode_related_id', '=', $id); }) | |
->count(); | |
dump($related); | |
return $related > 0; | |
} | |
public function hasFringe() { | |
$id = $this->id; | |
$related = Episode::visible() | |
->whereHas('related', function($q) use ($id) { $q->where('type', '=', 'fringe')->where('episode_id', '=', $id); }) | |
->count(); | |
dump($related); | |
return $related > 0; | |
} | |
public function hasParent() { | |
$id = $this->id; | |
$related = Episode::visible() | |
->whereHas('related', function($q) use ($id) { $q->where('type', '=', 'parent')->where('episode_id', '=', $id); }) | |
->count(); | |
dump($related); | |
return $related > 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment